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