blob: 27c93b5f029df92b17c22456c0bfe7a5a3fa085c [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 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
Christoph Hellwig0365c5d2018-10-18 17:19:26 +110065static void
66xfs_hole_to_iomap(
67 struct xfs_inode *ip,
68 struct iomap *iomap,
69 xfs_fileoff_t offset_fsb,
70 xfs_fileoff_t end_fsb)
71{
72 iomap->addr = IOMAP_NULL_ADDR;
73 iomap->type = IOMAP_HOLE;
74 iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb);
75 iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb);
76 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
77 iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
78}
79
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070080xfs_extlen_t
Christoph Hellwigf8e3a822016-09-19 11:09:28 +100081xfs_eof_alignment(
82 struct xfs_inode *ip,
83 xfs_extlen_t extsize)
Nathan Scottdd9f4382006-01-11 15:28:28 +110084{
Christoph Hellwigf8e3a822016-09-19 11:09:28 +100085 struct xfs_mount *mp = ip->i_mount;
86 xfs_extlen_t align = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +110087
Christoph Hellwigbf322d92011-12-18 20:00:05 +000088 if (!XFS_IS_REALTIME_INODE(ip)) {
89 /*
90 * Round up the allocation request to a stripe unit
91 * (m_dalign) boundary if the file size is >= stripe unit
92 * size, and we are allocating past the allocation eof.
93 *
94 * If mounted with the "-o swalloc" option the alignment is
95 * increased from the strip unit size to the stripe width.
96 */
97 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
98 align = mp->m_swidth;
99 else if (mp->m_dalign)
100 align = mp->m_dalign;
101
Peter Watkins76b57302014-12-04 09:30:51 +1100102 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
103 align = 0;
Christoph Hellwigbf322d92011-12-18 20:00:05 +0000104 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100105
106 /*
107 * Always round up the allocation request to an extent boundary
108 * (when file on a real-time subvolume or has di_extsize hint).
109 */
110 if (extsize) {
Peter Watkins76b57302014-12-04 09:30:51 +1100111 if (align)
112 align = roundup_64(align, extsize);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100113 else
114 align = extsize;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100115 }
116
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000117 return align;
118}
119
120STATIC int
121xfs_iomap_eof_align_last_fsb(
122 struct xfs_inode *ip,
123 xfs_extlen_t extsize,
124 xfs_fileoff_t *last_fsb)
125{
126 xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
127
Peter Watkins76b57302014-12-04 09:30:51 +1100128 if (align) {
129 xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000130 int eof, error;
131
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000132 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100133 if (error)
134 return error;
135 if (eof)
136 *last_fsb = new_last_fsb;
137 }
138 return 0;
139}
140
141STATIC int
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100142xfs_alert_fsblock_zero(
Nathan Scott572d95f2006-09-28 11:03:20 +1000143 xfs_inode_t *ip,
144 xfs_bmbt_irec_t *imap)
145{
Dave Chinner6a19d932011-03-07 10:02:35 +1100146 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +1000147 "Access to block zero in inode %llu "
148 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -0500149 "blkcnt: %llx extent-state: %x",
Nathan Scott572d95f2006-09-28 11:03:20 +1000150 (unsigned long long)ip->i_ino,
151 (unsigned long long)imap->br_startblock,
152 (unsigned long long)imap->br_startoff,
153 (unsigned long long)imap->br_blockcount,
154 imap->br_state);
Dave Chinner24513372014-06-25 14:58:08 +1000155 return -EFSCORRUPTED;
Nathan Scott572d95f2006-09-28 11:03:20 +1000156}
157
Christoph Hellwiga206c812010-12-10 08:42:20 +0000158int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159xfs_iomap_write_direct(
160 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700161 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 size_t count,
Christoph Hellwig30704512010-06-24 11:42:19 +1000163 xfs_bmbt_irec_t *imap,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000164 int nmaps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 xfs_fileoff_t offset_fsb;
168 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100169 xfs_filblks_t count_fsb, resaligned;
Christoph Hellwigf13eb202017-02-06 10:42:26 -0800170 xfs_extlen_t extsz;
Eric Sandeen0116d932005-11-02 15:00:01 +1100171 int nimaps;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000172 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 int rt;
174 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100175 uint qblocks, resblks, resrtextents;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100176 int error;
Brian Foster009c6e82015-10-12 15:34:20 +1100177 int lockmode;
Dave Chinner1ca19152015-11-03 12:37:00 +1100178 int bmapi_flags = XFS_BMAPI_PREALLOC;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000179 uint tflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Nathan Scottdd9f4382006-01-11 15:28:28 +1100181 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000182 extsz = xfs_get_extsz_hint(ip);
Brian Foster009c6e82015-10-12 15:34:20 +1100183 lockmode = XFS_ILOCK_SHARED; /* locked by caller */
184
185 ASSERT(xfs_isilocked(ip, lockmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
David Chinner957d0eb2007-06-18 16:50:37 +1000187 offset_fsb = XFS_B_TO_FSBT(mp, offset);
188 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000189 if ((offset + count) > XFS_ISIZE(ip)) {
Brian Foster009c6e82015-10-12 15:34:20 +1100190 /*
191 * Assert that the in-core extent list is present since this can
192 * call xfs_iread_extents() and we only have the ilock shared.
193 * This should be safe because the lock was held around a bmapi
194 * call in the caller and we only need it to access the in-core
195 * list.
196 */
197 ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
198 XFS_IFEXTENTS);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000199 error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100200 if (error)
Brian Foster009c6e82015-10-12 15:34:20 +1100201 goto out_unlock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100202 } else {
Christoph Hellwig405f8042010-12-10 08:42:19 +0000203 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
Dave Chinner9bb54cb2018-06-07 07:54:02 -0700204 last_fsb = min(last_fsb, (xfs_fileoff_t)
Christoph Hellwig30704512010-06-24 11:42:19 +1000205 imap->br_blockcount +
206 imap->br_startoff);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100207 }
208 count_fsb = last_fsb - offset_fsb;
209 ASSERT(count_fsb > 0);
Christoph Hellwigf13eb202017-02-06 10:42:26 -0800210 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100211
212 if (unlikely(rt)) {
213 resrtextents = qblocks = resaligned;
214 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000215 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
216 quota_flag = XFS_QMOPT_RES_RTBLKS;
217 } else {
218 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100219 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000220 quota_flag = XFS_QMOPT_RES_REGBLKS;
221 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /*
Brian Foster009c6e82015-10-12 15:34:20 +1100224 * Drop the shared lock acquired by the caller, attach the dquot if
225 * necessary and move on to transaction setup.
226 */
227 xfs_iunlock(ip, lockmode);
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700228 error = xfs_qm_dqattach(ip);
Brian Foster009c6e82015-10-12 15:34:20 +1100229 if (error)
230 return error;
231
232 /*
Dave Chinner1ca19152015-11-03 12:37:00 +1100233 * For DAX, we do not allocate unwritten extents, but instead we zero
234 * the block before we commit the transaction. Ideally we'd like to do
235 * this outside the transaction context, but if we commit and then crash
236 * we may not have zeroed the blocks and this will be exposed on
237 * recovery of the allocation. Hence we must zero before commit.
Dave Chinner3b0fe472016-01-04 16:22:45 +1100238 *
Dave Chinner1ca19152015-11-03 12:37:00 +1100239 * Further, if we are mapping unwritten extents here, we need to zero
240 * and convert them to written so that we don't need an unwritten extent
241 * callback for DAX. This also means that we need to be able to dip into
Dave Chinner3b0fe472016-01-04 16:22:45 +1100242 * the reserve block pool for bmbt block allocation if there is no space
243 * left but we need to do unwritten extent conversion.
Dave Chinner1ca19152015-11-03 12:37:00 +1100244 */
245 if (IS_DAX(VFS_I(ip))) {
246 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
Christoph Hellwig63fbb4c2017-03-28 14:53:36 -0700247 if (imap->br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig253f4912016-04-06 09:19:55 +1000248 tflags |= XFS_TRANS_RESERVE;
Dave Chinner3b0fe472016-01-04 16:22:45 +1100249 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
250 }
Dave Chinner1ca19152015-11-03 12:37:00 +1100251 }
Christoph Hellwig253f4912016-04-06 09:19:55 +1000252 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
253 tflags, &tp);
254 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000255 return error;
Dave Chinner507630b2012-03-27 10:34:50 -0400256
Brian Foster009c6e82015-10-12 15:34:20 +1100257 lockmode = XFS_ILOCK_EXCL;
258 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Christoph Hellwig7d095252009-06-08 15:33:32 +0200260 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100261 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400262 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Christoph Hellwigddc34152011-09-19 15:00:54 +0000264 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000267 * From this point onwards we overwrite the imap pointer that the
268 * caller gave to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
Nathan Scott06d10dd2005-06-21 15:48:47 +1000270 nimaps = 1;
Christoph Hellwigd531d912014-02-10 10:27:43 +1100271 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Brian Fostera7beabe2018-07-11 22:26:25 -0700272 bmapi_flags, resblks, imap, &nimaps);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000273 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700274 goto out_res_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000277 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Christoph Hellwig70393312015-06-04 13:48:08 +1000279 error = xfs_trans_commit(tp);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000280 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400281 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Nathan Scott06d10dd2005-06-21 15:48:47 +1000283 /*
284 * Copy any maps to caller's array and return any error.
285 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (nimaps == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000287 error = -ENOSPC;
Dave Chinner507630b2012-03-27 10:34:50 -0400288 goto out_unlock;
Nathan Scott572d95f2006-09-28 11:03:20 +1000289 }
290
Dave Chinner507630b2012-03-27 10:34:50 -0400291 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100292 error = xfs_alert_fsblock_zero(ip, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Dave Chinner507630b2012-03-27 10:34:50 -0400294out_unlock:
Brian Foster009c6e82015-10-12 15:34:20 +1100295 xfs_iunlock(ip, lockmode);
Dave Chinner507630b2012-03-27 10:34:50 -0400296 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Brian Fosterc8eac492018-07-24 13:43:13 -0700298out_res_cancel:
Dave Chinnerea562ed2012-05-08 20:48:53 +1000299 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
Dave Chinner507630b2012-03-27 10:34:50 -0400300out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000301 xfs_trans_cancel(tp);
Dave Chinner507630b2012-03-27 10:34:50 -0400302 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Brian Foster76a42022013-03-18 10:51:47 -0400305STATIC bool
306xfs_quota_need_throttle(
307 struct xfs_inode *ip,
308 int type,
309 xfs_fsblock_t alloc_blocks)
310{
311 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
312
313 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
314 return false;
315
316 /* no hi watermark, no throttle */
317 if (!dq->q_prealloc_hi_wmark)
318 return false;
319
320 /* under the lo watermark, no throttle */
321 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
322 return false;
323
324 return true;
325}
326
327STATIC void
328xfs_quota_calc_throttle(
329 struct xfs_inode *ip,
330 int type,
331 xfs_fsblock_t *qblocks,
Brian Fosterf0740512014-07-24 19:56:08 +1000332 int *qshift,
333 int64_t *qfreesp)
Brian Foster76a42022013-03-18 10:51:47 -0400334{
335 int64_t freesp;
336 int shift = 0;
337 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
338
Eric Sandeen5cca3f62014-10-02 09:27:09 +1000339 /* no dq, or over hi wmark, squash the prealloc completely */
340 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
Brian Foster76a42022013-03-18 10:51:47 -0400341 *qblocks = 0;
Brian Fosterf0740512014-07-24 19:56:08 +1000342 *qfreesp = 0;
Brian Foster76a42022013-03-18 10:51:47 -0400343 return;
344 }
345
346 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
347 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
348 shift = 2;
349 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
350 shift += 2;
351 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
352 shift += 2;
353 }
354
Brian Fosterf0740512014-07-24 19:56:08 +1000355 if (freesp < *qfreesp)
356 *qfreesp = freesp;
357
Brian Foster76a42022013-03-18 10:51:47 -0400358 /* only overwrite the throttle values if we are more aggressive */
359 if ((freesp >> shift) < (*qblocks >> *qshift)) {
360 *qblocks = freesp;
361 *qshift = shift;
362 }
363}
364
Dave Chinnera1e16c22013-02-11 16:05:01 +1100365/*
Christoph Hellwig51446f52016-09-19 11:10:21 +1000366 * If we are doing a write at the end of the file and there are no allocations
367 * past this one, then extend the allocation out to the file system's write
368 * iosize.
369 *
Dave Chinner055388a2011-01-04 11:35:03 +1100370 * If we don't have a user specified preallocation size, dynamically increase
Christoph Hellwig51446f52016-09-19 11:10:21 +1000371 * the preallocation size as the size of the file grows. Cap the maximum size
Dave Chinner055388a2011-01-04 11:35:03 +1100372 * at a single extent or less if the filesystem is near full. The closer the
373 * filesystem is to full, the smaller the maximum prealocation.
Christoph Hellwig51446f52016-09-19 11:10:21 +1000374 *
375 * As an exception we don't do any preallocation at all if the file is smaller
376 * than the minimum preallocation and we are using the default dynamic
377 * preallocation scheme, as it is likely this is the only write to the file that
378 * is going to be done.
379 *
380 * We clean up any extra space left over when the file is closed in
381 * xfs_inactive().
Dave Chinner055388a2011-01-04 11:35:03 +1100382 */
383STATIC xfs_fsblock_t
384xfs_iomap_prealloc_size(
Dave Chinnera1e16c22013-02-11 16:05:01 +1100385 struct xfs_inode *ip,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000386 loff_t offset,
387 loff_t count,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700388 struct xfs_iext_cursor *icur)
Dave Chinner055388a2011-01-04 11:35:03 +1100389{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000390 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig656152e2016-11-24 11:39:44 +1100391 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000392 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig656152e2016-11-24 11:39:44 +1100393 struct xfs_bmbt_irec prev;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400394 int shift = 0;
395 int64_t freesp;
Brian Foster76a42022013-03-18 10:51:47 -0400396 xfs_fsblock_t qblocks;
397 int qshift = 0;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000398 xfs_fsblock_t alloc_blocks = 0;
Dave Chinner055388a2011-01-04 11:35:03 +1100399
Christoph Hellwig51446f52016-09-19 11:10:21 +1000400 if (offset + count <= XFS_ISIZE(ip))
401 return 0;
402
403 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
404 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
405 return 0;
406
407 /*
408 * If an explicit allocsize is set, the file is small, or we
409 * are writing behind a hole, then use the minimum prealloc:
410 */
411 if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
412 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700413 !xfs_iext_peek_prev_extent(ifp, icur, &prev) ||
Christoph Hellwig656152e2016-11-24 11:39:44 +1100414 prev.br_startoff + prev.br_blockcount < offset_fsb)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000415 return mp->m_writeio_blocks;
416
417 /*
418 * Determine the initial size of the preallocation. We are beyond the
419 * current EOF here, but we need to take into account whether this is
420 * a sparse write or an extending write when determining the
421 * preallocation size. Hence we need to look up the extent that ends
422 * at the current write offset and use the result to determine the
423 * preallocation size.
424 *
425 * If the extent is a hole, then preallocation is essentially disabled.
426 * Otherwise we take the size of the preceding data extent as the basis
427 * for the preallocation size. If the size of the extent is greater than
428 * half the maximum extent length, then use the current offset as the
429 * basis. This ensures that for large files the preallocation size
430 * always extends to MAXEXTLEN rather than falling short due to things
431 * like stripe unit/width alignment of real extents.
432 */
Christoph Hellwig656152e2016-11-24 11:39:44 +1100433 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
434 alloc_blocks = prev.br_blockcount << 1;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000435 else
436 alloc_blocks = XFS_B_TO_FSB(mp, offset);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400437 if (!alloc_blocks)
438 goto check_writeio;
Brian Foster76a42022013-03-18 10:51:47 -0400439 qblocks = alloc_blocks;
Dave Chinner055388a2011-01-04 11:35:03 +1100440
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400441 /*
442 * MAXEXTLEN is not a power of two value but we round the prealloc down
443 * to the nearest power of two value after throttling. To prevent the
444 * round down from unconditionally reducing the maximum supported prealloc
445 * size, we round up first, apply appropriate throttling, round down and
446 * cap the value to MAXEXTLEN.
447 */
448 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
449 alloc_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100450
Dave Chinner0d485ad2015-02-23 21:22:03 +1100451 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400452 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
453 shift = 2;
454 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
455 shift++;
456 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
457 shift++;
458 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
459 shift++;
460 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
461 shift++;
Dave Chinner055388a2011-01-04 11:35:03 +1100462 }
Brian Foster76a42022013-03-18 10:51:47 -0400463
464 /*
Brian Fosterf0740512014-07-24 19:56:08 +1000465 * Check each quota to cap the prealloc size, provide a shift value to
466 * throttle with and adjust amount of available space.
Brian Foster76a42022013-03-18 10:51:47 -0400467 */
468 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000469 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
470 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400471 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000472 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
473 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400474 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000475 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
476 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400477
478 /*
479 * The final prealloc size is set to the minimum of free space available
480 * in each of the quotas and the overall filesystem.
481 *
482 * The shift throttle value is set to the maximum value as determined by
483 * the global low free space values and per-quota low free space values.
484 */
Dave Chinner9bb54cb2018-06-07 07:54:02 -0700485 alloc_blocks = min(alloc_blocks, qblocks);
486 shift = max(shift, qshift);
Brian Foster76a42022013-03-18 10:51:47 -0400487
Brian Foster3c58b5f2013-03-18 10:51:43 -0400488 if (shift)
489 alloc_blocks >>= shift;
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400490 /*
491 * rounddown_pow_of_two() returns an undefined result if we pass in
492 * alloc_blocks = 0.
493 */
494 if (alloc_blocks)
495 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
496 if (alloc_blocks > MAXEXTLEN)
497 alloc_blocks = MAXEXTLEN;
Dave Chinner055388a2011-01-04 11:35:03 +1100498
Brian Foster3c58b5f2013-03-18 10:51:43 -0400499 /*
500 * If we are still trying to allocate more space than is
501 * available, squash the prealloc hard. This can happen if we
502 * have a large file on a small filesystem and the above
503 * lowspace thresholds are smaller than MAXEXTLEN.
504 */
505 while (alloc_blocks && alloc_blocks >= freesp)
506 alloc_blocks >>= 4;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400507check_writeio:
Dave Chinner055388a2011-01-04 11:35:03 +1100508 if (alloc_blocks < mp->m_writeio_blocks)
509 alloc_blocks = mp->m_writeio_blocks;
Brian Foster19cb7e32013-03-18 10:51:48 -0400510 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
511 mp->m_writeio_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100512 return alloc_blocks;
513}
514
Christoph Hellwig51446f52016-09-19 11:10:21 +1000515static int
516xfs_file_iomap_begin_delay(
517 struct inode *inode,
518 loff_t offset,
519 loff_t count,
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100520 unsigned flags,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000521 struct iomap *iomap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000523 struct xfs_inode *ip = XFS_I(inode);
524 struct xfs_mount *mp = ip->i_mount;
525 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
526 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
527 xfs_fileoff_t maxbytes_fsb =
528 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Brian Fosterf7820882016-11-28 14:57:42 +1100529 xfs_fileoff_t end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000530 int error = 0, eof = 0;
531 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700532 struct xfs_iext_cursor icur;
Brian Fosterf7820882016-11-28 14:57:42 +1100533 xfs_fsblock_t prealloc_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Christoph Hellwig51446f52016-09-19 11:10:21 +1000535 ASSERT(!XFS_IS_REALTIME_INODE(ip));
536 ASSERT(!xfs_get_extsz_hint(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Christoph Hellwig51446f52016-09-19 11:10:21 +1000538 xfs_ilock(ip, XFS_ILOCK_EXCL);
539
540 if (unlikely(XFS_TEST_ERROR(
541 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
542 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -0700543 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig51446f52016-09-19 11:10:21 +1000544 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
545 error = -EFSCORRUPTED;
546 goto out_unlock;
547 }
548
549 XFS_STATS_INC(mp, xs_blk_mapw);
550
551 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
552 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
553 if (error)
554 goto out_unlock;
555 }
556
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100557 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
558
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700559 eof = !xfs_iext_lookup_extent(ip, ifp, offset_fsb, &icur, &got);
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100560 if (eof)
561 got.br_startoff = end_fsb; /* fake hole until the end */
562
563 if (got.br_startoff <= offset_fsb) {
564 /*
565 * For reflink files we may need a delalloc reservation when
566 * overwriting shared extents. This includes zeroing of
567 * existing extents that contain data.
568 */
569 if (xfs_is_reflink_inode(ip) &&
570 ((flags & IOMAP_WRITE) ||
571 got.br_state != XFS_EXT_UNWRITTEN)) {
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100572 xfs_trim_extent(&got, offset_fsb, end_fsb - offset_fsb);
Christoph Hellwigfc439462018-10-18 17:19:37 +1100573 error = xfs_reflink_reserve_cow(ip, &got);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100574 if (error)
575 goto out_unlock;
576 }
577
Christoph Hellwig51446f52016-09-19 11:10:21 +1000578 trace_xfs_iomap_found(ip, offset, count, 0, &got);
579 goto done;
580 }
581
Christoph Hellwig0365c5d2018-10-18 17:19:26 +1100582 if (flags & IOMAP_ZERO) {
583 xfs_hole_to_iomap(ip, iomap, offset_fsb, got.br_startoff);
584 goto out_unlock;
585 }
586
Darrick J. Wong4882c192018-05-04 15:30:22 -0700587 error = xfs_qm_dqattach_locked(ip, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (error)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000589 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Christoph Hellwig51446f52016-09-19 11:10:21 +1000591 /*
592 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES pages
593 * to keep the chunks of work done where somewhat symmetric with the
594 * work writeback does. This is a completely arbitrary number pulled
595 * out of thin air as a best guess for initial testing.
596 *
597 * Note that the values needs to be less than 32-bits wide until
598 * the lower level functions are updated.
599 */
600 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
Brian Fosterf7820882016-11-28 14:57:42 +1100601 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100602
Christoph Hellwig51446f52016-09-19 11:10:21 +1000603 if (eof) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700604 prealloc_blocks = xfs_iomap_prealloc_size(ip, offset, count,
605 &icur);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000606 if (prealloc_blocks) {
607 xfs_extlen_t align;
608 xfs_off_t end_offset;
Brian Fosterf7820882016-11-28 14:57:42 +1100609 xfs_fileoff_t p_end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000610
611 end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
Brian Fosterf7820882016-11-28 14:57:42 +1100612 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
613 prealloc_blocks;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000614
615 align = xfs_eof_alignment(ip, 0);
616 if (align)
Brian Fosterf7820882016-11-28 14:57:42 +1100617 p_end_fsb = roundup_64(p_end_fsb, align);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000618
Brian Fosterf7820882016-11-28 14:57:42 +1100619 p_end_fsb = min(p_end_fsb, maxbytes_fsb);
620 ASSERT(p_end_fsb > offset_fsb);
621 prealloc_blocks = p_end_fsb - end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000622 }
623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Dave Chinner8de2bf92009-04-06 18:49:12 +0200625retry:
Darrick J. Wongbe51f812016-10-03 09:11:32 -0700626 error = xfs_bmapi_reserve_delalloc(ip, XFS_DATA_FORK, offset_fsb,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700627 end_fsb - offset_fsb, prealloc_blocks, &got, &icur,
628 eof);
Dave Chinner055388a2011-01-04 11:35:03 +1100629 switch (error) {
630 case 0:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000631 break;
Dave Chinner24513372014-06-25 14:58:08 +1000632 case -ENOSPC:
633 case -EDQUOT:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000634 /* retry without any preallocation */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000635 trace_xfs_delalloc_enospc(ip, offset, count);
Brian Fosterf7820882016-11-28 14:57:42 +1100636 if (prealloc_blocks) {
637 prealloc_blocks = 0;
Dave Chinner9aa05002012-10-08 21:56:04 +1100638 goto retry;
Dave Chinner055388a2011-01-04 11:35:03 +1100639 }
Christoph Hellwig51446f52016-09-19 11:10:21 +1000640 /*FALLTHRU*/
641 default:
642 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
Brian Fosterf65e6fa2017-03-08 09:58:08 -0800645 /*
646 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
647 * them out if the write happens to fail.
648 */
Christoph Hellwigc03cea42018-06-19 15:10:58 -0700649 iomap->flags |= IOMAP_F_NEW;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000650 trace_xfs_iomap_alloc(ip, offset, count, 0, &got);
651done:
652 if (isnullstartblock(got.br_startblock))
653 got.br_startblock = DELAYSTARTBLOCK;
654
655 if (!got.br_startblock) {
656 error = xfs_alert_fsblock_zero(ip, &got);
657 if (error)
658 goto out_unlock;
659 }
660
661 xfs_bmbt_to_iomap(ip, iomap, &got);
662
663out_unlock:
664 xfs_iunlock(ip, XFS_ILOCK_EXCL);
665 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668/*
669 * Pass in a delayed allocate extent, convert it to real extents;
670 * return to the caller the extent we create which maps on top of
671 * the originating callers request.
672 *
673 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100674 *
675 * We no longer bother to look at the incoming map - all we have to
676 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
Christoph Hellwiga206c812010-12-10 08:42:20 +0000678int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679xfs_iomap_write_allocate(
680 xfs_inode_t *ip,
Darrick J. Wong60b49842016-10-03 09:11:34 -0700681 int whichfork,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700682 xfs_off_t offset,
Christoph Hellwige666aa32018-07-17 16:51:52 -0700683 xfs_bmbt_irec_t *imap,
684 unsigned int *cow_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 xfs_mount_t *mp = ip->i_mount;
Christoph Hellwig2ba090d2018-08-07 10:57:12 -0700687 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 xfs_fileoff_t offset_fsb, last_block;
689 xfs_fileoff_t end_fsb, map_start_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 xfs_filblks_t count_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 xfs_trans_t *tp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100692 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int error = 0;
Christoph Hellwigd2b39642017-01-20 09:31:54 -0800694 int flags = XFS_BMAPI_DELALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 int nres;
696
Darrick J. Wong60b49842016-10-03 09:11:34 -0700697 if (whichfork == XFS_COW_FORK)
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800698 flags |= XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /*
701 * Make sure that the dquots are there.
702 */
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700703 error = xfs_qm_dqattach(ip);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200704 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000705 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Nathan Scott24e17b52005-05-05 13:33:20 -0700707 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig30704512010-06-24 11:42:19 +1000708 count_fsb = imap->br_blockcount;
709 map_start_fsb = imap->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100711 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 while (count_fsb != 0) {
714 /*
715 * Set up a transaction with which to allocate the
716 * backing store for the file. Do allocations in a
717 * loop until we get some space in the range we are
718 * interested in. The other space that might be allocated
719 * is in the delayed allocation extent on which we sit
720 * but before our buffer starts.
721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 nimaps = 0;
723 while (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
Christoph Hellwig0af32fb2016-08-17 08:30:28 +1000725 /*
726 * We have already reserved space for the extent and any
727 * indirect blocks when creating the delalloc extent,
728 * there is no need to reserve space in this transaction
729 * again.
730 */
731 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0,
Christoph Hellwig253f4912016-04-06 09:19:55 +1000732 0, XFS_TRANS_RESERVE, &tp);
733 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000734 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000737 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /*
David Chinnere4143a12007-11-23 16:29:11 +1100740 * it is possible that the extents have changed since
741 * we did the read call as we dropped the ilock for a
742 * while. We have to be careful about truncates or hole
743 * punchs here - we are not allowed to allocate
744 * non-delalloc blocks here.
745 *
746 * The only protection against truncation is the pages
747 * for the range we are being asked to convert are
748 * locked and hence a truncate will block on them
749 * first.
750 *
751 * As a result, if we go beyond the range we really
752 * need and hit an delalloc extent boundary followed by
753 * a hole while we have excess blocks in the map, we
754 * will fill the hole incorrectly and overrun the
755 * transaction reservation.
756 *
757 * Using a single map prevents this as we are forced to
758 * check each map we look for overlap with the desired
759 * range and abort as soon as we find it. Also, given
760 * that we only return a single map, having one beyond
761 * what we can return is probably a bit silly.
762 *
763 * We also need to check that we don't go beyond EOF;
764 * this is a truncate optimisation as a truncate sets
765 * the new file size before block on the pages we
766 * currently have locked under writeback. Because they
767 * are about to be tossed, we don't need to write them
768 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
David Chinnere4143a12007-11-23 16:29:11 +1100770 nimaps = 1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000771 end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000772 error = xfs_bmap_last_offset(ip, &last_block,
David Chinner7c9ef852008-04-10 12:21:59 +1000773 XFS_DATA_FORK);
774 if (error)
775 goto trans_cancel;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
778 if ((map_start_fsb + count_fsb) > last_block) {
779 count_fsb = last_block - map_start_fsb;
780 if (count_fsb == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000781 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 goto trans_cancel;
783 }
784 }
785
Christoph Hellwig30704512010-06-24 11:42:19 +1000786 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000787 * From this point onwards we overwrite the imap
788 * pointer that the caller gave to us.
789 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000790 error = xfs_bmapi_write(tp, ip, map_start_fsb,
Brian Fostera7beabe2018-07-11 22:26:25 -0700791 count_fsb, flags, nres, imap,
Brian Foster650919f2018-07-11 22:26:23 -0700792 &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (error)
794 goto trans_cancel;
795
Christoph Hellwig70393312015-06-04 13:48:08 +1000796 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (error)
798 goto error0;
799
Christoph Hellwige666aa32018-07-17 16:51:52 -0700800 if (whichfork == XFS_COW_FORK)
Christoph Hellwig2ba090d2018-08-07 10:57:12 -0700801 *cow_seq = READ_ONCE(ifp->if_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 xfs_iunlock(ip, XFS_ILOCK_EXCL);
803 }
804
805 /*
806 * See if we were able to allocate an extent that
807 * covers at least part of the callers request
808 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000809 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100810 return xfs_alert_fsblock_zero(ip, imap);
David Chinner86c4d622008-04-29 12:53:21 +1000811
Christoph Hellwig30704512010-06-24 11:42:19 +1000812 if ((offset_fsb >= imap->br_startoff) &&
813 (offset_fsb < (imap->br_startoff +
814 imap->br_blockcount))) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100815 XFS_STATS_INC(mp, xs_xstrat_quick);
David Chinnere4143a12007-11-23 16:29:11 +1100816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
David Chinnere4143a12007-11-23 16:29:11 +1100819 /*
820 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * file, just surrounding data, try again.
822 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000823 count_fsb -= imap->br_blockcount;
824 map_start_fsb = imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
827trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000828 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829error0:
830 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000831 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834int
835xfs_iomap_write_unwritten(
836 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700837 xfs_off_t offset,
Eryu Guanee70daa2017-09-21 11:26:18 -0700838 xfs_off_t count,
839 bool update_isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 xfs_fileoff_t offset_fsb;
843 xfs_filblks_t count_fsb;
844 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100845 int nimaps;
846 xfs_trans_t *tp;
847 xfs_bmbt_irec_t imap;
Eryu Guanee70daa2017-09-21 11:26:18 -0700848 struct inode *inode = VFS_I(ip);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000849 xfs_fsize_t i_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100850 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000853 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 offset_fsb = XFS_B_TO_FSBT(mp, offset);
856 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
857 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
858
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000859 /*
860 * Reserve enough blocks in this transaction for two complete extent
861 * btree splits. We may be converting the middle part of an unwritten
862 * extent and in this case we will insert two new extents in the btree
863 * each of which could cause a full split.
864 *
865 * This reservation amount will be used in the first call to
866 * xfs_bmbt_split() to select an AG with enough space to satisfy the
867 * rest of the operation.
868 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100869 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Nathan Scottdd9f4382006-01-11 15:28:28 +1100871 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /*
Christoph Hellwig253f4912016-04-06 09:19:55 +1000873 * Set up a transaction to convert the range of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * from unwritten to real. Do allocations in a loop until
875 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000876 *
Christoph Hellwig253f4912016-04-06 09:19:55 +1000877 * Note that we can't risk to recursing back into the filesystem
878 * here as we might be asked to write out the same inode that we
879 * complete here and might deadlock on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000881 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
882 XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
883 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000884 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000887 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /*
890 * Modify the unwritten extent state of the buffer.
891 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 nimaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000893 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Brian Fostera7beabe2018-07-11 22:26:25 -0700894 XFS_BMAPI_CONVERT, resblks, &imap,
895 &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (error)
897 goto error_on_bmapi_transaction;
898
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000899 /*
900 * Log the updated inode size as we go. We have to be careful
901 * to only log it up to the actual write offset if it is
902 * halfway into a block.
903 */
904 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
905 if (i_size > offset + count)
906 i_size = offset + count;
Eryu Guanee70daa2017-09-21 11:26:18 -0700907 if (update_isize && i_size > i_size_read(inode))
908 i_size_write(inode, i_size);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000909 i_size = xfs_new_eof(ip, i_size);
910 if (i_size) {
911 ip->i_d.di_size = i_size;
912 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
913 }
914
Christoph Hellwig70393312015-06-04 13:48:08 +1000915 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 xfs_iunlock(ip, XFS_ILOCK_EXCL);
917 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000918 return error;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100919
David Chinner86c4d622008-04-29 12:53:21 +1000920 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100921 return xfs_alert_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if ((numblks_fsb = imap.br_blockcount) == 0) {
924 /*
925 * The numblks_fsb value should always get
926 * smaller, otherwise the loop is stuck.
927 */
928 ASSERT(imap.br_blockcount);
929 break;
930 }
931 offset_fsb += numblks_fsb;
932 count_fsb -= numblks_fsb;
933 } while (count_fsb > 0);
934
935 return 0;
936
937error_on_bmapi_transaction:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000938 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000940 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
Christoph Hellwig3b3dce02016-06-21 09:52:47 +1000942
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700943static inline bool
944imap_needs_alloc(
945 struct inode *inode,
946 struct xfs_bmbt_irec *imap,
947 int nimaps)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000948{
949 return !nimaps ||
950 imap->br_startblock == HOLESTARTBLOCK ||
Christoph Hellwig6c31f4952016-09-19 11:28:38 +1000951 imap->br_startblock == DELAYSTARTBLOCK ||
Christoph Hellwig63fbb4c2017-03-28 14:53:36 -0700952 (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000953}
954
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700955static inline bool
956needs_cow_for_zeroing(
957 struct xfs_bmbt_irec *imap,
958 int nimaps)
Christoph Hellwig172ed392018-03-01 14:10:31 -0800959{
960 return nimaps &&
961 imap->br_startblock != HOLESTARTBLOCK &&
962 imap->br_state != XFS_EXT_UNWRITTEN;
963}
964
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700965static int
966xfs_ilock_for_iomap(
967 struct xfs_inode *ip,
968 unsigned flags,
969 unsigned *lockmode)
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100970{
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700971 unsigned mode = XFS_ILOCK_SHARED;
Darrick J. Wong5bd88d12018-06-21 23:26:57 -0700972 bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700973
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100974 /*
Christoph Hellwigaf5b5af2018-03-01 14:12:12 -0800975 * COW writes may allocate delalloc space or convert unwritten COW
976 * extents, so we need to make sure to take the lock exclusively here.
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100977 */
Darrick J. Wong5bd88d12018-06-21 23:26:57 -0700978 if (xfs_is_reflink_inode(ip) && is_write) {
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700979 /*
980 * FIXME: It could still overwrite on unshared extents and not
981 * need allocation.
982 */
983 if (flags & IOMAP_NOWAIT)
984 return -EAGAIN;
985 mode = XFS_ILOCK_EXCL;
986 }
Christoph Hellwigff3d8b92018-03-01 14:12:45 -0800987
988 /*
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700989 * Extents not yet cached requires exclusive access, don't block. This
990 * is an opencoded xfs_ilock_data_map_shared() call but with
Christoph Hellwigff3d8b92018-03-01 14:12:45 -0800991 * non-blocking behaviour.
992 */
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700993 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
994 if (flags & IOMAP_NOWAIT)
995 return -EAGAIN;
996 mode = XFS_ILOCK_EXCL;
997 }
998
Darrick J. Wong5bd88d12018-06-21 23:26:57 -0700999relock:
Dave Chinnerdfa03a52018-05-02 12:54:54 -07001000 if (flags & IOMAP_NOWAIT) {
1001 if (!xfs_ilock_nowait(ip, mode))
1002 return -EAGAIN;
1003 } else {
1004 xfs_ilock(ip, mode);
1005 }
1006
Darrick J. Wong5bd88d12018-06-21 23:26:57 -07001007 /*
1008 * The reflink iflag could have changed since the earlier unlocked
1009 * check, so if we got ILOCK_SHARED for a write and but we're now a
1010 * reflink inode we have to switch to ILOCK_EXCL and relock.
1011 */
1012 if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_reflink_inode(ip)) {
1013 xfs_iunlock(ip, mode);
1014 mode = XFS_ILOCK_EXCL;
1015 goto relock;
1016 }
1017
Dave Chinnerdfa03a52018-05-02 12:54:54 -07001018 *lockmode = mode;
1019 return 0;
Christoph Hellwigacdda3a2016-11-30 14:37:15 +11001020}
1021
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001022static int
1023xfs_file_iomap_begin(
1024 struct inode *inode,
1025 loff_t offset,
1026 loff_t length,
1027 unsigned flags,
1028 struct iomap *iomap)
1029{
1030 struct xfs_inode *ip = XFS_I(inode);
1031 struct xfs_mount *mp = ip->i_mount;
1032 struct xfs_bmbt_irec imap;
1033 xfs_fileoff_t offset_fsb, end_fsb;
1034 int nimaps = 1, error = 0;
Christoph Hellwigd392bc82018-10-18 17:19:48 +11001035 bool shared = false;
Christoph Hellwig66642c52016-09-19 11:26:39 +10001036 unsigned lockmode;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001037
1038 if (XFS_FORCED_SHUTDOWN(mp))
1039 return -EIO;
1040
Christoph Hellwig0365c5d2018-10-18 17:19:26 +11001041 if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && !(flags & IOMAP_DIRECT) &&
Christoph Hellwigacdda3a2016-11-30 14:37:15 +11001042 !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
Darrick J. Wong2a067052016-10-03 09:11:33 -07001043 /* Reserve delalloc blocks for regular writeback. */
Christoph Hellwig0365c5d2018-10-18 17:19:26 +11001044 return xfs_file_iomap_begin_delay(inode, offset, length, flags,
1045 iomap);
Christoph Hellwig51446f52016-09-19 11:10:21 +10001046 }
1047
Dave Chinnerdfa03a52018-05-02 12:54:54 -07001048 /*
1049 * Lock the inode in the manner required for the specified operation and
1050 * check for as many conditions that would result in blocking as
1051 * possible. This removes most of the non-blocking checks from the
1052 * mapping code below.
1053 */
1054 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1055 if (error)
1056 return error;
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -05001057
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001058 ASSERT(offset <= mp->m_super->s_maxbytes);
Darrick J. Wongb4d8ad7f2017-12-22 13:14:34 -08001059 if (offset > mp->m_super->s_maxbytes - length)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001060 length = mp->m_super->s_maxbytes - offset;
1061 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1062 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1063
1064 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001065 &nimaps, 0);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001066 if (error)
1067 goto out_unlock;
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001068
Christoph Hellwig3c68d442017-02-06 10:51:03 -08001069 if (flags & IOMAP_REPORT) {
Christoph Hellwig5f9268c2016-10-20 15:53:32 +11001070 /* Trim the mapping to the nearest shared extent boundary. */
Christoph Hellwigd392bc82018-10-18 17:19:48 +11001071 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001072 if (error)
1073 goto out_unlock;
1074 }
1075
Dave Chinnerd0641782018-05-02 12:54:53 -07001076 /* Non-modifying mapping requested, so we are done */
1077 if (!(flags & (IOMAP_WRITE | IOMAP_ZERO)))
1078 goto out_found;
1079
Dave Chinnerdfa03a52018-05-02 12:54:54 -07001080 /*
1081 * Break shared extents if necessary. Checks for non-blocking IO have
1082 * been done up front, so we don't need to do them here.
1083 */
1084 if (xfs_is_reflink_inode(ip)) {
1085 /* if zeroing doesn't need COW allocation, then we are done. */
1086 if ((flags & IOMAP_ZERO) &&
1087 !needs_cow_for_zeroing(&imap, nimaps))
1088 goto out_found;
1089
Christoph Hellwig3c68d442017-02-06 10:51:03 -08001090 if (flags & IOMAP_DIRECT) {
1091 /* may drop and re-acquire the ilock */
1092 error = xfs_reflink_allocate_cow(ip, &imap, &shared,
1093 &lockmode);
1094 if (error)
1095 goto out_unlock;
1096 } else {
Christoph Hellwigfc439462018-10-18 17:19:37 +11001097 error = xfs_reflink_reserve_cow(ip, &imap);
Christoph Hellwig3c68d442017-02-06 10:51:03 -08001098 if (error)
1099 goto out_unlock;
1100 }
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001101
1102 end_fsb = imap.br_startoff + imap.br_blockcount;
1103 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001104 }
1105
Dave Chinnerd0641782018-05-02 12:54:53 -07001106 /* Don't need to allocate over holes when doing zeroing operations. */
1107 if (flags & IOMAP_ZERO)
1108 goto out_found;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001109
Dave Chinnerd0641782018-05-02 12:54:53 -07001110 if (!imap_needs_alloc(inode, &imap, nimaps))
1111 goto out_found;
Christoph Hellwigb95a2122016-08-17 08:44:52 +10001112
Dave Chinnerd0641782018-05-02 12:54:53 -07001113 /* If nowait is set bail since we are going to make allocations. */
1114 if (flags & IOMAP_NOWAIT) {
1115 error = -EAGAIN;
1116 goto out_unlock;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001117 }
1118
Dave Chinnerd0641782018-05-02 12:54:53 -07001119 /*
1120 * We cap the maximum length we map to a sane size to keep the chunks
1121 * of work done where somewhat symmetric with the work writeback does.
1122 * This is a completely arbitrary number pulled out of thin air as a
1123 * best guess for initial testing.
1124 *
1125 * Note that the values needs to be less than 32-bits wide until the
1126 * lower level functions are updated.
1127 */
1128 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
1129
1130 /*
1131 * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
1132 * return.
1133 */
1134 if (lockmode == XFS_ILOCK_EXCL)
1135 xfs_ilock_demote(ip, lockmode);
1136 error = xfs_iomap_write_direct(ip, offset, length, &imap,
1137 nimaps);
1138 if (error)
1139 return error;
1140
Christoph Hellwigc03cea42018-06-19 15:10:58 -07001141 iomap->flags |= IOMAP_F_NEW;
Dave Chinnerd0641782018-05-02 12:54:53 -07001142 trace_xfs_iomap_alloc(ip, offset, length, 0, &imap);
1143
1144out_finish:
Dan Williamsaaa422c2017-11-13 16:38:44 -08001145 if (xfs_ipincount(ip) && (ip->i_itemp->ili_fsync_fields
1146 & ~XFS_ILOG_TIMESTAMP))
Christoph Hellwiga39e5962017-11-01 16:36:47 +01001147 iomap->flags |= IOMAP_F_DIRTY;
1148
Christoph Hellwigb95a2122016-08-17 08:44:52 +10001149 xfs_bmbt_to_iomap(ip, iomap, &imap);
Dan Williamsfa5d9322017-01-27 12:04:59 -08001150
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001151 if (shared)
1152 iomap->flags |= IOMAP_F_SHARED;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001153 return 0;
Dave Chinnerd0641782018-05-02 12:54:53 -07001154
1155out_found:
1156 ASSERT(nimaps);
1157 xfs_iunlock(ip, lockmode);
1158 trace_xfs_iomap_found(ip, offset, length, 0, &imap);
1159 goto out_finish;
1160
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001161out_unlock:
1162 xfs_iunlock(ip, lockmode);
1163 return error;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001164}
1165
1166static int
1167xfs_file_iomap_end_delalloc(
1168 struct xfs_inode *ip,
1169 loff_t offset,
1170 loff_t length,
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001171 ssize_t written,
1172 struct iomap *iomap)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001173{
1174 struct xfs_mount *mp = ip->i_mount;
1175 xfs_fileoff_t start_fsb;
1176 xfs_fileoff_t end_fsb;
1177 int error = 0;
1178
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001179 /*
1180 * Behave as if the write failed if drop writes is enabled. Set the NEW
1181 * flag to force delalloc cleanup.
1182 */
Darrick J. Wongf8c47252017-06-20 17:54:48 -07001183 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001184 iomap->flags |= IOMAP_F_NEW;
Brian Foster9dbddd72017-02-13 22:48:17 -08001185 written = 0;
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001186 }
Brian Foster9dbddd72017-02-13 22:48:17 -08001187
Brian Fosterfa7f1382017-02-16 17:19:12 -08001188 /*
1189 * start_fsb refers to the first unused block after a short write. If
1190 * nothing was written, round offset down to point at the first block in
1191 * the range.
1192 */
1193 if (unlikely(!written))
1194 start_fsb = XFS_B_TO_FSBT(mp, offset);
1195 else
1196 start_fsb = XFS_B_TO_FSB(mp, offset + written);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001197 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1198
1199 /*
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001200 * Trim delalloc blocks if they were allocated by this write and we
1201 * didn't manage to write the whole range.
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001202 *
1203 * We don't need to care about racing delalloc as we hold i_mutex
1204 * across the reserve/allocate/unreserve calls. If there are delalloc
1205 * blocks in the range, they are ours.
1206 */
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001207 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
Brian Fosterfa7f1382017-02-16 17:19:12 -08001208 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1209 XFS_FSB_TO_B(mp, end_fsb) - 1);
1210
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001211 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1212 end_fsb - start_fsb);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001213 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1214 xfs_alert(mp, "%s: unable to clean up ino %lld",
1215 __func__, ip->i_ino);
1216 return error;
1217 }
1218 }
1219
1220 return 0;
1221}
1222
1223static int
1224xfs_file_iomap_end(
1225 struct inode *inode,
1226 loff_t offset,
1227 loff_t length,
1228 ssize_t written,
1229 unsigned flags,
1230 struct iomap *iomap)
1231{
1232 if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
1233 return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001234 length, written, iomap);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001235 return 0;
1236}
1237
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001238const struct iomap_ops xfs_iomap_ops = {
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001239 .iomap_begin = xfs_file_iomap_begin,
1240 .iomap_end = xfs_file_iomap_end,
1241};
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001242
1243static int
1244xfs_xattr_iomap_begin(
1245 struct inode *inode,
1246 loff_t offset,
1247 loff_t length,
1248 unsigned flags,
1249 struct iomap *iomap)
1250{
1251 struct xfs_inode *ip = XFS_I(inode);
1252 struct xfs_mount *mp = ip->i_mount;
1253 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1254 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1255 struct xfs_bmbt_irec imap;
1256 int nimaps = 1, error = 0;
1257 unsigned lockmode;
1258
1259 if (XFS_FORCED_SHUTDOWN(mp))
1260 return -EIO;
1261
Darrick J. Wong84358536d2017-04-06 16:00:39 -07001262 lockmode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001263
1264 /* if there are no attribute fork or extents, return ENOENT */
Darrick J. Wong84358536d2017-04-06 16:00:39 -07001265 if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001266 error = -ENOENT;
1267 goto out_unlock;
1268 }
1269
1270 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1271 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongb7e0b6f2017-12-06 16:13:35 -08001272 &nimaps, XFS_BMAPI_ATTRFORK);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001273out_unlock:
1274 xfs_iunlock(ip, lockmode);
1275
1276 if (!error) {
1277 ASSERT(nimaps);
1278 xfs_bmbt_to_iomap(ip, iomap, &imap);
1279 }
1280
1281 return error;
1282}
1283
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001284const struct iomap_ops xfs_xattr_iomap_ops = {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001285 .iomap_begin = xfs_xattr_iomap_begin,
1286};