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