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