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