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