xref: /linux/fs/xfs/xfs_iomap.c (revision e445fba2d76369d72b497ecadf6b9787930693d9)
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_rtgroup.h"
28 #include "xfs_dquot_item.h"
29 #include "xfs_dquot.h"
30 #include "xfs_reflink.h"
31 #include "xfs_health.h"
32 #include "xfs_rtbitmap.h"
33 #include "xfs_icache.h"
34 #include "xfs_zone_alloc.h"
35 
36 #define XFS_ALLOC_ALIGN(mp, off) \
37 	(((off) >> mp->m_allocsize_log) << mp->m_allocsize_log)
38 
39 static int
xfs_alert_fsblock_zero(xfs_inode_t * ip,xfs_bmbt_irec_t * imap)40 xfs_alert_fsblock_zero(
41 	xfs_inode_t	*ip,
42 	xfs_bmbt_irec_t	*imap)
43 {
44 	xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
45 			"Access to block zero in inode %llu "
46 			"start_block: %llx start_off: %llx "
47 			"blkcnt: %llx extent-state: %x",
48 		(unsigned long long)ip->i_ino,
49 		(unsigned long long)imap->br_startblock,
50 		(unsigned long long)imap->br_startoff,
51 		(unsigned long long)imap->br_blockcount,
52 		imap->br_state);
53 	xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
54 	return -EFSCORRUPTED;
55 }
56 
57 u64
xfs_iomap_inode_sequence(struct xfs_inode * ip,u16 iomap_flags)58 xfs_iomap_inode_sequence(
59 	struct xfs_inode	*ip,
60 	u16			iomap_flags)
61 {
62 	u64			cookie = 0;
63 
64 	if (iomap_flags & IOMAP_F_XATTR)
65 		return READ_ONCE(ip->i_af.if_seq);
66 	if ((iomap_flags & IOMAP_F_SHARED) && ip->i_cowfp)
67 		cookie = (u64)READ_ONCE(ip->i_cowfp->if_seq) << 32;
68 	return cookie | READ_ONCE(ip->i_df.if_seq);
69 }
70 
71 /*
72  * Check that the iomap passed to us is still valid for the given offset and
73  * length.
74  */
75 static bool
xfs_iomap_valid(struct inode * inode,const struct iomap * iomap)76 xfs_iomap_valid(
77 	struct inode		*inode,
78 	const struct iomap	*iomap)
79 {
80 	struct xfs_inode	*ip = XFS_I(inode);
81 
82 	if (iomap->type == IOMAP_HOLE)
83 		return true;
84 
85 	if (iomap->validity_cookie !=
86 			xfs_iomap_inode_sequence(ip, iomap->flags)) {
87 		trace_xfs_iomap_invalid(ip, iomap);
88 		return false;
89 	}
90 
91 	XFS_ERRORTAG_DELAY(ip->i_mount, XFS_ERRTAG_WRITE_DELAY_MS);
92 	return true;
93 }
94 
95 const struct iomap_write_ops xfs_iomap_write_ops = {
96 	.iomap_valid		= xfs_iomap_valid,
97 };
98 
99 int
xfs_bmbt_to_iomap(struct xfs_inode * ip,struct iomap * iomap,struct xfs_bmbt_irec * imap,unsigned int mapping_flags,u16 iomap_flags,u64 sequence_cookie)100 xfs_bmbt_to_iomap(
101 	struct xfs_inode	*ip,
102 	struct iomap		*iomap,
103 	struct xfs_bmbt_irec	*imap,
104 	unsigned int		mapping_flags,
105 	u16			iomap_flags,
106 	u64			sequence_cookie)
107 {
108 	struct xfs_mount	*mp = ip->i_mount;
109 	struct xfs_buftarg	*target = xfs_inode_buftarg(ip);
110 
111 	if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock))) {
112 		xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
113 		return xfs_alert_fsblock_zero(ip, imap);
114 	}
115 
116 	if (imap->br_startblock == HOLESTARTBLOCK) {
117 		iomap->addr = IOMAP_NULL_ADDR;
118 		iomap->type = IOMAP_HOLE;
119 	} else if (imap->br_startblock == DELAYSTARTBLOCK ||
120 		   isnullstartblock(imap->br_startblock)) {
121 		iomap->addr = IOMAP_NULL_ADDR;
122 		iomap->type = IOMAP_DELALLOC;
123 	} else {
124 		xfs_daddr_t	daddr = xfs_fsb_to_db(ip, imap->br_startblock);
125 
126 		iomap->addr = BBTOB(daddr);
127 		if (mapping_flags & IOMAP_DAX)
128 			iomap->addr += target->bt_dax_part_off;
129 
130 		if (imap->br_state == XFS_EXT_UNWRITTEN)
131 			iomap->type = IOMAP_UNWRITTEN;
132 		else
133 			iomap->type = IOMAP_MAPPED;
134 
135 		/*
136 		 * Mark iomaps starting at the first sector of a RTG as merge
137 		 * boundary so that each I/O completions is contained to a
138 		 * single RTG.
139 		 */
140 		if (XFS_IS_REALTIME_INODE(ip) && xfs_has_rtgroups(mp) &&
141 		    xfs_rtbno_is_group_start(mp, imap->br_startblock))
142 			iomap->flags |= IOMAP_F_BOUNDARY;
143 	}
144 	iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
145 	iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
146 	if (mapping_flags & IOMAP_DAX)
147 		iomap->dax_dev = target->bt_daxdev;
148 	else
149 		iomap->bdev = target->bt_bdev;
150 	iomap->flags = iomap_flags;
151 
152 	/*
153 	 * If the inode is dirty for datasync purposes, let iomap know so it
154 	 * doesn't elide the IO completion journal flushes on O_DSYNC IO.
155 	 */
156 	if (ip->i_itemp) {
157 		struct xfs_inode_log_item *iip = ip->i_itemp;
158 
159 		spin_lock(&iip->ili_lock);
160 		if (iip->ili_datasync_seq)
161 			iomap->flags |= IOMAP_F_DIRTY;
162 		spin_unlock(&iip->ili_lock);
163 	}
164 
165 	iomap->validity_cookie = sequence_cookie;
166 	return 0;
167 }
168 
169 static void
xfs_hole_to_iomap(struct xfs_inode * ip,struct iomap * iomap,xfs_fileoff_t offset_fsb,xfs_fileoff_t end_fsb)170 xfs_hole_to_iomap(
171 	struct xfs_inode	*ip,
172 	struct iomap		*iomap,
173 	xfs_fileoff_t		offset_fsb,
174 	xfs_fileoff_t		end_fsb)
175 {
176 	struct xfs_buftarg	*target = xfs_inode_buftarg(ip);
177 
178 	iomap->addr = IOMAP_NULL_ADDR;
179 	iomap->type = IOMAP_HOLE;
180 	iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb);
181 	iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb);
182 	iomap->bdev = target->bt_bdev;
183 	iomap->dax_dev = target->bt_daxdev;
184 }
185 
186 static inline xfs_fileoff_t
xfs_iomap_end_fsb(struct xfs_mount * mp,loff_t offset,loff_t count)187 xfs_iomap_end_fsb(
188 	struct xfs_mount	*mp,
189 	loff_t			offset,
190 	loff_t			count)
191 {
192 	ASSERT(offset <= mp->m_super->s_maxbytes);
193 	return min(XFS_B_TO_FSB(mp, offset + count),
194 		   XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
195 }
196 
197 static xfs_extlen_t
xfs_eof_alignment(struct xfs_inode * ip)198 xfs_eof_alignment(
199 	struct xfs_inode	*ip)
200 {
201 	struct xfs_mount	*mp = ip->i_mount;
202 	xfs_extlen_t		align = 0;
203 
204 	if (!XFS_IS_REALTIME_INODE(ip)) {
205 		/*
206 		 * Round up the allocation request to a stripe unit
207 		 * (m_dalign) boundary if the file size is >= stripe unit
208 		 * size, and we are allocating past the allocation eof.
209 		 *
210 		 * If mounted with the "-o swalloc" option the alignment is
211 		 * increased from the strip unit size to the stripe width.
212 		 */
213 		if (mp->m_swidth && xfs_has_swalloc(mp))
214 			align = mp->m_swidth;
215 		else if (mp->m_dalign)
216 			align = mp->m_dalign;
217 
218 		if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
219 			align = 0;
220 	}
221 
222 	return align;
223 }
224 
225 /*
226  * Check if last_fsb is outside the last extent, and if so grow it to the next
227  * stripe unit boundary.
228  */
229 xfs_fileoff_t
xfs_iomap_eof_align_last_fsb(struct xfs_inode * ip,xfs_fileoff_t end_fsb)230 xfs_iomap_eof_align_last_fsb(
231 	struct xfs_inode	*ip,
232 	xfs_fileoff_t		end_fsb)
233 {
234 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
235 	xfs_extlen_t		extsz = xfs_get_extsz_hint(ip);
236 	xfs_extlen_t		align = xfs_eof_alignment(ip);
237 	struct xfs_bmbt_irec	irec;
238 	struct xfs_iext_cursor	icur;
239 
240 	ASSERT(!xfs_need_iread_extents(ifp));
241 
242 	/*
243 	 * Always round up the allocation request to the extent hint boundary.
244 	 */
245 	if (extsz) {
246 		if (align)
247 			align = roundup_64(align, extsz);
248 		else
249 			align = extsz;
250 	}
251 
252 	if (align) {
253 		xfs_fileoff_t	aligned_end_fsb = roundup_64(end_fsb, align);
254 
255 		xfs_iext_last(ifp, &icur);
256 		if (!xfs_iext_get_extent(ifp, &icur, &irec) ||
257 		    aligned_end_fsb >= irec.br_startoff + irec.br_blockcount)
258 			return aligned_end_fsb;
259 	}
260 
261 	return end_fsb;
262 }
263 
264 int
xfs_iomap_write_direct(struct xfs_inode * ip,xfs_fileoff_t offset_fsb,xfs_fileoff_t count_fsb,unsigned int flags,struct xfs_bmbt_irec * imap,u64 * seq)265 xfs_iomap_write_direct(
266 	struct xfs_inode	*ip,
267 	xfs_fileoff_t		offset_fsb,
268 	xfs_fileoff_t		count_fsb,
269 	unsigned int		flags,
270 	struct xfs_bmbt_irec	*imap,
271 	u64			*seq)
272 {
273 	struct xfs_mount	*mp = ip->i_mount;
274 	struct xfs_trans	*tp;
275 	xfs_filblks_t		resaligned;
276 	int			nimaps;
277 	unsigned int		dblocks, rblocks;
278 	bool			force = false;
279 	int			error;
280 	int			bmapi_flags = XFS_BMAPI_PREALLOC;
281 	int			nr_exts = XFS_IEXT_ADD_NOSPLIT_CNT;
282 
283 	ASSERT(count_fsb > 0);
284 
285 	resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb,
286 					   xfs_get_extsz_hint(ip));
287 	if (unlikely(XFS_IS_REALTIME_INODE(ip))) {
288 		dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
289 		rblocks = resaligned;
290 	} else {
291 		dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
292 		rblocks = 0;
293 	}
294 
295 	error = xfs_qm_dqattach(ip);
296 	if (error)
297 		return error;
298 
299 	/*
300 	 * For DAX, we do not allocate unwritten extents, but instead we zero
301 	 * the block before we commit the transaction.  Ideally we'd like to do
302 	 * this outside the transaction context, but if we commit and then crash
303 	 * we may not have zeroed the blocks and this will be exposed on
304 	 * recovery of the allocation. Hence we must zero before commit.
305 	 *
306 	 * Further, if we are mapping unwritten extents here, we need to zero
307 	 * and convert them to written so that we don't need an unwritten extent
308 	 * callback for DAX. This also means that we need to be able to dip into
309 	 * the reserve block pool for bmbt block allocation if there is no space
310 	 * left but we need to do unwritten extent conversion.
311 	 */
312 	if (flags & IOMAP_DAX) {
313 		bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
314 		if (imap->br_state == XFS_EXT_UNWRITTEN) {
315 			force = true;
316 			nr_exts = XFS_IEXT_WRITE_UNWRITTEN_CNT;
317 			dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
318 		}
319 	}
320 
321 	error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, dblocks,
322 			rblocks, force, &tp);
323 	if (error)
324 		return error;
325 
326 	error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK, nr_exts);
327 	if (error)
328 		goto out_trans_cancel;
329 
330 	/*
331 	 * From this point onwards we overwrite the imap pointer that the
332 	 * caller gave to us.
333 	 */
334 	nimaps = 1;
335 	error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, bmapi_flags, 0,
336 				imap, &nimaps);
337 	if (error)
338 		goto out_trans_cancel;
339 
340 	/*
341 	 * Complete the transaction
342 	 */
343 	error = xfs_trans_commit(tp);
344 	if (error)
345 		goto out_unlock;
346 
347 	if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock))) {
348 		xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
349 		error = xfs_alert_fsblock_zero(ip, imap);
350 	}
351 
352 out_unlock:
353 	*seq = xfs_iomap_inode_sequence(ip, 0);
354 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
355 	return error;
356 
357 out_trans_cancel:
358 	xfs_trans_cancel(tp);
359 	goto out_unlock;
360 }
361 
362 STATIC bool
xfs_quota_need_throttle(struct xfs_inode * ip,xfs_dqtype_t type,xfs_fsblock_t alloc_blocks)363 xfs_quota_need_throttle(
364 	struct xfs_inode	*ip,
365 	xfs_dqtype_t		type,
366 	xfs_fsblock_t		alloc_blocks)
367 {
368 	struct xfs_dquot	*dq = xfs_inode_dquot(ip, type);
369 	struct xfs_dquot_res	*res;
370 	struct xfs_dquot_pre	*pre;
371 
372 	if (!dq || !xfs_this_quota_on(ip->i_mount, type))
373 		return false;
374 
375 	if (XFS_IS_REALTIME_INODE(ip)) {
376 		res = &dq->q_rtb;
377 		pre = &dq->q_rtb_prealloc;
378 	} else {
379 		res = &dq->q_blk;
380 		pre = &dq->q_blk_prealloc;
381 	}
382 
383 	/* no hi watermark, no throttle */
384 	if (!pre->q_prealloc_hi_wmark)
385 		return false;
386 
387 	/* under the lo watermark, no throttle */
388 	if (res->reserved + alloc_blocks < pre->q_prealloc_lo_wmark)
389 		return false;
390 
391 	return true;
392 }
393 
394 STATIC void
xfs_quota_calc_throttle(struct xfs_inode * ip,xfs_dqtype_t type,xfs_fsblock_t * qblocks,int * qshift,int64_t * qfreesp)395 xfs_quota_calc_throttle(
396 	struct xfs_inode	*ip,
397 	xfs_dqtype_t		type,
398 	xfs_fsblock_t		*qblocks,
399 	int			*qshift,
400 	int64_t			*qfreesp)
401 {
402 	struct xfs_dquot	*dq = xfs_inode_dquot(ip, type);
403 	struct xfs_dquot_res	*res;
404 	struct xfs_dquot_pre	*pre;
405 	int64_t			freesp;
406 	int			shift = 0;
407 
408 	if (!dq) {
409 		res = NULL;
410 		pre = NULL;
411 	} else if (XFS_IS_REALTIME_INODE(ip)) {
412 		res = &dq->q_rtb;
413 		pre = &dq->q_rtb_prealloc;
414 	} else {
415 		res = &dq->q_blk;
416 		pre = &dq->q_blk_prealloc;
417 	}
418 
419 	/* no dq, or over hi wmark, squash the prealloc completely */
420 	if (!res || res->reserved >= pre->q_prealloc_hi_wmark) {
421 		*qblocks = 0;
422 		*qfreesp = 0;
423 		return;
424 	}
425 
426 	freesp = pre->q_prealloc_hi_wmark - res->reserved;
427 	if (freesp < pre->q_low_space[XFS_QLOWSP_5_PCNT]) {
428 		shift = 2;
429 		if (freesp < pre->q_low_space[XFS_QLOWSP_3_PCNT])
430 			shift += 2;
431 		if (freesp < pre->q_low_space[XFS_QLOWSP_1_PCNT])
432 			shift += 2;
433 	}
434 
435 	if (freesp < *qfreesp)
436 		*qfreesp = freesp;
437 
438 	/* only overwrite the throttle values if we are more aggressive */
439 	if ((freesp >> shift) < (*qblocks >> *qshift)) {
440 		*qblocks = freesp;
441 		*qshift = shift;
442 	}
443 }
444 
445 static int64_t
xfs_iomap_freesp(struct xfs_mount * mp,unsigned int idx,uint64_t low_space[XFS_LOWSP_MAX],int * shift)446 xfs_iomap_freesp(
447 	struct xfs_mount	*mp,
448 	unsigned int		idx,
449 	uint64_t		low_space[XFS_LOWSP_MAX],
450 	int			*shift)
451 {
452 	int64_t			freesp;
453 
454 	freesp = xfs_estimate_freecounter(mp, idx);
455 	if (freesp < low_space[XFS_LOWSP_5_PCNT]) {
456 		*shift = 2;
457 		if (freesp < low_space[XFS_LOWSP_4_PCNT])
458 			(*shift)++;
459 		if (freesp < low_space[XFS_LOWSP_3_PCNT])
460 			(*shift)++;
461 		if (freesp < low_space[XFS_LOWSP_2_PCNT])
462 			(*shift)++;
463 		if (freesp < low_space[XFS_LOWSP_1_PCNT])
464 			(*shift)++;
465 	}
466 	return freesp;
467 }
468 
469 /*
470  * If we don't have a user specified preallocation size, dynamically increase
471  * the preallocation size as the size of the file grows.  Cap the maximum size
472  * at a single extent or less if the filesystem is near full. The closer the
473  * filesystem is to being full, the smaller the maximum preallocation.
474  */
475 STATIC xfs_fsblock_t
xfs_iomap_prealloc_size(struct xfs_inode * ip,int whichfork,loff_t offset,loff_t count,struct xfs_iext_cursor * icur)476 xfs_iomap_prealloc_size(
477 	struct xfs_inode	*ip,
478 	int			whichfork,
479 	loff_t			offset,
480 	loff_t			count,
481 	struct xfs_iext_cursor	*icur)
482 {
483 	struct xfs_iext_cursor	ncur = *icur;
484 	struct xfs_bmbt_irec	prev, got;
485 	struct xfs_mount	*mp = ip->i_mount;
486 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
487 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
488 	int64_t			freesp;
489 	xfs_fsblock_t		qblocks;
490 	xfs_fsblock_t		alloc_blocks = 0;
491 	xfs_extlen_t		plen;
492 	int			shift = 0;
493 	int			qshift = 0;
494 
495 	/*
496 	 * As an exception we don't do any preallocation at all if the file is
497 	 * smaller than the minimum preallocation and we are using the default
498 	 * dynamic preallocation scheme, as it is likely this is the only write
499 	 * to the file that is going to be done.
500 	 */
501 	if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_allocsize_blocks))
502 		return 0;
503 
504 	/*
505 	 * Use the minimum preallocation size for small files or if we are
506 	 * writing right after a hole.
507 	 */
508 	if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
509 	    !xfs_iext_prev_extent(ifp, &ncur, &prev) ||
510 	    prev.br_startoff + prev.br_blockcount < offset_fsb)
511 		return mp->m_allocsize_blocks;
512 
513 	/*
514 	 * Take the size of the preceding data extents as the basis for the
515 	 * preallocation size. Note that we don't care if the previous extents
516 	 * are written or not.
517 	 */
518 	plen = prev.br_blockcount;
519 	while (xfs_iext_prev_extent(ifp, &ncur, &got)) {
520 		if (plen > XFS_MAX_BMBT_EXTLEN / 2 ||
521 		    isnullstartblock(got.br_startblock) ||
522 		    got.br_startoff + got.br_blockcount != prev.br_startoff ||
523 		    got.br_startblock + got.br_blockcount != prev.br_startblock)
524 			break;
525 		plen += got.br_blockcount;
526 		prev = got;
527 	}
528 
529 	/*
530 	 * If the size of the extents is greater than half the maximum extent
531 	 * length, then use the current offset as the basis.  This ensures that
532 	 * for large files the preallocation size always extends to
533 	 * XFS_BMBT_MAX_EXTLEN rather than falling short due to things like stripe
534 	 * unit/width alignment of real extents.
535 	 */
536 	alloc_blocks = plen * 2;
537 	if (alloc_blocks > XFS_MAX_BMBT_EXTLEN)
538 		alloc_blocks = XFS_B_TO_FSB(mp, offset);
539 	qblocks = alloc_blocks;
540 
541 	/*
542 	 * XFS_BMBT_MAX_EXTLEN is not a power of two value but we round the prealloc
543 	 * down to the nearest power of two value after throttling. To prevent
544 	 * the round down from unconditionally reducing the maximum supported
545 	 * prealloc size, we round up first, apply appropriate throttling, round
546 	 * down and cap the value to XFS_BMBT_MAX_EXTLEN.
547 	 */
548 	alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(XFS_MAX_BMBT_EXTLEN),
549 				       alloc_blocks);
550 
551 	if (unlikely(XFS_IS_REALTIME_INODE(ip)))
552 		freesp = xfs_rtbxlen_to_blen(mp,
553 				xfs_iomap_freesp(mp, XC_FREE_RTEXTENTS,
554 					mp->m_low_rtexts, &shift));
555 	else
556 		freesp = xfs_iomap_freesp(mp, XC_FREE_BLOCKS, mp->m_low_space,
557 				&shift);
558 
559 	/*
560 	 * Check each quota to cap the prealloc size, provide a shift value to
561 	 * throttle with and adjust amount of available space.
562 	 */
563 	if (xfs_quota_need_throttle(ip, XFS_DQTYPE_USER, alloc_blocks))
564 		xfs_quota_calc_throttle(ip, XFS_DQTYPE_USER, &qblocks, &qshift,
565 					&freesp);
566 	if (xfs_quota_need_throttle(ip, XFS_DQTYPE_GROUP, alloc_blocks))
567 		xfs_quota_calc_throttle(ip, XFS_DQTYPE_GROUP, &qblocks, &qshift,
568 					&freesp);
569 	if (xfs_quota_need_throttle(ip, XFS_DQTYPE_PROJ, alloc_blocks))
570 		xfs_quota_calc_throttle(ip, XFS_DQTYPE_PROJ, &qblocks, &qshift,
571 					&freesp);
572 
573 	/*
574 	 * The final prealloc size is set to the minimum of free space available
575 	 * in each of the quotas and the overall filesystem.
576 	 *
577 	 * The shift throttle value is set to the maximum value as determined by
578 	 * the global low free space values and per-quota low free space values.
579 	 */
580 	alloc_blocks = min(alloc_blocks, qblocks);
581 	shift = max(shift, qshift);
582 
583 	if (shift)
584 		alloc_blocks >>= shift;
585 	/*
586 	 * rounddown_pow_of_two() returns an undefined result if we pass in
587 	 * alloc_blocks = 0.
588 	 */
589 	if (alloc_blocks)
590 		alloc_blocks = rounddown_pow_of_two(alloc_blocks);
591 	if (alloc_blocks > XFS_MAX_BMBT_EXTLEN)
592 		alloc_blocks = XFS_MAX_BMBT_EXTLEN;
593 
594 	/*
595 	 * If we are still trying to allocate more space than is
596 	 * available, squash the prealloc hard. This can happen if we
597 	 * have a large file on a small filesystem and the above
598 	 * lowspace thresholds are smaller than XFS_BMBT_MAX_EXTLEN.
599 	 */
600 	while (alloc_blocks && alloc_blocks >= freesp)
601 		alloc_blocks >>= 4;
602 	if (alloc_blocks < mp->m_allocsize_blocks)
603 		alloc_blocks = mp->m_allocsize_blocks;
604 	trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
605 				      mp->m_allocsize_blocks);
606 	return alloc_blocks;
607 }
608 
609 int
xfs_iomap_write_unwritten(xfs_inode_t * ip,xfs_off_t offset,xfs_off_t count,bool update_isize)610 xfs_iomap_write_unwritten(
611 	xfs_inode_t	*ip,
612 	xfs_off_t	offset,
613 	xfs_off_t	count,
614 	bool		update_isize)
615 {
616 	xfs_mount_t	*mp = ip->i_mount;
617 	xfs_fileoff_t	offset_fsb;
618 	xfs_filblks_t	count_fsb;
619 	xfs_filblks_t	numblks_fsb;
620 	int		nimaps;
621 	xfs_trans_t	*tp;
622 	xfs_bmbt_irec_t imap;
623 	struct inode	*inode = VFS_I(ip);
624 	xfs_fsize_t	i_size;
625 	uint		resblks;
626 	int		error;
627 
628 	trace_xfs_unwritten_convert(ip, offset, count);
629 
630 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
631 	count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
632 	count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
633 
634 	/*
635 	 * Reserve enough blocks in this transaction for two complete extent
636 	 * btree splits.  We may be converting the middle part of an unwritten
637 	 * extent and in this case we will insert two new extents in the btree
638 	 * each of which could cause a full split.
639 	 *
640 	 * This reservation amount will be used in the first call to
641 	 * xfs_bmbt_split() to select an AG with enough space to satisfy the
642 	 * rest of the operation.
643 	 */
644 	resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
645 
646 	/* Attach dquots so that bmbt splits are accounted correctly. */
647 	error = xfs_qm_dqattach(ip);
648 	if (error)
649 		return error;
650 
651 	do {
652 		/*
653 		 * Set up a transaction to convert the range of extents
654 		 * from unwritten to real. Do allocations in a loop until
655 		 * we have covered the range passed in.
656 		 *
657 		 * Note that we can't risk to recursing back into the filesystem
658 		 * here as we might be asked to write out the same inode that we
659 		 * complete here and might deadlock on the iolock.
660 		 */
661 		error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks,
662 				0, true, &tp);
663 		if (error)
664 			return error;
665 
666 		error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
667 				XFS_IEXT_WRITE_UNWRITTEN_CNT);
668 		if (error)
669 			goto error_on_bmapi_transaction;
670 
671 		/*
672 		 * Modify the unwritten extent state of the buffer.
673 		 */
674 		nimaps = 1;
675 		error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
676 					XFS_BMAPI_CONVERT, resblks, &imap,
677 					&nimaps);
678 		if (error)
679 			goto error_on_bmapi_transaction;
680 
681 		/*
682 		 * Log the updated inode size as we go.  We have to be careful
683 		 * to only log it up to the actual write offset if it is
684 		 * halfway into a block.
685 		 */
686 		i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
687 		if (i_size > offset + count)
688 			i_size = offset + count;
689 		if (update_isize && i_size > i_size_read(inode))
690 			i_size_write(inode, i_size);
691 		i_size = xfs_new_eof(ip, i_size);
692 		if (i_size) {
693 			ip->i_disk_size = i_size;
694 			xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
695 		}
696 
697 		error = xfs_trans_commit(tp);
698 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
699 		if (error)
700 			return error;
701 
702 		if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock))) {
703 			xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
704 			return xfs_alert_fsblock_zero(ip, &imap);
705 		}
706 
707 		if ((numblks_fsb = imap.br_blockcount) == 0) {
708 			/*
709 			 * The numblks_fsb value should always get
710 			 * smaller, otherwise the loop is stuck.
711 			 */
712 			ASSERT(imap.br_blockcount);
713 			break;
714 		}
715 		offset_fsb += numblks_fsb;
716 		count_fsb -= numblks_fsb;
717 	} while (count_fsb > 0);
718 
719 	return 0;
720 
721 error_on_bmapi_transaction:
722 	xfs_trans_cancel(tp);
723 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
724 	return error;
725 }
726 
727 static inline bool
imap_needs_alloc(struct inode * inode,unsigned flags,struct xfs_bmbt_irec * imap,int nimaps)728 imap_needs_alloc(
729 	struct inode		*inode,
730 	unsigned		flags,
731 	struct xfs_bmbt_irec	*imap,
732 	int			nimaps)
733 {
734 	/* don't allocate blocks when just zeroing */
735 	if (flags & IOMAP_ZERO)
736 		return false;
737 	if (!nimaps ||
738 	    imap->br_startblock == HOLESTARTBLOCK ||
739 	    imap->br_startblock == DELAYSTARTBLOCK)
740 		return true;
741 	/* we convert unwritten extents before copying the data for DAX */
742 	if ((flags & IOMAP_DAX) && imap->br_state == XFS_EXT_UNWRITTEN)
743 		return true;
744 	return false;
745 }
746 
747 static inline bool
imap_needs_cow(struct xfs_inode * ip,unsigned int flags,struct xfs_bmbt_irec * imap,int nimaps)748 imap_needs_cow(
749 	struct xfs_inode	*ip,
750 	unsigned int		flags,
751 	struct xfs_bmbt_irec	*imap,
752 	int			nimaps)
753 {
754 	if (!xfs_is_cow_inode(ip))
755 		return false;
756 
757 	/* when zeroing we don't have to COW holes or unwritten extents */
758 	if (flags & (IOMAP_UNSHARE | IOMAP_ZERO)) {
759 		if (!nimaps ||
760 		    imap->br_startblock == HOLESTARTBLOCK ||
761 		    imap->br_state == XFS_EXT_UNWRITTEN)
762 			return false;
763 	}
764 
765 	return true;
766 }
767 
768 /*
769  * Extents not yet cached requires exclusive access, don't block for
770  * IOMAP_NOWAIT.
771  *
772  * This is basically an opencoded xfs_ilock_data_map_shared() call, but with
773  * support for IOMAP_NOWAIT.
774  */
775 static int
xfs_ilock_for_iomap(struct xfs_inode * ip,unsigned flags,unsigned * lockmode)776 xfs_ilock_for_iomap(
777 	struct xfs_inode	*ip,
778 	unsigned		flags,
779 	unsigned		*lockmode)
780 {
781 	if (flags & IOMAP_NOWAIT) {
782 		if (xfs_need_iread_extents(&ip->i_df))
783 			return -EAGAIN;
784 		if (!xfs_ilock_nowait(ip, *lockmode))
785 			return -EAGAIN;
786 	} else {
787 		if (xfs_need_iread_extents(&ip->i_df))
788 			*lockmode = XFS_ILOCK_EXCL;
789 		xfs_ilock(ip, *lockmode);
790 	}
791 
792 	return 0;
793 }
794 
795 /*
796  * Check that the imap we are going to return to the caller spans the entire
797  * range that the caller requested for the IO.
798  */
799 static bool
imap_spans_range(struct xfs_bmbt_irec * imap,xfs_fileoff_t offset_fsb,xfs_fileoff_t end_fsb)800 imap_spans_range(
801 	struct xfs_bmbt_irec	*imap,
802 	xfs_fileoff_t		offset_fsb,
803 	xfs_fileoff_t		end_fsb)
804 {
805 	if (imap->br_startoff > offset_fsb)
806 		return false;
807 	if (imap->br_startoff + imap->br_blockcount < end_fsb)
808 		return false;
809 	return true;
810 }
811 
812 static bool
xfs_bmap_hw_atomic_write_possible(struct xfs_inode * ip,struct xfs_bmbt_irec * imap,xfs_fileoff_t offset_fsb,xfs_fileoff_t end_fsb)813 xfs_bmap_hw_atomic_write_possible(
814 	struct xfs_inode	*ip,
815 	struct xfs_bmbt_irec	*imap,
816 	xfs_fileoff_t		offset_fsb,
817 	xfs_fileoff_t		end_fsb)
818 {
819 	struct xfs_mount	*mp = ip->i_mount;
820 	xfs_fsize_t		len = XFS_FSB_TO_B(mp, end_fsb - offset_fsb);
821 
822 	/*
823 	 * atomic writes are required to be naturally aligned for disk blocks,
824 	 * which ensures that we adhere to block layer rules that we won't
825 	 * straddle any boundary or violate write alignment requirement.
826 	 */
827 	if (!IS_ALIGNED(imap->br_startblock, imap->br_blockcount))
828 		return false;
829 
830 	/*
831 	 * Spanning multiple extents would mean that multiple BIOs would be
832 	 * issued, and so would lose atomicity required for REQ_ATOMIC-based
833 	 * atomics.
834 	 */
835 	if (!imap_spans_range(imap, offset_fsb, end_fsb))
836 		return false;
837 
838 	/*
839 	 * The ->iomap_begin caller should ensure this, but check anyway.
840 	 */
841 	return len <= xfs_inode_buftarg(ip)->bt_awu_max;
842 }
843 
844 static int
xfs_direct_write_iomap_begin(struct inode * inode,loff_t offset,loff_t length,unsigned flags,struct iomap * iomap,struct iomap * srcmap)845 xfs_direct_write_iomap_begin(
846 	struct inode		*inode,
847 	loff_t			offset,
848 	loff_t			length,
849 	unsigned		flags,
850 	struct iomap		*iomap,
851 	struct iomap		*srcmap)
852 {
853 	struct xfs_inode	*ip = XFS_I(inode);
854 	struct xfs_mount	*mp = ip->i_mount;
855 	struct xfs_bmbt_irec	imap, cmap;
856 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
857 	xfs_fileoff_t		end_fsb = xfs_iomap_end_fsb(mp, offset, length);
858 	xfs_fileoff_t		orig_end_fsb = end_fsb;
859 	int			nimaps = 1, error = 0;
860 	bool			shared = false;
861 	u16			iomap_flags = 0;
862 	bool			needs_alloc;
863 	unsigned int		lockmode;
864 	u64			seq;
865 
866 	ASSERT(flags & (IOMAP_WRITE | IOMAP_ZERO));
867 
868 	if (xfs_is_shutdown(mp))
869 		return -EIO;
870 
871 	/*
872 	 * Writes that span EOF might trigger an IO size update on completion,
873 	 * so consider them to be dirty for the purposes of O_DSYNC even if
874 	 * there is no other metadata changes pending or have been made here.
875 	 */
876 	if (offset + length > i_size_read(inode))
877 		iomap_flags |= IOMAP_F_DIRTY;
878 
879 	/* HW-offload atomics are always used in this path */
880 	if (flags & IOMAP_ATOMIC)
881 		iomap_flags |= IOMAP_F_ATOMIC_BIO;
882 
883 	/*
884 	 * COW writes may allocate delalloc space or convert unwritten COW
885 	 * extents, so we need to make sure to take the lock exclusively here.
886 	 */
887 	if (xfs_is_cow_inode(ip))
888 		lockmode = XFS_ILOCK_EXCL;
889 	else
890 		lockmode = XFS_ILOCK_SHARED;
891 
892 relock:
893 	error = xfs_ilock_for_iomap(ip, flags, &lockmode);
894 	if (error)
895 		return error;
896 
897 	/*
898 	 * The reflink iflag could have changed since the earlier unlocked
899 	 * check, check if it again and relock if needed.
900 	 */
901 	if (xfs_is_cow_inode(ip) && lockmode == XFS_ILOCK_SHARED) {
902 		xfs_iunlock(ip, lockmode);
903 		lockmode = XFS_ILOCK_EXCL;
904 		goto relock;
905 	}
906 
907 	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
908 			       &nimaps, 0);
909 	if (error)
910 		goto out_unlock;
911 
912 	if (imap_needs_cow(ip, flags, &imap, nimaps)) {
913 		error = -EAGAIN;
914 		if (flags & IOMAP_NOWAIT)
915 			goto out_unlock;
916 
917 		/* may drop and re-acquire the ilock */
918 		error = xfs_reflink_allocate_cow(ip, &imap, &cmap, &shared,
919 				&lockmode,
920 				(flags & IOMAP_DIRECT) || IS_DAX(inode));
921 		if (error)
922 			goto out_unlock;
923 		if (shared) {
924 			if ((flags & IOMAP_ATOMIC) &&
925 			    !xfs_bmap_hw_atomic_write_possible(ip, &cmap,
926 					offset_fsb, end_fsb)) {
927 				error = -ENOPROTOOPT;
928 				goto out_unlock;
929 			}
930 			goto out_found_cow;
931 		}
932 		end_fsb = imap.br_startoff + imap.br_blockcount;
933 		length = XFS_FSB_TO_B(mp, end_fsb) - offset;
934 	}
935 
936 	needs_alloc = imap_needs_alloc(inode, flags, &imap, nimaps);
937 
938 	if (flags & IOMAP_ATOMIC) {
939 		error = -ENOPROTOOPT;
940 		/*
941 		 * If we allocate less than what is required for the write
942 		 * then we may end up with multiple extents, which means that
943 		 * REQ_ATOMIC-based cannot be used, so avoid this possibility.
944 		 */
945 		if (needs_alloc && orig_end_fsb - offset_fsb > 1)
946 			goto out_unlock;
947 
948 		if (!xfs_bmap_hw_atomic_write_possible(ip, &imap, offset_fsb,
949 				orig_end_fsb))
950 			goto out_unlock;
951 	}
952 
953 	if (needs_alloc)
954 		goto allocate_blocks;
955 
956 	/*
957 	 * NOWAIT and OVERWRITE I/O needs to span the entire requested I/O with
958 	 * a single map so that we avoid partial IO failures due to the rest of
959 	 * the I/O range not covered by this map triggering an EAGAIN condition
960 	 * when it is subsequently mapped and aborting the I/O.
961 	 */
962 	if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY)) {
963 		error = -EAGAIN;
964 		if (!imap_spans_range(&imap, offset_fsb, end_fsb))
965 			goto out_unlock;
966 	}
967 
968 	/*
969 	 * For overwrite only I/O, we cannot convert unwritten extents without
970 	 * requiring sub-block zeroing.  This can only be done under an
971 	 * exclusive IOLOCK, hence return -EAGAIN if this is not a written
972 	 * extent to tell the caller to try again.
973 	 */
974 	if (flags & IOMAP_OVERWRITE_ONLY) {
975 		error = -EAGAIN;
976 		if (imap.br_state != XFS_EXT_NORM &&
977 	            ((offset | length) & mp->m_blockmask))
978 			goto out_unlock;
979 	}
980 
981 	seq = xfs_iomap_inode_sequence(ip, iomap_flags);
982 	xfs_iunlock(ip, lockmode);
983 	trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
984 	return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags, seq);
985 
986 allocate_blocks:
987 	error = -EAGAIN;
988 	if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY))
989 		goto out_unlock;
990 
991 	/*
992 	 * We cap the maximum length we map to a sane size  to keep the chunks
993 	 * of work done where somewhat symmetric with the work writeback does.
994 	 * This is a completely arbitrary number pulled out of thin air as a
995 	 * best guess for initial testing.
996 	 *
997 	 * Note that the values needs to be less than 32-bits wide until the
998 	 * lower level functions are updated.
999 	 */
1000 	length = min_t(loff_t, length, 1024 * PAGE_SIZE);
1001 	end_fsb = xfs_iomap_end_fsb(mp, offset, length);
1002 
1003 	if (offset + length > XFS_ISIZE(ip))
1004 		end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
1005 	else if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
1006 		end_fsb = min(end_fsb, imap.br_startoff + imap.br_blockcount);
1007 	xfs_iunlock(ip, lockmode);
1008 
1009 	error = xfs_iomap_write_direct(ip, offset_fsb, end_fsb - offset_fsb,
1010 			flags, &imap, &seq);
1011 	if (error)
1012 		return error;
1013 
1014 	trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
1015 	return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
1016 				 iomap_flags | IOMAP_F_NEW, seq);
1017 
1018 out_found_cow:
1019 	length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
1020 	trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
1021 	if (imap.br_startblock != HOLESTARTBLOCK) {
1022 		seq = xfs_iomap_inode_sequence(ip, 0);
1023 		error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0, seq);
1024 		if (error)
1025 			goto out_unlock;
1026 	}
1027 	seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED);
1028 	xfs_iunlock(ip, lockmode);
1029 	return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED, seq);
1030 
1031 out_unlock:
1032 	if (lockmode)
1033 		xfs_iunlock(ip, lockmode);
1034 	return error;
1035 }
1036 
1037 const struct iomap_ops xfs_direct_write_iomap_ops = {
1038 	.iomap_begin		= xfs_direct_write_iomap_begin,
1039 };
1040 
1041 #ifdef CONFIG_XFS_RT
1042 /*
1043  * This is really simple.  The space has already been reserved before taking the
1044  * IOLOCK, the actual block allocation is done just before submitting the bio
1045  * and only recorded in the extent map on I/O completion.
1046  */
1047 static int
xfs_zoned_direct_write_iomap_begin(struct inode * inode,loff_t offset,loff_t length,unsigned flags,struct iomap * iomap,struct iomap * srcmap)1048 xfs_zoned_direct_write_iomap_begin(
1049 	struct inode		*inode,
1050 	loff_t			offset,
1051 	loff_t			length,
1052 	unsigned		flags,
1053 	struct iomap		*iomap,
1054 	struct iomap		*srcmap)
1055 {
1056 	struct xfs_inode	*ip = XFS_I(inode);
1057 	int			error;
1058 
1059 	ASSERT(!(flags & IOMAP_OVERWRITE_ONLY));
1060 
1061 	/*
1062 	 * Needs to be pushed down into the allocator so that only writes into
1063 	 * a single zone can be supported.
1064 	 */
1065 	if (flags & IOMAP_NOWAIT)
1066 		return -EAGAIN;
1067 
1068 	/*
1069 	 * Ensure the extent list is in memory in so that we don't have to do
1070 	 * read it from the I/O completion handler.
1071 	 */
1072 	if (xfs_need_iread_extents(&ip->i_df)) {
1073 		xfs_ilock(ip, XFS_ILOCK_EXCL);
1074 		error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1075 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
1076 		if (error)
1077 			return error;
1078 	}
1079 
1080 	iomap->type = IOMAP_MAPPED;
1081 	iomap->flags = IOMAP_F_DIRTY;
1082 	iomap->bdev = ip->i_mount->m_rtdev_targp->bt_bdev;
1083 	iomap->offset = offset;
1084 	iomap->length = length;
1085 	iomap->flags = IOMAP_F_ANON_WRITE;
1086 	return 0;
1087 }
1088 
1089 const struct iomap_ops xfs_zoned_direct_write_iomap_ops = {
1090 	.iomap_begin		= xfs_zoned_direct_write_iomap_begin,
1091 };
1092 #endif /* CONFIG_XFS_RT */
1093 
1094 static int
xfs_atomic_write_cow_iomap_begin(struct inode * inode,loff_t offset,loff_t length,unsigned flags,struct iomap * iomap,struct iomap * srcmap)1095 xfs_atomic_write_cow_iomap_begin(
1096 	struct inode		*inode,
1097 	loff_t			offset,
1098 	loff_t			length,
1099 	unsigned		flags,
1100 	struct iomap		*iomap,
1101 	struct iomap		*srcmap)
1102 {
1103 	struct xfs_inode	*ip = XFS_I(inode);
1104 	struct xfs_mount	*mp = ip->i_mount;
1105 	const xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
1106 	xfs_fileoff_t		end_fsb = xfs_iomap_end_fsb(mp, offset, length);
1107 	xfs_filblks_t		count_fsb = end_fsb - offset_fsb;
1108 	int			nmaps = 1;
1109 	xfs_filblks_t		resaligned;
1110 	struct xfs_bmbt_irec	cmap;
1111 	struct xfs_iext_cursor	icur;
1112 	struct xfs_trans	*tp;
1113 	unsigned int		dblocks = 0, rblocks = 0;
1114 	int			error;
1115 	u64			seq;
1116 
1117 	ASSERT(flags & IOMAP_WRITE);
1118 	ASSERT(flags & IOMAP_DIRECT);
1119 
1120 	if (xfs_is_shutdown(mp))
1121 		return -EIO;
1122 
1123 	if (!xfs_can_sw_atomic_write(mp)) {
1124 		ASSERT(xfs_can_sw_atomic_write(mp));
1125 		return -EINVAL;
1126 	}
1127 
1128 	/* blocks are always allocated in this path */
1129 	if (flags & IOMAP_NOWAIT)
1130 		return -EAGAIN;
1131 
1132 	trace_xfs_iomap_atomic_write_cow(ip, offset, length);
1133 
1134 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1135 
1136 	if (!ip->i_cowfp) {
1137 		ASSERT(!xfs_is_reflink_inode(ip));
1138 		xfs_ifork_init_cow(ip);
1139 	}
1140 
1141 	if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1142 		cmap.br_startoff = end_fsb;
1143 	if (cmap.br_startoff <= offset_fsb) {
1144 		xfs_trim_extent(&cmap, offset_fsb, count_fsb);
1145 		goto found;
1146 	}
1147 
1148 	end_fsb = cmap.br_startoff;
1149 	count_fsb = end_fsb - offset_fsb;
1150 
1151 	resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb,
1152 			xfs_get_cowextsz_hint(ip));
1153 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1154 
1155 	if (XFS_IS_REALTIME_INODE(ip)) {
1156 		dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1157 		rblocks = resaligned;
1158 	} else {
1159 		dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
1160 		rblocks = 0;
1161 	}
1162 
1163 	error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, dblocks,
1164 			rblocks, false, &tp);
1165 	if (error)
1166 		return error;
1167 
1168 	/* extent layout could have changed since the unlock, so check again */
1169 	if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1170 		cmap.br_startoff = end_fsb;
1171 	if (cmap.br_startoff <= offset_fsb) {
1172 		xfs_trim_extent(&cmap, offset_fsb, count_fsb);
1173 		xfs_trans_cancel(tp);
1174 		goto found;
1175 	}
1176 
1177 	/*
1178 	 * Allocate the entire reservation as unwritten blocks.
1179 	 *
1180 	 * Use XFS_BMAPI_EXTSZALIGN to hint at aligning new extents according to
1181 	 * extszhint, such that there will be a greater chance that future
1182 	 * atomic writes to that same range will be aligned (and don't require
1183 	 * this COW-based method).
1184 	 */
1185 	error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
1186 			XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC |
1187 			XFS_BMAPI_EXTSZALIGN, 0, &cmap, &nmaps);
1188 	if (error) {
1189 		xfs_trans_cancel(tp);
1190 		goto out_unlock;
1191 	}
1192 
1193 	xfs_inode_set_cowblocks_tag(ip);
1194 	error = xfs_trans_commit(tp);
1195 	if (error)
1196 		goto out_unlock;
1197 
1198 found:
1199 	if (cmap.br_state != XFS_EXT_NORM) {
1200 		error = xfs_reflink_convert_cow_locked(ip, offset_fsb,
1201 				count_fsb);
1202 		if (error)
1203 			goto out_unlock;
1204 		cmap.br_state = XFS_EXT_NORM;
1205 	}
1206 
1207 	length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
1208 	trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
1209 	seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED);
1210 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1211 	return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED, seq);
1212 
1213 out_unlock:
1214 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1215 	return error;
1216 }
1217 
1218 const struct iomap_ops xfs_atomic_write_cow_iomap_ops = {
1219 	.iomap_begin		= xfs_atomic_write_cow_iomap_begin,
1220 };
1221 
1222 static int
xfs_dax_write_iomap_end(struct inode * inode,loff_t pos,loff_t length,ssize_t written,unsigned flags,struct iomap * iomap)1223 xfs_dax_write_iomap_end(
1224 	struct inode		*inode,
1225 	loff_t			pos,
1226 	loff_t			length,
1227 	ssize_t			written,
1228 	unsigned		flags,
1229 	struct iomap		*iomap)
1230 {
1231 	struct xfs_inode	*ip = XFS_I(inode);
1232 
1233 	if (!xfs_is_cow_inode(ip))
1234 		return 0;
1235 
1236 	if (!written)
1237 		return xfs_reflink_cancel_cow_range(ip, pos, length, true);
1238 
1239 	return xfs_reflink_end_cow(ip, pos, written);
1240 }
1241 
1242 const struct iomap_ops xfs_dax_write_iomap_ops = {
1243 	.iomap_begin	= xfs_direct_write_iomap_begin,
1244 	.iomap_end	= xfs_dax_write_iomap_end,
1245 };
1246 
1247 /*
1248  * Convert a hole to a delayed allocation.
1249  */
1250 static void
xfs_bmap_add_extent_hole_delay(struct xfs_inode * ip,int whichfork,struct xfs_iext_cursor * icur,struct xfs_bmbt_irec * new)1251 xfs_bmap_add_extent_hole_delay(
1252 	struct xfs_inode	*ip,	/* incore inode pointer */
1253 	int			whichfork,
1254 	struct xfs_iext_cursor	*icur,
1255 	struct xfs_bmbt_irec	*new)	/* new data to add to file extents */
1256 {
1257 	struct xfs_ifork	*ifp;	/* inode fork pointer */
1258 	xfs_bmbt_irec_t		left;	/* left neighbor extent entry */
1259 	xfs_filblks_t		newlen=0;	/* new indirect size */
1260 	xfs_filblks_t		oldlen=0;	/* old indirect size */
1261 	xfs_bmbt_irec_t		right;	/* right neighbor extent entry */
1262 	uint32_t		state = xfs_bmap_fork_to_state(whichfork);
1263 	xfs_filblks_t		temp;	 /* temp for indirect calculations */
1264 
1265 	ifp = xfs_ifork_ptr(ip, whichfork);
1266 	ASSERT(isnullstartblock(new->br_startblock));
1267 
1268 	/*
1269 	 * Check and set flags if this segment has a left neighbor
1270 	 */
1271 	if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
1272 		state |= BMAP_LEFT_VALID;
1273 		if (isnullstartblock(left.br_startblock))
1274 			state |= BMAP_LEFT_DELAY;
1275 	}
1276 
1277 	/*
1278 	 * Check and set flags if the current (right) segment exists.
1279 	 * If it doesn't exist, we're converting the hole at end-of-file.
1280 	 */
1281 	if (xfs_iext_get_extent(ifp, icur, &right)) {
1282 		state |= BMAP_RIGHT_VALID;
1283 		if (isnullstartblock(right.br_startblock))
1284 			state |= BMAP_RIGHT_DELAY;
1285 	}
1286 
1287 	/*
1288 	 * Set contiguity flags on the left and right neighbors.
1289 	 * Don't let extents get too large, even if the pieces are contiguous.
1290 	 */
1291 	if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1292 	    left.br_startoff + left.br_blockcount == new->br_startoff &&
1293 	    left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
1294 		state |= BMAP_LEFT_CONTIG;
1295 
1296 	if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1297 	    new->br_startoff + new->br_blockcount == right.br_startoff &&
1298 	    new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
1299 	    (!(state & BMAP_LEFT_CONTIG) ||
1300 	     (left.br_blockcount + new->br_blockcount +
1301 	      right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
1302 		state |= BMAP_RIGHT_CONTIG;
1303 
1304 	/*
1305 	 * Switch out based on the contiguity flags.
1306 	 */
1307 	switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1308 	case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1309 		/*
1310 		 * New allocation is contiguous with delayed allocations
1311 		 * on the left and on the right.
1312 		 * Merge all three into a single extent record.
1313 		 */
1314 		temp = left.br_blockcount + new->br_blockcount +
1315 			right.br_blockcount;
1316 
1317 		oldlen = startblockval(left.br_startblock) +
1318 			startblockval(new->br_startblock) +
1319 			startblockval(right.br_startblock);
1320 		newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1321 					 oldlen);
1322 		left.br_startblock = nullstartblock(newlen);
1323 		left.br_blockcount = temp;
1324 
1325 		xfs_iext_remove(ip, icur, state);
1326 		xfs_iext_prev(ifp, icur);
1327 		xfs_iext_update_extent(ip, state, icur, &left);
1328 		break;
1329 
1330 	case BMAP_LEFT_CONTIG:
1331 		/*
1332 		 * New allocation is contiguous with a delayed allocation
1333 		 * on the left.
1334 		 * Merge the new allocation with the left neighbor.
1335 		 */
1336 		temp = left.br_blockcount + new->br_blockcount;
1337 
1338 		oldlen = startblockval(left.br_startblock) +
1339 			startblockval(new->br_startblock);
1340 		newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1341 					 oldlen);
1342 		left.br_blockcount = temp;
1343 		left.br_startblock = nullstartblock(newlen);
1344 
1345 		xfs_iext_prev(ifp, icur);
1346 		xfs_iext_update_extent(ip, state, icur, &left);
1347 		break;
1348 
1349 	case BMAP_RIGHT_CONTIG:
1350 		/*
1351 		 * New allocation is contiguous with a delayed allocation
1352 		 * on the right.
1353 		 * Merge the new allocation with the right neighbor.
1354 		 */
1355 		temp = new->br_blockcount + right.br_blockcount;
1356 		oldlen = startblockval(new->br_startblock) +
1357 			startblockval(right.br_startblock);
1358 		newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1359 					 oldlen);
1360 		right.br_startoff = new->br_startoff;
1361 		right.br_startblock = nullstartblock(newlen);
1362 		right.br_blockcount = temp;
1363 		xfs_iext_update_extent(ip, state, icur, &right);
1364 		break;
1365 
1366 	case 0:
1367 		/*
1368 		 * New allocation is not contiguous with another
1369 		 * delayed allocation.
1370 		 * Insert a new entry.
1371 		 */
1372 		oldlen = newlen = 0;
1373 		xfs_iext_insert(ip, icur, new, state);
1374 		break;
1375 	}
1376 	if (oldlen != newlen) {
1377 		ASSERT(oldlen > newlen);
1378 		xfs_add_fdblocks(ip->i_mount, oldlen - newlen);
1379 
1380 		/*
1381 		 * Nothing to do for disk quota accounting here.
1382 		 */
1383 		xfs_mod_delalloc(ip, 0, (int64_t)newlen - oldlen);
1384 	}
1385 }
1386 
1387 /*
1388  * Add a delayed allocation extent to an inode. Blocks are reserved from the
1389  * global pool and the extent inserted into the inode in-core extent tree.
1390  *
1391  * On entry, got refers to the first extent beyond the offset of the extent to
1392  * allocate or eof is specified if no such extent exists. On return, got refers
1393  * to the extent record that was inserted to the inode fork.
1394  *
1395  * Note that the allocated extent may have been merged with contiguous extents
1396  * during insertion into the inode fork. Thus, got does not reflect the current
1397  * state of the inode fork on return. If necessary, the caller can use lastx to
1398  * look up the updated record in the inode fork.
1399  */
1400 static int
xfs_bmapi_reserve_delalloc(struct xfs_inode * ip,int whichfork,xfs_fileoff_t off,xfs_filblks_t len,xfs_filblks_t prealloc,struct xfs_bmbt_irec * got,struct xfs_iext_cursor * icur,int eof)1401 xfs_bmapi_reserve_delalloc(
1402 	struct xfs_inode	*ip,
1403 	int			whichfork,
1404 	xfs_fileoff_t		off,
1405 	xfs_filblks_t		len,
1406 	xfs_filblks_t		prealloc,
1407 	struct xfs_bmbt_irec	*got,
1408 	struct xfs_iext_cursor	*icur,
1409 	int			eof)
1410 {
1411 	struct xfs_mount	*mp = ip->i_mount;
1412 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
1413 	xfs_extlen_t		alen;
1414 	xfs_extlen_t		indlen;
1415 	uint64_t		fdblocks;
1416 	int			error;
1417 	xfs_fileoff_t		aoff;
1418 	bool			use_cowextszhint =
1419 					whichfork == XFS_COW_FORK && !prealloc;
1420 
1421 retry:
1422 	/*
1423 	 * Cap the alloc length. Keep track of prealloc so we know whether to
1424 	 * tag the inode before we return.
1425 	 */
1426 	aoff = off;
1427 	alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
1428 	if (!eof)
1429 		alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
1430 	if (prealloc && alen >= len)
1431 		prealloc = alen - len;
1432 
1433 	/*
1434 	 * If we're targetting the COW fork but aren't creating a speculative
1435 	 * posteof preallocation, try to expand the reservation to align with
1436 	 * the COW extent size hint if there's sufficient free space.
1437 	 *
1438 	 * Unlike the data fork, the CoW cancellation functions will free all
1439 	 * the reservations at inactivation, so we don't require that every
1440 	 * delalloc reservation have a dirty pagecache.
1441 	 */
1442 	if (use_cowextszhint) {
1443 		struct xfs_bmbt_irec	prev;
1444 		xfs_extlen_t		extsz = xfs_get_cowextsz_hint(ip);
1445 
1446 		if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
1447 			prev.br_startoff = NULLFILEOFF;
1448 
1449 		error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
1450 					       1, 0, &aoff, &alen);
1451 		ASSERT(!error);
1452 	}
1453 
1454 	/*
1455 	 * Make a transaction-less quota reservation for delayed allocation
1456 	 * blocks.  This number gets adjusted later.  We return if we haven't
1457 	 * allocated blocks already inside this loop.
1458 	 */
1459 	error = xfs_quota_reserve_blkres(ip, alen);
1460 	if (error)
1461 		goto out;
1462 
1463 	/*
1464 	 * Split changing sb for alen and indlen since they could be coming
1465 	 * from different places.
1466 	 */
1467 	indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
1468 	ASSERT(indlen > 0);
1469 
1470 	fdblocks = indlen;
1471 	if (XFS_IS_REALTIME_INODE(ip)) {
1472 		ASSERT(!xfs_is_zoned_inode(ip));
1473 		error = xfs_dec_frextents(mp, xfs_blen_to_rtbxlen(mp, alen));
1474 		if (error)
1475 			goto out_unreserve_quota;
1476 	} else {
1477 		fdblocks += alen;
1478 	}
1479 
1480 	error = xfs_dec_fdblocks(mp, fdblocks, false);
1481 	if (error)
1482 		goto out_unreserve_frextents;
1483 
1484 	ip->i_delayed_blks += alen;
1485 	xfs_mod_delalloc(ip, alen, indlen);
1486 
1487 	got->br_startoff = aoff;
1488 	got->br_startblock = nullstartblock(indlen);
1489 	got->br_blockcount = alen;
1490 	got->br_state = XFS_EXT_NORM;
1491 
1492 	xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
1493 
1494 	/*
1495 	 * Tag the inode if blocks were preallocated. Note that COW fork
1496 	 * preallocation can occur at the start or end of the extent, even when
1497 	 * prealloc == 0, so we must also check the aligned offset and length.
1498 	 */
1499 	if (whichfork == XFS_DATA_FORK && prealloc)
1500 		xfs_inode_set_eofblocks_tag(ip);
1501 	if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
1502 		xfs_inode_set_cowblocks_tag(ip);
1503 
1504 	return 0;
1505 
1506 out_unreserve_frextents:
1507 	if (XFS_IS_REALTIME_INODE(ip))
1508 		xfs_add_frextents(mp, xfs_blen_to_rtbxlen(mp, alen));
1509 out_unreserve_quota:
1510 	if (XFS_IS_QUOTA_ON(mp))
1511 		xfs_quota_unreserve_blkres(ip, alen);
1512 out:
1513 	if (error == -ENOSPC || error == -EDQUOT) {
1514 		trace_xfs_delalloc_enospc(ip, off, len);
1515 
1516 		if (prealloc || use_cowextszhint) {
1517 			/* retry without any preallocation */
1518 			use_cowextszhint = false;
1519 			prealloc = 0;
1520 			goto retry;
1521 		}
1522 	}
1523 	return error;
1524 }
1525 
1526 static int
xfs_zoned_buffered_write_iomap_begin(struct inode * inode,loff_t offset,loff_t count,unsigned flags,struct iomap * iomap,struct iomap * srcmap)1527 xfs_zoned_buffered_write_iomap_begin(
1528 	struct inode		*inode,
1529 	loff_t			offset,
1530 	loff_t			count,
1531 	unsigned		flags,
1532 	struct iomap		*iomap,
1533 	struct iomap		*srcmap)
1534 {
1535 	struct iomap_iter	*iter =
1536 		container_of(iomap, struct iomap_iter, iomap);
1537 	struct xfs_zone_alloc_ctx *ac = iter->private;
1538 	struct xfs_inode	*ip = XFS_I(inode);
1539 	struct xfs_mount	*mp = ip->i_mount;
1540 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
1541 	xfs_fileoff_t		end_fsb = xfs_iomap_end_fsb(mp, offset, count);
1542 	u16			iomap_flags = IOMAP_F_SHARED;
1543 	unsigned int		lockmode = XFS_ILOCK_EXCL;
1544 	xfs_filblks_t		count_fsb;
1545 	xfs_extlen_t		indlen;
1546 	struct xfs_bmbt_irec	got;
1547 	struct xfs_iext_cursor	icur;
1548 	int			error = 0;
1549 
1550 	ASSERT(!xfs_get_extsz_hint(ip));
1551 	ASSERT(!(flags & IOMAP_UNSHARE));
1552 	ASSERT(ac);
1553 
1554 	if (xfs_is_shutdown(mp))
1555 		return -EIO;
1556 
1557 	error = xfs_qm_dqattach(ip);
1558 	if (error)
1559 		return error;
1560 
1561 	error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1562 	if (error)
1563 		return error;
1564 
1565 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(&ip->i_df)) ||
1566 	    XFS_TEST_ERROR(mp, XFS_ERRTAG_BMAPIFORMAT)) {
1567 		xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
1568 		error = -EFSCORRUPTED;
1569 		goto out_unlock;
1570 	}
1571 
1572 	XFS_STATS_INC(mp, xs_blk_mapw);
1573 
1574 	error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1575 	if (error)
1576 		goto out_unlock;
1577 
1578 	/*
1579 	 * For zeroing operations check if there is any data to zero first.
1580 	 *
1581 	 * For regular writes we always need to allocate new blocks, but need to
1582 	 * provide the source mapping when the range is unaligned to support
1583 	 * read-modify-write of the whole block in the page cache.
1584 	 *
1585 	 * In either case we need to limit the reported range to the boundaries
1586 	 * of the source map in the data fork.
1587 	 */
1588 	if (!IS_ALIGNED(offset, mp->m_sb.sb_blocksize) ||
1589 	    !IS_ALIGNED(offset + count, mp->m_sb.sb_blocksize) ||
1590 	    (flags & IOMAP_ZERO)) {
1591 		struct xfs_bmbt_irec	smap;
1592 		struct xfs_iext_cursor	scur;
1593 
1594 		if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &scur,
1595 				&smap))
1596 			smap.br_startoff = end_fsb; /* fake hole until EOF */
1597 		if (smap.br_startoff > offset_fsb) {
1598 			/*
1599 			 * We never need to allocate blocks for zeroing a hole.
1600 			 */
1601 			if (flags & IOMAP_ZERO) {
1602 				xfs_hole_to_iomap(ip, iomap, offset_fsb,
1603 						smap.br_startoff);
1604 				goto out_unlock;
1605 			}
1606 			end_fsb = min(end_fsb, smap.br_startoff);
1607 		} else {
1608 			end_fsb = min(end_fsb,
1609 				smap.br_startoff + smap.br_blockcount);
1610 			xfs_trim_extent(&smap, offset_fsb,
1611 					end_fsb - offset_fsb);
1612 			error = xfs_bmbt_to_iomap(ip, srcmap, &smap, flags, 0,
1613 					xfs_iomap_inode_sequence(ip, 0));
1614 			if (error)
1615 				goto out_unlock;
1616 		}
1617 	}
1618 
1619 	if (!ip->i_cowfp)
1620 		xfs_ifork_init_cow(ip);
1621 
1622 	if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got))
1623 		got.br_startoff = end_fsb;
1624 	if (got.br_startoff <= offset_fsb) {
1625 		trace_xfs_reflink_cow_found(ip, &got);
1626 		goto done;
1627 	}
1628 
1629 	/*
1630 	 * Cap the maximum length to keep the chunks of work done here somewhat
1631 	 * symmetric with the work writeback does.
1632 	 */
1633 	end_fsb = min(end_fsb, got.br_startoff);
1634 	count_fsb = min3(end_fsb - offset_fsb, XFS_MAX_BMBT_EXTLEN,
1635 			 XFS_B_TO_FSB(mp, 1024 * PAGE_SIZE));
1636 
1637 	/*
1638 	 * The block reservation is supposed to cover all blocks that the
1639 	 * operation could possible write, but there is a nasty corner case
1640 	 * where blocks could be stolen from underneath us:
1641 	 *
1642 	 *  1) while this thread iterates over a larger buffered write,
1643 	 *  2) another thread is causing a write fault that calls into
1644 	 *     ->page_mkwrite in range this thread writes to, using up the
1645 	 *     delalloc reservation created by a previous call to this function.
1646 	 *  3) another thread does direct I/O on the range that the write fault
1647 	 *     happened on, which causes writeback of the dirty data.
1648 	 *  4) this then set the stale flag, which cuts the current iomap
1649 	 *     iteration short, causing the new call to ->iomap_begin that gets
1650 	 *     us here again, but now without a sufficient reservation.
1651 	 *
1652 	 * This is a very unusual I/O pattern, and nothing but generic/095 is
1653 	 * known to hit it. There's not really much we can do here, so turn this
1654 	 * into a short write.
1655 	 */
1656 	if (count_fsb > ac->reserved_blocks) {
1657 		xfs_warn_ratelimited(mp,
1658 "Short write on ino 0x%llx comm %.20s due to three-way race with write fault and direct I/O",
1659 			ip->i_ino, current->comm);
1660 		count_fsb = ac->reserved_blocks;
1661 		if (!count_fsb) {
1662 			error = -EIO;
1663 			goto out_unlock;
1664 		}
1665 	}
1666 
1667 	error = xfs_quota_reserve_blkres(ip, count_fsb);
1668 	if (error)
1669 		goto out_unlock;
1670 
1671 	indlen = xfs_bmap_worst_indlen(ip, count_fsb);
1672 	error = xfs_dec_fdblocks(mp, indlen, false);
1673 	if (error)
1674 		goto out_unlock;
1675 	ip->i_delayed_blks += count_fsb;
1676 	xfs_mod_delalloc(ip, count_fsb, indlen);
1677 
1678 	got.br_startoff = offset_fsb;
1679 	got.br_startblock = nullstartblock(indlen);
1680 	got.br_blockcount = count_fsb;
1681 	got.br_state = XFS_EXT_NORM;
1682 	xfs_bmap_add_extent_hole_delay(ip, XFS_COW_FORK, &icur, &got);
1683 	ac->reserved_blocks -= count_fsb;
1684 	iomap_flags |= IOMAP_F_NEW;
1685 
1686 	trace_xfs_iomap_alloc(ip, offset, XFS_FSB_TO_B(mp, count_fsb),
1687 			XFS_COW_FORK, &got);
1688 done:
1689 	error = xfs_bmbt_to_iomap(ip, iomap, &got, flags, iomap_flags,
1690 			xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED));
1691 out_unlock:
1692 	xfs_iunlock(ip, lockmode);
1693 	return error;
1694 }
1695 
1696 static int
xfs_buffered_write_iomap_begin(struct inode * inode,loff_t offset,loff_t count,unsigned flags,struct iomap * iomap,struct iomap * srcmap)1697 xfs_buffered_write_iomap_begin(
1698 	struct inode		*inode,
1699 	loff_t			offset,
1700 	loff_t			count,
1701 	unsigned		flags,
1702 	struct iomap		*iomap,
1703 	struct iomap		*srcmap)
1704 {
1705 	struct xfs_inode	*ip = XFS_I(inode);
1706 	struct xfs_mount	*mp = ip->i_mount;
1707 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
1708 	xfs_fileoff_t		end_fsb = xfs_iomap_end_fsb(mp, offset, count);
1709 	struct xfs_bmbt_irec	imap, cmap;
1710 	struct xfs_iext_cursor	icur, ccur;
1711 	xfs_fsblock_t		prealloc_blocks = 0;
1712 	bool			eof = false, cow_eof = false, shared = false;
1713 	int			allocfork = XFS_DATA_FORK;
1714 	int			error = 0;
1715 	unsigned int		lockmode = XFS_ILOCK_EXCL;
1716 	unsigned int		iomap_flags = 0;
1717 	u64			seq;
1718 
1719 	if (xfs_is_shutdown(mp))
1720 		return -EIO;
1721 
1722 	if (xfs_is_zoned_inode(ip))
1723 		return xfs_zoned_buffered_write_iomap_begin(inode, offset,
1724 				count, flags, iomap, srcmap);
1725 
1726 	/* we can't use delayed allocations when using extent size hints */
1727 	if (xfs_get_extsz_hint(ip))
1728 		return xfs_direct_write_iomap_begin(inode, offset, count,
1729 				flags, iomap, srcmap);
1730 
1731 	error = xfs_qm_dqattach(ip);
1732 	if (error)
1733 		return error;
1734 
1735 	error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1736 	if (error)
1737 		return error;
1738 
1739 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(&ip->i_df)) ||
1740 	    XFS_TEST_ERROR(mp, XFS_ERRTAG_BMAPIFORMAT)) {
1741 		xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
1742 		error = -EFSCORRUPTED;
1743 		goto out_unlock;
1744 	}
1745 
1746 	XFS_STATS_INC(mp, xs_blk_mapw);
1747 
1748 	error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1749 	if (error)
1750 		goto out_unlock;
1751 
1752 	/*
1753 	 * Search the data fork first to look up our source mapping.  We
1754 	 * always need the data fork map, as we have to return it to the
1755 	 * iomap code so that the higher level write code can read data in to
1756 	 * perform read-modify-write cycles for unaligned writes.
1757 	 */
1758 	eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
1759 	if (eof)
1760 		imap.br_startoff = end_fsb; /* fake hole until the end */
1761 
1762 	/* We never need to allocate blocks for zeroing or unsharing a hole. */
1763 	if ((flags & (IOMAP_UNSHARE | IOMAP_ZERO)) &&
1764 	    imap.br_startoff > offset_fsb) {
1765 		xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
1766 		goto out_unlock;
1767 	}
1768 
1769 	/*
1770 	 * For zeroing, trim a delalloc extent that extends beyond the EOF
1771 	 * block.  If it starts beyond the EOF block, convert it to an
1772 	 * unwritten extent.
1773 	 */
1774 	if ((flags & IOMAP_ZERO) && imap.br_startoff <= offset_fsb &&
1775 	    isnullstartblock(imap.br_startblock)) {
1776 		xfs_fileoff_t eof_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
1777 
1778 		if (offset_fsb >= eof_fsb)
1779 			goto convert_delay;
1780 		if (end_fsb > eof_fsb) {
1781 			end_fsb = eof_fsb;
1782 			xfs_trim_extent(&imap, offset_fsb,
1783 					end_fsb - offset_fsb);
1784 		}
1785 	}
1786 
1787 	/*
1788 	 * Search the COW fork extent list even if we did not find a data fork
1789 	 * extent.  This serves two purposes: first this implements the
1790 	 * speculative preallocation using cowextsize, so that we also unshare
1791 	 * block adjacent to shared blocks instead of just the shared blocks
1792 	 * themselves.  Second the lookup in the extent list is generally faster
1793 	 * than going out to the shared extent tree.
1794 	 */
1795 	if (xfs_is_cow_inode(ip)) {
1796 		if (!ip->i_cowfp) {
1797 			ASSERT(!xfs_is_reflink_inode(ip));
1798 			xfs_ifork_init_cow(ip);
1799 		}
1800 		cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
1801 				&ccur, &cmap);
1802 		if (!cow_eof && cmap.br_startoff <= offset_fsb) {
1803 			trace_xfs_reflink_cow_found(ip, &cmap);
1804 			goto found_cow;
1805 		}
1806 	}
1807 
1808 	if (imap.br_startoff <= offset_fsb) {
1809 		/*
1810 		 * For reflink files we may need a delalloc reservation when
1811 		 * overwriting shared extents.   This includes zeroing of
1812 		 * existing extents that contain data.
1813 		 */
1814 		if (!xfs_is_cow_inode(ip) ||
1815 		    ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
1816 			trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
1817 					&imap);
1818 			goto found_imap;
1819 		}
1820 
1821 		xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
1822 
1823 		/* Trim the mapping to the nearest shared extent boundary. */
1824 		error = xfs_bmap_trim_cow(ip, &imap, &shared);
1825 		if (error)
1826 			goto out_unlock;
1827 
1828 		/* Not shared?  Just report the (potentially capped) extent. */
1829 		if (!shared) {
1830 			trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
1831 					&imap);
1832 			goto found_imap;
1833 		}
1834 
1835 		/*
1836 		 * Fork all the shared blocks from our write offset until the
1837 		 * end of the extent.
1838 		 */
1839 		allocfork = XFS_COW_FORK;
1840 		end_fsb = imap.br_startoff + imap.br_blockcount;
1841 	} else {
1842 		/*
1843 		 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
1844 		 * pages to keep the chunks of work done where somewhat
1845 		 * symmetric with the work writeback does.  This is a completely
1846 		 * arbitrary number pulled out of thin air.
1847 		 *
1848 		 * Note that the values needs to be less than 32-bits wide until
1849 		 * the lower level functions are updated.
1850 		 */
1851 		count = min_t(loff_t, count, 1024 * PAGE_SIZE);
1852 		end_fsb = xfs_iomap_end_fsb(mp, offset, count);
1853 
1854 		if (xfs_is_always_cow_inode(ip))
1855 			allocfork = XFS_COW_FORK;
1856 	}
1857 
1858 	if (eof && offset + count > XFS_ISIZE(ip)) {
1859 		/*
1860 		 * Determine the initial size of the preallocation.
1861 		 * We clean up any extra preallocation when the file is closed.
1862 		 */
1863 		if (xfs_has_allocsize(mp))
1864 			prealloc_blocks = mp->m_allocsize_blocks;
1865 		else if (allocfork == XFS_DATA_FORK)
1866 			prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork,
1867 						offset, count, &icur);
1868 		else
1869 			prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork,
1870 						offset, count, &ccur);
1871 		if (prealloc_blocks) {
1872 			xfs_extlen_t	align;
1873 			xfs_off_t	end_offset;
1874 			xfs_fileoff_t	p_end_fsb;
1875 
1876 			end_offset = XFS_ALLOC_ALIGN(mp, offset + count - 1);
1877 			p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
1878 					prealloc_blocks;
1879 
1880 			align = xfs_eof_alignment(ip);
1881 			if (align)
1882 				p_end_fsb = roundup_64(p_end_fsb, align);
1883 
1884 			p_end_fsb = min(p_end_fsb,
1885 				XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
1886 			ASSERT(p_end_fsb > offset_fsb);
1887 			prealloc_blocks = p_end_fsb - end_fsb;
1888 		}
1889 	}
1890 
1891 	/*
1892 	 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
1893 	 * them out if the write happens to fail.
1894 	 */
1895 	iomap_flags |= IOMAP_F_NEW;
1896 	if (allocfork == XFS_COW_FORK) {
1897 		error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
1898 				end_fsb - offset_fsb, prealloc_blocks, &cmap,
1899 				&ccur, cow_eof);
1900 		if (error)
1901 			goto out_unlock;
1902 
1903 		trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap);
1904 		goto found_cow;
1905 	}
1906 
1907 	error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
1908 			end_fsb - offset_fsb, prealloc_blocks, &imap, &icur,
1909 			eof);
1910 	if (error)
1911 		goto out_unlock;
1912 
1913 	trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap);
1914 found_imap:
1915 	seq = xfs_iomap_inode_sequence(ip, iomap_flags);
1916 	xfs_iunlock(ip, lockmode);
1917 	return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags, seq);
1918 
1919 convert_delay:
1920 	xfs_iunlock(ip, lockmode);
1921 	truncate_pagecache(inode, offset);
1922 	error = xfs_bmapi_convert_delalloc(ip, XFS_DATA_FORK, offset,
1923 					   iomap, NULL);
1924 	if (error)
1925 		return error;
1926 
1927 	trace_xfs_iomap_alloc(ip, offset, count, XFS_DATA_FORK, &imap);
1928 	return 0;
1929 
1930 found_cow:
1931 	if (imap.br_startoff <= offset_fsb) {
1932 		error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0,
1933 				xfs_iomap_inode_sequence(ip, 0));
1934 		if (error)
1935 			goto out_unlock;
1936 	} else {
1937 		xfs_trim_extent(&cmap, offset_fsb,
1938 				imap.br_startoff - offset_fsb);
1939 	}
1940 
1941 	iomap_flags |= IOMAP_F_SHARED;
1942 	seq = xfs_iomap_inode_sequence(ip, iomap_flags);
1943 	xfs_iunlock(ip, lockmode);
1944 	return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, iomap_flags, seq);
1945 
1946 out_unlock:
1947 	xfs_iunlock(ip, lockmode);
1948 	return error;
1949 }
1950 
1951 static void
xfs_buffered_write_delalloc_punch(struct inode * inode,loff_t offset,loff_t length,struct iomap * iomap)1952 xfs_buffered_write_delalloc_punch(
1953 	struct inode		*inode,
1954 	loff_t			offset,
1955 	loff_t			length,
1956 	struct iomap		*iomap)
1957 {
1958 	struct iomap_iter	*iter =
1959 		container_of(iomap, struct iomap_iter, iomap);
1960 
1961 	xfs_bmap_punch_delalloc_range(XFS_I(inode),
1962 			(iomap->flags & IOMAP_F_SHARED) ?
1963 				XFS_COW_FORK : XFS_DATA_FORK,
1964 			offset, offset + length, iter->private);
1965 }
1966 
1967 static int
xfs_buffered_write_iomap_end(struct inode * inode,loff_t offset,loff_t length,ssize_t written,unsigned flags,struct iomap * iomap)1968 xfs_buffered_write_iomap_end(
1969 	struct inode		*inode,
1970 	loff_t			offset,
1971 	loff_t			length,
1972 	ssize_t			written,
1973 	unsigned		flags,
1974 	struct iomap		*iomap)
1975 {
1976 	loff_t			start_byte, end_byte;
1977 
1978 	/* If we didn't reserve the blocks, we're not allowed to punch them. */
1979 	if (iomap->type != IOMAP_DELALLOC || !(iomap->flags & IOMAP_F_NEW))
1980 		return 0;
1981 
1982 	/*
1983 	 * iomap_page_mkwrite() will never fail in a way that requires delalloc
1984 	 * extents that it allocated to be revoked.  Hence never try to release
1985 	 * them here.
1986 	 */
1987 	if (flags & IOMAP_FAULT)
1988 		return 0;
1989 
1990 	/* Nothing to do if we've written the entire delalloc extent */
1991 	start_byte = iomap_last_written_block(inode, offset, written);
1992 	end_byte = round_up(offset + length, i_blocksize(inode));
1993 	if (start_byte >= end_byte)
1994 		return 0;
1995 
1996 	/* For zeroing operations the callers already hold invalidate_lock. */
1997 	if (flags & (IOMAP_UNSHARE | IOMAP_ZERO)) {
1998 		rwsem_assert_held_write(&inode->i_mapping->invalidate_lock);
1999 		iomap_write_delalloc_release(inode, start_byte, end_byte, flags,
2000 				iomap, xfs_buffered_write_delalloc_punch);
2001 	} else {
2002 		filemap_invalidate_lock(inode->i_mapping);
2003 		iomap_write_delalloc_release(inode, start_byte, end_byte, flags,
2004 				iomap, xfs_buffered_write_delalloc_punch);
2005 		filemap_invalidate_unlock(inode->i_mapping);
2006 	}
2007 
2008 	return 0;
2009 }
2010 
2011 const struct iomap_ops xfs_buffered_write_iomap_ops = {
2012 	.iomap_begin		= xfs_buffered_write_iomap_begin,
2013 	.iomap_end		= xfs_buffered_write_iomap_end,
2014 };
2015 
2016 static int
xfs_read_iomap_begin(struct inode * inode,loff_t offset,loff_t length,unsigned flags,struct iomap * iomap,struct iomap * srcmap)2017 xfs_read_iomap_begin(
2018 	struct inode		*inode,
2019 	loff_t			offset,
2020 	loff_t			length,
2021 	unsigned		flags,
2022 	struct iomap		*iomap,
2023 	struct iomap		*srcmap)
2024 {
2025 	struct xfs_inode	*ip = XFS_I(inode);
2026 	struct xfs_mount	*mp = ip->i_mount;
2027 	struct xfs_bmbt_irec	imap;
2028 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
2029 	xfs_fileoff_t		end_fsb = xfs_iomap_end_fsb(mp, offset, length);
2030 	int			nimaps = 1, error = 0;
2031 	bool			shared = false;
2032 	unsigned int		lockmode = XFS_ILOCK_SHARED;
2033 	u64			seq;
2034 
2035 	ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO)));
2036 
2037 	if (xfs_is_shutdown(mp))
2038 		return -EIO;
2039 
2040 	error = xfs_ilock_for_iomap(ip, flags, &lockmode);
2041 	if (error)
2042 		return error;
2043 	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
2044 			       &nimaps, 0);
2045 	if (!error && ((flags & IOMAP_REPORT) || IS_DAX(inode)))
2046 		error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
2047 	seq = xfs_iomap_inode_sequence(ip, shared ? IOMAP_F_SHARED : 0);
2048 	xfs_iunlock(ip, lockmode);
2049 
2050 	if (error)
2051 		return error;
2052 	trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
2053 	return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
2054 				 shared ? IOMAP_F_SHARED : 0, seq);
2055 }
2056 
2057 const struct iomap_ops xfs_read_iomap_ops = {
2058 	.iomap_begin		= xfs_read_iomap_begin,
2059 };
2060 
2061 static int
xfs_seek_iomap_begin(struct inode * inode,loff_t offset,loff_t length,unsigned flags,struct iomap * iomap,struct iomap * srcmap)2062 xfs_seek_iomap_begin(
2063 	struct inode		*inode,
2064 	loff_t			offset,
2065 	loff_t			length,
2066 	unsigned		flags,
2067 	struct iomap		*iomap,
2068 	struct iomap		*srcmap)
2069 {
2070 	struct xfs_inode	*ip = XFS_I(inode);
2071 	struct xfs_mount	*mp = ip->i_mount;
2072 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
2073 	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, offset + length);
2074 	xfs_fileoff_t		cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
2075 	struct xfs_iext_cursor	icur;
2076 	struct xfs_bmbt_irec	imap, cmap;
2077 	int			error = 0;
2078 	unsigned		lockmode;
2079 	u64			seq;
2080 
2081 	if (xfs_is_shutdown(mp))
2082 		return -EIO;
2083 
2084 	lockmode = xfs_ilock_data_map_shared(ip);
2085 	error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
2086 	if (error)
2087 		goto out_unlock;
2088 
2089 	if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
2090 		/*
2091 		 * If we found a data extent we are done.
2092 		 */
2093 		if (imap.br_startoff <= offset_fsb)
2094 			goto done;
2095 		data_fsb = imap.br_startoff;
2096 	} else {
2097 		/*
2098 		 * Fake a hole until the end of the file.
2099 		 */
2100 		data_fsb = xfs_iomap_end_fsb(mp, offset, length);
2101 	}
2102 
2103 	/*
2104 	 * If a COW fork extent covers the hole, report it - capped to the next
2105 	 * data fork extent:
2106 	 */
2107 	if (xfs_inode_has_cow_data(ip) &&
2108 	    xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
2109 		cow_fsb = cmap.br_startoff;
2110 	if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
2111 		if (data_fsb < cow_fsb + cmap.br_blockcount)
2112 			end_fsb = min(end_fsb, data_fsb);
2113 		xfs_trim_extent(&cmap, offset_fsb, end_fsb - offset_fsb);
2114 		seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED);
2115 		error = xfs_bmbt_to_iomap(ip, iomap, &cmap, flags,
2116 				IOMAP_F_SHARED, seq);
2117 		/*
2118 		 * This is a COW extent, so we must probe the page cache
2119 		 * because there could be dirty page cache being backed
2120 		 * by this extent.
2121 		 */
2122 		iomap->type = IOMAP_UNWRITTEN;
2123 		goto out_unlock;
2124 	}
2125 
2126 	/*
2127 	 * Else report a hole, capped to the next found data or COW extent.
2128 	 */
2129 	if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
2130 		imap.br_blockcount = cow_fsb - offset_fsb;
2131 	else
2132 		imap.br_blockcount = data_fsb - offset_fsb;
2133 	imap.br_startoff = offset_fsb;
2134 	imap.br_startblock = HOLESTARTBLOCK;
2135 	imap.br_state = XFS_EXT_NORM;
2136 done:
2137 	seq = xfs_iomap_inode_sequence(ip, 0);
2138 	xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
2139 	error = xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0, seq);
2140 out_unlock:
2141 	xfs_iunlock(ip, lockmode);
2142 	return error;
2143 }
2144 
2145 const struct iomap_ops xfs_seek_iomap_ops = {
2146 	.iomap_begin		= xfs_seek_iomap_begin,
2147 };
2148 
2149 static int
xfs_xattr_iomap_begin(struct inode * inode,loff_t offset,loff_t length,unsigned flags,struct iomap * iomap,struct iomap * srcmap)2150 xfs_xattr_iomap_begin(
2151 	struct inode		*inode,
2152 	loff_t			offset,
2153 	loff_t			length,
2154 	unsigned		flags,
2155 	struct iomap		*iomap,
2156 	struct iomap		*srcmap)
2157 {
2158 	struct xfs_inode	*ip = XFS_I(inode);
2159 	struct xfs_mount	*mp = ip->i_mount;
2160 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
2161 	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, offset + length);
2162 	struct xfs_bmbt_irec	imap;
2163 	int			nimaps = 1, error = 0;
2164 	unsigned		lockmode;
2165 	int			seq;
2166 
2167 	if (xfs_is_shutdown(mp))
2168 		return -EIO;
2169 
2170 	lockmode = xfs_ilock_attr_map_shared(ip);
2171 
2172 	/* if there are no attribute fork or extents, return ENOENT */
2173 	if (!xfs_inode_has_attr_fork(ip) || !ip->i_af.if_nextents) {
2174 		error = -ENOENT;
2175 		goto out_unlock;
2176 	}
2177 
2178 	ASSERT(ip->i_af.if_format != XFS_DINODE_FMT_LOCAL);
2179 	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
2180 			       &nimaps, XFS_BMAPI_ATTRFORK);
2181 out_unlock:
2182 
2183 	seq = xfs_iomap_inode_sequence(ip, IOMAP_F_XATTR);
2184 	xfs_iunlock(ip, lockmode);
2185 
2186 	if (error)
2187 		return error;
2188 	ASSERT(nimaps);
2189 	return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, IOMAP_F_XATTR, seq);
2190 }
2191 
2192 const struct iomap_ops xfs_xattr_iomap_ops = {
2193 	.iomap_begin		= xfs_xattr_iomap_begin,
2194 };
2195 
2196 int
xfs_zero_range(struct xfs_inode * ip,loff_t pos,loff_t len,struct xfs_zone_alloc_ctx * ac,bool * did_zero)2197 xfs_zero_range(
2198 	struct xfs_inode	*ip,
2199 	loff_t			pos,
2200 	loff_t			len,
2201 	struct xfs_zone_alloc_ctx *ac,
2202 	bool			*did_zero)
2203 {
2204 	struct inode		*inode = VFS_I(ip);
2205 
2206 	xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL);
2207 
2208 	if (IS_DAX(inode))
2209 		return dax_zero_range(inode, pos, len, did_zero,
2210 				      &xfs_dax_write_iomap_ops);
2211 	return iomap_zero_range(inode, pos, len, did_zero,
2212 			&xfs_buffered_write_iomap_ops, &xfs_iomap_write_ops,
2213 			ac);
2214 }
2215 
2216 int
xfs_truncate_page(struct xfs_inode * ip,loff_t pos,struct xfs_zone_alloc_ctx * ac,bool * did_zero)2217 xfs_truncate_page(
2218 	struct xfs_inode	*ip,
2219 	loff_t			pos,
2220 	struct xfs_zone_alloc_ctx *ac,
2221 	bool			*did_zero)
2222 {
2223 	struct inode		*inode = VFS_I(ip);
2224 
2225 	if (IS_DAX(inode))
2226 		return dax_truncate_page(inode, pos, did_zero,
2227 					&xfs_dax_write_iomap_ops);
2228 	return iomap_truncate_page(inode, pos, did_zero,
2229 			&xfs_buffered_write_iomap_ops, &xfs_iomap_write_ops,
2230 			ac);
2231 }
2232