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