]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/xfs/xfs_iomap.c
6b429bfd5bb8271fd443047cc0f26218a4e42f1e
[linux.git] / fs / xfs / xfs_iomap.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4  * Copyright (c) 2016-2018 Christoph Hellwig.
5  * All Rights Reserved.
6  */
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_btree.h"
16 #include "xfs_bmap_btree.h"
17 #include "xfs_bmap.h"
18 #include "xfs_bmap_util.h"
19 #include "xfs_errortag.h"
20 #include "xfs_error.h"
21 #include "xfs_trans.h"
22 #include "xfs_trans_space.h"
23 #include "xfs_inode_item.h"
24 #include "xfs_iomap.h"
25 #include "xfs_trace.h"
26 #include "xfs_quota.h"
27 #include "xfs_dquot_item.h"
28 #include "xfs_dquot.h"
29 #include "xfs_reflink.h"
30
31
32 #define XFS_WRITEIO_ALIGN(mp,off)       (((off) >> mp->m_writeio_log) \
33                                                 << mp->m_writeio_log)
34
35 static int
36 xfs_alert_fsblock_zero(
37         xfs_inode_t     *ip,
38         xfs_bmbt_irec_t *imap)
39 {
40         xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
41                         "Access to block zero in inode %llu "
42                         "start_block: %llx start_off: %llx "
43                         "blkcnt: %llx extent-state: %x",
44                 (unsigned long long)ip->i_ino,
45                 (unsigned long long)imap->br_startblock,
46                 (unsigned long long)imap->br_startoff,
47                 (unsigned long long)imap->br_blockcount,
48                 imap->br_state);
49         return -EFSCORRUPTED;
50 }
51
52 int
53 xfs_bmbt_to_iomap(
54         struct xfs_inode        *ip,
55         struct iomap            *iomap,
56         struct xfs_bmbt_irec    *imap,
57         u16                     flags)
58 {
59         struct xfs_mount        *mp = ip->i_mount;
60
61         if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
62                 return xfs_alert_fsblock_zero(ip, imap);
63
64         if (imap->br_startblock == HOLESTARTBLOCK) {
65                 iomap->addr = IOMAP_NULL_ADDR;
66                 iomap->type = IOMAP_HOLE;
67         } else if (imap->br_startblock == DELAYSTARTBLOCK ||
68                    isnullstartblock(imap->br_startblock)) {
69                 iomap->addr = IOMAP_NULL_ADDR;
70                 iomap->type = IOMAP_DELALLOC;
71         } else {
72                 iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
73                 if (imap->br_state == XFS_EXT_UNWRITTEN)
74                         iomap->type = IOMAP_UNWRITTEN;
75                 else
76                         iomap->type = IOMAP_MAPPED;
77         }
78         iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
79         iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
80         iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
81         iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
82         iomap->flags = flags;
83
84         if (xfs_ipincount(ip) &&
85             (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
86                 iomap->flags |= IOMAP_F_DIRTY;
87         return 0;
88 }
89
90 static void
91 xfs_hole_to_iomap(
92         struct xfs_inode        *ip,
93         struct iomap            *iomap,
94         xfs_fileoff_t           offset_fsb,
95         xfs_fileoff_t           end_fsb)
96 {
97         iomap->addr = IOMAP_NULL_ADDR;
98         iomap->type = IOMAP_HOLE;
99         iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb);
100         iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb);
101         iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
102         iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
103 }
104
105 static inline xfs_fileoff_t
106 xfs_iomap_end_fsb(
107         struct xfs_mount        *mp,
108         loff_t                  offset,
109         loff_t                  count)
110 {
111         ASSERT(offset <= mp->m_super->s_maxbytes);
112         return min(XFS_B_TO_FSB(mp, offset + count),
113                    XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
114 }
115
116 xfs_extlen_t
117 xfs_eof_alignment(
118         struct xfs_inode        *ip,
119         xfs_extlen_t            extsize)
120 {
121         struct xfs_mount        *mp = ip->i_mount;
122         xfs_extlen_t            align = 0;
123
124         if (!XFS_IS_REALTIME_INODE(ip)) {
125                 /*
126                  * Round up the allocation request to a stripe unit
127                  * (m_dalign) boundary if the file size is >= stripe unit
128                  * size, and we are allocating past the allocation eof.
129                  *
130                  * If mounted with the "-o swalloc" option the alignment is
131                  * increased from the strip unit size to the stripe width.
132                  */
133                 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
134                         align = mp->m_swidth;
135                 else if (mp->m_dalign)
136                         align = mp->m_dalign;
137
138                 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
139                         align = 0;
140         }
141
142         /*
143          * Always round up the allocation request to an extent boundary
144          * (when file on a real-time subvolume or has di_extsize hint).
145          */
146         if (extsize) {
147                 if (align)
148                         align = roundup_64(align, extsize);
149                 else
150                         align = extsize;
151         }
152
153         return align;
154 }
155
156 STATIC int
157 xfs_iomap_eof_align_last_fsb(
158         struct xfs_inode        *ip,
159         xfs_extlen_t            extsize,
160         xfs_fileoff_t           *last_fsb)
161 {
162         xfs_extlen_t            align = xfs_eof_alignment(ip, extsize);
163
164         if (align) {
165                 xfs_fileoff_t   new_last_fsb = roundup_64(*last_fsb, align);
166                 int             eof, error;
167
168                 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
169                 if (error)
170                         return error;
171                 if (eof)
172                         *last_fsb = new_last_fsb;
173         }
174         return 0;
175 }
176
177 int
178 xfs_iomap_write_direct(
179         xfs_inode_t     *ip,
180         xfs_off_t       offset,
181         size_t          count,
182         xfs_bmbt_irec_t *imap,
183         int             nmaps)
184 {
185         xfs_mount_t     *mp = ip->i_mount;
186         xfs_fileoff_t   offset_fsb = XFS_B_TO_FSBT(mp, offset);
187         xfs_fileoff_t   last_fsb = xfs_iomap_end_fsb(mp, offset, count);
188         xfs_filblks_t   count_fsb, resaligned;
189         xfs_extlen_t    extsz;
190         int             nimaps;
191         int             quota_flag;
192         int             rt;
193         xfs_trans_t     *tp;
194         uint            qblocks, resblks, resrtextents;
195         int             error;
196         int             lockmode;
197         int             bmapi_flags = XFS_BMAPI_PREALLOC;
198         uint            tflags = 0;
199
200         rt = XFS_IS_REALTIME_INODE(ip);
201         extsz = xfs_get_extsz_hint(ip);
202         lockmode = XFS_ILOCK_SHARED;    /* locked by caller */
203
204         ASSERT(xfs_isilocked(ip, lockmode));
205
206         if ((offset + count) > XFS_ISIZE(ip)) {
207                 /*
208                  * Assert that the in-core extent list is present since this can
209                  * call xfs_iread_extents() and we only have the ilock shared.
210                  * This should be safe because the lock was held around a bmapi
211                  * call in the caller and we only need it to access the in-core
212                  * list.
213                  */
214                 ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
215                                                                 XFS_IFEXTENTS);
216                 error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
217                 if (error)
218                         goto out_unlock;
219         } else {
220                 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
221                         last_fsb = min(last_fsb, (xfs_fileoff_t)
222                                         imap->br_blockcount +
223                                         imap->br_startoff);
224         }
225         count_fsb = last_fsb - offset_fsb;
226         ASSERT(count_fsb > 0);
227         resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
228
229         if (unlikely(rt)) {
230                 resrtextents = qblocks = resaligned;
231                 resrtextents /= mp->m_sb.sb_rextsize;
232                 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
233                 quota_flag = XFS_QMOPT_RES_RTBLKS;
234         } else {
235                 resrtextents = 0;
236                 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
237                 quota_flag = XFS_QMOPT_RES_REGBLKS;
238         }
239
240         /*
241          * Drop the shared lock acquired by the caller, attach the dquot if
242          * necessary and move on to transaction setup.
243          */
244         xfs_iunlock(ip, lockmode);
245         error = xfs_qm_dqattach(ip);
246         if (error)
247                 return error;
248
249         /*
250          * For DAX, we do not allocate unwritten extents, but instead we zero
251          * the block before we commit the transaction.  Ideally we'd like to do
252          * this outside the transaction context, but if we commit and then crash
253          * we may not have zeroed the blocks and this will be exposed on
254          * recovery of the allocation. Hence we must zero before commit.
255          *
256          * Further, if we are mapping unwritten extents here, we need to zero
257          * and convert them to written so that we don't need an unwritten extent
258          * callback for DAX. This also means that we need to be able to dip into
259          * the reserve block pool for bmbt block allocation if there is no space
260          * left but we need to do unwritten extent conversion.
261          */
262         if (IS_DAX(VFS_I(ip))) {
263                 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
264                 if (imap->br_state == XFS_EXT_UNWRITTEN) {
265                         tflags |= XFS_TRANS_RESERVE;
266                         resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
267                 }
268         }
269         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
270                         tflags, &tp);
271         if (error)
272                 return error;
273
274         lockmode = XFS_ILOCK_EXCL;
275         xfs_ilock(ip, lockmode);
276
277         error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
278         if (error)
279                 goto out_trans_cancel;
280
281         xfs_trans_ijoin(tp, ip, 0);
282
283         /*
284          * From this point onwards we overwrite the imap pointer that the
285          * caller gave to us.
286          */
287         nimaps = 1;
288         error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
289                                 bmapi_flags, resblks, imap, &nimaps);
290         if (error)
291                 goto out_res_cancel;
292
293         /*
294          * Complete the transaction
295          */
296         error = xfs_trans_commit(tp);
297         if (error)
298                 goto out_unlock;
299
300         /*
301          * Copy any maps to caller's array and return any error.
302          */
303         if (nimaps == 0) {
304                 error = -ENOSPC;
305                 goto out_unlock;
306         }
307
308         if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
309                 error = xfs_alert_fsblock_zero(ip, imap);
310
311 out_unlock:
312         xfs_iunlock(ip, lockmode);
313         return error;
314
315 out_res_cancel:
316         xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
317 out_trans_cancel:
318         xfs_trans_cancel(tp);
319         goto out_unlock;
320 }
321
322 STATIC bool
323 xfs_quota_need_throttle(
324         struct xfs_inode *ip,
325         int type,
326         xfs_fsblock_t alloc_blocks)
327 {
328         struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
329
330         if (!dq || !xfs_this_quota_on(ip->i_mount, type))
331                 return false;
332
333         /* no hi watermark, no throttle */
334         if (!dq->q_prealloc_hi_wmark)
335                 return false;
336
337         /* under the lo watermark, no throttle */
338         if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
339                 return false;
340
341         return true;
342 }
343
344 STATIC void
345 xfs_quota_calc_throttle(
346         struct xfs_inode *ip,
347         int type,
348         xfs_fsblock_t *qblocks,
349         int *qshift,
350         int64_t *qfreesp)
351 {
352         int64_t freesp;
353         int shift = 0;
354         struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
355
356         /* no dq, or over hi wmark, squash the prealloc completely */
357         if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
358                 *qblocks = 0;
359                 *qfreesp = 0;
360                 return;
361         }
362
363         freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
364         if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
365                 shift = 2;
366                 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
367                         shift += 2;
368                 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
369                         shift += 2;
370         }
371
372         if (freesp < *qfreesp)
373                 *qfreesp = freesp;
374
375         /* only overwrite the throttle values if we are more aggressive */
376         if ((freesp >> shift) < (*qblocks >> *qshift)) {
377                 *qblocks = freesp;
378                 *qshift = shift;
379         }
380 }
381
382 /*
383  * If we are doing a write at the end of the file and there are no allocations
384  * past this one, then extend the allocation out to the file system's write
385  * iosize.
386  *
387  * If we don't have a user specified preallocation size, dynamically increase
388  * the preallocation size as the size of the file grows.  Cap the maximum size
389  * at a single extent or less if the filesystem is near full. The closer the
390  * filesystem is to full, the smaller the maximum prealocation.
391  *
392  * As an exception we don't do any preallocation at all if the file is smaller
393  * than the minimum preallocation and we are using the default dynamic
394  * preallocation scheme, as it is likely this is the only write to the file that
395  * is going to be done.
396  *
397  * We clean up any extra space left over when the file is closed in
398  * xfs_inactive().
399  */
400 STATIC xfs_fsblock_t
401 xfs_iomap_prealloc_size(
402         struct xfs_inode        *ip,
403         int                     whichfork,
404         loff_t                  offset,
405         loff_t                  count,
406         struct xfs_iext_cursor  *icur)
407 {
408         struct xfs_mount        *mp = ip->i_mount;
409         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
410         xfs_fileoff_t           offset_fsb = XFS_B_TO_FSBT(mp, offset);
411         struct xfs_bmbt_irec    prev;
412         int                     shift = 0;
413         int64_t                 freesp;
414         xfs_fsblock_t           qblocks;
415         int                     qshift = 0;
416         xfs_fsblock_t           alloc_blocks = 0;
417
418         if (offset + count <= XFS_ISIZE(ip))
419                 return 0;
420
421         if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
422             (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
423                 return 0;
424
425         /*
426          * If an explicit allocsize is set, the file is small, or we
427          * are writing behind a hole, then use the minimum prealloc:
428          */
429         if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
430             XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
431             !xfs_iext_peek_prev_extent(ifp, icur, &prev) ||
432             prev.br_startoff + prev.br_blockcount < offset_fsb)
433                 return mp->m_writeio_blocks;
434
435         /*
436          * Determine the initial size of the preallocation. We are beyond the
437          * current EOF here, but we need to take into account whether this is
438          * a sparse write or an extending write when determining the
439          * preallocation size.  Hence we need to look up the extent that ends
440          * at the current write offset and use the result to determine the
441          * preallocation size.
442          *
443          * If the extent is a hole, then preallocation is essentially disabled.
444          * Otherwise we take the size of the preceding data extent as the basis
445          * for the preallocation size. If the size of the extent is greater than
446          * half the maximum extent length, then use the current offset as the
447          * basis. This ensures that for large files the preallocation size
448          * always extends to MAXEXTLEN rather than falling short due to things
449          * like stripe unit/width alignment of real extents.
450          */
451         if (prev.br_blockcount <= (MAXEXTLEN >> 1))
452                 alloc_blocks = prev.br_blockcount << 1;
453         else
454                 alloc_blocks = XFS_B_TO_FSB(mp, offset);
455         if (!alloc_blocks)
456                 goto check_writeio;
457         qblocks = alloc_blocks;
458
459         /*
460          * MAXEXTLEN is not a power of two value but we round the prealloc down
461          * to the nearest power of two value after throttling. To prevent the
462          * round down from unconditionally reducing the maximum supported prealloc
463          * size, we round up first, apply appropriate throttling, round down and
464          * cap the value to MAXEXTLEN.
465          */
466         alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
467                                        alloc_blocks);
468
469         freesp = percpu_counter_read_positive(&mp->m_fdblocks);
470         if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
471                 shift = 2;
472                 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
473                         shift++;
474                 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
475                         shift++;
476                 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
477                         shift++;
478                 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
479                         shift++;
480         }
481
482         /*
483          * Check each quota to cap the prealloc size, provide a shift value to
484          * throttle with and adjust amount of available space.
485          */
486         if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
487                 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
488                                         &freesp);
489         if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
490                 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
491                                         &freesp);
492         if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
493                 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
494                                         &freesp);
495
496         /*
497          * The final prealloc size is set to the minimum of free space available
498          * in each of the quotas and the overall filesystem.
499          *
500          * The shift throttle value is set to the maximum value as determined by
501          * the global low free space values and per-quota low free space values.
502          */
503         alloc_blocks = min(alloc_blocks, qblocks);
504         shift = max(shift, qshift);
505
506         if (shift)
507                 alloc_blocks >>= shift;
508         /*
509          * rounddown_pow_of_two() returns an undefined result if we pass in
510          * alloc_blocks = 0.
511          */
512         if (alloc_blocks)
513                 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
514         if (alloc_blocks > MAXEXTLEN)
515                 alloc_blocks = MAXEXTLEN;
516
517         /*
518          * If we are still trying to allocate more space than is
519          * available, squash the prealloc hard. This can happen if we
520          * have a large file on a small filesystem and the above
521          * lowspace thresholds are smaller than MAXEXTLEN.
522          */
523         while (alloc_blocks && alloc_blocks >= freesp)
524                 alloc_blocks >>= 4;
525 check_writeio:
526         if (alloc_blocks < mp->m_writeio_blocks)
527                 alloc_blocks = mp->m_writeio_blocks;
528         trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
529                                       mp->m_writeio_blocks);
530         return alloc_blocks;
531 }
532
533 int
534 xfs_iomap_write_unwritten(
535         xfs_inode_t     *ip,
536         xfs_off_t       offset,
537         xfs_off_t       count,
538         bool            update_isize)
539 {
540         xfs_mount_t     *mp = ip->i_mount;
541         xfs_fileoff_t   offset_fsb;
542         xfs_filblks_t   count_fsb;
543         xfs_filblks_t   numblks_fsb;
544         int             nimaps;
545         xfs_trans_t     *tp;
546         xfs_bmbt_irec_t imap;
547         struct inode    *inode = VFS_I(ip);
548         xfs_fsize_t     i_size;
549         uint            resblks;
550         int             error;
551
552         trace_xfs_unwritten_convert(ip, offset, count);
553
554         offset_fsb = XFS_B_TO_FSBT(mp, offset);
555         count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
556         count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
557
558         /*
559          * Reserve enough blocks in this transaction for two complete extent
560          * btree splits.  We may be converting the middle part of an unwritten
561          * extent and in this case we will insert two new extents in the btree
562          * each of which could cause a full split.
563          *
564          * This reservation amount will be used in the first call to
565          * xfs_bmbt_split() to select an AG with enough space to satisfy the
566          * rest of the operation.
567          */
568         resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
569
570         do {
571                 /*
572                  * Set up a transaction to convert the range of extents
573                  * from unwritten to real. Do allocations in a loop until
574                  * we have covered the range passed in.
575                  *
576                  * Note that we can't risk to recursing back into the filesystem
577                  * here as we might be asked to write out the same inode that we
578                  * complete here and might deadlock on the iolock.
579                  */
580                 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
581                                 XFS_TRANS_RESERVE, &tp);
582                 if (error)
583                         return error;
584
585                 xfs_ilock(ip, XFS_ILOCK_EXCL);
586                 xfs_trans_ijoin(tp, ip, 0);
587
588                 /*
589                  * Modify the unwritten extent state of the buffer.
590                  */
591                 nimaps = 1;
592                 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
593                                         XFS_BMAPI_CONVERT, resblks, &imap,
594                                         &nimaps);
595                 if (error)
596                         goto error_on_bmapi_transaction;
597
598                 /*
599                  * Log the updated inode size as we go.  We have to be careful
600                  * to only log it up to the actual write offset if it is
601                  * halfway into a block.
602                  */
603                 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
604                 if (i_size > offset + count)
605                         i_size = offset + count;
606                 if (update_isize && i_size > i_size_read(inode))
607                         i_size_write(inode, i_size);
608                 i_size = xfs_new_eof(ip, i_size);
609                 if (i_size) {
610                         ip->i_d.di_size = i_size;
611                         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
612                 }
613
614                 error = xfs_trans_commit(tp);
615                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
616                 if (error)
617                         return error;
618
619                 if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock)))
620                         return xfs_alert_fsblock_zero(ip, &imap);
621
622                 if ((numblks_fsb = imap.br_blockcount) == 0) {
623                         /*
624                          * The numblks_fsb value should always get
625                          * smaller, otherwise the loop is stuck.
626                          */
627                         ASSERT(imap.br_blockcount);
628                         break;
629                 }
630                 offset_fsb += numblks_fsb;
631                 count_fsb -= numblks_fsb;
632         } while (count_fsb > 0);
633
634         return 0;
635
636 error_on_bmapi_transaction:
637         xfs_trans_cancel(tp);
638         xfs_iunlock(ip, XFS_ILOCK_EXCL);
639         return error;
640 }
641
642 static inline bool
643 imap_needs_alloc(
644         struct inode            *inode,
645         unsigned                flags,
646         struct xfs_bmbt_irec    *imap,
647         int                     nimaps)
648 {
649         /* don't allocate blocks when just zeroing */
650         if (flags & IOMAP_ZERO)
651                 return false;
652         if (!nimaps ||
653             imap->br_startblock == HOLESTARTBLOCK ||
654             imap->br_startblock == DELAYSTARTBLOCK)
655                 return true;
656         /* we convert unwritten extents before copying the data for DAX */
657         if (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN)
658                 return true;
659         return false;
660 }
661
662 static inline bool
663 imap_needs_cow(
664         struct xfs_inode        *ip,
665         unsigned int            flags,
666         struct xfs_bmbt_irec    *imap,
667         int                     nimaps)
668 {
669         if (!xfs_is_cow_inode(ip))
670                 return false;
671
672         /* when zeroing we don't have to COW holes or unwritten extents */
673         if (flags & IOMAP_ZERO) {
674                 if (!nimaps ||
675                     imap->br_startblock == HOLESTARTBLOCK ||
676                     imap->br_state == XFS_EXT_UNWRITTEN)
677                         return false;
678         }
679
680         return true;
681 }
682
683 static int
684 xfs_ilock_for_iomap(
685         struct xfs_inode        *ip,
686         unsigned                flags,
687         unsigned                *lockmode)
688 {
689         unsigned                mode = XFS_ILOCK_SHARED;
690         bool                    is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
691
692         /*
693          * COW writes may allocate delalloc space or convert unwritten COW
694          * extents, so we need to make sure to take the lock exclusively here.
695          */
696         if (xfs_is_cow_inode(ip) && is_write) {
697                 /*
698                  * FIXME: It could still overwrite on unshared extents and not
699                  * need allocation.
700                  */
701                 if (flags & IOMAP_NOWAIT)
702                         return -EAGAIN;
703                 mode = XFS_ILOCK_EXCL;
704         }
705
706         /*
707          * Extents not yet cached requires exclusive access, don't block.  This
708          * is an opencoded xfs_ilock_data_map_shared() call but with
709          * non-blocking behaviour.
710          */
711         if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
712                 if (flags & IOMAP_NOWAIT)
713                         return -EAGAIN;
714                 mode = XFS_ILOCK_EXCL;
715         }
716
717 relock:
718         if (flags & IOMAP_NOWAIT) {
719                 if (!xfs_ilock_nowait(ip, mode))
720                         return -EAGAIN;
721         } else {
722                 xfs_ilock(ip, mode);
723         }
724
725         /*
726          * The reflink iflag could have changed since the earlier unlocked
727          * check, so if we got ILOCK_SHARED for a write and but we're now a
728          * reflink inode we have to switch to ILOCK_EXCL and relock.
729          */
730         if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_cow_inode(ip)) {
731                 xfs_iunlock(ip, mode);
732                 mode = XFS_ILOCK_EXCL;
733                 goto relock;
734         }
735
736         *lockmode = mode;
737         return 0;
738 }
739
740 static int
741 xfs_direct_write_iomap_begin(
742         struct inode            *inode,
743         loff_t                  offset,
744         loff_t                  length,
745         unsigned                flags,
746         struct iomap            *iomap,
747         struct iomap            *srcmap)
748 {
749         struct xfs_inode        *ip = XFS_I(inode);
750         struct xfs_mount        *mp = ip->i_mount;
751         struct xfs_bmbt_irec    imap, cmap;
752         xfs_fileoff_t           offset_fsb = XFS_B_TO_FSBT(mp, offset);
753         xfs_fileoff_t           end_fsb = xfs_iomap_end_fsb(mp, offset, length);
754         int                     nimaps = 1, error = 0;
755         bool                    shared = false;
756         u16                     iomap_flags = 0;
757         unsigned                lockmode;
758
759         ASSERT(flags & (IOMAP_WRITE | IOMAP_ZERO));
760
761         if (XFS_FORCED_SHUTDOWN(mp))
762                 return -EIO;
763
764         /*
765          * Writes that span EOF might trigger an IO size update on completion,
766          * so consider them to be dirty for the purposes of O_DSYNC even if
767          * there is no other metadata changes pending or have been made here.
768          */
769         if (offset + length > i_size_read(inode))
770                 iomap_flags |= IOMAP_F_DIRTY;
771
772         /*
773          * Lock the inode in the manner required for the specified operation and
774          * check for as many conditions that would result in blocking as
775          * possible. This removes most of the non-blocking checks from the
776          * mapping code below.
777          */
778         error = xfs_ilock_for_iomap(ip, flags, &lockmode);
779         if (error)
780                 return error;
781
782         error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
783                                &nimaps, 0);
784         if (error)
785                 goto out_unlock;
786
787         /*
788          * Break shared extents if necessary. Checks for non-blocking IO have
789          * been done up front, so we don't need to do them here.
790          */
791         if (imap_needs_cow(ip, flags, &imap, nimaps)) {
792                 /* may drop and re-acquire the ilock */
793                 error = xfs_reflink_allocate_cow(ip, &imap, &cmap, &shared,
794                                 &lockmode, flags & IOMAP_DIRECT);
795                 if (error)
796                         goto out_unlock;
797                 if (shared)
798                         goto out_found_cow;
799                 end_fsb = imap.br_startoff + imap.br_blockcount;
800                 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
801         }
802
803         if (imap_needs_alloc(inode, flags, &imap, nimaps))
804                 goto allocate_blocks;
805
806         xfs_iunlock(ip, lockmode);
807         trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
808         return xfs_bmbt_to_iomap(ip, iomap, &imap, iomap_flags);
809
810 allocate_blocks:
811         error = -EAGAIN;
812         if (flags & IOMAP_NOWAIT)
813                 goto out_unlock;
814
815         /*
816          * We cap the maximum length we map to a sane size  to keep the chunks
817          * of work done where somewhat symmetric with the work writeback does.
818          * This is a completely arbitrary number pulled out of thin air as a
819          * best guess for initial testing.
820          *
821          * Note that the values needs to be less than 32-bits wide until the
822          * lower level functions are updated.
823          */
824         length = min_t(loff_t, length, 1024 * PAGE_SIZE);
825
826         /*
827          * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
828          * return.
829          */
830         if (lockmode == XFS_ILOCK_EXCL)
831                 xfs_ilock_demote(ip, lockmode);
832         error = xfs_iomap_write_direct(ip, offset, length, &imap, nimaps);
833         if (error)
834                 return error;
835
836         trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
837         return xfs_bmbt_to_iomap(ip, iomap, &imap, iomap_flags | IOMAP_F_NEW);
838
839 out_found_cow:
840         xfs_iunlock(ip, lockmode);
841         length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
842         trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
843         if (imap.br_startblock != HOLESTARTBLOCK) {
844                 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, 0);
845                 if (error)
846                         return error;
847         }
848         return xfs_bmbt_to_iomap(ip, iomap, &cmap, IOMAP_F_SHARED);
849
850 out_unlock:
851         xfs_iunlock(ip, lockmode);
852         return error;
853 }
854
855 const struct iomap_ops xfs_direct_write_iomap_ops = {
856         .iomap_begin            = xfs_direct_write_iomap_begin,
857 };
858
859 static int
860 xfs_buffered_write_iomap_begin(
861         struct inode            *inode,
862         loff_t                  offset,
863         loff_t                  count,
864         unsigned                flags,
865         struct iomap            *iomap,
866         struct iomap            *srcmap)
867 {
868         struct xfs_inode        *ip = XFS_I(inode);
869         struct xfs_mount        *mp = ip->i_mount;
870         xfs_fileoff_t           offset_fsb = XFS_B_TO_FSBT(mp, offset);
871         xfs_fileoff_t           end_fsb = xfs_iomap_end_fsb(mp, offset, count);
872         struct xfs_bmbt_irec    imap, cmap;
873         struct xfs_iext_cursor  icur, ccur;
874         xfs_fsblock_t           prealloc_blocks = 0;
875         bool                    eof = false, cow_eof = false, shared = false;
876         int                     allocfork = XFS_DATA_FORK;
877         int                     error = 0;
878
879         /* we can't use delayed allocations when using extent size hints */
880         if (xfs_get_extsz_hint(ip))
881                 return xfs_direct_write_iomap_begin(inode, offset, count,
882                                 flags, iomap, srcmap);
883
884         ASSERT(!XFS_IS_REALTIME_INODE(ip));
885
886         xfs_ilock(ip, XFS_ILOCK_EXCL);
887
888         if (unlikely(XFS_TEST_ERROR(
889             (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
890              XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
891              mp, XFS_ERRTAG_BMAPIFORMAT))) {
892                 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
893                 error = -EFSCORRUPTED;
894                 goto out_unlock;
895         }
896
897         XFS_STATS_INC(mp, xs_blk_mapw);
898
899         if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
900                 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
901                 if (error)
902                         goto out_unlock;
903         }
904
905         /*
906          * Search the data fork fork first to look up our source mapping.  We
907          * always need the data fork map, as we have to return it to the
908          * iomap code so that the higher level write code can read data in to
909          * perform read-modify-write cycles for unaligned writes.
910          */
911         eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
912         if (eof)
913                 imap.br_startoff = end_fsb; /* fake hole until the end */
914
915         /* We never need to allocate blocks for zeroing a hole. */
916         if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) {
917                 xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
918                 goto out_unlock;
919         }
920
921         /*
922          * Search the COW fork extent list even if we did not find a data fork
923          * extent.  This serves two purposes: first this implements the
924          * speculative preallocation using cowextsize, so that we also unshare
925          * block adjacent to shared blocks instead of just the shared blocks
926          * themselves.  Second the lookup in the extent list is generally faster
927          * than going out to the shared extent tree.
928          */
929         if (xfs_is_cow_inode(ip)) {
930                 if (!ip->i_cowfp) {
931                         ASSERT(!xfs_is_reflink_inode(ip));
932                         xfs_ifork_init_cow(ip);
933                 }
934                 cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
935                                 &ccur, &cmap);
936                 if (!cow_eof && cmap.br_startoff <= offset_fsb) {
937                         trace_xfs_reflink_cow_found(ip, &cmap);
938                         goto found_cow;
939                 }
940         }
941
942         if (imap.br_startoff <= offset_fsb) {
943                 /*
944                  * For reflink files we may need a delalloc reservation when
945                  * overwriting shared extents.   This includes zeroing of
946                  * existing extents that contain data.
947                  */
948                 if (!xfs_is_cow_inode(ip) ||
949                     ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
950                         trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
951                                         &imap);
952                         goto found_imap;
953                 }
954
955                 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
956
957                 /* Trim the mapping to the nearest shared extent boundary. */
958                 error = xfs_inode_need_cow(ip, &imap, &shared);
959                 if (error)
960                         goto out_unlock;
961
962                 /* Not shared?  Just report the (potentially capped) extent. */
963                 if (!shared) {
964                         trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
965                                         &imap);
966                         goto found_imap;
967                 }
968
969                 /*
970                  * Fork all the shared blocks from our write offset until the
971                  * end of the extent.
972                  */
973                 allocfork = XFS_COW_FORK;
974                 end_fsb = imap.br_startoff + imap.br_blockcount;
975         } else {
976                 /*
977                  * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
978                  * pages to keep the chunks of work done where somewhat
979                  * symmetric with the work writeback does.  This is a completely
980                  * arbitrary number pulled out of thin air.
981                  *
982                  * Note that the values needs to be less than 32-bits wide until
983                  * the lower level functions are updated.
984                  */
985                 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
986                 end_fsb = xfs_iomap_end_fsb(mp, offset, count);
987
988                 if (xfs_is_always_cow_inode(ip))
989                         allocfork = XFS_COW_FORK;
990         }
991
992         error = xfs_qm_dqattach_locked(ip, false);
993         if (error)
994                 goto out_unlock;
995
996         if (eof) {
997                 prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork, offset,
998                                 count, &icur);
999                 if (prealloc_blocks) {
1000                         xfs_extlen_t    align;
1001                         xfs_off_t       end_offset;
1002                         xfs_fileoff_t   p_end_fsb;
1003
1004                         end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
1005                         p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
1006                                         prealloc_blocks;
1007
1008                         align = xfs_eof_alignment(ip, 0);
1009                         if (align)
1010                                 p_end_fsb = roundup_64(p_end_fsb, align);
1011
1012                         p_end_fsb = min(p_end_fsb,
1013                                 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
1014                         ASSERT(p_end_fsb > offset_fsb);
1015                         prealloc_blocks = p_end_fsb - end_fsb;
1016                 }
1017         }
1018
1019 retry:
1020         error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
1021                         end_fsb - offset_fsb, prealloc_blocks,
1022                         allocfork == XFS_DATA_FORK ? &imap : &cmap,
1023                         allocfork == XFS_DATA_FORK ? &icur : &ccur,
1024                         allocfork == XFS_DATA_FORK ? eof : cow_eof);
1025         switch (error) {
1026         case 0:
1027                 break;
1028         case -ENOSPC:
1029         case -EDQUOT:
1030                 /* retry without any preallocation */
1031                 trace_xfs_delalloc_enospc(ip, offset, count);
1032                 if (prealloc_blocks) {
1033                         prealloc_blocks = 0;
1034                         goto retry;
1035                 }
1036                 /*FALLTHRU*/
1037         default:
1038                 goto out_unlock;
1039         }
1040
1041         if (allocfork == XFS_COW_FORK) {
1042                 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap);
1043                 goto found_cow;
1044         }
1045
1046         /*
1047          * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
1048          * them out if the write happens to fail.
1049          */
1050         xfs_iunlock(ip, XFS_ILOCK_EXCL);
1051         trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap);
1052         return xfs_bmbt_to_iomap(ip, iomap, &imap, IOMAP_F_NEW);
1053
1054 found_imap:
1055         xfs_iunlock(ip, XFS_ILOCK_EXCL);
1056         return xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1057
1058 found_cow:
1059         xfs_iunlock(ip, XFS_ILOCK_EXCL);
1060         if (imap.br_startoff <= offset_fsb) {
1061                 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, 0);
1062                 if (error)
1063                         return error;
1064         } else {
1065                 xfs_trim_extent(&cmap, offset_fsb,
1066                                 imap.br_startoff - offset_fsb);
1067         }
1068         return xfs_bmbt_to_iomap(ip, iomap, &cmap, IOMAP_F_SHARED);
1069
1070 out_unlock:
1071         xfs_iunlock(ip, XFS_ILOCK_EXCL);
1072         return error;
1073 }
1074
1075 static int
1076 xfs_buffered_write_iomap_end(
1077         struct inode            *inode,
1078         loff_t                  offset,
1079         loff_t                  length,
1080         ssize_t                 written,
1081         unsigned                flags,
1082         struct iomap            *iomap)
1083 {
1084         struct xfs_inode        *ip = XFS_I(inode);
1085         struct xfs_mount        *mp = ip->i_mount;
1086         xfs_fileoff_t           start_fsb;
1087         xfs_fileoff_t           end_fsb;
1088         int                     error = 0;
1089
1090         if (iomap->type != IOMAP_DELALLOC)
1091                 return 0;
1092
1093         /*
1094          * Behave as if the write failed if drop writes is enabled. Set the NEW
1095          * flag to force delalloc cleanup.
1096          */
1097         if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
1098                 iomap->flags |= IOMAP_F_NEW;
1099                 written = 0;
1100         }
1101
1102         /*
1103          * start_fsb refers to the first unused block after a short write. If
1104          * nothing was written, round offset down to point at the first block in
1105          * the range.
1106          */
1107         if (unlikely(!written))
1108                 start_fsb = XFS_B_TO_FSBT(mp, offset);
1109         else
1110                 start_fsb = XFS_B_TO_FSB(mp, offset + written);
1111         end_fsb = XFS_B_TO_FSB(mp, offset + length);
1112
1113         /*
1114          * Trim delalloc blocks if they were allocated by this write and we
1115          * didn't manage to write the whole range.
1116          *
1117          * We don't need to care about racing delalloc as we hold i_mutex
1118          * across the reserve/allocate/unreserve calls. If there are delalloc
1119          * blocks in the range, they are ours.
1120          */
1121         if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
1122                 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1123                                          XFS_FSB_TO_B(mp, end_fsb) - 1);
1124
1125                 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1126                                                end_fsb - start_fsb);
1127                 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1128                         xfs_alert(mp, "%s: unable to clean up ino %lld",
1129                                 __func__, ip->i_ino);
1130                         return error;
1131                 }
1132         }
1133
1134         return 0;
1135 }
1136
1137 const struct iomap_ops xfs_buffered_write_iomap_ops = {
1138         .iomap_begin            = xfs_buffered_write_iomap_begin,
1139         .iomap_end              = xfs_buffered_write_iomap_end,
1140 };
1141
1142 static int
1143 xfs_read_iomap_begin(
1144         struct inode            *inode,
1145         loff_t                  offset,
1146         loff_t                  length,
1147         unsigned                flags,
1148         struct iomap            *iomap,
1149         struct iomap            *srcmap)
1150 {
1151         struct xfs_inode        *ip = XFS_I(inode);
1152         struct xfs_mount        *mp = ip->i_mount;
1153         struct xfs_bmbt_irec    imap;
1154         xfs_fileoff_t           offset_fsb = XFS_B_TO_FSBT(mp, offset);
1155         xfs_fileoff_t           end_fsb = xfs_iomap_end_fsb(mp, offset, length);
1156         int                     nimaps = 1, error = 0;
1157         bool                    shared = false;
1158         unsigned                lockmode;
1159
1160         ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO)));
1161
1162         if (XFS_FORCED_SHUTDOWN(mp))
1163                 return -EIO;
1164
1165         error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1166         if (error)
1167                 return error;
1168         error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1169                                &nimaps, 0);
1170         if (!error && (flags & IOMAP_REPORT))
1171                 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
1172         xfs_iunlock(ip, lockmode);
1173
1174         if (error)
1175                 return error;
1176         trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
1177         return xfs_bmbt_to_iomap(ip, iomap, &imap, shared ? IOMAP_F_SHARED : 0);
1178 }
1179
1180 const struct iomap_ops xfs_read_iomap_ops = {
1181         .iomap_begin            = xfs_read_iomap_begin,
1182 };
1183
1184 static int
1185 xfs_seek_iomap_begin(
1186         struct inode            *inode,
1187         loff_t                  offset,
1188         loff_t                  length,
1189         unsigned                flags,
1190         struct iomap            *iomap,
1191         struct iomap            *srcmap)
1192 {
1193         struct xfs_inode        *ip = XFS_I(inode);
1194         struct xfs_mount        *mp = ip->i_mount;
1195         xfs_fileoff_t           offset_fsb = XFS_B_TO_FSBT(mp, offset);
1196         xfs_fileoff_t           end_fsb = XFS_B_TO_FSB(mp, offset + length);
1197         xfs_fileoff_t           cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
1198         struct xfs_iext_cursor  icur;
1199         struct xfs_bmbt_irec    imap, cmap;
1200         int                     error = 0;
1201         unsigned                lockmode;
1202
1203         if (XFS_FORCED_SHUTDOWN(mp))
1204                 return -EIO;
1205
1206         lockmode = xfs_ilock_data_map_shared(ip);
1207         if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
1208                 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1209                 if (error)
1210                         goto out_unlock;
1211         }
1212
1213         if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
1214                 /*
1215                  * If we found a data extent we are done.
1216                  */
1217                 if (imap.br_startoff <= offset_fsb)
1218                         goto done;
1219                 data_fsb = imap.br_startoff;
1220         } else {
1221                 /*
1222                  * Fake a hole until the end of the file.
1223                  */
1224                 data_fsb = xfs_iomap_end_fsb(mp, offset, length);
1225         }
1226
1227         /*
1228          * If a COW fork extent covers the hole, report it - capped to the next
1229          * data fork extent:
1230          */
1231         if (xfs_inode_has_cow_data(ip) &&
1232             xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1233                 cow_fsb = cmap.br_startoff;
1234         if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
1235                 if (data_fsb < cow_fsb + cmap.br_blockcount)
1236                         end_fsb = min(end_fsb, data_fsb);
1237                 xfs_trim_extent(&cmap, offset_fsb, end_fsb);
1238                 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, IOMAP_F_SHARED);
1239                 /*
1240                  * This is a COW extent, so we must probe the page cache
1241                  * because there could be dirty page cache being backed
1242                  * by this extent.
1243                  */
1244                 iomap->type = IOMAP_UNWRITTEN;
1245                 goto out_unlock;
1246         }
1247
1248         /*
1249          * Else report a hole, capped to the next found data or COW extent.
1250          */
1251         if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
1252                 imap.br_blockcount = cow_fsb - offset_fsb;
1253         else
1254                 imap.br_blockcount = data_fsb - offset_fsb;
1255         imap.br_startoff = offset_fsb;
1256         imap.br_startblock = HOLESTARTBLOCK;
1257         imap.br_state = XFS_EXT_NORM;
1258 done:
1259         xfs_trim_extent(&imap, offset_fsb, end_fsb);
1260         error = xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1261 out_unlock:
1262         xfs_iunlock(ip, lockmode);
1263         return error;
1264 }
1265
1266 const struct iomap_ops xfs_seek_iomap_ops = {
1267         .iomap_begin            = xfs_seek_iomap_begin,
1268 };
1269
1270 static int
1271 xfs_xattr_iomap_begin(
1272         struct inode            *inode,
1273         loff_t                  offset,
1274         loff_t                  length,
1275         unsigned                flags,
1276         struct iomap            *iomap,
1277         struct iomap            *srcmap)
1278 {
1279         struct xfs_inode        *ip = XFS_I(inode);
1280         struct xfs_mount        *mp = ip->i_mount;
1281         xfs_fileoff_t           offset_fsb = XFS_B_TO_FSBT(mp, offset);
1282         xfs_fileoff_t           end_fsb = XFS_B_TO_FSB(mp, offset + length);
1283         struct xfs_bmbt_irec    imap;
1284         int                     nimaps = 1, error = 0;
1285         unsigned                lockmode;
1286
1287         if (XFS_FORCED_SHUTDOWN(mp))
1288                 return -EIO;
1289
1290         lockmode = xfs_ilock_attr_map_shared(ip);
1291
1292         /* if there are no attribute fork or extents, return ENOENT */
1293         if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
1294                 error = -ENOENT;
1295                 goto out_unlock;
1296         }
1297
1298         ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1299         error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1300                                &nimaps, XFS_BMAPI_ATTRFORK);
1301 out_unlock:
1302         xfs_iunlock(ip, lockmode);
1303
1304         if (error)
1305                 return error;
1306         ASSERT(nimaps);
1307         return xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1308 }
1309
1310 const struct iomap_ops xfs_xattr_iomap_ops = {
1311         .iomap_begin            = xfs_xattr_iomap_begin,
1312 };