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