blob: 41662fb14e87d8b1546c42d6a65508db5c5a76bf [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110034#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_iomap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000037#include "xfs_trace.h"
Brian Foster27b52862012-11-06 09:50:38 -050038#include "xfs_icache.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110039#include "xfs_quota.h"
Brian Foster76a42022013-03-18 10:51:47 -040040#include "xfs_dquot_item.h"
41#include "xfs_dquot.h"
Darrick J. Wong2a067052016-10-03 09:11:33 -070042#include "xfs_reflink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
46 << mp->m_writeio_log)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Christoph Hellwige9c49732016-09-19 11:09:12 +100048void
49xfs_bmbt_to_iomap(
50 struct xfs_inode *ip,
51 struct iomap *iomap,
52 struct xfs_bmbt_irec *imap)
53{
54 struct xfs_mount *mp = ip->i_mount;
55
56 if (imap->br_startblock == HOLESTARTBLOCK) {
57 iomap->blkno = IOMAP_NULL_BLOCK;
58 iomap->type = IOMAP_HOLE;
59 } else if (imap->br_startblock == DELAYSTARTBLOCK) {
60 iomap->blkno = IOMAP_NULL_BLOCK;
61 iomap->type = IOMAP_DELALLOC;
62 } else {
63 iomap->blkno = xfs_fsb_to_db(ip, imap->br_startblock);
64 if (imap->br_state == XFS_EXT_UNWRITTEN)
65 iomap->type = IOMAP_UNWRITTEN;
66 else
67 iomap->type = IOMAP_MAPPED;
68 }
69 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
70 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
71 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
72}
73
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070074xfs_extlen_t
Christoph Hellwigf8e3a822016-09-19 11:09:28 +100075xfs_eof_alignment(
76 struct xfs_inode *ip,
77 xfs_extlen_t extsize)
Nathan Scottdd9f4382006-01-11 15:28:28 +110078{
Christoph Hellwigf8e3a822016-09-19 11:09:28 +100079 struct xfs_mount *mp = ip->i_mount;
80 xfs_extlen_t align = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +110081
Christoph Hellwigbf322d92011-12-18 20:00:05 +000082 if (!XFS_IS_REALTIME_INODE(ip)) {
83 /*
84 * Round up the allocation request to a stripe unit
85 * (m_dalign) boundary if the file size is >= stripe unit
86 * size, and we are allocating past the allocation eof.
87 *
88 * If mounted with the "-o swalloc" option the alignment is
89 * increased from the strip unit size to the stripe width.
90 */
91 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
92 align = mp->m_swidth;
93 else if (mp->m_dalign)
94 align = mp->m_dalign;
95
Peter Watkins76b57302014-12-04 09:30:51 +110096 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
97 align = 0;
Christoph Hellwigbf322d92011-12-18 20:00:05 +000098 }
Nathan Scottdd9f4382006-01-11 15:28:28 +110099
100 /*
101 * Always round up the allocation request to an extent boundary
102 * (when file on a real-time subvolume or has di_extsize hint).
103 */
104 if (extsize) {
Peter Watkins76b57302014-12-04 09:30:51 +1100105 if (align)
106 align = roundup_64(align, extsize);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100107 else
108 align = extsize;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100109 }
110
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000111 return align;
112}
113
114STATIC int
115xfs_iomap_eof_align_last_fsb(
116 struct xfs_inode *ip,
117 xfs_extlen_t extsize,
118 xfs_fileoff_t *last_fsb)
119{
120 xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
121
Peter Watkins76b57302014-12-04 09:30:51 +1100122 if (align) {
123 xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000124 int eof, error;
125
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000126 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100127 if (error)
128 return error;
129 if (eof)
130 *last_fsb = new_last_fsb;
131 }
132 return 0;
133}
134
135STATIC int
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100136xfs_alert_fsblock_zero(
Nathan Scott572d95f2006-09-28 11:03:20 +1000137 xfs_inode_t *ip,
138 xfs_bmbt_irec_t *imap)
139{
Dave Chinner6a19d932011-03-07 10:02:35 +1100140 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +1000141 "Access to block zero in inode %llu "
142 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -0500143 "blkcnt: %llx extent-state: %x",
Nathan Scott572d95f2006-09-28 11:03:20 +1000144 (unsigned long long)ip->i_ino,
145 (unsigned long long)imap->br_startblock,
146 (unsigned long long)imap->br_startoff,
147 (unsigned long long)imap->br_blockcount,
148 imap->br_state);
Dave Chinner24513372014-06-25 14:58:08 +1000149 return -EFSCORRUPTED;
Nathan Scott572d95f2006-09-28 11:03:20 +1000150}
151
Christoph Hellwiga206c812010-12-10 08:42:20 +0000152int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153xfs_iomap_write_direct(
154 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700155 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 size_t count,
Christoph Hellwig30704512010-06-24 11:42:19 +1000157 xfs_bmbt_irec_t *imap,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000158 int nmaps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 xfs_fileoff_t offset_fsb;
162 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100163 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 xfs_fsblock_t firstfsb;
Christoph Hellwigf13eb202017-02-06 10:42:26 -0800165 xfs_extlen_t extsz;
Eric Sandeen0116d932005-11-02 15:00:01 +1100166 int nimaps;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000167 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int rt;
169 xfs_trans_t *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000170 struct xfs_defer_ops dfops;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100171 uint qblocks, resblks, resrtextents;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100172 int error;
Brian Foster009c6e82015-10-12 15:34:20 +1100173 int lockmode;
Dave Chinner1ca19152015-11-03 12:37:00 +1100174 int bmapi_flags = XFS_BMAPI_PREALLOC;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000175 uint tflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Nathan Scottdd9f4382006-01-11 15:28:28 +1100177 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000178 extsz = xfs_get_extsz_hint(ip);
Brian Foster009c6e82015-10-12 15:34:20 +1100179 lockmode = XFS_ILOCK_SHARED; /* locked by caller */
180
181 ASSERT(xfs_isilocked(ip, lockmode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
David Chinner957d0eb2007-06-18 16:50:37 +1000183 offset_fsb = XFS_B_TO_FSBT(mp, offset);
184 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000185 if ((offset + count) > XFS_ISIZE(ip)) {
Brian Foster009c6e82015-10-12 15:34:20 +1100186 /*
187 * Assert that the in-core extent list is present since this can
188 * call xfs_iread_extents() and we only have the ilock shared.
189 * This should be safe because the lock was held around a bmapi
190 * call in the caller and we only need it to access the in-core
191 * list.
192 */
193 ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
194 XFS_IFEXTENTS);
Christoph Hellwigf8e3a822016-09-19 11:09:28 +1000195 error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100196 if (error)
Brian Foster009c6e82015-10-12 15:34:20 +1100197 goto out_unlock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100198 } else {
Christoph Hellwig405f8042010-12-10 08:42:19 +0000199 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100200 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
Christoph Hellwig30704512010-06-24 11:42:19 +1000201 imap->br_blockcount +
202 imap->br_startoff);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100203 }
204 count_fsb = last_fsb - offset_fsb;
205 ASSERT(count_fsb > 0);
Christoph Hellwigf13eb202017-02-06 10:42:26 -0800206 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100207
208 if (unlikely(rt)) {
209 resrtextents = qblocks = resaligned;
210 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000211 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
212 quota_flag = XFS_QMOPT_RES_RTBLKS;
213 } else {
214 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100215 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000216 quota_flag = XFS_QMOPT_RES_REGBLKS;
217 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /*
Brian Foster009c6e82015-10-12 15:34:20 +1100220 * Drop the shared lock acquired by the caller, attach the dquot if
221 * necessary and move on to transaction setup.
222 */
223 xfs_iunlock(ip, lockmode);
224 error = xfs_qm_dqattach(ip, 0);
225 if (error)
226 return error;
227
228 /*
Dave Chinner1ca19152015-11-03 12:37:00 +1100229 * For DAX, we do not allocate unwritten extents, but instead we zero
230 * the block before we commit the transaction. Ideally we'd like to do
231 * this outside the transaction context, but if we commit and then crash
232 * we may not have zeroed the blocks and this will be exposed on
233 * recovery of the allocation. Hence we must zero before commit.
Dave Chinner3b0fe472016-01-04 16:22:45 +1100234 *
Dave Chinner1ca19152015-11-03 12:37:00 +1100235 * Further, if we are mapping unwritten extents here, we need to zero
236 * and convert them to written so that we don't need an unwritten extent
237 * callback for DAX. This also means that we need to be able to dip into
Dave Chinner3b0fe472016-01-04 16:22:45 +1100238 * the reserve block pool for bmbt block allocation if there is no space
239 * left but we need to do unwritten extent conversion.
Dave Chinner1ca19152015-11-03 12:37:00 +1100240 */
241 if (IS_DAX(VFS_I(ip))) {
242 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
Dave Chinner3b0fe472016-01-04 16:22:45 +1100243 if (ISUNWRITTEN(imap)) {
Christoph Hellwig253f4912016-04-06 09:19:55 +1000244 tflags |= XFS_TRANS_RESERVE;
Dave Chinner3b0fe472016-01-04 16:22:45 +1100245 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
246 }
Dave Chinner1ca19152015-11-03 12:37:00 +1100247 }
Christoph Hellwig253f4912016-04-06 09:19:55 +1000248 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
249 tflags, &tp);
250 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000251 return error;
Dave Chinner507630b2012-03-27 10:34:50 -0400252
Brian Foster009c6e82015-10-12 15:34:20 +1100253 lockmode = XFS_ILOCK_EXCL;
254 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Christoph Hellwig7d095252009-06-08 15:33:32 +0200256 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100257 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400258 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Christoph Hellwigddc34152011-09-19 15:00:54 +0000260 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000263 * From this point onwards we overwrite the imap pointer that the
264 * caller gave to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000266 xfs_defer_init(&dfops, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000267 nimaps = 1;
Christoph Hellwigd531d912014-02-10 10:27:43 +1100268 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Dave Chinner264e89a2015-11-03 13:28:41 +1100269 bmapi_flags, &firstfsb, resblks, imap,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000270 &nimaps, &dfops);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000271 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400272 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000275 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000277 error = xfs_defer_finish(&tp, &dfops, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000278 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400279 goto out_bmap_cancel;
Dave Chinner1ca19152015-11-03 12:37:00 +1100280
Christoph Hellwig70393312015-06-04 13:48:08 +1000281 error = xfs_trans_commit(tp);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000282 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400283 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Nathan Scott06d10dd2005-06-21 15:48:47 +1000285 /*
286 * Copy any maps to caller's array and return any error.
287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (nimaps == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000289 error = -ENOSPC;
Dave Chinner507630b2012-03-27 10:34:50 -0400290 goto out_unlock;
Nathan Scott572d95f2006-09-28 11:03:20 +1000291 }
292
Dave Chinner507630b2012-03-27 10:34:50 -0400293 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100294 error = xfs_alert_fsblock_zero(ip, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Dave Chinner507630b2012-03-27 10:34:50 -0400296out_unlock:
Brian Foster009c6e82015-10-12 15:34:20 +1100297 xfs_iunlock(ip, lockmode);
Dave Chinner507630b2012-03-27 10:34:50 -0400298 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Dave Chinner507630b2012-03-27 10:34:50 -0400300out_bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000301 xfs_defer_cancel(&dfops);
Dave Chinnerea562ed2012-05-08 20:48:53 +1000302 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
Dave Chinner507630b2012-03-27 10:34:50 -0400303out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000304 xfs_trans_cancel(tp);
Dave Chinner507630b2012-03-27 10:34:50 -0400305 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Brian Foster76a42022013-03-18 10:51:47 -0400308STATIC bool
309xfs_quota_need_throttle(
310 struct xfs_inode *ip,
311 int type,
312 xfs_fsblock_t alloc_blocks)
313{
314 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
315
316 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
317 return false;
318
319 /* no hi watermark, no throttle */
320 if (!dq->q_prealloc_hi_wmark)
321 return false;
322
323 /* under the lo watermark, no throttle */
324 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
325 return false;
326
327 return true;
328}
329
330STATIC void
331xfs_quota_calc_throttle(
332 struct xfs_inode *ip,
333 int type,
334 xfs_fsblock_t *qblocks,
Brian Fosterf0740512014-07-24 19:56:08 +1000335 int *qshift,
336 int64_t *qfreesp)
Brian Foster76a42022013-03-18 10:51:47 -0400337{
338 int64_t freesp;
339 int shift = 0;
340 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
341
Eric Sandeen5cca3f62014-10-02 09:27:09 +1000342 /* no dq, or over hi wmark, squash the prealloc completely */
343 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
Brian Foster76a42022013-03-18 10:51:47 -0400344 *qblocks = 0;
Brian Fosterf0740512014-07-24 19:56:08 +1000345 *qfreesp = 0;
Brian Foster76a42022013-03-18 10:51:47 -0400346 return;
347 }
348
349 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
350 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
351 shift = 2;
352 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
353 shift += 2;
354 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
355 shift += 2;
356 }
357
Brian Fosterf0740512014-07-24 19:56:08 +1000358 if (freesp < *qfreesp)
359 *qfreesp = freesp;
360
Brian Foster76a42022013-03-18 10:51:47 -0400361 /* only overwrite the throttle values if we are more aggressive */
362 if ((freesp >> shift) < (*qblocks >> *qshift)) {
363 *qblocks = freesp;
364 *qshift = shift;
365 }
366}
367
Dave Chinnera1e16c22013-02-11 16:05:01 +1100368/*
Christoph Hellwig51446f52016-09-19 11:10:21 +1000369 * If we are doing a write at the end of the file and there are no allocations
370 * past this one, then extend the allocation out to the file system's write
371 * iosize.
372 *
Dave Chinner055388a2011-01-04 11:35:03 +1100373 * If we don't have a user specified preallocation size, dynamically increase
Christoph Hellwig51446f52016-09-19 11:10:21 +1000374 * the preallocation size as the size of the file grows. Cap the maximum size
Dave Chinner055388a2011-01-04 11:35:03 +1100375 * at a single extent or less if the filesystem is near full. The closer the
376 * filesystem is to full, the smaller the maximum prealocation.
Christoph Hellwig51446f52016-09-19 11:10:21 +1000377 *
378 * As an exception we don't do any preallocation at all if the file is smaller
379 * than the minimum preallocation and we are using the default dynamic
380 * preallocation scheme, as it is likely this is the only write to the file that
381 * is going to be done.
382 *
383 * We clean up any extra space left over when the file is closed in
384 * xfs_inactive().
Dave Chinner055388a2011-01-04 11:35:03 +1100385 */
386STATIC xfs_fsblock_t
387xfs_iomap_prealloc_size(
Dave Chinnera1e16c22013-02-11 16:05:01 +1100388 struct xfs_inode *ip,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000389 loff_t offset,
390 loff_t count,
Christoph Hellwig656152e2016-11-24 11:39:44 +1100391 xfs_extnum_t idx)
Dave Chinner055388a2011-01-04 11:35:03 +1100392{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000393 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig656152e2016-11-24 11:39:44 +1100394 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000395 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig656152e2016-11-24 11:39:44 +1100396 struct xfs_bmbt_irec prev;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400397 int shift = 0;
398 int64_t freesp;
Brian Foster76a42022013-03-18 10:51:47 -0400399 xfs_fsblock_t qblocks;
400 int qshift = 0;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000401 xfs_fsblock_t alloc_blocks = 0;
Dave Chinner055388a2011-01-04 11:35:03 +1100402
Christoph Hellwig51446f52016-09-19 11:10:21 +1000403 if (offset + count <= XFS_ISIZE(ip))
404 return 0;
405
406 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
407 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
408 return 0;
409
410 /*
411 * If an explicit allocsize is set, the file is small, or we
412 * are writing behind a hole, then use the minimum prealloc:
413 */
414 if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
415 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
Christoph Hellwig656152e2016-11-24 11:39:44 +1100416 !xfs_iext_get_extent(ifp, idx - 1, &prev) ||
417 prev.br_startoff + prev.br_blockcount < offset_fsb)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000418 return mp->m_writeio_blocks;
419
420 /*
421 * Determine the initial size of the preallocation. We are beyond the
422 * current EOF here, but we need to take into account whether this is
423 * a sparse write or an extending write when determining the
424 * preallocation size. Hence we need to look up the extent that ends
425 * at the current write offset and use the result to determine the
426 * preallocation size.
427 *
428 * If the extent is a hole, then preallocation is essentially disabled.
429 * Otherwise we take the size of the preceding data extent as the basis
430 * for the preallocation size. If the size of the extent is greater than
431 * half the maximum extent length, then use the current offset as the
432 * basis. This ensures that for large files the preallocation size
433 * always extends to MAXEXTLEN rather than falling short due to things
434 * like stripe unit/width alignment of real extents.
435 */
Christoph Hellwig656152e2016-11-24 11:39:44 +1100436 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
437 alloc_blocks = prev.br_blockcount << 1;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000438 else
439 alloc_blocks = XFS_B_TO_FSB(mp, offset);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400440 if (!alloc_blocks)
441 goto check_writeio;
Brian Foster76a42022013-03-18 10:51:47 -0400442 qblocks = alloc_blocks;
Dave Chinner055388a2011-01-04 11:35:03 +1100443
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400444 /*
445 * MAXEXTLEN is not a power of two value but we round the prealloc down
446 * to the nearest power of two value after throttling. To prevent the
447 * round down from unconditionally reducing the maximum supported prealloc
448 * size, we round up first, apply appropriate throttling, round down and
449 * cap the value to MAXEXTLEN.
450 */
451 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
452 alloc_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100453
Dave Chinner0d485ad2015-02-23 21:22:03 +1100454 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400455 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
456 shift = 2;
457 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
458 shift++;
459 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
460 shift++;
461 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
462 shift++;
463 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
464 shift++;
Dave Chinner055388a2011-01-04 11:35:03 +1100465 }
Brian Foster76a42022013-03-18 10:51:47 -0400466
467 /*
Brian Fosterf0740512014-07-24 19:56:08 +1000468 * Check each quota to cap the prealloc size, provide a shift value to
469 * throttle with and adjust amount of available space.
Brian Foster76a42022013-03-18 10:51:47 -0400470 */
471 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000472 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
473 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400474 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000475 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
476 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400477 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000478 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
479 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400480
481 /*
482 * The final prealloc size is set to the minimum of free space available
483 * in each of the quotas and the overall filesystem.
484 *
485 * The shift throttle value is set to the maximum value as determined by
486 * the global low free space values and per-quota low free space values.
487 */
488 alloc_blocks = MIN(alloc_blocks, qblocks);
489 shift = MAX(shift, qshift);
490
Brian Foster3c58b5f2013-03-18 10:51:43 -0400491 if (shift)
492 alloc_blocks >>= shift;
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400493 /*
494 * rounddown_pow_of_two() returns an undefined result if we pass in
495 * alloc_blocks = 0.
496 */
497 if (alloc_blocks)
498 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
499 if (alloc_blocks > MAXEXTLEN)
500 alloc_blocks = MAXEXTLEN;
Dave Chinner055388a2011-01-04 11:35:03 +1100501
Brian Foster3c58b5f2013-03-18 10:51:43 -0400502 /*
503 * If we are still trying to allocate more space than is
504 * available, squash the prealloc hard. This can happen if we
505 * have a large file on a small filesystem and the above
506 * lowspace thresholds are smaller than MAXEXTLEN.
507 */
508 while (alloc_blocks && alloc_blocks >= freesp)
509 alloc_blocks >>= 4;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400510check_writeio:
Dave Chinner055388a2011-01-04 11:35:03 +1100511 if (alloc_blocks < mp->m_writeio_blocks)
512 alloc_blocks = mp->m_writeio_blocks;
Brian Foster19cb7e32013-03-18 10:51:48 -0400513 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
514 mp->m_writeio_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100515 return alloc_blocks;
516}
517
Christoph Hellwig51446f52016-09-19 11:10:21 +1000518static int
519xfs_file_iomap_begin_delay(
520 struct inode *inode,
521 loff_t offset,
522 loff_t count,
523 unsigned flags,
524 struct iomap *iomap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000526 struct xfs_inode *ip = XFS_I(inode);
527 struct xfs_mount *mp = ip->i_mount;
528 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
529 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
530 xfs_fileoff_t maxbytes_fsb =
531 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Brian Fosterf7820882016-11-28 14:57:42 +1100532 xfs_fileoff_t end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000533 int error = 0, eof = 0;
534 struct xfs_bmbt_irec got;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000535 xfs_extnum_t idx;
Brian Fosterf7820882016-11-28 14:57:42 +1100536 xfs_fsblock_t prealloc_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Christoph Hellwig51446f52016-09-19 11:10:21 +1000538 ASSERT(!XFS_IS_REALTIME_INODE(ip));
539 ASSERT(!xfs_get_extsz_hint(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Christoph Hellwig51446f52016-09-19 11:10:21 +1000541 xfs_ilock(ip, XFS_ILOCK_EXCL);
542
543 if (unlikely(XFS_TEST_ERROR(
544 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
545 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
546 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
547 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
548 error = -EFSCORRUPTED;
549 goto out_unlock;
550 }
551
552 XFS_STATS_INC(mp, xs_blk_mapw);
553
554 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
555 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
556 if (error)
557 goto out_unlock;
558 }
559
Christoph Hellwig656152e2016-11-24 11:39:44 +1100560 eof = !xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000561 if (!eof && got.br_startoff <= offset_fsb) {
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100562 if (xfs_is_reflink_inode(ip)) {
563 bool shared;
564
565 end_fsb = min(XFS_B_TO_FSB(mp, offset + count),
566 maxbytes_fsb);
567 xfs_trim_extent(&got, offset_fsb, end_fsb - offset_fsb);
568 error = xfs_reflink_reserve_cow(ip, &got, &shared);
569 if (error)
570 goto out_unlock;
571 }
572
Christoph Hellwig51446f52016-09-19 11:10:21 +1000573 trace_xfs_iomap_found(ip, offset, count, 0, &got);
574 goto done;
575 }
576
Christoph Hellwig7d095252009-06-08 15:33:32 +0200577 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (error)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000579 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Christoph Hellwig51446f52016-09-19 11:10:21 +1000581 /*
582 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES pages
583 * to keep the chunks of work done where somewhat symmetric with the
584 * work writeback does. This is a completely arbitrary number pulled
585 * out of thin air as a best guess for initial testing.
586 *
587 * Note that the values needs to be less than 32-bits wide until
588 * the lower level functions are updated.
589 */
590 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
Brian Fosterf7820882016-11-28 14:57:42 +1100591 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100592
Christoph Hellwig51446f52016-09-19 11:10:21 +1000593 if (eof) {
Christoph Hellwig656152e2016-11-24 11:39:44 +1100594 prealloc_blocks = xfs_iomap_prealloc_size(ip, offset, count, idx);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000595 if (prealloc_blocks) {
596 xfs_extlen_t align;
597 xfs_off_t end_offset;
Brian Fosterf7820882016-11-28 14:57:42 +1100598 xfs_fileoff_t p_end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000599
600 end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
Brian Fosterf7820882016-11-28 14:57:42 +1100601 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
602 prealloc_blocks;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000603
604 align = xfs_eof_alignment(ip, 0);
605 if (align)
Brian Fosterf7820882016-11-28 14:57:42 +1100606 p_end_fsb = roundup_64(p_end_fsb, align);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000607
Brian Fosterf7820882016-11-28 14:57:42 +1100608 p_end_fsb = min(p_end_fsb, maxbytes_fsb);
609 ASSERT(p_end_fsb > offset_fsb);
610 prealloc_blocks = p_end_fsb - end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000611 }
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Dave Chinner8de2bf92009-04-06 18:49:12 +0200614retry:
Darrick J. Wongbe51f812016-10-03 09:11:32 -0700615 error = xfs_bmapi_reserve_delalloc(ip, XFS_DATA_FORK, offset_fsb,
Brian Fosterf7820882016-11-28 14:57:42 +1100616 end_fsb - offset_fsb, prealloc_blocks, &got, &idx, eof);
Dave Chinner055388a2011-01-04 11:35:03 +1100617 switch (error) {
618 case 0:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000619 break;
Dave Chinner24513372014-06-25 14:58:08 +1000620 case -ENOSPC:
621 case -EDQUOT:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000622 /* retry without any preallocation */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000623 trace_xfs_delalloc_enospc(ip, offset, count);
Brian Fosterf7820882016-11-28 14:57:42 +1100624 if (prealloc_blocks) {
625 prealloc_blocks = 0;
Dave Chinner9aa05002012-10-08 21:56:04 +1100626 goto retry;
Dave Chinner055388a2011-01-04 11:35:03 +1100627 }
Christoph Hellwig51446f52016-09-19 11:10:21 +1000628 /*FALLTHRU*/
629 default:
630 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Christoph Hellwig51446f52016-09-19 11:10:21 +1000633 trace_xfs_iomap_alloc(ip, offset, count, 0, &got);
634done:
635 if (isnullstartblock(got.br_startblock))
636 got.br_startblock = DELAYSTARTBLOCK;
637
638 if (!got.br_startblock) {
639 error = xfs_alert_fsblock_zero(ip, &got);
640 if (error)
641 goto out_unlock;
642 }
643
644 xfs_bmbt_to_iomap(ip, iomap, &got);
645
646out_unlock:
647 xfs_iunlock(ip, XFS_ILOCK_EXCL);
648 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651/*
652 * Pass in a delayed allocate extent, convert it to real extents;
653 * return to the caller the extent we create which maps on top of
654 * the originating callers request.
655 *
656 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100657 *
658 * We no longer bother to look at the incoming map - all we have to
659 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
Christoph Hellwiga206c812010-12-10 08:42:20 +0000661int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662xfs_iomap_write_allocate(
663 xfs_inode_t *ip,
Darrick J. Wong60b49842016-10-03 09:11:34 -0700664 int whichfork,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700665 xfs_off_t offset,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000666 xfs_bmbt_irec_t *imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 xfs_fileoff_t offset_fsb, last_block;
670 xfs_fileoff_t end_fsb, map_start_fsb;
671 xfs_fsblock_t first_block;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000672 struct xfs_defer_ops dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 xfs_filblks_t count_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 xfs_trans_t *tp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100675 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int error = 0;
Christoph Hellwigd2b39642017-01-20 09:31:54 -0800677 int flags = XFS_BMAPI_DELALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int nres;
679
Darrick J. Wong60b49842016-10-03 09:11:34 -0700680 if (whichfork == XFS_COW_FORK)
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800681 flags |= XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /*
684 * Make sure that the dquots are there.
685 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200686 error = xfs_qm_dqattach(ip, 0);
687 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000688 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Nathan Scott24e17b52005-05-05 13:33:20 -0700690 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig30704512010-06-24 11:42:19 +1000691 count_fsb = imap->br_blockcount;
692 map_start_fsb = imap->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100694 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 while (count_fsb != 0) {
697 /*
698 * Set up a transaction with which to allocate the
699 * backing store for the file. Do allocations in a
700 * loop until we get some space in the range we are
701 * interested in. The other space that might be allocated
702 * is in the delayed allocation extent on which we sit
703 * but before our buffer starts.
704 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 nimaps = 0;
706 while (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
Christoph Hellwig0af32fb2016-08-17 08:30:28 +1000708 /*
709 * We have already reserved space for the extent and any
710 * indirect blocks when creating the delalloc extent,
711 * there is no need to reserve space in this transaction
712 * again.
713 */
714 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0,
Christoph Hellwig253f4912016-04-06 09:19:55 +1000715 0, XFS_TRANS_RESERVE, &tp);
716 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000717 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000720 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000722 xfs_defer_init(&dfops, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /*
David Chinnere4143a12007-11-23 16:29:11 +1100725 * it is possible that the extents have changed since
726 * we did the read call as we dropped the ilock for a
727 * while. We have to be careful about truncates or hole
728 * punchs here - we are not allowed to allocate
729 * non-delalloc blocks here.
730 *
731 * The only protection against truncation is the pages
732 * for the range we are being asked to convert are
733 * locked and hence a truncate will block on them
734 * first.
735 *
736 * As a result, if we go beyond the range we really
737 * need and hit an delalloc extent boundary followed by
738 * a hole while we have excess blocks in the map, we
739 * will fill the hole incorrectly and overrun the
740 * transaction reservation.
741 *
742 * Using a single map prevents this as we are forced to
743 * check each map we look for overlap with the desired
744 * range and abort as soon as we find it. Also, given
745 * that we only return a single map, having one beyond
746 * what we can return is probably a bit silly.
747 *
748 * We also need to check that we don't go beyond EOF;
749 * this is a truncate optimisation as a truncate sets
750 * the new file size before block on the pages we
751 * currently have locked under writeback. Because they
752 * are about to be tossed, we don't need to write them
753 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 */
David Chinnere4143a12007-11-23 16:29:11 +1100755 nimaps = 1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000756 end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000757 error = xfs_bmap_last_offset(ip, &last_block,
David Chinner7c9ef852008-04-10 12:21:59 +1000758 XFS_DATA_FORK);
759 if (error)
760 goto trans_cancel;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
763 if ((map_start_fsb + count_fsb) > last_block) {
764 count_fsb = last_block - map_start_fsb;
765 if (count_fsb == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000766 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto trans_cancel;
768 }
769 }
770
Christoph Hellwig30704512010-06-24 11:42:19 +1000771 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000772 * From this point onwards we overwrite the imap
773 * pointer that the caller gave to us.
774 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000775 error = xfs_bmapi_write(tp, ip, map_start_fsb,
Darrick J. Wong60b49842016-10-03 09:11:34 -0700776 count_fsb, flags, &first_block,
Brian Fosterdbd5c8c2015-10-12 16:04:13 +1100777 nres, imap, &nimaps,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000778 &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (error)
780 goto trans_cancel;
781
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000782 error = xfs_defer_finish(&tp, &dfops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (error)
784 goto trans_cancel;
785
Christoph Hellwig70393312015-06-04 13:48:08 +1000786 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (error)
788 goto error0;
789
790 xfs_iunlock(ip, XFS_ILOCK_EXCL);
791 }
792
793 /*
794 * See if we were able to allocate an extent that
795 * covers at least part of the callers request
796 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000797 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100798 return xfs_alert_fsblock_zero(ip, imap);
David Chinner86c4d622008-04-29 12:53:21 +1000799
Christoph Hellwig30704512010-06-24 11:42:19 +1000800 if ((offset_fsb >= imap->br_startoff) &&
801 (offset_fsb < (imap->br_startoff +
802 imap->br_blockcount))) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100803 XFS_STATS_INC(mp, xs_xstrat_quick);
David Chinnere4143a12007-11-23 16:29:11 +1100804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
David Chinnere4143a12007-11-23 16:29:11 +1100807 /*
808 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * file, just surrounding data, try again.
810 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000811 count_fsb -= imap->br_blockcount;
812 map_start_fsb = imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814
815trans_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000816 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000817 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818error0:
819 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000820 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823int
824xfs_iomap_write_unwritten(
825 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700826 xfs_off_t offset,
Christoph Hellwigd32057f2015-01-09 10:48:12 +1100827 xfs_off_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 xfs_fileoff_t offset_fsb;
831 xfs_filblks_t count_fsb;
832 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100833 xfs_fsblock_t firstfsb;
834 int nimaps;
835 xfs_trans_t *tp;
836 xfs_bmbt_irec_t imap;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000837 struct xfs_defer_ops dfops;
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000838 xfs_fsize_t i_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100839 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000842 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 offset_fsb = XFS_B_TO_FSBT(mp, offset);
845 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
846 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
847
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000848 /*
849 * Reserve enough blocks in this transaction for two complete extent
850 * btree splits. We may be converting the middle part of an unwritten
851 * extent and in this case we will insert two new extents in the btree
852 * each of which could cause a full split.
853 *
854 * This reservation amount will be used in the first call to
855 * xfs_bmbt_split() to select an AG with enough space to satisfy the
856 * rest of the operation.
857 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100858 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Nathan Scottdd9f4382006-01-11 15:28:28 +1100860 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
Christoph Hellwig253f4912016-04-06 09:19:55 +1000862 * Set up a transaction to convert the range of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * from unwritten to real. Do allocations in a loop until
864 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000865 *
Christoph Hellwig253f4912016-04-06 09:19:55 +1000866 * Note that we can't risk to recursing back into the filesystem
867 * here as we might be asked to write out the same inode that we
868 * complete here and might deadlock on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000870 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
871 XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
872 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000873 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000876 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /*
879 * Modify the unwritten extent state of the buffer.
880 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000881 xfs_defer_init(&dfops, &firstfsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 nimaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000883 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Brian Fosterdbd5c8c2015-10-12 16:04:13 +1100884 XFS_BMAPI_CONVERT, &firstfsb, resblks,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000885 &imap, &nimaps, &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (error)
887 goto error_on_bmapi_transaction;
888
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000889 /*
890 * Log the updated inode size as we go. We have to be careful
891 * to only log it up to the actual write offset if it is
892 * halfway into a block.
893 */
894 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
895 if (i_size > offset + count)
896 i_size = offset + count;
897
898 i_size = xfs_new_eof(ip, i_size);
899 if (i_size) {
900 ip->i_d.di_size = i_size;
901 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
902 }
903
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000904 error = xfs_defer_finish(&tp, &dfops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (error)
906 goto error_on_bmapi_transaction;
907
Christoph Hellwig70393312015-06-04 13:48:08 +1000908 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 xfs_iunlock(ip, XFS_ILOCK_EXCL);
910 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000911 return error;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100912
David Chinner86c4d622008-04-29 12:53:21 +1000913 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100914 return xfs_alert_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if ((numblks_fsb = imap.br_blockcount) == 0) {
917 /*
918 * The numblks_fsb value should always get
919 * smaller, otherwise the loop is stuck.
920 */
921 ASSERT(imap.br_blockcount);
922 break;
923 }
924 offset_fsb += numblks_fsb;
925 count_fsb -= numblks_fsb;
926 } while (count_fsb > 0);
927
928 return 0;
929
930error_on_bmapi_transaction:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000931 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000932 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000934 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
Christoph Hellwig3b3dce02016-06-21 09:52:47 +1000936
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000937static inline bool imap_needs_alloc(struct inode *inode,
938 struct xfs_bmbt_irec *imap, int nimaps)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000939{
940 return !nimaps ||
941 imap->br_startblock == HOLESTARTBLOCK ||
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000942 imap->br_startblock == DELAYSTARTBLOCK ||
943 (IS_DAX(inode) && ISUNWRITTEN(imap));
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000944}
945
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100946static inline bool need_excl_ilock(struct xfs_inode *ip, unsigned flags)
947{
948 /*
949 * COW writes will allocate delalloc space, so we need to make sure
950 * to take the lock exclusively here.
951 */
952 if (xfs_is_reflink_inode(ip) && (flags & (IOMAP_WRITE | IOMAP_ZERO)))
953 return true;
954 if ((flags & IOMAP_DIRECT) && (flags & IOMAP_WRITE))
955 return true;
956 return false;
957}
958
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000959static int
960xfs_file_iomap_begin(
961 struct inode *inode,
962 loff_t offset,
963 loff_t length,
964 unsigned flags,
965 struct iomap *iomap)
966{
967 struct xfs_inode *ip = XFS_I(inode);
968 struct xfs_mount *mp = ip->i_mount;
969 struct xfs_bmbt_irec imap;
970 xfs_fileoff_t offset_fsb, end_fsb;
971 int nimaps = 1, error = 0;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100972 bool shared = false, trimmed = false;
Christoph Hellwig66642c52016-09-19 11:26:39 +1000973 unsigned lockmode;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000974
975 if (XFS_FORCED_SHUTDOWN(mp))
976 return -EIO;
977
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100978 if (((flags & (IOMAP_WRITE | IOMAP_DIRECT)) == IOMAP_WRITE) &&
979 !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
Darrick J. Wong2a067052016-10-03 09:11:33 -0700980 /* Reserve delalloc blocks for regular writeback. */
Christoph Hellwig51446f52016-09-19 11:10:21 +1000981 return xfs_file_iomap_begin_delay(inode, offset, length, flags,
982 iomap);
983 }
984
Christoph Hellwigacdda3a2016-11-30 14:37:15 +1100985 if (need_excl_ilock(ip, flags)) {
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100986 lockmode = XFS_ILOCK_EXCL;
987 xfs_ilock(ip, XFS_ILOCK_EXCL);
988 } else {
989 lockmode = xfs_ilock_data_map_shared(ip);
990 }
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000991
992 ASSERT(offset <= mp->m_super->s_maxbytes);
993 if ((xfs_fsize_t)offset + length > mp->m_super->s_maxbytes)
994 length = mp->m_super->s_maxbytes - offset;
995 offset_fsb = XFS_B_TO_FSBT(mp, offset);
996 end_fsb = XFS_B_TO_FSB(mp, offset + length);
997
998 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700999 &nimaps, 0);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001000 if (error)
1001 goto out_unlock;
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001002
Christoph Hellwig3c68d442017-02-06 10:51:03 -08001003 if (flags & IOMAP_REPORT) {
Christoph Hellwig5f9268c2016-10-20 15:53:32 +11001004 /* Trim the mapping to the nearest shared extent boundary. */
1005 error = xfs_reflink_trim_around_shared(ip, &imap, &shared,
1006 &trimmed);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001007 if (error)
1008 goto out_unlock;
1009 }
1010
1011 if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && xfs_is_reflink_inode(ip)) {
Christoph Hellwig3c68d442017-02-06 10:51:03 -08001012 if (flags & IOMAP_DIRECT) {
1013 /* may drop and re-acquire the ilock */
1014 error = xfs_reflink_allocate_cow(ip, &imap, &shared,
1015 &lockmode);
1016 if (error)
1017 goto out_unlock;
1018 } else {
1019 error = xfs_reflink_reserve_cow(ip, &imap, &shared);
1020 if (error)
1021 goto out_unlock;
1022 }
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001023
1024 end_fsb = imap.br_startoff + imap.br_blockcount;
1025 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001026 }
1027
Christoph Hellwig6c31f492016-09-19 11:28:38 +10001028 if ((flags & IOMAP_WRITE) && imap_needs_alloc(inode, &imap, nimaps)) {
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001029 /*
1030 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
1031 * pages to keep the chunks of work done where somewhat symmetric
1032 * with the work writeback does. This is a completely arbitrary
1033 * number pulled out of thin air as a best guess for initial
1034 * testing.
1035 *
1036 * Note that the values needs to be less than 32-bits wide until
1037 * the lower level functions are updated.
1038 */
1039 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
Christoph Hellwig51446f52016-09-19 11:10:21 +10001040 /*
1041 * xfs_iomap_write_direct() expects the shared lock. It
1042 * is unlocked on return.
1043 */
Christoph Hellwig66642c52016-09-19 11:26:39 +10001044 if (lockmode == XFS_ILOCK_EXCL)
1045 xfs_ilock_demote(ip, lockmode);
Christoph Hellwig51446f52016-09-19 11:10:21 +10001046 error = xfs_iomap_write_direct(ip, offset, length, &imap,
1047 nimaps);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001048 if (error)
1049 return error;
1050
Christoph Hellwigecd50722016-09-19 11:24:37 +10001051 iomap->flags = IOMAP_F_NEW;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001052 trace_xfs_iomap_alloc(ip, offset, length, 0, &imap);
Christoph Hellwigb95a2122016-08-17 08:44:52 +10001053 } else {
1054 ASSERT(nimaps);
1055
Christoph Hellwig66642c52016-09-19 11:26:39 +10001056 xfs_iunlock(ip, lockmode);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001057 trace_xfs_iomap_found(ip, offset, length, 0, &imap);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001058 }
1059
Christoph Hellwigb95a2122016-08-17 08:44:52 +10001060 xfs_bmbt_to_iomap(ip, iomap, &imap);
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001061 if (shared)
1062 iomap->flags |= IOMAP_F_SHARED;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001063 return 0;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001064out_unlock:
1065 xfs_iunlock(ip, lockmode);
1066 return error;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001067}
1068
1069static int
1070xfs_file_iomap_end_delalloc(
1071 struct xfs_inode *ip,
1072 loff_t offset,
1073 loff_t length,
1074 ssize_t written)
1075{
1076 struct xfs_mount *mp = ip->i_mount;
1077 xfs_fileoff_t start_fsb;
1078 xfs_fileoff_t end_fsb;
1079 int error = 0;
1080
Brian Foster9dbddd72017-02-13 22:48:17 -08001081 /* behave as if the write failed if drop writes is enabled */
1082 if (xfs_mp_drop_writes(mp))
1083 written = 0;
1084
Brian Fosterfa7f1382017-02-16 17:19:12 -08001085 /*
1086 * start_fsb refers to the first unused block after a short write. If
1087 * nothing was written, round offset down to point at the first block in
1088 * the range.
1089 */
1090 if (unlikely(!written))
1091 start_fsb = XFS_B_TO_FSBT(mp, offset);
1092 else
1093 start_fsb = XFS_B_TO_FSB(mp, offset + written);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001094 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1095
1096 /*
1097 * Trim back delalloc blocks if we didn't manage to write the whole
1098 * range reserved.
1099 *
1100 * We don't need to care about racing delalloc as we hold i_mutex
1101 * across the reserve/allocate/unreserve calls. If there are delalloc
1102 * blocks in the range, they are ours.
1103 */
1104 if (start_fsb < end_fsb) {
Brian Fosterfa7f1382017-02-16 17:19:12 -08001105 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1106 XFS_FSB_TO_B(mp, end_fsb) - 1);
1107
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001108 xfs_ilock(ip, XFS_ILOCK_EXCL);
1109 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1110 end_fsb - start_fsb);
1111 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1112
1113 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1114 xfs_alert(mp, "%s: unable to clean up ino %lld",
1115 __func__, ip->i_ino);
1116 return error;
1117 }
1118 }
1119
1120 return 0;
1121}
1122
1123static int
1124xfs_file_iomap_end(
1125 struct inode *inode,
1126 loff_t offset,
1127 loff_t length,
1128 ssize_t written,
1129 unsigned flags,
1130 struct iomap *iomap)
1131{
1132 if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
1133 return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
1134 length, written);
1135 return 0;
1136}
1137
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001138const struct iomap_ops xfs_iomap_ops = {
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001139 .iomap_begin = xfs_file_iomap_begin,
1140 .iomap_end = xfs_file_iomap_end,
1141};
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001142
1143static int
1144xfs_xattr_iomap_begin(
1145 struct inode *inode,
1146 loff_t offset,
1147 loff_t length,
1148 unsigned flags,
1149 struct iomap *iomap)
1150{
1151 struct xfs_inode *ip = XFS_I(inode);
1152 struct xfs_mount *mp = ip->i_mount;
1153 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1154 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1155 struct xfs_bmbt_irec imap;
1156 int nimaps = 1, error = 0;
1157 unsigned lockmode;
1158
1159 if (XFS_FORCED_SHUTDOWN(mp))
1160 return -EIO;
1161
1162 lockmode = xfs_ilock_data_map_shared(ip);
1163
1164 /* if there are no attribute fork or extents, return ENOENT */
1165 if (XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
1166 error = -ENOENT;
1167 goto out_unlock;
1168 }
1169
1170 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1171 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1172 &nimaps, XFS_BMAPI_ENTIRE | XFS_BMAPI_ATTRFORK);
1173out_unlock:
1174 xfs_iunlock(ip, lockmode);
1175
1176 if (!error) {
1177 ASSERT(nimaps);
1178 xfs_bmbt_to_iomap(ip, iomap, &imap);
1179 }
1180
1181 return error;
1182}
1183
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001184const struct iomap_ops xfs_xattr_iomap_ops = {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001185 .iomap_begin = xfs_xattr_iomap_begin,
1186};