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