blob: 55876dd02f0c8c75fa5653eeab82881bd3741928 [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 Hellwig51446f52016-09-19 11:10:21 +10004 * Copyright (c) 2016 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 Hellwige9c49732016-09-19 11:09:12 +100038void
39xfs_bmbt_to_iomap(
40 struct xfs_inode *ip,
41 struct iomap *iomap,
42 struct xfs_bmbt_irec *imap)
43{
44 struct xfs_mount *mp = ip->i_mount;
45
46 if (imap->br_startblock == HOLESTARTBLOCK) {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040047 iomap->addr = IOMAP_NULL_ADDR;
Christoph Hellwige9c49732016-09-19 11:09:12 +100048 iomap->type = IOMAP_HOLE;
49 } else if (imap->br_startblock == DELAYSTARTBLOCK) {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040050 iomap->addr = IOMAP_NULL_ADDR;
Christoph Hellwige9c49732016-09-19 11:09:12 +100051 iomap->type = IOMAP_DELALLOC;
52 } else {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040053 iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
Christoph Hellwige9c49732016-09-19 11:09:12 +100054 if (imap->br_state == XFS_EXT_UNWRITTEN)
55 iomap->type = IOMAP_UNWRITTEN;
56 else
57 iomap->type = IOMAP_MAPPED;
58 }
59 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
60 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
61 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
Dan Williams486aff52017-08-24 15:12:50 -070062 iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
Christoph Hellwige9c49732016-09-19 11:09:12 +100063}
64
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070065xfs_extlen_t
Christoph Hellwigf8e3a822016-09-19 11:09:28 +100066xfs_eof_alignment(
67 struct xfs_inode *ip,
68 xfs_extlen_t extsize)
Nathan Scottdd9f4382006-01-11 15:28:28 +110069{
Christoph Hellwigf8e3a822016-09-19 11:09:28 +100070 struct xfs_mount *mp = ip->i_mount;
71 xfs_extlen_t align = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +110072
Christoph Hellwigbf322d92011-12-18 20:00:05 +000073 if (!XFS_IS_REALTIME_INODE(ip)) {
74 /*
75 * Round up the allocation request to a stripe unit
76 * (m_dalign) boundary if the file size is >= stripe unit
77 * size, and we are allocating past the allocation eof.
78 *
79 * If mounted with the "-o swalloc" option the alignment is
80 * increased from the strip unit size to the stripe width.
81 */
82 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
83 align = mp->m_swidth;
84 else if (mp->m_dalign)
85 align = mp->m_dalign;
86
Peter Watkins76b57302014-12-04 09:30:51 +110087 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
88 align = 0;
Christoph Hellwigbf322d92011-12-18 20:00:05 +000089 }
Nathan Scottdd9f4382006-01-11 15:28:28 +110090
91 /*
92 * Always round up the allocation request to an extent boundary
93 * (when file on a real-time subvolume or has di_extsize hint).
94 */
95 if (extsize) {
Peter Watkins76b57302014-12-04 09:30:51 +110096 if (align)
97 align = roundup_64(align, extsize);
Nathan Scottdd9f4382006-01-11 15:28:28 +110098 else
99 align = extsize;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100100 }
101
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000102 return align;
103}
104
105STATIC int
106xfs_iomap_eof_align_last_fsb(
107 struct xfs_inode *ip,
108 xfs_extlen_t extsize,
109 xfs_fileoff_t *last_fsb)
110{
111 xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
112
Peter Watkins76b57302014-12-04 09:30:51 +1100113 if (align) {
114 xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000115 int eof, error;
116
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000117 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100118 if (error)
119 return error;
120 if (eof)
121 *last_fsb = new_last_fsb;
122 }
123 return 0;
124}
125
126STATIC int
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100127xfs_alert_fsblock_zero(
Nathan Scott572d95f2006-09-28 11:03:20 +1000128 xfs_inode_t *ip,
129 xfs_bmbt_irec_t *imap)
130{
Dave Chinner6a19d932011-03-07 10:02:35 +1100131 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +1000132 "Access to block zero in inode %llu "
133 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -0500134 "blkcnt: %llx extent-state: %x",
Nathan Scott572d95f2006-09-28 11:03:20 +1000135 (unsigned long long)ip->i_ino,
136 (unsigned long long)imap->br_startblock,
137 (unsigned long long)imap->br_startoff,
138 (unsigned long long)imap->br_blockcount,
139 imap->br_state);
Dave Chinner24513372014-06-25 14:58:08 +1000140 return -EFSCORRUPTED;
Nathan Scott572d95f2006-09-28 11:03:20 +1000141}
142
Christoph Hellwiga206c812010-12-10 08:42:20 +0000143int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144xfs_iomap_write_direct(
145 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700146 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 size_t count,
Christoph Hellwig30704512010-06-24 11:42:19 +1000148 xfs_bmbt_irec_t *imap,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000149 int nmaps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 xfs_fileoff_t offset_fsb;
153 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100154 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 xfs_fsblock_t firstfsb;
Christoph Hellwigf13eb202017-02-06 10:42:26 -0800156 xfs_extlen_t extsz;
Eric Sandeen0116d932005-11-02 15:00:01 +1100157 int nimaps;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000158 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int rt;
160 xfs_trans_t *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000161 struct xfs_defer_ops dfops;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100162 uint qblocks, resblks, resrtextents;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100163 int error;
Brian Foster009c6e82015-10-12 15:34:20 +1100164 int lockmode;
Dave Chinner1ca19152015-11-03 12:37:00 +1100165 int bmapi_flags = XFS_BMAPI_PREALLOC;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000166 uint tflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Nathan Scottdd9f4382006-01-11 15:28:28 +1100168 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000169 extsz = xfs_get_extsz_hint(ip);
Brian Foster009c6e82015-10-12 15:34:20 +1100170 lockmode = XFS_ILOCK_SHARED; /* locked by caller */
171
172 ASSERT(xfs_isilocked(ip, lockmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David Chinner957d0eb2007-06-18 16:50:37 +1000174 offset_fsb = XFS_B_TO_FSBT(mp, offset);
175 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000176 if ((offset + count) > XFS_ISIZE(ip)) {
Brian Foster009c6e82015-10-12 15:34:20 +1100177 /*
178 * Assert that the in-core extent list is present since this can
179 * call xfs_iread_extents() and we only have the ilock shared.
180 * This should be safe because the lock was held around a bmapi
181 * call in the caller and we only need it to access the in-core
182 * list.
183 */
184 ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
185 XFS_IFEXTENTS);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000186 error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100187 if (error)
Brian Foster009c6e82015-10-12 15:34:20 +1100188 goto out_unlock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100189 } else {
Christoph Hellwig405f8042010-12-10 08:42:19 +0000190 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
Dave Chinner9bb54cb2018-06-07 07:54:02 -0700191 last_fsb = min(last_fsb, (xfs_fileoff_t)
Christoph Hellwig30704512010-06-24 11:42:19 +1000192 imap->br_blockcount +
193 imap->br_startoff);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100194 }
195 count_fsb = last_fsb - offset_fsb;
196 ASSERT(count_fsb > 0);
Christoph Hellwigf13eb202017-02-06 10:42:26 -0800197 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100198
199 if (unlikely(rt)) {
200 resrtextents = qblocks = resaligned;
201 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000202 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
203 quota_flag = XFS_QMOPT_RES_RTBLKS;
204 } else {
205 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100206 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000207 quota_flag = XFS_QMOPT_RES_REGBLKS;
208 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /*
Brian Foster009c6e82015-10-12 15:34:20 +1100211 * Drop the shared lock acquired by the caller, attach the dquot if
212 * necessary and move on to transaction setup.
213 */
214 xfs_iunlock(ip, lockmode);
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700215 error = xfs_qm_dqattach(ip);
Brian Foster009c6e82015-10-12 15:34:20 +1100216 if (error)
217 return error;
218
219 /*
Dave Chinner1ca19152015-11-03 12:37:00 +1100220 * For DAX, we do not allocate unwritten extents, but instead we zero
221 * the block before we commit the transaction. Ideally we'd like to do
222 * this outside the transaction context, but if we commit and then crash
223 * we may not have zeroed the blocks and this will be exposed on
224 * recovery of the allocation. Hence we must zero before commit.
Dave Chinner3b0fe472016-01-04 16:22:45 +1100225 *
Dave Chinner1ca19152015-11-03 12:37:00 +1100226 * Further, if we are mapping unwritten extents here, we need to zero
227 * and convert them to written so that we don't need an unwritten extent
228 * callback for DAX. This also means that we need to be able to dip into
Dave Chinner3b0fe472016-01-04 16:22:45 +1100229 * the reserve block pool for bmbt block allocation if there is no space
230 * left but we need to do unwritten extent conversion.
Dave Chinner1ca19152015-11-03 12:37:00 +1100231 */
232 if (IS_DAX(VFS_I(ip))) {
233 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
Christoph Hellwig63fbb4c2017-03-28 14:53:36 -0700234 if (imap->br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig253f4912016-04-06 09:19:55 +1000235 tflags |= XFS_TRANS_RESERVE;
Dave Chinner3b0fe472016-01-04 16:22:45 +1100236 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
237 }
Dave Chinner1ca19152015-11-03 12:37:00 +1100238 }
Christoph Hellwig253f4912016-04-06 09:19:55 +1000239 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
240 tflags, &tp);
241 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000242 return error;
Dave Chinner507630b2012-03-27 10:34:50 -0400243
Brian Foster009c6e82015-10-12 15:34:20 +1100244 lockmode = XFS_ILOCK_EXCL;
245 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Christoph Hellwig7d095252009-06-08 15:33:32 +0200247 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
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
Christoph Hellwigddc34152011-09-19 15:00:54 +0000251 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000254 * From this point onwards we overwrite the imap pointer that the
255 * caller gave to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000257 xfs_defer_init(&dfops, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000258 nimaps = 1;
Christoph Hellwigd531d912014-02-10 10:27:43 +1100259 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Dave Chinner264e89a2015-11-03 13:28:41 +1100260 bmapi_flags, &firstfsb, resblks, imap,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000261 &nimaps, &dfops);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000262 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400263 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000266 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700268 error = xfs_defer_finish(&tp, &dfops);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000269 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400270 goto out_bmap_cancel;
Dave Chinner1ca19152015-11-03 12:37:00 +1100271
Christoph Hellwig70393312015-06-04 13:48:08 +1000272 error = xfs_trans_commit(tp);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000273 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400274 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Nathan Scott06d10dd2005-06-21 15:48:47 +1000276 /*
277 * Copy any maps to caller's array and return any error.
278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (nimaps == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000280 error = -ENOSPC;
Dave Chinner507630b2012-03-27 10:34:50 -0400281 goto out_unlock;
Nathan Scott572d95f2006-09-28 11:03:20 +1000282 }
283
Dave Chinner507630b2012-03-27 10:34:50 -0400284 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100285 error = xfs_alert_fsblock_zero(ip, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Dave Chinner507630b2012-03-27 10:34:50 -0400287out_unlock:
Brian Foster009c6e82015-10-12 15:34:20 +1100288 xfs_iunlock(ip, lockmode);
Dave Chinner507630b2012-03-27 10:34:50 -0400289 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Dave Chinner507630b2012-03-27 10:34:50 -0400291out_bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000292 xfs_defer_cancel(&dfops);
Dave Chinnerea562ed2012-05-08 20:48:53 +1000293 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
Dave Chinner507630b2012-03-27 10:34:50 -0400294out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000295 xfs_trans_cancel(tp);
Dave Chinner507630b2012-03-27 10:34:50 -0400296 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Brian Foster76a42022013-03-18 10:51:47 -0400299STATIC bool
300xfs_quota_need_throttle(
301 struct xfs_inode *ip,
302 int type,
303 xfs_fsblock_t alloc_blocks)
304{
305 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
306
307 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
308 return false;
309
310 /* no hi watermark, no throttle */
311 if (!dq->q_prealloc_hi_wmark)
312 return false;
313
314 /* under the lo watermark, no throttle */
315 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
316 return false;
317
318 return true;
319}
320
321STATIC void
322xfs_quota_calc_throttle(
323 struct xfs_inode *ip,
324 int type,
325 xfs_fsblock_t *qblocks,
Brian Fosterf0740512014-07-24 19:56:08 +1000326 int *qshift,
327 int64_t *qfreesp)
Brian Foster76a42022013-03-18 10:51:47 -0400328{
329 int64_t freesp;
330 int shift = 0;
331 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
332
Eric Sandeen5cca3f62014-10-02 09:27:09 +1000333 /* no dq, or over hi wmark, squash the prealloc completely */
334 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
Brian Foster76a42022013-03-18 10:51:47 -0400335 *qblocks = 0;
Brian Fosterf0740512014-07-24 19:56:08 +1000336 *qfreesp = 0;
Brian Foster76a42022013-03-18 10:51:47 -0400337 return;
338 }
339
340 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
341 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
342 shift = 2;
343 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
344 shift += 2;
345 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
346 shift += 2;
347 }
348
Brian Fosterf0740512014-07-24 19:56:08 +1000349 if (freesp < *qfreesp)
350 *qfreesp = freesp;
351
Brian Foster76a42022013-03-18 10:51:47 -0400352 /* only overwrite the throttle values if we are more aggressive */
353 if ((freesp >> shift) < (*qblocks >> *qshift)) {
354 *qblocks = freesp;
355 *qshift = shift;
356 }
357}
358
Dave Chinnera1e16c22013-02-11 16:05:01 +1100359/*
Christoph Hellwig51446f52016-09-19 11:10:21 +1000360 * If we are doing a write at the end of the file and there are no allocations
361 * past this one, then extend the allocation out to the file system's write
362 * iosize.
363 *
Dave Chinner055388a2011-01-04 11:35:03 +1100364 * If we don't have a user specified preallocation size, dynamically increase
Christoph Hellwig51446f52016-09-19 11:10:21 +1000365 * the preallocation size as the size of the file grows. Cap the maximum size
Dave Chinner055388a2011-01-04 11:35:03 +1100366 * at a single extent or less if the filesystem is near full. The closer the
367 * filesystem is to full, the smaller the maximum prealocation.
Christoph Hellwig51446f52016-09-19 11:10:21 +1000368 *
369 * As an exception we don't do any preallocation at all if the file is smaller
370 * than the minimum preallocation and we are using the default dynamic
371 * preallocation scheme, as it is likely this is the only write to the file that
372 * is going to be done.
373 *
374 * We clean up any extra space left over when the file is closed in
375 * xfs_inactive().
Dave Chinner055388a2011-01-04 11:35:03 +1100376 */
377STATIC xfs_fsblock_t
378xfs_iomap_prealloc_size(
Dave Chinnera1e16c22013-02-11 16:05:01 +1100379 struct xfs_inode *ip,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000380 loff_t offset,
381 loff_t count,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700382 struct xfs_iext_cursor *icur)
Dave Chinner055388a2011-01-04 11:35:03 +1100383{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000384 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig656152e2016-11-24 11:39:44 +1100385 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000386 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig656152e2016-11-24 11:39:44 +1100387 struct xfs_bmbt_irec prev;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400388 int shift = 0;
389 int64_t freesp;
Brian Foster76a42022013-03-18 10:51:47 -0400390 xfs_fsblock_t qblocks;
391 int qshift = 0;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000392 xfs_fsblock_t alloc_blocks = 0;
Dave Chinner055388a2011-01-04 11:35:03 +1100393
Christoph Hellwig51446f52016-09-19 11:10:21 +1000394 if (offset + count <= XFS_ISIZE(ip))
395 return 0;
396
397 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
398 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
399 return 0;
400
401 /*
402 * If an explicit allocsize is set, the file is small, or we
403 * are writing behind a hole, then use the minimum prealloc:
404 */
405 if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
406 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700407 !xfs_iext_peek_prev_extent(ifp, icur, &prev) ||
Christoph Hellwig656152e2016-11-24 11:39:44 +1100408 prev.br_startoff + prev.br_blockcount < offset_fsb)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000409 return mp->m_writeio_blocks;
410
411 /*
412 * Determine the initial size of the preallocation. We are beyond the
413 * current EOF here, but we need to take into account whether this is
414 * a sparse write or an extending write when determining the
415 * preallocation size. Hence we need to look up the extent that ends
416 * at the current write offset and use the result to determine the
417 * preallocation size.
418 *
419 * If the extent is a hole, then preallocation is essentially disabled.
420 * Otherwise we take the size of the preceding data extent as the basis
421 * for the preallocation size. If the size of the extent is greater than
422 * half the maximum extent length, then use the current offset as the
423 * basis. This ensures that for large files the preallocation size
424 * always extends to MAXEXTLEN rather than falling short due to things
425 * like stripe unit/width alignment of real extents.
426 */
Christoph Hellwig656152e2016-11-24 11:39:44 +1100427 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
428 alloc_blocks = prev.br_blockcount << 1;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000429 else
430 alloc_blocks = XFS_B_TO_FSB(mp, offset);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400431 if (!alloc_blocks)
432 goto check_writeio;
Brian Foster76a42022013-03-18 10:51:47 -0400433 qblocks = alloc_blocks;
Dave Chinner055388a2011-01-04 11:35:03 +1100434
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400435 /*
436 * MAXEXTLEN is not a power of two value but we round the prealloc down
437 * to the nearest power of two value after throttling. To prevent the
438 * round down from unconditionally reducing the maximum supported prealloc
439 * size, we round up first, apply appropriate throttling, round down and
440 * cap the value to MAXEXTLEN.
441 */
442 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
443 alloc_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100444
Dave Chinner0d485ad2015-02-23 21:22:03 +1100445 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400446 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
447 shift = 2;
448 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
449 shift++;
450 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
451 shift++;
452 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
453 shift++;
454 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
455 shift++;
Dave Chinner055388a2011-01-04 11:35:03 +1100456 }
Brian Foster76a42022013-03-18 10:51:47 -0400457
458 /*
Brian Fosterf0740512014-07-24 19:56:08 +1000459 * Check each quota to cap the prealloc size, provide a shift value to
460 * throttle with and adjust amount of available space.
Brian Foster76a42022013-03-18 10:51:47 -0400461 */
462 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000463 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
464 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400465 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000466 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
467 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400468 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000469 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
470 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400471
472 /*
473 * The final prealloc size is set to the minimum of free space available
474 * in each of the quotas and the overall filesystem.
475 *
476 * The shift throttle value is set to the maximum value as determined by
477 * the global low free space values and per-quota low free space values.
478 */
Dave Chinner9bb54cb2018-06-07 07:54:02 -0700479 alloc_blocks = min(alloc_blocks, qblocks);
480 shift = max(shift, qshift);
Brian Foster76a42022013-03-18 10:51:47 -0400481
Brian Foster3c58b5f2013-03-18 10:51:43 -0400482 if (shift)
483 alloc_blocks >>= shift;
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400484 /*
485 * rounddown_pow_of_two() returns an undefined result if we pass in
486 * alloc_blocks = 0.
487 */
488 if (alloc_blocks)
489 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
490 if (alloc_blocks > MAXEXTLEN)
491 alloc_blocks = MAXEXTLEN;
Dave Chinner055388a2011-01-04 11:35:03 +1100492
Brian Foster3c58b5f2013-03-18 10:51:43 -0400493 /*
494 * If we are still trying to allocate more space than is
495 * available, squash the prealloc hard. This can happen if we
496 * have a large file on a small filesystem and the above
497 * lowspace thresholds are smaller than MAXEXTLEN.
498 */
499 while (alloc_blocks && alloc_blocks >= freesp)
500 alloc_blocks >>= 4;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400501check_writeio:
Dave Chinner055388a2011-01-04 11:35:03 +1100502 if (alloc_blocks < mp->m_writeio_blocks)
503 alloc_blocks = mp->m_writeio_blocks;
Brian Foster19cb7e32013-03-18 10:51:48 -0400504 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
505 mp->m_writeio_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100506 return alloc_blocks;
507}
508
Christoph Hellwig51446f52016-09-19 11:10:21 +1000509static int
510xfs_file_iomap_begin_delay(
511 struct inode *inode,
512 loff_t offset,
513 loff_t count,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000514 struct iomap *iomap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000516 struct xfs_inode *ip = XFS_I(inode);
517 struct xfs_mount *mp = ip->i_mount;
518 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
519 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
520 xfs_fileoff_t maxbytes_fsb =
521 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Brian Fosterf7820882016-11-28 14:57:42 +1100522 xfs_fileoff_t end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000523 int error = 0, eof = 0;
524 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700525 struct xfs_iext_cursor icur;
Brian Fosterf7820882016-11-28 14:57:42 +1100526 xfs_fsblock_t prealloc_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Christoph Hellwig51446f52016-09-19 11:10:21 +1000528 ASSERT(!XFS_IS_REALTIME_INODE(ip));
529 ASSERT(!xfs_get_extsz_hint(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Christoph Hellwig51446f52016-09-19 11:10:21 +1000531 xfs_ilock(ip, XFS_ILOCK_EXCL);
532
533 if (unlikely(XFS_TEST_ERROR(
534 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
535 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -0700536 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig51446f52016-09-19 11:10:21 +1000537 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
538 error = -EFSCORRUPTED;
539 goto out_unlock;
540 }
541
542 XFS_STATS_INC(mp, xs_blk_mapw);
543
544 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
545 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
546 if (error)
547 goto out_unlock;
548 }
549
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700550 eof = !xfs_iext_lookup_extent(ip, ifp, offset_fsb, &icur, &got);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000551 if (!eof && got.br_startoff <= offset_fsb) {
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100552 if (xfs_is_reflink_inode(ip)) {
553 bool shared;
554
555 end_fsb = min(XFS_B_TO_FSB(mp, offset + count),
556 maxbytes_fsb);
557 xfs_trim_extent(&got, offset_fsb, end_fsb - offset_fsb);
558 error = xfs_reflink_reserve_cow(ip, &got, &shared);
559 if (error)
560 goto out_unlock;
561 }
562
Christoph Hellwig51446f52016-09-19 11:10:21 +1000563 trace_xfs_iomap_found(ip, offset, count, 0, &got);
564 goto done;
565 }
566
Darrick J. Wong4882c192018-05-04 15:30:22 -0700567 error = xfs_qm_dqattach_locked(ip, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (error)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000569 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Christoph Hellwig51446f52016-09-19 11:10:21 +1000571 /*
572 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES pages
573 * to keep the chunks of work done where somewhat symmetric with the
574 * work writeback does. This is a completely arbitrary number pulled
575 * out of thin air as a best guess for initial testing.
576 *
577 * Note that the values needs to be less than 32-bits wide until
578 * the lower level functions are updated.
579 */
580 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
Brian Fosterf7820882016-11-28 14:57:42 +1100581 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100582
Christoph Hellwig51446f52016-09-19 11:10:21 +1000583 if (eof) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700584 prealloc_blocks = xfs_iomap_prealloc_size(ip, offset, count,
585 &icur);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000586 if (prealloc_blocks) {
587 xfs_extlen_t align;
588 xfs_off_t end_offset;
Brian Fosterf7820882016-11-28 14:57:42 +1100589 xfs_fileoff_t p_end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000590
591 end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
Brian Fosterf7820882016-11-28 14:57:42 +1100592 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
593 prealloc_blocks;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000594
595 align = xfs_eof_alignment(ip, 0);
596 if (align)
Brian Fosterf7820882016-11-28 14:57:42 +1100597 p_end_fsb = roundup_64(p_end_fsb, align);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000598
Brian Fosterf7820882016-11-28 14:57:42 +1100599 p_end_fsb = min(p_end_fsb, maxbytes_fsb);
600 ASSERT(p_end_fsb > offset_fsb);
601 prealloc_blocks = p_end_fsb - end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000602 }
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Dave Chinner8de2bf92009-04-06 18:49:12 +0200605retry:
Darrick J. Wongbe51f812016-10-03 09:11:32 -0700606 error = xfs_bmapi_reserve_delalloc(ip, XFS_DATA_FORK, offset_fsb,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700607 end_fsb - offset_fsb, prealloc_blocks, &got, &icur,
608 eof);
Dave Chinner055388a2011-01-04 11:35:03 +1100609 switch (error) {
610 case 0:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000611 break;
Dave Chinner24513372014-06-25 14:58:08 +1000612 case -ENOSPC:
613 case -EDQUOT:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000614 /* retry without any preallocation */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000615 trace_xfs_delalloc_enospc(ip, offset, count);
Brian Fosterf7820882016-11-28 14:57:42 +1100616 if (prealloc_blocks) {
617 prealloc_blocks = 0;
Dave Chinner9aa05002012-10-08 21:56:04 +1100618 goto retry;
Dave Chinner055388a2011-01-04 11:35:03 +1100619 }
Christoph Hellwig51446f52016-09-19 11:10:21 +1000620 /*FALLTHRU*/
621 default:
622 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Brian Fosterf65e6fa2017-03-08 09:58:08 -0800625 /*
626 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
627 * them out if the write happens to fail.
628 */
629 iomap->flags = IOMAP_F_NEW;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000630 trace_xfs_iomap_alloc(ip, offset, count, 0, &got);
631done:
632 if (isnullstartblock(got.br_startblock))
633 got.br_startblock = DELAYSTARTBLOCK;
634
635 if (!got.br_startblock) {
636 error = xfs_alert_fsblock_zero(ip, &got);
637 if (error)
638 goto out_unlock;
639 }
640
641 xfs_bmbt_to_iomap(ip, iomap, &got);
642
643out_unlock:
644 xfs_iunlock(ip, XFS_ILOCK_EXCL);
645 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/*
649 * Pass in a delayed allocate extent, convert it to real extents;
650 * return to the caller the extent we create which maps on top of
651 * the originating callers request.
652 *
653 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100654 *
655 * We no longer bother to look at the incoming map - all we have to
656 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Christoph Hellwiga206c812010-12-10 08:42:20 +0000658int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659xfs_iomap_write_allocate(
660 xfs_inode_t *ip,
Darrick J. Wong60b49842016-10-03 09:11:34 -0700661 int whichfork,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700662 xfs_off_t offset,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000663 xfs_bmbt_irec_t *imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 xfs_fileoff_t offset_fsb, last_block;
667 xfs_fileoff_t end_fsb, map_start_fsb;
668 xfs_fsblock_t first_block;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000669 struct xfs_defer_ops dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 xfs_filblks_t count_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 xfs_trans_t *tp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100672 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int error = 0;
Christoph Hellwigd2b39642017-01-20 09:31:54 -0800674 int flags = XFS_BMAPI_DELALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int nres;
676
Darrick J. Wong60b49842016-10-03 09:11:34 -0700677 if (whichfork == XFS_COW_FORK)
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800678 flags |= XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /*
681 * Make sure that the dquots are there.
682 */
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700683 error = xfs_qm_dqattach(ip);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200684 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000685 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Nathan Scott24e17b52005-05-05 13:33:20 -0700687 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig30704512010-06-24 11:42:19 +1000688 count_fsb = imap->br_blockcount;
689 map_start_fsb = imap->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100691 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 while (count_fsb != 0) {
694 /*
695 * Set up a transaction with which to allocate the
696 * backing store for the file. Do allocations in a
697 * loop until we get some space in the range we are
698 * interested in. The other space that might be allocated
699 * is in the delayed allocation extent on which we sit
700 * but before our buffer starts.
701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 nimaps = 0;
703 while (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
Christoph Hellwig0af32fb2016-08-17 08:30:28 +1000705 /*
706 * We have already reserved space for the extent and any
707 * indirect blocks when creating the delalloc extent,
708 * there is no need to reserve space in this transaction
709 * again.
710 */
711 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0,
Christoph Hellwig253f4912016-04-06 09:19:55 +1000712 0, XFS_TRANS_RESERVE, &tp);
713 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000714 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000717 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000719 xfs_defer_init(&dfops, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /*
David Chinnere4143a12007-11-23 16:29:11 +1100722 * it is possible that the extents have changed since
723 * we did the read call as we dropped the ilock for a
724 * while. We have to be careful about truncates or hole
725 * punchs here - we are not allowed to allocate
726 * non-delalloc blocks here.
727 *
728 * The only protection against truncation is the pages
729 * for the range we are being asked to convert are
730 * locked and hence a truncate will block on them
731 * first.
732 *
733 * As a result, if we go beyond the range we really
734 * need and hit an delalloc extent boundary followed by
735 * a hole while we have excess blocks in the map, we
736 * will fill the hole incorrectly and overrun the
737 * transaction reservation.
738 *
739 * Using a single map prevents this as we are forced to
740 * check each map we look for overlap with the desired
741 * range and abort as soon as we find it. Also, given
742 * that we only return a single map, having one beyond
743 * what we can return is probably a bit silly.
744 *
745 * We also need to check that we don't go beyond EOF;
746 * this is a truncate optimisation as a truncate sets
747 * the new file size before block on the pages we
748 * currently have locked under writeback. Because they
749 * are about to be tossed, we don't need to write them
750 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
David Chinnere4143a12007-11-23 16:29:11 +1100752 nimaps = 1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000753 end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000754 error = xfs_bmap_last_offset(ip, &last_block,
David Chinner7c9ef852008-04-10 12:21:59 +1000755 XFS_DATA_FORK);
756 if (error)
757 goto trans_cancel;
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
760 if ((map_start_fsb + count_fsb) > last_block) {
761 count_fsb = last_block - map_start_fsb;
762 if (count_fsb == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000763 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 goto trans_cancel;
765 }
766 }
767
Christoph Hellwig30704512010-06-24 11:42:19 +1000768 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000769 * From this point onwards we overwrite the imap
770 * pointer that the caller gave to us.
771 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000772 error = xfs_bmapi_write(tp, ip, map_start_fsb,
Darrick J. Wong60b49842016-10-03 09:11:34 -0700773 count_fsb, flags, &first_block,
Brian Fosterdbd5c8c2015-10-12 16:04:13 +1100774 nres, imap, &nimaps,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000775 &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (error)
777 goto trans_cancel;
778
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700779 error = xfs_defer_finish(&tp, &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (error)
781 goto trans_cancel;
782
Christoph Hellwig70393312015-06-04 13:48:08 +1000783 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (error)
785 goto error0;
786
787 xfs_iunlock(ip, XFS_ILOCK_EXCL);
788 }
789
790 /*
791 * See if we were able to allocate an extent that
792 * covers at least part of the callers request
793 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000794 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100795 return xfs_alert_fsblock_zero(ip, imap);
David Chinner86c4d622008-04-29 12:53:21 +1000796
Christoph Hellwig30704512010-06-24 11:42:19 +1000797 if ((offset_fsb >= imap->br_startoff) &&
798 (offset_fsb < (imap->br_startoff +
799 imap->br_blockcount))) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100800 XFS_STATS_INC(mp, xs_xstrat_quick);
David Chinnere4143a12007-11-23 16:29:11 +1100801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
David Chinnere4143a12007-11-23 16:29:11 +1100804 /*
805 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * file, just surrounding data, try again.
807 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000808 count_fsb -= imap->br_blockcount;
809 map_start_fsb = imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
812trans_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000813 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000814 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815error0:
816 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000817 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820int
821xfs_iomap_write_unwritten(
822 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700823 xfs_off_t offset,
Eryu Guanee70daa2017-09-21 11:26:18 -0700824 xfs_off_t count,
825 bool update_isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 xfs_fileoff_t offset_fsb;
829 xfs_filblks_t count_fsb;
830 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100831 xfs_fsblock_t firstfsb;
832 int nimaps;
833 xfs_trans_t *tp;
834 xfs_bmbt_irec_t imap;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000835 struct xfs_defer_ops dfops;
Eryu Guanee70daa2017-09-21 11:26:18 -0700836 struct inode *inode = VFS_I(ip);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000837 xfs_fsize_t i_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100838 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000841 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 offset_fsb = XFS_B_TO_FSBT(mp, offset);
844 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
845 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
846
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000847 /*
848 * Reserve enough blocks in this transaction for two complete extent
849 * btree splits. We may be converting the middle part of an unwritten
850 * extent and in this case we will insert two new extents in the btree
851 * each of which could cause a full split.
852 *
853 * This reservation amount will be used in the first call to
854 * xfs_bmbt_split() to select an AG with enough space to satisfy the
855 * rest of the operation.
856 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100857 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Nathan Scottdd9f4382006-01-11 15:28:28 +1100859 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /*
Christoph Hellwig253f4912016-04-06 09:19:55 +1000861 * Set up a transaction to convert the range of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * from unwritten to real. Do allocations in a loop until
863 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000864 *
Christoph Hellwig253f4912016-04-06 09:19:55 +1000865 * Note that we can't risk to recursing back into the filesystem
866 * here as we might be asked to write out the same inode that we
867 * complete here and might deadlock on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000869 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
870 XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
871 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000872 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000875 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /*
878 * Modify the unwritten extent state of the buffer.
879 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000880 xfs_defer_init(&dfops, &firstfsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 nimaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000882 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Brian Fosterdbd5c8c2015-10-12 16:04:13 +1100883 XFS_BMAPI_CONVERT, &firstfsb, resblks,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000884 &imap, &nimaps, &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (error)
886 goto error_on_bmapi_transaction;
887
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000888 /*
889 * Log the updated inode size as we go. We have to be careful
890 * to only log it up to the actual write offset if it is
891 * halfway into a block.
892 */
893 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
894 if (i_size > offset + count)
895 i_size = offset + count;
Eryu Guanee70daa2017-09-21 11:26:18 -0700896 if (update_isize && i_size > i_size_read(inode))
897 i_size_write(inode, i_size);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000898 i_size = xfs_new_eof(ip, i_size);
899 if (i_size) {
900 ip->i_d.di_size = i_size;
901 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
902 }
903
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700904 error = xfs_defer_finish(&tp, &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (error)
906 goto error_on_bmapi_transaction;
907
Christoph Hellwig70393312015-06-04 13:48:08 +1000908 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 xfs_iunlock(ip, XFS_ILOCK_EXCL);
910 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000911 return error;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100912
David Chinner86c4d622008-04-29 12:53:21 +1000913 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100914 return xfs_alert_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if ((numblks_fsb = imap.br_blockcount) == 0) {
917 /*
918 * The numblks_fsb value should always get
919 * smaller, otherwise the loop is stuck.
920 */
921 ASSERT(imap.br_blockcount);
922 break;
923 }
924 offset_fsb += numblks_fsb;
925 count_fsb -= numblks_fsb;
926 } while (count_fsb > 0);
927
928 return 0;
929
930error_on_bmapi_transaction:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000931 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000932 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000934 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
Christoph Hellwig3b3dce02016-06-21 09:52:47 +1000936
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700937static inline bool
938imap_needs_alloc(
939 struct inode *inode,
940 struct xfs_bmbt_irec *imap,
941 int nimaps)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000942{
943 return !nimaps ||
944 imap->br_startblock == HOLESTARTBLOCK ||
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000945 imap->br_startblock == DELAYSTARTBLOCK ||
Christoph Hellwig63fbb4c2017-03-28 14:53:36 -0700946 (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000947}
948
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700949static inline bool
950needs_cow_for_zeroing(
951 struct xfs_bmbt_irec *imap,
952 int nimaps)
Christoph Hellwig172ed392018-03-01 14:10:31 -0800953{
954 return nimaps &&
955 imap->br_startblock != HOLESTARTBLOCK &&
956 imap->br_state != XFS_EXT_UNWRITTEN;
957}
958
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700959static int
960xfs_ilock_for_iomap(
961 struct xfs_inode *ip,
962 unsigned flags,
963 unsigned *lockmode)
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100964{
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700965 unsigned mode = XFS_ILOCK_SHARED;
Darrick J. Wong5bd88d12018-06-21 23:26:57 -0700966 bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700967
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100968 /*
Christoph Hellwigaf5b5af2018-03-01 14:12:12 -0800969 * COW writes may allocate delalloc space or convert unwritten COW
970 * extents, so we need to make sure to take the lock exclusively here.
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100971 */
Darrick J. Wong5bd88d12018-06-21 23:26:57 -0700972 if (xfs_is_reflink_inode(ip) && is_write) {
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700973 /*
974 * FIXME: It could still overwrite on unshared extents and not
975 * need allocation.
976 */
977 if (flags & IOMAP_NOWAIT)
978 return -EAGAIN;
979 mode = XFS_ILOCK_EXCL;
980 }
Christoph Hellwigff3d8b92018-03-01 14:12:45 -0800981
982 /*
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700983 * Extents not yet cached requires exclusive access, don't block. This
984 * is an opencoded xfs_ilock_data_map_shared() call but with
Christoph Hellwigff3d8b92018-03-01 14:12:45 -0800985 * non-blocking behaviour.
986 */
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700987 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
988 if (flags & IOMAP_NOWAIT)
989 return -EAGAIN;
990 mode = XFS_ILOCK_EXCL;
991 }
992
Darrick J. Wong5bd88d12018-06-21 23:26:57 -0700993relock:
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700994 if (flags & IOMAP_NOWAIT) {
995 if (!xfs_ilock_nowait(ip, mode))
996 return -EAGAIN;
997 } else {
998 xfs_ilock(ip, mode);
999 }
1000
Darrick J. Wong5bd88d12018-06-21 23:26:57 -07001001 /*
1002 * The reflink iflag could have changed since the earlier unlocked
1003 * check, so if we got ILOCK_SHARED for a write and but we're now a
1004 * reflink inode we have to switch to ILOCK_EXCL and relock.
1005 */
1006 if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_reflink_inode(ip)) {
1007 xfs_iunlock(ip, mode);
1008 mode = XFS_ILOCK_EXCL;
1009 goto relock;
1010 }
1011
Dave Chinnerdfa03a52018-05-02 12:54:54 -07001012 *lockmode = mode;
1013 return 0;
Christoph Hellwigacdda3a2016-11-30 14:37:15 +11001014}
1015
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001016static int
1017xfs_file_iomap_begin(
1018 struct inode *inode,
1019 loff_t offset,
1020 loff_t length,
1021 unsigned flags,
1022 struct iomap *iomap)
1023{
1024 struct xfs_inode *ip = XFS_I(inode);
1025 struct xfs_mount *mp = ip->i_mount;
1026 struct xfs_bmbt_irec imap;
1027 xfs_fileoff_t offset_fsb, end_fsb;
1028 int nimaps = 1, error = 0;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001029 bool shared = false, trimmed = false;
Christoph Hellwig66642c52016-09-19 11:26:39 +10001030 unsigned lockmode;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001031
1032 if (XFS_FORCED_SHUTDOWN(mp))
1033 return -EIO;
1034
Christoph Hellwigacdda3a2016-11-30 14:37:15 +11001035 if (((flags & (IOMAP_WRITE | IOMAP_DIRECT)) == IOMAP_WRITE) &&
1036 !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
Darrick J. Wong2a067052016-10-03 09:11:33 -07001037 /* Reserve delalloc blocks for regular writeback. */
Eric Sandeenf91fb952017-08-30 09:24:12 -07001038 return xfs_file_iomap_begin_delay(inode, offset, length, iomap);
Christoph Hellwig51446f52016-09-19 11:10:21 +10001039 }
1040
Dave Chinnerdfa03a52018-05-02 12:54:54 -07001041 /*
1042 * Lock the inode in the manner required for the specified operation and
1043 * check for as many conditions that would result in blocking as
1044 * possible. This removes most of the non-blocking checks from the
1045 * mapping code below.
1046 */
1047 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1048 if (error)
1049 return error;
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -05001050
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001051 ASSERT(offset <= mp->m_super->s_maxbytes);
Darrick J. Wongb4d8ad7f2017-12-22 13:14:34 -08001052 if (offset > mp->m_super->s_maxbytes - length)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001053 length = mp->m_super->s_maxbytes - offset;
1054 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1055 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1056
1057 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001058 &nimaps, 0);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001059 if (error)
1060 goto out_unlock;
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001061
Christoph Hellwig3c68d442017-02-06 10:51:03 -08001062 if (flags & IOMAP_REPORT) {
Christoph Hellwig5f9268c2016-10-20 15:53:32 +11001063 /* Trim the mapping to the nearest shared extent boundary. */
1064 error = xfs_reflink_trim_around_shared(ip, &imap, &shared,
1065 &trimmed);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001066 if (error)
1067 goto out_unlock;
1068 }
1069
Dave Chinnerd0641782018-05-02 12:54:53 -07001070 /* Non-modifying mapping requested, so we are done */
1071 if (!(flags & (IOMAP_WRITE | IOMAP_ZERO)))
1072 goto out_found;
1073
Dave Chinnerdfa03a52018-05-02 12:54:54 -07001074 /*
1075 * Break shared extents if necessary. Checks for non-blocking IO have
1076 * been done up front, so we don't need to do them here.
1077 */
1078 if (xfs_is_reflink_inode(ip)) {
1079 /* if zeroing doesn't need COW allocation, then we are done. */
1080 if ((flags & IOMAP_ZERO) &&
1081 !needs_cow_for_zeroing(&imap, nimaps))
1082 goto out_found;
1083
Christoph Hellwig3c68d442017-02-06 10:51:03 -08001084 if (flags & IOMAP_DIRECT) {
1085 /* may drop and re-acquire the ilock */
1086 error = xfs_reflink_allocate_cow(ip, &imap, &shared,
1087 &lockmode);
1088 if (error)
1089 goto out_unlock;
1090 } else {
1091 error = xfs_reflink_reserve_cow(ip, &imap, &shared);
1092 if (error)
1093 goto out_unlock;
1094 }
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001095
1096 end_fsb = imap.br_startoff + imap.br_blockcount;
1097 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001098 }
1099
Dave Chinnerd0641782018-05-02 12:54:53 -07001100 /* Don't need to allocate over holes when doing zeroing operations. */
1101 if (flags & IOMAP_ZERO)
1102 goto out_found;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001103
Dave Chinnerd0641782018-05-02 12:54:53 -07001104 if (!imap_needs_alloc(inode, &imap, nimaps))
1105 goto out_found;
Christoph Hellwigb95a2122016-08-17 08:44:52 +10001106
Dave Chinnerd0641782018-05-02 12:54:53 -07001107 /* If nowait is set bail since we are going to make allocations. */
1108 if (flags & IOMAP_NOWAIT) {
1109 error = -EAGAIN;
1110 goto out_unlock;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001111 }
1112
Dave Chinnerd0641782018-05-02 12:54:53 -07001113 /*
1114 * We cap the maximum length we map to a sane size to keep the chunks
1115 * of work done where somewhat symmetric with the work writeback does.
1116 * This is a completely arbitrary number pulled out of thin air as a
1117 * best guess for initial testing.
1118 *
1119 * Note that the values needs to be less than 32-bits wide until the
1120 * lower level functions are updated.
1121 */
1122 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
1123
1124 /*
1125 * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
1126 * return.
1127 */
1128 if (lockmode == XFS_ILOCK_EXCL)
1129 xfs_ilock_demote(ip, lockmode);
1130 error = xfs_iomap_write_direct(ip, offset, length, &imap,
1131 nimaps);
1132 if (error)
1133 return error;
1134
1135 iomap->flags = IOMAP_F_NEW;
1136 trace_xfs_iomap_alloc(ip, offset, length, 0, &imap);
1137
1138out_finish:
Dan Williamsaaa422c2017-11-13 16:38:44 -08001139 if (xfs_ipincount(ip) && (ip->i_itemp->ili_fsync_fields
1140 & ~XFS_ILOG_TIMESTAMP))
Christoph Hellwiga39e5962017-11-01 16:36:47 +01001141 iomap->flags |= IOMAP_F_DIRTY;
1142
Christoph Hellwigb95a2122016-08-17 08:44:52 +10001143 xfs_bmbt_to_iomap(ip, iomap, &imap);
Dan Williamsfa5d9322017-01-27 12:04:59 -08001144
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001145 if (shared)
1146 iomap->flags |= IOMAP_F_SHARED;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001147 return 0;
Dave Chinnerd0641782018-05-02 12:54:53 -07001148
1149out_found:
1150 ASSERT(nimaps);
1151 xfs_iunlock(ip, lockmode);
1152 trace_xfs_iomap_found(ip, offset, length, 0, &imap);
1153 goto out_finish;
1154
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001155out_unlock:
1156 xfs_iunlock(ip, lockmode);
1157 return error;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001158}
1159
1160static int
1161xfs_file_iomap_end_delalloc(
1162 struct xfs_inode *ip,
1163 loff_t offset,
1164 loff_t length,
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001165 ssize_t written,
1166 struct iomap *iomap)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001167{
1168 struct xfs_mount *mp = ip->i_mount;
1169 xfs_fileoff_t start_fsb;
1170 xfs_fileoff_t end_fsb;
1171 int error = 0;
1172
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001173 /*
1174 * Behave as if the write failed if drop writes is enabled. Set the NEW
1175 * flag to force delalloc cleanup.
1176 */
Darrick J. Wongf8c47252017-06-20 17:54:48 -07001177 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001178 iomap->flags |= IOMAP_F_NEW;
Brian Foster9dbddd72017-02-13 22:48:17 -08001179 written = 0;
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001180 }
Brian Foster9dbddd72017-02-13 22:48:17 -08001181
Brian Fosterfa7f1382017-02-16 17:19:12 -08001182 /*
1183 * start_fsb refers to the first unused block after a short write. If
1184 * nothing was written, round offset down to point at the first block in
1185 * the range.
1186 */
1187 if (unlikely(!written))
1188 start_fsb = XFS_B_TO_FSBT(mp, offset);
1189 else
1190 start_fsb = XFS_B_TO_FSB(mp, offset + written);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001191 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1192
1193 /*
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001194 * Trim delalloc blocks if they were allocated by this write and we
1195 * didn't manage to write the whole range.
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001196 *
1197 * We don't need to care about racing delalloc as we hold i_mutex
1198 * across the reserve/allocate/unreserve calls. If there are delalloc
1199 * blocks in the range, they are ours.
1200 */
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001201 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
Brian Fosterfa7f1382017-02-16 17:19:12 -08001202 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1203 XFS_FSB_TO_B(mp, end_fsb) - 1);
1204
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001205 xfs_ilock(ip, XFS_ILOCK_EXCL);
1206 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1207 end_fsb - start_fsb);
1208 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1209
1210 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1211 xfs_alert(mp, "%s: unable to clean up ino %lld",
1212 __func__, ip->i_ino);
1213 return error;
1214 }
1215 }
1216
1217 return 0;
1218}
1219
1220static int
1221xfs_file_iomap_end(
1222 struct inode *inode,
1223 loff_t offset,
1224 loff_t length,
1225 ssize_t written,
1226 unsigned flags,
1227 struct iomap *iomap)
1228{
1229 if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
1230 return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001231 length, written, iomap);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001232 return 0;
1233}
1234
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001235const struct iomap_ops xfs_iomap_ops = {
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001236 .iomap_begin = xfs_file_iomap_begin,
1237 .iomap_end = xfs_file_iomap_end,
1238};
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001239
1240static int
1241xfs_xattr_iomap_begin(
1242 struct inode *inode,
1243 loff_t offset,
1244 loff_t length,
1245 unsigned flags,
1246 struct iomap *iomap)
1247{
1248 struct xfs_inode *ip = XFS_I(inode);
1249 struct xfs_mount *mp = ip->i_mount;
1250 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1251 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1252 struct xfs_bmbt_irec imap;
1253 int nimaps = 1, error = 0;
1254 unsigned lockmode;
1255
1256 if (XFS_FORCED_SHUTDOWN(mp))
1257 return -EIO;
1258
Darrick J. Wong84358536d2017-04-06 16:00:39 -07001259 lockmode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001260
1261 /* if there are no attribute fork or extents, return ENOENT */
Darrick J. Wong84358536d2017-04-06 16:00:39 -07001262 if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001263 error = -ENOENT;
1264 goto out_unlock;
1265 }
1266
1267 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1268 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongb7e0b6f2017-12-06 16:13:35 -08001269 &nimaps, XFS_BMAPI_ATTRFORK);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001270out_unlock:
1271 xfs_iunlock(ip, lockmode);
1272
1273 if (!error) {
1274 ASSERT(nimaps);
1275 xfs_bmbt_to_iomap(ip, iomap, &imap);
1276 }
1277
1278 return error;
1279}
1280
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001281const struct iomap_ops xfs_xattr_iomap_ops = {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001282 .iomap_begin = xfs_xattr_iomap_begin,
1283};