Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6cbd5570 CM |
2 | /* |
3 | * Copyright (C) 2007 Oracle. All rights reserved. | |
6cbd5570 CM |
4 | */ |
5 | ||
39279cc3 CM |
6 | #include <linux/fs.h> |
7 | #include <linux/pagemap.h> | |
39279cc3 CM |
8 | #include <linux/time.h> |
9 | #include <linux/init.h> | |
10 | #include <linux/string.h> | |
39279cc3 | 11 | #include <linux/backing-dev.h> |
2fe17c10 | 12 | #include <linux/falloc.h> |
39279cc3 | 13 | #include <linux/writeback.h> |
39279cc3 | 14 | #include <linux/compat.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
55e301fd | 16 | #include <linux/btrfs.h> |
e2e40f2c | 17 | #include <linux/uio.h> |
ae5e165d | 18 | #include <linux/iversion.h> |
14605409 | 19 | #include <linux/fsverity.h> |
39279cc3 CM |
20 | #include "ctree.h" |
21 | #include "disk-io.h" | |
22 | #include "transaction.h" | |
23 | #include "btrfs_inode.h" | |
39279cc3 | 24 | #include "print-tree.h" |
e02119d5 CM |
25 | #include "tree-log.h" |
26 | #include "locking.h" | |
2aaa6655 | 27 | #include "volumes.h" |
fcebe456 | 28 | #include "qgroup.h" |
ebb8765b | 29 | #include "compression.h" |
86736342 | 30 | #include "delalloc-space.h" |
6a177381 | 31 | #include "reflink.h" |
f02a85d2 | 32 | #include "subpage.h" |
39279cc3 | 33 | |
9247f317 | 34 | static struct kmem_cache *btrfs_inode_defrag_cachep; |
4cb5300b CM |
35 | /* |
36 | * when auto defrag is enabled we | |
37 | * queue up these defrag structs to remember which | |
38 | * inodes need defragging passes | |
39 | */ | |
40 | struct inode_defrag { | |
41 | struct rb_node rb_node; | |
42 | /* objectid */ | |
43 | u64 ino; | |
44 | /* | |
45 | * transid where the defrag was added, we search for | |
46 | * extents newer than this | |
47 | */ | |
48 | u64 transid; | |
49 | ||
50 | /* root objectid */ | |
51 | u64 root; | |
558732df QW |
52 | |
53 | /* | |
54 | * The extent size threshold for autodefrag. | |
55 | * | |
56 | * This value is different for compressed/non-compressed extents, | |
57 | * thus needs to be passed from higher layer. | |
58 | * (aka, inode_should_defrag()) | |
59 | */ | |
60 | u32 extent_thresh; | |
4cb5300b CM |
61 | }; |
62 | ||
762f2263 MX |
63 | static int __compare_inode_defrag(struct inode_defrag *defrag1, |
64 | struct inode_defrag *defrag2) | |
65 | { | |
66 | if (defrag1->root > defrag2->root) | |
67 | return 1; | |
68 | else if (defrag1->root < defrag2->root) | |
69 | return -1; | |
70 | else if (defrag1->ino > defrag2->ino) | |
71 | return 1; | |
72 | else if (defrag1->ino < defrag2->ino) | |
73 | return -1; | |
74 | else | |
75 | return 0; | |
76 | } | |
77 | ||
4cb5300b CM |
78 | /* pop a record for an inode into the defrag tree. The lock |
79 | * must be held already | |
80 | * | |
81 | * If you're inserting a record for an older transid than an | |
82 | * existing record, the transid already in the tree is lowered | |
83 | * | |
84 | * If an existing record is found the defrag item you | |
85 | * pass in is freed | |
86 | */ | |
6158e1ce | 87 | static int __btrfs_add_inode_defrag(struct btrfs_inode *inode, |
4cb5300b CM |
88 | struct inode_defrag *defrag) |
89 | { | |
3ffbd68c | 90 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
4cb5300b CM |
91 | struct inode_defrag *entry; |
92 | struct rb_node **p; | |
93 | struct rb_node *parent = NULL; | |
762f2263 | 94 | int ret; |
4cb5300b | 95 | |
0b246afa | 96 | p = &fs_info->defrag_inodes.rb_node; |
4cb5300b CM |
97 | while (*p) { |
98 | parent = *p; | |
99 | entry = rb_entry(parent, struct inode_defrag, rb_node); | |
100 | ||
762f2263 MX |
101 | ret = __compare_inode_defrag(defrag, entry); |
102 | if (ret < 0) | |
4cb5300b | 103 | p = &parent->rb_left; |
762f2263 | 104 | else if (ret > 0) |
4cb5300b CM |
105 | p = &parent->rb_right; |
106 | else { | |
107 | /* if we're reinserting an entry for | |
108 | * an old defrag run, make sure to | |
109 | * lower the transid of our existing record | |
110 | */ | |
111 | if (defrag->transid < entry->transid) | |
112 | entry->transid = defrag->transid; | |
558732df QW |
113 | entry->extent_thresh = min(defrag->extent_thresh, |
114 | entry->extent_thresh); | |
8ddc4734 | 115 | return -EEXIST; |
4cb5300b CM |
116 | } |
117 | } | |
6158e1ce | 118 | set_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags); |
4cb5300b | 119 | rb_link_node(&defrag->rb_node, parent, p); |
0b246afa | 120 | rb_insert_color(&defrag->rb_node, &fs_info->defrag_inodes); |
8ddc4734 MX |
121 | return 0; |
122 | } | |
4cb5300b | 123 | |
2ff7e61e | 124 | static inline int __need_auto_defrag(struct btrfs_fs_info *fs_info) |
8ddc4734 | 125 | { |
0b246afa | 126 | if (!btrfs_test_opt(fs_info, AUTO_DEFRAG)) |
8ddc4734 MX |
127 | return 0; |
128 | ||
0b246afa | 129 | if (btrfs_fs_closing(fs_info)) |
8ddc4734 | 130 | return 0; |
4cb5300b | 131 | |
8ddc4734 | 132 | return 1; |
4cb5300b CM |
133 | } |
134 | ||
135 | /* | |
136 | * insert a defrag record for this inode if auto defrag is | |
137 | * enabled | |
138 | */ | |
139 | int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans, | |
558732df | 140 | struct btrfs_inode *inode, u32 extent_thresh) |
4cb5300b | 141 | { |
6158e1ce | 142 | struct btrfs_root *root = inode->root; |
3ffbd68c | 143 | struct btrfs_fs_info *fs_info = root->fs_info; |
4cb5300b | 144 | struct inode_defrag *defrag; |
4cb5300b | 145 | u64 transid; |
8ddc4734 | 146 | int ret; |
4cb5300b | 147 | |
2ff7e61e | 148 | if (!__need_auto_defrag(fs_info)) |
4cb5300b CM |
149 | return 0; |
150 | ||
6158e1ce | 151 | if (test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags)) |
4cb5300b CM |
152 | return 0; |
153 | ||
154 | if (trans) | |
155 | transid = trans->transid; | |
156 | else | |
6158e1ce | 157 | transid = inode->root->last_trans; |
4cb5300b | 158 | |
9247f317 | 159 | defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS); |
4cb5300b CM |
160 | if (!defrag) |
161 | return -ENOMEM; | |
162 | ||
6158e1ce | 163 | defrag->ino = btrfs_ino(inode); |
4cb5300b CM |
164 | defrag->transid = transid; |
165 | defrag->root = root->root_key.objectid; | |
558732df | 166 | defrag->extent_thresh = extent_thresh; |
4cb5300b | 167 | |
0b246afa | 168 | spin_lock(&fs_info->defrag_inodes_lock); |
6158e1ce | 169 | if (!test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags)) { |
8ddc4734 MX |
170 | /* |
171 | * If we set IN_DEFRAG flag and evict the inode from memory, | |
172 | * and then re-read this inode, this new inode doesn't have | |
173 | * IN_DEFRAG flag. At the case, we may find the existed defrag. | |
174 | */ | |
175 | ret = __btrfs_add_inode_defrag(inode, defrag); | |
176 | if (ret) | |
177 | kmem_cache_free(btrfs_inode_defrag_cachep, defrag); | |
178 | } else { | |
9247f317 | 179 | kmem_cache_free(btrfs_inode_defrag_cachep, defrag); |
8ddc4734 | 180 | } |
0b246afa | 181 | spin_unlock(&fs_info->defrag_inodes_lock); |
a0f98dde | 182 | return 0; |
4cb5300b CM |
183 | } |
184 | ||
185 | /* | |
26176e7c MX |
186 | * pick the defragable inode that we want, if it doesn't exist, we will get |
187 | * the next one. | |
4cb5300b | 188 | */ |
26176e7c MX |
189 | static struct inode_defrag * |
190 | btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino) | |
4cb5300b CM |
191 | { |
192 | struct inode_defrag *entry = NULL; | |
762f2263 | 193 | struct inode_defrag tmp; |
4cb5300b CM |
194 | struct rb_node *p; |
195 | struct rb_node *parent = NULL; | |
762f2263 MX |
196 | int ret; |
197 | ||
198 | tmp.ino = ino; | |
199 | tmp.root = root; | |
4cb5300b | 200 | |
26176e7c MX |
201 | spin_lock(&fs_info->defrag_inodes_lock); |
202 | p = fs_info->defrag_inodes.rb_node; | |
4cb5300b CM |
203 | while (p) { |
204 | parent = p; | |
205 | entry = rb_entry(parent, struct inode_defrag, rb_node); | |
206 | ||
762f2263 MX |
207 | ret = __compare_inode_defrag(&tmp, entry); |
208 | if (ret < 0) | |
4cb5300b | 209 | p = parent->rb_left; |
762f2263 | 210 | else if (ret > 0) |
4cb5300b CM |
211 | p = parent->rb_right; |
212 | else | |
26176e7c | 213 | goto out; |
4cb5300b CM |
214 | } |
215 | ||
26176e7c MX |
216 | if (parent && __compare_inode_defrag(&tmp, entry) > 0) { |
217 | parent = rb_next(parent); | |
218 | if (parent) | |
4cb5300b | 219 | entry = rb_entry(parent, struct inode_defrag, rb_node); |
26176e7c MX |
220 | else |
221 | entry = NULL; | |
4cb5300b | 222 | } |
26176e7c MX |
223 | out: |
224 | if (entry) | |
225 | rb_erase(parent, &fs_info->defrag_inodes); | |
226 | spin_unlock(&fs_info->defrag_inodes_lock); | |
227 | return entry; | |
4cb5300b CM |
228 | } |
229 | ||
26176e7c | 230 | void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info) |
4cb5300b CM |
231 | { |
232 | struct inode_defrag *defrag; | |
26176e7c MX |
233 | struct rb_node *node; |
234 | ||
235 | spin_lock(&fs_info->defrag_inodes_lock); | |
236 | node = rb_first(&fs_info->defrag_inodes); | |
237 | while (node) { | |
238 | rb_erase(node, &fs_info->defrag_inodes); | |
239 | defrag = rb_entry(node, struct inode_defrag, rb_node); | |
240 | kmem_cache_free(btrfs_inode_defrag_cachep, defrag); | |
241 | ||
351810c1 | 242 | cond_resched_lock(&fs_info->defrag_inodes_lock); |
26176e7c MX |
243 | |
244 | node = rb_first(&fs_info->defrag_inodes); | |
245 | } | |
246 | spin_unlock(&fs_info->defrag_inodes_lock); | |
247 | } | |
248 | ||
249 | #define BTRFS_DEFRAG_BATCH 1024 | |
250 | ||
251 | static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info, | |
252 | struct inode_defrag *defrag) | |
253 | { | |
4cb5300b CM |
254 | struct btrfs_root *inode_root; |
255 | struct inode *inode; | |
4cb5300b | 256 | struct btrfs_ioctl_defrag_range_args range; |
26fbac25 QW |
257 | int ret = 0; |
258 | u64 cur = 0; | |
259 | ||
260 | again: | |
261 | if (test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state)) | |
262 | goto cleanup; | |
263 | if (!__need_auto_defrag(fs_info)) | |
264 | goto cleanup; | |
4cb5300b | 265 | |
26176e7c | 266 | /* get the inode */ |
56e9357a | 267 | inode_root = btrfs_get_fs_root(fs_info, defrag->root, true); |
26176e7c | 268 | if (IS_ERR(inode_root)) { |
6f1c3605 LB |
269 | ret = PTR_ERR(inode_root); |
270 | goto cleanup; | |
271 | } | |
26176e7c | 272 | |
0202e83f | 273 | inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root); |
00246528 | 274 | btrfs_put_root(inode_root); |
26176e7c | 275 | if (IS_ERR(inode)) { |
6f1c3605 LB |
276 | ret = PTR_ERR(inode); |
277 | goto cleanup; | |
26176e7c MX |
278 | } |
279 | ||
26fbac25 QW |
280 | if (cur >= i_size_read(inode)) { |
281 | iput(inode); | |
282 | goto cleanup; | |
283 | } | |
284 | ||
26176e7c MX |
285 | /* do a chunk of defrag */ |
286 | clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags); | |
4cb5300b CM |
287 | memset(&range, 0, sizeof(range)); |
288 | range.len = (u64)-1; | |
26fbac25 | 289 | range.start = cur; |
558732df | 290 | range.extent_thresh = defrag->extent_thresh; |
b66f00da MX |
291 | |
292 | sb_start_write(fs_info->sb); | |
26fbac25 | 293 | ret = btrfs_defrag_file(inode, NULL, &range, defrag->transid, |
26176e7c | 294 | BTRFS_DEFRAG_BATCH); |
b66f00da | 295 | sb_end_write(fs_info->sb); |
26176e7c | 296 | iput(inode); |
26fbac25 QW |
297 | |
298 | if (ret < 0) | |
299 | goto cleanup; | |
300 | ||
301 | cur = max(cur + fs_info->sectorsize, range.start); | |
302 | goto again; | |
303 | ||
6f1c3605 | 304 | cleanup: |
6f1c3605 LB |
305 | kmem_cache_free(btrfs_inode_defrag_cachep, defrag); |
306 | return ret; | |
26176e7c MX |
307 | } |
308 | ||
309 | /* | |
310 | * run through the list of inodes in the FS that need | |
311 | * defragging | |
312 | */ | |
313 | int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info) | |
314 | { | |
315 | struct inode_defrag *defrag; | |
316 | u64 first_ino = 0; | |
317 | u64 root_objectid = 0; | |
4cb5300b CM |
318 | |
319 | atomic_inc(&fs_info->defrag_running); | |
67871254 | 320 | while (1) { |
dc81cdc5 MX |
321 | /* Pause the auto defragger. */ |
322 | if (test_bit(BTRFS_FS_STATE_REMOUNTING, | |
323 | &fs_info->fs_state)) | |
324 | break; | |
325 | ||
2ff7e61e | 326 | if (!__need_auto_defrag(fs_info)) |
26176e7c | 327 | break; |
4cb5300b CM |
328 | |
329 | /* find an inode to defrag */ | |
26176e7c MX |
330 | defrag = btrfs_pick_defrag_inode(fs_info, root_objectid, |
331 | first_ino); | |
4cb5300b | 332 | if (!defrag) { |
26176e7c | 333 | if (root_objectid || first_ino) { |
762f2263 | 334 | root_objectid = 0; |
4cb5300b CM |
335 | first_ino = 0; |
336 | continue; | |
337 | } else { | |
338 | break; | |
339 | } | |
340 | } | |
341 | ||
4cb5300b | 342 | first_ino = defrag->ino + 1; |
762f2263 | 343 | root_objectid = defrag->root; |
4cb5300b | 344 | |
26176e7c | 345 | __btrfs_run_defrag_inode(fs_info, defrag); |
4cb5300b | 346 | } |
4cb5300b CM |
347 | atomic_dec(&fs_info->defrag_running); |
348 | ||
349 | /* | |
350 | * during unmount, we use the transaction_wait queue to | |
351 | * wait for the defragger to stop | |
352 | */ | |
353 | wake_up(&fs_info->transaction_wait); | |
354 | return 0; | |
355 | } | |
39279cc3 | 356 | |
d352ac68 CM |
357 | /* simple helper to fault in pages and copy. This should go away |
358 | * and be replaced with calls into generic code. | |
359 | */ | |
ee22f0c4 | 360 | static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes, |
a1b32a59 | 361 | struct page **prepared_pages, |
11c65dcc | 362 | struct iov_iter *i) |
39279cc3 | 363 | { |
914ee295 | 364 | size_t copied = 0; |
d0215f3e | 365 | size_t total_copied = 0; |
11c65dcc | 366 | int pg = 0; |
7073017a | 367 | int offset = offset_in_page(pos); |
39279cc3 | 368 | |
11c65dcc | 369 | while (write_bytes > 0) { |
39279cc3 | 370 | size_t count = min_t(size_t, |
09cbfeaf | 371 | PAGE_SIZE - offset, write_bytes); |
11c65dcc | 372 | struct page *page = prepared_pages[pg]; |
914ee295 XZ |
373 | /* |
374 | * Copy data from userspace to the current page | |
914ee295 | 375 | */ |
f0b65f39 | 376 | copied = copy_page_from_iter_atomic(page, offset, count, i); |
11c65dcc | 377 | |
39279cc3 CM |
378 | /* Flush processor's dcache for this page */ |
379 | flush_dcache_page(page); | |
31339acd CM |
380 | |
381 | /* | |
382 | * if we get a partial write, we can end up with | |
383 | * partially up to date pages. These add | |
384 | * a lot of complexity, so make sure they don't | |
385 | * happen by forcing this copy to be retried. | |
386 | * | |
387 | * The rest of the btrfs_file_write code will fall | |
388 | * back to page at a time copies after we return 0. | |
389 | */ | |
f0b65f39 AV |
390 | if (unlikely(copied < count)) { |
391 | if (!PageUptodate(page)) { | |
392 | iov_iter_revert(i, copied); | |
393 | copied = 0; | |
394 | } | |
395 | if (!copied) | |
396 | break; | |
397 | } | |
31339acd | 398 | |
11c65dcc | 399 | write_bytes -= copied; |
914ee295 | 400 | total_copied += copied; |
f0b65f39 AV |
401 | offset += copied; |
402 | if (offset == PAGE_SIZE) { | |
11c65dcc JB |
403 | pg++; |
404 | offset = 0; | |
405 | } | |
39279cc3 | 406 | } |
914ee295 | 407 | return total_copied; |
39279cc3 CM |
408 | } |
409 | ||
d352ac68 CM |
410 | /* |
411 | * unlocks pages after btrfs_file_write is done with them | |
412 | */ | |
e4f94347 QW |
413 | static void btrfs_drop_pages(struct btrfs_fs_info *fs_info, |
414 | struct page **pages, size_t num_pages, | |
415 | u64 pos, u64 copied) | |
39279cc3 CM |
416 | { |
417 | size_t i; | |
e4f94347 QW |
418 | u64 block_start = round_down(pos, fs_info->sectorsize); |
419 | u64 block_len = round_up(pos + copied, fs_info->sectorsize) - block_start; | |
420 | ||
421 | ASSERT(block_len <= U32_MAX); | |
39279cc3 | 422 | for (i = 0; i < num_pages; i++) { |
d352ac68 CM |
423 | /* page checked is some magic around finding pages that |
424 | * have been modified without going through btrfs_set_page_dirty | |
2457aec6 MG |
425 | * clear it here. There should be no need to mark the pages |
426 | * accessed as prepare_pages should have marked them accessed | |
427 | * in prepare_pages via find_or_create_page() | |
d352ac68 | 428 | */ |
e4f94347 QW |
429 | btrfs_page_clamp_clear_checked(fs_info, pages[i], block_start, |
430 | block_len); | |
39279cc3 | 431 | unlock_page(pages[i]); |
09cbfeaf | 432 | put_page(pages[i]); |
39279cc3 CM |
433 | } |
434 | } | |
435 | ||
d352ac68 | 436 | /* |
c0fab480 QW |
437 | * After btrfs_copy_from_user(), update the following things for delalloc: |
438 | * - Mark newly dirtied pages as DELALLOC in the io tree. | |
439 | * Used to advise which range is to be written back. | |
440 | * - Mark modified pages as Uptodate/Dirty and not needing COW fixup | |
441 | * - Update inode size for past EOF write | |
d352ac68 | 442 | */ |
088545f6 | 443 | int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages, |
2ff7e61e | 444 | size_t num_pages, loff_t pos, size_t write_bytes, |
aa8c1a41 | 445 | struct extent_state **cached, bool noreserve) |
39279cc3 | 446 | { |
088545f6 | 447 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
39279cc3 | 448 | int err = 0; |
a52d9a80 | 449 | int i; |
db94535d | 450 | u64 num_bytes; |
a52d9a80 CM |
451 | u64 start_pos; |
452 | u64 end_of_last_block; | |
453 | u64 end_pos = pos + write_bytes; | |
088545f6 | 454 | loff_t isize = i_size_read(&inode->vfs_inode); |
e3b8a485 | 455 | unsigned int extra_bits = 0; |
39279cc3 | 456 | |
aa8c1a41 GR |
457 | if (write_bytes == 0) |
458 | return 0; | |
459 | ||
460 | if (noreserve) | |
461 | extra_bits |= EXTENT_NORESERVE; | |
462 | ||
13f0dd8f | 463 | start_pos = round_down(pos, fs_info->sectorsize); |
da17066c | 464 | num_bytes = round_up(write_bytes + pos - start_pos, |
0b246afa | 465 | fs_info->sectorsize); |
f02a85d2 | 466 | ASSERT(num_bytes <= U32_MAX); |
39279cc3 | 467 | |
db94535d | 468 | end_of_last_block = start_pos + num_bytes - 1; |
e3b8a485 | 469 | |
7703bdd8 CM |
470 | /* |
471 | * The pages may have already been dirty, clear out old accounting so | |
472 | * we can set things up properly | |
473 | */ | |
088545f6 | 474 | clear_extent_bit(&inode->io_tree, start_pos, end_of_last_block, |
e182163d OS |
475 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
476 | 0, 0, cached); | |
7703bdd8 | 477 | |
088545f6 | 478 | err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, |
330a5827 | 479 | extra_bits, cached); |
d0215f3e JB |
480 | if (err) |
481 | return err; | |
9ed74f2d | 482 | |
c8b97818 CM |
483 | for (i = 0; i < num_pages; i++) { |
484 | struct page *p = pages[i]; | |
f02a85d2 QW |
485 | |
486 | btrfs_page_clamp_set_uptodate(fs_info, p, start_pos, num_bytes); | |
e4f94347 | 487 | btrfs_page_clamp_clear_checked(fs_info, p, start_pos, num_bytes); |
f02a85d2 | 488 | btrfs_page_clamp_set_dirty(fs_info, p, start_pos, num_bytes); |
a52d9a80 | 489 | } |
9f570b8d JB |
490 | |
491 | /* | |
492 | * we've only changed i_size in ram, and we haven't updated | |
493 | * the disk i_size. There is no need to log the inode | |
494 | * at this time. | |
495 | */ | |
496 | if (end_pos > isize) | |
088545f6 | 497 | i_size_write(&inode->vfs_inode, end_pos); |
a22285a6 | 498 | return 0; |
39279cc3 CM |
499 | } |
500 | ||
d352ac68 CM |
501 | /* |
502 | * this drops all the extents in the cache that intersect the range | |
503 | * [start, end]. Existing extents are split as required. | |
504 | */ | |
dcdbc059 | 505 | void btrfs_drop_extent_cache(struct btrfs_inode *inode, u64 start, u64 end, |
7014cdb4 | 506 | int skip_pinned) |
a52d9a80 CM |
507 | { |
508 | struct extent_map *em; | |
3b951516 CM |
509 | struct extent_map *split = NULL; |
510 | struct extent_map *split2 = NULL; | |
dcdbc059 | 511 | struct extent_map_tree *em_tree = &inode->extent_tree; |
39b5637f | 512 | u64 len = end - start + 1; |
5dc562c5 | 513 | u64 gen; |
3b951516 CM |
514 | int ret; |
515 | int testend = 1; | |
5b21f2ed | 516 | unsigned long flags; |
c8b97818 | 517 | int compressed = 0; |
09a2a8f9 | 518 | bool modified; |
a52d9a80 | 519 | |
e6dcd2dc | 520 | WARN_ON(end < start); |
3b951516 | 521 | if (end == (u64)-1) { |
39b5637f | 522 | len = (u64)-1; |
3b951516 CM |
523 | testend = 0; |
524 | } | |
d397712b | 525 | while (1) { |
7014cdb4 JB |
526 | int no_splits = 0; |
527 | ||
09a2a8f9 | 528 | modified = false; |
3b951516 | 529 | if (!split) |
172ddd60 | 530 | split = alloc_extent_map(); |
3b951516 | 531 | if (!split2) |
172ddd60 | 532 | split2 = alloc_extent_map(); |
7014cdb4 JB |
533 | if (!split || !split2) |
534 | no_splits = 1; | |
3b951516 | 535 | |
890871be | 536 | write_lock(&em_tree->lock); |
39b5637f | 537 | em = lookup_extent_mapping(em_tree, start, len); |
d1310b2e | 538 | if (!em) { |
890871be | 539 | write_unlock(&em_tree->lock); |
a52d9a80 | 540 | break; |
d1310b2e | 541 | } |
5b21f2ed | 542 | flags = em->flags; |
5dc562c5 | 543 | gen = em->generation; |
5b21f2ed | 544 | if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) { |
55ef6899 | 545 | if (testend && em->start + em->len >= start + len) { |
5b21f2ed | 546 | free_extent_map(em); |
a1ed835e | 547 | write_unlock(&em_tree->lock); |
5b21f2ed ZY |
548 | break; |
549 | } | |
55ef6899 YZ |
550 | start = em->start + em->len; |
551 | if (testend) | |
5b21f2ed | 552 | len = start + len - (em->start + em->len); |
5b21f2ed | 553 | free_extent_map(em); |
a1ed835e | 554 | write_unlock(&em_tree->lock); |
5b21f2ed ZY |
555 | continue; |
556 | } | |
c8b97818 | 557 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
3ce7e67a | 558 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
3b277594 | 559 | clear_bit(EXTENT_FLAG_LOGGING, &flags); |
09a2a8f9 | 560 | modified = !list_empty(&em->list); |
7014cdb4 JB |
561 | if (no_splits) |
562 | goto next; | |
3b951516 | 563 | |
ee20a983 | 564 | if (em->start < start) { |
3b951516 CM |
565 | split->start = em->start; |
566 | split->len = start - em->start; | |
ee20a983 JB |
567 | |
568 | if (em->block_start < EXTENT_MAP_LAST_BYTE) { | |
569 | split->orig_start = em->orig_start; | |
570 | split->block_start = em->block_start; | |
571 | ||
572 | if (compressed) | |
573 | split->block_len = em->block_len; | |
574 | else | |
575 | split->block_len = split->len; | |
576 | split->orig_block_len = max(split->block_len, | |
577 | em->orig_block_len); | |
578 | split->ram_bytes = em->ram_bytes; | |
579 | } else { | |
580 | split->orig_start = split->start; | |
581 | split->block_len = 0; | |
582 | split->block_start = em->block_start; | |
583 | split->orig_block_len = 0; | |
584 | split->ram_bytes = split->len; | |
585 | } | |
586 | ||
5dc562c5 | 587 | split->generation = gen; |
5b21f2ed | 588 | split->flags = flags; |
261507a0 | 589 | split->compress_type = em->compress_type; |
176840b3 | 590 | replace_extent_mapping(em_tree, em, split, modified); |
3b951516 CM |
591 | free_extent_map(split); |
592 | split = split2; | |
593 | split2 = NULL; | |
594 | } | |
ee20a983 | 595 | if (testend && em->start + em->len > start + len) { |
3b951516 CM |
596 | u64 diff = start + len - em->start; |
597 | ||
598 | split->start = start + len; | |
599 | split->len = em->start + em->len - (start + len); | |
5b21f2ed | 600 | split->flags = flags; |
261507a0 | 601 | split->compress_type = em->compress_type; |
5dc562c5 | 602 | split->generation = gen; |
ee20a983 JB |
603 | |
604 | if (em->block_start < EXTENT_MAP_LAST_BYTE) { | |
605 | split->orig_block_len = max(em->block_len, | |
b4939680 | 606 | em->orig_block_len); |
3b951516 | 607 | |
ee20a983 JB |
608 | split->ram_bytes = em->ram_bytes; |
609 | if (compressed) { | |
610 | split->block_len = em->block_len; | |
611 | split->block_start = em->block_start; | |
612 | split->orig_start = em->orig_start; | |
613 | } else { | |
614 | split->block_len = split->len; | |
615 | split->block_start = em->block_start | |
616 | + diff; | |
617 | split->orig_start = em->orig_start; | |
618 | } | |
c8b97818 | 619 | } else { |
ee20a983 JB |
620 | split->ram_bytes = split->len; |
621 | split->orig_start = split->start; | |
622 | split->block_len = 0; | |
623 | split->block_start = em->block_start; | |
624 | split->orig_block_len = 0; | |
c8b97818 | 625 | } |
3b951516 | 626 | |
176840b3 FM |
627 | if (extent_map_in_tree(em)) { |
628 | replace_extent_mapping(em_tree, em, split, | |
629 | modified); | |
630 | } else { | |
631 | ret = add_extent_mapping(em_tree, split, | |
632 | modified); | |
633 | ASSERT(ret == 0); /* Logic error */ | |
634 | } | |
3b951516 CM |
635 | free_extent_map(split); |
636 | split = NULL; | |
637 | } | |
7014cdb4 | 638 | next: |
176840b3 FM |
639 | if (extent_map_in_tree(em)) |
640 | remove_extent_mapping(em_tree, em); | |
890871be | 641 | write_unlock(&em_tree->lock); |
d1310b2e | 642 | |
a52d9a80 CM |
643 | /* once for us */ |
644 | free_extent_map(em); | |
645 | /* once for the tree*/ | |
646 | free_extent_map(em); | |
647 | } | |
3b951516 CM |
648 | if (split) |
649 | free_extent_map(split); | |
650 | if (split2) | |
651 | free_extent_map(split2); | |
a52d9a80 CM |
652 | } |
653 | ||
39279cc3 CM |
654 | /* |
655 | * this is very complex, but the basic idea is to drop all extents | |
656 | * in the range start - end. hint_block is filled in with a block number | |
657 | * that would be a good hint to the block allocator for this file. | |
658 | * | |
659 | * If an extent intersects the range but is not entirely inside the range | |
660 | * it is either truncated or split. Anything entirely inside the range | |
661 | * is deleted from the tree. | |
2766ff61 FM |
662 | * |
663 | * Note: the VFS' inode number of bytes is not updated, it's up to the caller | |
664 | * to deal with that. We set the field 'bytes_found' of the arguments structure | |
665 | * with the number of allocated bytes found in the target range, so that the | |
666 | * caller can update the inode's number of bytes in an atomic way when | |
667 | * replacing extents in a range to avoid races with stat(2). | |
39279cc3 | 668 | */ |
5893dfb9 FM |
669 | int btrfs_drop_extents(struct btrfs_trans_handle *trans, |
670 | struct btrfs_root *root, struct btrfs_inode *inode, | |
671 | struct btrfs_drop_extents_args *args) | |
39279cc3 | 672 | { |
0b246afa | 673 | struct btrfs_fs_info *fs_info = root->fs_info; |
5f39d397 | 674 | struct extent_buffer *leaf; |
920bbbfb | 675 | struct btrfs_file_extent_item *fi; |
82fa113f | 676 | struct btrfs_ref ref = { 0 }; |
00f5c795 | 677 | struct btrfs_key key; |
920bbbfb | 678 | struct btrfs_key new_key; |
906c448c | 679 | u64 ino = btrfs_ino(inode); |
5893dfb9 | 680 | u64 search_start = args->start; |
920bbbfb YZ |
681 | u64 disk_bytenr = 0; |
682 | u64 num_bytes = 0; | |
683 | u64 extent_offset = 0; | |
684 | u64 extent_end = 0; | |
5893dfb9 | 685 | u64 last_end = args->start; |
920bbbfb YZ |
686 | int del_nr = 0; |
687 | int del_slot = 0; | |
688 | int extent_type; | |
ccd467d6 | 689 | int recow; |
00f5c795 | 690 | int ret; |
dc7fdde3 | 691 | int modify_tree = -1; |
27cdeb70 | 692 | int update_refs; |
c3308f84 | 693 | int found = 0; |
5893dfb9 FM |
694 | struct btrfs_path *path = args->path; |
695 | ||
2766ff61 | 696 | args->bytes_found = 0; |
5893dfb9 FM |
697 | args->extent_inserted = false; |
698 | ||
699 | /* Must always have a path if ->replace_extent is true */ | |
700 | ASSERT(!(args->replace_extent && !args->path)); | |
701 | ||
702 | if (!path) { | |
703 | path = btrfs_alloc_path(); | |
704 | if (!path) { | |
705 | ret = -ENOMEM; | |
706 | goto out; | |
707 | } | |
708 | } | |
39279cc3 | 709 | |
5893dfb9 FM |
710 | if (args->drop_cache) |
711 | btrfs_drop_extent_cache(inode, args->start, args->end - 1, 0); | |
a52d9a80 | 712 | |
5893dfb9 | 713 | if (args->start >= inode->disk_i_size && !args->replace_extent) |
dc7fdde3 CM |
714 | modify_tree = 0; |
715 | ||
d175209b | 716 | update_refs = (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID); |
d397712b | 717 | while (1) { |
ccd467d6 | 718 | recow = 0; |
33345d01 | 719 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
dc7fdde3 | 720 | search_start, modify_tree); |
39279cc3 | 721 | if (ret < 0) |
920bbbfb | 722 | break; |
5893dfb9 | 723 | if (ret > 0 && path->slots[0] > 0 && search_start == args->start) { |
920bbbfb YZ |
724 | leaf = path->nodes[0]; |
725 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); | |
33345d01 | 726 | if (key.objectid == ino && |
920bbbfb YZ |
727 | key.type == BTRFS_EXTENT_DATA_KEY) |
728 | path->slots[0]--; | |
39279cc3 | 729 | } |
920bbbfb | 730 | ret = 0; |
8c2383c3 | 731 | next_slot: |
5f39d397 | 732 | leaf = path->nodes[0]; |
920bbbfb YZ |
733 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
734 | BUG_ON(del_nr > 0); | |
735 | ret = btrfs_next_leaf(root, path); | |
736 | if (ret < 0) | |
737 | break; | |
738 | if (ret > 0) { | |
739 | ret = 0; | |
740 | break; | |
8c2383c3 | 741 | } |
920bbbfb YZ |
742 | leaf = path->nodes[0]; |
743 | recow = 1; | |
744 | } | |
745 | ||
746 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
aeafbf84 FM |
747 | |
748 | if (key.objectid > ino) | |
749 | break; | |
750 | if (WARN_ON_ONCE(key.objectid < ino) || | |
751 | key.type < BTRFS_EXTENT_DATA_KEY) { | |
752 | ASSERT(del_nr == 0); | |
753 | path->slots[0]++; | |
754 | goto next_slot; | |
755 | } | |
5893dfb9 | 756 | if (key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= args->end) |
920bbbfb YZ |
757 | break; |
758 | ||
759 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
760 | struct btrfs_file_extent_item); | |
761 | extent_type = btrfs_file_extent_type(leaf, fi); | |
762 | ||
763 | if (extent_type == BTRFS_FILE_EXTENT_REG || | |
764 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
765 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | |
766 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
767 | extent_offset = btrfs_file_extent_offset(leaf, fi); | |
768 | extent_end = key.offset + | |
769 | btrfs_file_extent_num_bytes(leaf, fi); | |
770 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
771 | extent_end = key.offset + | |
e41ca589 | 772 | btrfs_file_extent_ram_bytes(leaf, fi); |
8c2383c3 | 773 | } else { |
aeafbf84 FM |
774 | /* can't happen */ |
775 | BUG(); | |
39279cc3 CM |
776 | } |
777 | ||
fc19c5e7 FM |
778 | /* |
779 | * Don't skip extent items representing 0 byte lengths. They | |
780 | * used to be created (bug) if while punching holes we hit | |
781 | * -ENOSPC condition. So if we find one here, just ensure we | |
782 | * delete it, otherwise we would insert a new file extent item | |
783 | * with the same key (offset) as that 0 bytes length file | |
784 | * extent item in the call to setup_items_for_insert() later | |
785 | * in this function. | |
786 | */ | |
62fe51c1 JB |
787 | if (extent_end == key.offset && extent_end >= search_start) { |
788 | last_end = extent_end; | |
fc19c5e7 | 789 | goto delete_extent_item; |
62fe51c1 | 790 | } |
fc19c5e7 | 791 | |
920bbbfb YZ |
792 | if (extent_end <= search_start) { |
793 | path->slots[0]++; | |
8c2383c3 | 794 | goto next_slot; |
39279cc3 CM |
795 | } |
796 | ||
c3308f84 | 797 | found = 1; |
5893dfb9 | 798 | search_start = max(key.offset, args->start); |
dc7fdde3 CM |
799 | if (recow || !modify_tree) { |
800 | modify_tree = -1; | |
b3b4aa74 | 801 | btrfs_release_path(path); |
920bbbfb | 802 | continue; |
39279cc3 | 803 | } |
6643558d | 804 | |
920bbbfb YZ |
805 | /* |
806 | * | - range to drop - | | |
807 | * | -------- extent -------- | | |
808 | */ | |
5893dfb9 | 809 | if (args->start > key.offset && args->end < extent_end) { |
920bbbfb | 810 | BUG_ON(del_nr > 0); |
00fdf13a | 811 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
3f9e3df8 | 812 | ret = -EOPNOTSUPP; |
00fdf13a LB |
813 | break; |
814 | } | |
920bbbfb YZ |
815 | |
816 | memcpy(&new_key, &key, sizeof(new_key)); | |
5893dfb9 | 817 | new_key.offset = args->start; |
920bbbfb YZ |
818 | ret = btrfs_duplicate_item(trans, root, path, |
819 | &new_key); | |
820 | if (ret == -EAGAIN) { | |
b3b4aa74 | 821 | btrfs_release_path(path); |
920bbbfb | 822 | continue; |
6643558d | 823 | } |
920bbbfb YZ |
824 | if (ret < 0) |
825 | break; | |
826 | ||
827 | leaf = path->nodes[0]; | |
828 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, | |
829 | struct btrfs_file_extent_item); | |
830 | btrfs_set_file_extent_num_bytes(leaf, fi, | |
5893dfb9 | 831 | args->start - key.offset); |
920bbbfb YZ |
832 | |
833 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
834 | struct btrfs_file_extent_item); | |
835 | ||
5893dfb9 | 836 | extent_offset += args->start - key.offset; |
920bbbfb YZ |
837 | btrfs_set_file_extent_offset(leaf, fi, extent_offset); |
838 | btrfs_set_file_extent_num_bytes(leaf, fi, | |
5893dfb9 | 839 | extent_end - args->start); |
920bbbfb YZ |
840 | btrfs_mark_buffer_dirty(leaf); |
841 | ||
5dc562c5 | 842 | if (update_refs && disk_bytenr > 0) { |
82fa113f QW |
843 | btrfs_init_generic_ref(&ref, |
844 | BTRFS_ADD_DELAYED_REF, | |
845 | disk_bytenr, num_bytes, 0); | |
846 | btrfs_init_data_ref(&ref, | |
920bbbfb YZ |
847 | root->root_key.objectid, |
848 | new_key.objectid, | |
f42c5da6 NB |
849 | args->start - extent_offset, |
850 | 0, false); | |
82fa113f | 851 | ret = btrfs_inc_extent_ref(trans, &ref); |
79787eaa | 852 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 853 | } |
5893dfb9 | 854 | key.offset = args->start; |
6643558d | 855 | } |
62fe51c1 JB |
856 | /* |
857 | * From here on out we will have actually dropped something, so | |
858 | * last_end can be updated. | |
859 | */ | |
860 | last_end = extent_end; | |
861 | ||
920bbbfb YZ |
862 | /* |
863 | * | ---- range to drop ----- | | |
864 | * | -------- extent -------- | | |
865 | */ | |
5893dfb9 | 866 | if (args->start <= key.offset && args->end < extent_end) { |
00fdf13a | 867 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
3f9e3df8 | 868 | ret = -EOPNOTSUPP; |
00fdf13a LB |
869 | break; |
870 | } | |
6643558d | 871 | |
920bbbfb | 872 | memcpy(&new_key, &key, sizeof(new_key)); |
5893dfb9 | 873 | new_key.offset = args->end; |
0b246afa | 874 | btrfs_set_item_key_safe(fs_info, path, &new_key); |
6643558d | 875 | |
5893dfb9 | 876 | extent_offset += args->end - key.offset; |
920bbbfb YZ |
877 | btrfs_set_file_extent_offset(leaf, fi, extent_offset); |
878 | btrfs_set_file_extent_num_bytes(leaf, fi, | |
5893dfb9 | 879 | extent_end - args->end); |
920bbbfb | 880 | btrfs_mark_buffer_dirty(leaf); |
2671485d | 881 | if (update_refs && disk_bytenr > 0) |
2766ff61 | 882 | args->bytes_found += args->end - key.offset; |
920bbbfb | 883 | break; |
39279cc3 | 884 | } |
771ed689 | 885 | |
920bbbfb YZ |
886 | search_start = extent_end; |
887 | /* | |
888 | * | ---- range to drop ----- | | |
889 | * | -------- extent -------- | | |
890 | */ | |
5893dfb9 | 891 | if (args->start > key.offset && args->end >= extent_end) { |
920bbbfb | 892 | BUG_ON(del_nr > 0); |
00fdf13a | 893 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
3f9e3df8 | 894 | ret = -EOPNOTSUPP; |
00fdf13a LB |
895 | break; |
896 | } | |
8c2383c3 | 897 | |
920bbbfb | 898 | btrfs_set_file_extent_num_bytes(leaf, fi, |
5893dfb9 | 899 | args->start - key.offset); |
920bbbfb | 900 | btrfs_mark_buffer_dirty(leaf); |
2671485d | 901 | if (update_refs && disk_bytenr > 0) |
2766ff61 | 902 | args->bytes_found += extent_end - args->start; |
5893dfb9 | 903 | if (args->end == extent_end) |
920bbbfb | 904 | break; |
c8b97818 | 905 | |
920bbbfb YZ |
906 | path->slots[0]++; |
907 | goto next_slot; | |
31840ae1 ZY |
908 | } |
909 | ||
920bbbfb YZ |
910 | /* |
911 | * | ---- range to drop ----- | | |
912 | * | ------ extent ------ | | |
913 | */ | |
5893dfb9 | 914 | if (args->start <= key.offset && args->end >= extent_end) { |
fc19c5e7 | 915 | delete_extent_item: |
920bbbfb YZ |
916 | if (del_nr == 0) { |
917 | del_slot = path->slots[0]; | |
918 | del_nr = 1; | |
919 | } else { | |
920 | BUG_ON(del_slot + del_nr != path->slots[0]); | |
921 | del_nr++; | |
922 | } | |
31840ae1 | 923 | |
5dc562c5 JB |
924 | if (update_refs && |
925 | extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
2766ff61 | 926 | args->bytes_found += extent_end - key.offset; |
920bbbfb | 927 | extent_end = ALIGN(extent_end, |
0b246afa | 928 | fs_info->sectorsize); |
5dc562c5 | 929 | } else if (update_refs && disk_bytenr > 0) { |
ffd4bb2a QW |
930 | btrfs_init_generic_ref(&ref, |
931 | BTRFS_DROP_DELAYED_REF, | |
932 | disk_bytenr, num_bytes, 0); | |
933 | btrfs_init_data_ref(&ref, | |
920bbbfb | 934 | root->root_key.objectid, |
ffd4bb2a | 935 | key.objectid, |
f42c5da6 NB |
936 | key.offset - extent_offset, 0, |
937 | false); | |
ffd4bb2a | 938 | ret = btrfs_free_extent(trans, &ref); |
79787eaa | 939 | BUG_ON(ret); /* -ENOMEM */ |
2766ff61 | 940 | args->bytes_found += extent_end - key.offset; |
31840ae1 | 941 | } |
31840ae1 | 942 | |
5893dfb9 | 943 | if (args->end == extent_end) |
920bbbfb YZ |
944 | break; |
945 | ||
946 | if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) { | |
947 | path->slots[0]++; | |
948 | goto next_slot; | |
949 | } | |
950 | ||
951 | ret = btrfs_del_items(trans, root, path, del_slot, | |
952 | del_nr); | |
79787eaa | 953 | if (ret) { |
66642832 | 954 | btrfs_abort_transaction(trans, ret); |
5dc562c5 | 955 | break; |
79787eaa | 956 | } |
920bbbfb YZ |
957 | |
958 | del_nr = 0; | |
959 | del_slot = 0; | |
960 | ||
b3b4aa74 | 961 | btrfs_release_path(path); |
920bbbfb | 962 | continue; |
39279cc3 | 963 | } |
920bbbfb | 964 | |
290342f6 | 965 | BUG(); |
39279cc3 | 966 | } |
920bbbfb | 967 | |
79787eaa | 968 | if (!ret && del_nr > 0) { |
1acae57b FDBM |
969 | /* |
970 | * Set path->slots[0] to first slot, so that after the delete | |
971 | * if items are move off from our leaf to its immediate left or | |
972 | * right neighbor leafs, we end up with a correct and adjusted | |
5893dfb9 | 973 | * path->slots[0] for our insertion (if args->replace_extent). |
1acae57b FDBM |
974 | */ |
975 | path->slots[0] = del_slot; | |
920bbbfb | 976 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); |
79787eaa | 977 | if (ret) |
66642832 | 978 | btrfs_abort_transaction(trans, ret); |
d5f37527 | 979 | } |
1acae57b | 980 | |
d5f37527 FDBM |
981 | leaf = path->nodes[0]; |
982 | /* | |
983 | * If btrfs_del_items() was called, it might have deleted a leaf, in | |
984 | * which case it unlocked our path, so check path->locks[0] matches a | |
985 | * write lock. | |
986 | */ | |
7ecb4c31 | 987 | if (!ret && args->replace_extent && |
ac5887c8 | 988 | path->locks[0] == BTRFS_WRITE_LOCK && |
e902baac | 989 | btrfs_leaf_free_space(leaf) >= |
5893dfb9 | 990 | sizeof(struct btrfs_item) + args->extent_item_size) { |
d5f37527 FDBM |
991 | |
992 | key.objectid = ino; | |
993 | key.type = BTRFS_EXTENT_DATA_KEY; | |
5893dfb9 | 994 | key.offset = args->start; |
d5f37527 FDBM |
995 | if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) { |
996 | struct btrfs_key slot_key; | |
997 | ||
998 | btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]); | |
999 | if (btrfs_comp_cpu_keys(&key, &slot_key) > 0) | |
1000 | path->slots[0]++; | |
1acae57b | 1001 | } |
f0641656 | 1002 | btrfs_setup_item_for_insert(root, path, &key, args->extent_item_size); |
5893dfb9 | 1003 | args->extent_inserted = true; |
6643558d | 1004 | } |
920bbbfb | 1005 | |
5893dfb9 FM |
1006 | if (!args->path) |
1007 | btrfs_free_path(path); | |
1008 | else if (!args->extent_inserted) | |
1acae57b | 1009 | btrfs_release_path(path); |
5893dfb9 FM |
1010 | out: |
1011 | args->drop_end = found ? min(args->end, last_end) : args->end; | |
5dc562c5 | 1012 | |
39279cc3 CM |
1013 | return ret; |
1014 | } | |
1015 | ||
d899e052 | 1016 | static int extent_mergeable(struct extent_buffer *leaf, int slot, |
6c7d54ac YZ |
1017 | u64 objectid, u64 bytenr, u64 orig_offset, |
1018 | u64 *start, u64 *end) | |
d899e052 YZ |
1019 | { |
1020 | struct btrfs_file_extent_item *fi; | |
1021 | struct btrfs_key key; | |
1022 | u64 extent_end; | |
1023 | ||
1024 | if (slot < 0 || slot >= btrfs_header_nritems(leaf)) | |
1025 | return 0; | |
1026 | ||
1027 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
1028 | if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY) | |
1029 | return 0; | |
1030 | ||
1031 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
1032 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG || | |
1033 | btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr || | |
6c7d54ac | 1034 | btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset || |
d899e052 YZ |
1035 | btrfs_file_extent_compression(leaf, fi) || |
1036 | btrfs_file_extent_encryption(leaf, fi) || | |
1037 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1038 | return 0; | |
1039 | ||
1040 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); | |
1041 | if ((*start && *start != key.offset) || (*end && *end != extent_end)) | |
1042 | return 0; | |
1043 | ||
1044 | *start = key.offset; | |
1045 | *end = extent_end; | |
1046 | return 1; | |
1047 | } | |
1048 | ||
1049 | /* | |
1050 | * Mark extent in the range start - end as written. | |
1051 | * | |
1052 | * This changes extent type from 'pre-allocated' to 'regular'. If only | |
1053 | * part of extent is marked as written, the extent will be split into | |
1054 | * two or three. | |
1055 | */ | |
1056 | int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, | |
7a6d7067 | 1057 | struct btrfs_inode *inode, u64 start, u64 end) |
d899e052 | 1058 | { |
3ffbd68c | 1059 | struct btrfs_fs_info *fs_info = trans->fs_info; |
7a6d7067 | 1060 | struct btrfs_root *root = inode->root; |
d899e052 YZ |
1061 | struct extent_buffer *leaf; |
1062 | struct btrfs_path *path; | |
1063 | struct btrfs_file_extent_item *fi; | |
82fa113f | 1064 | struct btrfs_ref ref = { 0 }; |
d899e052 | 1065 | struct btrfs_key key; |
920bbbfb | 1066 | struct btrfs_key new_key; |
d899e052 YZ |
1067 | u64 bytenr; |
1068 | u64 num_bytes; | |
1069 | u64 extent_end; | |
5d4f98a2 | 1070 | u64 orig_offset; |
d899e052 YZ |
1071 | u64 other_start; |
1072 | u64 other_end; | |
920bbbfb YZ |
1073 | u64 split; |
1074 | int del_nr = 0; | |
1075 | int del_slot = 0; | |
6c7d54ac | 1076 | int recow; |
e7b2ec3d | 1077 | int ret = 0; |
7a6d7067 | 1078 | u64 ino = btrfs_ino(inode); |
d899e052 | 1079 | |
d899e052 | 1080 | path = btrfs_alloc_path(); |
d8926bb3 MF |
1081 | if (!path) |
1082 | return -ENOMEM; | |
d899e052 | 1083 | again: |
6c7d54ac | 1084 | recow = 0; |
920bbbfb | 1085 | split = start; |
33345d01 | 1086 | key.objectid = ino; |
d899e052 | 1087 | key.type = BTRFS_EXTENT_DATA_KEY; |
920bbbfb | 1088 | key.offset = split; |
d899e052 YZ |
1089 | |
1090 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
41415730 JB |
1091 | if (ret < 0) |
1092 | goto out; | |
d899e052 YZ |
1093 | if (ret > 0 && path->slots[0] > 0) |
1094 | path->slots[0]--; | |
1095 | ||
1096 | leaf = path->nodes[0]; | |
1097 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
9c8e63db JB |
1098 | if (key.objectid != ino || |
1099 | key.type != BTRFS_EXTENT_DATA_KEY) { | |
1100 | ret = -EINVAL; | |
1101 | btrfs_abort_transaction(trans, ret); | |
1102 | goto out; | |
1103 | } | |
d899e052 YZ |
1104 | fi = btrfs_item_ptr(leaf, path->slots[0], |
1105 | struct btrfs_file_extent_item); | |
9c8e63db JB |
1106 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_PREALLOC) { |
1107 | ret = -EINVAL; | |
1108 | btrfs_abort_transaction(trans, ret); | |
1109 | goto out; | |
1110 | } | |
d899e052 | 1111 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
9c8e63db JB |
1112 | if (key.offset > start || extent_end < end) { |
1113 | ret = -EINVAL; | |
1114 | btrfs_abort_transaction(trans, ret); | |
1115 | goto out; | |
1116 | } | |
d899e052 YZ |
1117 | |
1118 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | |
1119 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
5d4f98a2 | 1120 | orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi); |
6c7d54ac YZ |
1121 | memcpy(&new_key, &key, sizeof(new_key)); |
1122 | ||
1123 | if (start == key.offset && end < extent_end) { | |
1124 | other_start = 0; | |
1125 | other_end = start; | |
1126 | if (extent_mergeable(leaf, path->slots[0] - 1, | |
33345d01 | 1127 | ino, bytenr, orig_offset, |
6c7d54ac YZ |
1128 | &other_start, &other_end)) { |
1129 | new_key.offset = end; | |
0b246afa | 1130 | btrfs_set_item_key_safe(fs_info, path, &new_key); |
6c7d54ac YZ |
1131 | fi = btrfs_item_ptr(leaf, path->slots[0], |
1132 | struct btrfs_file_extent_item); | |
224ecce5 JB |
1133 | btrfs_set_file_extent_generation(leaf, fi, |
1134 | trans->transid); | |
6c7d54ac YZ |
1135 | btrfs_set_file_extent_num_bytes(leaf, fi, |
1136 | extent_end - end); | |
1137 | btrfs_set_file_extent_offset(leaf, fi, | |
1138 | end - orig_offset); | |
1139 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, | |
1140 | struct btrfs_file_extent_item); | |
224ecce5 JB |
1141 | btrfs_set_file_extent_generation(leaf, fi, |
1142 | trans->transid); | |
6c7d54ac YZ |
1143 | btrfs_set_file_extent_num_bytes(leaf, fi, |
1144 | end - other_start); | |
1145 | btrfs_mark_buffer_dirty(leaf); | |
1146 | goto out; | |
1147 | } | |
1148 | } | |
1149 | ||
1150 | if (start > key.offset && end == extent_end) { | |
1151 | other_start = end; | |
1152 | other_end = 0; | |
1153 | if (extent_mergeable(leaf, path->slots[0] + 1, | |
33345d01 | 1154 | ino, bytenr, orig_offset, |
6c7d54ac YZ |
1155 | &other_start, &other_end)) { |
1156 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1157 | struct btrfs_file_extent_item); | |
1158 | btrfs_set_file_extent_num_bytes(leaf, fi, | |
1159 | start - key.offset); | |
224ecce5 JB |
1160 | btrfs_set_file_extent_generation(leaf, fi, |
1161 | trans->transid); | |
6c7d54ac YZ |
1162 | path->slots[0]++; |
1163 | new_key.offset = start; | |
0b246afa | 1164 | btrfs_set_item_key_safe(fs_info, path, &new_key); |
6c7d54ac YZ |
1165 | |
1166 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1167 | struct btrfs_file_extent_item); | |
224ecce5 JB |
1168 | btrfs_set_file_extent_generation(leaf, fi, |
1169 | trans->transid); | |
6c7d54ac YZ |
1170 | btrfs_set_file_extent_num_bytes(leaf, fi, |
1171 | other_end - start); | |
1172 | btrfs_set_file_extent_offset(leaf, fi, | |
1173 | start - orig_offset); | |
1174 | btrfs_mark_buffer_dirty(leaf); | |
1175 | goto out; | |
1176 | } | |
1177 | } | |
d899e052 | 1178 | |
920bbbfb YZ |
1179 | while (start > key.offset || end < extent_end) { |
1180 | if (key.offset == start) | |
1181 | split = end; | |
1182 | ||
920bbbfb YZ |
1183 | new_key.offset = split; |
1184 | ret = btrfs_duplicate_item(trans, root, path, &new_key); | |
1185 | if (ret == -EAGAIN) { | |
b3b4aa74 | 1186 | btrfs_release_path(path); |
920bbbfb | 1187 | goto again; |
d899e052 | 1188 | } |
79787eaa | 1189 | if (ret < 0) { |
66642832 | 1190 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
1191 | goto out; |
1192 | } | |
d899e052 | 1193 | |
920bbbfb YZ |
1194 | leaf = path->nodes[0]; |
1195 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, | |
d899e052 | 1196 | struct btrfs_file_extent_item); |
224ecce5 | 1197 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
d899e052 | 1198 | btrfs_set_file_extent_num_bytes(leaf, fi, |
920bbbfb YZ |
1199 | split - key.offset); |
1200 | ||
1201 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1202 | struct btrfs_file_extent_item); | |
1203 | ||
224ecce5 | 1204 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
920bbbfb YZ |
1205 | btrfs_set_file_extent_offset(leaf, fi, split - orig_offset); |
1206 | btrfs_set_file_extent_num_bytes(leaf, fi, | |
1207 | extent_end - split); | |
d899e052 YZ |
1208 | btrfs_mark_buffer_dirty(leaf); |
1209 | ||
82fa113f QW |
1210 | btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, bytenr, |
1211 | num_bytes, 0); | |
1212 | btrfs_init_data_ref(&ref, root->root_key.objectid, ino, | |
f42c5da6 | 1213 | orig_offset, 0, false); |
82fa113f | 1214 | ret = btrfs_inc_extent_ref(trans, &ref); |
9c8e63db JB |
1215 | if (ret) { |
1216 | btrfs_abort_transaction(trans, ret); | |
1217 | goto out; | |
1218 | } | |
d899e052 | 1219 | |
920bbbfb YZ |
1220 | if (split == start) { |
1221 | key.offset = start; | |
1222 | } else { | |
9c8e63db JB |
1223 | if (start != key.offset) { |
1224 | ret = -EINVAL; | |
1225 | btrfs_abort_transaction(trans, ret); | |
1226 | goto out; | |
1227 | } | |
d899e052 | 1228 | path->slots[0]--; |
920bbbfb | 1229 | extent_end = end; |
d899e052 | 1230 | } |
6c7d54ac | 1231 | recow = 1; |
d899e052 YZ |
1232 | } |
1233 | ||
920bbbfb YZ |
1234 | other_start = end; |
1235 | other_end = 0; | |
ffd4bb2a QW |
1236 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr, |
1237 | num_bytes, 0); | |
f42c5da6 NB |
1238 | btrfs_init_data_ref(&ref, root->root_key.objectid, ino, orig_offset, |
1239 | 0, false); | |
6c7d54ac | 1240 | if (extent_mergeable(leaf, path->slots[0] + 1, |
33345d01 | 1241 | ino, bytenr, orig_offset, |
6c7d54ac YZ |
1242 | &other_start, &other_end)) { |
1243 | if (recow) { | |
b3b4aa74 | 1244 | btrfs_release_path(path); |
6c7d54ac YZ |
1245 | goto again; |
1246 | } | |
920bbbfb YZ |
1247 | extent_end = other_end; |
1248 | del_slot = path->slots[0] + 1; | |
1249 | del_nr++; | |
ffd4bb2a | 1250 | ret = btrfs_free_extent(trans, &ref); |
9c8e63db JB |
1251 | if (ret) { |
1252 | btrfs_abort_transaction(trans, ret); | |
1253 | goto out; | |
1254 | } | |
d899e052 | 1255 | } |
920bbbfb YZ |
1256 | other_start = 0; |
1257 | other_end = start; | |
6c7d54ac | 1258 | if (extent_mergeable(leaf, path->slots[0] - 1, |
33345d01 | 1259 | ino, bytenr, orig_offset, |
6c7d54ac YZ |
1260 | &other_start, &other_end)) { |
1261 | if (recow) { | |
b3b4aa74 | 1262 | btrfs_release_path(path); |
6c7d54ac YZ |
1263 | goto again; |
1264 | } | |
920bbbfb YZ |
1265 | key.offset = other_start; |
1266 | del_slot = path->slots[0]; | |
1267 | del_nr++; | |
ffd4bb2a | 1268 | ret = btrfs_free_extent(trans, &ref); |
9c8e63db JB |
1269 | if (ret) { |
1270 | btrfs_abort_transaction(trans, ret); | |
1271 | goto out; | |
1272 | } | |
920bbbfb YZ |
1273 | } |
1274 | if (del_nr == 0) { | |
3f6fae95 SL |
1275 | fi = btrfs_item_ptr(leaf, path->slots[0], |
1276 | struct btrfs_file_extent_item); | |
920bbbfb YZ |
1277 | btrfs_set_file_extent_type(leaf, fi, |
1278 | BTRFS_FILE_EXTENT_REG); | |
224ecce5 | 1279 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
920bbbfb | 1280 | btrfs_mark_buffer_dirty(leaf); |
6c7d54ac | 1281 | } else { |
3f6fae95 SL |
1282 | fi = btrfs_item_ptr(leaf, del_slot - 1, |
1283 | struct btrfs_file_extent_item); | |
6c7d54ac YZ |
1284 | btrfs_set_file_extent_type(leaf, fi, |
1285 | BTRFS_FILE_EXTENT_REG); | |
224ecce5 | 1286 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
6c7d54ac YZ |
1287 | btrfs_set_file_extent_num_bytes(leaf, fi, |
1288 | extent_end - key.offset); | |
1289 | btrfs_mark_buffer_dirty(leaf); | |
920bbbfb | 1290 | |
6c7d54ac | 1291 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); |
79787eaa | 1292 | if (ret < 0) { |
66642832 | 1293 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
1294 | goto out; |
1295 | } | |
6c7d54ac | 1296 | } |
920bbbfb | 1297 | out: |
d899e052 | 1298 | btrfs_free_path(path); |
e7b2ec3d | 1299 | return ret; |
d899e052 YZ |
1300 | } |
1301 | ||
b1bf862e CM |
1302 | /* |
1303 | * on error we return an unlocked page and the error value | |
1304 | * on success we return a locked page and 0 | |
1305 | */ | |
bb1591b4 CM |
1306 | static int prepare_uptodate_page(struct inode *inode, |
1307 | struct page *page, u64 pos, | |
b6316429 | 1308 | bool force_uptodate) |
b1bf862e | 1309 | { |
fb12489b | 1310 | struct folio *folio = page_folio(page); |
b1bf862e CM |
1311 | int ret = 0; |
1312 | ||
09cbfeaf | 1313 | if (((pos & (PAGE_SIZE - 1)) || force_uptodate) && |
b6316429 | 1314 | !PageUptodate(page)) { |
fb12489b | 1315 | ret = btrfs_read_folio(NULL, folio); |
b1bf862e CM |
1316 | if (ret) |
1317 | return ret; | |
1318 | lock_page(page); | |
1319 | if (!PageUptodate(page)) { | |
1320 | unlock_page(page); | |
1321 | return -EIO; | |
1322 | } | |
e0467866 QW |
1323 | |
1324 | /* | |
fb12489b | 1325 | * Since btrfs_read_folio() will unlock the folio before it |
f913cff3 | 1326 | * returns, there is a window where btrfs_release_folio() can be |
7c11d0ae QW |
1327 | * called to release the page. Here we check both inode |
1328 | * mapping and PagePrivate() to make sure the page was not | |
1329 | * released. | |
e0467866 QW |
1330 | * |
1331 | * The private flag check is essential for subpage as we need | |
1332 | * to store extra bitmap using page->private. | |
1333 | */ | |
1334 | if (page->mapping != inode->i_mapping || !PagePrivate(page)) { | |
bb1591b4 CM |
1335 | unlock_page(page); |
1336 | return -EAGAIN; | |
1337 | } | |
b1bf862e CM |
1338 | } |
1339 | return 0; | |
1340 | } | |
1341 | ||
39279cc3 | 1342 | /* |
376cc685 | 1343 | * this just gets pages into the page cache and locks them down. |
39279cc3 | 1344 | */ |
b37392ea MX |
1345 | static noinline int prepare_pages(struct inode *inode, struct page **pages, |
1346 | size_t num_pages, loff_t pos, | |
1347 | size_t write_bytes, bool force_uptodate) | |
39279cc3 CM |
1348 | { |
1349 | int i; | |
09cbfeaf | 1350 | unsigned long index = pos >> PAGE_SHIFT; |
3b16a4e3 | 1351 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
fc28b62d | 1352 | int err = 0; |
376cc685 | 1353 | int faili; |
8c2383c3 | 1354 | |
39279cc3 | 1355 | for (i = 0; i < num_pages; i++) { |
bb1591b4 | 1356 | again: |
a94733d0 | 1357 | pages[i] = find_or_create_page(inode->i_mapping, index + i, |
e3a41a5b | 1358 | mask | __GFP_WRITE); |
39279cc3 | 1359 | if (!pages[i]) { |
b1bf862e CM |
1360 | faili = i - 1; |
1361 | err = -ENOMEM; | |
1362 | goto fail; | |
1363 | } | |
1364 | ||
32443de3 QW |
1365 | err = set_page_extent_mapped(pages[i]); |
1366 | if (err < 0) { | |
1367 | faili = i; | |
1368 | goto fail; | |
1369 | } | |
1370 | ||
b1bf862e | 1371 | if (i == 0) |
bb1591b4 | 1372 | err = prepare_uptodate_page(inode, pages[i], pos, |
b6316429 | 1373 | force_uptodate); |
bb1591b4 CM |
1374 | if (!err && i == num_pages - 1) |
1375 | err = prepare_uptodate_page(inode, pages[i], | |
b6316429 | 1376 | pos + write_bytes, false); |
b1bf862e | 1377 | if (err) { |
09cbfeaf | 1378 | put_page(pages[i]); |
bb1591b4 CM |
1379 | if (err == -EAGAIN) { |
1380 | err = 0; | |
1381 | goto again; | |
1382 | } | |
b1bf862e CM |
1383 | faili = i - 1; |
1384 | goto fail; | |
39279cc3 | 1385 | } |
ccd467d6 | 1386 | wait_on_page_writeback(pages[i]); |
39279cc3 | 1387 | } |
376cc685 MX |
1388 | |
1389 | return 0; | |
1390 | fail: | |
1391 | while (faili >= 0) { | |
1392 | unlock_page(pages[faili]); | |
09cbfeaf | 1393 | put_page(pages[faili]); |
376cc685 MX |
1394 | faili--; |
1395 | } | |
1396 | return err; | |
1397 | ||
1398 | } | |
1399 | ||
1400 | /* | |
1401 | * This function locks the extent and properly waits for data=ordered extents | |
1402 | * to finish before allowing the pages to be modified if need. | |
1403 | * | |
1404 | * The return value: | |
1405 | * 1 - the extent is locked | |
1406 | * 0 - the extent is not locked, and everything is OK | |
1407 | * -EAGAIN - need re-prepare the pages | |
1408 | * the other < 0 number - Something wrong happens | |
1409 | */ | |
1410 | static noinline int | |
2cff578c | 1411 | lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct page **pages, |
376cc685 | 1412 | size_t num_pages, loff_t pos, |
2e78c927 | 1413 | size_t write_bytes, |
376cc685 MX |
1414 | u64 *lockstart, u64 *lockend, |
1415 | struct extent_state **cached_state) | |
1416 | { | |
3ffbd68c | 1417 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
376cc685 MX |
1418 | u64 start_pos; |
1419 | u64 last_pos; | |
1420 | int i; | |
1421 | int ret = 0; | |
1422 | ||
0b246afa | 1423 | start_pos = round_down(pos, fs_info->sectorsize); |
e21139c6 | 1424 | last_pos = round_up(pos + write_bytes, fs_info->sectorsize) - 1; |
376cc685 | 1425 | |
e3b8a485 | 1426 | if (start_pos < inode->vfs_inode.i_size) { |
e6dcd2dc | 1427 | struct btrfs_ordered_extent *ordered; |
a7e3b975 | 1428 | |
2cff578c NB |
1429 | lock_extent_bits(&inode->io_tree, start_pos, last_pos, |
1430 | cached_state); | |
b88935bf MX |
1431 | ordered = btrfs_lookup_ordered_range(inode, start_pos, |
1432 | last_pos - start_pos + 1); | |
e6dcd2dc | 1433 | if (ordered && |
bffe633e | 1434 | ordered->file_offset + ordered->num_bytes > start_pos && |
376cc685 | 1435 | ordered->file_offset <= last_pos) { |
2cff578c | 1436 | unlock_extent_cached(&inode->io_tree, start_pos, |
e43bbe5e | 1437 | last_pos, cached_state); |
e6dcd2dc CM |
1438 | for (i = 0; i < num_pages; i++) { |
1439 | unlock_page(pages[i]); | |
09cbfeaf | 1440 | put_page(pages[i]); |
e6dcd2dc | 1441 | } |
c0a43603 | 1442 | btrfs_start_ordered_extent(ordered, 1); |
b88935bf MX |
1443 | btrfs_put_ordered_extent(ordered); |
1444 | return -EAGAIN; | |
e6dcd2dc CM |
1445 | } |
1446 | if (ordered) | |
1447 | btrfs_put_ordered_extent(ordered); | |
7703bdd8 | 1448 | |
376cc685 MX |
1449 | *lockstart = start_pos; |
1450 | *lockend = last_pos; | |
1451 | ret = 1; | |
0762704b | 1452 | } |
376cc685 | 1453 | |
7703bdd8 | 1454 | /* |
32443de3 QW |
1455 | * We should be called after prepare_pages() which should have locked |
1456 | * all pages in the range. | |
7703bdd8 | 1457 | */ |
32443de3 | 1458 | for (i = 0; i < num_pages; i++) |
e6dcd2dc | 1459 | WARN_ON(!PageLocked(pages[i])); |
b1bf862e | 1460 | |
376cc685 | 1461 | return ret; |
39279cc3 CM |
1462 | } |
1463 | ||
d7a8ab4e FM |
1464 | /* |
1465 | * Check if we can do nocow write into the range [@pos, @pos + @write_bytes) | |
1466 | * | |
1467 | * @pos: File offset. | |
1468 | * @write_bytes: The length to write, will be updated to the nocow writeable | |
1469 | * range. | |
1470 | * | |
1471 | * This function will flush ordered extents in the range to ensure proper | |
1472 | * nocow checks. | |
1473 | * | |
1474 | * Return: | |
1475 | * > 0 If we can nocow, and updates @write_bytes. | |
1476 | * 0 If we can't do a nocow write. | |
1477 | * -EAGAIN If we can't do a nocow write because snapshoting of the inode's | |
1478 | * root is in progress. | |
1479 | * < 0 If an error happened. | |
1480 | * | |
1481 | * NOTE: Callers need to call btrfs_check_nocow_unlock() if we return > 0. | |
1482 | */ | |
1483 | int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos, | |
1484 | size_t *write_bytes) | |
7ee9e440 | 1485 | { |
3ffbd68c | 1486 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
85b7ab67 | 1487 | struct btrfs_root *root = inode->root; |
7ee9e440 JB |
1488 | u64 lockstart, lockend; |
1489 | u64 num_bytes; | |
1490 | int ret; | |
1491 | ||
38d37aa9 QW |
1492 | if (!(inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC))) |
1493 | return 0; | |
1494 | ||
d7a8ab4e | 1495 | if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) |
5f791ec3 | 1496 | return -EAGAIN; |
8257b2dc | 1497 | |
0b246afa | 1498 | lockstart = round_down(pos, fs_info->sectorsize); |
da17066c | 1499 | lockend = round_up(pos + *write_bytes, |
0b246afa | 1500 | fs_info->sectorsize) - 1; |
5dbb75ed | 1501 | num_bytes = lockend - lockstart + 1; |
7ee9e440 | 1502 | |
d7a8ab4e | 1503 | btrfs_lock_and_flush_ordered_range(inode, lockstart, lockend, NULL); |
85b7ab67 | 1504 | ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes, |
a84d5d42 | 1505 | NULL, NULL, NULL, false); |
7ee9e440 JB |
1506 | if (ret <= 0) { |
1507 | ret = 0; | |
d7a8ab4e | 1508 | btrfs_drew_write_unlock(&root->snapshot_lock); |
7ee9e440 | 1509 | } else { |
c933956d MX |
1510 | *write_bytes = min_t(size_t, *write_bytes , |
1511 | num_bytes - pos + lockstart); | |
7ee9e440 | 1512 | } |
85b7ab67 | 1513 | unlock_extent(&inode->io_tree, lockstart, lockend); |
7ee9e440 JB |
1514 | |
1515 | return ret; | |
1516 | } | |
1517 | ||
38d37aa9 QW |
1518 | void btrfs_check_nocow_unlock(struct btrfs_inode *inode) |
1519 | { | |
1520 | btrfs_drew_write_unlock(&inode->root->snapshot_lock); | |
1521 | } | |
1522 | ||
b8d8e1fd GR |
1523 | static void update_time_for_write(struct inode *inode) |
1524 | { | |
1525 | struct timespec64 now; | |
1526 | ||
1527 | if (IS_NOCMTIME(inode)) | |
1528 | return; | |
1529 | ||
1530 | now = current_time(inode); | |
1531 | if (!timespec64_equal(&inode->i_mtime, &now)) | |
1532 | inode->i_mtime = now; | |
1533 | ||
1534 | if (!timespec64_equal(&inode->i_ctime, &now)) | |
1535 | inode->i_ctime = now; | |
1536 | ||
1537 | if (IS_I_VERSION(inode)) | |
1538 | inode_inc_iversion(inode); | |
1539 | } | |
1540 | ||
1541 | static int btrfs_write_check(struct kiocb *iocb, struct iov_iter *from, | |
1542 | size_t count) | |
1543 | { | |
1544 | struct file *file = iocb->ki_filp; | |
1545 | struct inode *inode = file_inode(file); | |
1546 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
1547 | loff_t pos = iocb->ki_pos; | |
1548 | int ret; | |
1549 | loff_t oldsize; | |
1550 | loff_t start_pos; | |
1551 | ||
d7a8ab4e FM |
1552 | /* |
1553 | * Quickly bail out on NOWAIT writes if we don't have the nodatacow or | |
1554 | * prealloc flags, as without those flags we always have to COW. We will | |
1555 | * later check if we can really COW into the target range (using | |
1556 | * can_nocow_extent() at btrfs_get_blocks_direct_write()). | |
1557 | */ | |
1558 | if ((iocb->ki_flags & IOCB_NOWAIT) && | |
1559 | !(BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC))) | |
1560 | return -EAGAIN; | |
b8d8e1fd GR |
1561 | |
1562 | current->backing_dev_info = inode_to_bdi(inode); | |
1563 | ret = file_remove_privs(file); | |
1564 | if (ret) | |
1565 | return ret; | |
1566 | ||
1567 | /* | |
1568 | * We reserve space for updating the inode when we reserve space for the | |
1569 | * extent we are going to write, so we will enospc out there. We don't | |
1570 | * need to start yet another transaction to update the inode as we will | |
1571 | * update the inode when we finish writing whatever data we write. | |
1572 | */ | |
1573 | update_time_for_write(inode); | |
1574 | ||
1575 | start_pos = round_down(pos, fs_info->sectorsize); | |
1576 | oldsize = i_size_read(inode); | |
1577 | if (start_pos > oldsize) { | |
1578 | /* Expand hole size to cover write data, preventing empty gap */ | |
1579 | loff_t end_pos = round_up(pos + count, fs_info->sectorsize); | |
1580 | ||
b06359a3 | 1581 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, end_pos); |
b8d8e1fd GR |
1582 | if (ret) { |
1583 | current->backing_dev_info = NULL; | |
1584 | return ret; | |
1585 | } | |
1586 | } | |
1587 | ||
1588 | return 0; | |
1589 | } | |
1590 | ||
e4af400a GR |
1591 | static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb, |
1592 | struct iov_iter *i) | |
4b46fce2 | 1593 | { |
e4af400a | 1594 | struct file *file = iocb->ki_filp; |
c3523706 | 1595 | loff_t pos; |
496ad9aa | 1596 | struct inode *inode = file_inode(file); |
0b246afa | 1597 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
11c65dcc | 1598 | struct page **pages = NULL; |
364ecf36 | 1599 | struct extent_changeset *data_reserved = NULL; |
7ee9e440 | 1600 | u64 release_bytes = 0; |
376cc685 MX |
1601 | u64 lockstart; |
1602 | u64 lockend; | |
d0215f3e JB |
1603 | size_t num_written = 0; |
1604 | int nrptrs; | |
c3523706 | 1605 | ssize_t ret; |
7ee9e440 | 1606 | bool only_release_metadata = false; |
b6316429 | 1607 | bool force_page_uptodate = false; |
5e8b9ef3 | 1608 | loff_t old_isize = i_size_read(inode); |
c3523706 GR |
1609 | unsigned int ilock_flags = 0; |
1610 | ||
1611 | if (iocb->ki_flags & IOCB_NOWAIT) | |
1612 | ilock_flags |= BTRFS_ILOCK_TRY; | |
1613 | ||
1614 | ret = btrfs_inode_lock(inode, ilock_flags); | |
1615 | if (ret < 0) | |
1616 | return ret; | |
4b46fce2 | 1617 | |
c3523706 GR |
1618 | ret = generic_write_checks(iocb, i); |
1619 | if (ret <= 0) | |
1620 | goto out; | |
1621 | ||
1622 | ret = btrfs_write_check(iocb, i, ret); | |
1623 | if (ret < 0) | |
1624 | goto out; | |
1625 | ||
1626 | pos = iocb->ki_pos; | |
09cbfeaf KS |
1627 | nrptrs = min(DIV_ROUND_UP(iov_iter_count(i), PAGE_SIZE), |
1628 | PAGE_SIZE / (sizeof(struct page *))); | |
142349f5 WF |
1629 | nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied); |
1630 | nrptrs = max(nrptrs, 8); | |
31e818fe | 1631 | pages = kmalloc_array(nrptrs, sizeof(struct page *), GFP_KERNEL); |
c3523706 GR |
1632 | if (!pages) { |
1633 | ret = -ENOMEM; | |
1634 | goto out; | |
1635 | } | |
ab93dbec | 1636 | |
d0215f3e | 1637 | while (iov_iter_count(i) > 0) { |
c67d970f | 1638 | struct extent_state *cached_state = NULL; |
7073017a | 1639 | size_t offset = offset_in_page(pos); |
2e78c927 | 1640 | size_t sector_offset; |
d0215f3e | 1641 | size_t write_bytes = min(iov_iter_count(i), |
09cbfeaf | 1642 | nrptrs * (size_t)PAGE_SIZE - |
8c2383c3 | 1643 | offset); |
eefa45f5 | 1644 | size_t num_pages; |
7ee9e440 | 1645 | size_t reserve_bytes; |
d0215f3e JB |
1646 | size_t dirty_pages; |
1647 | size_t copied; | |
2e78c927 CR |
1648 | size_t dirty_sectors; |
1649 | size_t num_sectors; | |
79f015f2 | 1650 | int extents_locked; |
39279cc3 | 1651 | |
914ee295 XZ |
1652 | /* |
1653 | * Fault pages before locking them in prepare_pages | |
1654 | * to avoid recursive lock | |
1655 | */ | |
a6294593 | 1656 | if (unlikely(fault_in_iov_iter_readable(i, write_bytes))) { |
914ee295 | 1657 | ret = -EFAULT; |
d0215f3e | 1658 | break; |
914ee295 XZ |
1659 | } |
1660 | ||
a0e248bb | 1661 | only_release_metadata = false; |
da17066c | 1662 | sector_offset = pos & (fs_info->sectorsize - 1); |
d9d8b2a5 | 1663 | |
364ecf36 | 1664 | extent_changeset_release(data_reserved); |
36ea6f3e NB |
1665 | ret = btrfs_check_data_free_space(BTRFS_I(inode), |
1666 | &data_reserved, pos, | |
364ecf36 | 1667 | write_bytes); |
c6887cd1 | 1668 | if (ret < 0) { |
eefa45f5 GR |
1669 | /* |
1670 | * If we don't have to COW at the offset, reserve | |
1671 | * metadata only. write_bytes may get smaller than | |
1672 | * requested here. | |
1673 | */ | |
38d37aa9 | 1674 | if (btrfs_check_nocow_lock(BTRFS_I(inode), pos, |
eefa45f5 | 1675 | &write_bytes) > 0) |
c6887cd1 | 1676 | only_release_metadata = true; |
eefa45f5 | 1677 | else |
c6887cd1 | 1678 | break; |
c6887cd1 | 1679 | } |
1832a6d5 | 1680 | |
eefa45f5 GR |
1681 | num_pages = DIV_ROUND_UP(write_bytes + offset, PAGE_SIZE); |
1682 | WARN_ON(num_pages > nrptrs); | |
1683 | reserve_bytes = round_up(write_bytes + sector_offset, | |
1684 | fs_info->sectorsize); | |
8b62f87b | 1685 | WARN_ON(reserve_bytes == 0); |
9f3db423 | 1686 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), |
28c9b1e7 | 1687 | reserve_bytes, |
d4135134 | 1688 | reserve_bytes, false); |
7ee9e440 JB |
1689 | if (ret) { |
1690 | if (!only_release_metadata) | |
25ce28ca | 1691 | btrfs_free_reserved_data_space(BTRFS_I(inode), |
bc42bda2 QW |
1692 | data_reserved, pos, |
1693 | write_bytes); | |
8257b2dc | 1694 | else |
38d37aa9 | 1695 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
7ee9e440 JB |
1696 | break; |
1697 | } | |
1698 | ||
1699 | release_bytes = reserve_bytes; | |
376cc685 | 1700 | again: |
4a64001f JB |
1701 | /* |
1702 | * This is going to setup the pages array with the number of | |
1703 | * pages we want, so we don't really need to worry about the | |
1704 | * contents of pages from loop to loop | |
1705 | */ | |
b37392ea MX |
1706 | ret = prepare_pages(inode, pages, num_pages, |
1707 | pos, write_bytes, | |
b6316429 | 1708 | force_page_uptodate); |
8b62f87b JB |
1709 | if (ret) { |
1710 | btrfs_delalloc_release_extents(BTRFS_I(inode), | |
8702ba93 | 1711 | reserve_bytes); |
d0215f3e | 1712 | break; |
8b62f87b | 1713 | } |
39279cc3 | 1714 | |
79f015f2 GR |
1715 | extents_locked = lock_and_cleanup_extent_if_need( |
1716 | BTRFS_I(inode), pages, | |
2cff578c NB |
1717 | num_pages, pos, write_bytes, &lockstart, |
1718 | &lockend, &cached_state); | |
79f015f2 GR |
1719 | if (extents_locked < 0) { |
1720 | if (extents_locked == -EAGAIN) | |
376cc685 | 1721 | goto again; |
8b62f87b | 1722 | btrfs_delalloc_release_extents(BTRFS_I(inode), |
8702ba93 | 1723 | reserve_bytes); |
79f015f2 | 1724 | ret = extents_locked; |
376cc685 | 1725 | break; |
376cc685 MX |
1726 | } |
1727 | ||
ee22f0c4 | 1728 | copied = btrfs_copy_from_user(pos, write_bytes, pages, i); |
b1bf862e | 1729 | |
0b246afa | 1730 | num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes); |
56244ef1 | 1731 | dirty_sectors = round_up(copied + sector_offset, |
0b246afa JM |
1732 | fs_info->sectorsize); |
1733 | dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors); | |
56244ef1 | 1734 | |
b1bf862e CM |
1735 | /* |
1736 | * if we have trouble faulting in the pages, fall | |
1737 | * back to one page at a time | |
1738 | */ | |
1739 | if (copied < write_bytes) | |
1740 | nrptrs = 1; | |
1741 | ||
b6316429 JB |
1742 | if (copied == 0) { |
1743 | force_page_uptodate = true; | |
56244ef1 | 1744 | dirty_sectors = 0; |
b1bf862e | 1745 | dirty_pages = 0; |
b6316429 JB |
1746 | } else { |
1747 | force_page_uptodate = false; | |
ed6078f7 | 1748 | dirty_pages = DIV_ROUND_UP(copied + offset, |
09cbfeaf | 1749 | PAGE_SIZE); |
b6316429 | 1750 | } |
914ee295 | 1751 | |
2e78c927 | 1752 | if (num_sectors > dirty_sectors) { |
8b8b08cb | 1753 | /* release everything except the sectors we dirtied */ |
265fdfa6 | 1754 | release_bytes -= dirty_sectors << fs_info->sectorsize_bits; |
485290a7 | 1755 | if (only_release_metadata) { |
691fa059 | 1756 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
43b18595 | 1757 | release_bytes, true); |
485290a7 QW |
1758 | } else { |
1759 | u64 __pos; | |
1760 | ||
da17066c | 1761 | __pos = round_down(pos, |
0b246afa | 1762 | fs_info->sectorsize) + |
09cbfeaf | 1763 | (dirty_pages << PAGE_SHIFT); |
86d52921 | 1764 | btrfs_delalloc_release_space(BTRFS_I(inode), |
bc42bda2 | 1765 | data_reserved, __pos, |
43b18595 | 1766 | release_bytes, true); |
485290a7 | 1767 | } |
914ee295 XZ |
1768 | } |
1769 | ||
2e78c927 | 1770 | release_bytes = round_up(copied + sector_offset, |
0b246afa | 1771 | fs_info->sectorsize); |
376cc685 | 1772 | |
aa8c1a41 GR |
1773 | ret = btrfs_dirty_pages(BTRFS_I(inode), pages, |
1774 | dirty_pages, pos, copied, | |
1775 | &cached_state, only_release_metadata); | |
c67d970f FM |
1776 | |
1777 | /* | |
1778 | * If we have not locked the extent range, because the range's | |
1779 | * start offset is >= i_size, we might still have a non-NULL | |
1780 | * cached extent state, acquired while marking the extent range | |
1781 | * as delalloc through btrfs_dirty_pages(). Therefore free any | |
1782 | * possible cached extent state to avoid a memory leak. | |
1783 | */ | |
79f015f2 | 1784 | if (extents_locked) |
376cc685 | 1785 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
e43bbe5e | 1786 | lockstart, lockend, &cached_state); |
c67d970f FM |
1787 | else |
1788 | free_extent_state(cached_state); | |
1789 | ||
8702ba93 | 1790 | btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes); |
f1de9683 | 1791 | if (ret) { |
e4f94347 | 1792 | btrfs_drop_pages(fs_info, pages, num_pages, pos, copied); |
376cc685 | 1793 | break; |
f1de9683 | 1794 | } |
39279cc3 | 1795 | |
376cc685 | 1796 | release_bytes = 0; |
8257b2dc | 1797 | if (only_release_metadata) |
38d37aa9 | 1798 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
8257b2dc | 1799 | |
e4f94347 | 1800 | btrfs_drop_pages(fs_info, pages, num_pages, pos, copied); |
f1de9683 | 1801 | |
d0215f3e JB |
1802 | cond_resched(); |
1803 | ||
d0e1d66b | 1804 | balance_dirty_pages_ratelimited(inode->i_mapping); |
cb843a6f | 1805 | |
914ee295 XZ |
1806 | pos += copied; |
1807 | num_written += copied; | |
d0215f3e | 1808 | } |
39279cc3 | 1809 | |
d0215f3e JB |
1810 | kfree(pages); |
1811 | ||
7ee9e440 | 1812 | if (release_bytes) { |
8257b2dc | 1813 | if (only_release_metadata) { |
38d37aa9 | 1814 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
691fa059 | 1815 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
43b18595 | 1816 | release_bytes, true); |
8257b2dc | 1817 | } else { |
86d52921 NB |
1818 | btrfs_delalloc_release_space(BTRFS_I(inode), |
1819 | data_reserved, | |
bc42bda2 | 1820 | round_down(pos, fs_info->sectorsize), |
43b18595 | 1821 | release_bytes, true); |
8257b2dc | 1822 | } |
7ee9e440 JB |
1823 | } |
1824 | ||
364ecf36 | 1825 | extent_changeset_free(data_reserved); |
5e8b9ef3 GR |
1826 | if (num_written > 0) { |
1827 | pagecache_isize_extended(inode, old_isize, iocb->ki_pos); | |
1828 | iocb->ki_pos += num_written; | |
1829 | } | |
c3523706 GR |
1830 | out: |
1831 | btrfs_inode_unlock(inode, ilock_flags); | |
d0215f3e JB |
1832 | return num_written ? num_written : ret; |
1833 | } | |
1834 | ||
4e4cabec GR |
1835 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
1836 | const struct iov_iter *iter, loff_t offset) | |
1837 | { | |
1838 | const u32 blocksize_mask = fs_info->sectorsize - 1; | |
1839 | ||
1840 | if (offset & blocksize_mask) | |
1841 | return -EINVAL; | |
1842 | ||
1843 | if (iov_iter_alignment(iter) & blocksize_mask) | |
1844 | return -EINVAL; | |
1845 | ||
1846 | return 0; | |
1847 | } | |
1848 | ||
1849 | static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) | |
d0215f3e JB |
1850 | { |
1851 | struct file *file = iocb->ki_filp; | |
728404da | 1852 | struct inode *inode = file_inode(file); |
4e4cabec | 1853 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
c3523706 | 1854 | loff_t pos; |
4e4cabec | 1855 | ssize_t written = 0; |
d0215f3e | 1856 | ssize_t written_buffered; |
51bd9563 | 1857 | size_t prev_left = 0; |
d0215f3e | 1858 | loff_t endbyte; |
c3523706 GR |
1859 | ssize_t err; |
1860 | unsigned int ilock_flags = 0; | |
1861 | ||
1862 | if (iocb->ki_flags & IOCB_NOWAIT) | |
1863 | ilock_flags |= BTRFS_ILOCK_TRY; | |
1864 | ||
e9adabb9 GR |
1865 | /* If the write DIO is within EOF, use a shared lock */ |
1866 | if (iocb->ki_pos + iov_iter_count(from) <= i_size_read(inode)) | |
1867 | ilock_flags |= BTRFS_ILOCK_SHARED; | |
1868 | ||
1869 | relock: | |
c3523706 GR |
1870 | err = btrfs_inode_lock(inode, ilock_flags); |
1871 | if (err < 0) | |
1872 | return err; | |
1873 | ||
1874 | err = generic_write_checks(iocb, from); | |
1875 | if (err <= 0) { | |
1876 | btrfs_inode_unlock(inode, ilock_flags); | |
1877 | return err; | |
1878 | } | |
d0215f3e | 1879 | |
c3523706 GR |
1880 | err = btrfs_write_check(iocb, from, err); |
1881 | if (err < 0) { | |
1882 | btrfs_inode_unlock(inode, ilock_flags); | |
1883 | goto out; | |
1884 | } | |
1885 | ||
1886 | pos = iocb->ki_pos; | |
e9adabb9 GR |
1887 | /* |
1888 | * Re-check since file size may have changed just before taking the | |
1889 | * lock or pos may have changed because of O_APPEND in generic_write_check() | |
1890 | */ | |
1891 | if ((ilock_flags & BTRFS_ILOCK_SHARED) && | |
1892 | pos + iov_iter_count(from) > i_size_read(inode)) { | |
1893 | btrfs_inode_unlock(inode, ilock_flags); | |
1894 | ilock_flags &= ~BTRFS_ILOCK_SHARED; | |
1895 | goto relock; | |
1896 | } | |
c3523706 GR |
1897 | |
1898 | if (check_direct_IO(fs_info, from, pos)) { | |
1899 | btrfs_inode_unlock(inode, ilock_flags); | |
4e4cabec | 1900 | goto buffered; |
c3523706 | 1901 | } |
4e4cabec | 1902 | |
51bd9563 FM |
1903 | /* |
1904 | * The iov_iter can be mapped to the same file range we are writing to. | |
1905 | * If that's the case, then we will deadlock in the iomap code, because | |
1906 | * it first calls our callback btrfs_dio_iomap_begin(), which will create | |
1907 | * an ordered extent, and after that it will fault in the pages that the | |
1908 | * iov_iter refers to. During the fault in we end up in the readahead | |
1909 | * pages code (starting at btrfs_readahead()), which will lock the range, | |
1910 | * find that ordered extent and then wait for it to complete (at | |
1911 | * btrfs_lock_and_flush_ordered_range()), resulting in a deadlock since | |
1912 | * obviously the ordered extent can never complete as we didn't submit | |
1913 | * yet the respective bio(s). This always happens when the buffer is | |
1914 | * memory mapped to the same file range, since the iomap DIO code always | |
1915 | * invalidates pages in the target file range (after starting and waiting | |
1916 | * for any writeback). | |
1917 | * | |
1918 | * So here we disable page faults in the iov_iter and then retry if we | |
1919 | * got -EFAULT, faulting in the pages before the retry. | |
1920 | */ | |
1921 | again: | |
1922 | from->nofault = true; | |
36e8c622 | 1923 | err = btrfs_dio_rw(iocb, from, written); |
51bd9563 | 1924 | from->nofault = false; |
d0215f3e | 1925 | |
51bd9563 FM |
1926 | /* No increment (+=) because iomap returns a cumulative value. */ |
1927 | if (err > 0) | |
1928 | written = err; | |
1929 | ||
1930 | if (iov_iter_count(from) > 0 && (err == -EFAULT || err > 0)) { | |
1931 | const size_t left = iov_iter_count(from); | |
1932 | /* | |
1933 | * We have more data left to write. Try to fault in as many as | |
1934 | * possible of the remainder pages and retry. We do this without | |
1935 | * releasing and locking again the inode, to prevent races with | |
1936 | * truncate. | |
1937 | * | |
1938 | * Also, in case the iov refers to pages in the file range of the | |
1939 | * file we want to write to (due to a mmap), we could enter an | |
1940 | * infinite loop if we retry after faulting the pages in, since | |
1941 | * iomap will invalidate any pages in the range early on, before | |
1942 | * it tries to fault in the pages of the iov. So we keep track of | |
1943 | * how much was left of iov in the previous EFAULT and fallback | |
1944 | * to buffered IO in case we haven't made any progress. | |
1945 | */ | |
1946 | if (left == prev_left) { | |
1947 | err = -ENOTBLK; | |
1948 | } else { | |
1949 | fault_in_iov_iter_readable(from, left); | |
1950 | prev_left = left; | |
1951 | goto again; | |
1952 | } | |
a42fa643 GR |
1953 | } |
1954 | ||
51bd9563 FM |
1955 | btrfs_inode_unlock(inode, ilock_flags); |
1956 | ||
51bd9563 FM |
1957 | /* If 'err' is -ENOTBLK then it means we must fallback to buffered IO. */ |
1958 | if ((err < 0 && err != -ENOTBLK) || !iov_iter_count(from)) | |
c3523706 | 1959 | goto out; |
d0215f3e | 1960 | |
4e4cabec | 1961 | buffered: |
e4af400a GR |
1962 | pos = iocb->ki_pos; |
1963 | written_buffered = btrfs_buffered_write(iocb, from); | |
d0215f3e JB |
1964 | if (written_buffered < 0) { |
1965 | err = written_buffered; | |
1966 | goto out; | |
39279cc3 | 1967 | } |
075bdbdb FM |
1968 | /* |
1969 | * Ensure all data is persisted. We want the next direct IO read to be | |
1970 | * able to read what was just written. | |
1971 | */ | |
d0215f3e | 1972 | endbyte = pos + written_buffered - 1; |
728404da | 1973 | err = btrfs_fdatawrite_range(inode, pos, endbyte); |
075bdbdb FM |
1974 | if (err) |
1975 | goto out; | |
728404da | 1976 | err = filemap_fdatawait_range(inode->i_mapping, pos, endbyte); |
d0215f3e JB |
1977 | if (err) |
1978 | goto out; | |
1979 | written += written_buffered; | |
867c4f93 | 1980 | iocb->ki_pos = pos + written_buffered; |
09cbfeaf KS |
1981 | invalidate_mapping_pages(file->f_mapping, pos >> PAGE_SHIFT, |
1982 | endbyte >> PAGE_SHIFT); | |
39279cc3 | 1983 | out: |
51bd9563 | 1984 | return err < 0 ? err : written; |
d0215f3e | 1985 | } |
5b92ee72 | 1986 | |
7c0c7269 OS |
1987 | static ssize_t btrfs_encoded_write(struct kiocb *iocb, struct iov_iter *from, |
1988 | const struct btrfs_ioctl_encoded_io_args *encoded) | |
1989 | { | |
1990 | struct file *file = iocb->ki_filp; | |
1991 | struct inode *inode = file_inode(file); | |
1992 | loff_t count; | |
1993 | ssize_t ret; | |
1994 | ||
1995 | btrfs_inode_lock(inode, 0); | |
1996 | count = encoded->len; | |
1997 | ret = generic_write_checks_count(iocb, &count); | |
1998 | if (ret == 0 && count != encoded->len) { | |
1999 | /* | |
2000 | * The write got truncated by generic_write_checks_count(). We | |
2001 | * can't do a partial encoded write. | |
2002 | */ | |
2003 | ret = -EFBIG; | |
2004 | } | |
2005 | if (ret || encoded->len == 0) | |
2006 | goto out; | |
2007 | ||
2008 | ret = btrfs_write_check(iocb, from, encoded->len); | |
2009 | if (ret < 0) | |
2010 | goto out; | |
2011 | ||
2012 | ret = btrfs_do_encoded_write(iocb, from, encoded); | |
2013 | out: | |
2014 | btrfs_inode_unlock(inode, 0); | |
2015 | return ret; | |
2016 | } | |
2017 | ||
2018 | ssize_t btrfs_do_write_iter(struct kiocb *iocb, struct iov_iter *from, | |
2019 | const struct btrfs_ioctl_encoded_io_args *encoded) | |
d0215f3e JB |
2020 | { |
2021 | struct file *file = iocb->ki_filp; | |
14971657 | 2022 | struct btrfs_inode *inode = BTRFS_I(file_inode(file)); |
7c0c7269 | 2023 | ssize_t num_written, num_sync; |
f50cb7af | 2024 | const bool sync = iocb->ki_flags & IOCB_DSYNC; |
d0215f3e | 2025 | |
c86537a4 GR |
2026 | /* |
2027 | * If the fs flips readonly due to some impossible error, although we | |
2028 | * have opened a file as writable, we have to stop this write operation | |
2029 | * to ensure consistency. | |
2030 | */ | |
84961539 | 2031 | if (BTRFS_FS_ERROR(inode->root->fs_info)) |
c86537a4 GR |
2032 | return -EROFS; |
2033 | ||
7c0c7269 | 2034 | if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT)) |
91f9943e CH |
2035 | return -EOPNOTSUPP; |
2036 | ||
b812ce28 | 2037 | if (sync) |
14971657 | 2038 | atomic_inc(&inode->sync_writers); |
b812ce28 | 2039 | |
7c0c7269 OS |
2040 | if (encoded) { |
2041 | num_written = btrfs_encoded_write(iocb, from, encoded); | |
2042 | num_sync = encoded->len; | |
2043 | } else if (iocb->ki_flags & IOCB_DIRECT) { | |
2044 | num_written = num_sync = btrfs_direct_write(iocb, from); | |
2045 | } else { | |
2046 | num_written = num_sync = btrfs_buffered_write(iocb, from); | |
2047 | } | |
d0215f3e | 2048 | |
bc0939fc FM |
2049 | btrfs_set_inode_last_sub_trans(inode); |
2050 | ||
7c0c7269 OS |
2051 | if (num_sync > 0) { |
2052 | num_sync = generic_write_sync(iocb, num_sync); | |
2053 | if (num_sync < 0) | |
2054 | num_written = num_sync; | |
2055 | } | |
0a3404dc | 2056 | |
b812ce28 | 2057 | if (sync) |
14971657 | 2058 | atomic_dec(&inode->sync_writers); |
b8d8e1fd | 2059 | |
39279cc3 | 2060 | current->backing_dev_info = NULL; |
c3523706 | 2061 | return num_written; |
39279cc3 CM |
2062 | } |
2063 | ||
7c0c7269 OS |
2064 | static ssize_t btrfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
2065 | { | |
2066 | return btrfs_do_write_iter(iocb, from, NULL); | |
2067 | } | |
2068 | ||
d397712b | 2069 | int btrfs_release_file(struct inode *inode, struct file *filp) |
e1b81e67 | 2070 | { |
23b5ec74 JB |
2071 | struct btrfs_file_private *private = filp->private_data; |
2072 | ||
23b5ec74 JB |
2073 | if (private && private->filldir_buf) |
2074 | kfree(private->filldir_buf); | |
2075 | kfree(private); | |
2076 | filp->private_data = NULL; | |
2077 | ||
f6dc45c7 | 2078 | /* |
1fd4033d NB |
2079 | * Set by setattr when we are about to truncate a file from a non-zero |
2080 | * size to a zero size. This tries to flush down new bytes that may | |
2081 | * have been written if the application were using truncate to replace | |
2082 | * a file in place. | |
f6dc45c7 | 2083 | */ |
1fd4033d | 2084 | if (test_and_clear_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
f6dc45c7 CM |
2085 | &BTRFS_I(inode)->runtime_flags)) |
2086 | filemap_flush(inode->i_mapping); | |
e1b81e67 M |
2087 | return 0; |
2088 | } | |
2089 | ||
669249ee FM |
2090 | static int start_ordered_ops(struct inode *inode, loff_t start, loff_t end) |
2091 | { | |
2092 | int ret; | |
343e4fc1 | 2093 | struct blk_plug plug; |
669249ee | 2094 | |
343e4fc1 LB |
2095 | /* |
2096 | * This is only called in fsync, which would do synchronous writes, so | |
2097 | * a plug can merge adjacent IOs as much as possible. Esp. in case of | |
2098 | * multiple disks using raid profile, a large IO can be split to | |
2099 | * several segments of stripe length (currently 64K). | |
2100 | */ | |
2101 | blk_start_plug(&plug); | |
669249ee | 2102 | atomic_inc(&BTRFS_I(inode)->sync_writers); |
728404da | 2103 | ret = btrfs_fdatawrite_range(inode, start, end); |
669249ee | 2104 | atomic_dec(&BTRFS_I(inode)->sync_writers); |
343e4fc1 | 2105 | blk_finish_plug(&plug); |
669249ee FM |
2106 | |
2107 | return ret; | |
2108 | } | |
2109 | ||
626e9f41 FM |
2110 | static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx) |
2111 | { | |
2112 | struct btrfs_inode *inode = BTRFS_I(ctx->inode); | |
2113 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
2114 | ||
2115 | if (btrfs_inode_in_log(inode, fs_info->generation) && | |
2116 | list_empty(&ctx->ordered_extents)) | |
2117 | return true; | |
2118 | ||
2119 | /* | |
2120 | * If we are doing a fast fsync we can not bail out if the inode's | |
2121 | * last_trans is <= then the last committed transaction, because we only | |
2122 | * update the last_trans of the inode during ordered extent completion, | |
2123 | * and for a fast fsync we don't wait for that, we only wait for the | |
2124 | * writeback to complete. | |
2125 | */ | |
2126 | if (inode->last_trans <= fs_info->last_trans_committed && | |
2127 | (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) || | |
2128 | list_empty(&ctx->ordered_extents))) | |
2129 | return true; | |
2130 | ||
2131 | return false; | |
2132 | } | |
2133 | ||
d352ac68 CM |
2134 | /* |
2135 | * fsync call for both files and directories. This logs the inode into | |
2136 | * the tree log instead of forcing full commits whenever possible. | |
2137 | * | |
2138 | * It needs to call filemap_fdatawait so that all ordered extent updates are | |
2139 | * in the metadata btree are up to date for copying to the log. | |
2140 | * | |
2141 | * It drops the inode mutex before doing the tree log commit. This is an | |
2142 | * important optimization for directories because holding the mutex prevents | |
2143 | * new operations on the dir while we write to disk. | |
2144 | */ | |
02c24a82 | 2145 | int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) |
39279cc3 | 2146 | { |
de17e793 | 2147 | struct dentry *dentry = file_dentry(file); |
2b0143b5 | 2148 | struct inode *inode = d_inode(dentry); |
0b246afa | 2149 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 2150 | struct btrfs_root *root = BTRFS_I(inode)->root; |
39279cc3 | 2151 | struct btrfs_trans_handle *trans; |
8b050d35 | 2152 | struct btrfs_log_ctx ctx; |
333427a5 | 2153 | int ret = 0, err; |
48778179 FM |
2154 | u64 len; |
2155 | bool full_sync; | |
39279cc3 | 2156 | |
1abe9b8a | 2157 | trace_btrfs_sync_file(file, datasync); |
257c62e1 | 2158 | |
ebb70442 LB |
2159 | btrfs_init_log_ctx(&ctx, inode); |
2160 | ||
95418ed1 | 2161 | /* |
48778179 FM |
2162 | * Always set the range to a full range, otherwise we can get into |
2163 | * several problems, from missing file extent items to represent holes | |
2164 | * when not using the NO_HOLES feature, to log tree corruption due to | |
2165 | * races between hole detection during logging and completion of ordered | |
2166 | * extents outside the range, to missing checksums due to ordered extents | |
2167 | * for which we flushed only a subset of their pages. | |
95418ed1 | 2168 | */ |
48778179 FM |
2169 | start = 0; |
2170 | end = LLONG_MAX; | |
2171 | len = (u64)LLONG_MAX + 1; | |
95418ed1 | 2172 | |
90abccf2 MX |
2173 | /* |
2174 | * We write the dirty pages in the range and wait until they complete | |
2175 | * out of the ->i_mutex. If so, we can flush the dirty pages by | |
2ab28f32 JB |
2176 | * multi-task, and make the performance up. See |
2177 | * btrfs_wait_ordered_range for an explanation of the ASYNC check. | |
90abccf2 | 2178 | */ |
669249ee | 2179 | ret = start_ordered_ops(inode, start, end); |
90abccf2 | 2180 | if (ret) |
333427a5 | 2181 | goto out; |
90abccf2 | 2182 | |
885f46d8 | 2183 | btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP); |
c495144b | 2184 | |
2ecb7923 | 2185 | atomic_inc(&root->log_batch); |
b5e6c3e1 | 2186 | |
7af59743 | 2187 | /* |
48778179 FM |
2188 | * Always check for the full sync flag while holding the inode's lock, |
2189 | * to avoid races with other tasks. The flag must be either set all the | |
2190 | * time during logging or always off all the time while logging. | |
7af59743 | 2191 | */ |
48778179 FM |
2192 | full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
2193 | &BTRFS_I(inode)->runtime_flags); | |
7af59743 | 2194 | |
aab15e8e | 2195 | /* |
885f46d8 FM |
2196 | * Before we acquired the inode's lock and the mmap lock, someone may |
2197 | * have dirtied more pages in the target range. We need to make sure | |
2198 | * that writeback for any such pages does not start while we are logging | |
2199 | * the inode, because if it does, any of the following might happen when | |
2200 | * we are not doing a full inode sync: | |
aab15e8e FM |
2201 | * |
2202 | * 1) We log an extent after its writeback finishes but before its | |
2203 | * checksums are added to the csum tree, leading to -EIO errors | |
2204 | * when attempting to read the extent after a log replay. | |
2205 | * | |
2206 | * 2) We can end up logging an extent before its writeback finishes. | |
2207 | * Therefore after the log replay we will have a file extent item | |
2208 | * pointing to an unwritten extent (and no data checksums as well). | |
2209 | * | |
2210 | * So trigger writeback for any eventual new dirty pages and then we | |
2211 | * wait for all ordered extents to complete below. | |
2212 | */ | |
2213 | ret = start_ordered_ops(inode, start, end); | |
2214 | if (ret) { | |
885f46d8 | 2215 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
aab15e8e FM |
2216 | goto out; |
2217 | } | |
2218 | ||
669249ee | 2219 | /* |
b5e6c3e1 | 2220 | * We have to do this here to avoid the priority inversion of waiting on |
52042d8e | 2221 | * IO of a lower priority task while holding a transaction open. |
ba0b084a | 2222 | * |
48778179 FM |
2223 | * For a full fsync we wait for the ordered extents to complete while |
2224 | * for a fast fsync we wait just for writeback to complete, and then | |
2225 | * attach the ordered extents to the transaction so that a transaction | |
2226 | * commit waits for their completion, to avoid data loss if we fsync, | |
2227 | * the current transaction commits before the ordered extents complete | |
2228 | * and a power failure happens right after that. | |
d8e3fb10 NA |
2229 | * |
2230 | * For zoned filesystem, if a write IO uses a ZONE_APPEND command, the | |
2231 | * logical address recorded in the ordered extent may change. We need | |
2232 | * to wait for the IO to stabilize the logical address. | |
669249ee | 2233 | */ |
d8e3fb10 | 2234 | if (full_sync || btrfs_is_zoned(fs_info)) { |
48778179 FM |
2235 | ret = btrfs_wait_ordered_range(inode, start, len); |
2236 | } else { | |
2237 | /* | |
2238 | * Get our ordered extents as soon as possible to avoid doing | |
2239 | * checksum lookups in the csum tree, and use instead the | |
2240 | * checksums attached to the ordered extents. | |
2241 | */ | |
2242 | btrfs_get_ordered_extents_for_logging(BTRFS_I(inode), | |
2243 | &ctx.ordered_extents); | |
2244 | ret = filemap_fdatawait_range(inode->i_mapping, start, end); | |
0ef8b726 | 2245 | } |
48778179 FM |
2246 | |
2247 | if (ret) | |
2248 | goto out_release_extents; | |
2249 | ||
2ecb7923 | 2250 | atomic_inc(&root->log_batch); |
257c62e1 | 2251 | |
a4abeea4 | 2252 | smp_mb(); |
626e9f41 | 2253 | if (skip_inode_logging(&ctx)) { |
5dc562c5 | 2254 | /* |
01327610 | 2255 | * We've had everything committed since the last time we were |
5dc562c5 JB |
2256 | * modified so clear this flag in case it was set for whatever |
2257 | * reason, it's no longer relevant. | |
2258 | */ | |
2259 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
2260 | &BTRFS_I(inode)->runtime_flags); | |
0596a904 FM |
2261 | /* |
2262 | * An ordered extent might have started before and completed | |
2263 | * already with io errors, in which case the inode was not | |
2264 | * updated and we end up here. So check the inode's mapping | |
333427a5 JL |
2265 | * for any errors that might have happened since we last |
2266 | * checked called fsync. | |
0596a904 | 2267 | */ |
333427a5 | 2268 | ret = filemap_check_wb_err(inode->i_mapping, file->f_wb_err); |
48778179 | 2269 | goto out_release_extents; |
15ee9bc7 | 2270 | } |
15ee9bc7 | 2271 | |
5039eddc JB |
2272 | /* |
2273 | * We use start here because we will need to wait on the IO to complete | |
2274 | * in btrfs_sync_log, which could require joining a transaction (for | |
2275 | * example checking cross references in the nocow path). If we use join | |
2276 | * here we could get into a situation where we're waiting on IO to | |
2277 | * happen that is blocked on a transaction trying to commit. With start | |
2278 | * we inc the extwriter counter, so we wait for all extwriters to exit | |
52042d8e | 2279 | * before we start blocking joiners. This comment is to keep somebody |
5039eddc JB |
2280 | * from thinking they are super smart and changing this to |
2281 | * btrfs_join_transaction *cough*Josef*cough*. | |
2282 | */ | |
a22285a6 YZ |
2283 | trans = btrfs_start_transaction(root, 0); |
2284 | if (IS_ERR(trans)) { | |
2285 | ret = PTR_ERR(trans); | |
48778179 | 2286 | goto out_release_extents; |
39279cc3 | 2287 | } |
d0c2f4fa | 2288 | trans->in_fsync = true; |
e02119d5 | 2289 | |
48778179 FM |
2290 | ret = btrfs_log_dentry_safe(trans, dentry, &ctx); |
2291 | btrfs_release_log_ctx_extents(&ctx); | |
02c24a82 | 2292 | if (ret < 0) { |
a0634be5 FDBM |
2293 | /* Fallthrough and commit/free transaction. */ |
2294 | ret = 1; | |
02c24a82 | 2295 | } |
49eb7e46 CM |
2296 | |
2297 | /* we've logged all the items and now have a consistent | |
2298 | * version of the file in the log. It is possible that | |
2299 | * someone will come in and modify the file, but that's | |
2300 | * fine because the log is consistent on disk, and we | |
2301 | * have references to all of the file's extents | |
2302 | * | |
2303 | * It is possible that someone will come in and log the | |
2304 | * file again, but that will end up using the synchronization | |
2305 | * inside btrfs_sync_log to keep things safe. | |
2306 | */ | |
885f46d8 | 2307 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
49eb7e46 | 2308 | |
257c62e1 | 2309 | if (ret != BTRFS_NO_LOG_SYNC) { |
0ef8b726 | 2310 | if (!ret) { |
8b050d35 | 2311 | ret = btrfs_sync_log(trans, root, &ctx); |
0ef8b726 | 2312 | if (!ret) { |
3a45bb20 | 2313 | ret = btrfs_end_transaction(trans); |
0ef8b726 | 2314 | goto out; |
2ab28f32 | 2315 | } |
257c62e1 | 2316 | } |
48778179 FM |
2317 | if (!full_sync) { |
2318 | ret = btrfs_wait_ordered_range(inode, start, len); | |
2319 | if (ret) { | |
2320 | btrfs_end_transaction(trans); | |
2321 | goto out; | |
2322 | } | |
2323 | } | |
3a45bb20 | 2324 | ret = btrfs_commit_transaction(trans); |
257c62e1 | 2325 | } else { |
3a45bb20 | 2326 | ret = btrfs_end_transaction(trans); |
e02119d5 | 2327 | } |
39279cc3 | 2328 | out: |
ebb70442 | 2329 | ASSERT(list_empty(&ctx.list)); |
333427a5 JL |
2330 | err = file_check_and_advance_wb_err(file); |
2331 | if (!ret) | |
2332 | ret = err; | |
014e4ac4 | 2333 | return ret > 0 ? -EIO : ret; |
48778179 FM |
2334 | |
2335 | out_release_extents: | |
2336 | btrfs_release_log_ctx_extents(&ctx); | |
885f46d8 | 2337 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
48778179 | 2338 | goto out; |
39279cc3 CM |
2339 | } |
2340 | ||
f0f37e2f | 2341 | static const struct vm_operations_struct btrfs_file_vm_ops = { |
92fee66d | 2342 | .fault = filemap_fault, |
f1820361 | 2343 | .map_pages = filemap_map_pages, |
9ebefb18 CM |
2344 | .page_mkwrite = btrfs_page_mkwrite, |
2345 | }; | |
2346 | ||
2347 | static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma) | |
2348 | { | |
058a457e MX |
2349 | struct address_space *mapping = filp->f_mapping; |
2350 | ||
7e0a1265 | 2351 | if (!mapping->a_ops->read_folio) |
058a457e MX |
2352 | return -ENOEXEC; |
2353 | ||
9ebefb18 | 2354 | file_accessed(filp); |
058a457e | 2355 | vma->vm_ops = &btrfs_file_vm_ops; |
058a457e | 2356 | |
9ebefb18 CM |
2357 | return 0; |
2358 | } | |
2359 | ||
35339c24 | 2360 | static int hole_mergeable(struct btrfs_inode *inode, struct extent_buffer *leaf, |
2aaa6655 JB |
2361 | int slot, u64 start, u64 end) |
2362 | { | |
2363 | struct btrfs_file_extent_item *fi; | |
2364 | struct btrfs_key key; | |
2365 | ||
2366 | if (slot < 0 || slot >= btrfs_header_nritems(leaf)) | |
2367 | return 0; | |
2368 | ||
2369 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
35339c24 | 2370 | if (key.objectid != btrfs_ino(inode) || |
2aaa6655 JB |
2371 | key.type != BTRFS_EXTENT_DATA_KEY) |
2372 | return 0; | |
2373 | ||
2374 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
2375 | ||
2376 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2377 | return 0; | |
2378 | ||
2379 | if (btrfs_file_extent_disk_bytenr(leaf, fi)) | |
2380 | return 0; | |
2381 | ||
2382 | if (key.offset == end) | |
2383 | return 1; | |
2384 | if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start) | |
2385 | return 1; | |
2386 | return 0; | |
2387 | } | |
2388 | ||
a012a74e NB |
2389 | static int fill_holes(struct btrfs_trans_handle *trans, |
2390 | struct btrfs_inode *inode, | |
2391 | struct btrfs_path *path, u64 offset, u64 end) | |
2aaa6655 | 2392 | { |
3ffbd68c | 2393 | struct btrfs_fs_info *fs_info = trans->fs_info; |
a012a74e | 2394 | struct btrfs_root *root = inode->root; |
2aaa6655 JB |
2395 | struct extent_buffer *leaf; |
2396 | struct btrfs_file_extent_item *fi; | |
2397 | struct extent_map *hole_em; | |
a012a74e | 2398 | struct extent_map_tree *em_tree = &inode->extent_tree; |
2aaa6655 JB |
2399 | struct btrfs_key key; |
2400 | int ret; | |
2401 | ||
0b246afa | 2402 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) |
16e7549f JB |
2403 | goto out; |
2404 | ||
a012a74e | 2405 | key.objectid = btrfs_ino(inode); |
2aaa6655 JB |
2406 | key.type = BTRFS_EXTENT_DATA_KEY; |
2407 | key.offset = offset; | |
2408 | ||
2aaa6655 | 2409 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
f94480bd JB |
2410 | if (ret <= 0) { |
2411 | /* | |
2412 | * We should have dropped this offset, so if we find it then | |
2413 | * something has gone horribly wrong. | |
2414 | */ | |
2415 | if (ret == 0) | |
2416 | ret = -EINVAL; | |
2aaa6655 | 2417 | return ret; |
f94480bd | 2418 | } |
2aaa6655 JB |
2419 | |
2420 | leaf = path->nodes[0]; | |
a012a74e | 2421 | if (hole_mergeable(inode, leaf, path->slots[0] - 1, offset, end)) { |
2aaa6655 JB |
2422 | u64 num_bytes; |
2423 | ||
2424 | path->slots[0]--; | |
2425 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2426 | struct btrfs_file_extent_item); | |
2427 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + | |
2428 | end - offset; | |
2429 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2430 | btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes); | |
2431 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2432 | btrfs_mark_buffer_dirty(leaf); | |
2433 | goto out; | |
2434 | } | |
2435 | ||
1707e26d | 2436 | if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) { |
2aaa6655 JB |
2437 | u64 num_bytes; |
2438 | ||
2aaa6655 | 2439 | key.offset = offset; |
0b246afa | 2440 | btrfs_set_item_key_safe(fs_info, path, &key); |
2aaa6655 JB |
2441 | fi = btrfs_item_ptr(leaf, path->slots[0], |
2442 | struct btrfs_file_extent_item); | |
2443 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end - | |
2444 | offset; | |
2445 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2446 | btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes); | |
2447 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2448 | btrfs_mark_buffer_dirty(leaf); | |
2449 | goto out; | |
2450 | } | |
2451 | btrfs_release_path(path); | |
2452 | ||
a012a74e | 2453 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), |
f85b7379 | 2454 | offset, 0, 0, end - offset, 0, end - offset, 0, 0, 0); |
2aaa6655 JB |
2455 | if (ret) |
2456 | return ret; | |
2457 | ||
2458 | out: | |
2459 | btrfs_release_path(path); | |
2460 | ||
2461 | hole_em = alloc_extent_map(); | |
2462 | if (!hole_em) { | |
2463 | btrfs_drop_extent_cache(inode, offset, end - 1, 0); | |
23e3337f | 2464 | btrfs_set_inode_full_sync(inode); |
2aaa6655 JB |
2465 | } else { |
2466 | hole_em->start = offset; | |
2467 | hole_em->len = end - offset; | |
cc95bef6 | 2468 | hole_em->ram_bytes = hole_em->len; |
2aaa6655 JB |
2469 | hole_em->orig_start = offset; |
2470 | ||
2471 | hole_em->block_start = EXTENT_MAP_HOLE; | |
2472 | hole_em->block_len = 0; | |
b4939680 | 2473 | hole_em->orig_block_len = 0; |
2aaa6655 JB |
2474 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
2475 | hole_em->generation = trans->transid; | |
2476 | ||
2477 | do { | |
2478 | btrfs_drop_extent_cache(inode, offset, end - 1, 0); | |
2479 | write_lock(&em_tree->lock); | |
09a2a8f9 | 2480 | ret = add_extent_mapping(em_tree, hole_em, 1); |
2aaa6655 JB |
2481 | write_unlock(&em_tree->lock); |
2482 | } while (ret == -EEXIST); | |
2483 | free_extent_map(hole_em); | |
2484 | if (ret) | |
23e3337f | 2485 | btrfs_set_inode_full_sync(inode); |
2aaa6655 JB |
2486 | } |
2487 | ||
2488 | return 0; | |
2489 | } | |
2490 | ||
d7781546 QW |
2491 | /* |
2492 | * Find a hole extent on given inode and change start/len to the end of hole | |
2493 | * extent.(hole/vacuum extent whose em->start <= start && | |
2494 | * em->start + em->len > start) | |
2495 | * When a hole extent is found, return 1 and modify start/len. | |
2496 | */ | |
dea46d84 | 2497 | static int find_first_non_hole(struct btrfs_inode *inode, u64 *start, u64 *len) |
d7781546 | 2498 | { |
dea46d84 | 2499 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
d7781546 QW |
2500 | struct extent_map *em; |
2501 | int ret = 0; | |
2502 | ||
dea46d84 | 2503 | em = btrfs_get_extent(inode, NULL, 0, |
609805d8 | 2504 | round_down(*start, fs_info->sectorsize), |
39b07b5d | 2505 | round_up(*len, fs_info->sectorsize)); |
9986277e DC |
2506 | if (IS_ERR(em)) |
2507 | return PTR_ERR(em); | |
d7781546 QW |
2508 | |
2509 | /* Hole or vacuum extent(only exists in no-hole mode) */ | |
2510 | if (em->block_start == EXTENT_MAP_HOLE) { | |
2511 | ret = 1; | |
2512 | *len = em->start + em->len > *start + *len ? | |
2513 | 0 : *start + *len - em->start - em->len; | |
2514 | *start = em->start + em->len; | |
2515 | } | |
2516 | free_extent_map(em); | |
2517 | return ret; | |
2518 | } | |
2519 | ||
55961c8a FM |
2520 | static void btrfs_punch_hole_lock_range(struct inode *inode, |
2521 | const u64 lockstart, | |
2522 | const u64 lockend, | |
2523 | struct extent_state **cached_state) | |
f27451f2 | 2524 | { |
0528476b QW |
2525 | /* |
2526 | * For subpage case, if the range is not at page boundary, we could | |
2527 | * have pages at the leading/tailing part of the range. | |
2528 | * This could lead to dead loop since filemap_range_has_page() | |
2529 | * will always return true. | |
2530 | * So here we need to do extra page alignment for | |
2531 | * filemap_range_has_page(). | |
2532 | */ | |
2533 | const u64 page_lockstart = round_up(lockstart, PAGE_SIZE); | |
2534 | const u64 page_lockend = round_down(lockend + 1, PAGE_SIZE) - 1; | |
2535 | ||
f27451f2 | 2536 | while (1) { |
f27451f2 FM |
2537 | truncate_pagecache_range(inode, lockstart, lockend); |
2538 | ||
2539 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
2540 | cached_state); | |
f27451f2 | 2541 | /* |
55961c8a FM |
2542 | * We can't have ordered extents in the range, nor dirty/writeback |
2543 | * pages, because we have locked the inode's VFS lock in exclusive | |
2544 | * mode, we have locked the inode's i_mmap_lock in exclusive mode, | |
2545 | * we have flushed all delalloc in the range and we have waited | |
2546 | * for any ordered extents in the range to complete. | |
2547 | * We can race with anyone reading pages from this range, so after | |
2548 | * locking the range check if we have pages in the range, and if | |
2549 | * we do, unlock the range and retry. | |
f27451f2 | 2550 | */ |
55961c8a FM |
2551 | if (!filemap_range_has_page(inode->i_mapping, page_lockstart, |
2552 | page_lockend)) | |
f27451f2 | 2553 | break; |
55961c8a | 2554 | |
f27451f2 FM |
2555 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, |
2556 | lockend, cached_state); | |
f27451f2 | 2557 | } |
63c34cb4 FM |
2558 | |
2559 | btrfs_assert_inode_range_clean(BTRFS_I(inode), lockstart, lockend); | |
f27451f2 FM |
2560 | } |
2561 | ||
0cbb5bdf | 2562 | static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans, |
03fcb1ab | 2563 | struct btrfs_inode *inode, |
690a5dbf | 2564 | struct btrfs_path *path, |
bf385648 | 2565 | struct btrfs_replace_extent_info *extent_info, |
2766ff61 FM |
2566 | const u64 replace_len, |
2567 | const u64 bytes_to_drop) | |
690a5dbf | 2568 | { |
03fcb1ab NB |
2569 | struct btrfs_fs_info *fs_info = trans->fs_info; |
2570 | struct btrfs_root *root = inode->root; | |
690a5dbf FM |
2571 | struct btrfs_file_extent_item *extent; |
2572 | struct extent_buffer *leaf; | |
2573 | struct btrfs_key key; | |
2574 | int slot; | |
2575 | struct btrfs_ref ref = { 0 }; | |
690a5dbf FM |
2576 | int ret; |
2577 | ||
bf385648 | 2578 | if (replace_len == 0) |
690a5dbf FM |
2579 | return 0; |
2580 | ||
bf385648 | 2581 | if (extent_info->disk_offset == 0 && |
2766ff61 | 2582 | btrfs_fs_incompat(fs_info, NO_HOLES)) { |
03fcb1ab | 2583 | btrfs_update_inode_bytes(inode, 0, bytes_to_drop); |
690a5dbf | 2584 | return 0; |
2766ff61 | 2585 | } |
690a5dbf | 2586 | |
03fcb1ab | 2587 | key.objectid = btrfs_ino(inode); |
690a5dbf | 2588 | key.type = BTRFS_EXTENT_DATA_KEY; |
bf385648 | 2589 | key.offset = extent_info->file_offset; |
690a5dbf | 2590 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
fb870f6c | 2591 | sizeof(struct btrfs_file_extent_item)); |
690a5dbf FM |
2592 | if (ret) |
2593 | return ret; | |
2594 | leaf = path->nodes[0]; | |
2595 | slot = path->slots[0]; | |
bf385648 | 2596 | write_extent_buffer(leaf, extent_info->extent_buf, |
690a5dbf | 2597 | btrfs_item_ptr_offset(leaf, slot), |
fb870f6c | 2598 | sizeof(struct btrfs_file_extent_item)); |
690a5dbf | 2599 | extent = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
fb870f6c | 2600 | ASSERT(btrfs_file_extent_type(leaf, extent) != BTRFS_FILE_EXTENT_INLINE); |
bf385648 FM |
2601 | btrfs_set_file_extent_offset(leaf, extent, extent_info->data_offset); |
2602 | btrfs_set_file_extent_num_bytes(leaf, extent, replace_len); | |
2603 | if (extent_info->is_new_extent) | |
8fccebfa | 2604 | btrfs_set_file_extent_generation(leaf, extent, trans->transid); |
690a5dbf FM |
2605 | btrfs_mark_buffer_dirty(leaf); |
2606 | btrfs_release_path(path); | |
2607 | ||
03fcb1ab NB |
2608 | ret = btrfs_inode_set_file_extent_range(inode, extent_info->file_offset, |
2609 | replace_len); | |
9ddc959e JB |
2610 | if (ret) |
2611 | return ret; | |
2612 | ||
690a5dbf | 2613 | /* If it's a hole, nothing more needs to be done. */ |
2766ff61 | 2614 | if (extent_info->disk_offset == 0) { |
03fcb1ab | 2615 | btrfs_update_inode_bytes(inode, 0, bytes_to_drop); |
690a5dbf | 2616 | return 0; |
2766ff61 | 2617 | } |
690a5dbf | 2618 | |
03fcb1ab | 2619 | btrfs_update_inode_bytes(inode, replace_len, bytes_to_drop); |
8fccebfa | 2620 | |
bf385648 FM |
2621 | if (extent_info->is_new_extent && extent_info->insertions == 0) { |
2622 | key.objectid = extent_info->disk_offset; | |
8fccebfa | 2623 | key.type = BTRFS_EXTENT_ITEM_KEY; |
bf385648 | 2624 | key.offset = extent_info->disk_len; |
8fccebfa | 2625 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
03fcb1ab | 2626 | btrfs_ino(inode), |
bf385648 FM |
2627 | extent_info->file_offset, |
2628 | extent_info->qgroup_reserved, | |
8fccebfa FM |
2629 | &key); |
2630 | } else { | |
2631 | u64 ref_offset; | |
2632 | ||
2633 | btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, | |
bf385648 FM |
2634 | extent_info->disk_offset, |
2635 | extent_info->disk_len, 0); | |
2636 | ref_offset = extent_info->file_offset - extent_info->data_offset; | |
8fccebfa | 2637 | btrfs_init_data_ref(&ref, root->root_key.objectid, |
f42c5da6 | 2638 | btrfs_ino(inode), ref_offset, 0, false); |
8fccebfa FM |
2639 | ret = btrfs_inc_extent_ref(trans, &ref); |
2640 | } | |
2641 | ||
bf385648 | 2642 | extent_info->insertions++; |
690a5dbf FM |
2643 | |
2644 | return ret; | |
2645 | } | |
2646 | ||
9cba40a6 FM |
2647 | /* |
2648 | * The respective range must have been previously locked, as well as the inode. | |
2649 | * The end offset is inclusive (last byte of the range). | |
bf385648 FM |
2650 | * @extent_info is NULL for fallocate's hole punching and non-NULL when replacing |
2651 | * the file range with an extent. | |
2652 | * When not punching a hole, we don't want to end up in a state where we dropped | |
2653 | * extents without inserting a new one, so we must abort the transaction to avoid | |
2654 | * a corruption. | |
9cba40a6 | 2655 | */ |
bfc78479 NB |
2656 | int btrfs_replace_file_extents(struct btrfs_inode *inode, |
2657 | struct btrfs_path *path, const u64 start, | |
2658 | const u64 end, | |
2659 | struct btrfs_replace_extent_info *extent_info, | |
2660 | struct btrfs_trans_handle **trans_out) | |
9cba40a6 | 2661 | { |
5893dfb9 | 2662 | struct btrfs_drop_extents_args drop_args = { 0 }; |
bfc78479 NB |
2663 | struct btrfs_root *root = inode->root; |
2664 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2bd36e7b | 2665 | u64 min_size = btrfs_calc_insert_metadata_size(fs_info, 1); |
bfc78479 | 2666 | u64 ino_size = round_up(inode->vfs_inode.i_size, fs_info->sectorsize); |
9cba40a6 FM |
2667 | struct btrfs_trans_handle *trans = NULL; |
2668 | struct btrfs_block_rsv *rsv; | |
2669 | unsigned int rsv_count; | |
2670 | u64 cur_offset; | |
9cba40a6 FM |
2671 | u64 len = end - start; |
2672 | int ret = 0; | |
2673 | ||
2674 | if (end <= start) | |
2675 | return -EINVAL; | |
2676 | ||
2677 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); | |
2678 | if (!rsv) { | |
2679 | ret = -ENOMEM; | |
2680 | goto out; | |
2681 | } | |
2bd36e7b | 2682 | rsv->size = btrfs_calc_insert_metadata_size(fs_info, 1); |
9cba40a6 FM |
2683 | rsv->failfast = 1; |
2684 | ||
2685 | /* | |
2686 | * 1 - update the inode | |
2687 | * 1 - removing the extents in the range | |
bf385648 FM |
2688 | * 1 - adding the hole extent if no_holes isn't set or if we are |
2689 | * replacing the range with a new extent | |
9cba40a6 | 2690 | */ |
bf385648 | 2691 | if (!btrfs_fs_incompat(fs_info, NO_HOLES) || extent_info) |
690a5dbf FM |
2692 | rsv_count = 3; |
2693 | else | |
2694 | rsv_count = 2; | |
2695 | ||
9cba40a6 FM |
2696 | trans = btrfs_start_transaction(root, rsv_count); |
2697 | if (IS_ERR(trans)) { | |
2698 | ret = PTR_ERR(trans); | |
2699 | trans = NULL; | |
2700 | goto out_free; | |
2701 | } | |
2702 | ||
2703 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, | |
2704 | min_size, false); | |
2705 | BUG_ON(ret); | |
2706 | trans->block_rsv = rsv; | |
2707 | ||
2708 | cur_offset = start; | |
5893dfb9 FM |
2709 | drop_args.path = path; |
2710 | drop_args.end = end + 1; | |
2711 | drop_args.drop_cache = true; | |
9cba40a6 | 2712 | while (cur_offset < end) { |
5893dfb9 | 2713 | drop_args.start = cur_offset; |
bfc78479 | 2714 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
2766ff61 FM |
2715 | /* If we are punching a hole decrement the inode's byte count */ |
2716 | if (!extent_info) | |
bfc78479 | 2717 | btrfs_update_inode_bytes(inode, 0, |
2766ff61 | 2718 | drop_args.bytes_found); |
690a5dbf FM |
2719 | if (ret != -ENOSPC) { |
2720 | /* | |
4afb912f JB |
2721 | * The only time we don't want to abort is if we are |
2722 | * attempting to clone a partial inline extent, in which | |
2723 | * case we'll get EOPNOTSUPP. However if we aren't | |
2724 | * clone we need to abort no matter what, because if we | |
2725 | * got EOPNOTSUPP via prealloc then we messed up and | |
2726 | * need to abort. | |
690a5dbf | 2727 | */ |
4afb912f JB |
2728 | if (ret && |
2729 | (ret != -EOPNOTSUPP || | |
2730 | (extent_info && extent_info->is_new_extent))) | |
690a5dbf | 2731 | btrfs_abort_transaction(trans, ret); |
9cba40a6 | 2732 | break; |
690a5dbf | 2733 | } |
9cba40a6 FM |
2734 | |
2735 | trans->block_rsv = &fs_info->trans_block_rsv; | |
2736 | ||
5893dfb9 | 2737 | if (!extent_info && cur_offset < drop_args.drop_end && |
690a5dbf | 2738 | cur_offset < ino_size) { |
bfc78479 NB |
2739 | ret = fill_holes(trans, inode, path, cur_offset, |
2740 | drop_args.drop_end); | |
9cba40a6 FM |
2741 | if (ret) { |
2742 | /* | |
2743 | * If we failed then we didn't insert our hole | |
2744 | * entries for the area we dropped, so now the | |
2745 | * fs is corrupted, so we must abort the | |
2746 | * transaction. | |
2747 | */ | |
2748 | btrfs_abort_transaction(trans, ret); | |
2749 | break; | |
2750 | } | |
5893dfb9 | 2751 | } else if (!extent_info && cur_offset < drop_args.drop_end) { |
9ddc959e JB |
2752 | /* |
2753 | * We are past the i_size here, but since we didn't | |
2754 | * insert holes we need to clear the mapped area so we | |
2755 | * know to not set disk_i_size in this area until a new | |
2756 | * file extent is inserted here. | |
2757 | */ | |
bfc78479 | 2758 | ret = btrfs_inode_clear_file_extent_range(inode, |
5893dfb9 FM |
2759 | cur_offset, |
2760 | drop_args.drop_end - cur_offset); | |
9ddc959e JB |
2761 | if (ret) { |
2762 | /* | |
2763 | * We couldn't clear our area, so we could | |
2764 | * presumably adjust up and corrupt the fs, so | |
2765 | * we need to abort. | |
2766 | */ | |
2767 | btrfs_abort_transaction(trans, ret); | |
2768 | break; | |
2769 | } | |
9cba40a6 FM |
2770 | } |
2771 | ||
5893dfb9 FM |
2772 | if (extent_info && |
2773 | drop_args.drop_end > extent_info->file_offset) { | |
2774 | u64 replace_len = drop_args.drop_end - | |
2775 | extent_info->file_offset; | |
690a5dbf | 2776 | |
bfc78479 NB |
2777 | ret = btrfs_insert_replace_extent(trans, inode, path, |
2778 | extent_info, replace_len, | |
03fcb1ab | 2779 | drop_args.bytes_found); |
690a5dbf FM |
2780 | if (ret) { |
2781 | btrfs_abort_transaction(trans, ret); | |
2782 | break; | |
2783 | } | |
bf385648 FM |
2784 | extent_info->data_len -= replace_len; |
2785 | extent_info->data_offset += replace_len; | |
2786 | extent_info->file_offset += replace_len; | |
690a5dbf FM |
2787 | } |
2788 | ||
bfc78479 | 2789 | ret = btrfs_update_inode(trans, root, inode); |
9cba40a6 FM |
2790 | if (ret) |
2791 | break; | |
2792 | ||
2793 | btrfs_end_transaction(trans); | |
2794 | btrfs_btree_balance_dirty(fs_info); | |
2795 | ||
2796 | trans = btrfs_start_transaction(root, rsv_count); | |
2797 | if (IS_ERR(trans)) { | |
2798 | ret = PTR_ERR(trans); | |
2799 | trans = NULL; | |
2800 | break; | |
2801 | } | |
2802 | ||
2803 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, | |
2804 | rsv, min_size, false); | |
2805 | BUG_ON(ret); /* shouldn't happen */ | |
2806 | trans->block_rsv = rsv; | |
2807 | ||
3227788c BC |
2808 | cur_offset = drop_args.drop_end; |
2809 | len = end - cur_offset; | |
2810 | if (!extent_info && len) { | |
bfc78479 | 2811 | ret = find_first_non_hole(inode, &cur_offset, &len); |
690a5dbf FM |
2812 | if (unlikely(ret < 0)) |
2813 | break; | |
2814 | if (ret && !len) { | |
2815 | ret = 0; | |
2816 | break; | |
2817 | } | |
9cba40a6 FM |
2818 | } |
2819 | } | |
2820 | ||
690a5dbf FM |
2821 | /* |
2822 | * If we were cloning, force the next fsync to be a full one since we | |
2823 | * we replaced (or just dropped in the case of cloning holes when | |
e2b84217 FM |
2824 | * NO_HOLES is enabled) file extent items and did not setup new extent |
2825 | * maps for the replacement extents (or holes). | |
690a5dbf | 2826 | */ |
bf385648 | 2827 | if (extent_info && !extent_info->is_new_extent) |
23e3337f | 2828 | btrfs_set_inode_full_sync(inode); |
690a5dbf | 2829 | |
9cba40a6 FM |
2830 | if (ret) |
2831 | goto out_trans; | |
2832 | ||
2833 | trans->block_rsv = &fs_info->trans_block_rsv; | |
2834 | /* | |
2835 | * If we are using the NO_HOLES feature we might have had already an | |
2836 | * hole that overlaps a part of the region [lockstart, lockend] and | |
2837 | * ends at (or beyond) lockend. Since we have no file extent items to | |
2838 | * represent holes, drop_end can be less than lockend and so we must | |
2839 | * make sure we have an extent map representing the existing hole (the | |
2840 | * call to __btrfs_drop_extents() might have dropped the existing extent | |
2841 | * map representing the existing hole), otherwise the fast fsync path | |
2842 | * will not record the existence of the hole region | |
2843 | * [existing_hole_start, lockend]. | |
2844 | */ | |
5893dfb9 FM |
2845 | if (drop_args.drop_end <= end) |
2846 | drop_args.drop_end = end + 1; | |
9cba40a6 FM |
2847 | /* |
2848 | * Don't insert file hole extent item if it's for a range beyond eof | |
2849 | * (because it's useless) or if it represents a 0 bytes range (when | |
2850 | * cur_offset == drop_end). | |
2851 | */ | |
5893dfb9 FM |
2852 | if (!extent_info && cur_offset < ino_size && |
2853 | cur_offset < drop_args.drop_end) { | |
bfc78479 NB |
2854 | ret = fill_holes(trans, inode, path, cur_offset, |
2855 | drop_args.drop_end); | |
9cba40a6 FM |
2856 | if (ret) { |
2857 | /* Same comment as above. */ | |
2858 | btrfs_abort_transaction(trans, ret); | |
2859 | goto out_trans; | |
2860 | } | |
5893dfb9 | 2861 | } else if (!extent_info && cur_offset < drop_args.drop_end) { |
9ddc959e | 2862 | /* See the comment in the loop above for the reasoning here. */ |
bfc78479 NB |
2863 | ret = btrfs_inode_clear_file_extent_range(inode, cur_offset, |
2864 | drop_args.drop_end - cur_offset); | |
9ddc959e JB |
2865 | if (ret) { |
2866 | btrfs_abort_transaction(trans, ret); | |
2867 | goto out_trans; | |
2868 | } | |
2869 | ||
9cba40a6 | 2870 | } |
bf385648 | 2871 | if (extent_info) { |
bfc78479 | 2872 | ret = btrfs_insert_replace_extent(trans, inode, path, |
03fcb1ab NB |
2873 | extent_info, extent_info->data_len, |
2874 | drop_args.bytes_found); | |
690a5dbf FM |
2875 | if (ret) { |
2876 | btrfs_abort_transaction(trans, ret); | |
2877 | goto out_trans; | |
2878 | } | |
2879 | } | |
9cba40a6 FM |
2880 | |
2881 | out_trans: | |
2882 | if (!trans) | |
2883 | goto out_free; | |
2884 | ||
2885 | trans->block_rsv = &fs_info->trans_block_rsv; | |
2886 | if (ret) | |
2887 | btrfs_end_transaction(trans); | |
2888 | else | |
2889 | *trans_out = trans; | |
2890 | out_free: | |
2891 | btrfs_free_block_rsv(fs_info, rsv); | |
2892 | out: | |
2893 | return ret; | |
2894 | } | |
2895 | ||
05fd9564 | 2896 | static int btrfs_punch_hole(struct file *file, loff_t offset, loff_t len) |
2aaa6655 | 2897 | { |
05fd9564 | 2898 | struct inode *inode = file_inode(file); |
0b246afa | 2899 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 JB |
2900 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2901 | struct extent_state *cached_state = NULL; | |
2902 | struct btrfs_path *path; | |
9cba40a6 | 2903 | struct btrfs_trans_handle *trans = NULL; |
d7781546 QW |
2904 | u64 lockstart; |
2905 | u64 lockend; | |
2906 | u64 tail_start; | |
2907 | u64 tail_len; | |
2908 | u64 orig_start = offset; | |
2aaa6655 | 2909 | int ret = 0; |
9703fefe | 2910 | bool same_block; |
a1a50f60 | 2911 | u64 ino_size; |
9703fefe | 2912 | bool truncated_block = false; |
e8c1c76e | 2913 | bool updated_inode = false; |
2aaa6655 | 2914 | |
bd6526d0 FM |
2915 | btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP); |
2916 | ||
0ef8b726 JB |
2917 | ret = btrfs_wait_ordered_range(inode, offset, len); |
2918 | if (ret) | |
bd6526d0 | 2919 | goto out_only_mutex; |
2aaa6655 | 2920 | |
0b246afa | 2921 | ino_size = round_up(inode->i_size, fs_info->sectorsize); |
dea46d84 | 2922 | ret = find_first_non_hole(BTRFS_I(inode), &offset, &len); |
d7781546 QW |
2923 | if (ret < 0) |
2924 | goto out_only_mutex; | |
2925 | if (ret && !len) { | |
2926 | /* Already in a large hole */ | |
2927 | ret = 0; | |
2928 | goto out_only_mutex; | |
2929 | } | |
2930 | ||
05fd9564 DW |
2931 | ret = file_modified(file); |
2932 | if (ret) | |
2933 | goto out_only_mutex; | |
2934 | ||
6fee248d | 2935 | lockstart = round_up(offset, btrfs_inode_sectorsize(BTRFS_I(inode))); |
d7781546 | 2936 | lockend = round_down(offset + len, |
6fee248d | 2937 | btrfs_inode_sectorsize(BTRFS_I(inode))) - 1; |
0b246afa JM |
2938 | same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset)) |
2939 | == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)); | |
7426cc04 | 2940 | /* |
9703fefe | 2941 | * We needn't truncate any block which is beyond the end of the file |
7426cc04 MX |
2942 | * because we are sure there is no data there. |
2943 | */ | |
2aaa6655 | 2944 | /* |
9703fefe CR |
2945 | * Only do this if we are in the same block and we aren't doing the |
2946 | * entire block. | |
2aaa6655 | 2947 | */ |
0b246afa | 2948 | if (same_block && len < fs_info->sectorsize) { |
e8c1c76e | 2949 | if (offset < ino_size) { |
9703fefe | 2950 | truncated_block = true; |
217f42eb NB |
2951 | ret = btrfs_truncate_block(BTRFS_I(inode), offset, len, |
2952 | 0); | |
e8c1c76e FM |
2953 | } else { |
2954 | ret = 0; | |
2955 | } | |
d7781546 | 2956 | goto out_only_mutex; |
2aaa6655 JB |
2957 | } |
2958 | ||
9703fefe | 2959 | /* zero back part of the first block */ |
12870f1c | 2960 | if (offset < ino_size) { |
9703fefe | 2961 | truncated_block = true; |
217f42eb | 2962 | ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0); |
7426cc04 | 2963 | if (ret) { |
8d9b4a16 | 2964 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
7426cc04 MX |
2965 | return ret; |
2966 | } | |
2aaa6655 JB |
2967 | } |
2968 | ||
d7781546 QW |
2969 | /* Check the aligned pages after the first unaligned page, |
2970 | * if offset != orig_start, which means the first unaligned page | |
01327610 | 2971 | * including several following pages are already in holes, |
d7781546 QW |
2972 | * the extra check can be skipped */ |
2973 | if (offset == orig_start) { | |
2974 | /* after truncate page, check hole again */ | |
2975 | len = offset + len - lockstart; | |
2976 | offset = lockstart; | |
dea46d84 | 2977 | ret = find_first_non_hole(BTRFS_I(inode), &offset, &len); |
d7781546 QW |
2978 | if (ret < 0) |
2979 | goto out_only_mutex; | |
2980 | if (ret && !len) { | |
2981 | ret = 0; | |
2982 | goto out_only_mutex; | |
2983 | } | |
2984 | lockstart = offset; | |
2985 | } | |
2986 | ||
2987 | /* Check the tail unaligned part is in a hole */ | |
2988 | tail_start = lockend + 1; | |
2989 | tail_len = offset + len - tail_start; | |
2990 | if (tail_len) { | |
dea46d84 | 2991 | ret = find_first_non_hole(BTRFS_I(inode), &tail_start, &tail_len); |
d7781546 QW |
2992 | if (unlikely(ret < 0)) |
2993 | goto out_only_mutex; | |
2994 | if (!ret) { | |
2995 | /* zero the front end of the last page */ | |
2996 | if (tail_start + tail_len < ino_size) { | |
9703fefe | 2997 | truncated_block = true; |
217f42eb | 2998 | ret = btrfs_truncate_block(BTRFS_I(inode), |
9703fefe CR |
2999 | tail_start + tail_len, |
3000 | 0, 1); | |
d7781546 QW |
3001 | if (ret) |
3002 | goto out_only_mutex; | |
51f395ad | 3003 | } |
0061280d | 3004 | } |
2aaa6655 JB |
3005 | } |
3006 | ||
3007 | if (lockend < lockstart) { | |
e8c1c76e FM |
3008 | ret = 0; |
3009 | goto out_only_mutex; | |
2aaa6655 JB |
3010 | } |
3011 | ||
55961c8a | 3012 | btrfs_punch_hole_lock_range(inode, lockstart, lockend, &cached_state); |
2aaa6655 JB |
3013 | |
3014 | path = btrfs_alloc_path(); | |
3015 | if (!path) { | |
3016 | ret = -ENOMEM; | |
3017 | goto out; | |
3018 | } | |
3019 | ||
bfc78479 NB |
3020 | ret = btrfs_replace_file_extents(BTRFS_I(inode), path, lockstart, |
3021 | lockend, NULL, &trans); | |
9cba40a6 FM |
3022 | btrfs_free_path(path); |
3023 | if (ret) | |
3024 | goto out; | |
2aaa6655 | 3025 | |
9cba40a6 | 3026 | ASSERT(trans != NULL); |
e1f5790e | 3027 | inode_inc_iversion(inode); |
c2050a45 | 3028 | inode->i_mtime = inode->i_ctime = current_time(inode); |
9a56fcd1 | 3029 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
e8c1c76e | 3030 | updated_inode = true; |
3a45bb20 | 3031 | btrfs_end_transaction(trans); |
2ff7e61e | 3032 | btrfs_btree_balance_dirty(fs_info); |
2aaa6655 JB |
3033 | out: |
3034 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
e43bbe5e | 3035 | &cached_state); |
d7781546 | 3036 | out_only_mutex: |
9cba40a6 | 3037 | if (!updated_inode && truncated_block && !ret) { |
e8c1c76e FM |
3038 | /* |
3039 | * If we only end up zeroing part of a page, we still need to | |
3040 | * update the inode item, so that all the time fields are | |
3041 | * updated as well as the necessary btrfs inode in memory fields | |
3042 | * for detecting, at fsync time, if the inode isn't yet in the | |
3043 | * log tree or it's there but not up to date. | |
3044 | */ | |
17900668 FM |
3045 | struct timespec64 now = current_time(inode); |
3046 | ||
3047 | inode_inc_iversion(inode); | |
3048 | inode->i_mtime = now; | |
3049 | inode->i_ctime = now; | |
e8c1c76e FM |
3050 | trans = btrfs_start_transaction(root, 1); |
3051 | if (IS_ERR(trans)) { | |
9cba40a6 | 3052 | ret = PTR_ERR(trans); |
e8c1c76e | 3053 | } else { |
9cba40a6 FM |
3054 | int ret2; |
3055 | ||
9a56fcd1 | 3056 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
9cba40a6 FM |
3057 | ret2 = btrfs_end_transaction(trans); |
3058 | if (!ret) | |
3059 | ret = ret2; | |
e8c1c76e FM |
3060 | } |
3061 | } | |
8d9b4a16 | 3062 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
9cba40a6 | 3063 | return ret; |
2aaa6655 JB |
3064 | } |
3065 | ||
14524a84 QW |
3066 | /* Helper structure to record which range is already reserved */ |
3067 | struct falloc_range { | |
3068 | struct list_head list; | |
3069 | u64 start; | |
3070 | u64 len; | |
3071 | }; | |
3072 | ||
3073 | /* | |
3074 | * Helper function to add falloc range | |
3075 | * | |
3076 | * Caller should have locked the larger range of extent containing | |
3077 | * [start, len) | |
3078 | */ | |
3079 | static int add_falloc_range(struct list_head *head, u64 start, u64 len) | |
3080 | { | |
14524a84 QW |
3081 | struct falloc_range *range = NULL; |
3082 | ||
77d25534 NB |
3083 | if (!list_empty(head)) { |
3084 | /* | |
3085 | * As fallocate iterates by bytenr order, we only need to check | |
3086 | * the last range. | |
3087 | */ | |
3088 | range = list_last_entry(head, struct falloc_range, list); | |
3089 | if (range->start + range->len == start) { | |
3090 | range->len += len; | |
3091 | return 0; | |
3092 | } | |
14524a84 | 3093 | } |
77d25534 | 3094 | |
32fc932e | 3095 | range = kmalloc(sizeof(*range), GFP_KERNEL); |
14524a84 QW |
3096 | if (!range) |
3097 | return -ENOMEM; | |
3098 | range->start = start; | |
3099 | range->len = len; | |
3100 | list_add_tail(&range->list, head); | |
3101 | return 0; | |
3102 | } | |
3103 | ||
f27451f2 FM |
3104 | static int btrfs_fallocate_update_isize(struct inode *inode, |
3105 | const u64 end, | |
3106 | const int mode) | |
3107 | { | |
3108 | struct btrfs_trans_handle *trans; | |
3109 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3110 | int ret; | |
3111 | int ret2; | |
3112 | ||
3113 | if (mode & FALLOC_FL_KEEP_SIZE || end <= i_size_read(inode)) | |
3114 | return 0; | |
3115 | ||
3116 | trans = btrfs_start_transaction(root, 1); | |
3117 | if (IS_ERR(trans)) | |
3118 | return PTR_ERR(trans); | |
3119 | ||
3120 | inode->i_ctime = current_time(inode); | |
3121 | i_size_write(inode, end); | |
76aea537 | 3122 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
9a56fcd1 | 3123 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
f27451f2 FM |
3124 | ret2 = btrfs_end_transaction(trans); |
3125 | ||
3126 | return ret ? ret : ret2; | |
3127 | } | |
3128 | ||
81fdf638 | 3129 | enum { |
f262fa8d DS |
3130 | RANGE_BOUNDARY_WRITTEN_EXTENT, |
3131 | RANGE_BOUNDARY_PREALLOC_EXTENT, | |
3132 | RANGE_BOUNDARY_HOLE, | |
81fdf638 FM |
3133 | }; |
3134 | ||
948dfeb8 | 3135 | static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode, |
f27451f2 FM |
3136 | u64 offset) |
3137 | { | |
948dfeb8 | 3138 | const u64 sectorsize = btrfs_inode_sectorsize(inode); |
f27451f2 | 3139 | struct extent_map *em; |
81fdf638 | 3140 | int ret; |
f27451f2 FM |
3141 | |
3142 | offset = round_down(offset, sectorsize); | |
948dfeb8 | 3143 | em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize); |
f27451f2 FM |
3144 | if (IS_ERR(em)) |
3145 | return PTR_ERR(em); | |
3146 | ||
3147 | if (em->block_start == EXTENT_MAP_HOLE) | |
81fdf638 FM |
3148 | ret = RANGE_BOUNDARY_HOLE; |
3149 | else if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
3150 | ret = RANGE_BOUNDARY_PREALLOC_EXTENT; | |
3151 | else | |
3152 | ret = RANGE_BOUNDARY_WRITTEN_EXTENT; | |
f27451f2 FM |
3153 | |
3154 | free_extent_map(em); | |
3155 | return ret; | |
3156 | } | |
3157 | ||
3158 | static int btrfs_zero_range(struct inode *inode, | |
3159 | loff_t offset, | |
3160 | loff_t len, | |
3161 | const int mode) | |
3162 | { | |
3163 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
3164 | struct extent_map *em; | |
3165 | struct extent_changeset *data_reserved = NULL; | |
3166 | int ret; | |
3167 | u64 alloc_hint = 0; | |
6fee248d | 3168 | const u64 sectorsize = btrfs_inode_sectorsize(BTRFS_I(inode)); |
f27451f2 FM |
3169 | u64 alloc_start = round_down(offset, sectorsize); |
3170 | u64 alloc_end = round_up(offset + len, sectorsize); | |
3171 | u64 bytes_to_reserve = 0; | |
3172 | bool space_reserved = false; | |
3173 | ||
39b07b5d OS |
3174 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start, |
3175 | alloc_end - alloc_start); | |
f27451f2 FM |
3176 | if (IS_ERR(em)) { |
3177 | ret = PTR_ERR(em); | |
3178 | goto out; | |
3179 | } | |
3180 | ||
3181 | /* | |
3182 | * Avoid hole punching and extent allocation for some cases. More cases | |
3183 | * could be considered, but these are unlikely common and we keep things | |
3184 | * as simple as possible for now. Also, intentionally, if the target | |
3185 | * range contains one or more prealloc extents together with regular | |
3186 | * extents and holes, we drop all the existing extents and allocate a | |
3187 | * new prealloc extent, so that we get a larger contiguous disk extent. | |
3188 | */ | |
3189 | if (em->start <= alloc_start && | |
3190 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { | |
3191 | const u64 em_end = em->start + em->len; | |
3192 | ||
3193 | if (em_end >= offset + len) { | |
3194 | /* | |
3195 | * The whole range is already a prealloc extent, | |
3196 | * do nothing except updating the inode's i_size if | |
3197 | * needed. | |
3198 | */ | |
3199 | free_extent_map(em); | |
3200 | ret = btrfs_fallocate_update_isize(inode, offset + len, | |
3201 | mode); | |
3202 | goto out; | |
3203 | } | |
3204 | /* | |
3205 | * Part of the range is already a prealloc extent, so operate | |
3206 | * only on the remaining part of the range. | |
3207 | */ | |
3208 | alloc_start = em_end; | |
3209 | ASSERT(IS_ALIGNED(alloc_start, sectorsize)); | |
3210 | len = offset + len - alloc_start; | |
3211 | offset = alloc_start; | |
3212 | alloc_hint = em->block_start + em->len; | |
3213 | } | |
3214 | free_extent_map(em); | |
3215 | ||
3216 | if (BTRFS_BYTES_TO_BLKS(fs_info, offset) == | |
3217 | BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)) { | |
39b07b5d OS |
3218 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start, |
3219 | sectorsize); | |
f27451f2 FM |
3220 | if (IS_ERR(em)) { |
3221 | ret = PTR_ERR(em); | |
3222 | goto out; | |
3223 | } | |
3224 | ||
3225 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { | |
3226 | free_extent_map(em); | |
3227 | ret = btrfs_fallocate_update_isize(inode, offset + len, | |
3228 | mode); | |
3229 | goto out; | |
3230 | } | |
3231 | if (len < sectorsize && em->block_start != EXTENT_MAP_HOLE) { | |
3232 | free_extent_map(em); | |
217f42eb NB |
3233 | ret = btrfs_truncate_block(BTRFS_I(inode), offset, len, |
3234 | 0); | |
f27451f2 FM |
3235 | if (!ret) |
3236 | ret = btrfs_fallocate_update_isize(inode, | |
3237 | offset + len, | |
3238 | mode); | |
3239 | return ret; | |
3240 | } | |
3241 | free_extent_map(em); | |
3242 | alloc_start = round_down(offset, sectorsize); | |
3243 | alloc_end = alloc_start + sectorsize; | |
3244 | goto reserve_space; | |
3245 | } | |
3246 | ||
3247 | alloc_start = round_up(offset, sectorsize); | |
3248 | alloc_end = round_down(offset + len, sectorsize); | |
3249 | ||
3250 | /* | |
3251 | * For unaligned ranges, check the pages at the boundaries, they might | |
3252 | * map to an extent, in which case we need to partially zero them, or | |
3253 | * they might map to a hole, in which case we need our allocation range | |
3254 | * to cover them. | |
3255 | */ | |
3256 | if (!IS_ALIGNED(offset, sectorsize)) { | |
948dfeb8 NB |
3257 | ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode), |
3258 | offset); | |
f27451f2 FM |
3259 | if (ret < 0) |
3260 | goto out; | |
81fdf638 | 3261 | if (ret == RANGE_BOUNDARY_HOLE) { |
f27451f2 FM |
3262 | alloc_start = round_down(offset, sectorsize); |
3263 | ret = 0; | |
81fdf638 | 3264 | } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) { |
217f42eb | 3265 | ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0); |
f27451f2 FM |
3266 | if (ret) |
3267 | goto out; | |
81fdf638 FM |
3268 | } else { |
3269 | ret = 0; | |
f27451f2 FM |
3270 | } |
3271 | } | |
3272 | ||
3273 | if (!IS_ALIGNED(offset + len, sectorsize)) { | |
948dfeb8 | 3274 | ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode), |
f27451f2 FM |
3275 | offset + len); |
3276 | if (ret < 0) | |
3277 | goto out; | |
81fdf638 | 3278 | if (ret == RANGE_BOUNDARY_HOLE) { |
f27451f2 FM |
3279 | alloc_end = round_up(offset + len, sectorsize); |
3280 | ret = 0; | |
81fdf638 | 3281 | } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) { |
217f42eb NB |
3282 | ret = btrfs_truncate_block(BTRFS_I(inode), offset + len, |
3283 | 0, 1); | |
f27451f2 FM |
3284 | if (ret) |
3285 | goto out; | |
81fdf638 FM |
3286 | } else { |
3287 | ret = 0; | |
f27451f2 FM |
3288 | } |
3289 | } | |
3290 | ||
3291 | reserve_space: | |
3292 | if (alloc_start < alloc_end) { | |
3293 | struct extent_state *cached_state = NULL; | |
3294 | const u64 lockstart = alloc_start; | |
3295 | const u64 lockend = alloc_end - 1; | |
3296 | ||
3297 | bytes_to_reserve = alloc_end - alloc_start; | |
3298 | ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), | |
3299 | bytes_to_reserve); | |
3300 | if (ret < 0) | |
3301 | goto out; | |
3302 | space_reserved = true; | |
55961c8a FM |
3303 | btrfs_punch_hole_lock_range(inode, lockstart, lockend, |
3304 | &cached_state); | |
7661a3e0 | 3305 | ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), &data_reserved, |
a7f8b1c2 | 3306 | alloc_start, bytes_to_reserve); |
4f6a49de NB |
3307 | if (ret) { |
3308 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, | |
3309 | lockend, &cached_state); | |
a7f8b1c2 | 3310 | goto out; |
4f6a49de | 3311 | } |
f27451f2 FM |
3312 | ret = btrfs_prealloc_file_range(inode, mode, alloc_start, |
3313 | alloc_end - alloc_start, | |
3314 | i_blocksize(inode), | |
3315 | offset + len, &alloc_hint); | |
3316 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, | |
3317 | lockend, &cached_state); | |
3318 | /* btrfs_prealloc_file_range releases reserved space on error */ | |
9f13ce74 | 3319 | if (ret) { |
f27451f2 | 3320 | space_reserved = false; |
9f13ce74 FM |
3321 | goto out; |
3322 | } | |
f27451f2 | 3323 | } |
9f13ce74 | 3324 | ret = btrfs_fallocate_update_isize(inode, offset + len, mode); |
f27451f2 FM |
3325 | out: |
3326 | if (ret && space_reserved) | |
25ce28ca | 3327 | btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved, |
f27451f2 FM |
3328 | alloc_start, bytes_to_reserve); |
3329 | extent_changeset_free(data_reserved); | |
3330 | ||
3331 | return ret; | |
3332 | } | |
3333 | ||
2fe17c10 CH |
3334 | static long btrfs_fallocate(struct file *file, int mode, |
3335 | loff_t offset, loff_t len) | |
3336 | { | |
496ad9aa | 3337 | struct inode *inode = file_inode(file); |
2fe17c10 | 3338 | struct extent_state *cached_state = NULL; |
364ecf36 | 3339 | struct extent_changeset *data_reserved = NULL; |
14524a84 QW |
3340 | struct falloc_range *range; |
3341 | struct falloc_range *tmp; | |
3342 | struct list_head reserve_list; | |
2fe17c10 CH |
3343 | u64 cur_offset; |
3344 | u64 last_byte; | |
3345 | u64 alloc_start; | |
3346 | u64 alloc_end; | |
3347 | u64 alloc_hint = 0; | |
3348 | u64 locked_end; | |
14524a84 | 3349 | u64 actual_end = 0; |
47e1d1c7 FM |
3350 | u64 data_space_needed = 0; |
3351 | u64 data_space_reserved = 0; | |
3352 | u64 qgroup_reserved = 0; | |
2fe17c10 | 3353 | struct extent_map *em; |
6fee248d | 3354 | int blocksize = btrfs_inode_sectorsize(BTRFS_I(inode)); |
2fe17c10 CH |
3355 | int ret; |
3356 | ||
f1569c4c NA |
3357 | /* Do not allow fallocate in ZONED mode */ |
3358 | if (btrfs_is_zoned(btrfs_sb(inode->i_sb))) | |
3359 | return -EOPNOTSUPP; | |
3360 | ||
797f4277 MX |
3361 | alloc_start = round_down(offset, blocksize); |
3362 | alloc_end = round_up(offset + len, blocksize); | |
18513091 | 3363 | cur_offset = alloc_start; |
2fe17c10 | 3364 | |
2aaa6655 | 3365 | /* Make sure we aren't being give some crap mode */ |
f27451f2 FM |
3366 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
3367 | FALLOC_FL_ZERO_RANGE)) | |
2fe17c10 CH |
3368 | return -EOPNOTSUPP; |
3369 | ||
2aaa6655 | 3370 | if (mode & FALLOC_FL_PUNCH_HOLE) |
05fd9564 | 3371 | return btrfs_punch_hole(file, offset, len); |
2aaa6655 | 3372 | |
8d9b4a16 | 3373 | btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP); |
2a162ce9 DI |
3374 | |
3375 | if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) { | |
3376 | ret = inode_newsize_ok(inode, offset + len); | |
3377 | if (ret) | |
3378 | goto out; | |
3379 | } | |
2fe17c10 | 3380 | |
05fd9564 DW |
3381 | ret = file_modified(file); |
3382 | if (ret) | |
3383 | goto out; | |
3384 | ||
14524a84 QW |
3385 | /* |
3386 | * TODO: Move these two operations after we have checked | |
3387 | * accurate reserved space, or fallocate can still fail but | |
3388 | * with page truncated or size expanded. | |
3389 | * | |
3390 | * But that's a minor problem and won't do much harm BTW. | |
3391 | */ | |
2fe17c10 | 3392 | if (alloc_start > inode->i_size) { |
b06359a3 | 3393 | ret = btrfs_cont_expand(BTRFS_I(inode), i_size_read(inode), |
a41ad394 | 3394 | alloc_start); |
2fe17c10 CH |
3395 | if (ret) |
3396 | goto out; | |
0f6925fa | 3397 | } else if (offset + len > inode->i_size) { |
a71754fc JB |
3398 | /* |
3399 | * If we are fallocating from the end of the file onward we | |
9703fefe CR |
3400 | * need to zero out the end of the block if i_size lands in the |
3401 | * middle of a block. | |
a71754fc | 3402 | */ |
217f42eb | 3403 | ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0); |
a71754fc JB |
3404 | if (ret) |
3405 | goto out; | |
2fe17c10 CH |
3406 | } |
3407 | ||
a71754fc | 3408 | /* |
ffa8fc60 FM |
3409 | * We have locked the inode at the VFS level (in exclusive mode) and we |
3410 | * have locked the i_mmap_lock lock (in exclusive mode). Now before | |
3411 | * locking the file range, flush all dealloc in the range and wait for | |
3412 | * all ordered extents in the range to complete. After this we can lock | |
3413 | * the file range and, due to the previous locking we did, we know there | |
3414 | * can't be more delalloc or ordered extents in the range. | |
a71754fc | 3415 | */ |
0ef8b726 JB |
3416 | ret = btrfs_wait_ordered_range(inode, alloc_start, |
3417 | alloc_end - alloc_start); | |
3418 | if (ret) | |
3419 | goto out; | |
a71754fc | 3420 | |
f27451f2 FM |
3421 | if (mode & FALLOC_FL_ZERO_RANGE) { |
3422 | ret = btrfs_zero_range(inode, offset, len, mode); | |
8d9b4a16 | 3423 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
f27451f2 FM |
3424 | return ret; |
3425 | } | |
3426 | ||
2fe17c10 | 3427 | locked_end = alloc_end - 1; |
ffa8fc60 FM |
3428 | lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
3429 | &cached_state); | |
2fe17c10 | 3430 | |
63c34cb4 FM |
3431 | btrfs_assert_inode_range_clean(BTRFS_I(inode), alloc_start, locked_end); |
3432 | ||
14524a84 QW |
3433 | /* First, check if we exceed the qgroup limit */ |
3434 | INIT_LIST_HEAD(&reserve_list); | |
6b7d6e93 | 3435 | while (cur_offset < alloc_end) { |
fc4f21b1 | 3436 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
39b07b5d | 3437 | alloc_end - cur_offset); |
9986277e DC |
3438 | if (IS_ERR(em)) { |
3439 | ret = PTR_ERR(em); | |
79787eaa JM |
3440 | break; |
3441 | } | |
2fe17c10 | 3442 | last_byte = min(extent_map_end(em), alloc_end); |
f1e490a7 | 3443 | actual_end = min_t(u64, extent_map_end(em), offset + len); |
797f4277 | 3444 | last_byte = ALIGN(last_byte, blocksize); |
2fe17c10 CH |
3445 | if (em->block_start == EXTENT_MAP_HOLE || |
3446 | (cur_offset >= inode->i_size && | |
3447 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
47e1d1c7 FM |
3448 | const u64 range_len = last_byte - cur_offset; |
3449 | ||
3450 | ret = add_falloc_range(&reserve_list, cur_offset, range_len); | |
14524a84 QW |
3451 | if (ret < 0) { |
3452 | free_extent_map(em); | |
3453 | break; | |
3d850dd4 | 3454 | } |
7661a3e0 | 3455 | ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), |
47e1d1c7 | 3456 | &data_reserved, cur_offset, range_len); |
be2d253c FM |
3457 | if (ret < 0) { |
3458 | free_extent_map(em); | |
14524a84 | 3459 | break; |
be2d253c | 3460 | } |
47e1d1c7 FM |
3461 | qgroup_reserved += range_len; |
3462 | data_space_needed += range_len; | |
2fe17c10 CH |
3463 | } |
3464 | free_extent_map(em); | |
2fe17c10 | 3465 | cur_offset = last_byte; |
14524a84 QW |
3466 | } |
3467 | ||
47e1d1c7 FM |
3468 | if (!ret && data_space_needed > 0) { |
3469 | /* | |
3470 | * We are safe to reserve space here as we can't have delalloc | |
3471 | * in the range, see above. | |
3472 | */ | |
3473 | ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), | |
3474 | data_space_needed); | |
3475 | if (!ret) | |
3476 | data_space_reserved = data_space_needed; | |
3477 | } | |
3478 | ||
14524a84 QW |
3479 | /* |
3480 | * If ret is still 0, means we're OK to fallocate. | |
3481 | * Or just cleanup the list and exit. | |
3482 | */ | |
3483 | list_for_each_entry_safe(range, tmp, &reserve_list, list) { | |
47e1d1c7 | 3484 | if (!ret) { |
14524a84 QW |
3485 | ret = btrfs_prealloc_file_range(inode, mode, |
3486 | range->start, | |
93407472 | 3487 | range->len, i_blocksize(inode), |
14524a84 | 3488 | offset + len, &alloc_hint); |
47e1d1c7 FM |
3489 | /* |
3490 | * btrfs_prealloc_file_range() releases space even | |
3491 | * if it returns an error. | |
3492 | */ | |
3493 | data_space_reserved -= range->len; | |
3494 | qgroup_reserved -= range->len; | |
3495 | } else if (data_space_reserved > 0) { | |
25ce28ca | 3496 | btrfs_free_reserved_data_space(BTRFS_I(inode), |
47e1d1c7 FM |
3497 | data_reserved, range->start, |
3498 | range->len); | |
3499 | data_space_reserved -= range->len; | |
3500 | qgroup_reserved -= range->len; | |
3501 | } else if (qgroup_reserved > 0) { | |
3502 | btrfs_qgroup_free_data(BTRFS_I(inode), data_reserved, | |
3503 | range->start, range->len); | |
3504 | qgroup_reserved -= range->len; | |
3505 | } | |
14524a84 QW |
3506 | list_del(&range->list); |
3507 | kfree(range); | |
3508 | } | |
3509 | if (ret < 0) | |
3510 | goto out_unlock; | |
3511 | ||
f27451f2 FM |
3512 | /* |
3513 | * We didn't need to allocate any more space, but we still extended the | |
3514 | * size of the file so we need to update i_size and the inode item. | |
3515 | */ | |
3516 | ret = btrfs_fallocate_update_isize(inode, actual_end, mode); | |
14524a84 | 3517 | out_unlock: |
2fe17c10 | 3518 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
e43bbe5e | 3519 | &cached_state); |
2fe17c10 | 3520 | out: |
8d9b4a16 | 3521 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
364ecf36 | 3522 | extent_changeset_free(data_reserved); |
2fe17c10 CH |
3523 | return ret; |
3524 | } | |
3525 | ||
cca5de97 | 3526 | static loff_t find_desired_extent(struct btrfs_inode *inode, loff_t offset, |
bc80230e | 3527 | int whence) |
b2675157 | 3528 | { |
cca5de97 | 3529 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
7f4ca37c | 3530 | struct extent_map *em = NULL; |
b2675157 | 3531 | struct extent_state *cached_state = NULL; |
cca5de97 | 3532 | loff_t i_size = inode->vfs_inode.i_size; |
4d1a40c6 LB |
3533 | u64 lockstart; |
3534 | u64 lockend; | |
3535 | u64 start; | |
3536 | u64 len; | |
b2675157 JB |
3537 | int ret = 0; |
3538 | ||
bc80230e | 3539 | if (i_size == 0 || offset >= i_size) |
4d1a40c6 LB |
3540 | return -ENXIO; |
3541 | ||
3542 | /* | |
bc80230e | 3543 | * offset can be negative, in this case we start finding DATA/HOLE from |
4d1a40c6 LB |
3544 | * the very start of the file. |
3545 | */ | |
bc80230e | 3546 | start = max_t(loff_t, 0, offset); |
4d1a40c6 | 3547 | |
0b246afa | 3548 | lockstart = round_down(start, fs_info->sectorsize); |
d79b7c26 | 3549 | lockend = round_up(i_size, fs_info->sectorsize); |
b2675157 | 3550 | if (lockend <= lockstart) |
0b246afa | 3551 | lockend = lockstart + fs_info->sectorsize; |
1214b53f | 3552 | lockend--; |
b2675157 JB |
3553 | len = lockend - lockstart + 1; |
3554 | ||
cca5de97 | 3555 | lock_extent_bits(&inode->io_tree, lockstart, lockend, &cached_state); |
b2675157 | 3556 | |
d79b7c26 | 3557 | while (start < i_size) { |
cca5de97 | 3558 | em = btrfs_get_extent_fiemap(inode, start, len); |
b2675157 | 3559 | if (IS_ERR(em)) { |
6af021d8 | 3560 | ret = PTR_ERR(em); |
7f4ca37c | 3561 | em = NULL; |
b2675157 JB |
3562 | break; |
3563 | } | |
3564 | ||
7f4ca37c JB |
3565 | if (whence == SEEK_HOLE && |
3566 | (em->block_start == EXTENT_MAP_HOLE || | |
3567 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) | |
3568 | break; | |
3569 | else if (whence == SEEK_DATA && | |
3570 | (em->block_start != EXTENT_MAP_HOLE && | |
3571 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) | |
3572 | break; | |
b2675157 JB |
3573 | |
3574 | start = em->start + em->len; | |
b2675157 | 3575 | free_extent_map(em); |
7f4ca37c | 3576 | em = NULL; |
b2675157 JB |
3577 | cond_resched(); |
3578 | } | |
7f4ca37c | 3579 | free_extent_map(em); |
cca5de97 | 3580 | unlock_extent_cached(&inode->io_tree, lockstart, lockend, |
bc80230e NB |
3581 | &cached_state); |
3582 | if (ret) { | |
3583 | offset = ret; | |
3584 | } else { | |
d79b7c26 | 3585 | if (whence == SEEK_DATA && start >= i_size) |
bc80230e | 3586 | offset = -ENXIO; |
7f4ca37c | 3587 | else |
bc80230e | 3588 | offset = min_t(loff_t, start, i_size); |
7f4ca37c | 3589 | } |
bc80230e NB |
3590 | |
3591 | return offset; | |
b2675157 JB |
3592 | } |
3593 | ||
965c8e59 | 3594 | static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence) |
b2675157 JB |
3595 | { |
3596 | struct inode *inode = file->f_mapping->host; | |
b2675157 | 3597 | |
965c8e59 | 3598 | switch (whence) { |
2034f3b4 NB |
3599 | default: |
3600 | return generic_file_llseek(file, offset, whence); | |
b2675157 JB |
3601 | case SEEK_DATA: |
3602 | case SEEK_HOLE: | |
a14b78ad | 3603 | btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED); |
cca5de97 | 3604 | offset = find_desired_extent(BTRFS_I(inode), offset, whence); |
a14b78ad | 3605 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
bc80230e | 3606 | break; |
b2675157 JB |
3607 | } |
3608 | ||
bc80230e NB |
3609 | if (offset < 0) |
3610 | return offset; | |
3611 | ||
2034f3b4 | 3612 | return vfs_setpos(file, offset, inode->i_sb->s_maxbytes); |
b2675157 JB |
3613 | } |
3614 | ||
edf064e7 GR |
3615 | static int btrfs_file_open(struct inode *inode, struct file *filp) |
3616 | { | |
14605409 BB |
3617 | int ret; |
3618 | ||
8730f12b | 3619 | filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC; |
14605409 BB |
3620 | |
3621 | ret = fsverity_file_open(inode, filp); | |
3622 | if (ret) | |
3623 | return ret; | |
edf064e7 GR |
3624 | return generic_file_open(inode, filp); |
3625 | } | |
3626 | ||
4e4cabec GR |
3627 | static int check_direct_read(struct btrfs_fs_info *fs_info, |
3628 | const struct iov_iter *iter, loff_t offset) | |
3629 | { | |
3630 | int ret; | |
3631 | int i, seg; | |
3632 | ||
3633 | ret = check_direct_IO(fs_info, iter, offset); | |
3634 | if (ret < 0) | |
3635 | return ret; | |
3636 | ||
3637 | if (!iter_is_iovec(iter)) | |
3638 | return 0; | |
3639 | ||
3640 | for (seg = 0; seg < iter->nr_segs; seg++) | |
3641 | for (i = seg + 1; i < iter->nr_segs; i++) | |
3642 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
3643 | return -EINVAL; | |
3644 | return 0; | |
3645 | } | |
3646 | ||
3647 | static ssize_t btrfs_direct_read(struct kiocb *iocb, struct iov_iter *to) | |
3648 | { | |
3649 | struct inode *inode = file_inode(iocb->ki_filp); | |
51bd9563 FM |
3650 | size_t prev_left = 0; |
3651 | ssize_t read = 0; | |
4e4cabec GR |
3652 | ssize_t ret; |
3653 | ||
14605409 BB |
3654 | if (fsverity_active(inode)) |
3655 | return 0; | |
3656 | ||
4e4cabec GR |
3657 | if (check_direct_read(btrfs_sb(inode->i_sb), to, iocb->ki_pos)) |
3658 | return 0; | |
3659 | ||
a14b78ad | 3660 | btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED); |
51bd9563 FM |
3661 | again: |
3662 | /* | |
3663 | * This is similar to what we do for direct IO writes, see the comment | |
3664 | * at btrfs_direct_write(), but we also disable page faults in addition | |
3665 | * to disabling them only at the iov_iter level. This is because when | |
3666 | * reading from a hole or prealloc extent, iomap calls iov_iter_zero(), | |
3667 | * which can still trigger page fault ins despite having set ->nofault | |
3668 | * to true of our 'to' iov_iter. | |
3669 | * | |
3670 | * The difference to direct IO writes is that we deadlock when trying | |
3671 | * to lock the extent range in the inode's tree during he page reads | |
3672 | * triggered by the fault in (while for writes it is due to waiting for | |
3673 | * our own ordered extent). This is because for direct IO reads, | |
3674 | * btrfs_dio_iomap_begin() returns with the extent range locked, which | |
3675 | * is only unlocked in the endio callback (end_bio_extent_readpage()). | |
3676 | */ | |
3677 | pagefault_disable(); | |
3678 | to->nofault = true; | |
36e8c622 | 3679 | ret = btrfs_dio_rw(iocb, to, read); |
51bd9563 FM |
3680 | to->nofault = false; |
3681 | pagefault_enable(); | |
3682 | ||
3683 | /* No increment (+=) because iomap returns a cumulative value. */ | |
3684 | if (ret > 0) | |
3685 | read = ret; | |
3686 | ||
3687 | if (iov_iter_count(to) > 0 && (ret == -EFAULT || ret > 0)) { | |
3688 | const size_t left = iov_iter_count(to); | |
3689 | ||
3690 | if (left == prev_left) { | |
3691 | /* | |
3692 | * We didn't make any progress since the last attempt, | |
3693 | * fallback to a buffered read for the remainder of the | |
3694 | * range. This is just to avoid any possibility of looping | |
3695 | * for too long. | |
3696 | */ | |
3697 | ret = read; | |
3698 | } else { | |
3699 | /* | |
3700 | * We made some progress since the last retry or this is | |
3701 | * the first time we are retrying. Fault in as many pages | |
3702 | * as possible and retry. | |
3703 | */ | |
3704 | fault_in_iov_iter_writeable(to, left); | |
3705 | prev_left = left; | |
3706 | goto again; | |
3707 | } | |
3708 | } | |
a14b78ad | 3709 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
51bd9563 | 3710 | return ret < 0 ? ret : read; |
4e4cabec GR |
3711 | } |
3712 | ||
f85781fb GR |
3713 | static ssize_t btrfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
3714 | { | |
3715 | ssize_t ret = 0; | |
3716 | ||
3717 | if (iocb->ki_flags & IOCB_DIRECT) { | |
4e4cabec | 3718 | ret = btrfs_direct_read(iocb, to); |
0425e7ba JT |
3719 | if (ret < 0 || !iov_iter_count(to) || |
3720 | iocb->ki_pos >= i_size_read(file_inode(iocb->ki_filp))) | |
f85781fb GR |
3721 | return ret; |
3722 | } | |
3723 | ||
87fa0f3e | 3724 | return filemap_read(iocb, to, ret); |
f85781fb GR |
3725 | } |
3726 | ||
828c0950 | 3727 | const struct file_operations btrfs_file_operations = { |
b2675157 | 3728 | .llseek = btrfs_file_llseek, |
f85781fb | 3729 | .read_iter = btrfs_file_read_iter, |
e9906a98 | 3730 | .splice_read = generic_file_splice_read, |
b30ac0fc | 3731 | .write_iter = btrfs_file_write_iter, |
d7776591 | 3732 | .splice_write = iter_file_splice_write, |
9ebefb18 | 3733 | .mmap = btrfs_file_mmap, |
edf064e7 | 3734 | .open = btrfs_file_open, |
e1b81e67 | 3735 | .release = btrfs_release_file, |
39279cc3 | 3736 | .fsync = btrfs_sync_file, |
2fe17c10 | 3737 | .fallocate = btrfs_fallocate, |
34287aa3 | 3738 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 3739 | #ifdef CONFIG_COMPAT |
4c63c245 | 3740 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 3741 | #endif |
2e5dfc99 | 3742 | .remap_file_range = btrfs_remap_file_range, |
39279cc3 | 3743 | }; |
9247f317 | 3744 | |
e67c718b | 3745 | void __cold btrfs_auto_defrag_exit(void) |
9247f317 | 3746 | { |
5598e900 | 3747 | kmem_cache_destroy(btrfs_inode_defrag_cachep); |
9247f317 MX |
3748 | } |
3749 | ||
f5c29bd9 | 3750 | int __init btrfs_auto_defrag_init(void) |
9247f317 MX |
3751 | { |
3752 | btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag", | |
3753 | sizeof(struct inode_defrag), 0, | |
fba4b697 | 3754 | SLAB_MEM_SPREAD, |
9247f317 MX |
3755 | NULL); |
3756 | if (!btrfs_inode_defrag_cachep) | |
3757 | return -ENOMEM; | |
3758 | ||
3759 | return 0; | |
3760 | } | |
728404da FM |
3761 | |
3762 | int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end) | |
3763 | { | |
3764 | int ret; | |
3765 | ||
3766 | /* | |
3767 | * So with compression we will find and lock a dirty page and clear the | |
3768 | * first one as dirty, setup an async extent, and immediately return | |
3769 | * with the entire range locked but with nobody actually marked with | |
3770 | * writeback. So we can't just filemap_write_and_wait_range() and | |
3771 | * expect it to work since it will just kick off a thread to do the | |
3772 | * actual work. So we need to call filemap_fdatawrite_range _again_ | |
3773 | * since it will wait on the page lock, which won't be unlocked until | |
3774 | * after the pages have been marked as writeback and so we're good to go | |
3775 | * from there. We have to do this otherwise we'll miss the ordered | |
3776 | * extents and that results in badness. Please Josef, do not think you | |
3777 | * know better and pull this out at some point in the future, it is | |
3778 | * right and you are wrong. | |
3779 | */ | |
3780 | ret = filemap_fdatawrite_range(inode->i_mapping, start, end); | |
3781 | if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
3782 | &BTRFS_I(inode)->runtime_flags)) | |
3783 | ret = filemap_fdatawrite_range(inode->i_mapping, start, end); | |
3784 | ||
3785 | return ret; | |
3786 | } |