xref: /openbmc/linux/fs/xfs/xfs_iomap.c (revision 5e012745)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4  * Copyright (c) 2016-2018 Christoph Hellwig.
5  * All Rights Reserved.
6  */
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_btree.h"
16 #include "xfs_bmap_btree.h"
17 #include "xfs_bmap.h"
18 #include "xfs_bmap_util.h"
19 #include "xfs_errortag.h"
20 #include "xfs_error.h"
21 #include "xfs_trans.h"
22 #include "xfs_trans_space.h"
23 #include "xfs_inode_item.h"
24 #include "xfs_iomap.h"
25 #include "xfs_trace.h"
26 #include "xfs_quota.h"
27 #include "xfs_dquot_item.h"
28 #include "xfs_dquot.h"
29 #include "xfs_reflink.h"
30 
31 
32 #define XFS_WRITEIO_ALIGN(mp,off)	(((off) >> mp->m_writeio_log) \
33 						<< mp->m_writeio_log)
34 
35 static int
36 xfs_alert_fsblock_zero(
37 	xfs_inode_t	*ip,
38 	xfs_bmbt_irec_t	*imap)
39 {
40 	xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
41 			"Access to block zero in inode %llu "
42 			"start_block: %llx start_off: %llx "
43 			"blkcnt: %llx extent-state: %x",
44 		(unsigned long long)ip->i_ino,
45 		(unsigned long long)imap->br_startblock,
46 		(unsigned long long)imap->br_startoff,
47 		(unsigned long long)imap->br_blockcount,
48 		imap->br_state);
49 	return -EFSCORRUPTED;
50 }
51 
52 int
53 xfs_bmbt_to_iomap(
54 	struct xfs_inode	*ip,
55 	struct iomap		*iomap,
56 	struct xfs_bmbt_irec	*imap,
57 	bool			shared)
58 {
59 	struct xfs_mount	*mp = ip->i_mount;
60 
61 	if (unlikely(!imap->br_startblock && !XFS_IS_REALTIME_INODE(ip)))
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
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
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
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
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 (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
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
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
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
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
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
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 	do {
769 		/*
770 		 * Set up a transaction to convert the range of extents
771 		 * from unwritten to real. Do allocations in a loop until
772 		 * we have covered the range passed in.
773 		 *
774 		 * Note that we can't risk to recursing back into the filesystem
775 		 * here as we might be asked to write out the same inode that we
776 		 * complete here and might deadlock on the iolock.
777 		 */
778 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
779 				XFS_TRANS_RESERVE, &tp);
780 		if (error)
781 			return error;
782 
783 		xfs_ilock(ip, XFS_ILOCK_EXCL);
784 		xfs_trans_ijoin(tp, ip, 0);
785 
786 		/*
787 		 * Modify the unwritten extent state of the buffer.
788 		 */
789 		nimaps = 1;
790 		error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
791 					XFS_BMAPI_CONVERT, resblks, &imap,
792 					&nimaps);
793 		if (error)
794 			goto error_on_bmapi_transaction;
795 
796 		/*
797 		 * Log the updated inode size as we go.  We have to be careful
798 		 * to only log it up to the actual write offset if it is
799 		 * halfway into a block.
800 		 */
801 		i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
802 		if (i_size > offset + count)
803 			i_size = offset + count;
804 		if (update_isize && i_size > i_size_read(inode))
805 			i_size_write(inode, i_size);
806 		i_size = xfs_new_eof(ip, i_size);
807 		if (i_size) {
808 			ip->i_d.di_size = i_size;
809 			xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
810 		}
811 
812 		error = xfs_trans_commit(tp);
813 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
814 		if (error)
815 			return error;
816 
817 		if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
818 			return xfs_alert_fsblock_zero(ip, &imap);
819 
820 		if ((numblks_fsb = imap.br_blockcount) == 0) {
821 			/*
822 			 * The numblks_fsb value should always get
823 			 * smaller, otherwise the loop is stuck.
824 			 */
825 			ASSERT(imap.br_blockcount);
826 			break;
827 		}
828 		offset_fsb += numblks_fsb;
829 		count_fsb -= numblks_fsb;
830 	} while (count_fsb > 0);
831 
832 	return 0;
833 
834 error_on_bmapi_transaction:
835 	xfs_trans_cancel(tp);
836 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
837 	return error;
838 }
839 
840 static inline bool
841 imap_needs_alloc(
842 	struct inode		*inode,
843 	struct xfs_bmbt_irec	*imap,
844 	int			nimaps)
845 {
846 	return !nimaps ||
847 		imap->br_startblock == HOLESTARTBLOCK ||
848 		imap->br_startblock == DELAYSTARTBLOCK ||
849 		(IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN);
850 }
851 
852 static inline bool
853 needs_cow_for_zeroing(
854 	struct xfs_bmbt_irec	*imap,
855 	int			nimaps)
856 {
857 	return nimaps &&
858 		imap->br_startblock != HOLESTARTBLOCK &&
859 		imap->br_state != XFS_EXT_UNWRITTEN;
860 }
861 
862 static int
863 xfs_ilock_for_iomap(
864 	struct xfs_inode	*ip,
865 	unsigned		flags,
866 	unsigned		*lockmode)
867 {
868 	unsigned		mode = XFS_ILOCK_SHARED;
869 	bool			is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
870 
871 	/*
872 	 * COW writes may allocate delalloc space or convert unwritten COW
873 	 * extents, so we need to make sure to take the lock exclusively here.
874 	 */
875 	if (xfs_is_cow_inode(ip) && is_write) {
876 		/*
877 		 * FIXME: It could still overwrite on unshared extents and not
878 		 * need allocation.
879 		 */
880 		if (flags & IOMAP_NOWAIT)
881 			return -EAGAIN;
882 		mode = XFS_ILOCK_EXCL;
883 	}
884 
885 	/*
886 	 * Extents not yet cached requires exclusive access, don't block.  This
887 	 * is an opencoded xfs_ilock_data_map_shared() call but with
888 	 * non-blocking behaviour.
889 	 */
890 	if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
891 		if (flags & IOMAP_NOWAIT)
892 			return -EAGAIN;
893 		mode = XFS_ILOCK_EXCL;
894 	}
895 
896 relock:
897 	if (flags & IOMAP_NOWAIT) {
898 		if (!xfs_ilock_nowait(ip, mode))
899 			return -EAGAIN;
900 	} else {
901 		xfs_ilock(ip, mode);
902 	}
903 
904 	/*
905 	 * The reflink iflag could have changed since the earlier unlocked
906 	 * check, so if we got ILOCK_SHARED for a write and but we're now a
907 	 * reflink inode we have to switch to ILOCK_EXCL and relock.
908 	 */
909 	if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_cow_inode(ip)) {
910 		xfs_iunlock(ip, mode);
911 		mode = XFS_ILOCK_EXCL;
912 		goto relock;
913 	}
914 
915 	*lockmode = mode;
916 	return 0;
917 }
918 
919 static int
920 xfs_file_iomap_begin(
921 	struct inode		*inode,
922 	loff_t			offset,
923 	loff_t			length,
924 	unsigned		flags,
925 	struct iomap		*iomap)
926 {
927 	struct xfs_inode	*ip = XFS_I(inode);
928 	struct xfs_mount	*mp = ip->i_mount;
929 	struct xfs_bmbt_irec	imap;
930 	xfs_fileoff_t		offset_fsb, end_fsb;
931 	int			nimaps = 1, error = 0;
932 	bool			shared = false;
933 	unsigned		lockmode;
934 
935 	if (XFS_FORCED_SHUTDOWN(mp))
936 		return -EIO;
937 
938 	if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && !(flags & IOMAP_DIRECT) &&
939 			!IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
940 		/* Reserve delalloc blocks for regular writeback. */
941 		return xfs_file_iomap_begin_delay(inode, offset, length, flags,
942 				iomap);
943 	}
944 
945 	/*
946 	 * Lock the inode in the manner required for the specified operation and
947 	 * check for as many conditions that would result in blocking as
948 	 * possible. This removes most of the non-blocking checks from the
949 	 * mapping code below.
950 	 */
951 	error = xfs_ilock_for_iomap(ip, flags, &lockmode);
952 	if (error)
953 		return error;
954 
955 	ASSERT(offset <= mp->m_super->s_maxbytes);
956 	if (offset > mp->m_super->s_maxbytes - length)
957 		length = mp->m_super->s_maxbytes - offset;
958 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
959 	end_fsb = XFS_B_TO_FSB(mp, offset + length);
960 
961 	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
962 			       &nimaps, 0);
963 	if (error)
964 		goto out_unlock;
965 
966 	if (flags & IOMAP_REPORT) {
967 		/* Trim the mapping to the nearest shared extent boundary. */
968 		error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
969 		if (error)
970 			goto out_unlock;
971 	}
972 
973 	/* Non-modifying mapping requested, so we are done */
974 	if (!(flags & (IOMAP_WRITE | IOMAP_ZERO)))
975 		goto out_found;
976 
977 	/*
978 	 * Break shared extents if necessary. Checks for non-blocking IO have
979 	 * been done up front, so we don't need to do them here.
980 	 */
981 	if (xfs_is_cow_inode(ip)) {
982 		struct xfs_bmbt_irec	cmap;
983 		bool			directio = (flags & IOMAP_DIRECT);
984 
985 		/* if zeroing doesn't need COW allocation, then we are done. */
986 		if ((flags & IOMAP_ZERO) &&
987 		    !needs_cow_for_zeroing(&imap, nimaps))
988 			goto out_found;
989 
990 		/* may drop and re-acquire the ilock */
991 		cmap = imap;
992 		error = xfs_reflink_allocate_cow(ip, &cmap, &shared, &lockmode,
993 				directio);
994 		if (error)
995 			goto out_unlock;
996 
997 		/*
998 		 * For buffered writes we need to report the address of the
999 		 * previous block (if there was any) so that the higher level
1000 		 * write code can perform read-modify-write operations; we
1001 		 * won't need the CoW fork mapping until writeback.  For direct
1002 		 * I/O, which must be block aligned, we need to report the
1003 		 * newly allocated address.  If the data fork has a hole, copy
1004 		 * the COW fork mapping to avoid allocating to the data fork.
1005 		 */
1006 		if (directio || imap.br_startblock == HOLESTARTBLOCK)
1007 			imap = cmap;
1008 
1009 		end_fsb = imap.br_startoff + imap.br_blockcount;
1010 		length = XFS_FSB_TO_B(mp, end_fsb) - offset;
1011 	}
1012 
1013 	/* Don't need to allocate over holes when doing zeroing operations. */
1014 	if (flags & IOMAP_ZERO)
1015 		goto out_found;
1016 
1017 	if (!imap_needs_alloc(inode, &imap, nimaps))
1018 		goto out_found;
1019 
1020 	/* If nowait is set bail since we are going to make allocations. */
1021 	if (flags & IOMAP_NOWAIT) {
1022 		error = -EAGAIN;
1023 		goto out_unlock;
1024 	}
1025 
1026 	/*
1027 	 * We cap the maximum length we map to a sane size  to keep the chunks
1028 	 * of work done where somewhat symmetric with the work writeback does.
1029 	 * This is a completely arbitrary number pulled out of thin air as a
1030 	 * best guess for initial testing.
1031 	 *
1032 	 * Note that the values needs to be less than 32-bits wide until the
1033 	 * lower level functions are updated.
1034 	 */
1035 	length = min_t(loff_t, length, 1024 * PAGE_SIZE);
1036 
1037 	/*
1038 	 * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
1039 	 * return.
1040 	 */
1041 	if (lockmode == XFS_ILOCK_EXCL)
1042 		xfs_ilock_demote(ip, lockmode);
1043 	error = xfs_iomap_write_direct(ip, offset, length, &imap,
1044 			nimaps);
1045 	if (error)
1046 		return error;
1047 
1048 	iomap->flags |= IOMAP_F_NEW;
1049 	trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
1050 
1051 out_finish:
1052 	return xfs_bmbt_to_iomap(ip, iomap, &imap, shared);
1053 
1054 out_found:
1055 	ASSERT(nimaps);
1056 	xfs_iunlock(ip, lockmode);
1057 	trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
1058 	goto out_finish;
1059 
1060 out_unlock:
1061 	xfs_iunlock(ip, lockmode);
1062 	return error;
1063 }
1064 
1065 static int
1066 xfs_file_iomap_end_delalloc(
1067 	struct xfs_inode	*ip,
1068 	loff_t			offset,
1069 	loff_t			length,
1070 	ssize_t			written,
1071 	struct iomap		*iomap)
1072 {
1073 	struct xfs_mount	*mp = ip->i_mount;
1074 	xfs_fileoff_t		start_fsb;
1075 	xfs_fileoff_t		end_fsb;
1076 	int			error = 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 static int
1123 xfs_file_iomap_end(
1124 	struct inode		*inode,
1125 	loff_t			offset,
1126 	loff_t			length,
1127 	ssize_t			written,
1128 	unsigned		flags,
1129 	struct iomap		*iomap)
1130 {
1131 	if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
1132 		return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
1133 				length, written, iomap);
1134 	return 0;
1135 }
1136 
1137 const struct iomap_ops xfs_iomap_ops = {
1138 	.iomap_begin		= xfs_file_iomap_begin,
1139 	.iomap_end		= xfs_file_iomap_end,
1140 };
1141 
1142 static int
1143 xfs_seek_iomap_begin(
1144 	struct inode		*inode,
1145 	loff_t			offset,
1146 	loff_t			length,
1147 	unsigned		flags,
1148 	struct iomap		*iomap)
1149 {
1150 	struct xfs_inode	*ip = XFS_I(inode);
1151 	struct xfs_mount	*mp = ip->i_mount;
1152 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
1153 	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, offset + length);
1154 	xfs_fileoff_t		cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
1155 	struct xfs_iext_cursor	icur;
1156 	struct xfs_bmbt_irec	imap, cmap;
1157 	int			error = 0;
1158 	unsigned		lockmode;
1159 
1160 	if (XFS_FORCED_SHUTDOWN(mp))
1161 		return -EIO;
1162 
1163 	lockmode = xfs_ilock_data_map_shared(ip);
1164 	if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
1165 		error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1166 		if (error)
1167 			goto out_unlock;
1168 	}
1169 
1170 	if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
1171 		/*
1172 		 * If we found a data extent we are done.
1173 		 */
1174 		if (imap.br_startoff <= offset_fsb)
1175 			goto done;
1176 		data_fsb = imap.br_startoff;
1177 	} else {
1178 		/*
1179 		 * Fake a hole until the end of the file.
1180 		 */
1181 		data_fsb = min(XFS_B_TO_FSB(mp, offset + length),
1182 			       XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
1183 	}
1184 
1185 	/*
1186 	 * If a COW fork extent covers the hole, report it - capped to the next
1187 	 * data fork extent:
1188 	 */
1189 	if (xfs_inode_has_cow_data(ip) &&
1190 	    xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1191 		cow_fsb = cmap.br_startoff;
1192 	if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
1193 		if (data_fsb < cow_fsb + cmap.br_blockcount)
1194 			end_fsb = min(end_fsb, data_fsb);
1195 		xfs_trim_extent(&cmap, offset_fsb, end_fsb);
1196 		error = xfs_bmbt_to_iomap(ip, iomap, &cmap, true);
1197 		/*
1198 		 * This is a COW extent, so we must probe the page cache
1199 		 * because there could be dirty page cache being backed
1200 		 * by this extent.
1201 		 */
1202 		iomap->type = IOMAP_UNWRITTEN;
1203 		goto out_unlock;
1204 	}
1205 
1206 	/*
1207 	 * Else report a hole, capped to the next found data or COW extent.
1208 	 */
1209 	if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
1210 		imap.br_blockcount = cow_fsb - offset_fsb;
1211 	else
1212 		imap.br_blockcount = data_fsb - offset_fsb;
1213 	imap.br_startoff = offset_fsb;
1214 	imap.br_startblock = HOLESTARTBLOCK;
1215 	imap.br_state = XFS_EXT_NORM;
1216 done:
1217 	xfs_trim_extent(&imap, offset_fsb, end_fsb);
1218 	error = xfs_bmbt_to_iomap(ip, iomap, &imap, false);
1219 out_unlock:
1220 	xfs_iunlock(ip, lockmode);
1221 	return error;
1222 }
1223 
1224 const struct iomap_ops xfs_seek_iomap_ops = {
1225 	.iomap_begin		= xfs_seek_iomap_begin,
1226 };
1227 
1228 static int
1229 xfs_xattr_iomap_begin(
1230 	struct inode		*inode,
1231 	loff_t			offset,
1232 	loff_t			length,
1233 	unsigned		flags,
1234 	struct iomap		*iomap)
1235 {
1236 	struct xfs_inode	*ip = XFS_I(inode);
1237 	struct xfs_mount	*mp = ip->i_mount;
1238 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
1239 	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, offset + length);
1240 	struct xfs_bmbt_irec	imap;
1241 	int			nimaps = 1, error = 0;
1242 	unsigned		lockmode;
1243 
1244 	if (XFS_FORCED_SHUTDOWN(mp))
1245 		return -EIO;
1246 
1247 	lockmode = xfs_ilock_attr_map_shared(ip);
1248 
1249 	/* if there are no attribute fork or extents, return ENOENT */
1250 	if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
1251 		error = -ENOENT;
1252 		goto out_unlock;
1253 	}
1254 
1255 	ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1256 	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1257 			       &nimaps, XFS_BMAPI_ATTRFORK);
1258 out_unlock:
1259 	xfs_iunlock(ip, lockmode);
1260 
1261 	if (error)
1262 		return error;
1263 	ASSERT(nimaps);
1264 	return xfs_bmbt_to_iomap(ip, iomap, &imap, false);
1265 }
1266 
1267 const struct iomap_ops xfs_xattr_iomap_ops = {
1268 	.iomap_begin		= xfs_xattr_iomap_begin,
1269 };
1270