blob: c6ce6f9335b65e66caf1ee303390253d36a5a8f1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Christoph Hellwig51446f52016-09-19 11:10:21 +10003 * Copyright (c) 2016 Christoph Hellwig.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Christoph Hellwig3b3dce02016-06-21 09:52:47 +100019#include <linux/iomap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110022#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_btree.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100032#include "xfs_bmap_util.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070033#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_error.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110035#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_trans_space.h"
Christoph Hellwiga39e5962017-11-01 16:36:47 +010037#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_iomap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000039#include "xfs_trace.h"
Brian Foster27b52862012-11-06 09:50:38 -050040#include "xfs_icache.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110041#include "xfs_quota.h"
Brian Foster76a42022013-03-18 10:51:47 -040042#include "xfs_dquot_item.h"
43#include "xfs_dquot.h"
Darrick J. Wong2a067052016-10-03 09:11:33 -070044#include "xfs_reflink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
48 << mp->m_writeio_log)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Christoph Hellwige9c49732016-09-19 11:09:12 +100050void
51xfs_bmbt_to_iomap(
52 struct xfs_inode *ip,
53 struct iomap *iomap,
54 struct xfs_bmbt_irec *imap)
55{
56 struct xfs_mount *mp = ip->i_mount;
57
58 if (imap->br_startblock == HOLESTARTBLOCK) {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040059 iomap->addr = IOMAP_NULL_ADDR;
Christoph Hellwige9c49732016-09-19 11:09:12 +100060 iomap->type = IOMAP_HOLE;
61 } else if (imap->br_startblock == DELAYSTARTBLOCK) {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040062 iomap->addr = IOMAP_NULL_ADDR;
Christoph Hellwige9c49732016-09-19 11:09:12 +100063 iomap->type = IOMAP_DELALLOC;
64 } else {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040065 iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
Christoph Hellwige9c49732016-09-19 11:09:12 +100066 if (imap->br_state == XFS_EXT_UNWRITTEN)
67 iomap->type = IOMAP_UNWRITTEN;
68 else
69 iomap->type = IOMAP_MAPPED;
70 }
71 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
72 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
73 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
Dan Williams486aff52017-08-24 15:12:50 -070074 iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
Christoph Hellwige9c49732016-09-19 11:09:12 +100075}
76
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070077xfs_extlen_t
Christoph Hellwigf8e3a822016-09-19 11:09:28 +100078xfs_eof_alignment(
79 struct xfs_inode *ip,
80 xfs_extlen_t extsize)
Nathan Scottdd9f4382006-01-11 15:28:28 +110081{
Christoph Hellwigf8e3a822016-09-19 11:09:28 +100082 struct xfs_mount *mp = ip->i_mount;
83 xfs_extlen_t align = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +110084
Christoph Hellwigbf322d92011-12-18 20:00:05 +000085 if (!XFS_IS_REALTIME_INODE(ip)) {
86 /*
87 * Round up the allocation request to a stripe unit
88 * (m_dalign) boundary if the file size is >= stripe unit
89 * size, and we are allocating past the allocation eof.
90 *
91 * If mounted with the "-o swalloc" option the alignment is
92 * increased from the strip unit size to the stripe width.
93 */
94 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
95 align = mp->m_swidth;
96 else if (mp->m_dalign)
97 align = mp->m_dalign;
98
Peter Watkins76b57302014-12-04 09:30:51 +110099 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
100 align = 0;
Christoph Hellwigbf322d92011-12-18 20:00:05 +0000101 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100102
103 /*
104 * Always round up the allocation request to an extent boundary
105 * (when file on a real-time subvolume or has di_extsize hint).
106 */
107 if (extsize) {
Peter Watkins76b57302014-12-04 09:30:51 +1100108 if (align)
109 align = roundup_64(align, extsize);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100110 else
111 align = extsize;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100112 }
113
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000114 return align;
115}
116
117STATIC int
118xfs_iomap_eof_align_last_fsb(
119 struct xfs_inode *ip,
120 xfs_extlen_t extsize,
121 xfs_fileoff_t *last_fsb)
122{
123 xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
124
Peter Watkins76b57302014-12-04 09:30:51 +1100125 if (align) {
126 xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000127 int eof, error;
128
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000129 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100130 if (error)
131 return error;
132 if (eof)
133 *last_fsb = new_last_fsb;
134 }
135 return 0;
136}
137
138STATIC int
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100139xfs_alert_fsblock_zero(
Nathan Scott572d95f2006-09-28 11:03:20 +1000140 xfs_inode_t *ip,
141 xfs_bmbt_irec_t *imap)
142{
Dave Chinner6a19d932011-03-07 10:02:35 +1100143 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +1000144 "Access to block zero in inode %llu "
145 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -0500146 "blkcnt: %llx extent-state: %x",
Nathan Scott572d95f2006-09-28 11:03:20 +1000147 (unsigned long long)ip->i_ino,
148 (unsigned long long)imap->br_startblock,
149 (unsigned long long)imap->br_startoff,
150 (unsigned long long)imap->br_blockcount,
151 imap->br_state);
Dave Chinner24513372014-06-25 14:58:08 +1000152 return -EFSCORRUPTED;
Nathan Scott572d95f2006-09-28 11:03:20 +1000153}
154
Christoph Hellwiga206c812010-12-10 08:42:20 +0000155int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156xfs_iomap_write_direct(
157 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700158 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 size_t count,
Christoph Hellwig30704512010-06-24 11:42:19 +1000160 xfs_bmbt_irec_t *imap,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000161 int nmaps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 xfs_fileoff_t offset_fsb;
165 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100166 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 xfs_fsblock_t firstfsb;
Christoph Hellwigf13eb202017-02-06 10:42:26 -0800168 xfs_extlen_t extsz;
Eric Sandeen0116d932005-11-02 15:00:01 +1100169 int nimaps;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000170 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 int rt;
172 xfs_trans_t *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000173 struct xfs_defer_ops dfops;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100174 uint qblocks, resblks, resrtextents;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100175 int error;
Brian Foster009c6e82015-10-12 15:34:20 +1100176 int lockmode;
Dave Chinner1ca19152015-11-03 12:37:00 +1100177 int bmapi_flags = XFS_BMAPI_PREALLOC;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000178 uint tflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Nathan Scottdd9f4382006-01-11 15:28:28 +1100180 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000181 extsz = xfs_get_extsz_hint(ip);
Brian Foster009c6e82015-10-12 15:34:20 +1100182 lockmode = XFS_ILOCK_SHARED; /* locked by caller */
183
184 ASSERT(xfs_isilocked(ip, lockmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David Chinner957d0eb2007-06-18 16:50:37 +1000186 offset_fsb = XFS_B_TO_FSBT(mp, offset);
187 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000188 if ((offset + count) > XFS_ISIZE(ip)) {
Brian Foster009c6e82015-10-12 15:34:20 +1100189 /*
190 * Assert that the in-core extent list is present since this can
191 * call xfs_iread_extents() and we only have the ilock shared.
192 * This should be safe because the lock was held around a bmapi
193 * call in the caller and we only need it to access the in-core
194 * list.
195 */
196 ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
197 XFS_IFEXTENTS);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000198 error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100199 if (error)
Brian Foster009c6e82015-10-12 15:34:20 +1100200 goto out_unlock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100201 } else {
Christoph Hellwig405f8042010-12-10 08:42:19 +0000202 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100203 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
Christoph Hellwig30704512010-06-24 11:42:19 +1000204 imap->br_blockcount +
205 imap->br_startoff);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100206 }
207 count_fsb = last_fsb - offset_fsb;
208 ASSERT(count_fsb > 0);
Christoph Hellwigf13eb202017-02-06 10:42:26 -0800209 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100210
211 if (unlikely(rt)) {
212 resrtextents = qblocks = resaligned;
213 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000214 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
215 quota_flag = XFS_QMOPT_RES_RTBLKS;
216 } else {
217 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100218 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000219 quota_flag = XFS_QMOPT_RES_REGBLKS;
220 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /*
Brian Foster009c6e82015-10-12 15:34:20 +1100223 * Drop the shared lock acquired by the caller, attach the dquot if
224 * necessary and move on to transaction setup.
225 */
226 xfs_iunlock(ip, lockmode);
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700227 error = xfs_qm_dqattach(ip);
Brian Foster009c6e82015-10-12 15:34:20 +1100228 if (error)
229 return error;
230
231 /*
Dave Chinner1ca19152015-11-03 12:37:00 +1100232 * For DAX, we do not allocate unwritten extents, but instead we zero
233 * the block before we commit the transaction. Ideally we'd like to do
234 * this outside the transaction context, but if we commit and then crash
235 * we may not have zeroed the blocks and this will be exposed on
236 * recovery of the allocation. Hence we must zero before commit.
Dave Chinner3b0fe472016-01-04 16:22:45 +1100237 *
Dave Chinner1ca19152015-11-03 12:37:00 +1100238 * Further, if we are mapping unwritten extents here, we need to zero
239 * and convert them to written so that we don't need an unwritten extent
240 * callback for DAX. This also means that we need to be able to dip into
Dave Chinner3b0fe472016-01-04 16:22:45 +1100241 * the reserve block pool for bmbt block allocation if there is no space
242 * left but we need to do unwritten extent conversion.
Dave Chinner1ca19152015-11-03 12:37:00 +1100243 */
244 if (IS_DAX(VFS_I(ip))) {
245 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
Christoph Hellwig63fbb4c2017-03-28 14:53:36 -0700246 if (imap->br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig253f4912016-04-06 09:19:55 +1000247 tflags |= XFS_TRANS_RESERVE;
Dave Chinner3b0fe472016-01-04 16:22:45 +1100248 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
249 }
Dave Chinner1ca19152015-11-03 12:37:00 +1100250 }
Christoph Hellwig253f4912016-04-06 09:19:55 +1000251 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
252 tflags, &tp);
253 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000254 return error;
Dave Chinner507630b2012-03-27 10:34:50 -0400255
Brian Foster009c6e82015-10-12 15:34:20 +1100256 lockmode = XFS_ILOCK_EXCL;
257 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Christoph Hellwig7d095252009-06-08 15:33:32 +0200259 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100260 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400261 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Christoph Hellwigddc34152011-09-19 15:00:54 +0000263 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000266 * From this point onwards we overwrite the imap pointer that the
267 * caller gave to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000269 xfs_defer_init(&dfops, &firstfsb);
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,
Dave Chinner264e89a2015-11-03 13:28:41 +1100272 bmapi_flags, &firstfsb, resblks, imap,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000273 &nimaps, &dfops);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000274 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400275 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000278 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700280 error = xfs_defer_finish(&tp, &dfops);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000281 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400282 goto out_bmap_cancel;
Dave Chinner1ca19152015-11-03 12:37:00 +1100283
Christoph Hellwig70393312015-06-04 13:48:08 +1000284 error = xfs_trans_commit(tp);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000285 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400286 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Nathan Scott06d10dd2005-06-21 15:48:47 +1000288 /*
289 * Copy any maps to caller's array and return any error.
290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (nimaps == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000292 error = -ENOSPC;
Dave Chinner507630b2012-03-27 10:34:50 -0400293 goto out_unlock;
Nathan Scott572d95f2006-09-28 11:03:20 +1000294 }
295
Dave Chinner507630b2012-03-27 10:34:50 -0400296 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100297 error = xfs_alert_fsblock_zero(ip, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Dave Chinner507630b2012-03-27 10:34:50 -0400299out_unlock:
Brian Foster009c6e82015-10-12 15:34:20 +1100300 xfs_iunlock(ip, lockmode);
Dave Chinner507630b2012-03-27 10:34:50 -0400301 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Dave Chinner507630b2012-03-27 10:34:50 -0400303out_bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000304 xfs_defer_cancel(&dfops);
Dave Chinnerea562ed2012-05-08 20:48:53 +1000305 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
Dave Chinner507630b2012-03-27 10:34:50 -0400306out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000307 xfs_trans_cancel(tp);
Dave Chinner507630b2012-03-27 10:34:50 -0400308 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Brian Foster76a42022013-03-18 10:51:47 -0400311STATIC bool
312xfs_quota_need_throttle(
313 struct xfs_inode *ip,
314 int type,
315 xfs_fsblock_t alloc_blocks)
316{
317 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
318
319 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
320 return false;
321
322 /* no hi watermark, no throttle */
323 if (!dq->q_prealloc_hi_wmark)
324 return false;
325
326 /* under the lo watermark, no throttle */
327 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
328 return false;
329
330 return true;
331}
332
333STATIC void
334xfs_quota_calc_throttle(
335 struct xfs_inode *ip,
336 int type,
337 xfs_fsblock_t *qblocks,
Brian Fosterf0740512014-07-24 19:56:08 +1000338 int *qshift,
339 int64_t *qfreesp)
Brian Foster76a42022013-03-18 10:51:47 -0400340{
341 int64_t freesp;
342 int shift = 0;
343 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
344
Eric Sandeen5cca3f62014-10-02 09:27:09 +1000345 /* no dq, or over hi wmark, squash the prealloc completely */
346 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
Brian Foster76a42022013-03-18 10:51:47 -0400347 *qblocks = 0;
Brian Fosterf0740512014-07-24 19:56:08 +1000348 *qfreesp = 0;
Brian Foster76a42022013-03-18 10:51:47 -0400349 return;
350 }
351
352 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
353 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
354 shift = 2;
355 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
356 shift += 2;
357 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
358 shift += 2;
359 }
360
Brian Fosterf0740512014-07-24 19:56:08 +1000361 if (freesp < *qfreesp)
362 *qfreesp = freesp;
363
Brian Foster76a42022013-03-18 10:51:47 -0400364 /* only overwrite the throttle values if we are more aggressive */
365 if ((freesp >> shift) < (*qblocks >> *qshift)) {
366 *qblocks = freesp;
367 *qshift = shift;
368 }
369}
370
Dave Chinnera1e16c22013-02-11 16:05:01 +1100371/*
Christoph Hellwig51446f52016-09-19 11:10:21 +1000372 * If we are doing a write at the end of the file and there are no allocations
373 * past this one, then extend the allocation out to the file system's write
374 * iosize.
375 *
Dave Chinner055388a2011-01-04 11:35:03 +1100376 * If we don't have a user specified preallocation size, dynamically increase
Christoph Hellwig51446f52016-09-19 11:10:21 +1000377 * the preallocation size as the size of the file grows. Cap the maximum size
Dave Chinner055388a2011-01-04 11:35:03 +1100378 * at a single extent or less if the filesystem is near full. The closer the
379 * filesystem is to full, the smaller the maximum prealocation.
Christoph Hellwig51446f52016-09-19 11:10:21 +1000380 *
381 * As an exception we don't do any preallocation at all if the file is smaller
382 * than the minimum preallocation and we are using the default dynamic
383 * preallocation scheme, as it is likely this is the only write to the file that
384 * is going to be done.
385 *
386 * We clean up any extra space left over when the file is closed in
387 * xfs_inactive().
Dave Chinner055388a2011-01-04 11:35:03 +1100388 */
389STATIC xfs_fsblock_t
390xfs_iomap_prealloc_size(
Dave Chinnera1e16c22013-02-11 16:05:01 +1100391 struct xfs_inode *ip,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000392 loff_t offset,
393 loff_t count,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700394 struct xfs_iext_cursor *icur)
Dave Chinner055388a2011-01-04 11:35:03 +1100395{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000396 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig656152e2016-11-24 11:39:44 +1100397 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000398 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig656152e2016-11-24 11:39:44 +1100399 struct xfs_bmbt_irec prev;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400400 int shift = 0;
401 int64_t freesp;
Brian Foster76a42022013-03-18 10:51:47 -0400402 xfs_fsblock_t qblocks;
403 int qshift = 0;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000404 xfs_fsblock_t alloc_blocks = 0;
Dave Chinner055388a2011-01-04 11:35:03 +1100405
Christoph Hellwig51446f52016-09-19 11:10:21 +1000406 if (offset + count <= XFS_ISIZE(ip))
407 return 0;
408
409 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
410 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
411 return 0;
412
413 /*
414 * If an explicit allocsize is set, the file is small, or we
415 * are writing behind a hole, then use the minimum prealloc:
416 */
417 if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
418 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700419 !xfs_iext_peek_prev_extent(ifp, icur, &prev) ||
Christoph Hellwig656152e2016-11-24 11:39:44 +1100420 prev.br_startoff + prev.br_blockcount < offset_fsb)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000421 return mp->m_writeio_blocks;
422
423 /*
424 * Determine the initial size of the preallocation. We are beyond the
425 * current EOF here, but we need to take into account whether this is
426 * a sparse write or an extending write when determining the
427 * preallocation size. Hence we need to look up the extent that ends
428 * at the current write offset and use the result to determine the
429 * preallocation size.
430 *
431 * If the extent is a hole, then preallocation is essentially disabled.
432 * Otherwise we take the size of the preceding data extent as the basis
433 * for the preallocation size. If the size of the extent is greater than
434 * half the maximum extent length, then use the current offset as the
435 * basis. This ensures that for large files the preallocation size
436 * always extends to MAXEXTLEN rather than falling short due to things
437 * like stripe unit/width alignment of real extents.
438 */
Christoph Hellwig656152e2016-11-24 11:39:44 +1100439 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
440 alloc_blocks = prev.br_blockcount << 1;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000441 else
442 alloc_blocks = XFS_B_TO_FSB(mp, offset);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400443 if (!alloc_blocks)
444 goto check_writeio;
Brian Foster76a42022013-03-18 10:51:47 -0400445 qblocks = alloc_blocks;
Dave Chinner055388a2011-01-04 11:35:03 +1100446
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400447 /*
448 * MAXEXTLEN is not a power of two value but we round the prealloc down
449 * to the nearest power of two value after throttling. To prevent the
450 * round down from unconditionally reducing the maximum supported prealloc
451 * size, we round up first, apply appropriate throttling, round down and
452 * cap the value to MAXEXTLEN.
453 */
454 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
455 alloc_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100456
Dave Chinner0d485ad2015-02-23 21:22:03 +1100457 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400458 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
459 shift = 2;
460 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
461 shift++;
462 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
463 shift++;
464 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
465 shift++;
466 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
467 shift++;
Dave Chinner055388a2011-01-04 11:35:03 +1100468 }
Brian Foster76a42022013-03-18 10:51:47 -0400469
470 /*
Brian Fosterf0740512014-07-24 19:56:08 +1000471 * Check each quota to cap the prealloc size, provide a shift value to
472 * throttle with and adjust amount of available space.
Brian Foster76a42022013-03-18 10:51:47 -0400473 */
474 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000475 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
476 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400477 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000478 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
479 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400480 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000481 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
482 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400483
484 /*
485 * The final prealloc size is set to the minimum of free space available
486 * in each of the quotas and the overall filesystem.
487 *
488 * The shift throttle value is set to the maximum value as determined by
489 * the global low free space values and per-quota low free space values.
490 */
491 alloc_blocks = MIN(alloc_blocks, qblocks);
492 shift = MAX(shift, qshift);
493
Brian Foster3c58b5f2013-03-18 10:51:43 -0400494 if (shift)
495 alloc_blocks >>= shift;
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400496 /*
497 * rounddown_pow_of_two() returns an undefined result if we pass in
498 * alloc_blocks = 0.
499 */
500 if (alloc_blocks)
501 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
502 if (alloc_blocks > MAXEXTLEN)
503 alloc_blocks = MAXEXTLEN;
Dave Chinner055388a2011-01-04 11:35:03 +1100504
Brian Foster3c58b5f2013-03-18 10:51:43 -0400505 /*
506 * If we are still trying to allocate more space than is
507 * available, squash the prealloc hard. This can happen if we
508 * have a large file on a small filesystem and the above
509 * lowspace thresholds are smaller than MAXEXTLEN.
510 */
511 while (alloc_blocks && alloc_blocks >= freesp)
512 alloc_blocks >>= 4;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400513check_writeio:
Dave Chinner055388a2011-01-04 11:35:03 +1100514 if (alloc_blocks < mp->m_writeio_blocks)
515 alloc_blocks = mp->m_writeio_blocks;
Brian Foster19cb7e32013-03-18 10:51:48 -0400516 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
517 mp->m_writeio_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100518 return alloc_blocks;
519}
520
Christoph Hellwig51446f52016-09-19 11:10:21 +1000521static int
522xfs_file_iomap_begin_delay(
523 struct inode *inode,
524 loff_t offset,
525 loff_t count,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000526 struct iomap *iomap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000528 struct xfs_inode *ip = XFS_I(inode);
529 struct xfs_mount *mp = ip->i_mount;
530 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
531 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
532 xfs_fileoff_t maxbytes_fsb =
533 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Brian Fosterf7820882016-11-28 14:57:42 +1100534 xfs_fileoff_t end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000535 int error = 0, eof = 0;
536 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700537 struct xfs_iext_cursor icur;
Brian Fosterf7820882016-11-28 14:57:42 +1100538 xfs_fsblock_t prealloc_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Christoph Hellwig51446f52016-09-19 11:10:21 +1000540 ASSERT(!XFS_IS_REALTIME_INODE(ip));
541 ASSERT(!xfs_get_extsz_hint(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Christoph Hellwig51446f52016-09-19 11:10:21 +1000543 xfs_ilock(ip, XFS_ILOCK_EXCL);
544
545 if (unlikely(XFS_TEST_ERROR(
546 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
547 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -0700548 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig51446f52016-09-19 11:10:21 +1000549 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
550 error = -EFSCORRUPTED;
551 goto out_unlock;
552 }
553
554 XFS_STATS_INC(mp, xs_blk_mapw);
555
556 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
557 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
558 if (error)
559 goto out_unlock;
560 }
561
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700562 eof = !xfs_iext_lookup_extent(ip, ifp, offset_fsb, &icur, &got);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000563 if (!eof && got.br_startoff <= offset_fsb) {
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100564 if (xfs_is_reflink_inode(ip)) {
565 bool shared;
566
567 end_fsb = min(XFS_B_TO_FSB(mp, offset + count),
568 maxbytes_fsb);
569 xfs_trim_extent(&got, offset_fsb, end_fsb - offset_fsb);
570 error = xfs_reflink_reserve_cow(ip, &got, &shared);
571 if (error)
572 goto out_unlock;
573 }
574
Christoph Hellwig51446f52016-09-19 11:10:21 +1000575 trace_xfs_iomap_found(ip, offset, count, 0, &got);
576 goto done;
577 }
578
Darrick J. Wong4882c192018-05-04 15:30:22 -0700579 error = xfs_qm_dqattach_locked(ip, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (error)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000581 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Christoph Hellwig51446f52016-09-19 11:10:21 +1000583 /*
584 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES pages
585 * to keep the chunks of work done where somewhat symmetric with the
586 * work writeback does. This is a completely arbitrary number pulled
587 * out of thin air as a best guess for initial testing.
588 *
589 * Note that the values needs to be less than 32-bits wide until
590 * the lower level functions are updated.
591 */
592 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
Brian Fosterf7820882016-11-28 14:57:42 +1100593 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100594
Christoph Hellwig51446f52016-09-19 11:10:21 +1000595 if (eof) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700596 prealloc_blocks = xfs_iomap_prealloc_size(ip, offset, count,
597 &icur);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000598 if (prealloc_blocks) {
599 xfs_extlen_t align;
600 xfs_off_t end_offset;
Brian Fosterf7820882016-11-28 14:57:42 +1100601 xfs_fileoff_t p_end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000602
603 end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
Brian Fosterf7820882016-11-28 14:57:42 +1100604 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
605 prealloc_blocks;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000606
607 align = xfs_eof_alignment(ip, 0);
608 if (align)
Brian Fosterf7820882016-11-28 14:57:42 +1100609 p_end_fsb = roundup_64(p_end_fsb, align);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000610
Brian Fosterf7820882016-11-28 14:57:42 +1100611 p_end_fsb = min(p_end_fsb, maxbytes_fsb);
612 ASSERT(p_end_fsb > offset_fsb);
613 prealloc_blocks = p_end_fsb - end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000614 }
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Dave Chinner8de2bf92009-04-06 18:49:12 +0200617retry:
Darrick J. Wongbe51f812016-10-03 09:11:32 -0700618 error = xfs_bmapi_reserve_delalloc(ip, XFS_DATA_FORK, offset_fsb,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700619 end_fsb - offset_fsb, prealloc_blocks, &got, &icur,
620 eof);
Dave Chinner055388a2011-01-04 11:35:03 +1100621 switch (error) {
622 case 0:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000623 break;
Dave Chinner24513372014-06-25 14:58:08 +1000624 case -ENOSPC:
625 case -EDQUOT:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000626 /* retry without any preallocation */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000627 trace_xfs_delalloc_enospc(ip, offset, count);
Brian Fosterf7820882016-11-28 14:57:42 +1100628 if (prealloc_blocks) {
629 prealloc_blocks = 0;
Dave Chinner9aa05002012-10-08 21:56:04 +1100630 goto retry;
Dave Chinner055388a2011-01-04 11:35:03 +1100631 }
Christoph Hellwig51446f52016-09-19 11:10:21 +1000632 /*FALLTHRU*/
633 default:
634 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
Brian Fosterf65e6fa2017-03-08 09:58:08 -0800637 /*
638 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
639 * them out if the write happens to fail.
640 */
641 iomap->flags = IOMAP_F_NEW;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000642 trace_xfs_iomap_alloc(ip, offset, count, 0, &got);
643done:
644 if (isnullstartblock(got.br_startblock))
645 got.br_startblock = DELAYSTARTBLOCK;
646
647 if (!got.br_startblock) {
648 error = xfs_alert_fsblock_zero(ip, &got);
649 if (error)
650 goto out_unlock;
651 }
652
653 xfs_bmbt_to_iomap(ip, iomap, &got);
654
655out_unlock:
656 xfs_iunlock(ip, XFS_ILOCK_EXCL);
657 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/*
661 * Pass in a delayed allocate extent, convert it to real extents;
662 * return to the caller the extent we create which maps on top of
663 * the originating callers request.
664 *
665 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100666 *
667 * We no longer bother to look at the incoming map - all we have to
668 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
Christoph Hellwiga206c812010-12-10 08:42:20 +0000670int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671xfs_iomap_write_allocate(
672 xfs_inode_t *ip,
Darrick J. Wong60b49842016-10-03 09:11:34 -0700673 int whichfork,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700674 xfs_off_t offset,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000675 xfs_bmbt_irec_t *imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 xfs_fileoff_t offset_fsb, last_block;
679 xfs_fileoff_t end_fsb, map_start_fsb;
680 xfs_fsblock_t first_block;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000681 struct xfs_defer_ops dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 xfs_filblks_t count_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 xfs_trans_t *tp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100684 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int error = 0;
Christoph Hellwigd2b39642017-01-20 09:31:54 -0800686 int flags = XFS_BMAPI_DELALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int nres;
688
Darrick J. Wong60b49842016-10-03 09:11:34 -0700689 if (whichfork == XFS_COW_FORK)
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800690 flags |= XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 * Make sure that the dquots are there.
694 */
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700695 error = xfs_qm_dqattach(ip);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200696 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000697 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Nathan Scott24e17b52005-05-05 13:33:20 -0700699 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig30704512010-06-24 11:42:19 +1000700 count_fsb = imap->br_blockcount;
701 map_start_fsb = imap->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100703 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 while (count_fsb != 0) {
706 /*
707 * Set up a transaction with which to allocate the
708 * backing store for the file. Do allocations in a
709 * loop until we get some space in the range we are
710 * interested in. The other space that might be allocated
711 * is in the delayed allocation extent on which we sit
712 * but before our buffer starts.
713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 nimaps = 0;
715 while (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
Christoph Hellwig0af32fb2016-08-17 08:30:28 +1000717 /*
718 * We have already reserved space for the extent and any
719 * indirect blocks when creating the delalloc extent,
720 * there is no need to reserve space in this transaction
721 * again.
722 */
723 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0,
Christoph Hellwig253f4912016-04-06 09:19:55 +1000724 0, XFS_TRANS_RESERVE, &tp);
725 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000726 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000729 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000731 xfs_defer_init(&dfops, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
David Chinnere4143a12007-11-23 16:29:11 +1100734 * it is possible that the extents have changed since
735 * we did the read call as we dropped the ilock for a
736 * while. We have to be careful about truncates or hole
737 * punchs here - we are not allowed to allocate
738 * non-delalloc blocks here.
739 *
740 * The only protection against truncation is the pages
741 * for the range we are being asked to convert are
742 * locked and hence a truncate will block on them
743 * first.
744 *
745 * As a result, if we go beyond the range we really
746 * need and hit an delalloc extent boundary followed by
747 * a hole while we have excess blocks in the map, we
748 * will fill the hole incorrectly and overrun the
749 * transaction reservation.
750 *
751 * Using a single map prevents this as we are forced to
752 * check each map we look for overlap with the desired
753 * range and abort as soon as we find it. Also, given
754 * that we only return a single map, having one beyond
755 * what we can return is probably a bit silly.
756 *
757 * We also need to check that we don't go beyond EOF;
758 * this is a truncate optimisation as a truncate sets
759 * the new file size before block on the pages we
760 * currently have locked under writeback. Because they
761 * are about to be tossed, we don't need to write them
762 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
David Chinnere4143a12007-11-23 16:29:11 +1100764 nimaps = 1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000765 end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000766 error = xfs_bmap_last_offset(ip, &last_block,
David Chinner7c9ef852008-04-10 12:21:59 +1000767 XFS_DATA_FORK);
768 if (error)
769 goto trans_cancel;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
772 if ((map_start_fsb + count_fsb) > last_block) {
773 count_fsb = last_block - map_start_fsb;
774 if (count_fsb == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000775 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 goto trans_cancel;
777 }
778 }
779
Christoph Hellwig30704512010-06-24 11:42:19 +1000780 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000781 * From this point onwards we overwrite the imap
782 * pointer that the caller gave to us.
783 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000784 error = xfs_bmapi_write(tp, ip, map_start_fsb,
Darrick J. Wong60b49842016-10-03 09:11:34 -0700785 count_fsb, flags, &first_block,
Brian Fosterdbd5c8c2015-10-12 16:04:13 +1100786 nres, imap, &nimaps,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000787 &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (error)
789 goto trans_cancel;
790
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700791 error = xfs_defer_finish(&tp, &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (error)
793 goto trans_cancel;
794
Christoph Hellwig70393312015-06-04 13:48:08 +1000795 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (error)
797 goto error0;
798
799 xfs_iunlock(ip, XFS_ILOCK_EXCL);
800 }
801
802 /*
803 * See if we were able to allocate an extent that
804 * covers at least part of the callers request
805 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000806 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100807 return xfs_alert_fsblock_zero(ip, imap);
David Chinner86c4d622008-04-29 12:53:21 +1000808
Christoph Hellwig30704512010-06-24 11:42:19 +1000809 if ((offset_fsb >= imap->br_startoff) &&
810 (offset_fsb < (imap->br_startoff +
811 imap->br_blockcount))) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100812 XFS_STATS_INC(mp, xs_xstrat_quick);
David Chinnere4143a12007-11-23 16:29:11 +1100813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
David Chinnere4143a12007-11-23 16:29:11 +1100816 /*
817 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 * file, just surrounding data, try again.
819 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000820 count_fsb -= imap->br_blockcount;
821 map_start_fsb = imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
824trans_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000825 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000826 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827error0:
828 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000829 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832int
833xfs_iomap_write_unwritten(
834 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700835 xfs_off_t offset,
Eryu Guanee70daa2017-09-21 11:26:18 -0700836 xfs_off_t count,
837 bool update_isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 xfs_fileoff_t offset_fsb;
841 xfs_filblks_t count_fsb;
842 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100843 xfs_fsblock_t firstfsb;
844 int nimaps;
845 xfs_trans_t *tp;
846 xfs_bmbt_irec_t imap;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000847 struct xfs_defer_ops dfops;
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 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000892 xfs_defer_init(&dfops, &firstfsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 nimaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000894 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Brian Fosterdbd5c8c2015-10-12 16:04:13 +1100895 XFS_BMAPI_CONVERT, &firstfsb, resblks,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000896 &imap, &nimaps, &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (error)
898 goto error_on_bmapi_transaction;
899
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000900 /*
901 * Log the updated inode size as we go. We have to be careful
902 * to only log it up to the actual write offset if it is
903 * halfway into a block.
904 */
905 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
906 if (i_size > offset + count)
907 i_size = offset + count;
Eryu Guanee70daa2017-09-21 11:26:18 -0700908 if (update_isize && i_size > i_size_read(inode))
909 i_size_write(inode, i_size);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000910 i_size = xfs_new_eof(ip, i_size);
911 if (i_size) {
912 ip->i_d.di_size = i_size;
913 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
914 }
915
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700916 error = xfs_defer_finish(&tp, &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (error)
918 goto error_on_bmapi_transaction;
919
Christoph Hellwig70393312015-06-04 13:48:08 +1000920 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 xfs_iunlock(ip, XFS_ILOCK_EXCL);
922 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000923 return error;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100924
David Chinner86c4d622008-04-29 12:53:21 +1000925 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100926 return xfs_alert_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if ((numblks_fsb = imap.br_blockcount) == 0) {
929 /*
930 * The numblks_fsb value should always get
931 * smaller, otherwise the loop is stuck.
932 */
933 ASSERT(imap.br_blockcount);
934 break;
935 }
936 offset_fsb += numblks_fsb;
937 count_fsb -= numblks_fsb;
938 } while (count_fsb > 0);
939
940 return 0;
941
942error_on_bmapi_transaction:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000943 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000944 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000946 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
Christoph Hellwig3b3dce02016-06-21 09:52:47 +1000948
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700949static inline bool
950imap_needs_alloc(
951 struct inode *inode,
952 struct xfs_bmbt_irec *imap,
953 int nimaps)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000954{
955 return !nimaps ||
956 imap->br_startblock == HOLESTARTBLOCK ||
Christoph Hellwig6c31f4952016-09-19 11:28:38 +1000957 imap->br_startblock == DELAYSTARTBLOCK ||
Christoph Hellwig63fbb4c2017-03-28 14:53:36 -0700958 (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000959}
960
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700961static inline bool
962needs_cow_for_zeroing(
963 struct xfs_bmbt_irec *imap,
964 int nimaps)
Christoph Hellwig172ed392018-03-01 14:10:31 -0800965{
966 return nimaps &&
967 imap->br_startblock != HOLESTARTBLOCK &&
968 imap->br_state != XFS_EXT_UNWRITTEN;
969}
970
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700971static int
972xfs_ilock_for_iomap(
973 struct xfs_inode *ip,
974 unsigned flags,
975 unsigned *lockmode)
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100976{
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700977 unsigned mode = XFS_ILOCK_SHARED;
978
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100979 /*
Christoph Hellwigaf5b5af2018-03-01 14:12:12 -0800980 * COW writes may allocate delalloc space or convert unwritten COW
981 * extents, so we need to make sure to take the lock exclusively here.
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100982 */
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700983 if (xfs_is_reflink_inode(ip) && (flags & (IOMAP_WRITE | IOMAP_ZERO))) {
984 /*
985 * FIXME: It could still overwrite on unshared extents and not
986 * need allocation.
987 */
988 if (flags & IOMAP_NOWAIT)
989 return -EAGAIN;
990 mode = XFS_ILOCK_EXCL;
991 }
Christoph Hellwigff3d8b92018-03-01 14:12:45 -0800992
993 /*
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700994 * Extents not yet cached requires exclusive access, don't block. This
995 * is an opencoded xfs_ilock_data_map_shared() call but with
Christoph Hellwigff3d8b92018-03-01 14:12:45 -0800996 * non-blocking behaviour.
997 */
Dave Chinnerdfa03a52018-05-02 12:54:54 -0700998 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
999 if (flags & IOMAP_NOWAIT)
1000 return -EAGAIN;
1001 mode = XFS_ILOCK_EXCL;
1002 }
1003
1004 if (flags & IOMAP_NOWAIT) {
1005 if (!xfs_ilock_nowait(ip, mode))
1006 return -EAGAIN;
1007 } else {
1008 xfs_ilock(ip, mode);
1009 }
1010
1011 *lockmode = mode;
1012 return 0;
Christoph Hellwigacdda3a2016-11-30 14:37:15 +11001013}
1014
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001015static int
1016xfs_file_iomap_begin(
1017 struct inode *inode,
1018 loff_t offset,
1019 loff_t length,
1020 unsigned flags,
1021 struct iomap *iomap)
1022{
1023 struct xfs_inode *ip = XFS_I(inode);
1024 struct xfs_mount *mp = ip->i_mount;
1025 struct xfs_bmbt_irec imap;
1026 xfs_fileoff_t offset_fsb, end_fsb;
1027 int nimaps = 1, error = 0;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001028 bool shared = false, trimmed = false;
Christoph Hellwig66642c52016-09-19 11:26:39 +10001029 unsigned lockmode;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001030
1031 if (XFS_FORCED_SHUTDOWN(mp))
1032 return -EIO;
1033
Christoph Hellwigacdda3a2016-11-30 14:37:15 +11001034 if (((flags & (IOMAP_WRITE | IOMAP_DIRECT)) == IOMAP_WRITE) &&
1035 !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
Darrick J. Wong2a067052016-10-03 09:11:33 -07001036 /* Reserve delalloc blocks for regular writeback. */
Eric Sandeenf91fb952017-08-30 09:24:12 -07001037 return xfs_file_iomap_begin_delay(inode, offset, length, iomap);
Christoph Hellwig51446f52016-09-19 11:10:21 +10001038 }
1039
Dave Chinnerdfa03a52018-05-02 12:54:54 -07001040 /*
1041 * Lock the inode in the manner required for the specified operation and
1042 * check for as many conditions that would result in blocking as
1043 * possible. This removes most of the non-blocking checks from the
1044 * mapping code below.
1045 */
1046 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1047 if (error)
1048 return error;
Goldwyn Rodrigues29a5d292017-06-20 07:05:48 -05001049
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001050 ASSERT(offset <= mp->m_super->s_maxbytes);
Darrick J. Wongb4d8ad7f2017-12-22 13:14:34 -08001051 if (offset > mp->m_super->s_maxbytes - length)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001052 length = mp->m_super->s_maxbytes - offset;
1053 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1054 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1055
1056 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001057 &nimaps, 0);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001058 if (error)
1059 goto out_unlock;
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001060
Christoph Hellwig3c68d442017-02-06 10:51:03 -08001061 if (flags & IOMAP_REPORT) {
Christoph Hellwig5f9268c2016-10-20 15:53:32 +11001062 /* Trim the mapping to the nearest shared extent boundary. */
1063 error = xfs_reflink_trim_around_shared(ip, &imap, &shared,
1064 &trimmed);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001065 if (error)
1066 goto out_unlock;
1067 }
1068
Dave Chinnerd0641782018-05-02 12:54:53 -07001069 /* Non-modifying mapping requested, so we are done */
1070 if (!(flags & (IOMAP_WRITE | IOMAP_ZERO)))
1071 goto out_found;
1072
Dave Chinnerdfa03a52018-05-02 12:54:54 -07001073 /*
1074 * Break shared extents if necessary. Checks for non-blocking IO have
1075 * been done up front, so we don't need to do them here.
1076 */
1077 if (xfs_is_reflink_inode(ip)) {
1078 /* if zeroing doesn't need COW allocation, then we are done. */
1079 if ((flags & IOMAP_ZERO) &&
1080 !needs_cow_for_zeroing(&imap, nimaps))
1081 goto out_found;
1082
Christoph Hellwig3c68d442017-02-06 10:51:03 -08001083 if (flags & IOMAP_DIRECT) {
1084 /* may drop and re-acquire the ilock */
1085 error = xfs_reflink_allocate_cow(ip, &imap, &shared,
1086 &lockmode);
1087 if (error)
1088 goto out_unlock;
1089 } else {
1090 error = xfs_reflink_reserve_cow(ip, &imap, &shared);
1091 if (error)
1092 goto out_unlock;
1093 }
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001094
1095 end_fsb = imap.br_startoff + imap.br_blockcount;
1096 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001097 }
1098
Dave Chinnerd0641782018-05-02 12:54:53 -07001099 /* Don't need to allocate over holes when doing zeroing operations. */
1100 if (flags & IOMAP_ZERO)
1101 goto out_found;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001102
Dave Chinnerd0641782018-05-02 12:54:53 -07001103 if (!imap_needs_alloc(inode, &imap, nimaps))
1104 goto out_found;
Christoph Hellwigb95a2122016-08-17 08:44:52 +10001105
Dave Chinnerd0641782018-05-02 12:54:53 -07001106 /* If nowait is set bail since we are going to make allocations. */
1107 if (flags & IOMAP_NOWAIT) {
1108 error = -EAGAIN;
1109 goto out_unlock;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001110 }
1111
Dave Chinnerd0641782018-05-02 12:54:53 -07001112 /*
1113 * We cap the maximum length we map to a sane size to keep the chunks
1114 * of work done where somewhat symmetric with the work writeback does.
1115 * This is a completely arbitrary number pulled out of thin air as a
1116 * best guess for initial testing.
1117 *
1118 * Note that the values needs to be less than 32-bits wide until the
1119 * lower level functions are updated.
1120 */
1121 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
1122
1123 /*
1124 * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
1125 * return.
1126 */
1127 if (lockmode == XFS_ILOCK_EXCL)
1128 xfs_ilock_demote(ip, lockmode);
1129 error = xfs_iomap_write_direct(ip, offset, length, &imap,
1130 nimaps);
1131 if (error)
1132 return error;
1133
1134 iomap->flags = IOMAP_F_NEW;
1135 trace_xfs_iomap_alloc(ip, offset, length, 0, &imap);
1136
1137out_finish:
Dan Williamsaaa422c2017-11-13 16:38:44 -08001138 if (xfs_ipincount(ip) && (ip->i_itemp->ili_fsync_fields
1139 & ~XFS_ILOG_TIMESTAMP))
Christoph Hellwiga39e5962017-11-01 16:36:47 +01001140 iomap->flags |= IOMAP_F_DIRTY;
1141
Christoph Hellwigb95a2122016-08-17 08:44:52 +10001142 xfs_bmbt_to_iomap(ip, iomap, &imap);
Dan Williamsfa5d9322017-01-27 12:04:59 -08001143
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001144 if (shared)
1145 iomap->flags |= IOMAP_F_SHARED;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001146 return 0;
Dave Chinnerd0641782018-05-02 12:54:53 -07001147
1148out_found:
1149 ASSERT(nimaps);
1150 xfs_iunlock(ip, lockmode);
1151 trace_xfs_iomap_found(ip, offset, length, 0, &imap);
1152 goto out_finish;
1153
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001154out_unlock:
1155 xfs_iunlock(ip, lockmode);
1156 return error;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001157}
1158
1159static int
1160xfs_file_iomap_end_delalloc(
1161 struct xfs_inode *ip,
1162 loff_t offset,
1163 loff_t length,
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001164 ssize_t written,
1165 struct iomap *iomap)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001166{
1167 struct xfs_mount *mp = ip->i_mount;
1168 xfs_fileoff_t start_fsb;
1169 xfs_fileoff_t end_fsb;
1170 int error = 0;
1171
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001172 /*
1173 * Behave as if the write failed if drop writes is enabled. Set the NEW
1174 * flag to force delalloc cleanup.
1175 */
Darrick J. Wongf8c47252017-06-20 17:54:48 -07001176 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001177 iomap->flags |= IOMAP_F_NEW;
Brian Foster9dbddd72017-02-13 22:48:17 -08001178 written = 0;
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001179 }
Brian Foster9dbddd72017-02-13 22:48:17 -08001180
Brian Fosterfa7f1382017-02-16 17:19:12 -08001181 /*
1182 * start_fsb refers to the first unused block after a short write. If
1183 * nothing was written, round offset down to point at the first block in
1184 * the range.
1185 */
1186 if (unlikely(!written))
1187 start_fsb = XFS_B_TO_FSBT(mp, offset);
1188 else
1189 start_fsb = XFS_B_TO_FSB(mp, offset + written);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001190 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1191
1192 /*
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001193 * Trim delalloc blocks if they were allocated by this write and we
1194 * didn't manage to write the whole range.
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001195 *
1196 * We don't need to care about racing delalloc as we hold i_mutex
1197 * across the reserve/allocate/unreserve calls. If there are delalloc
1198 * blocks in the range, they are ours.
1199 */
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001200 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
Brian Fosterfa7f1382017-02-16 17:19:12 -08001201 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1202 XFS_FSB_TO_B(mp, end_fsb) - 1);
1203
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001204 xfs_ilock(ip, XFS_ILOCK_EXCL);
1205 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1206 end_fsb - start_fsb);
1207 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1208
1209 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1210 xfs_alert(mp, "%s: unable to clean up ino %lld",
1211 __func__, ip->i_ino);
1212 return error;
1213 }
1214 }
1215
1216 return 0;
1217}
1218
1219static int
1220xfs_file_iomap_end(
1221 struct inode *inode,
1222 loff_t offset,
1223 loff_t length,
1224 ssize_t written,
1225 unsigned flags,
1226 struct iomap *iomap)
1227{
1228 if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
1229 return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
Brian Fosterf65e6fa2017-03-08 09:58:08 -08001230 length, written, iomap);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001231 return 0;
1232}
1233
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001234const struct iomap_ops xfs_iomap_ops = {
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001235 .iomap_begin = xfs_file_iomap_begin,
1236 .iomap_end = xfs_file_iomap_end,
1237};
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001238
1239static int
1240xfs_xattr_iomap_begin(
1241 struct inode *inode,
1242 loff_t offset,
1243 loff_t length,
1244 unsigned flags,
1245 struct iomap *iomap)
1246{
1247 struct xfs_inode *ip = XFS_I(inode);
1248 struct xfs_mount *mp = ip->i_mount;
1249 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1250 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1251 struct xfs_bmbt_irec imap;
1252 int nimaps = 1, error = 0;
1253 unsigned lockmode;
1254
1255 if (XFS_FORCED_SHUTDOWN(mp))
1256 return -EIO;
1257
Darrick J. Wong84358536d2017-04-06 16:00:39 -07001258 lockmode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001259
1260 /* if there are no attribute fork or extents, return ENOENT */
Darrick J. Wong84358536d2017-04-06 16:00:39 -07001261 if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001262 error = -ENOENT;
1263 goto out_unlock;
1264 }
1265
1266 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1267 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongb7e0b6f2017-12-06 16:13:35 -08001268 &nimaps, XFS_BMAPI_ATTRFORK);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001269out_unlock:
1270 xfs_iunlock(ip, lockmode);
1271
1272 if (!error) {
1273 ASSERT(nimaps);
1274 xfs_bmbt_to_iomap(ip, iomap, &imap);
1275 }
1276
1277 return error;
1278}
1279
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001280const struct iomap_ops xfs_xattr_iomap_ops = {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001281 .iomap_begin = xfs_xattr_iomap_begin,
1282};