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