]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xfs/xfs_iomap.c
xfs: support for synchronous DAX faults
[mirror_ubuntu-bionic-kernel.git] / fs / xfs / xfs_iomap.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
51446f5b 3 * Copyright (c) 2016 Christoph Hellwig.
7b718769 4 * All Rights Reserved.
1da177e4 5 *
7b718769
NS
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
1da177e4
LT
8 * published by the Free Software Foundation.
9 *
7b718769
NS
10 * 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.
1da177e4 14 *
7b718769
NS
15 * 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
1da177e4 18 */
3b3dce05 19#include <linux/iomap.h>
1da177e4 20#include "xfs.h"
1da177e4 21#include "xfs_fs.h"
70a9883c 22#include "xfs_shared.h"
239880ef
DC
23#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
1da177e4 26#include "xfs_mount.h"
3ab78df2 27#include "xfs_defer.h"
1da177e4 28#include "xfs_inode.h"
a844f451 29#include "xfs_btree.h"
a4fbe6ab 30#include "xfs_bmap_btree.h"
1da177e4 31#include "xfs_bmap.h"
68988114 32#include "xfs_bmap_util.h"
1da177e4 33#include "xfs_error.h"
a4fbe6ab 34#include "xfs_trans.h"
1da177e4 35#include "xfs_trans_space.h"
a39e596b 36#include "xfs_inode_item.h"
1da177e4 37#include "xfs_iomap.h"
0b1b213f 38#include "xfs_trace.h"
27b52867 39#include "xfs_icache.h"
a4fbe6ab 40#include "xfs_quota.h"
76a4202a
BF
41#include "xfs_dquot_item.h"
42#include "xfs_dquot.h"
2a06705c 43#include "xfs_reflink.h"
1da177e4 44
1da177e4
LT
45
46#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
47 << mp->m_writeio_log)
1da177e4 48
e9c49736
CH
49void
50xfs_bmbt_to_iomap(
51 struct xfs_inode *ip,
52 struct iomap *iomap,
53 struct xfs_bmbt_irec *imap)
54{
55 struct xfs_mount *mp = ip->i_mount;
56
57 if (imap->br_startblock == HOLESTARTBLOCK) {
58 iomap->blkno = IOMAP_NULL_BLOCK;
59 iomap->type = IOMAP_HOLE;
60 } else if (imap->br_startblock == DELAYSTARTBLOCK) {
61 iomap->blkno = IOMAP_NULL_BLOCK;
62 iomap->type = IOMAP_DELALLOC;
63 } else {
64 iomap->blkno = xfs_fsb_to_db(ip, imap->br_startblock);
65 if (imap->br_state == XFS_EXT_UNWRITTEN)
66 iomap->type = IOMAP_UNWRITTEN;
67 else
68 iomap->type = IOMAP_MAPPED;
69 }
70 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
71 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
72 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
486aff5e 73 iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
e9c49736
CH
74}
75
f7ca3522 76xfs_extlen_t
f8e3a825
CH
77xfs_eof_alignment(
78 struct xfs_inode *ip,
79 xfs_extlen_t extsize)
dd9f438e 80{
f8e3a825
CH
81 struct xfs_mount *mp = ip->i_mount;
82 xfs_extlen_t align = 0;
dd9f438e 83
bf322d98
CH
84 if (!XFS_IS_REALTIME_INODE(ip)) {
85 /*
86 * Round up the allocation request to a stripe unit
87 * (m_dalign) boundary if the file size is >= stripe unit
88 * size, and we are allocating past the allocation eof.
89 *
90 * If mounted with the "-o swalloc" option the alignment is
91 * increased from the strip unit size to the stripe width.
92 */
93 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
94 align = mp->m_swidth;
95 else if (mp->m_dalign)
96 align = mp->m_dalign;
97
76b57302
PW
98 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
99 align = 0;
bf322d98 100 }
dd9f438e
NS
101
102 /*
103 * Always round up the allocation request to an extent boundary
104 * (when file on a real-time subvolume or has di_extsize hint).
105 */
106 if (extsize) {
76b57302
PW
107 if (align)
108 align = roundup_64(align, extsize);
dd9f438e
NS
109 else
110 align = extsize;
dd9f438e
NS
111 }
112
f8e3a825
CH
113 return align;
114}
115
116STATIC int
117xfs_iomap_eof_align_last_fsb(
118 struct xfs_inode *ip,
119 xfs_extlen_t extsize,
120 xfs_fileoff_t *last_fsb)
121{
122 xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
123
76b57302
PW
124 if (align) {
125 xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
f8e3a825
CH
126 int eof, error;
127
541d7d3c 128 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
dd9f438e
NS
129 if (error)
130 return error;
131 if (eof)
132 *last_fsb = new_last_fsb;
133 }
134 return 0;
135}
136
572d95f4 137STATIC int
6d4a8ecb 138xfs_alert_fsblock_zero(
572d95f4
NS
139 xfs_inode_t *ip,
140 xfs_bmbt_irec_t *imap)
141{
6a19d939 142 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
572d95f4
NS
143 "Access to block zero in inode %llu "
144 "start_block: %llx start_off: %llx "
08e96e1a 145 "blkcnt: %llx extent-state: %x",
572d95f4
NS
146 (unsigned long long)ip->i_ino,
147 (unsigned long long)imap->br_startblock,
148 (unsigned long long)imap->br_startoff,
149 (unsigned long long)imap->br_blockcount,
150 imap->br_state);
2451337d 151 return -EFSCORRUPTED;
572d95f4
NS
152}
153
a206c817 154int
1da177e4
LT
155xfs_iomap_write_direct(
156 xfs_inode_t *ip,
f403b7f4 157 xfs_off_t offset,
1da177e4 158 size_t count,
3070451e 159 xfs_bmbt_irec_t *imap,
405f8042 160 int nmaps)
1da177e4
LT
161{
162 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
163 xfs_fileoff_t offset_fsb;
164 xfs_fileoff_t last_fsb;
dd9f438e 165 xfs_filblks_t count_fsb, resaligned;
1da177e4 166 xfs_fsblock_t firstfsb;
f13eb205 167 xfs_extlen_t extsz;
0116d935 168 int nimaps;
06d10dd9 169 int quota_flag;
1da177e4
LT
170 int rt;
171 xfs_trans_t *tp;
2c3234d1 172 struct xfs_defer_ops dfops;
dd9f438e 173 uint qblocks, resblks, resrtextents;
dd9f438e 174 int error;
009c6e87 175 int lockmode;
1ca19157 176 int bmapi_flags = XFS_BMAPI_PREALLOC;
253f4911 177 uint tflags = 0;
1da177e4 178
dd9f438e 179 rt = XFS_IS_REALTIME_INODE(ip);
957d0ebe 180 extsz = xfs_get_extsz_hint(ip);
009c6e87
BF
181 lockmode = XFS_ILOCK_SHARED; /* locked by caller */
182
183 ASSERT(xfs_isilocked(ip, lockmode));
1da177e4 184
957d0ebe
DC
185 offset_fsb = XFS_B_TO_FSBT(mp, offset);
186 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
ce7ae151 187 if ((offset + count) > XFS_ISIZE(ip)) {
009c6e87
BF
188 /*
189 * Assert that the in-core extent list is present since this can
190 * call xfs_iread_extents() and we only have the ilock shared.
191 * This should be safe because the lock was held around a bmapi
192 * call in the caller and we only need it to access the in-core
193 * list.
194 */
195 ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
196 XFS_IFEXTENTS);
f8e3a825 197 error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
dd9f438e 198 if (error)
009c6e87 199 goto out_unlock;
1da177e4 200 } else {
405f8042 201 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
dd9f438e 202 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
3070451e
CH
203 imap->br_blockcount +
204 imap->br_startoff);
1da177e4 205 }
dd9f438e
NS
206 count_fsb = last_fsb - offset_fsb;
207 ASSERT(count_fsb > 0);
f13eb205 208 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
dd9f438e
NS
209
210 if (unlikely(rt)) {
211 resrtextents = qblocks = resaligned;
212 resrtextents /= mp->m_sb.sb_rextsize;
84e1e99f
DC
213 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
214 quota_flag = XFS_QMOPT_RES_RTBLKS;
215 } else {
216 resrtextents = 0;
dd9f438e 217 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
84e1e99f
DC
218 quota_flag = XFS_QMOPT_RES_REGBLKS;
219 }
1da177e4 220
009c6e87
BF
221 /*
222 * Drop the shared lock acquired by the caller, attach the dquot if
223 * necessary and move on to transaction setup.
224 */
225 xfs_iunlock(ip, lockmode);
226 error = xfs_qm_dqattach(ip, 0);
227 if (error)
228 return error;
229
1ca19157
DC
230 /*
231 * For DAX, we do not allocate unwritten extents, but instead we zero
232 * the block before we commit the transaction. Ideally we'd like to do
233 * this outside the transaction context, but if we commit and then crash
234 * we may not have zeroed the blocks and this will be exposed on
235 * recovery of the allocation. Hence we must zero before commit.
3b0fe478 236 *
1ca19157
DC
237 * Further, if we are mapping unwritten extents here, we need to zero
238 * and convert them to written so that we don't need an unwritten extent
239 * callback for DAX. This also means that we need to be able to dip into
3b0fe478
DC
240 * the reserve block pool for bmbt block allocation if there is no space
241 * left but we need to do unwritten extent conversion.
1ca19157
DC
242 */
243 if (IS_DAX(VFS_I(ip))) {
244 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
63fbb4c1 245 if (imap->br_state == XFS_EXT_UNWRITTEN) {
253f4911 246 tflags |= XFS_TRANS_RESERVE;
3b0fe478
DC
247 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
248 }
1ca19157 249 }
253f4911
CH
250 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
251 tflags, &tp);
252 if (error)
b474c7ae 253 return error;
507630b2 254
009c6e87
BF
255 lockmode = XFS_ILOCK_EXCL;
256 xfs_ilock(ip, lockmode);
1da177e4 257
7d095257 258 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
dd9f438e 259 if (error)
507630b2 260 goto out_trans_cancel;
1da177e4 261
ddc3415a 262 xfs_trans_ijoin(tp, ip, 0);
1da177e4 263
1da177e4 264 /*
3070451e
CH
265 * From this point onwards we overwrite the imap pointer that the
266 * caller gave to us.
1da177e4 267 */
2c3234d1 268 xfs_defer_init(&dfops, &firstfsb);
06d10dd9 269 nimaps = 1;
d531d91d 270 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
264e89ad 271 bmapi_flags, &firstfsb, resblks, imap,
2c3234d1 272 &nimaps, &dfops);
06d10dd9 273 if (error)
507630b2 274 goto out_bmap_cancel;
1da177e4
LT
275
276 /*
06d10dd9 277 * Complete the transaction
1da177e4 278 */
8ad7c629 279 error = xfs_defer_finish(&tp, &dfops);
06d10dd9 280 if (error)
507630b2 281 goto out_bmap_cancel;
1ca19157 282
70393313 283 error = xfs_trans_commit(tp);
06d10dd9 284 if (error)
507630b2 285 goto out_unlock;
1da177e4 286
06d10dd9
NS
287 /*
288 * Copy any maps to caller's array and return any error.
289 */
1da177e4 290 if (nimaps == 0) {
2451337d 291 error = -ENOSPC;
507630b2 292 goto out_unlock;
572d95f4
NS
293 }
294
507630b2 295 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
6d4a8ecb 296 error = xfs_alert_fsblock_zero(ip, imap);
1da177e4 297
507630b2 298out_unlock:
009c6e87 299 xfs_iunlock(ip, lockmode);
507630b2 300 return error;
1da177e4 301
507630b2 302out_bmap_cancel:
2c3234d1 303 xfs_defer_cancel(&dfops);
ea562ed6 304 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
507630b2 305out_trans_cancel:
4906e215 306 xfs_trans_cancel(tp);
507630b2 307 goto out_unlock;
1da177e4
LT
308}
309
76a4202a
BF
310STATIC bool
311xfs_quota_need_throttle(
312 struct xfs_inode *ip,
313 int type,
314 xfs_fsblock_t alloc_blocks)
315{
316 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
317
318 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
319 return false;
320
321 /* no hi watermark, no throttle */
322 if (!dq->q_prealloc_hi_wmark)
323 return false;
324
325 /* under the lo watermark, no throttle */
326 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
327 return false;
328
329 return true;
330}
331
332STATIC void
333xfs_quota_calc_throttle(
334 struct xfs_inode *ip,
335 int type,
336 xfs_fsblock_t *qblocks,
f074051f
BF
337 int *qshift,
338 int64_t *qfreesp)
76a4202a
BF
339{
340 int64_t freesp;
341 int shift = 0;
342 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
343
5cca3f61
ES
344 /* no dq, or over hi wmark, squash the prealloc completely */
345 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
76a4202a 346 *qblocks = 0;
f074051f 347 *qfreesp = 0;
76a4202a
BF
348 return;
349 }
350
351 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
352 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
353 shift = 2;
354 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
355 shift += 2;
356 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
357 shift += 2;
358 }
359
f074051f
BF
360 if (freesp < *qfreesp)
361 *qfreesp = freesp;
362
76a4202a
BF
363 /* only overwrite the throttle values if we are more aggressive */
364 if ((freesp >> shift) < (*qblocks >> *qshift)) {
365 *qblocks = freesp;
366 *qshift = shift;
367 }
368}
369
055388a3 370/*
51446f5b
CH
371 * If we are doing a write at the end of the file and there are no allocations
372 * past this one, then extend the allocation out to the file system's write
373 * iosize.
374 *
055388a3 375 * If we don't have a user specified preallocation size, dynamically increase
51446f5b 376 * the preallocation size as the size of the file grows. Cap the maximum size
055388a3
DC
377 * at a single extent or less if the filesystem is near full. The closer the
378 * filesystem is to full, the smaller the maximum prealocation.
51446f5b
CH
379 *
380 * As an exception we don't do any preallocation at all if the file is smaller
381 * than the minimum preallocation and we are using the default dynamic
382 * preallocation scheme, as it is likely this is the only write to the file that
383 * is going to be done.
384 *
385 * We clean up any extra space left over when the file is closed in
386 * xfs_inactive().
055388a3
DC
387 */
388STATIC xfs_fsblock_t
389xfs_iomap_prealloc_size(
a1e16c26 390 struct xfs_inode *ip,
51446f5b
CH
391 loff_t offset,
392 loff_t count,
656152e5 393 xfs_extnum_t idx)
055388a3 394{
51446f5b 395 struct xfs_mount *mp = ip->i_mount;
656152e5 396 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
51446f5b 397 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
656152e5 398 struct xfs_bmbt_irec prev;
3c58b5f8
BF
399 int shift = 0;
400 int64_t freesp;
76a4202a
BF
401 xfs_fsblock_t qblocks;
402 int qshift = 0;
51446f5b
CH
403 xfs_fsblock_t alloc_blocks = 0;
404
405 if (offset + count <= XFS_ISIZE(ip))
406 return 0;
407
408 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
409 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
410 return 0;
411
412 /*
413 * If an explicit allocsize is set, the file is small, or we
414 * are writing behind a hole, then use the minimum prealloc:
415 */
416 if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
417 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
656152e5
CH
418 !xfs_iext_get_extent(ifp, idx - 1, &prev) ||
419 prev.br_startoff + prev.br_blockcount < offset_fsb)
51446f5b 420 return mp->m_writeio_blocks;
055388a3 421
51446f5b
CH
422 /*
423 * Determine the initial size of the preallocation. We are beyond the
424 * current EOF here, but we need to take into account whether this is
425 * a sparse write or an extending write when determining the
426 * preallocation size. Hence we need to look up the extent that ends
427 * at the current write offset and use the result to determine the
428 * preallocation size.
429 *
430 * If the extent is a hole, then preallocation is essentially disabled.
431 * Otherwise we take the size of the preceding data extent as the basis
432 * for the preallocation size. If the size of the extent is greater than
433 * half the maximum extent length, then use the current offset as the
434 * basis. This ensures that for large files the preallocation size
435 * always extends to MAXEXTLEN rather than falling short due to things
436 * like stripe unit/width alignment of real extents.
437 */
656152e5
CH
438 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
439 alloc_blocks = prev.br_blockcount << 1;
51446f5b
CH
440 else
441 alloc_blocks = XFS_B_TO_FSB(mp, offset);
3c58b5f8
BF
442 if (!alloc_blocks)
443 goto check_writeio;
76a4202a 444 qblocks = alloc_blocks;
3c58b5f8 445
c9bdbdc0
BF
446 /*
447 * MAXEXTLEN is not a power of two value but we round the prealloc down
448 * to the nearest power of two value after throttling. To prevent the
449 * round down from unconditionally reducing the maximum supported prealloc
450 * size, we round up first, apply appropriate throttling, round down and
451 * cap the value to MAXEXTLEN.
452 */
453 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
454 alloc_blocks);
3c58b5f8 455
0d485ada 456 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
3c58b5f8
BF
457 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
458 shift = 2;
459 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
460 shift++;
461 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
462 shift++;
463 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
464 shift++;
465 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
466 shift++;
055388a3 467 }
76a4202a
BF
468
469 /*
f074051f
BF
470 * Check each quota to cap the prealloc size, provide a shift value to
471 * throttle with and adjust amount of available space.
76a4202a
BF
472 */
473 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
f074051f
BF
474 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
475 &freesp);
76a4202a 476 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
f074051f
BF
477 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
478 &freesp);
76a4202a 479 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
f074051f
BF
480 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
481 &freesp);
76a4202a
BF
482
483 /*
484 * The final prealloc size is set to the minimum of free space available
485 * in each of the quotas and the overall filesystem.
486 *
487 * The shift throttle value is set to the maximum value as determined by
488 * the global low free space values and per-quota low free space values.
489 */
490 alloc_blocks = MIN(alloc_blocks, qblocks);
491 shift = MAX(shift, qshift);
492
3c58b5f8
BF
493 if (shift)
494 alloc_blocks >>= shift;
c9bdbdc0
BF
495 /*
496 * rounddown_pow_of_two() returns an undefined result if we pass in
497 * alloc_blocks = 0.
498 */
499 if (alloc_blocks)
500 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
501 if (alloc_blocks > MAXEXTLEN)
502 alloc_blocks = MAXEXTLEN;
3c58b5f8
BF
503
504 /*
505 * If we are still trying to allocate more space than is
506 * available, squash the prealloc hard. This can happen if we
507 * have a large file on a small filesystem and the above
508 * lowspace thresholds are smaller than MAXEXTLEN.
509 */
510 while (alloc_blocks && alloc_blocks >= freesp)
511 alloc_blocks >>= 4;
3c58b5f8 512check_writeio:
055388a3
DC
513 if (alloc_blocks < mp->m_writeio_blocks)
514 alloc_blocks = mp->m_writeio_blocks;
19cb7e38
BF
515 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
516 mp->m_writeio_blocks);
055388a3
DC
517 return alloc_blocks;
518}
519
51446f5b
CH
520static int
521xfs_file_iomap_begin_delay(
522 struct inode *inode,
523 loff_t offset,
524 loff_t count,
51446f5b 525 struct iomap *iomap)
1da177e4 526{
51446f5b
CH
527 struct xfs_inode *ip = XFS_I(inode);
528 struct xfs_mount *mp = ip->i_mount;
529 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
530 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
531 xfs_fileoff_t maxbytes_fsb =
532 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
f782088c 533 xfs_fileoff_t end_fsb;
51446f5b
CH
534 int error = 0, eof = 0;
535 struct xfs_bmbt_irec got;
51446f5b 536 xfs_extnum_t idx;
f782088c 537 xfs_fsblock_t prealloc_blocks = 0;
51446f5b
CH
538
539 ASSERT(!XFS_IS_REALTIME_INODE(ip));
540 ASSERT(!xfs_get_extsz_hint(ip));
dd9f438e 541
51446f5b 542 xfs_ilock(ip, XFS_ILOCK_EXCL);
1da177e4 543
51446f5b
CH
544 if (unlikely(XFS_TEST_ERROR(
545 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
546 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
9e24cfd0 547 mp, XFS_ERRTAG_BMAPIFORMAT))) {
51446f5b
CH
548 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
549 error = -EFSCORRUPTED;
550 goto out_unlock;
551 }
a1e16c26 552
51446f5b 553 XFS_STATS_INC(mp, xs_blk_mapw);
055388a3 554
51446f5b
CH
555 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
556 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
557 if (error)
558 goto out_unlock;
1da177e4 559 }
1da177e4 560
656152e5 561 eof = !xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got);
51446f5b 562 if (!eof && got.br_startoff <= offset_fsb) {
3ba020be
CH
563 if (xfs_is_reflink_inode(ip)) {
564 bool shared;
565
566 end_fsb = min(XFS_B_TO_FSB(mp, offset + count),
567 maxbytes_fsb);
568 xfs_trim_extent(&got, offset_fsb, end_fsb - offset_fsb);
569 error = xfs_reflink_reserve_cow(ip, &got, &shared);
570 if (error)
571 goto out_unlock;
572 }
573
51446f5b
CH
574 trace_xfs_iomap_found(ip, offset, count, 0, &got);
575 goto done;
1da177e4 576 }
dd9f438e 577
51446f5b
CH
578 error = xfs_qm_dqattach_locked(ip, 0);
579 if (error)
580 goto out_unlock;
581
3ed9116e 582 /*
51446f5b
CH
583 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES pages
584 * to keep the chunks of work done where somewhat symmetric with the
585 * work writeback does. This is a completely arbitrary number pulled
586 * out of thin air as a best guess for initial testing.
587 *
588 * Note that the values needs to be less than 32-bits wide until
589 * the lower level functions are updated.
3ed9116e 590 */
51446f5b 591 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
f782088c 592 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
51446f5b
CH
593
594 if (eof) {
656152e5 595 prealloc_blocks = xfs_iomap_prealloc_size(ip, offset, count, idx);
51446f5b
CH
596 if (prealloc_blocks) {
597 xfs_extlen_t align;
598 xfs_off_t end_offset;
f782088c 599 xfs_fileoff_t p_end_fsb;
3ed9116e 600
51446f5b 601 end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
f782088c
BF
602 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
603 prealloc_blocks;
51446f5b
CH
604
605 align = xfs_eof_alignment(ip, 0);
606 if (align)
f782088c 607 p_end_fsb = roundup_64(p_end_fsb, align);
51446f5b 608
f782088c
BF
609 p_end_fsb = min(p_end_fsb, maxbytes_fsb);
610 ASSERT(p_end_fsb > offset_fsb);
611 prealloc_blocks = p_end_fsb - end_fsb;
51446f5b
CH
612 }
613 }
614
615retry:
be51f811 616 error = xfs_bmapi_reserve_delalloc(ip, XFS_DATA_FORK, offset_fsb,
f782088c 617 end_fsb - offset_fsb, prealloc_blocks, &got, &idx, eof);
055388a3
DC
618 switch (error) {
619 case 0:
51446f5b 620 break;
2451337d
DC
621 case -ENOSPC:
622 case -EDQUOT:
51446f5b 623 /* retry without any preallocation */
0b1b213f 624 trace_xfs_delalloc_enospc(ip, offset, count);
f782088c
BF
625 if (prealloc_blocks) {
626 prealloc_blocks = 0;
9aa05000 627 goto retry;
055388a3 628 }
51446f5b
CH
629 /*FALLTHRU*/
630 default:
631 goto out_unlock;
1da177e4
LT
632 }
633
f65e6fad
BF
634 /*
635 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
636 * them out if the write happens to fail.
637 */
638 iomap->flags = IOMAP_F_NEW;
51446f5b
CH
639 trace_xfs_iomap_alloc(ip, offset, count, 0, &got);
640done:
641 if (isnullstartblock(got.br_startblock))
642 got.br_startblock = DELAYSTARTBLOCK;
643
644 if (!got.br_startblock) {
645 error = xfs_alert_fsblock_zero(ip, &got);
646 if (error)
647 goto out_unlock;
648 }
649
650 xfs_bmbt_to_iomap(ip, iomap, &got);
651
652out_unlock:
653 xfs_iunlock(ip, XFS_ILOCK_EXCL);
654 return error;
1da177e4
LT
655}
656
657/*
658 * Pass in a delayed allocate extent, convert it to real extents;
659 * return to the caller the extent we create which maps on top of
660 * the originating callers request.
661 *
662 * Called without a lock on the inode.
e4143a1c
DC
663 *
664 * We no longer bother to look at the incoming map - all we have to
665 * guarantee is that whatever we allocate fills the required range.
1da177e4 666 */
a206c817 667int
1da177e4
LT
668xfs_iomap_write_allocate(
669 xfs_inode_t *ip,
60b4984f 670 int whichfork,
f403b7f4 671 xfs_off_t offset,
405f8042 672 xfs_bmbt_irec_t *imap)
1da177e4
LT
673{
674 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
675 xfs_fileoff_t offset_fsb, last_block;
676 xfs_fileoff_t end_fsb, map_start_fsb;
677 xfs_fsblock_t first_block;
2c3234d1 678 struct xfs_defer_ops dfops;
1da177e4 679 xfs_filblks_t count_fsb;
1da177e4 680 xfs_trans_t *tp;
f6106efa 681 int nimaps;
1da177e4 682 int error = 0;
d2b3964a 683 int flags = XFS_BMAPI_DELALLOC;
1da177e4
LT
684 int nres;
685
60b4984f 686 if (whichfork == XFS_COW_FORK)
5eda4300 687 flags |= XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
60b4984f 688
1da177e4
LT
689 /*
690 * Make sure that the dquots are there.
691 */
7d095257
CH
692 error = xfs_qm_dqattach(ip, 0);
693 if (error)
b474c7ae 694 return error;
1da177e4 695
24e17b5f 696 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3070451e
CH
697 count_fsb = imap->br_blockcount;
698 map_start_fsb = imap->br_startoff;
1da177e4 699
ff6d6af2 700 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
1da177e4
LT
701
702 while (count_fsb != 0) {
703 /*
704 * Set up a transaction with which to allocate the
705 * backing store for the file. Do allocations in a
706 * loop until we get some space in the range we are
707 * interested in. The other space that might be allocated
708 * is in the delayed allocation extent on which we sit
709 * but before our buffer starts.
710 */
1da177e4
LT
711 nimaps = 0;
712 while (nimaps == 0) {
1da177e4 713 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
0af32fb4
CH
714 /*
715 * We have already reserved space for the extent and any
716 * indirect blocks when creating the delalloc extent,
717 * there is no need to reserve space in this transaction
718 * again.
719 */
720 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0,
253f4911
CH
721 0, XFS_TRANS_RESERVE, &tp);
722 if (error)
b474c7ae 723 return error;
253f4911 724
1da177e4 725 xfs_ilock(ip, XFS_ILOCK_EXCL);
ddc3415a 726 xfs_trans_ijoin(tp, ip, 0);
1da177e4 727
2c3234d1 728 xfs_defer_init(&dfops, &first_block);
1da177e4 729
1da177e4 730 /*
e4143a1c
DC
731 * it is possible that the extents have changed since
732 * we did the read call as we dropped the ilock for a
733 * while. We have to be careful about truncates or hole
734 * punchs here - we are not allowed to allocate
735 * non-delalloc blocks here.
736 *
737 * The only protection against truncation is the pages
738 * for the range we are being asked to convert are
739 * locked and hence a truncate will block on them
740 * first.
741 *
742 * As a result, if we go beyond the range we really
743 * need and hit an delalloc extent boundary followed by
744 * a hole while we have excess blocks in the map, we
745 * will fill the hole incorrectly and overrun the
746 * transaction reservation.
747 *
748 * Using a single map prevents this as we are forced to
749 * check each map we look for overlap with the desired
750 * range and abort as soon as we find it. Also, given
751 * that we only return a single map, having one beyond
752 * what we can return is probably a bit silly.
753 *
754 * We also need to check that we don't go beyond EOF;
755 * this is a truncate optimisation as a truncate sets
756 * the new file size before block on the pages we
757 * currently have locked under writeback. Because they
758 * are about to be tossed, we don't need to write them
759 * back....
1da177e4 760 */
e4143a1c 761 nimaps = 1;
ce7ae151 762 end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
7fb2cd4d 763 error = xfs_bmap_last_offset(ip, &last_block,
7c9ef85c
DC
764 XFS_DATA_FORK);
765 if (error)
766 goto trans_cancel;
767
1da177e4
LT
768 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
769 if ((map_start_fsb + count_fsb) > last_block) {
770 count_fsb = last_block - map_start_fsb;
771 if (count_fsb == 0) {
2451337d 772 error = -EAGAIN;
1da177e4
LT
773 goto trans_cancel;
774 }
775 }
776
3070451e 777 /*
3070451e
CH
778 * From this point onwards we overwrite the imap
779 * pointer that the caller gave to us.
780 */
c0dc7828 781 error = xfs_bmapi_write(tp, ip, map_start_fsb,
60b4984f 782 count_fsb, flags, &first_block,
dbd5c8c9 783 nres, imap, &nimaps,
2c3234d1 784 &dfops);
1da177e4
LT
785 if (error)
786 goto trans_cancel;
787
8ad7c629 788 error = xfs_defer_finish(&tp, &dfops);
1da177e4
LT
789 if (error)
790 goto trans_cancel;
791
70393313 792 error = xfs_trans_commit(tp);
1da177e4
LT
793 if (error)
794 goto error0;
795
796 xfs_iunlock(ip, XFS_ILOCK_EXCL);
797 }
798
799 /*
800 * See if we were able to allocate an extent that
801 * covers at least part of the callers request
802 */
3070451e 803 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
6d4a8ecb 804 return xfs_alert_fsblock_zero(ip, imap);
86c4d623 805
3070451e
CH
806 if ((offset_fsb >= imap->br_startoff) &&
807 (offset_fsb < (imap->br_startoff +
808 imap->br_blockcount))) {
ff6d6af2 809 XFS_STATS_INC(mp, xs_xstrat_quick);
e4143a1c 810 return 0;
1da177e4
LT
811 }
812
e4143a1c
DC
813 /*
814 * So far we have not mapped the requested part of the
1da177e4
LT
815 * file, just surrounding data, try again.
816 */
3070451e
CH
817 count_fsb -= imap->br_blockcount;
818 map_start_fsb = imap->br_startoff + imap->br_blockcount;
1da177e4
LT
819 }
820
821trans_cancel:
2c3234d1 822 xfs_defer_cancel(&dfops);
4906e215 823 xfs_trans_cancel(tp);
1da177e4
LT
824error0:
825 xfs_iunlock(ip, XFS_ILOCK_EXCL);
b474c7ae 826 return error;
1da177e4
LT
827}
828
829int
830xfs_iomap_write_unwritten(
831 xfs_inode_t *ip,
f403b7f4 832 xfs_off_t offset,
ee70daab
EG
833 xfs_off_t count,
834 bool update_isize)
1da177e4
LT
835{
836 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
837 xfs_fileoff_t offset_fsb;
838 xfs_filblks_t count_fsb;
839 xfs_filblks_t numblks_fsb;
dd9f438e
NS
840 xfs_fsblock_t firstfsb;
841 int nimaps;
842 xfs_trans_t *tp;
843 xfs_bmbt_irec_t imap;
2c3234d1 844 struct xfs_defer_ops dfops;
ee70daab 845 struct inode *inode = VFS_I(ip);
84803fb7 846 xfs_fsize_t i_size;
dd9f438e 847 uint resblks;
1da177e4 848 int error;
1da177e4 849
0b1b213f 850 trace_xfs_unwritten_convert(ip, offset, count);
1da177e4
LT
851
852 offset_fsb = XFS_B_TO_FSBT(mp, offset);
853 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
854 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
855
4ddd8bb1
LM
856 /*
857 * Reserve enough blocks in this transaction for two complete extent
858 * btree splits. We may be converting the middle part of an unwritten
859 * extent and in this case we will insert two new extents in the btree
860 * each of which could cause a full split.
861 *
862 * This reservation amount will be used in the first call to
863 * xfs_bmbt_split() to select an AG with enough space to satisfy the
864 * rest of the operation.
865 */
dd9f438e 866 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
1da177e4 867
dd9f438e 868 do {
1da177e4 869 /*
253f4911 870 * Set up a transaction to convert the range of extents
1da177e4
LT
871 * from unwritten to real. Do allocations in a loop until
872 * we have covered the range passed in.
80641dc6 873 *
253f4911
CH
874 * Note that we can't risk to recursing back into the filesystem
875 * here as we might be asked to write out the same inode that we
876 * complete here and might deadlock on the iolock.
1da177e4 877 */
253f4911
CH
878 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
879 XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
880 if (error)
b474c7ae 881 return error;
1da177e4
LT
882
883 xfs_ilock(ip, XFS_ILOCK_EXCL);
ddc3415a 884 xfs_trans_ijoin(tp, ip, 0);
1da177e4
LT
885
886 /*
887 * Modify the unwritten extent state of the buffer.
888 */
2c3234d1 889 xfs_defer_init(&dfops, &firstfsb);
1da177e4 890 nimaps = 1;
c0dc7828 891 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
dbd5c8c9 892 XFS_BMAPI_CONVERT, &firstfsb, resblks,
2c3234d1 893 &imap, &nimaps, &dfops);
1da177e4
LT
894 if (error)
895 goto error_on_bmapi_transaction;
896
84803fb7
CH
897 /*
898 * Log the updated inode size as we go. We have to be careful
899 * to only log it up to the actual write offset if it is
900 * halfway into a block.
901 */
902 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
903 if (i_size > offset + count)
904 i_size = offset + count;
ee70daab
EG
905 if (update_isize && i_size > i_size_read(inode))
906 i_size_write(inode, i_size);
84803fb7
CH
907 i_size = xfs_new_eof(ip, i_size);
908 if (i_size) {
909 ip->i_d.di_size = i_size;
910 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
911 }
912
8ad7c629 913 error = xfs_defer_finish(&tp, &dfops);
1da177e4
LT
914 if (error)
915 goto error_on_bmapi_transaction;
916
70393313 917 error = xfs_trans_commit(tp);
1da177e4
LT
918 xfs_iunlock(ip, XFS_ILOCK_EXCL);
919 if (error)
b474c7ae 920 return error;
572d95f4 921
86c4d623 922 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
6d4a8ecb 923 return xfs_alert_fsblock_zero(ip, &imap);
1da177e4
LT
924
925 if ((numblks_fsb = imap.br_blockcount) == 0) {
926 /*
927 * The numblks_fsb value should always get
928 * smaller, otherwise the loop is stuck.
929 */
930 ASSERT(imap.br_blockcount);
931 break;
932 }
933 offset_fsb += numblks_fsb;
934 count_fsb -= numblks_fsb;
935 } while (count_fsb > 0);
936
937 return 0;
938
939error_on_bmapi_transaction:
2c3234d1 940 xfs_defer_cancel(&dfops);
4906e215 941 xfs_trans_cancel(tp);
1da177e4 942 xfs_iunlock(ip, XFS_ILOCK_EXCL);
b474c7ae 943 return error;
1da177e4 944}
3b3dce05 945
6c31f495
CH
946static inline bool imap_needs_alloc(struct inode *inode,
947 struct xfs_bmbt_irec *imap, int nimaps)
68a9f5e7
CH
948{
949 return !nimaps ||
950 imap->br_startblock == HOLESTARTBLOCK ||
6c31f495 951 imap->br_startblock == DELAYSTARTBLOCK ||
63fbb4c1 952 (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN);
68a9f5e7
CH
953}
954
acdda3aa
CH
955static inline bool need_excl_ilock(struct xfs_inode *ip, unsigned flags)
956{
957 /*
958 * COW writes will allocate delalloc space, so we need to make sure
959 * to take the lock exclusively here.
960 */
961 if (xfs_is_reflink_inode(ip) && (flags & (IOMAP_WRITE | IOMAP_ZERO)))
962 return true;
963 if ((flags & IOMAP_DIRECT) && (flags & IOMAP_WRITE))
964 return true;
965 return false;
966}
967
68a9f5e7
CH
968static int
969xfs_file_iomap_begin(
970 struct inode *inode,
971 loff_t offset,
972 loff_t length,
973 unsigned flags,
974 struct iomap *iomap)
975{
976 struct xfs_inode *ip = XFS_I(inode);
977 struct xfs_mount *mp = ip->i_mount;
978 struct xfs_bmbt_irec imap;
979 xfs_fileoff_t offset_fsb, end_fsb;
980 int nimaps = 1, error = 0;
3ba020be 981 bool shared = false, trimmed = false;
66642c5c 982 unsigned lockmode;
68a9f5e7
CH
983
984 if (XFS_FORCED_SHUTDOWN(mp))
985 return -EIO;
986
acdda3aa
CH
987 if (((flags & (IOMAP_WRITE | IOMAP_DIRECT)) == IOMAP_WRITE) &&
988 !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
2a06705c 989 /* Reserve delalloc blocks for regular writeback. */
f91fb956 990 return xfs_file_iomap_begin_delay(inode, offset, length, iomap);
51446f5b
CH
991 }
992
acdda3aa 993 if (need_excl_ilock(ip, flags)) {
3ba020be
CH
994 lockmode = XFS_ILOCK_EXCL;
995 xfs_ilock(ip, XFS_ILOCK_EXCL);
996 } else {
997 lockmode = xfs_ilock_data_map_shared(ip);
998 }
68a9f5e7 999
29a5d29e
GR
1000 if ((flags & IOMAP_NOWAIT) && !(ip->i_df.if_flags & XFS_IFEXTENTS)) {
1001 error = -EAGAIN;
1002 goto out_unlock;
1003 }
1004
68a9f5e7
CH
1005 ASSERT(offset <= mp->m_super->s_maxbytes);
1006 if ((xfs_fsize_t)offset + length > mp->m_super->s_maxbytes)
1007 length = mp->m_super->s_maxbytes - offset;
1008 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1009 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1010
1011 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
db1327b1 1012 &nimaps, 0);
3ba020be
CH
1013 if (error)
1014 goto out_unlock;
db1327b1 1015
3c68d44a 1016 if (flags & IOMAP_REPORT) {
5f9268ca
CH
1017 /* Trim the mapping to the nearest shared extent boundary. */
1018 error = xfs_reflink_trim_around_shared(ip, &imap, &shared,
1019 &trimmed);
3ba020be
CH
1020 if (error)
1021 goto out_unlock;
1022 }
1023
1024 if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && xfs_is_reflink_inode(ip)) {
3c68d44a 1025 if (flags & IOMAP_DIRECT) {
29a5d29e
GR
1026 /*
1027 * A reflinked inode will result in CoW alloc.
1028 * FIXME: It could still overwrite on unshared extents
1029 * and not need allocation.
1030 */
1031 if (flags & IOMAP_NOWAIT) {
1032 error = -EAGAIN;
1033 goto out_unlock;
1034 }
3c68d44a
CH
1035 /* may drop and re-acquire the ilock */
1036 error = xfs_reflink_allocate_cow(ip, &imap, &shared,
1037 &lockmode);
1038 if (error)
1039 goto out_unlock;
1040 } else {
1041 error = xfs_reflink_reserve_cow(ip, &imap, &shared);
1042 if (error)
1043 goto out_unlock;
1044 }
3ba020be
CH
1045
1046 end_fsb = imap.br_startoff + imap.br_blockcount;
1047 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
68a9f5e7
CH
1048 }
1049
6c31f495 1050 if ((flags & IOMAP_WRITE) && imap_needs_alloc(inode, &imap, nimaps)) {
29a5d29e
GR
1051 /*
1052 * If nowait is set bail since we are going to make
1053 * allocations.
1054 */
1055 if (flags & IOMAP_NOWAIT) {
1056 error = -EAGAIN;
1057 goto out_unlock;
1058 }
68a9f5e7
CH
1059 /*
1060 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
1061 * pages to keep the chunks of work done where somewhat symmetric
1062 * with the work writeback does. This is a completely arbitrary
1063 * number pulled out of thin air as a best guess for initial
1064 * testing.
1065 *
1066 * Note that the values needs to be less than 32-bits wide until
1067 * the lower level functions are updated.
1068 */
1069 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
51446f5b
CH
1070 /*
1071 * xfs_iomap_write_direct() expects the shared lock. It
1072 * is unlocked on return.
1073 */
66642c5c
CH
1074 if (lockmode == XFS_ILOCK_EXCL)
1075 xfs_ilock_demote(ip, lockmode);
51446f5b
CH
1076 error = xfs_iomap_write_direct(ip, offset, length, &imap,
1077 nimaps);
68a9f5e7
CH
1078 if (error)
1079 return error;
1080
ecd50729 1081 iomap->flags = IOMAP_F_NEW;
68a9f5e7 1082 trace_xfs_iomap_alloc(ip, offset, length, 0, &imap);
68a9f5e7 1083 } else {
b95a2127
CH
1084 ASSERT(nimaps);
1085
66642c5c 1086 xfs_iunlock(ip, lockmode);
b95a2127 1087 trace_xfs_iomap_found(ip, offset, length, 0, &imap);
68a9f5e7
CH
1088 }
1089
a39e596b
CH
1090 if ((flags & IOMAP_WRITE) && xfs_ipincount(ip) &&
1091 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
1092 iomap->flags |= IOMAP_F_DIRTY;
1093
b95a2127 1094 xfs_bmbt_to_iomap(ip, iomap, &imap);
fa5d932c 1095
db1327b1
DW
1096 if (shared)
1097 iomap->flags |= IOMAP_F_SHARED;
68a9f5e7 1098 return 0;
3ba020be
CH
1099out_unlock:
1100 xfs_iunlock(ip, lockmode);
1101 return error;
68a9f5e7
CH
1102}
1103
1104static int
1105xfs_file_iomap_end_delalloc(
1106 struct xfs_inode *ip,
1107 loff_t offset,
1108 loff_t length,
f65e6fad
BF
1109 ssize_t written,
1110 struct iomap *iomap)
68a9f5e7
CH
1111{
1112 struct xfs_mount *mp = ip->i_mount;
1113 xfs_fileoff_t start_fsb;
1114 xfs_fileoff_t end_fsb;
1115 int error = 0;
1116
f65e6fad
BF
1117 /*
1118 * Behave as if the write failed if drop writes is enabled. Set the NEW
1119 * flag to force delalloc cleanup.
1120 */
f8c47250 1121 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
f65e6fad 1122 iomap->flags |= IOMAP_F_NEW;
9dbddd7b 1123 written = 0;
f65e6fad 1124 }
9dbddd7b 1125
fa7f138a
BF
1126 /*
1127 * start_fsb refers to the first unused block after a short write. If
1128 * nothing was written, round offset down to point at the first block in
1129 * the range.
1130 */
1131 if (unlikely(!written))
1132 start_fsb = XFS_B_TO_FSBT(mp, offset);
1133 else
1134 start_fsb = XFS_B_TO_FSB(mp, offset + written);
68a9f5e7
CH
1135 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1136
1137 /*
f65e6fad
BF
1138 * Trim delalloc blocks if they were allocated by this write and we
1139 * didn't manage to write the whole range.
68a9f5e7
CH
1140 *
1141 * We don't need to care about racing delalloc as we hold i_mutex
1142 * across the reserve/allocate/unreserve calls. If there are delalloc
1143 * blocks in the range, they are ours.
1144 */
f65e6fad 1145 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
fa7f138a
BF
1146 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1147 XFS_FSB_TO_B(mp, end_fsb) - 1);
1148
68a9f5e7
CH
1149 xfs_ilock(ip, XFS_ILOCK_EXCL);
1150 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1151 end_fsb - start_fsb);
1152 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1153
1154 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1155 xfs_alert(mp, "%s: unable to clean up ino %lld",
1156 __func__, ip->i_ino);
1157 return error;
1158 }
1159 }
1160
1161 return 0;
1162}
1163
1164static int
1165xfs_file_iomap_end(
1166 struct inode *inode,
1167 loff_t offset,
1168 loff_t length,
1169 ssize_t written,
1170 unsigned flags,
1171 struct iomap *iomap)
1172{
1173 if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
1174 return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
f65e6fad 1175 length, written, iomap);
68a9f5e7
CH
1176 return 0;
1177}
1178
8ff6daa1 1179const struct iomap_ops xfs_iomap_ops = {
68a9f5e7
CH
1180 .iomap_begin = xfs_file_iomap_begin,
1181 .iomap_end = xfs_file_iomap_end,
1182};
1d4795e7
CH
1183
1184static int
1185xfs_xattr_iomap_begin(
1186 struct inode *inode,
1187 loff_t offset,
1188 loff_t length,
1189 unsigned flags,
1190 struct iomap *iomap)
1191{
1192 struct xfs_inode *ip = XFS_I(inode);
1193 struct xfs_mount *mp = ip->i_mount;
1194 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1195 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1196 struct xfs_bmbt_irec imap;
1197 int nimaps = 1, error = 0;
1198 unsigned lockmode;
1199
1200 if (XFS_FORCED_SHUTDOWN(mp))
1201 return -EIO;
1202
84358536 1203 lockmode = xfs_ilock_attr_map_shared(ip);
1d4795e7
CH
1204
1205 /* if there are no attribute fork or extents, return ENOENT */
84358536 1206 if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
1d4795e7
CH
1207 error = -ENOENT;
1208 goto out_unlock;
1209 }
1210
1211 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1212 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1213 &nimaps, XFS_BMAPI_ENTIRE | XFS_BMAPI_ATTRFORK);
1214out_unlock:
1215 xfs_iunlock(ip, lockmode);
1216
1217 if (!error) {
1218 ASSERT(nimaps);
1219 xfs_bmbt_to_iomap(ip, iomap, &imap);
1220 }
1221
1222 return error;
1223}
1224
8ff6daa1 1225const struct iomap_ops xfs_xattr_iomap_ops = {
1d4795e7
CH
1226 .iomap_begin = xfs_xattr_iomap_begin,
1227};