blob: 0ed3e7674353bc82bd0bb7baa6bbbf615866129c [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Christoph Hellwig98c1a7c02018-07-11 22:26:06 -07004 * Copyright (c) 2016-2018 Christoph Hellwig.
Nathan Scott7b718762005-11-02 14:58:39 +11005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11009#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_format.h"
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110015#include "xfs_btree.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110016#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100018#include "xfs_bmap_util.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070019#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_error.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_trans_space.h"
Christoph Hellwiga39e5962017-11-01 16:36:47 +010023#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_iomap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000025#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110026#include "xfs_quota.h"
Brian Foster76a42022013-03-18 10:51:47 -040027#include "xfs_dquot_item.h"
28#include "xfs_dquot.h"
Darrick J. Wong2a067052016-10-03 09:11:33 -070029#include "xfs_reflink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Christoph Hellwig5da8a072019-10-28 08:41:44 -070031#define XFS_ALLOC_ALIGN(mp, off) \
32 (((off) >> mp->m_allocsize_log) << mp->m_allocsize_log)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Christoph Hellwig16be1432019-02-18 09:38:46 -080034static int
35xfs_alert_fsblock_zero(
36 xfs_inode_t *ip,
37 xfs_bmbt_irec_t *imap)
38{
39 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
40 "Access to block zero in inode %llu "
41 "start_block: %llx start_off: %llx "
42 "blkcnt: %llx extent-state: %x",
43 (unsigned long long)ip->i_ino,
44 (unsigned long long)imap->br_startblock,
45 (unsigned long long)imap->br_startoff,
46 (unsigned long long)imap->br_blockcount,
47 imap->br_state);
48 return -EFSCORRUPTED;
49}
50
51int
Christoph Hellwige9c49732016-09-19 11:09:12 +100052xfs_bmbt_to_iomap(
53 struct xfs_inode *ip,
54 struct iomap *iomap,
Christoph Hellwig16be1432019-02-18 09:38:46 -080055 struct xfs_bmbt_irec *imap,
Christoph Hellwig740fd672021-11-29 11:21:57 +010056 unsigned int mapping_flags,
57 u16 iomap_flags)
Christoph Hellwige9c49732016-09-19 11:09:12 +100058{
59 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig30fa5292019-10-24 22:25:38 -070060 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
Christoph Hellwige9c49732016-09-19 11:09:12 +100061
Christoph Hellwigeb77b232019-09-03 08:13:13 -070062 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
Christoph Hellwig16be1432019-02-18 09:38:46 -080063 return xfs_alert_fsblock_zero(ip, imap);
64
Christoph Hellwige9c49732016-09-19 11:09:12 +100065 if (imap->br_startblock == HOLESTARTBLOCK) {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040066 iomap->addr = IOMAP_NULL_ADDR;
Christoph Hellwige9c49732016-09-19 11:09:12 +100067 iomap->type = IOMAP_HOLE;
Christoph Hellwig16be1432019-02-18 09:38:46 -080068 } else if (imap->br_startblock == DELAYSTARTBLOCK ||
69 isnullstartblock(imap->br_startblock)) {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040070 iomap->addr = IOMAP_NULL_ADDR;
Christoph Hellwige9c49732016-09-19 11:09:12 +100071 iomap->type = IOMAP_DELALLOC;
72 } else {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040073 iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
Christoph Hellwige9c49732016-09-19 11:09:12 +100074 if (imap->br_state == XFS_EXT_UNWRITTEN)
75 iomap->type = IOMAP_UNWRITTEN;
76 else
77 iomap->type = IOMAP_MAPPED;
78 }
79 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
80 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
Christoph Hellwig30fa5292019-10-24 22:25:38 -070081 iomap->bdev = target->bt_bdev;
82 iomap->dax_dev = target->bt_daxdev;
Christoph Hellwig740fd672021-11-29 11:21:57 +010083 iomap->flags = iomap_flags;
Christoph Hellwig16be1432019-02-18 09:38:46 -080084
85 if (xfs_ipincount(ip) &&
86 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
87 iomap->flags |= IOMAP_F_DIRTY;
Christoph Hellwig16be1432019-02-18 09:38:46 -080088 return 0;
Christoph Hellwige9c49732016-09-19 11:09:12 +100089}
90
Christoph Hellwig0365c5d2018-10-18 17:19:26 +110091static void
92xfs_hole_to_iomap(
93 struct xfs_inode *ip,
94 struct iomap *iomap,
95 xfs_fileoff_t offset_fsb,
96 xfs_fileoff_t end_fsb)
97{
Christoph Hellwig30fa5292019-10-24 22:25:38 -070098 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
99
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100100 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);
Christoph Hellwig30fa5292019-10-24 22:25:38 -0700104 iomap->bdev = target->bt_bdev;
105 iomap->dax_dev = target->bt_daxdev;
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100106}
107
Christoph Hellwig43568222019-10-19 09:09:44 -0700108static inline xfs_fileoff_t
109xfs_iomap_end_fsb(
110 struct xfs_mount *mp,
111 loff_t offset,
112 loff_t count)
113{
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));
117}
118
Christoph Hellwig49bbf8c2019-10-30 12:24:57 -0700119static xfs_extlen_t
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000120xfs_eof_alignment(
Christoph Hellwig57c49442019-10-30 12:24:58 -0700121 struct xfs_inode *ip)
Nathan Scottdd9f4382006-01-11 15:28:28 +1100122{
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000123 struct xfs_mount *mp = ip->i_mount;
124 xfs_extlen_t align = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100125
Christoph Hellwigbf322d92011-12-18 20:00:05 +0000126 if (!XFS_IS_REALTIME_INODE(ip)) {
127 /*
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.
131 *
132 * If mounted with the "-o swalloc" option the alignment is
133 * increased from the strip unit size to the stripe width.
134 */
Dave Chinner0560f312021-08-18 18:46:52 -0700135 if (mp->m_swidth && xfs_has_swalloc(mp))
Christoph Hellwigbf322d92011-12-18 20:00:05 +0000136 align = mp->m_swidth;
137 else if (mp->m_dalign)
138 align = mp->m_dalign;
139
Peter Watkins76b57302014-12-04 09:30:51 +1100140 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
141 align = 0;
Christoph Hellwigbf322d92011-12-18 20:00:05 +0000142 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100143
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000144 return align;
145}
146
Christoph Hellwigae7e4032019-10-30 12:24:57 -0700147/*
148 * Check if last_fsb is outside the last extent, and if so grow it to the next
149 * stripe unit boundary.
150 */
Christoph Hellwige6966632019-10-30 12:24:59 -0700151xfs_fileoff_t
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000152xfs_iomap_eof_align_last_fsb(
153 struct xfs_inode *ip,
Christoph Hellwigae7e4032019-10-30 12:24:57 -0700154 xfs_fileoff_t end_fsb)
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000155{
Christoph Hellwigae7e4032019-10-30 12:24:57 -0700156 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
157 xfs_extlen_t extsz = xfs_get_extsz_hint(ip);
Christoph Hellwig57c49442019-10-30 12:24:58 -0700158 xfs_extlen_t align = xfs_eof_alignment(ip);
Christoph Hellwigae7e4032019-10-30 12:24:57 -0700159 struct xfs_bmbt_irec irec;
160 struct xfs_iext_cursor icur;
161
Christoph Hellwigb2197a32021-04-13 11:15:12 -0700162 ASSERT(!xfs_need_iread_extents(ifp));
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000163
Christoph Hellwig57c49442019-10-30 12:24:58 -0700164 /*
165 * Always round up the allocation request to the extent hint boundary.
166 */
167 if (extsz) {
168 if (align)
169 align = roundup_64(align, extsz);
170 else
171 align = extsz;
172 }
173
Peter Watkins76b57302014-12-04 09:30:51 +1100174 if (align) {
Christoph Hellwigae7e4032019-10-30 12:24:57 -0700175 xfs_fileoff_t aligned_end_fsb = roundup_64(end_fsb, align);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000176
Christoph Hellwigae7e4032019-10-30 12:24:57 -0700177 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;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100181 }
Christoph Hellwigae7e4032019-10-30 12:24:57 -0700182
183 return end_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100184}
185
Christoph Hellwiga206c812010-12-10 08:42:20 +0000186int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187xfs_iomap_write_direct(
Christoph Hellwige6966632019-10-30 12:24:59 -0700188 struct xfs_inode *ip,
189 xfs_fileoff_t offset_fsb,
190 xfs_fileoff_t count_fsb,
Christoph Hellwig952da062021-11-29 11:21:58 +0100191 unsigned int flags,
Christoph Hellwige6966632019-10-30 12:24:59 -0700192 struct xfs_bmbt_irec *imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Christoph Hellwige6966632019-10-30 12:24:59 -0700194 struct xfs_mount *mp = ip->i_mount;
195 struct xfs_trans *tp;
196 xfs_filblks_t resaligned;
197 int nimaps;
Darrick J. Wong02b7ee42021-01-26 17:20:42 -0800198 unsigned int dblocks, rblocks;
Darrick J. Wong3de4eb12021-01-26 16:44:07 -0800199 bool force = false;
Christoph Hellwige6966632019-10-30 12:24:59 -0700200 int error;
201 int bmapi_flags = XFS_BMAPI_PREALLOC;
Chandan Babu R5147ef32021-03-25 11:48:18 -0700202 int nr_exts = XFS_IEXT_ADD_NOSPLIT_CNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Nathan Scottdd9f4382006-01-11 15:28:28 +1100204 ASSERT(count_fsb > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100205
Christoph Hellwige6966632019-10-30 12:24:59 -0700206 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb,
207 xfs_get_extsz_hint(ip));
208 if (unlikely(XFS_IS_REALTIME_INODE(ip))) {
Darrick J. Wong02b7ee42021-01-26 17:20:42 -0800209 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
210 rblocks = resaligned;
David Chinner84e1e992007-06-18 16:50:27 +1000211 } else {
Darrick J. Wong02b7ee42021-01-26 17:20:42 -0800212 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
213 rblocks = 0;
David Chinner84e1e992007-06-18 16:50:27 +1000214 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100215
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700216 error = xfs_qm_dqattach(ip);
Brian Foster009c6e82015-10-12 15:34:20 +1100217 if (error)
218 return error;
219
220 /*
Dave Chinner1ca19152015-11-03 12:37:00 +1100221 * For DAX, we do not allocate unwritten extents, but instead we zero
222 * the block before we commit the transaction. Ideally we'd like to do
223 * this outside the transaction context, but if we commit and then crash
224 * we may not have zeroed the blocks and this will be exposed on
225 * recovery of the allocation. Hence we must zero before commit.
Dave Chinner3b0fe472016-01-04 16:22:45 +1100226 *
Dave Chinner1ca19152015-11-03 12:37:00 +1100227 * Further, if we are mapping unwritten extents here, we need to zero
228 * and convert them to written so that we don't need an unwritten extent
229 * callback for DAX. This also means that we need to be able to dip into
Dave Chinner3b0fe472016-01-04 16:22:45 +1100230 * the reserve block pool for bmbt block allocation if there is no space
231 * left but we need to do unwritten extent conversion.
Dave Chinner1ca19152015-11-03 12:37:00 +1100232 */
Christoph Hellwig952da062021-11-29 11:21:58 +0100233 if (flags & IOMAP_DAX) {
Dave Chinner1ca19152015-11-03 12:37:00 +1100234 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
Christoph Hellwig63fbb4c2017-03-28 14:53:36 -0700235 if (imap->br_state == XFS_EXT_UNWRITTEN) {
Darrick J. Wong02b7ee42021-01-26 17:20:42 -0800236 force = true;
Chandan Babu R5147ef32021-03-25 11:48:18 -0700237 nr_exts = XFS_IEXT_WRITE_UNWRITTEN_CNT;
Darrick J. Wong02b7ee42021-01-26 17:20:42 -0800238 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Dave Chinner3b0fe472016-01-04 16:22:45 +1100239 }
Dave Chinner1ca19152015-11-03 12:37:00 +1100240 }
Darrick J. Wong3de4eb12021-01-26 16:44:07 -0800241
242 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, dblocks,
243 rblocks, force, &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000244 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000245 return error;
Dave Chinner507630b2012-03-27 10:34:50 -0400246
Chandan Babu R5147ef32021-03-25 11:48:18 -0700247 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, nr_exts);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100248 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400249 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000252 * From this point onwards we overwrite the imap pointer that the
253 * caller gave to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Nathan Scott06d10dd2005-06-21 15:48:47 +1000255 nimaps = 1;
Brian Fosterda781e62019-10-21 09:26:48 -0700256 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, bmapi_flags, 0,
257 imap, &nimaps);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000258 if (error)
Darrick J. Wong35b110102021-01-26 17:23:30 -0800259 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000262 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
Christoph Hellwig70393312015-06-04 13:48:08 +1000264 error = xfs_trans_commit(tp);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000265 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400266 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Nathan Scott06d10dd2005-06-21 15:48:47 +1000268 /*
269 * Copy any maps to caller's array and return any error.
270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (nimaps == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000272 error = -ENOSPC;
Dave Chinner507630b2012-03-27 10:34:50 -0400273 goto out_unlock;
Nathan Scott572d95f2006-09-28 11:03:20 +1000274 }
275
Christoph Hellwigeb77b232019-09-03 08:13:13 -0700276 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100277 error = xfs_alert_fsblock_zero(ip, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Dave Chinner507630b2012-03-27 10:34:50 -0400279out_unlock:
Christoph Hellwige6966632019-10-30 12:24:59 -0700280 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner507630b2012-03-27 10:34:50 -0400281 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Dave Chinner507630b2012-03-27 10:34:50 -0400283out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000284 xfs_trans_cancel(tp);
Dave Chinner507630b2012-03-27 10:34:50 -0400285 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Brian Foster76a42022013-03-18 10:51:47 -0400288STATIC bool
289xfs_quota_need_throttle(
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700290 struct xfs_inode *ip,
291 xfs_dqtype_t type,
292 xfs_fsblock_t alloc_blocks)
Brian Foster76a42022013-03-18 10:51:47 -0400293{
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700294 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
Brian Foster76a42022013-03-18 10:51:47 -0400295
296 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
297 return false;
298
299 /* no hi watermark, no throttle */
300 if (!dq->q_prealloc_hi_wmark)
301 return false;
302
303 /* under the lo watermark, no throttle */
Darrick J. Wong784e80f2020-07-14 10:37:30 -0700304 if (dq->q_blk.reserved + alloc_blocks < dq->q_prealloc_lo_wmark)
Brian Foster76a42022013-03-18 10:51:47 -0400305 return false;
306
307 return true;
308}
309
310STATIC void
311xfs_quota_calc_throttle(
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700312 struct xfs_inode *ip,
313 xfs_dqtype_t type,
314 xfs_fsblock_t *qblocks,
315 int *qshift,
316 int64_t *qfreesp)
Brian Foster76a42022013-03-18 10:51:47 -0400317{
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700318 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
319 int64_t freesp;
320 int shift = 0;
Brian Foster76a42022013-03-18 10:51:47 -0400321
Eric Sandeen5cca3f62014-10-02 09:27:09 +1000322 /* no dq, or over hi wmark, squash the prealloc completely */
Darrick J. Wong784e80f2020-07-14 10:37:30 -0700323 if (!dq || dq->q_blk.reserved >= dq->q_prealloc_hi_wmark) {
Brian Foster76a42022013-03-18 10:51:47 -0400324 *qblocks = 0;
Brian Fosterf0740512014-07-24 19:56:08 +1000325 *qfreesp = 0;
Brian Foster76a42022013-03-18 10:51:47 -0400326 return;
327 }
328
Darrick J. Wong784e80f2020-07-14 10:37:30 -0700329 freesp = dq->q_prealloc_hi_wmark - dq->q_blk.reserved;
Brian Foster76a42022013-03-18 10:51:47 -0400330 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
331 shift = 2;
332 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
333 shift += 2;
334 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
335 shift += 2;
336 }
337
Brian Fosterf0740512014-07-24 19:56:08 +1000338 if (freesp < *qfreesp)
339 *qfreesp = freesp;
340
Brian Foster76a42022013-03-18 10:51:47 -0400341 /* only overwrite the throttle values if we are more aggressive */
342 if ((freesp >> shift) < (*qblocks >> *qshift)) {
343 *qblocks = freesp;
344 *qshift = shift;
345 }
346}
347
Dave Chinnera1e16c22013-02-11 16:05:01 +1100348/*
Dave Chinner055388a2011-01-04 11:35:03 +1100349 * If we don't have a user specified preallocation size, dynamically increase
Christoph Hellwig51446f52016-09-19 11:10:21 +1000350 * the preallocation size as the size of the file grows. Cap the maximum size
Dave Chinner055388a2011-01-04 11:35:03 +1100351 * at a single extent or less if the filesystem is near full. The closer the
Darrick J. Wong590b1652020-05-23 09:43:30 -0700352 * filesystem is to being full, the smaller the maximum preallocation.
Dave Chinner055388a2011-01-04 11:35:03 +1100353 */
354STATIC xfs_fsblock_t
355xfs_iomap_prealloc_size(
Dave Chinnera1e16c22013-02-11 16:05:01 +1100356 struct xfs_inode *ip,
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800357 int whichfork,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000358 loff_t offset,
359 loff_t count,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700360 struct xfs_iext_cursor *icur)
Dave Chinner055388a2011-01-04 11:35:03 +1100361{
Darrick J. Wongf0322c72020-05-23 09:43:30 -0700362 struct xfs_iext_cursor ncur = *icur;
363 struct xfs_bmbt_irec prev, got;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000364 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800365 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000366 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400367 int64_t freesp;
Brian Foster76a42022013-03-18 10:51:47 -0400368 xfs_fsblock_t qblocks;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000369 xfs_fsblock_t alloc_blocks = 0;
Darrick J. Wongf0322c72020-05-23 09:43:30 -0700370 xfs_extlen_t plen;
371 int shift = 0;
372 int qshift = 0;
Dave Chinner055388a2011-01-04 11:35:03 +1100373
Darrick J. Wong590b1652020-05-23 09:43:30 -0700374 /*
375 * As an exception we don't do any preallocation at all if the file is
376 * smaller than the minimum preallocation and we are using the default
377 * dynamic preallocation scheme, as it is likely this is the only write
378 * to the file that is going to be done.
379 */
380 if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_allocsize_blocks))
Christoph Hellwig51446f52016-09-19 11:10:21 +1000381 return 0;
382
383 /*
Darrick J. Wong590b1652020-05-23 09:43:30 -0700384 * Use the minimum preallocation size for small files or if we are
385 * writing right after a hole.
Christoph Hellwig51446f52016-09-19 11:10:21 +1000386 */
Darrick J. Wong590b1652020-05-23 09:43:30 -0700387 if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
Darrick J. Wongf0322c72020-05-23 09:43:30 -0700388 !xfs_iext_prev_extent(ifp, &ncur, &prev) ||
Christoph Hellwig656152e2016-11-24 11:39:44 +1100389 prev.br_startoff + prev.br_blockcount < offset_fsb)
Christoph Hellwig5da8a072019-10-28 08:41:44 -0700390 return mp->m_allocsize_blocks;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000391
392 /*
Darrick J. Wong590b1652020-05-23 09:43:30 -0700393 * Take the size of the preceding data extents as the basis for the
394 * preallocation size. Note that we don't care if the previous extents
395 * are written or not.
Christoph Hellwig51446f52016-09-19 11:10:21 +1000396 */
Darrick J. Wongf0322c72020-05-23 09:43:30 -0700397 plen = prev.br_blockcount;
398 while (xfs_iext_prev_extent(ifp, &ncur, &got)) {
399 if (plen > MAXEXTLEN / 2 ||
400 isnullstartblock(got.br_startblock) ||
401 got.br_startoff + got.br_blockcount != prev.br_startoff ||
402 got.br_startblock + got.br_blockcount != prev.br_startblock)
403 break;
404 plen += got.br_blockcount;
405 prev = got;
406 }
Darrick J. Wong590b1652020-05-23 09:43:30 -0700407
408 /*
409 * If the size of the extents is greater than half the maximum extent
410 * length, then use the current offset as the basis. This ensures that
411 * for large files the preallocation size always extends to MAXEXTLEN
412 * rather than falling short due to things like stripe unit/width
413 * alignment of real extents.
414 */
Darrick J. Wongf0322c72020-05-23 09:43:30 -0700415 alloc_blocks = plen * 2;
416 if (alloc_blocks > MAXEXTLEN)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000417 alloc_blocks = XFS_B_TO_FSB(mp, offset);
Brian Foster76a42022013-03-18 10:51:47 -0400418 qblocks = alloc_blocks;
Dave Chinner055388a2011-01-04 11:35:03 +1100419
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400420 /*
421 * MAXEXTLEN is not a power of two value but we round the prealloc down
422 * to the nearest power of two value after throttling. To prevent the
Darrick J. Wong590b1652020-05-23 09:43:30 -0700423 * round down from unconditionally reducing the maximum supported
424 * prealloc size, we round up first, apply appropriate throttling,
425 * round down and cap the value to MAXEXTLEN.
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400426 */
427 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
428 alloc_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100429
Dave Chinner0d485ad2015-02-23 21:22:03 +1100430 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400431 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
432 shift = 2;
433 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
434 shift++;
435 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
436 shift++;
437 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
438 shift++;
439 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
440 shift++;
Dave Chinner055388a2011-01-04 11:35:03 +1100441 }
Brian Foster76a42022013-03-18 10:51:47 -0400442
443 /*
Brian Fosterf0740512014-07-24 19:56:08 +1000444 * Check each quota to cap the prealloc size, provide a shift value to
445 * throttle with and adjust amount of available space.
Brian Foster76a42022013-03-18 10:51:47 -0400446 */
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700447 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_USER, alloc_blocks))
448 xfs_quota_calc_throttle(ip, XFS_DQTYPE_USER, &qblocks, &qshift,
Brian Fosterf0740512014-07-24 19:56:08 +1000449 &freesp);
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700450 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_GROUP, alloc_blocks))
451 xfs_quota_calc_throttle(ip, XFS_DQTYPE_GROUP, &qblocks, &qshift,
Brian Fosterf0740512014-07-24 19:56:08 +1000452 &freesp);
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700453 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_PROJ, alloc_blocks))
454 xfs_quota_calc_throttle(ip, XFS_DQTYPE_PROJ, &qblocks, &qshift,
Brian Fosterf0740512014-07-24 19:56:08 +1000455 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400456
457 /*
458 * The final prealloc size is set to the minimum of free space available
459 * in each of the quotas and the overall filesystem.
460 *
461 * The shift throttle value is set to the maximum value as determined by
462 * the global low free space values and per-quota low free space values.
463 */
Dave Chinner9bb54cb2018-06-07 07:54:02 -0700464 alloc_blocks = min(alloc_blocks, qblocks);
465 shift = max(shift, qshift);
Brian Foster76a42022013-03-18 10:51:47 -0400466
Brian Foster3c58b5f2013-03-18 10:51:43 -0400467 if (shift)
468 alloc_blocks >>= shift;
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400469 /*
470 * rounddown_pow_of_two() returns an undefined result if we pass in
471 * alloc_blocks = 0.
472 */
473 if (alloc_blocks)
474 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
475 if (alloc_blocks > MAXEXTLEN)
476 alloc_blocks = MAXEXTLEN;
Dave Chinner055388a2011-01-04 11:35:03 +1100477
Brian Foster3c58b5f2013-03-18 10:51:43 -0400478 /*
479 * If we are still trying to allocate more space than is
480 * available, squash the prealloc hard. This can happen if we
481 * have a large file on a small filesystem and the above
482 * lowspace thresholds are smaller than MAXEXTLEN.
483 */
484 while (alloc_blocks && alloc_blocks >= freesp)
485 alloc_blocks >>= 4;
Christoph Hellwig5da8a072019-10-28 08:41:44 -0700486 if (alloc_blocks < mp->m_allocsize_blocks)
487 alloc_blocks = mp->m_allocsize_blocks;
Brian Foster19cb7e32013-03-18 10:51:48 -0400488 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
Christoph Hellwig5da8a072019-10-28 08:41:44 -0700489 mp->m_allocsize_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100490 return alloc_blocks;
491}
492
Christoph Hellwiga526c852019-10-19 09:09:46 -0700493int
494xfs_iomap_write_unwritten(
495 xfs_inode_t *ip,
496 xfs_off_t offset,
497 xfs_off_t count,
498 bool update_isize)
499{
500 xfs_mount_t *mp = ip->i_mount;
501 xfs_fileoff_t offset_fsb;
502 xfs_filblks_t count_fsb;
503 xfs_filblks_t numblks_fsb;
504 int nimaps;
505 xfs_trans_t *tp;
506 xfs_bmbt_irec_t imap;
507 struct inode *inode = VFS_I(ip);
508 xfs_fsize_t i_size;
509 uint resblks;
510 int error;
511
512 trace_xfs_unwritten_convert(ip, offset, count);
513
514 offset_fsb = XFS_B_TO_FSBT(mp, offset);
515 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
516 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
517
518 /*
519 * Reserve enough blocks in this transaction for two complete extent
520 * btree splits. We may be converting the middle part of an unwritten
521 * extent and in this case we will insert two new extents in the btree
522 * each of which could cause a full split.
523 *
524 * This reservation amount will be used in the first call to
525 * xfs_bmbt_split() to select an AG with enough space to satisfy the
526 * rest of the operation.
527 */
528 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
529
Darrick J. Wong2815a162019-11-08 23:04:20 -0800530 /* Attach dquots so that bmbt splits are accounted correctly. */
531 error = xfs_qm_dqattach(ip);
532 if (error)
533 return error;
534
Christoph Hellwiga526c852019-10-19 09:09:46 -0700535 do {
536 /*
537 * Set up a transaction to convert the range of extents
538 * from unwritten to real. Do allocations in a loop until
539 * we have covered the range passed in.
540 *
541 * Note that we can't risk to recursing back into the filesystem
542 * here as we might be asked to write out the same inode that we
543 * complete here and might deadlock on the iolock.
544 */
Darrick J. Wong3a1af6c2021-01-26 16:33:29 -0800545 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks,
Darrick J. Wong3de4eb12021-01-26 16:44:07 -0800546 0, true, &tp);
Christoph Hellwiga526c852019-10-19 09:09:46 -0700547 if (error)
548 return error;
549
Chandan Babu Rc442f302021-01-22 16:48:14 -0800550 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
551 XFS_IEXT_WRITE_UNWRITTEN_CNT);
Darrick J. Wong2815a162019-11-08 23:04:20 -0800552 if (error)
553 goto error_on_bmapi_transaction;
554
Christoph Hellwiga526c852019-10-19 09:09:46 -0700555 /*
556 * Modify the unwritten extent state of the buffer.
557 */
558 nimaps = 1;
559 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
560 XFS_BMAPI_CONVERT, resblks, &imap,
561 &nimaps);
562 if (error)
563 goto error_on_bmapi_transaction;
564
565 /*
566 * Log the updated inode size as we go. We have to be careful
567 * to only log it up to the actual write offset if it is
568 * halfway into a block.
569 */
570 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
571 if (i_size > offset + count)
572 i_size = offset + count;
573 if (update_isize && i_size > i_size_read(inode))
574 i_size_write(inode, i_size);
575 i_size = xfs_new_eof(ip, i_size);
576 if (i_size) {
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700577 ip->i_disk_size = i_size;
Christoph Hellwiga526c852019-10-19 09:09:46 -0700578 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
579 }
580
581 error = xfs_trans_commit(tp);
582 xfs_iunlock(ip, XFS_ILOCK_EXCL);
583 if (error)
584 return error;
585
586 if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock)))
587 return xfs_alert_fsblock_zero(ip, &imap);
588
589 if ((numblks_fsb = imap.br_blockcount) == 0) {
590 /*
591 * The numblks_fsb value should always get
592 * smaller, otherwise the loop is stuck.
593 */
594 ASSERT(imap.br_blockcount);
595 break;
596 }
597 offset_fsb += numblks_fsb;
598 count_fsb -= numblks_fsb;
599 } while (count_fsb > 0);
600
601 return 0;
602
603error_on_bmapi_transaction:
604 xfs_trans_cancel(tp);
605 xfs_iunlock(ip, XFS_ILOCK_EXCL);
606 return error;
607}
608
609static inline bool
610imap_needs_alloc(
611 struct inode *inode,
Christoph Hellwig5c5b6f72019-10-19 09:09:47 -0700612 unsigned flags,
Christoph Hellwiga526c852019-10-19 09:09:46 -0700613 struct xfs_bmbt_irec *imap,
614 int nimaps)
615{
Christoph Hellwig5c5b6f72019-10-19 09:09:47 -0700616 /* don't allocate blocks when just zeroing */
617 if (flags & IOMAP_ZERO)
618 return false;
619 if (!nimaps ||
620 imap->br_startblock == HOLESTARTBLOCK ||
621 imap->br_startblock == DELAYSTARTBLOCK)
622 return true;
623 /* we convert unwritten extents before copying the data for DAX */
Christoph Hellwig952da062021-11-29 11:21:58 +0100624 if ((flags & IOMAP_DAX) && imap->br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig5c5b6f72019-10-19 09:09:47 -0700625 return true;
626 return false;
Christoph Hellwiga526c852019-10-19 09:09:46 -0700627}
628
629static inline bool
Christoph Hellwig5c5b6f72019-10-19 09:09:47 -0700630imap_needs_cow(
631 struct xfs_inode *ip,
632 unsigned int flags,
Christoph Hellwiga526c852019-10-19 09:09:46 -0700633 struct xfs_bmbt_irec *imap,
634 int nimaps)
635{
Christoph Hellwig5c5b6f72019-10-19 09:09:47 -0700636 if (!xfs_is_cow_inode(ip))
637 return false;
638
639 /* when zeroing we don't have to COW holes or unwritten extents */
640 if (flags & IOMAP_ZERO) {
641 if (!nimaps ||
642 imap->br_startblock == HOLESTARTBLOCK ||
643 imap->br_state == XFS_EXT_UNWRITTEN)
644 return false;
645 }
646
647 return true;
Christoph Hellwiga526c852019-10-19 09:09:46 -0700648}
649
650static int
651xfs_ilock_for_iomap(
652 struct xfs_inode *ip,
653 unsigned flags,
654 unsigned *lockmode)
655{
656 unsigned mode = XFS_ILOCK_SHARED;
657 bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
658
659 /*
660 * COW writes may allocate delalloc space or convert unwritten COW
661 * extents, so we need to make sure to take the lock exclusively here.
662 */
Christoph Hellwig1e190f82019-10-19 09:09:47 -0700663 if (xfs_is_cow_inode(ip) && is_write)
Christoph Hellwiga526c852019-10-19 09:09:46 -0700664 mode = XFS_ILOCK_EXCL;
Christoph Hellwiga526c852019-10-19 09:09:46 -0700665
666 /*
667 * Extents not yet cached requires exclusive access, don't block. This
668 * is an opencoded xfs_ilock_data_map_shared() call but with
669 * non-blocking behaviour.
670 */
Christoph Hellwigb2197a32021-04-13 11:15:12 -0700671 if (xfs_need_iread_extents(&ip->i_df)) {
Christoph Hellwiga526c852019-10-19 09:09:46 -0700672 if (flags & IOMAP_NOWAIT)
673 return -EAGAIN;
674 mode = XFS_ILOCK_EXCL;
675 }
676
677relock:
678 if (flags & IOMAP_NOWAIT) {
679 if (!xfs_ilock_nowait(ip, mode))
680 return -EAGAIN;
681 } else {
682 xfs_ilock(ip, mode);
683 }
684
685 /*
686 * The reflink iflag could have changed since the earlier unlocked
687 * check, so if we got ILOCK_SHARED for a write and but we're now a
688 * reflink inode we have to switch to ILOCK_EXCL and relock.
689 */
690 if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_cow_inode(ip)) {
691 xfs_iunlock(ip, mode);
692 mode = XFS_ILOCK_EXCL;
693 goto relock;
694 }
695
696 *lockmode = mode;
697 return 0;
698}
699
Dave Chinner883a7902020-11-19 08:59:11 -0800700/*
701 * Check that the imap we are going to return to the caller spans the entire
702 * range that the caller requested for the IO.
703 */
704static bool
705imap_spans_range(
706 struct xfs_bmbt_irec *imap,
707 xfs_fileoff_t offset_fsb,
708 xfs_fileoff_t end_fsb)
709{
710 if (imap->br_startoff > offset_fsb)
711 return false;
712 if (imap->br_startoff + imap->br_blockcount < end_fsb)
713 return false;
714 return true;
715}
716
Christoph Hellwiga526c852019-10-19 09:09:46 -0700717static int
Christoph Hellwigf150b422019-10-19 09:09:46 -0700718xfs_direct_write_iomap_begin(
Christoph Hellwiga526c852019-10-19 09:09:46 -0700719 struct inode *inode,
720 loff_t offset,
721 loff_t length,
722 unsigned flags,
723 struct iomap *iomap,
724 struct iomap *srcmap)
725{
726 struct xfs_inode *ip = XFS_I(inode);
727 struct xfs_mount *mp = ip->i_mount;
728 struct xfs_bmbt_irec imap, cmap;
729 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
730 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
731 int nimaps = 1, error = 0;
732 bool shared = false;
733 u16 iomap_flags = 0;
734 unsigned lockmode;
735
736 ASSERT(flags & (IOMAP_WRITE | IOMAP_ZERO));
737
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700738 if (xfs_is_shutdown(mp))
Christoph Hellwiga526c852019-10-19 09:09:46 -0700739 return -EIO;
740
Christoph Hellwiga526c852019-10-19 09:09:46 -0700741 /*
Christoph Hellwig5c5b6f72019-10-19 09:09:47 -0700742 * Writes that span EOF might trigger an IO size update on completion,
743 * so consider them to be dirty for the purposes of O_DSYNC even if
744 * there is no other metadata changes pending or have been made here.
745 */
746 if (offset + length > i_size_read(inode))
747 iomap_flags |= IOMAP_F_DIRTY;
748
Christoph Hellwiga526c852019-10-19 09:09:46 -0700749 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
750 if (error)
751 return error;
752
753 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
754 &nimaps, 0);
755 if (error)
756 goto out_unlock;
757
Christoph Hellwig5c5b6f72019-10-19 09:09:47 -0700758 if (imap_needs_cow(ip, flags, &imap, nimaps)) {
Christoph Hellwig1e190f82019-10-19 09:09:47 -0700759 error = -EAGAIN;
760 if (flags & IOMAP_NOWAIT)
761 goto out_unlock;
762
Christoph Hellwiga526c852019-10-19 09:09:46 -0700763 /* may drop and re-acquire the ilock */
764 error = xfs_reflink_allocate_cow(ip, &imap, &cmap, &shared,
765 &lockmode, flags & IOMAP_DIRECT);
766 if (error)
767 goto out_unlock;
768 if (shared)
769 goto out_found_cow;
770 end_fsb = imap.br_startoff + imap.br_blockcount;
771 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
772 }
773
Christoph Hellwig5c5b6f72019-10-19 09:09:47 -0700774 if (imap_needs_alloc(inode, flags, &imap, nimaps))
775 goto allocate_blocks;
Christoph Hellwiga526c852019-10-19 09:09:46 -0700776
Dave Chinner883a7902020-11-19 08:59:11 -0800777 /*
Dave Chinnered1128c2021-01-23 10:06:31 -0800778 * NOWAIT and OVERWRITE I/O needs to span the entire requested I/O with
779 * a single map so that we avoid partial IO failures due to the rest of
780 * the I/O range not covered by this map triggering an EAGAIN condition
781 * when it is subsequently mapped and aborting the I/O.
Dave Chinner883a7902020-11-19 08:59:11 -0800782 */
Dave Chinnered1128c2021-01-23 10:06:31 -0800783 if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY)) {
Dave Chinner883a7902020-11-19 08:59:11 -0800784 error = -EAGAIN;
Dave Chinnered1128c2021-01-23 10:06:31 -0800785 if (!imap_spans_range(&imap, offset_fsb, end_fsb))
786 goto out_unlock;
787 }
788
789 /*
790 * For overwrite only I/O, we cannot convert unwritten extents without
791 * requiring sub-block zeroing. This can only be done under an
792 * exclusive IOLOCK, hence return -EAGAIN if this is not a written
793 * extent to tell the caller to try again.
794 */
795 if (flags & IOMAP_OVERWRITE_ONLY) {
796 error = -EAGAIN;
797 if (imap.br_state != XFS_EXT_NORM &&
798 ((offset | length) & mp->m_blockmask))
799 goto out_unlock;
Dave Chinner883a7902020-11-19 08:59:11 -0800800 }
801
Christoph Hellwig5c5b6f72019-10-19 09:09:47 -0700802 xfs_iunlock(ip, lockmode);
803 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
Christoph Hellwig740fd672021-11-29 11:21:57 +0100804 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags);
Christoph Hellwiga526c852019-10-19 09:09:46 -0700805
Christoph Hellwig5c5b6f72019-10-19 09:09:47 -0700806allocate_blocks:
807 error = -EAGAIN;
Dave Chinnered1128c2021-01-23 10:06:31 -0800808 if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY))
Christoph Hellwiga526c852019-10-19 09:09:46 -0700809 goto out_unlock;
Christoph Hellwiga526c852019-10-19 09:09:46 -0700810
811 /*
812 * We cap the maximum length we map to a sane size to keep the chunks
813 * of work done where somewhat symmetric with the work writeback does.
814 * This is a completely arbitrary number pulled out of thin air as a
815 * best guess for initial testing.
816 *
817 * Note that the values needs to be less than 32-bits wide until the
818 * lower level functions are updated.
819 */
820 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
Christoph Hellwige6966632019-10-30 12:24:59 -0700821 end_fsb = xfs_iomap_end_fsb(mp, offset, length);
Christoph Hellwiga526c852019-10-19 09:09:46 -0700822
Christoph Hellwige6966632019-10-30 12:24:59 -0700823 if (offset + length > XFS_ISIZE(ip))
824 end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
825 else if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
826 end_fsb = min(end_fsb, imap.br_startoff + imap.br_blockcount);
827 xfs_iunlock(ip, lockmode);
828
829 error = xfs_iomap_write_direct(ip, offset_fsb, end_fsb - offset_fsb,
Christoph Hellwig952da062021-11-29 11:21:58 +0100830 flags, &imap);
Christoph Hellwiga526c852019-10-19 09:09:46 -0700831 if (error)
832 return error;
833
Christoph Hellwiga526c852019-10-19 09:09:46 -0700834 trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
Christoph Hellwig740fd672021-11-29 11:21:57 +0100835 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
836 iomap_flags | IOMAP_F_NEW);
Christoph Hellwiga526c852019-10-19 09:09:46 -0700837
838out_found_cow:
839 xfs_iunlock(ip, lockmode);
840 length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
841 trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
842 if (imap.br_startblock != HOLESTARTBLOCK) {
Christoph Hellwig740fd672021-11-29 11:21:57 +0100843 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0);
Christoph Hellwiga526c852019-10-19 09:09:46 -0700844 if (error)
845 return error;
846 }
Christoph Hellwig740fd672021-11-29 11:21:57 +0100847 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED);
Christoph Hellwiga526c852019-10-19 09:09:46 -0700848
849out_unlock:
Darrick J. Wongf2733872021-01-27 10:07:27 -0800850 if (lockmode)
851 xfs_iunlock(ip, lockmode);
Christoph Hellwiga526c852019-10-19 09:09:46 -0700852 return error;
853}
854
Christoph Hellwigf150b422019-10-19 09:09:46 -0700855const struct iomap_ops xfs_direct_write_iomap_ops = {
856 .iomap_begin = xfs_direct_write_iomap_begin,
857};
858
Christoph Hellwig51446f52016-09-19 11:10:21 +1000859static int
Christoph Hellwigf150b422019-10-19 09:09:46 -0700860xfs_buffered_write_iomap_begin(
Christoph Hellwig51446f52016-09-19 11:10:21 +1000861 struct inode *inode,
862 loff_t offset,
863 loff_t count,
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100864 unsigned flags,
Christoph Hellwig36adcba2019-10-19 09:09:44 -0700865 struct iomap *iomap,
866 struct iomap *srcmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000868 struct xfs_inode *ip = XFS_I(inode);
869 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000870 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig43568222019-10-19 09:09:44 -0700871 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, count);
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800872 struct xfs_bmbt_irec imap, cmap;
873 struct xfs_iext_cursor icur, ccur;
Brian Fosterf7820882016-11-28 14:57:42 +1100874 xfs_fsblock_t prealloc_blocks = 0;
Christoph Hellwigc4feb0b2019-02-18 09:38:48 -0800875 bool eof = false, cow_eof = false, shared = false;
Christoph Hellwig12dfb582019-10-19 09:09:47 -0700876 int allocfork = XFS_DATA_FORK;
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800877 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700879 if (xfs_is_shutdown(mp))
Brian Fostere4826692021-02-10 17:27:20 -0800880 return -EIO;
881
Christoph Hellwigf150b422019-10-19 09:09:46 -0700882 /* we can't use delayed allocations when using extent size hints */
883 if (xfs_get_extsz_hint(ip))
884 return xfs_direct_write_iomap_begin(inode, offset, count,
885 flags, iomap, srcmap);
886
Christoph Hellwig51446f52016-09-19 11:10:21 +1000887 ASSERT(!XFS_IS_REALTIME_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Christoph Hellwig51446f52016-09-19 11:10:21 +1000889 xfs_ilock(ip, XFS_ILOCK_EXCL);
890
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700891 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(&ip->i_df)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800892 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Christoph Hellwig51446f52016-09-19 11:10:21 +1000893 error = -EFSCORRUPTED;
894 goto out_unlock;
895 }
896
897 XFS_STATS_INC(mp, xs_blk_mapw);
898
Christoph Hellwig862a8042021-04-13 11:15:09 -0700899 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
900 if (error)
901 goto out_unlock;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000902
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800903 /*
Randy Dunlapb63da6c2020-08-05 08:49:58 -0700904 * Search the data fork first to look up our source mapping. We
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800905 * always need the data fork map, as we have to return it to the
906 * iomap code so that the higher level write code can read data in to
907 * perform read-modify-write cycles for unaligned writes.
908 */
909 eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100910 if (eof)
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800911 imap.br_startoff = end_fsb; /* fake hole until the end */
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100912
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800913 /* We never need to allocate blocks for zeroing a hole. */
914 if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) {
915 xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
916 goto out_unlock;
917 }
918
919 /*
920 * Search the COW fork extent list even if we did not find a data fork
921 * extent. This serves two purposes: first this implements the
922 * speculative preallocation using cowextsize, so that we also unshare
923 * block adjacent to shared blocks instead of just the shared blocks
924 * themselves. Second the lookup in the extent list is generally faster
925 * than going out to the shared extent tree.
926 */
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800927 if (xfs_is_cow_inode(ip)) {
928 if (!ip->i_cowfp) {
929 ASSERT(!xfs_is_reflink_inode(ip));
930 xfs_ifork_init_cow(ip);
931 }
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800932 cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
933 &ccur, &cmap);
934 if (!cow_eof && cmap.br_startoff <= offset_fsb) {
935 trace_xfs_reflink_cow_found(ip, &cmap);
Christoph Hellwigae36b532019-10-19 09:09:43 -0700936 goto found_cow;
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800937 }
938 }
939
940 if (imap.br_startoff <= offset_fsb) {
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100941 /*
942 * For reflink files we may need a delalloc reservation when
943 * overwriting shared extents. This includes zeroing of
944 * existing extents that contain data.
945 */
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800946 if (!xfs_is_cow_inode(ip) ||
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800947 ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
948 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
949 &imap);
Christoph Hellwigae36b532019-10-19 09:09:43 -0700950 goto found_imap;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100951 }
952
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800953 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000954
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800955 /* Trim the mapping to the nearest shared extent boundary. */
zhengbinaa124432020-01-20 14:34:47 -0800956 error = xfs_bmap_trim_cow(ip, &imap, &shared);
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800957 if (error)
958 goto out_unlock;
959
960 /* Not shared? Just report the (potentially capped) extent. */
961 if (!shared) {
962 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
963 &imap);
Christoph Hellwigae36b532019-10-19 09:09:43 -0700964 goto found_imap;
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800965 }
966
967 /*
968 * Fork all the shared blocks from our write offset until the
969 * end of the extent.
970 */
Christoph Hellwig12dfb582019-10-19 09:09:47 -0700971 allocfork = XFS_COW_FORK;
Christoph Hellwigdb46e602019-02-18 09:38:47 -0800972 end_fsb = imap.br_startoff + imap.br_blockcount;
973 } else {
974 /*
975 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
976 * pages to keep the chunks of work done where somewhat
977 * symmetric with the work writeback does. This is a completely
978 * arbitrary number pulled out of thin air.
979 *
980 * Note that the values needs to be less than 32-bits wide until
981 * the lower level functions are updated.
982 */
983 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
Christoph Hellwig43568222019-10-19 09:09:44 -0700984 end_fsb = xfs_iomap_end_fsb(mp, offset, count);
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800985
986 if (xfs_is_always_cow_inode(ip))
Christoph Hellwig12dfb582019-10-19 09:09:47 -0700987 allocfork = XFS_COW_FORK;
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100988 }
989
Darrick J. Wong4882c192018-05-04 15:30:22 -0700990 error = xfs_qm_dqattach_locked(ip, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (error)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000992 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Darrick J. Wong590b1652020-05-23 09:43:30 -0700994 if (eof && offset + count > XFS_ISIZE(ip)) {
995 /*
996 * Determine the initial size of the preallocation.
997 * We clean up any extra preallocation when the file is closed.
998 */
Dave Chinner0560f312021-08-18 18:46:52 -0700999 if (xfs_has_allocsize(mp))
Darrick J. Wong590b1652020-05-23 09:43:30 -07001000 prealloc_blocks = mp->m_allocsize_blocks;
1001 else
1002 prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork,
1003 offset, count, &icur);
Christoph Hellwig51446f52016-09-19 11:10:21 +10001004 if (prealloc_blocks) {
1005 xfs_extlen_t align;
1006 xfs_off_t end_offset;
Brian Fosterf7820882016-11-28 14:57:42 +11001007 xfs_fileoff_t p_end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +10001008
Christoph Hellwig5da8a072019-10-28 08:41:44 -07001009 end_offset = XFS_ALLOC_ALIGN(mp, offset + count - 1);
Brian Fosterf7820882016-11-28 14:57:42 +11001010 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
1011 prealloc_blocks;
Christoph Hellwig51446f52016-09-19 11:10:21 +10001012
Christoph Hellwig57c49442019-10-30 12:24:58 -07001013 align = xfs_eof_alignment(ip);
Christoph Hellwig51446f52016-09-19 11:10:21 +10001014 if (align)
Brian Fosterf7820882016-11-28 14:57:42 +11001015 p_end_fsb = roundup_64(p_end_fsb, align);
Christoph Hellwig51446f52016-09-19 11:10:21 +10001016
Christoph Hellwig43568222019-10-19 09:09:44 -07001017 p_end_fsb = min(p_end_fsb,
1018 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
Brian Fosterf7820882016-11-28 14:57:42 +11001019 ASSERT(p_end_fsb > offset_fsb);
1020 prealloc_blocks = p_end_fsb - end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +10001021 }
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Chinner8de2bf92009-04-06 18:49:12 +02001024retry:
Christoph Hellwig12dfb582019-10-19 09:09:47 -07001025 error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
Christoph Hellwigdb46e602019-02-18 09:38:47 -08001026 end_fsb - offset_fsb, prealloc_blocks,
Christoph Hellwig12dfb582019-10-19 09:09:47 -07001027 allocfork == XFS_DATA_FORK ? &imap : &cmap,
1028 allocfork == XFS_DATA_FORK ? &icur : &ccur,
1029 allocfork == XFS_DATA_FORK ? eof : cow_eof);
Dave Chinner055388a2011-01-04 11:35:03 +11001030 switch (error) {
1031 case 0:
Christoph Hellwig51446f52016-09-19 11:10:21 +10001032 break;
Dave Chinner24513372014-06-25 14:58:08 +10001033 case -ENOSPC:
1034 case -EDQUOT:
Christoph Hellwig51446f52016-09-19 11:10:21 +10001035 /* retry without any preallocation */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001036 trace_xfs_delalloc_enospc(ip, offset, count);
Brian Fosterf7820882016-11-28 14:57:42 +11001037 if (prealloc_blocks) {
1038 prealloc_blocks = 0;
Dave Chinner9aa05002012-10-08 21:56:04 +11001039 goto retry;
Dave Chinner055388a2011-01-04 11:35:03 +11001040 }
Gustavo A. R. Silva53004ee2021-04-20 17:54:36 -05001041 fallthrough;
Christoph Hellwig51446f52016-09-19 11:10:21 +10001042 default:
1043 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
Christoph Hellwig12dfb582019-10-19 09:09:47 -07001046 if (allocfork == XFS_COW_FORK) {
1047 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap);
Christoph Hellwigae36b532019-10-19 09:09:43 -07001048 goto found_cow;
1049 }
1050
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001051 /*
1052 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
1053 * them out if the write happens to fail.
1054 */
Christoph Hellwigae36b532019-10-19 09:09:43 -07001055 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig12dfb582019-10-19 09:09:47 -07001056 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap);
Christoph Hellwig740fd672021-11-29 11:21:57 +01001057 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, IOMAP_F_NEW);
Christoph Hellwigae36b532019-10-19 09:09:43 -07001058
1059found_imap:
1060 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig740fd672021-11-29 11:21:57 +01001061 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
Christoph Hellwigae36b532019-10-19 09:09:43 -07001062
1063found_cow:
1064 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1065 if (imap.br_startoff <= offset_fsb) {
Christoph Hellwig740fd672021-11-29 11:21:57 +01001066 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0);
Christoph Hellwig36adcba2019-10-19 09:09:44 -07001067 if (error)
1068 return error;
Christoph Hellwig740fd672021-11-29 11:21:57 +01001069 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags,
1070 IOMAP_F_SHARED);
Christoph Hellwigdb46e602019-02-18 09:38:47 -08001071 }
Darrick J. Wong72a048c2021-08-20 14:42:39 -07001072
1073 xfs_trim_extent(&cmap, offset_fsb, imap.br_startoff - offset_fsb);
Christoph Hellwig740fd672021-11-29 11:21:57 +01001074 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, 0);
Christoph Hellwigae36b532019-10-19 09:09:43 -07001075
Christoph Hellwig51446f52016-09-19 11:10:21 +10001076out_unlock:
1077 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1078 return error;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001079}
1080
1081static int
Christoph Hellwigf150b422019-10-19 09:09:46 -07001082xfs_buffered_write_iomap_end(
1083 struct inode *inode,
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001084 loff_t offset,
1085 loff_t length,
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001086 ssize_t written,
Christoph Hellwigf150b422019-10-19 09:09:46 -07001087 unsigned flags,
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001088 struct iomap *iomap)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001089{
Christoph Hellwigf150b422019-10-19 09:09:46 -07001090 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001091 struct xfs_mount *mp = ip->i_mount;
1092 xfs_fileoff_t start_fsb;
1093 xfs_fileoff_t end_fsb;
1094 int error = 0;
1095
Christoph Hellwigf150b422019-10-19 09:09:46 -07001096 if (iomap->type != IOMAP_DELALLOC)
1097 return 0;
1098
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001099 /*
1100 * Behave as if the write failed if drop writes is enabled. Set the NEW
1101 * flag to force delalloc cleanup.
1102 */
Darrick J. Wongf8c47252017-06-20 17:54:48 -07001103 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001104 iomap->flags |= IOMAP_F_NEW;
Brian Foster9dbddd72017-02-13 22:48:17 -08001105 written = 0;
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001106 }
Brian Foster9dbddd72017-02-13 22:48:17 -08001107
Brian Fosterfa7f1382017-02-16 17:19:12 -08001108 /*
1109 * start_fsb refers to the first unused block after a short write. If
1110 * nothing was written, round offset down to point at the first block in
1111 * the range.
1112 */
1113 if (unlikely(!written))
1114 start_fsb = XFS_B_TO_FSBT(mp, offset);
1115 else
1116 start_fsb = XFS_B_TO_FSB(mp, offset + written);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001117 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1118
1119 /*
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001120 * Trim delalloc blocks if they were allocated by this write and we
1121 * didn't manage to write the whole range.
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001122 *
1123 * We don't need to care about racing delalloc as we hold i_mutex
1124 * across the reserve/allocate/unreserve calls. If there are delalloc
1125 * blocks in the range, they are ours.
1126 */
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001127 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
Brian Fosterfa7f1382017-02-16 17:19:12 -08001128 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1129 XFS_FSB_TO_B(mp, end_fsb) - 1);
1130
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001131 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1132 end_fsb - start_fsb);
Dave Chinner75c8c50f2021-08-18 18:46:53 -07001133 if (error && !xfs_is_shutdown(mp)) {
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001134 xfs_alert(mp, "%s: unable to clean up ino %lld",
1135 __func__, ip->i_ino);
1136 return error;
1137 }
1138 }
1139
1140 return 0;
1141}
1142
Christoph Hellwigf150b422019-10-19 09:09:46 -07001143const struct iomap_ops xfs_buffered_write_iomap_ops = {
1144 .iomap_begin = xfs_buffered_write_iomap_begin,
1145 .iomap_end = xfs_buffered_write_iomap_end,
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001146};
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001147
1148static int
Christoph Hellwig690c2a32019-10-19 09:09:45 -07001149xfs_read_iomap_begin(
1150 struct inode *inode,
1151 loff_t offset,
1152 loff_t length,
1153 unsigned flags,
1154 struct iomap *iomap,
1155 struct iomap *srcmap)
1156{
1157 struct xfs_inode *ip = XFS_I(inode);
1158 struct xfs_mount *mp = ip->i_mount;
1159 struct xfs_bmbt_irec imap;
1160 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1161 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
1162 int nimaps = 1, error = 0;
1163 bool shared = false;
1164 unsigned lockmode;
1165
1166 ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO)));
1167
Dave Chinner75c8c50f2021-08-18 18:46:53 -07001168 if (xfs_is_shutdown(mp))
Christoph Hellwig690c2a32019-10-19 09:09:45 -07001169 return -EIO;
1170
1171 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1172 if (error)
1173 return error;
1174 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1175 &nimaps, 0);
1176 if (!error && (flags & IOMAP_REPORT))
1177 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
1178 xfs_iunlock(ip, lockmode);
1179
1180 if (error)
1181 return error;
1182 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
Christoph Hellwig740fd672021-11-29 11:21:57 +01001183 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
1184 shared ? IOMAP_F_SHARED : 0);
Christoph Hellwig690c2a32019-10-19 09:09:45 -07001185}
1186
1187const struct iomap_ops xfs_read_iomap_ops = {
1188 .iomap_begin = xfs_read_iomap_begin,
1189};
1190
1191static int
Christoph Hellwig60271ab72019-02-18 09:38:46 -08001192xfs_seek_iomap_begin(
1193 struct inode *inode,
1194 loff_t offset,
1195 loff_t length,
1196 unsigned flags,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -07001197 struct iomap *iomap,
1198 struct iomap *srcmap)
Christoph Hellwig60271ab72019-02-18 09:38:46 -08001199{
1200 struct xfs_inode *ip = XFS_I(inode);
1201 struct xfs_mount *mp = ip->i_mount;
1202 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1203 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1204 xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
1205 struct xfs_iext_cursor icur;
1206 struct xfs_bmbt_irec imap, cmap;
1207 int error = 0;
1208 unsigned lockmode;
1209
Dave Chinner75c8c50f2021-08-18 18:46:53 -07001210 if (xfs_is_shutdown(mp))
Christoph Hellwig60271ab72019-02-18 09:38:46 -08001211 return -EIO;
1212
1213 lockmode = xfs_ilock_data_map_shared(ip);
Christoph Hellwig862a8042021-04-13 11:15:09 -07001214 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1215 if (error)
1216 goto out_unlock;
Christoph Hellwig60271ab72019-02-18 09:38:46 -08001217
1218 if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
1219 /*
1220 * If we found a data extent we are done.
1221 */
1222 if (imap.br_startoff <= offset_fsb)
1223 goto done;
1224 data_fsb = imap.br_startoff;
1225 } else {
1226 /*
1227 * Fake a hole until the end of the file.
1228 */
Christoph Hellwig43568222019-10-19 09:09:44 -07001229 data_fsb = xfs_iomap_end_fsb(mp, offset, length);
Christoph Hellwig60271ab72019-02-18 09:38:46 -08001230 }
1231
1232 /*
1233 * If a COW fork extent covers the hole, report it - capped to the next
1234 * data fork extent:
1235 */
1236 if (xfs_inode_has_cow_data(ip) &&
1237 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1238 cow_fsb = cmap.br_startoff;
1239 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
1240 if (data_fsb < cow_fsb + cmap.br_blockcount)
1241 end_fsb = min(end_fsb, data_fsb);
1242 xfs_trim_extent(&cmap, offset_fsb, end_fsb);
Christoph Hellwig740fd672021-11-29 11:21:57 +01001243 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, flags,
1244 IOMAP_F_SHARED);
Christoph Hellwig60271ab72019-02-18 09:38:46 -08001245 /*
1246 * This is a COW extent, so we must probe the page cache
1247 * because there could be dirty page cache being backed
1248 * by this extent.
1249 */
1250 iomap->type = IOMAP_UNWRITTEN;
1251 goto out_unlock;
1252 }
1253
1254 /*
1255 * Else report a hole, capped to the next found data or COW extent.
1256 */
1257 if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
1258 imap.br_blockcount = cow_fsb - offset_fsb;
1259 else
1260 imap.br_blockcount = data_fsb - offset_fsb;
1261 imap.br_startoff = offset_fsb;
1262 imap.br_startblock = HOLESTARTBLOCK;
1263 imap.br_state = XFS_EXT_NORM;
1264done:
1265 xfs_trim_extent(&imap, offset_fsb, end_fsb);
Christoph Hellwig740fd672021-11-29 11:21:57 +01001266 error = xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
Christoph Hellwig60271ab72019-02-18 09:38:46 -08001267out_unlock:
1268 xfs_iunlock(ip, lockmode);
1269 return error;
1270}
1271
1272const struct iomap_ops xfs_seek_iomap_ops = {
1273 .iomap_begin = xfs_seek_iomap_begin,
1274};
1275
1276static int
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001277xfs_xattr_iomap_begin(
1278 struct inode *inode,
1279 loff_t offset,
1280 loff_t length,
1281 unsigned flags,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -07001282 struct iomap *iomap,
1283 struct iomap *srcmap)
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001284{
1285 struct xfs_inode *ip = XFS_I(inode);
1286 struct xfs_mount *mp = ip->i_mount;
1287 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1288 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1289 struct xfs_bmbt_irec imap;
1290 int nimaps = 1, error = 0;
1291 unsigned lockmode;
1292
Dave Chinner75c8c50f2021-08-18 18:46:53 -07001293 if (xfs_is_shutdown(mp))
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001294 return -EIO;
1295
Darrick J. Wong84358536d2017-04-06 16:00:39 -07001296 lockmode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001297
1298 /* if there are no attribute fork or extents, return ENOENT */
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001299 if (!XFS_IFORK_Q(ip) || !ip->i_afp->if_nextents) {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001300 error = -ENOENT;
1301 goto out_unlock;
1302 }
1303
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001304 ASSERT(ip->i_afp->if_format != XFS_DINODE_FMT_LOCAL);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001305 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongb7e0b6f2017-12-06 16:13:35 -08001306 &nimaps, XFS_BMAPI_ATTRFORK);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001307out_unlock:
1308 xfs_iunlock(ip, lockmode);
1309
Christoph Hellwig16be1432019-02-18 09:38:46 -08001310 if (error)
1311 return error;
1312 ASSERT(nimaps);
Christoph Hellwig740fd672021-11-29 11:21:57 +01001313 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001314}
1315
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001316const struct iomap_ops xfs_xattr_iomap_ops = {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001317 .iomap_begin = xfs_xattr_iomap_begin,
1318};
Shiyang Ruanf1ba5fa2021-11-29 11:21:49 +01001319
1320int
1321xfs_zero_range(
1322 struct xfs_inode *ip,
1323 loff_t pos,
1324 loff_t len,
1325 bool *did_zero)
1326{
1327 struct inode *inode = VFS_I(ip);
1328
Christoph Hellwigc6f40462021-11-29 11:21:52 +01001329 if (IS_DAX(inode))
1330 return dax_zero_range(inode, pos, len, did_zero,
Christoph Hellwiga50f6ab2021-11-29 11:21:56 +01001331 &xfs_direct_write_iomap_ops);
Shiyang Ruanf1ba5fa2021-11-29 11:21:49 +01001332 return iomap_zero_range(inode, pos, len, did_zero,
1333 &xfs_buffered_write_iomap_ops);
1334}
1335
1336int
1337xfs_truncate_page(
1338 struct xfs_inode *ip,
1339 loff_t pos,
1340 bool *did_zero)
1341{
1342 struct inode *inode = VFS_I(ip);
1343
Christoph Hellwigc6f40462021-11-29 11:21:52 +01001344 if (IS_DAX(inode))
1345 return dax_truncate_page(inode, pos, did_zero,
Christoph Hellwiga50f6ab2021-11-29 11:21:56 +01001346 &xfs_direct_write_iomap_ops);
Shiyang Ruanf1ba5fa2021-11-29 11:21:49 +01001347 return iomap_truncate_page(inode, pos, did_zero,
1348 &xfs_buffered_write_iomap_ops);
1349}