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