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