blob: f286f63c430c0ec76baa39288f3a946c1a32818b [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 Chinner70a9883c2013-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;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100165 xfs_extlen_t extsz, temp;
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);
206
207 resaligned = count_fsb;
208 if (unlikely(extsz)) {
209 if ((temp = do_mod(offset_fsb, extsz)))
210 resaligned += temp;
211 if ((temp = do_mod(resaligned, extsz)))
212 resaligned += extsz - temp;
213 }
214
215 if (unlikely(rt)) {
216 resrtextents = qblocks = resaligned;
217 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000218 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
219 quota_flag = XFS_QMOPT_RES_RTBLKS;
220 } else {
221 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100222 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000223 quota_flag = XFS_QMOPT_RES_REGBLKS;
224 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /*
Brian Foster009c6e82015-10-12 15:34:20 +1100227 * Drop the shared lock acquired by the caller, attach the dquot if
228 * necessary and move on to transaction setup.
229 */
230 xfs_iunlock(ip, lockmode);
231 error = xfs_qm_dqattach(ip, 0);
232 if (error)
233 return error;
234
235 /*
Dave Chinner1ca19152015-11-03 12:37:00 +1100236 * For DAX, we do not allocate unwritten extents, but instead we zero
237 * the block before we commit the transaction. Ideally we'd like to do
238 * this outside the transaction context, but if we commit and then crash
239 * we may not have zeroed the blocks and this will be exposed on
240 * recovery of the allocation. Hence we must zero before commit.
Dave Chinner3b0fe472016-01-04 16:22:45 +1100241 *
Dave Chinner1ca19152015-11-03 12:37:00 +1100242 * Further, if we are mapping unwritten extents here, we need to zero
243 * and convert them to written so that we don't need an unwritten extent
244 * callback for DAX. This also means that we need to be able to dip into
Dave Chinner3b0fe472016-01-04 16:22:45 +1100245 * the reserve block pool for bmbt block allocation if there is no space
246 * left but we need to do unwritten extent conversion.
Dave Chinner1ca19152015-11-03 12:37:00 +1100247 */
248 if (IS_DAX(VFS_I(ip))) {
249 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
Dave Chinner3b0fe472016-01-04 16:22:45 +1100250 if (ISUNWRITTEN(imap)) {
Christoph Hellwig253f4912016-04-06 09:19:55 +1000251 tflags |= XFS_TRANS_RESERVE;
Dave Chinner3b0fe472016-01-04 16:22:45 +1100252 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
253 }
Dave Chinner1ca19152015-11-03 12:37:00 +1100254 }
Christoph Hellwig253f4912016-04-06 09:19:55 +1000255 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
256 tflags, &tp);
257 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000258 return error;
Dave Chinner507630b2012-03-27 10:34:50 -0400259
Brian Foster009c6e82015-10-12 15:34:20 +1100260 lockmode = XFS_ILOCK_EXCL;
261 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Christoph Hellwig7d095252009-06-08 15:33:32 +0200263 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100264 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400265 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Christoph Hellwigddc34152011-09-19 15:00:54 +0000267 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000270 * From this point onwards we overwrite the imap pointer that the
271 * caller gave to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000273 xfs_defer_init(&dfops, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000274 nimaps = 1;
Christoph Hellwigd531d912014-02-10 10:27:43 +1100275 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Dave Chinner264e89a2015-11-03 13:28:41 +1100276 bmapi_flags, &firstfsb, resblks, imap,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000277 &nimaps, &dfops);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000278 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400279 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000282 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000284 error = xfs_defer_finish(&tp, &dfops, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000285 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400286 goto out_bmap_cancel;
Dave Chinner1ca19152015-11-03 12:37:00 +1100287
Christoph Hellwig70393312015-06-04 13:48:08 +1000288 error = xfs_trans_commit(tp);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000289 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400290 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Nathan Scott06d10dd2005-06-21 15:48:47 +1000292 /*
293 * Copy any maps to caller's array and return any error.
294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (nimaps == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000296 error = -ENOSPC;
Dave Chinner507630b2012-03-27 10:34:50 -0400297 goto out_unlock;
Nathan Scott572d95f2006-09-28 11:03:20 +1000298 }
299
Dave Chinner507630b2012-03-27 10:34:50 -0400300 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100301 error = xfs_alert_fsblock_zero(ip, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Dave Chinner507630b2012-03-27 10:34:50 -0400303out_unlock:
Brian Foster009c6e82015-10-12 15:34:20 +1100304 xfs_iunlock(ip, lockmode);
Dave Chinner507630b2012-03-27 10:34:50 -0400305 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Dave Chinner507630b2012-03-27 10:34:50 -0400307out_bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000308 xfs_defer_cancel(&dfops);
Dave Chinnerea562ed2012-05-08 20:48:53 +1000309 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
Dave Chinner507630b2012-03-27 10:34:50 -0400310out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000311 xfs_trans_cancel(tp);
Dave Chinner507630b2012-03-27 10:34:50 -0400312 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Brian Foster76a42022013-03-18 10:51:47 -0400315STATIC bool
316xfs_quota_need_throttle(
317 struct xfs_inode *ip,
318 int type,
319 xfs_fsblock_t alloc_blocks)
320{
321 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
322
323 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
324 return false;
325
326 /* no hi watermark, no throttle */
327 if (!dq->q_prealloc_hi_wmark)
328 return false;
329
330 /* under the lo watermark, no throttle */
331 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
332 return false;
333
334 return true;
335}
336
337STATIC void
338xfs_quota_calc_throttle(
339 struct xfs_inode *ip,
340 int type,
341 xfs_fsblock_t *qblocks,
Brian Fosterf0740512014-07-24 19:56:08 +1000342 int *qshift,
343 int64_t *qfreesp)
Brian Foster76a42022013-03-18 10:51:47 -0400344{
345 int64_t freesp;
346 int shift = 0;
347 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
348
Eric Sandeen5cca3f62014-10-02 09:27:09 +1000349 /* no dq, or over hi wmark, squash the prealloc completely */
350 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
Brian Foster76a42022013-03-18 10:51:47 -0400351 *qblocks = 0;
Brian Fosterf0740512014-07-24 19:56:08 +1000352 *qfreesp = 0;
Brian Foster76a42022013-03-18 10:51:47 -0400353 return;
354 }
355
356 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
357 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
358 shift = 2;
359 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
360 shift += 2;
361 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
362 shift += 2;
363 }
364
Brian Fosterf0740512014-07-24 19:56:08 +1000365 if (freesp < *qfreesp)
366 *qfreesp = freesp;
367
Brian Foster76a42022013-03-18 10:51:47 -0400368 /* only overwrite the throttle values if we are more aggressive */
369 if ((freesp >> shift) < (*qblocks >> *qshift)) {
370 *qblocks = freesp;
371 *qshift = shift;
372 }
373}
374
Dave Chinnera1e16c22013-02-11 16:05:01 +1100375/*
Christoph Hellwig51446f52016-09-19 11:10:21 +1000376 * If we are doing a write at the end of the file and there are no allocations
377 * past this one, then extend the allocation out to the file system's write
378 * iosize.
379 *
Dave Chinner055388a2011-01-04 11:35:03 +1100380 * If we don't have a user specified preallocation size, dynamically increase
Christoph Hellwig51446f52016-09-19 11:10:21 +1000381 * the preallocation size as the size of the file grows. Cap the maximum size
Dave Chinner055388a2011-01-04 11:35:03 +1100382 * at a single extent or less if the filesystem is near full. The closer the
383 * filesystem is to full, the smaller the maximum prealocation.
Christoph Hellwig51446f52016-09-19 11:10:21 +1000384 *
385 * As an exception we don't do any preallocation at all if the file is smaller
386 * than the minimum preallocation and we are using the default dynamic
387 * preallocation scheme, as it is likely this is the only write to the file that
388 * is going to be done.
389 *
390 * We clean up any extra space left over when the file is closed in
391 * xfs_inactive().
Dave Chinner055388a2011-01-04 11:35:03 +1100392 */
393STATIC xfs_fsblock_t
394xfs_iomap_prealloc_size(
Dave Chinnera1e16c22013-02-11 16:05:01 +1100395 struct xfs_inode *ip,
Christoph Hellwig51446f52016-09-19 11:10:21 +1000396 loff_t offset,
397 loff_t count,
Christoph Hellwig553937d2017-01-09 16:38:46 +0100398 xfs_extnum_t idx)
Dave Chinner055388a2011-01-04 11:35:03 +1100399{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000400 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig553937d2017-01-09 16:38:46 +0100401 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000402 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig553937d2017-01-09 16:38:46 +0100403 struct xfs_bmbt_irec prev;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400404 int shift = 0;
405 int64_t freesp;
Brian Foster76a42022013-03-18 10:51:47 -0400406 xfs_fsblock_t qblocks;
407 int qshift = 0;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000408 xfs_fsblock_t alloc_blocks = 0;
Dave Chinner055388a2011-01-04 11:35:03 +1100409
Christoph Hellwig51446f52016-09-19 11:10:21 +1000410 if (offset + count <= XFS_ISIZE(ip))
411 return 0;
412
413 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
414 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
415 return 0;
416
417 /*
418 * If an explicit allocsize is set, the file is small, or we
419 * are writing behind a hole, then use the minimum prealloc:
420 */
421 if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
422 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
Christoph Hellwig553937d2017-01-09 16:38:46 +0100423 !xfs_iext_get_extent(ifp, idx - 1, &prev) ||
424 prev.br_startoff + prev.br_blockcount < offset_fsb)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000425 return mp->m_writeio_blocks;
426
427 /*
428 * Determine the initial size of the preallocation. We are beyond the
429 * current EOF here, but we need to take into account whether this is
430 * a sparse write or an extending write when determining the
431 * preallocation size. Hence we need to look up the extent that ends
432 * at the current write offset and use the result to determine the
433 * preallocation size.
434 *
435 * If the extent is a hole, then preallocation is essentially disabled.
436 * Otherwise we take the size of the preceding data extent as the basis
437 * for the preallocation size. If the size of the extent is greater than
438 * half the maximum extent length, then use the current offset as the
439 * basis. This ensures that for large files the preallocation size
440 * always extends to MAXEXTLEN rather than falling short due to things
441 * like stripe unit/width alignment of real extents.
442 */
Christoph Hellwig553937d2017-01-09 16:38:46 +0100443 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
444 alloc_blocks = prev.br_blockcount << 1;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000445 else
446 alloc_blocks = XFS_B_TO_FSB(mp, offset);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400447 if (!alloc_blocks)
448 goto check_writeio;
Brian Foster76a42022013-03-18 10:51:47 -0400449 qblocks = alloc_blocks;
Dave Chinner055388a2011-01-04 11:35:03 +1100450
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400451 /*
452 * MAXEXTLEN is not a power of two value but we round the prealloc down
453 * to the nearest power of two value after throttling. To prevent the
454 * round down from unconditionally reducing the maximum supported prealloc
455 * size, we round up first, apply appropriate throttling, round down and
456 * cap the value to MAXEXTLEN.
457 */
458 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
459 alloc_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100460
Dave Chinner0d485ad2015-02-23 21:22:03 +1100461 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400462 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
463 shift = 2;
464 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
465 shift++;
466 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
467 shift++;
468 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
469 shift++;
470 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
471 shift++;
Dave Chinner055388a2011-01-04 11:35:03 +1100472 }
Brian Foster76a42022013-03-18 10:51:47 -0400473
474 /*
Brian Fosterf0740512014-07-24 19:56:08 +1000475 * Check each quota to cap the prealloc size, provide a shift value to
476 * throttle with and adjust amount of available space.
Brian Foster76a42022013-03-18 10:51:47 -0400477 */
478 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000479 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
480 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400481 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000482 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
483 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400484 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
Brian Fosterf0740512014-07-24 19:56:08 +1000485 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
486 &freesp);
Brian Foster76a42022013-03-18 10:51:47 -0400487
488 /*
489 * The final prealloc size is set to the minimum of free space available
490 * in each of the quotas and the overall filesystem.
491 *
492 * The shift throttle value is set to the maximum value as determined by
493 * the global low free space values and per-quota low free space values.
494 */
495 alloc_blocks = MIN(alloc_blocks, qblocks);
496 shift = MAX(shift, qshift);
497
Brian Foster3c58b5f2013-03-18 10:51:43 -0400498 if (shift)
499 alloc_blocks >>= shift;
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400500 /*
501 * rounddown_pow_of_two() returns an undefined result if we pass in
502 * alloc_blocks = 0.
503 */
504 if (alloc_blocks)
505 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
506 if (alloc_blocks > MAXEXTLEN)
507 alloc_blocks = MAXEXTLEN;
Dave Chinner055388a2011-01-04 11:35:03 +1100508
Brian Foster3c58b5f2013-03-18 10:51:43 -0400509 /*
510 * If we are still trying to allocate more space than is
511 * available, squash the prealloc hard. This can happen if we
512 * have a large file on a small filesystem and the above
513 * lowspace thresholds are smaller than MAXEXTLEN.
514 */
515 while (alloc_blocks && alloc_blocks >= freesp)
516 alloc_blocks >>= 4;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400517check_writeio:
Dave Chinner055388a2011-01-04 11:35:03 +1100518 if (alloc_blocks < mp->m_writeio_blocks)
519 alloc_blocks = mp->m_writeio_blocks;
Brian Foster19cb7e32013-03-18 10:51:48 -0400520 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
521 mp->m_writeio_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100522 return alloc_blocks;
523}
524
Christoph Hellwig51446f52016-09-19 11:10:21 +1000525static int
526xfs_file_iomap_begin_delay(
527 struct inode *inode,
528 loff_t offset,
529 loff_t count,
530 unsigned flags,
531 struct iomap *iomap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Christoph Hellwig51446f52016-09-19 11:10:21 +1000533 struct xfs_inode *ip = XFS_I(inode);
534 struct xfs_mount *mp = ip->i_mount;
535 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
536 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
537 xfs_fileoff_t maxbytes_fsb =
538 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Brian Foster06ac11d2017-01-09 16:38:47 +0100539 xfs_fileoff_t end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000540 int error = 0, eof = 0;
541 struct xfs_bmbt_irec got;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000542 xfs_extnum_t idx;
Brian Foster06ac11d2017-01-09 16:38:47 +0100543 xfs_fsblock_t prealloc_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Christoph Hellwig51446f52016-09-19 11:10:21 +1000545 ASSERT(!XFS_IS_REALTIME_INODE(ip));
546 ASSERT(!xfs_get_extsz_hint(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Christoph Hellwig51446f52016-09-19 11:10:21 +1000548 xfs_ilock(ip, XFS_ILOCK_EXCL);
549
550 if (unlikely(XFS_TEST_ERROR(
551 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
552 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
553 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
554 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
555 error = -EFSCORRUPTED;
556 goto out_unlock;
557 }
558
559 XFS_STATS_INC(mp, xs_blk_mapw);
560
561 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
562 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
563 if (error)
564 goto out_unlock;
565 }
566
Christoph Hellwig553937d2017-01-09 16:38:46 +0100567 eof = !xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000568 if (!eof && got.br_startoff <= offset_fsb) {
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100569 if (xfs_is_reflink_inode(ip)) {
570 bool shared;
571
572 end_fsb = min(XFS_B_TO_FSB(mp, offset + count),
573 maxbytes_fsb);
574 xfs_trim_extent(&got, offset_fsb, end_fsb - offset_fsb);
575 error = xfs_reflink_reserve_cow(ip, &got, &shared);
576 if (error)
577 goto out_unlock;
578 }
579
Christoph Hellwig51446f52016-09-19 11:10:21 +1000580 trace_xfs_iomap_found(ip, offset, count, 0, &got);
581 goto done;
582 }
583
Christoph Hellwig7d095252009-06-08 15:33:32 +0200584 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (error)
Christoph Hellwig51446f52016-09-19 11:10:21 +1000586 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Christoph Hellwig51446f52016-09-19 11:10:21 +1000588 /*
589 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES pages
590 * to keep the chunks of work done where somewhat symmetric with the
591 * work writeback does. This is a completely arbitrary number pulled
592 * out of thin air as a best guess for initial testing.
593 *
594 * Note that the values needs to be less than 32-bits wide until
595 * the lower level functions are updated.
596 */
597 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
Brian Foster06ac11d2017-01-09 16:38:47 +0100598 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100599
Christoph Hellwig51446f52016-09-19 11:10:21 +1000600 if (eof) {
Christoph Hellwig553937d2017-01-09 16:38:46 +0100601 prealloc_blocks = xfs_iomap_prealloc_size(ip, offset, count, idx);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000602 if (prealloc_blocks) {
603 xfs_extlen_t align;
604 xfs_off_t end_offset;
Brian Foster06ac11d2017-01-09 16:38:47 +0100605 xfs_fileoff_t p_end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000606
607 end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
Brian Foster06ac11d2017-01-09 16:38:47 +0100608 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
609 prealloc_blocks;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000610
611 align = xfs_eof_alignment(ip, 0);
612 if (align)
Brian Foster06ac11d2017-01-09 16:38:47 +0100613 p_end_fsb = roundup_64(p_end_fsb, align);
Christoph Hellwig51446f52016-09-19 11:10:21 +1000614
Brian Foster06ac11d2017-01-09 16:38:47 +0100615 p_end_fsb = min(p_end_fsb, maxbytes_fsb);
616 ASSERT(p_end_fsb > offset_fsb);
617 prealloc_blocks = p_end_fsb - end_fsb;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000618 }
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dave Chinner8de2bf92009-04-06 18:49:12 +0200621retry:
Darrick J. Wongbe51f812016-10-03 09:11:32 -0700622 error = xfs_bmapi_reserve_delalloc(ip, XFS_DATA_FORK, offset_fsb,
Brian Foster06ac11d2017-01-09 16:38:47 +0100623 end_fsb - offset_fsb, prealloc_blocks, &got, &idx, eof);
Dave Chinner055388a2011-01-04 11:35:03 +1100624 switch (error) {
625 case 0:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000626 break;
Dave Chinner24513372014-06-25 14:58:08 +1000627 case -ENOSPC:
628 case -EDQUOT:
Christoph Hellwig51446f52016-09-19 11:10:21 +1000629 /* retry without any preallocation */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000630 trace_xfs_delalloc_enospc(ip, offset, count);
Brian Foster06ac11d2017-01-09 16:38:47 +0100631 if (prealloc_blocks) {
632 prealloc_blocks = 0;
Dave Chinner9aa05002012-10-08 21:56:04 +1100633 goto retry;
Dave Chinner055388a2011-01-04 11:35:03 +1100634 }
Christoph Hellwig51446f52016-09-19 11:10:21 +1000635 /*FALLTHRU*/
636 default:
637 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Brian Fosterda617af2017-03-08 09:58:08 -0800640 /*
641 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
642 * them out if the write happens to fail.
643 */
644 iomap->flags = IOMAP_F_NEW;
Christoph Hellwig51446f52016-09-19 11:10:21 +1000645 trace_xfs_iomap_alloc(ip, offset, count, 0, &got);
646done:
647 if (isnullstartblock(got.br_startblock))
648 got.br_startblock = DELAYSTARTBLOCK;
649
650 if (!got.br_startblock) {
651 error = xfs_alert_fsblock_zero(ip, &got);
652 if (error)
653 goto out_unlock;
654 }
655
656 xfs_bmbt_to_iomap(ip, iomap, &got);
657
658out_unlock:
659 xfs_iunlock(ip, XFS_ILOCK_EXCL);
660 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663/*
664 * Pass in a delayed allocate extent, convert it to real extents;
665 * return to the caller the extent we create which maps on top of
666 * the originating callers request.
667 *
668 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100669 *
670 * We no longer bother to look at the incoming map - all we have to
671 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
Christoph Hellwiga206c812010-12-10 08:42:20 +0000673int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674xfs_iomap_write_allocate(
675 xfs_inode_t *ip,
Darrick J. Wong60b49842016-10-03 09:11:34 -0700676 int whichfork,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700677 xfs_off_t offset,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000678 xfs_bmbt_irec_t *imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 xfs_fileoff_t offset_fsb, last_block;
682 xfs_fileoff_t end_fsb, map_start_fsb;
683 xfs_fsblock_t first_block;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000684 struct xfs_defer_ops dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 xfs_filblks_t count_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 xfs_trans_t *tp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100687 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 int error = 0;
Christoph Hellwig214d55e2017-02-02 08:56:06 +0100689 int flags = XFS_BMAPI_DELALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int nres;
691
Darrick J. Wong60b49842016-10-03 09:11:34 -0700692 if (whichfork == XFS_COW_FORK)
Darrick J. Wonge02f0ff2017-02-02 15:14:02 -0800693 flags |= XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * Make sure that the dquots are there.
697 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200698 error = xfs_qm_dqattach(ip, 0);
699 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000700 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Nathan Scott24e17b52005-05-05 13:33:20 -0700702 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig30704512010-06-24 11:42:19 +1000703 count_fsb = imap->br_blockcount;
704 map_start_fsb = imap->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100706 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 while (count_fsb != 0) {
709 /*
710 * Set up a transaction with which to allocate the
711 * backing store for the file. Do allocations in a
712 * loop until we get some space in the range we are
713 * interested in. The other space that might be allocated
714 * is in the delayed allocation extent on which we sit
715 * but before our buffer starts.
716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 nimaps = 0;
718 while (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
Christoph Hellwig0af32fb2016-08-17 08:30:28 +1000720 /*
721 * We have already reserved space for the extent and any
722 * indirect blocks when creating the delalloc extent,
723 * there is no need to reserve space in this transaction
724 * again.
725 */
726 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0,
Christoph Hellwig253f4912016-04-06 09:19:55 +1000727 0, XFS_TRANS_RESERVE, &tp);
728 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000729 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000732 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000734 xfs_defer_init(&dfops, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /*
David Chinnere4143a12007-11-23 16:29:11 +1100737 * it is possible that the extents have changed since
738 * we did the read call as we dropped the ilock for a
739 * while. We have to be careful about truncates or hole
740 * punchs here - we are not allowed to allocate
741 * non-delalloc blocks here.
742 *
743 * The only protection against truncation is the pages
744 * for the range we are being asked to convert are
745 * locked and hence a truncate will block on them
746 * first.
747 *
748 * As a result, if we go beyond the range we really
749 * need and hit an delalloc extent boundary followed by
750 * a hole while we have excess blocks in the map, we
751 * will fill the hole incorrectly and overrun the
752 * transaction reservation.
753 *
754 * Using a single map prevents this as we are forced to
755 * check each map we look for overlap with the desired
756 * range and abort as soon as we find it. Also, given
757 * that we only return a single map, having one beyond
758 * what we can return is probably a bit silly.
759 *
760 * We also need to check that we don't go beyond EOF;
761 * this is a truncate optimisation as a truncate sets
762 * the new file size before block on the pages we
763 * currently have locked under writeback. Because they
764 * are about to be tossed, we don't need to write them
765 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
David Chinnere4143a12007-11-23 16:29:11 +1100767 nimaps = 1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000768 end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
Eric Sandeen7fb2cd42014-04-14 18:58:05 +1000769 error = xfs_bmap_last_offset(ip, &last_block,
David Chinner7c9ef852008-04-10 12:21:59 +1000770 XFS_DATA_FORK);
771 if (error)
772 goto trans_cancel;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
775 if ((map_start_fsb + count_fsb) > last_block) {
776 count_fsb = last_block - map_start_fsb;
777 if (count_fsb == 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000778 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto trans_cancel;
780 }
781 }
782
Christoph Hellwig30704512010-06-24 11:42:19 +1000783 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000784 * From this point onwards we overwrite the imap
785 * pointer that the caller gave to us.
786 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000787 error = xfs_bmapi_write(tp, ip, map_start_fsb,
Darrick J. Wong60b49842016-10-03 09:11:34 -0700788 count_fsb, flags, &first_block,
Brian Fosterdbd5c8c2015-10-12 16:04:13 +1100789 nres, imap, &nimaps,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000790 &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (error)
792 goto trans_cancel;
793
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000794 error = xfs_defer_finish(&tp, &dfops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (error)
796 goto trans_cancel;
797
Christoph Hellwig70393312015-06-04 13:48:08 +1000798 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (error)
800 goto error0;
801
802 xfs_iunlock(ip, XFS_ILOCK_EXCL);
803 }
804
805 /*
806 * See if we were able to allocate an extent that
807 * covers at least part of the callers request
808 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000809 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100810 return xfs_alert_fsblock_zero(ip, imap);
David Chinner86c4d622008-04-29 12:53:21 +1000811
Christoph Hellwig30704512010-06-24 11:42:19 +1000812 if ((offset_fsb >= imap->br_startoff) &&
813 (offset_fsb < (imap->br_startoff +
814 imap->br_blockcount))) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100815 XFS_STATS_INC(mp, xs_xstrat_quick);
David Chinnere4143a12007-11-23 16:29:11 +1100816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
David Chinnere4143a12007-11-23 16:29:11 +1100819 /*
820 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * file, just surrounding data, try again.
822 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000823 count_fsb -= imap->br_blockcount;
824 map_start_fsb = imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
827trans_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000828 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000829 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830error0:
831 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000832 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835int
836xfs_iomap_write_unwritten(
837 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700838 xfs_off_t offset,
Eryu Guan0eebfed2017-09-21 11:26:18 -0700839 xfs_off_t count,
840 bool update_isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 xfs_fileoff_t offset_fsb;
844 xfs_filblks_t count_fsb;
845 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100846 xfs_fsblock_t firstfsb;
847 int nimaps;
848 xfs_trans_t *tp;
849 xfs_bmbt_irec_t imap;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000850 struct xfs_defer_ops dfops;
Eryu Guan0eebfed2017-09-21 11:26:18 -0700851 struct inode *inode = VFS_I(ip);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000852 xfs_fsize_t i_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100853 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000856 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 offset_fsb = XFS_B_TO_FSBT(mp, offset);
859 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
860 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
861
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000862 /*
863 * Reserve enough blocks in this transaction for two complete extent
864 * btree splits. We may be converting the middle part of an unwritten
865 * extent and in this case we will insert two new extents in the btree
866 * each of which could cause a full split.
867 *
868 * This reservation amount will be used in the first call to
869 * xfs_bmbt_split() to select an AG with enough space to satisfy the
870 * rest of the operation.
871 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100872 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Nathan Scottdd9f4382006-01-11 15:28:28 +1100874 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /*
Christoph Hellwig253f4912016-04-06 09:19:55 +1000876 * Set up a transaction to convert the range of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 * from unwritten to real. Do allocations in a loop until
878 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000879 *
Christoph Hellwig253f4912016-04-06 09:19:55 +1000880 * Note that we can't risk to recursing back into the filesystem
881 * here as we might be asked to write out the same inode that we
882 * complete here and might deadlock on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000884 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
885 XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
886 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000887 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000890 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 /*
893 * Modify the unwritten extent state of the buffer.
894 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000895 xfs_defer_init(&dfops, &firstfsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 nimaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000897 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
Brian Fosterdbd5c8c2015-10-12 16:04:13 +1100898 XFS_BMAPI_CONVERT, &firstfsb, resblks,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000899 &imap, &nimaps, &dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (error)
901 goto error_on_bmapi_transaction;
902
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000903 /*
904 * Log the updated inode size as we go. We have to be careful
905 * to only log it up to the actual write offset if it is
906 * halfway into a block.
907 */
908 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
909 if (i_size > offset + count)
910 i_size = offset + count;
Eryu Guan0eebfed2017-09-21 11:26:18 -0700911 if (update_isize && i_size > i_size_read(inode))
912 i_size_write(inode, i_size);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000913 i_size = xfs_new_eof(ip, i_size);
914 if (i_size) {
915 ip->i_d.di_size = i_size;
916 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
917 }
918
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000919 error = xfs_defer_finish(&tp, &dfops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (error)
921 goto error_on_bmapi_transaction;
922
Christoph Hellwig70393312015-06-04 13:48:08 +1000923 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 xfs_iunlock(ip, XFS_ILOCK_EXCL);
925 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000926 return error;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100927
David Chinner86c4d622008-04-29 12:53:21 +1000928 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100929 return xfs_alert_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if ((numblks_fsb = imap.br_blockcount) == 0) {
932 /*
933 * The numblks_fsb value should always get
934 * smaller, otherwise the loop is stuck.
935 */
936 ASSERT(imap.br_blockcount);
937 break;
938 }
939 offset_fsb += numblks_fsb;
940 count_fsb -= numblks_fsb;
941 } while (count_fsb > 0);
942
943 return 0;
944
945error_on_bmapi_transaction:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000946 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000947 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000949 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
Christoph Hellwig3b3dce02016-06-21 09:52:47 +1000951
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000952static inline bool imap_needs_alloc(struct inode *inode,
953 struct xfs_bmbt_irec *imap, int nimaps)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000954{
955 return !nimaps ||
956 imap->br_startblock == HOLESTARTBLOCK ||
Christoph Hellwig6c31f492016-09-19 11:28:38 +1000957 imap->br_startblock == DELAYSTARTBLOCK ||
958 (IS_DAX(inode) && ISUNWRITTEN(imap));
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000959}
960
961static int
962xfs_file_iomap_begin(
963 struct inode *inode,
964 loff_t offset,
965 loff_t length,
966 unsigned flags,
967 struct iomap *iomap)
968{
969 struct xfs_inode *ip = XFS_I(inode);
970 struct xfs_mount *mp = ip->i_mount;
971 struct xfs_bmbt_irec imap;
972 xfs_fileoff_t offset_fsb, end_fsb;
973 int nimaps = 1, error = 0;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100974 bool shared = false, trimmed = false;
Christoph Hellwig66642c52016-09-19 11:26:39 +1000975 unsigned lockmode;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000976
977 if (XFS_FORCED_SHUTDOWN(mp))
978 return -EIO;
979
Darrick J. Wong2a067052016-10-03 09:11:33 -0700980 if ((flags & IOMAP_WRITE) && !IS_DAX(inode) &&
981 !xfs_get_extsz_hint(ip)) {
982 /* Reserve delalloc blocks for regular writeback. */
Christoph Hellwig51446f52016-09-19 11:10:21 +1000983 return xfs_file_iomap_begin_delay(inode, offset, length, flags,
984 iomap);
985 }
986
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100987 /*
988 * COW writes will allocate delalloc space, so we need to make sure
989 * to take the lock exclusively here.
990 */
991 if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && xfs_is_reflink_inode(ip)) {
992 lockmode = XFS_ILOCK_EXCL;
993 xfs_ilock(ip, XFS_ILOCK_EXCL);
994 } else {
995 lockmode = xfs_ilock_data_map_shared(ip);
996 }
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +1000997
998 ASSERT(offset <= mp->m_super->s_maxbytes);
999 if ((xfs_fsize_t)offset + length > mp->m_super->s_maxbytes)
1000 length = mp->m_super->s_maxbytes - offset;
1001 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1002 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1003
1004 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001005 &nimaps, 0);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001006 if (error)
1007 goto out_unlock;
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001008
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001009 if (flags & IOMAP_REPORT) {
Christoph Hellwig5f9268c2016-10-20 15:53:32 +11001010 /* Trim the mapping to the nearest shared extent boundary. */
1011 error = xfs_reflink_trim_around_shared(ip, &imap, &shared,
1012 &trimmed);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001013 if (error)
1014 goto out_unlock;
1015 }
1016
1017 if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && xfs_is_reflink_inode(ip)) {
1018 error = xfs_reflink_reserve_cow(ip, &imap, &shared);
1019 if (error)
1020 goto out_unlock;
1021
1022 end_fsb = imap.br_startoff + imap.br_blockcount;
1023 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001024 }
1025
Christoph Hellwig6c31f492016-09-19 11:28:38 +10001026 if ((flags & IOMAP_WRITE) && imap_needs_alloc(inode, &imap, nimaps)) {
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001027 /*
1028 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
1029 * pages to keep the chunks of work done where somewhat symmetric
1030 * with the work writeback does. This is a completely arbitrary
1031 * number pulled out of thin air as a best guess for initial
1032 * testing.
1033 *
1034 * Note that the values needs to be less than 32-bits wide until
1035 * the lower level functions are updated.
1036 */
1037 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
Christoph Hellwig51446f52016-09-19 11:10:21 +10001038 /*
1039 * xfs_iomap_write_direct() expects the shared lock. It
1040 * is unlocked on return.
1041 */
Christoph Hellwig66642c52016-09-19 11:26:39 +10001042 if (lockmode == XFS_ILOCK_EXCL)
1043 xfs_ilock_demote(ip, lockmode);
Christoph Hellwig51446f52016-09-19 11:10:21 +10001044 error = xfs_iomap_write_direct(ip, offset, length, &imap,
1045 nimaps);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001046 if (error)
1047 return error;
1048
Christoph Hellwigecd50722016-09-19 11:24:37 +10001049 iomap->flags = IOMAP_F_NEW;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001050 trace_xfs_iomap_alloc(ip, offset, length, 0, &imap);
Christoph Hellwigb95a2122016-08-17 08:44:52 +10001051 } else {
1052 ASSERT(nimaps);
1053
Christoph Hellwig66642c52016-09-19 11:26:39 +10001054 xfs_iunlock(ip, lockmode);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001055 trace_xfs_iomap_found(ip, offset, length, 0, &imap);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001056 }
1057
Christoph Hellwigb95a2122016-08-17 08:44:52 +10001058 xfs_bmbt_to_iomap(ip, iomap, &imap);
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001059 if (shared)
1060 iomap->flags |= IOMAP_F_SHARED;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001061 return 0;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +11001062out_unlock:
1063 xfs_iunlock(ip, lockmode);
1064 return error;
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001065}
1066
1067static int
1068xfs_file_iomap_end_delalloc(
1069 struct xfs_inode *ip,
1070 loff_t offset,
1071 loff_t length,
Brian Fosterda617af2017-03-08 09:58:08 -08001072 ssize_t written,
1073 struct iomap *iomap)
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001074{
1075 struct xfs_mount *mp = ip->i_mount;
1076 xfs_fileoff_t start_fsb;
1077 xfs_fileoff_t end_fsb;
1078 int error = 0;
1079
Brian Fosterd0040062017-02-16 17:19:12 -08001080 /*
1081 * start_fsb refers to the first unused block after a short write. If
1082 * nothing was written, round offset down to point at the first block in
1083 * the range.
1084 */
1085 if (unlikely(!written))
1086 start_fsb = XFS_B_TO_FSBT(mp, offset);
1087 else
1088 start_fsb = XFS_B_TO_FSB(mp, offset + written);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001089 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1090
1091 /*
Brian Fosterda617af2017-03-08 09:58:08 -08001092 * Trim delalloc blocks if they were allocated by this write and we
1093 * didn't manage to write the whole range.
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001094 *
1095 * We don't need to care about racing delalloc as we hold i_mutex
1096 * across the reserve/allocate/unreserve calls. If there are delalloc
1097 * blocks in the range, they are ours.
1098 */
Brian Fosterda617af2017-03-08 09:58:08 -08001099 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
Brian Fosterd0040062017-02-16 17:19:12 -08001100 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1101 XFS_FSB_TO_B(mp, end_fsb) - 1);
1102
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001103 xfs_ilock(ip, XFS_ILOCK_EXCL);
1104 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1105 end_fsb - start_fsb);
1106 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1107
1108 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1109 xfs_alert(mp, "%s: unable to clean up ino %lld",
1110 __func__, ip->i_ino);
1111 return error;
1112 }
1113 }
1114
1115 return 0;
1116}
1117
1118static int
1119xfs_file_iomap_end(
1120 struct inode *inode,
1121 loff_t offset,
1122 loff_t length,
1123 ssize_t written,
1124 unsigned flags,
1125 struct iomap *iomap)
1126{
1127 if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
1128 return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
Brian Fosterda617af2017-03-08 09:58:08 -08001129 length, written, iomap);
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +10001130 return 0;
1131}
1132
1133struct iomap_ops xfs_iomap_ops = {
1134 .iomap_begin = xfs_file_iomap_begin,
1135 .iomap_end = xfs_file_iomap_end,
1136};
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001137
1138static int
1139xfs_xattr_iomap_begin(
1140 struct inode *inode,
1141 loff_t offset,
1142 loff_t length,
1143 unsigned flags,
1144 struct iomap *iomap)
1145{
1146 struct xfs_inode *ip = XFS_I(inode);
1147 struct xfs_mount *mp = ip->i_mount;
1148 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1149 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1150 struct xfs_bmbt_irec imap;
1151 int nimaps = 1, error = 0;
1152 unsigned lockmode;
1153
1154 if (XFS_FORCED_SHUTDOWN(mp))
1155 return -EIO;
1156
Darrick J. Wong4e8163f2017-04-06 16:00:39 -07001157 lockmode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001158
1159 /* if there are no attribute fork or extents, return ENOENT */
Darrick J. Wong4e8163f2017-04-06 16:00:39 -07001160 if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001161 error = -ENOENT;
1162 goto out_unlock;
1163 }
1164
1165 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1166 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1167 &nimaps, XFS_BMAPI_ENTIRE | XFS_BMAPI_ATTRFORK);
1168out_unlock:
1169 xfs_iunlock(ip, lockmode);
1170
1171 if (!error) {
1172 ASSERT(nimaps);
1173 xfs_bmbt_to_iomap(ip, iomap, &imap);
1174 }
1175
1176 return error;
1177}
1178
1179struct iomap_ops xfs_xattr_iomap_ops = {
1180 .iomap_begin = xfs_xattr_iomap_begin,
1181};