blob: 284c5e68f695ff264d3c3f95c116efb1edb63d99 [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 */
Christoph Hellwig3b3dce02016-06-21 09:52:47 +10007#include <linux/iomap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110010#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_format.h"
12#include "xfs_log_format.h"
13#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100015#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110017#include "xfs_btree.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110018#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100020#include "xfs_bmap_util.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070021#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_error.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110023#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans_space.h"
Christoph Hellwiga39e5962017-11-01 16:36:47 +010025#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_iomap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000027#include "xfs_trace.h"
Brian Foster27b52862012-11-06 09:50:38 -050028#include "xfs_icache.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110029#include "xfs_quota.h"
Brian Foster76a42022013-03-18 10:51:47 -040030#include "xfs_dquot_item.h"
31#include "xfs_dquot.h"
Darrick J. Wong2a067052016-10-03 09:11:33 -070032#include "xfs_reflink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
36 << mp->m_writeio_log)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Christoph Hellwig16be1432019-02-18 09:38:46 -080038static int
39xfs_alert_fsblock_zero(
40 xfs_inode_t *ip,
41 xfs_bmbt_irec_t *imap)
42{
43 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
44 "Access to block zero in inode %llu "
45 "start_block: %llx start_off: %llx "
46 "blkcnt: %llx extent-state: %x",
47 (unsigned long long)ip->i_ino,
48 (unsigned long long)imap->br_startblock,
49 (unsigned long long)imap->br_startoff,
50 (unsigned long long)imap->br_blockcount,
51 imap->br_state);
52 return -EFSCORRUPTED;
53}
54
55int
Christoph Hellwige9c49732016-09-19 11:09:12 +100056xfs_bmbt_to_iomap(
57 struct xfs_inode *ip,
58 struct iomap *iomap,
Christoph Hellwig16be1432019-02-18 09:38:46 -080059 struct xfs_bmbt_irec *imap,
60 bool shared)
Christoph Hellwige9c49732016-09-19 11:09:12 +100061{
62 struct xfs_mount *mp = ip->i_mount;
63
Christoph Hellwig16be1432019-02-18 09:38:46 -080064 if (unlikely(!imap->br_startblock && !XFS_IS_REALTIME_INODE(ip)))
65 return xfs_alert_fsblock_zero(ip, imap);
66
Christoph Hellwige9c49732016-09-19 11:09:12 +100067 if (imap->br_startblock == HOLESTARTBLOCK) {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040068 iomap->addr = IOMAP_NULL_ADDR;
Christoph Hellwige9c49732016-09-19 11:09:12 +100069 iomap->type = IOMAP_HOLE;
Christoph Hellwig16be1432019-02-18 09:38:46 -080070 } else if (imap->br_startblock == DELAYSTARTBLOCK ||
71 isnullstartblock(imap->br_startblock)) {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040072 iomap->addr = IOMAP_NULL_ADDR;
Christoph Hellwige9c49732016-09-19 11:09:12 +100073 iomap->type = IOMAP_DELALLOC;
74 } else {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040075 iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
Christoph Hellwige9c49732016-09-19 11:09:12 +100076 if (imap->br_state == XFS_EXT_UNWRITTEN)
77 iomap->type = IOMAP_UNWRITTEN;
78 else
79 iomap->type = IOMAP_MAPPED;
80 }
81 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
82 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
83 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
Dan Williams486aff52017-08-24 15:12:50 -070084 iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
Christoph Hellwig16be1432019-02-18 09:38:46 -080085
86 if (xfs_ipincount(ip) &&
87 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
88 iomap->flags |= IOMAP_F_DIRTY;
89 if (shared)
90 iomap->flags |= IOMAP_F_SHARED;
91 return 0;
Christoph Hellwige9c49732016-09-19 11:09:12 +100092}
93
Christoph Hellwig0365c5d2018-10-18 17:19:26 +110094static void
95xfs_hole_to_iomap(
96 struct xfs_inode *ip,
97 struct iomap *iomap,
98 xfs_fileoff_t offset_fsb,
99 xfs_fileoff_t end_fsb)
100{
101 iomap->addr = IOMAP_NULL_ADDR;
102 iomap->type = IOMAP_HOLE;
103 iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb);
104 iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb);
105 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
106 iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
107}
108
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700109xfs_extlen_t
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000110xfs_eof_alignment(
111 struct xfs_inode *ip,
112 xfs_extlen_t extsize)
Nathan Scottdd9f4382006-01-11 15:28:28 +1100113{
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000114 struct xfs_mount *mp = ip->i_mount;
115 xfs_extlen_t align = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100116
Christoph Hellwigbf322d92011-12-18 20:00:05 +0000117 if (!XFS_IS_REALTIME_INODE(ip)) {
118 /*
119 * Round up the allocation request to a stripe unit
120 * (m_dalign) boundary if the file size is >= stripe unit
121 * size, and we are allocating past the allocation eof.
122 *
123 * If mounted with the "-o swalloc" option the alignment is
124 * increased from the strip unit size to the stripe width.
125 */
126 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
127 align = mp->m_swidth;
128 else if (mp->m_dalign)
129 align = mp->m_dalign;
130
Peter Watkins76b57302014-12-04 09:30:51 +1100131 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
132 align = 0;
Christoph Hellwigbf322d92011-12-18 20:00:05 +0000133 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100134
135 /*
136 * Always round up the allocation request to an extent boundary
137 * (when file on a real-time subvolume or has di_extsize hint).
138 */
139 if (extsize) {
Peter Watkins76b57302014-12-04 09:30:51 +1100140 if (align)
141 align = roundup_64(align, extsize);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100142 else
143 align = extsize;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100144 }
145
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000146 return align;
147}
148
149STATIC int
150xfs_iomap_eof_align_last_fsb(
151 struct xfs_inode *ip,
152 xfs_extlen_t extsize,
153 xfs_fileoff_t *last_fsb)
154{
155 xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
156
Peter Watkins76b57302014-12-04 09:30:51 +1100157 if (align) {
158 xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000159 int eof, error;
160
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000161 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100162 if (error)
163 return error;
164 if (eof)
165 *last_fsb = new_last_fsb;
166 }
167 return 0;
168}
169
Christoph Hellwiga206c812010-12-10 08:42:20 +0000170int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171xfs_iomap_write_direct(
172 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700173 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 size_t count,
Christoph Hellwig30704512010-06-24 11:42:19 +1000175 xfs_bmbt_irec_t *imap,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000176 int nmaps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 xfs_fileoff_t offset_fsb;
180 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100181 xfs_filblks_t count_fsb, resaligned;
Christoph Hellwigf13eb202017-02-06 10:42:26 -0800182 xfs_extlen_t extsz;
Eric Sandeen0116d932005-11-02 15:00:01 +1100183 int nimaps;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000184 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int rt;
186 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100187 uint qblocks, resblks, resrtextents;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100188 int error;
Brian Foster009c6e82015-10-12 15:34:20 +1100189 int lockmode;
Dave Chinner1ca19152015-11-03 12:37:00 +1100190 int bmapi_flags = XFS_BMAPI_PREALLOC;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000191 uint tflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Nathan Scottdd9f4382006-01-11 15:28:28 +1100193 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000194 extsz = xfs_get_extsz_hint(ip);
Brian Foster009c6e82015-10-12 15:34:20 +1100195 lockmode = XFS_ILOCK_SHARED; /* locked by caller */
196
197 ASSERT(xfs_isilocked(ip, lockmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
David Chinner957d0eb2007-06-18 16:50:37 +1000199 offset_fsb = XFS_B_TO_FSBT(mp, offset);
200 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000201 if ((offset + count) > XFS_ISIZE(ip)) {
Brian Foster009c6e82015-10-12 15:34:20 +1100202 /*
203 * Assert that the in-core extent list is present since this can
204 * call xfs_iread_extents() and we only have the ilock shared.
205 * This should be safe because the lock was held around a bmapi
206 * call in the caller and we only need it to access the in-core
207 * list.
208 */
209 ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
210 XFS_IFEXTENTS);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000211 error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100212 if (error)
Brian Foster009c6e82015-10-12 15:34:20 +1100213 goto out_unlock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100214 } else {
Christoph Hellwig405f8042010-12-10 08:42:19 +0000215 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
Dave Chinner9bb54cb2018-06-07 07:54:02 -0700216 last_fsb = min(last_fsb, (xfs_fileoff_t)
Christoph Hellwig30704512010-06-24 11:42:19 +1000217 imap->br_blockcount +
218 imap->br_startoff);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100219 }
220 count_fsb = last_fsb - offset_fsb;
221 ASSERT(count_fsb > 0);
Christoph Hellwigf13eb202017-02-06 10:42:26 -0800222 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100223
224 if (unlikely(rt)) {
225 resrtextents = qblocks = resaligned;
226 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000227 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
228 quota_flag = XFS_QMOPT_RES_RTBLKS;
229 } else {
230 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100231 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000232 quota_flag = XFS_QMOPT_RES_REGBLKS;
233 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
Brian Foster009c6e82015-10-12 15:34:20 +1100236 * Drop the shared lock acquired by the caller, attach the dquot if
237 * necessary and move on to transaction setup.
238 */
239 xfs_iunlock(ip, lockmode);
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700240 error = xfs_qm_dqattach(ip);
Brian Foster009c6e82015-10-12 15:34:20 +1100241 if (error)
242 return error;
243
244 /*
Dave Chinner1ca19152015-11-03 12:37:00 +1100245 * For DAX, we do not allocate unwritten extents, but instead we zero
246 * the block before we commit the transaction. Ideally we'd like to do
247 * this outside the transaction context, but if we commit and then crash
248 * we may not have zeroed the blocks and this will be exposed on
249 * recovery of the allocation. Hence we must zero before commit.
Dave Chinner3b0fe472016-01-04 16:22:45 +1100250 *
Dave Chinner1ca19152015-11-03 12:37:00 +1100251 * Further, if we are mapping unwritten extents here, we need to zero
252 * and convert them to written so that we don't need an unwritten extent
253 * callback for DAX. This also means that we need to be able to dip into
Dave Chinner3b0fe472016-01-04 16:22:45 +1100254 * the reserve block pool for bmbt block allocation if there is no space
255 * left but we need to do unwritten extent conversion.
Dave Chinner1ca19152015-11-03 12:37:00 +1100256 */
257 if (IS_DAX(VFS_I(ip))) {
258 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
Christoph Hellwig63fbb4c2017-03-28 14:53:36 -0700259 if (imap->br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig253f4912016-04-06 09:19:55 +1000260 tflags |= XFS_TRANS_RESERVE;
Dave Chinner3b0fe472016-01-04 16:22:45 +1100261 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
262 }
Dave Chinner1ca19152015-11-03 12:37:00 +1100263 }
Christoph Hellwig253f4912016-04-06 09:19:55 +1000264 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
265 tflags, &tp);
266 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000267 return error;
Dave Chinner507630b2012-03-27 10:34:50 -0400268
Brian Foster009c6e82015-10-12 15:34:20 +1100269 lockmode = XFS_ILOCK_EXCL;
270 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Christoph Hellwig7d095252009-06-08 15:33:32 +0200272 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100273 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400274 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Christoph Hellwigddc34152011-09-19 15:00:54 +0000276 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000279 * From this point onwards we overwrite the imap pointer that the
280 * caller gave to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Nathan Scott06d10dd2005-06-21 15:48:47 +1000282 nimaps = 1;
Christoph Hellwigd531d912014-02-10 10:27:43 +1100283 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Brian Fostera7beabe2018-07-11 22:26:25 -0700284 bmapi_flags, resblks, imap, &nimaps);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000285 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700286 goto out_res_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000289 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
Christoph Hellwig70393312015-06-04 13:48:08 +1000291 error = xfs_trans_commit(tp);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000292 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400293 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Nathan Scott06d10dd2005-06-21 15:48:47 +1000295 /*
296 * Copy any maps to caller's array and return any error.
297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (nimaps == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000299 error = -ENOSPC;
Dave Chinner507630b2012-03-27 10:34:50 -0400300 goto out_unlock;
Nathan Scott572d95f2006-09-28 11:03:20 +1000301 }
302
Dave Chinner507630b2012-03-27 10:34:50 -0400303 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100304 error = xfs_alert_fsblock_zero(ip, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Dave Chinner507630b2012-03-27 10:34:50 -0400306out_unlock:
Brian Foster009c6e82015-10-12 15:34:20 +1100307 xfs_iunlock(ip, lockmode);
Dave Chinner507630b2012-03-27 10:34:50 -0400308 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Brian Fosterc8eac492018-07-24 13:43:13 -0700310out_res_cancel:
Dave Chinnerea562ed2012-05-08 20:48:53 +1000311 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
Dave Chinner507630b2012-03-27 10:34:50 -0400312out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000313 xfs_trans_cancel(tp);
Dave Chinner507630b2012-03-27 10:34:50 -0400314 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Brian Foster76a42022013-03-18 10:51:47 -0400317STATIC bool
318xfs_quota_need_throttle(
319 struct xfs_inode *ip,
320 int type,
321 xfs_fsblock_t alloc_blocks)
322{
323 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
324
325 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
326 return false;
327
328 /* no hi watermark, no throttle */
329 if (!dq->q_prealloc_hi_wmark)
330 return false;
331
332 /* under the lo watermark, no throttle */
333 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
334 return false;
335
336 return true;
337}
338
339STATIC void
340xfs_quota_calc_throttle(
341 struct xfs_inode *ip,
342 int type,
343 xfs_fsblock_t *qblocks,
Brian Fosterf0740512014-07-24 19:56:08 +1000344 int *qshift,
345 int64_t *qfreesp)
Brian Foster76a42022013-03-18 10:51:47 -0400346{
347 int64_t freesp;
348 int shift = 0;
349 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
350
Eric Sandeen5cca3f62014-10-02 09:27:09 +1000351 /* no dq, or over hi wmark, squash the prealloc completely */
352 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
Brian Foster76a42022013-03-18 10:51:47 -0400353 *qblocks = 0;
Brian Fosterf0740512014-07-24 19:56:08 +1000354 *qfreesp = 0;
Brian Foster76a42022013-03-18 10:51:47 -0400355 return;
356 }
357
358 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
359 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
360 shift = 2;
361 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
362 shift += 2;
363 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
364 shift += 2;
365 }
366
Brian Fosterf0740512014-07-24 19:56:08 +1000367 if (freesp < *qfreesp)
368 *qfreesp = freesp;
369
Brian Foster76a42022013-03-18 10:51:47 -0400370 /* only overwrite the throttle values if we are more aggressive */
371 if ((freesp >> shift) < (*qblocks >> *qshift)) {
372 *qblocks = freesp;
373 *qshift = shift;
374 }
375}
376
Dave Chinnera1e16c22013-02-11 16:05:01 +1100377/*
Christoph Hellwig51446f52016-09-19 11:10:21 +1000378 * If we are doing a write at the end of the file and there are no allocations
379 * past this one, then extend the allocation out to the file system's write
380 * iosize.
381 *
Dave Chinner055388a2011-01-04 11:35:03 +1100382 * If we don't have a user specified preallocation size, dynamically increase
Christoph Hellwig51446f52016-09-19 11:10:21 +1000383 * the preallocation size as the size of the file grows. Cap the maximum size
Dave Chinner055388a2011-01-04 11:35:03 +1100384 * at a single extent or less if the filesystem is near full. The closer the
385 * filesystem is to full, the smaller the maximum prealocation.
Christoph Hellwig51446f52016-09-19 11:10:21 +1000386 *
387 * As an exception we don't do any preallocation at all if the file is smaller
388 * than the minimum preallocation and we are using the default dynamic
389 * preallocation scheme, as it is likely this is the only write to the file that
390 * is going to be done.
391 *
392 * We clean up any extra space left over when the file is closed in
393 * xfs_inactive().
Dave Chinner055388a2011-01-04 11:35:03 +1100394 */
395STATIC xfs_fsblock_t
396xfs_iomap_prealloc_size(
Dave Chinnera1e16c22013-02-11 16:05:01 +1100397 struct xfs_inode *ip,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000398 loff_t offset,
399 loff_t count,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700400 struct xfs_iext_cursor *icur)
Dave Chinner055388a2011-01-04 11:35:03 +1100401{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000402 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig656152e2016-11-24 11:39:44 +1100403 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000404 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig656152e2016-11-24 11:39:44 +1100405 struct xfs_bmbt_irec prev;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400406 int shift = 0;
407 int64_t freesp;
Brian Foster76a42022013-03-18 10:51:47 -0400408 xfs_fsblock_t qblocks;
409 int qshift = 0;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000410 xfs_fsblock_t alloc_blocks = 0;
Dave Chinner055388a2011-01-04 11:35:03 +1100411
Christoph Hellwig51446f52016-09-19 11:10:21 +1000412 if (offset + count <= XFS_ISIZE(ip))
413 return 0;
414
415 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
416 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
417 return 0;
418
419 /*
420 * If an explicit allocsize is set, the file is small, or we
421 * are writing behind a hole, then use the minimum prealloc:
422 */
423 if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
424 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700425 !xfs_iext_peek_prev_extent(ifp, icur, &prev) ||
Christoph Hellwig656152e2016-11-24 11:39:44 +1100426 prev.br_startoff + prev.br_blockcount < offset_fsb)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000427 return mp->m_writeio_blocks;
428
429 /*
430 * Determine the initial size of the preallocation. We are beyond the
431 * current EOF here, but we need to take into account whether this is
432 * a sparse write or an extending write when determining the
433 * preallocation size. Hence we need to look up the extent that ends
434 * at the current write offset and use the result to determine the
435 * preallocation size.
436 *
437 * If the extent is a hole, then preallocation is essentially disabled.
438 * Otherwise we take the size of the preceding data extent as the basis
439 * for the preallocation size. If the size of the extent is greater than
440 * half the maximum extent length, then use the current offset as the
441 * basis. This ensures that for large files the preallocation size
442 * always extends to MAXEXTLEN rather than falling short due to things
443 * like stripe unit/width alignment of real extents.
444 */
Christoph Hellwig656152e2016-11-24 11:39:44 +1100445 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
446 alloc_blocks = prev.br_blockcount << 1;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000447 else
448 alloc_blocks = XFS_B_TO_FSB(mp, offset);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400449 if (!alloc_blocks)
450 goto check_writeio;
Brian Foster76a42022013-03-18 10:51:47 -0400451 qblocks = alloc_blocks;
Dave Chinner055388a2011-01-04 11:35:03 +1100452
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400453 /*
454 * MAXEXTLEN is not a power of two value but we round the prealloc down
455 * to the nearest power of two value after throttling. To prevent the
456 * round down from unconditionally reducing the maximum supported prealloc
457 * size, we round up first, apply appropriate throttling, round down and
458 * cap the value to MAXEXTLEN.
459 */
460 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
461 alloc_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100462
Dave Chinner0d485ad2015-02-23 21:22:03 +1100463 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400464 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
465 shift = 2;
466 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
467 shift++;
468 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
469 shift++;
470 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
471 shift++;
472 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
473 shift++;
Dave Chinner055388a2011-01-04 11:35:03 +1100474 }
Brian Foster76a42022013-03-18 10:51:47 -0400475
476 /*
Brian Fosterf0740512014-07-24 19:56:08 +1000477 * Check each quota to cap the prealloc size, provide a shift value to
478 * throttle with and adjust amount of available space.
Brian Foster76a42022013-03-18 10:51:47 -0400479 */
480 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000481 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
482 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400483 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000484 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
485 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400486 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000487 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
488 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400489
490 /*
491 * The final prealloc size is set to the minimum of free space available
492 * in each of the quotas and the overall filesystem.
493 *
494 * The shift throttle value is set to the maximum value as determined by
495 * the global low free space values and per-quota low free space values.
496 */
Dave Chinner9bb54cb2018-06-07 07:54:02 -0700497 alloc_blocks = min(alloc_blocks, qblocks);
498 shift = max(shift, qshift);
Brian Foster76a42022013-03-18 10:51:47 -0400499
Brian Foster3c58b5f2013-03-18 10:51:43 -0400500 if (shift)
501 alloc_blocks >>= shift;
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400502 /*
503 * rounddown_pow_of_two() returns an undefined result if we pass in
504 * alloc_blocks = 0.
505 */
506 if (alloc_blocks)
507 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
508 if (alloc_blocks > MAXEXTLEN)
509 alloc_blocks = MAXEXTLEN;
Dave Chinner055388a2011-01-04 11:35:03 +1100510
Brian Foster3c58b5f2013-03-18 10:51:43 -0400511 /*
512 * If we are still trying to allocate more space than is
513 * available, squash the prealloc hard. This can happen if we
514 * have a large file on a small filesystem and the above
515 * lowspace thresholds are smaller than MAXEXTLEN.
516 */
517 while (alloc_blocks && alloc_blocks >= freesp)
518 alloc_blocks >>= 4;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400519check_writeio:
Dave Chinner055388a2011-01-04 11:35:03 +1100520 if (alloc_blocks < mp->m_writeio_blocks)
521 alloc_blocks = mp->m_writeio_blocks;
Brian Foster19cb7e32013-03-18 10:51:48 -0400522 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
523 mp->m_writeio_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100524 return alloc_blocks;
525}
526
Christoph Hellwig51446f52016-09-19 11:10:21 +1000527static int
528xfs_file_iomap_begin_delay(
529 struct inode *inode,
530 loff_t offset,
531 loff_t count,
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100532 unsigned flags,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000533 struct iomap *iomap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000535 struct xfs_inode *ip = XFS_I(inode);
536 struct xfs_mount *mp = ip->i_mount;
537 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
538 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
539 xfs_fileoff_t maxbytes_fsb =
540 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Brian Fosterf7820882016-11-28 14:57:42 +1100541 xfs_fileoff_t end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000542 int error = 0, eof = 0;
543 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700544 struct xfs_iext_cursor icur;
Brian Fosterf7820882016-11-28 14:57:42 +1100545 xfs_fsblock_t prealloc_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Christoph Hellwig51446f52016-09-19 11:10:21 +1000547 ASSERT(!XFS_IS_REALTIME_INODE(ip));
548 ASSERT(!xfs_get_extsz_hint(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Christoph Hellwig51446f52016-09-19 11:10:21 +1000550 xfs_ilock(ip, XFS_ILOCK_EXCL);
551
552 if (unlikely(XFS_TEST_ERROR(
553 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
554 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -0700555 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig51446f52016-09-19 11:10:21 +1000556 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
557 error = -EFSCORRUPTED;
558 goto out_unlock;
559 }
560
561 XFS_STATS_INC(mp, xs_blk_mapw);
562
563 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
564 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
565 if (error)
566 goto out_unlock;
567 }
568
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100569 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
570
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700571 eof = !xfs_iext_lookup_extent(ip, ifp, offset_fsb, &icur, &got);
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100572 if (eof)
573 got.br_startoff = end_fsb; /* fake hole until the end */
574
575 if (got.br_startoff <= offset_fsb) {
576 /*
577 * For reflink files we may need a delalloc reservation when
578 * overwriting shared extents. This includes zeroing of
579 * existing extents that contain data.
580 */
581 if (xfs_is_reflink_inode(ip) &&
582 ((flags & IOMAP_WRITE) ||
583 got.br_state != XFS_EXT_UNWRITTEN)) {
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100584 xfs_trim_extent(&got, offset_fsb, end_fsb - offset_fsb);
Christoph Hellwigfc439462018-10-18 17:19:37 +1100585 error = xfs_reflink_reserve_cow(ip, &got);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100586 if (error)
587 goto out_unlock;
588 }
589
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800590 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK, &got);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000591 goto done;
592 }
593
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100594 if (flags & IOMAP_ZERO) {
595 xfs_hole_to_iomap(ip, iomap, offset_fsb, got.br_startoff);
596 goto out_unlock;
597 }
598
Darrick J. Wong4882c192018-05-04 15:30:22 -0700599 error = xfs_qm_dqattach_locked(ip, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (error)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000601 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Christoph Hellwig51446f52016-09-19 11:10:21 +1000603 /*
604 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES pages
605 * to keep the chunks of work done where somewhat symmetric with the
606 * work writeback does. This is a completely arbitrary number pulled
607 * out of thin air as a best guess for initial testing.
608 *
609 * Note that the values needs to be less than 32-bits wide until
610 * the lower level functions are updated.
611 */
612 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
Brian Fosterf7820882016-11-28 14:57:42 +1100613 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100614
Christoph Hellwig51446f52016-09-19 11:10:21 +1000615 if (eof) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700616 prealloc_blocks = xfs_iomap_prealloc_size(ip, offset, count,
617 &icur);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000618 if (prealloc_blocks) {
619 xfs_extlen_t align;
620 xfs_off_t end_offset;
Brian Fosterf7820882016-11-28 14:57:42 +1100621 xfs_fileoff_t p_end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000622
623 end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
Brian Fosterf7820882016-11-28 14:57:42 +1100624 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
625 prealloc_blocks;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000626
627 align = xfs_eof_alignment(ip, 0);
628 if (align)
Brian Fosterf7820882016-11-28 14:57:42 +1100629 p_end_fsb = roundup_64(p_end_fsb, align);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000630
Brian Fosterf7820882016-11-28 14:57:42 +1100631 p_end_fsb = min(p_end_fsb, maxbytes_fsb);
632 ASSERT(p_end_fsb > offset_fsb);
633 prealloc_blocks = p_end_fsb - end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000634 }
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Dave Chinner8de2bf92009-04-06 18:49:12 +0200637retry:
Darrick J. Wongbe51f812016-10-03 09:11:32 -0700638 error = xfs_bmapi_reserve_delalloc(ip, XFS_DATA_FORK, offset_fsb,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700639 end_fsb - offset_fsb, prealloc_blocks, &got, &icur,
640 eof);
Dave Chinner055388a2011-01-04 11:35:03 +1100641 switch (error) {
642 case 0:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000643 break;
Dave Chinner24513372014-06-25 14:58:08 +1000644 case -ENOSPC:
645 case -EDQUOT:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000646 /* retry without any preallocation */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000647 trace_xfs_delalloc_enospc(ip, offset, count);
Brian Fosterf7820882016-11-28 14:57:42 +1100648 if (prealloc_blocks) {
649 prealloc_blocks = 0;
Dave Chinner9aa05002012-10-08 21:56:04 +1100650 goto retry;
Dave Chinner055388a2011-01-04 11:35:03 +1100651 }
Christoph Hellwig51446f52016-09-19 11:10:21 +1000652 /*FALLTHRU*/
653 default:
654 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
Brian Fosterf65e6fa2017-03-08 09:58:08 -0800657 /*
658 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
659 * them out if the write happens to fail.
660 */
Christoph Hellwigc03cea42018-06-19 15:10:58 -0700661 iomap->flags |= IOMAP_F_NEW;
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800662 trace_xfs_iomap_alloc(ip, offset, count, XFS_DATA_FORK, &got);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000663done:
Christoph Hellwig16be1432019-02-18 09:38:46 -0800664 error = xfs_bmbt_to_iomap(ip, iomap, &got, false);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000665out_unlock:
666 xfs_iunlock(ip, XFS_ILOCK_EXCL);
667 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670int
671xfs_iomap_write_unwritten(
672 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700673 xfs_off_t offset,
Eryu Guanee70daa2017-09-21 11:26:18 -0700674 xfs_off_t count,
675 bool update_isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 xfs_fileoff_t offset_fsb;
679 xfs_filblks_t count_fsb;
680 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100681 int nimaps;
682 xfs_trans_t *tp;
683 xfs_bmbt_irec_t imap;
Eryu Guanee70daa2017-09-21 11:26:18 -0700684 struct inode *inode = VFS_I(ip);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000685 xfs_fsize_t i_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100686 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000689 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 offset_fsb = XFS_B_TO_FSBT(mp, offset);
692 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
693 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
694
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000695 /*
696 * Reserve enough blocks in this transaction for two complete extent
697 * btree splits. We may be converting the middle part of an unwritten
698 * extent and in this case we will insert two new extents in the btree
699 * each of which could cause a full split.
700 *
701 * This reservation amount will be used in the first call to
702 * xfs_bmbt_split() to select an AG with enough space to satisfy the
703 * rest of the operation.
704 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100705 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Nathan Scottdd9f4382006-01-11 15:28:28 +1100707 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /*
Christoph Hellwig253f4912016-04-06 09:19:55 +1000709 * Set up a transaction to convert the range of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 * from unwritten to real. Do allocations in a loop until
711 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000712 *
Christoph Hellwig253f4912016-04-06 09:19:55 +1000713 * Note that we can't risk to recursing back into the filesystem
714 * here as we might be asked to write out the same inode that we
715 * complete here and might deadlock on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000717 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
718 XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
719 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000720 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000723 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /*
726 * Modify the unwritten extent state of the buffer.
727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 nimaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000729 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Brian Fostera7beabe2018-07-11 22:26:25 -0700730 XFS_BMAPI_CONVERT, resblks, &imap,
731 &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (error)
733 goto error_on_bmapi_transaction;
734
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000735 /*
736 * Log the updated inode size as we go. We have to be careful
737 * to only log it up to the actual write offset if it is
738 * halfway into a block.
739 */
740 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
741 if (i_size > offset + count)
742 i_size = offset + count;
Eryu Guanee70daa2017-09-21 11:26:18 -0700743 if (update_isize && i_size > i_size_read(inode))
744 i_size_write(inode, i_size);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000745 i_size = xfs_new_eof(ip, i_size);
746 if (i_size) {
747 ip->i_d.di_size = i_size;
748 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
749 }
750
Christoph Hellwig70393312015-06-04 13:48:08 +1000751 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 xfs_iunlock(ip, XFS_ILOCK_EXCL);
753 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000754 return error;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100755
David Chinner86c4d622008-04-29 12:53:21 +1000756 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100757 return xfs_alert_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if ((numblks_fsb = imap.br_blockcount) == 0) {
760 /*
761 * The numblks_fsb value should always get
762 * smaller, otherwise the loop is stuck.
763 */
764 ASSERT(imap.br_blockcount);
765 break;
766 }
767 offset_fsb += numblks_fsb;
768 count_fsb -= numblks_fsb;
769 } while (count_fsb > 0);
770
771 return 0;
772
773error_on_bmapi_transaction:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000774 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000776 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
Christoph Hellwig3b3dce02016-06-21 09:52:47 +1000778
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700779static inline bool
780imap_needs_alloc(
781 struct inode *inode,
782 struct xfs_bmbt_irec *imap,
783 int nimaps)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000784{
785 return !nimaps ||
786 imap->br_startblock == HOLESTARTBLOCK ||
Christoph Hellwig6c31f4952016-09-19 11:28:38 +1000787 imap->br_startblock == DELAYSTARTBLOCK ||
Christoph Hellwig63fbb4c2017-03-28 14:53:36 -0700788 (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000789}
790
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700791static inline bool
792needs_cow_for_zeroing(
793 struct xfs_bmbt_irec *imap,
794 int nimaps)
Christoph Hellwig172ed392018-03-01 14:10:31 -0800795{
796 return nimaps &&
797 imap->br_startblock != HOLESTARTBLOCK &&
798 imap->br_state != XFS_EXT_UNWRITTEN;
799}
800
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700801static int
802xfs_ilock_for_iomap(
803 struct xfs_inode *ip,
804 unsigned flags,
805 unsigned *lockmode)
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100806{
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700807 unsigned mode = XFS_ILOCK_SHARED;
Darrick J. Wong5bd88d12018-06-21 23:26:57 -0700808 bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700809
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100810 /*
Christoph Hellwigaf5b5af2018-03-01 14:12:12 -0800811 * COW writes may allocate delalloc space or convert unwritten COW
812 * extents, so we need to make sure to take the lock exclusively here.
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100813 */
Darrick J. Wong5bd88d12018-06-21 23:26:57 -0700814 if (xfs_is_reflink_inode(ip) && is_write) {
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700815 /*
816 * FIXME: It could still overwrite on unshared extents and not
817 * need allocation.
818 */
819 if (flags & IOMAP_NOWAIT)
820 return -EAGAIN;
821 mode = XFS_ILOCK_EXCL;
822 }
Christoph Hellwigff3d8b92018-03-01 14:12:45 -0800823
824 /*
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700825 * Extents not yet cached requires exclusive access, don't block. This
826 * is an opencoded xfs_ilock_data_map_shared() call but with
Christoph Hellwigff3d8b92018-03-01 14:12:45 -0800827 * non-blocking behaviour.
828 */
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700829 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
830 if (flags & IOMAP_NOWAIT)
831 return -EAGAIN;
832 mode = XFS_ILOCK_EXCL;
833 }
834
Darrick J. Wong5bd88d12018-06-21 23:26:57 -0700835relock:
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700836 if (flags & IOMAP_NOWAIT) {
837 if (!xfs_ilock_nowait(ip, mode))
838 return -EAGAIN;
839 } else {
840 xfs_ilock(ip, mode);
841 }
842
Darrick J. Wong5bd88d12018-06-21 23:26:57 -0700843 /*
844 * The reflink iflag could have changed since the earlier unlocked
845 * check, so if we got ILOCK_SHARED for a write and but we're now a
846 * reflink inode we have to switch to ILOCK_EXCL and relock.
847 */
848 if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_reflink_inode(ip)) {
849 xfs_iunlock(ip, mode);
850 mode = XFS_ILOCK_EXCL;
851 goto relock;
852 }
853
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700854 *lockmode = mode;
855 return 0;
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100856}
857
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000858static int
859xfs_file_iomap_begin(
860 struct inode *inode,
861 loff_t offset,
862 loff_t length,
863 unsigned flags,
864 struct iomap *iomap)
865{
866 struct xfs_inode *ip = XFS_I(inode);
867 struct xfs_mount *mp = ip->i_mount;
868 struct xfs_bmbt_irec imap;
869 xfs_fileoff_t offset_fsb, end_fsb;
870 int nimaps = 1, error = 0;
Christoph Hellwigd392bc82018-10-18 17:19:48 +1100871 bool shared = false;
Christoph Hellwig66642c52016-09-19 11:26:39 +1000872 unsigned lockmode;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000873
874 if (XFS_FORCED_SHUTDOWN(mp))
875 return -EIO;
876
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100877 if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && !(flags & IOMAP_DIRECT) &&
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100878 !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
Darrick J. Wong2a067052016-10-03 09:11:33 -0700879 /* Reserve delalloc blocks for regular writeback. */
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100880 return xfs_file_iomap_begin_delay(inode, offset, length, flags,
881 iomap);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000882 }
883
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700884 /*
885 * Lock the inode in the manner required for the specified operation and
886 * check for as many conditions that would result in blocking as
887 * possible. This removes most of the non-blocking checks from the
888 * mapping code below.
889 */
890 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
891 if (error)
892 return error;
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -0500893
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000894 ASSERT(offset <= mp->m_super->s_maxbytes);
Darrick J. Wongb4d8ad7f2017-12-22 13:14:34 -0800895 if (offset > mp->m_super->s_maxbytes - length)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000896 length = mp->m_super->s_maxbytes - offset;
897 offset_fsb = XFS_B_TO_FSBT(mp, offset);
898 end_fsb = XFS_B_TO_FSB(mp, offset + length);
899
900 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700901 &nimaps, 0);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100902 if (error)
903 goto out_unlock;
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700904
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800905 if (flags & IOMAP_REPORT) {
Christoph Hellwig5f9268c2016-10-20 15:53:32 +1100906 /* Trim the mapping to the nearest shared extent boundary. */
Christoph Hellwigd392bc82018-10-18 17:19:48 +1100907 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100908 if (error)
909 goto out_unlock;
910 }
911
Dave Chinnerd0641782018-05-02 12:54:53 -0700912 /* Non-modifying mapping requested, so we are done */
913 if (!(flags & (IOMAP_WRITE | IOMAP_ZERO)))
914 goto out_found;
915
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700916 /*
917 * Break shared extents if necessary. Checks for non-blocking IO have
918 * been done up front, so we don't need to do them here.
919 */
920 if (xfs_is_reflink_inode(ip)) {
921 /* if zeroing doesn't need COW allocation, then we are done. */
922 if ((flags & IOMAP_ZERO) &&
923 !needs_cow_for_zeroing(&imap, nimaps))
924 goto out_found;
925
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800926 if (flags & IOMAP_DIRECT) {
927 /* may drop and re-acquire the ilock */
928 error = xfs_reflink_allocate_cow(ip, &imap, &shared,
929 &lockmode);
930 if (error)
931 goto out_unlock;
932 } else {
Christoph Hellwigfc439462018-10-18 17:19:37 +1100933 error = xfs_reflink_reserve_cow(ip, &imap);
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800934 if (error)
935 goto out_unlock;
936 }
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100937
938 end_fsb = imap.br_startoff + imap.br_blockcount;
939 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000940 }
941
Dave Chinnerd0641782018-05-02 12:54:53 -0700942 /* Don't need to allocate over holes when doing zeroing operations. */
943 if (flags & IOMAP_ZERO)
944 goto out_found;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000945
Dave Chinnerd0641782018-05-02 12:54:53 -0700946 if (!imap_needs_alloc(inode, &imap, nimaps))
947 goto out_found;
Christoph Hellwigb95a2122016-08-17 08:44:52 +1000948
Dave Chinnerd0641782018-05-02 12:54:53 -0700949 /* If nowait is set bail since we are going to make allocations. */
950 if (flags & IOMAP_NOWAIT) {
951 error = -EAGAIN;
952 goto out_unlock;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000953 }
954
Dave Chinnerd0641782018-05-02 12:54:53 -0700955 /*
956 * We cap the maximum length we map to a sane size to keep the chunks
957 * of work done where somewhat symmetric with the work writeback does.
958 * This is a completely arbitrary number pulled out of thin air as a
959 * best guess for initial testing.
960 *
961 * Note that the values needs to be less than 32-bits wide until the
962 * lower level functions are updated.
963 */
964 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
965
966 /*
967 * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
968 * return.
969 */
970 if (lockmode == XFS_ILOCK_EXCL)
971 xfs_ilock_demote(ip, lockmode);
972 error = xfs_iomap_write_direct(ip, offset, length, &imap,
973 nimaps);
974 if (error)
975 return error;
976
Christoph Hellwigc03cea42018-06-19 15:10:58 -0700977 iomap->flags |= IOMAP_F_NEW;
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800978 trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
Dave Chinnerd0641782018-05-02 12:54:53 -0700979
980out_finish:
Christoph Hellwig16be1432019-02-18 09:38:46 -0800981 return xfs_bmbt_to_iomap(ip, iomap, &imap, shared);
Dave Chinnerd0641782018-05-02 12:54:53 -0700982
983out_found:
984 ASSERT(nimaps);
985 xfs_iunlock(ip, lockmode);
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800986 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
Dave Chinnerd0641782018-05-02 12:54:53 -0700987 goto out_finish;
988
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100989out_unlock:
990 xfs_iunlock(ip, lockmode);
991 return error;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000992}
993
994static int
995xfs_file_iomap_end_delalloc(
996 struct xfs_inode *ip,
997 loff_t offset,
998 loff_t length,
Brian Fosterf65e6fa2017-03-08 09:58:08 -0800999 ssize_t written,
1000 struct iomap *iomap)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001001{
1002 struct xfs_mount *mp = ip->i_mount;
1003 xfs_fileoff_t start_fsb;
1004 xfs_fileoff_t end_fsb;
1005 int error = 0;
1006
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001007 /*
1008 * Behave as if the write failed if drop writes is enabled. Set the NEW
1009 * flag to force delalloc cleanup.
1010 */
Darrick J. Wongf8c47252017-06-20 17:54:48 -07001011 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001012 iomap->flags |= IOMAP_F_NEW;
Brian Foster9dbddd72017-02-13 22:48:17 -08001013 written = 0;
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001014 }
Brian Foster9dbddd72017-02-13 22:48:17 -08001015
Brian Fosterfa7f1382017-02-16 17:19:12 -08001016 /*
1017 * start_fsb refers to the first unused block after a short write. If
1018 * nothing was written, round offset down to point at the first block in
1019 * the range.
1020 */
1021 if (unlikely(!written))
1022 start_fsb = XFS_B_TO_FSBT(mp, offset);
1023 else
1024 start_fsb = XFS_B_TO_FSB(mp, offset + written);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001025 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1026
1027 /*
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001028 * Trim delalloc blocks if they were allocated by this write and we
1029 * didn't manage to write the whole range.
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001030 *
1031 * We don't need to care about racing delalloc as we hold i_mutex
1032 * across the reserve/allocate/unreserve calls. If there are delalloc
1033 * blocks in the range, they are ours.
1034 */
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001035 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
Brian Fosterfa7f1382017-02-16 17:19:12 -08001036 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1037 XFS_FSB_TO_B(mp, end_fsb) - 1);
1038
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001039 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1040 end_fsb - start_fsb);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001041 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1042 xfs_alert(mp, "%s: unable to clean up ino %lld",
1043 __func__, ip->i_ino);
1044 return error;
1045 }
1046 }
1047
1048 return 0;
1049}
1050
1051static int
1052xfs_file_iomap_end(
1053 struct inode *inode,
1054 loff_t offset,
1055 loff_t length,
1056 ssize_t written,
1057 unsigned flags,
1058 struct iomap *iomap)
1059{
1060 if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
1061 return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001062 length, written, iomap);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001063 return 0;
1064}
1065
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001066const struct iomap_ops xfs_iomap_ops = {
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001067 .iomap_begin = xfs_file_iomap_begin,
1068 .iomap_end = xfs_file_iomap_end,
1069};
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001070
1071static int
1072xfs_xattr_iomap_begin(
1073 struct inode *inode,
1074 loff_t offset,
1075 loff_t length,
1076 unsigned flags,
1077 struct iomap *iomap)
1078{
1079 struct xfs_inode *ip = XFS_I(inode);
1080 struct xfs_mount *mp = ip->i_mount;
1081 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1082 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1083 struct xfs_bmbt_irec imap;
1084 int nimaps = 1, error = 0;
1085 unsigned lockmode;
1086
1087 if (XFS_FORCED_SHUTDOWN(mp))
1088 return -EIO;
1089
Darrick J. Wong84358536d2017-04-06 16:00:39 -07001090 lockmode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001091
1092 /* if there are no attribute fork or extents, return ENOENT */
Darrick J. Wong84358536d2017-04-06 16:00:39 -07001093 if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001094 error = -ENOENT;
1095 goto out_unlock;
1096 }
1097
1098 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1099 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongb7e0b6f2017-12-06 16:13:35 -08001100 &nimaps, XFS_BMAPI_ATTRFORK);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001101out_unlock:
1102 xfs_iunlock(ip, lockmode);
1103
Christoph Hellwig16be1432019-02-18 09:38:46 -08001104 if (error)
1105 return error;
1106 ASSERT(nimaps);
1107 return xfs_bmbt_to_iomap(ip, iomap, &imap, false);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001108}
1109
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001110const struct iomap_ops xfs_xattr_iomap_ops = {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001111 .iomap_begin = xfs_xattr_iomap_begin,
1112};