]>
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 CM |
1309 | { |
1310 | int ret = 0; | |
1311 | ||
09cbfeaf | 1312 | if (((pos & (PAGE_SIZE - 1)) || force_uptodate) && |
b6316429 | 1313 | !PageUptodate(page)) { |
b1bf862e CM |
1314 | ret = btrfs_readpage(NULL, page); |
1315 | if (ret) | |
1316 | return ret; | |
1317 | lock_page(page); | |
1318 | if (!PageUptodate(page)) { | |
1319 | unlock_page(page); | |
1320 | return -EIO; | |
1321 | } | |
e0467866 QW |
1322 | |
1323 | /* | |
1324 | * Since btrfs_readpage() will unlock the page before it | |
7c11d0ae QW |
1325 | * returns, there is a window where btrfs_releasepage() can be |
1326 | * called to release the page. Here we check both inode | |
1327 | * mapping and PagePrivate() to make sure the page was not | |
1328 | * released. | |
e0467866 QW |
1329 | * |
1330 | * The private flag check is essential for subpage as we need | |
1331 | * to store extra bitmap using page->private. | |
1332 | */ | |
1333 | if (page->mapping != inode->i_mapping || !PagePrivate(page)) { | |
bb1591b4 CM |
1334 | unlock_page(page); |
1335 | return -EAGAIN; | |
1336 | } | |
b1bf862e CM |
1337 | } |
1338 | return 0; | |
1339 | } | |
1340 | ||
39279cc3 | 1341 | /* |
376cc685 | 1342 | * this just gets pages into the page cache and locks them down. |
39279cc3 | 1343 | */ |
b37392ea MX |
1344 | static noinline int prepare_pages(struct inode *inode, struct page **pages, |
1345 | size_t num_pages, loff_t pos, | |
1346 | size_t write_bytes, bool force_uptodate) | |
39279cc3 CM |
1347 | { |
1348 | int i; | |
09cbfeaf | 1349 | unsigned long index = pos >> PAGE_SHIFT; |
3b16a4e3 | 1350 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
fc28b62d | 1351 | int err = 0; |
376cc685 | 1352 | int faili; |
8c2383c3 | 1353 | |
39279cc3 | 1354 | for (i = 0; i < num_pages; i++) { |
bb1591b4 | 1355 | again: |
a94733d0 | 1356 | pages[i] = find_or_create_page(inode->i_mapping, index + i, |
e3a41a5b | 1357 | mask | __GFP_WRITE); |
39279cc3 | 1358 | if (!pages[i]) { |
b1bf862e CM |
1359 | faili = i - 1; |
1360 | err = -ENOMEM; | |
1361 | goto fail; | |
1362 | } | |
1363 | ||
32443de3 QW |
1364 | err = set_page_extent_mapped(pages[i]); |
1365 | if (err < 0) { | |
1366 | faili = i; | |
1367 | goto fail; | |
1368 | } | |
1369 | ||
b1bf862e | 1370 | if (i == 0) |
bb1591b4 | 1371 | err = prepare_uptodate_page(inode, pages[i], pos, |
b6316429 | 1372 | force_uptodate); |
bb1591b4 CM |
1373 | if (!err && i == num_pages - 1) |
1374 | err = prepare_uptodate_page(inode, pages[i], | |
b6316429 | 1375 | pos + write_bytes, false); |
b1bf862e | 1376 | if (err) { |
09cbfeaf | 1377 | put_page(pages[i]); |
bb1591b4 CM |
1378 | if (err == -EAGAIN) { |
1379 | err = 0; | |
1380 | goto again; | |
1381 | } | |
b1bf862e CM |
1382 | faili = i - 1; |
1383 | goto fail; | |
39279cc3 | 1384 | } |
ccd467d6 | 1385 | wait_on_page_writeback(pages[i]); |
39279cc3 | 1386 | } |
376cc685 MX |
1387 | |
1388 | return 0; | |
1389 | fail: | |
1390 | while (faili >= 0) { | |
1391 | unlock_page(pages[faili]); | |
09cbfeaf | 1392 | put_page(pages[faili]); |
376cc685 MX |
1393 | faili--; |
1394 | } | |
1395 | return err; | |
1396 | ||
1397 | } | |
1398 | ||
1399 | /* | |
1400 | * This function locks the extent and properly waits for data=ordered extents | |
1401 | * to finish before allowing the pages to be modified if need. | |
1402 | * | |
1403 | * The return value: | |
1404 | * 1 - the extent is locked | |
1405 | * 0 - the extent is not locked, and everything is OK | |
1406 | * -EAGAIN - need re-prepare the pages | |
1407 | * the other < 0 number - Something wrong happens | |
1408 | */ | |
1409 | static noinline int | |
2cff578c | 1410 | lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct page **pages, |
376cc685 | 1411 | size_t num_pages, loff_t pos, |
2e78c927 | 1412 | size_t write_bytes, |
376cc685 MX |
1413 | u64 *lockstart, u64 *lockend, |
1414 | struct extent_state **cached_state) | |
1415 | { | |
3ffbd68c | 1416 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
376cc685 MX |
1417 | u64 start_pos; |
1418 | u64 last_pos; | |
1419 | int i; | |
1420 | int ret = 0; | |
1421 | ||
0b246afa | 1422 | start_pos = round_down(pos, fs_info->sectorsize); |
e21139c6 | 1423 | last_pos = round_up(pos + write_bytes, fs_info->sectorsize) - 1; |
376cc685 | 1424 | |
e3b8a485 | 1425 | if (start_pos < inode->vfs_inode.i_size) { |
e6dcd2dc | 1426 | struct btrfs_ordered_extent *ordered; |
a7e3b975 | 1427 | |
2cff578c NB |
1428 | lock_extent_bits(&inode->io_tree, start_pos, last_pos, |
1429 | cached_state); | |
b88935bf MX |
1430 | ordered = btrfs_lookup_ordered_range(inode, start_pos, |
1431 | last_pos - start_pos + 1); | |
e6dcd2dc | 1432 | if (ordered && |
bffe633e | 1433 | ordered->file_offset + ordered->num_bytes > start_pos && |
376cc685 | 1434 | ordered->file_offset <= last_pos) { |
2cff578c | 1435 | unlock_extent_cached(&inode->io_tree, start_pos, |
e43bbe5e | 1436 | last_pos, cached_state); |
e6dcd2dc CM |
1437 | for (i = 0; i < num_pages; i++) { |
1438 | unlock_page(pages[i]); | |
09cbfeaf | 1439 | put_page(pages[i]); |
e6dcd2dc | 1440 | } |
c0a43603 | 1441 | btrfs_start_ordered_extent(ordered, 1); |
b88935bf MX |
1442 | btrfs_put_ordered_extent(ordered); |
1443 | return -EAGAIN; | |
e6dcd2dc CM |
1444 | } |
1445 | if (ordered) | |
1446 | btrfs_put_ordered_extent(ordered); | |
7703bdd8 | 1447 | |
376cc685 MX |
1448 | *lockstart = start_pos; |
1449 | *lockend = last_pos; | |
1450 | ret = 1; | |
0762704b | 1451 | } |
376cc685 | 1452 | |
7703bdd8 | 1453 | /* |
32443de3 QW |
1454 | * We should be called after prepare_pages() which should have locked |
1455 | * all pages in the range. | |
7703bdd8 | 1456 | */ |
32443de3 | 1457 | for (i = 0; i < num_pages; i++) |
e6dcd2dc | 1458 | WARN_ON(!PageLocked(pages[i])); |
b1bf862e | 1459 | |
376cc685 | 1460 | return ret; |
39279cc3 CM |
1461 | } |
1462 | ||
38d37aa9 QW |
1463 | static int check_can_nocow(struct btrfs_inode *inode, loff_t pos, |
1464 | size_t *write_bytes, bool nowait) | |
7ee9e440 | 1465 | { |
3ffbd68c | 1466 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
85b7ab67 | 1467 | struct btrfs_root *root = inode->root; |
7ee9e440 JB |
1468 | u64 lockstart, lockend; |
1469 | u64 num_bytes; | |
1470 | int ret; | |
1471 | ||
38d37aa9 QW |
1472 | if (!(inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC))) |
1473 | return 0; | |
1474 | ||
5dbb75ed | 1475 | if (!nowait && !btrfs_drew_try_write_lock(&root->snapshot_lock)) |
5f791ec3 | 1476 | return -EAGAIN; |
8257b2dc | 1477 | |
0b246afa | 1478 | lockstart = round_down(pos, fs_info->sectorsize); |
da17066c | 1479 | lockend = round_up(pos + *write_bytes, |
0b246afa | 1480 | fs_info->sectorsize) - 1; |
5dbb75ed | 1481 | num_bytes = lockend - lockstart + 1; |
7ee9e440 | 1482 | |
5dbb75ed FM |
1483 | if (nowait) { |
1484 | struct btrfs_ordered_extent *ordered; | |
1485 | ||
1486 | if (!try_lock_extent(&inode->io_tree, lockstart, lockend)) | |
1487 | return -EAGAIN; | |
1488 | ||
1489 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | |
1490 | num_bytes); | |
1491 | if (ordered) { | |
1492 | btrfs_put_ordered_extent(ordered); | |
1493 | ret = -EAGAIN; | |
1494 | goto out_unlock; | |
1495 | } | |
1496 | } else { | |
1497 | btrfs_lock_and_flush_ordered_range(inode, lockstart, | |
1498 | lockend, NULL); | |
1499 | } | |
7ee9e440 | 1500 | |
85b7ab67 | 1501 | ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes, |
a84d5d42 | 1502 | NULL, NULL, NULL, false); |
7ee9e440 JB |
1503 | if (ret <= 0) { |
1504 | ret = 0; | |
5dbb75ed FM |
1505 | if (!nowait) |
1506 | btrfs_drew_write_unlock(&root->snapshot_lock); | |
7ee9e440 | 1507 | } else { |
c933956d MX |
1508 | *write_bytes = min_t(size_t, *write_bytes , |
1509 | num_bytes - pos + lockstart); | |
7ee9e440 | 1510 | } |
5dbb75ed | 1511 | out_unlock: |
85b7ab67 | 1512 | unlock_extent(&inode->io_tree, lockstart, lockend); |
7ee9e440 JB |
1513 | |
1514 | return ret; | |
1515 | } | |
1516 | ||
38d37aa9 QW |
1517 | static int check_nocow_nolock(struct btrfs_inode *inode, loff_t pos, |
1518 | size_t *write_bytes) | |
1519 | { | |
1520 | return check_can_nocow(inode, pos, write_bytes, true); | |
1521 | } | |
1522 | ||
1523 | /* | |
1524 | * Check if we can do nocow write into the range [@pos, @pos + @write_bytes) | |
1525 | * | |
1526 | * @pos: File offset | |
1527 | * @write_bytes: The length to write, will be updated to the nocow writeable | |
1528 | * range | |
1529 | * | |
1530 | * This function will flush ordered extents in the range to ensure proper | |
1531 | * nocow checks. | |
1532 | * | |
1533 | * Return: | |
1534 | * >0 and update @write_bytes if we can do nocow write | |
1535 | * 0 if we can't do nocow write | |
1536 | * -EAGAIN if we can't get the needed lock or there are ordered extents | |
1537 | * for * (nowait == true) case | |
1538 | * <0 if other error happened | |
1539 | * | |
1540 | * NOTE: Callers need to release the lock by btrfs_check_nocow_unlock(). | |
1541 | */ | |
1542 | int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos, | |
1543 | size_t *write_bytes) | |
1544 | { | |
1545 | return check_can_nocow(inode, pos, write_bytes, false); | |
1546 | } | |
1547 | ||
1548 | void btrfs_check_nocow_unlock(struct btrfs_inode *inode) | |
1549 | { | |
1550 | btrfs_drew_write_unlock(&inode->root->snapshot_lock); | |
1551 | } | |
1552 | ||
b8d8e1fd GR |
1553 | static void update_time_for_write(struct inode *inode) |
1554 | { | |
1555 | struct timespec64 now; | |
1556 | ||
1557 | if (IS_NOCMTIME(inode)) | |
1558 | return; | |
1559 | ||
1560 | now = current_time(inode); | |
1561 | if (!timespec64_equal(&inode->i_mtime, &now)) | |
1562 | inode->i_mtime = now; | |
1563 | ||
1564 | if (!timespec64_equal(&inode->i_ctime, &now)) | |
1565 | inode->i_ctime = now; | |
1566 | ||
1567 | if (IS_I_VERSION(inode)) | |
1568 | inode_inc_iversion(inode); | |
1569 | } | |
1570 | ||
1571 | static int btrfs_write_check(struct kiocb *iocb, struct iov_iter *from, | |
1572 | size_t count) | |
1573 | { | |
1574 | struct file *file = iocb->ki_filp; | |
1575 | struct inode *inode = file_inode(file); | |
1576 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
1577 | loff_t pos = iocb->ki_pos; | |
1578 | int ret; | |
1579 | loff_t oldsize; | |
1580 | loff_t start_pos; | |
1581 | ||
1582 | if (iocb->ki_flags & IOCB_NOWAIT) { | |
1583 | size_t nocow_bytes = count; | |
1584 | ||
1585 | /* We will allocate space in case nodatacow is not set, so bail */ | |
1586 | if (check_nocow_nolock(BTRFS_I(inode), pos, &nocow_bytes) <= 0) | |
1587 | return -EAGAIN; | |
1588 | /* | |
1589 | * There are holes in the range or parts of the range that must | |
1590 | * be COWed (shared extents, RO block groups, etc), so just bail | |
1591 | * out. | |
1592 | */ | |
1593 | if (nocow_bytes < count) | |
1594 | return -EAGAIN; | |
1595 | } | |
1596 | ||
1597 | current->backing_dev_info = inode_to_bdi(inode); | |
1598 | ret = file_remove_privs(file); | |
1599 | if (ret) | |
1600 | return ret; | |
1601 | ||
1602 | /* | |
1603 | * We reserve space for updating the inode when we reserve space for the | |
1604 | * extent we are going to write, so we will enospc out there. We don't | |
1605 | * need to start yet another transaction to update the inode as we will | |
1606 | * update the inode when we finish writing whatever data we write. | |
1607 | */ | |
1608 | update_time_for_write(inode); | |
1609 | ||
1610 | start_pos = round_down(pos, fs_info->sectorsize); | |
1611 | oldsize = i_size_read(inode); | |
1612 | if (start_pos > oldsize) { | |
1613 | /* Expand hole size to cover write data, preventing empty gap */ | |
1614 | loff_t end_pos = round_up(pos + count, fs_info->sectorsize); | |
1615 | ||
b06359a3 | 1616 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, end_pos); |
b8d8e1fd GR |
1617 | if (ret) { |
1618 | current->backing_dev_info = NULL; | |
1619 | return ret; | |
1620 | } | |
1621 | } | |
1622 | ||
1623 | return 0; | |
1624 | } | |
1625 | ||
e4af400a GR |
1626 | static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb, |
1627 | struct iov_iter *i) | |
4b46fce2 | 1628 | { |
e4af400a | 1629 | struct file *file = iocb->ki_filp; |
c3523706 | 1630 | loff_t pos; |
496ad9aa | 1631 | struct inode *inode = file_inode(file); |
0b246afa | 1632 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
11c65dcc | 1633 | struct page **pages = NULL; |
364ecf36 | 1634 | struct extent_changeset *data_reserved = NULL; |
7ee9e440 | 1635 | u64 release_bytes = 0; |
376cc685 MX |
1636 | u64 lockstart; |
1637 | u64 lockend; | |
d0215f3e JB |
1638 | size_t num_written = 0; |
1639 | int nrptrs; | |
c3523706 | 1640 | ssize_t ret; |
7ee9e440 | 1641 | bool only_release_metadata = false; |
b6316429 | 1642 | bool force_page_uptodate = false; |
5e8b9ef3 | 1643 | loff_t old_isize = i_size_read(inode); |
c3523706 GR |
1644 | unsigned int ilock_flags = 0; |
1645 | ||
1646 | if (iocb->ki_flags & IOCB_NOWAIT) | |
1647 | ilock_flags |= BTRFS_ILOCK_TRY; | |
1648 | ||
1649 | ret = btrfs_inode_lock(inode, ilock_flags); | |
1650 | if (ret < 0) | |
1651 | return ret; | |
4b46fce2 | 1652 | |
c3523706 GR |
1653 | ret = generic_write_checks(iocb, i); |
1654 | if (ret <= 0) | |
1655 | goto out; | |
1656 | ||
1657 | ret = btrfs_write_check(iocb, i, ret); | |
1658 | if (ret < 0) | |
1659 | goto out; | |
1660 | ||
1661 | pos = iocb->ki_pos; | |
09cbfeaf KS |
1662 | nrptrs = min(DIV_ROUND_UP(iov_iter_count(i), PAGE_SIZE), |
1663 | PAGE_SIZE / (sizeof(struct page *))); | |
142349f5 WF |
1664 | nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied); |
1665 | nrptrs = max(nrptrs, 8); | |
31e818fe | 1666 | pages = kmalloc_array(nrptrs, sizeof(struct page *), GFP_KERNEL); |
c3523706 GR |
1667 | if (!pages) { |
1668 | ret = -ENOMEM; | |
1669 | goto out; | |
1670 | } | |
ab93dbec | 1671 | |
d0215f3e | 1672 | while (iov_iter_count(i) > 0) { |
c67d970f | 1673 | struct extent_state *cached_state = NULL; |
7073017a | 1674 | size_t offset = offset_in_page(pos); |
2e78c927 | 1675 | size_t sector_offset; |
d0215f3e | 1676 | size_t write_bytes = min(iov_iter_count(i), |
09cbfeaf | 1677 | nrptrs * (size_t)PAGE_SIZE - |
8c2383c3 | 1678 | offset); |
eefa45f5 | 1679 | size_t num_pages; |
7ee9e440 | 1680 | size_t reserve_bytes; |
d0215f3e JB |
1681 | size_t dirty_pages; |
1682 | size_t copied; | |
2e78c927 CR |
1683 | size_t dirty_sectors; |
1684 | size_t num_sectors; | |
79f015f2 | 1685 | int extents_locked; |
39279cc3 | 1686 | |
914ee295 XZ |
1687 | /* |
1688 | * Fault pages before locking them in prepare_pages | |
1689 | * to avoid recursive lock | |
1690 | */ | |
a6294593 | 1691 | if (unlikely(fault_in_iov_iter_readable(i, write_bytes))) { |
914ee295 | 1692 | ret = -EFAULT; |
d0215f3e | 1693 | break; |
914ee295 XZ |
1694 | } |
1695 | ||
a0e248bb | 1696 | only_release_metadata = false; |
da17066c | 1697 | sector_offset = pos & (fs_info->sectorsize - 1); |
d9d8b2a5 | 1698 | |
364ecf36 | 1699 | extent_changeset_release(data_reserved); |
36ea6f3e NB |
1700 | ret = btrfs_check_data_free_space(BTRFS_I(inode), |
1701 | &data_reserved, pos, | |
364ecf36 | 1702 | write_bytes); |
c6887cd1 | 1703 | if (ret < 0) { |
eefa45f5 GR |
1704 | /* |
1705 | * If we don't have to COW at the offset, reserve | |
1706 | * metadata only. write_bytes may get smaller than | |
1707 | * requested here. | |
1708 | */ | |
38d37aa9 | 1709 | if (btrfs_check_nocow_lock(BTRFS_I(inode), pos, |
eefa45f5 | 1710 | &write_bytes) > 0) |
c6887cd1 | 1711 | only_release_metadata = true; |
eefa45f5 | 1712 | else |
c6887cd1 | 1713 | break; |
c6887cd1 | 1714 | } |
1832a6d5 | 1715 | |
eefa45f5 GR |
1716 | num_pages = DIV_ROUND_UP(write_bytes + offset, PAGE_SIZE); |
1717 | WARN_ON(num_pages > nrptrs); | |
1718 | reserve_bytes = round_up(write_bytes + sector_offset, | |
1719 | fs_info->sectorsize); | |
8b62f87b | 1720 | WARN_ON(reserve_bytes == 0); |
9f3db423 | 1721 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), |
28c9b1e7 OS |
1722 | reserve_bytes, |
1723 | reserve_bytes); | |
7ee9e440 JB |
1724 | if (ret) { |
1725 | if (!only_release_metadata) | |
25ce28ca | 1726 | btrfs_free_reserved_data_space(BTRFS_I(inode), |
bc42bda2 QW |
1727 | data_reserved, pos, |
1728 | write_bytes); | |
8257b2dc | 1729 | else |
38d37aa9 | 1730 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
7ee9e440 JB |
1731 | break; |
1732 | } | |
1733 | ||
1734 | release_bytes = reserve_bytes; | |
376cc685 | 1735 | again: |
4a64001f JB |
1736 | /* |
1737 | * This is going to setup the pages array with the number of | |
1738 | * pages we want, so we don't really need to worry about the | |
1739 | * contents of pages from loop to loop | |
1740 | */ | |
b37392ea MX |
1741 | ret = prepare_pages(inode, pages, num_pages, |
1742 | pos, write_bytes, | |
b6316429 | 1743 | force_page_uptodate); |
8b62f87b JB |
1744 | if (ret) { |
1745 | btrfs_delalloc_release_extents(BTRFS_I(inode), | |
8702ba93 | 1746 | reserve_bytes); |
d0215f3e | 1747 | break; |
8b62f87b | 1748 | } |
39279cc3 | 1749 | |
79f015f2 GR |
1750 | extents_locked = lock_and_cleanup_extent_if_need( |
1751 | BTRFS_I(inode), pages, | |
2cff578c NB |
1752 | num_pages, pos, write_bytes, &lockstart, |
1753 | &lockend, &cached_state); | |
79f015f2 GR |
1754 | if (extents_locked < 0) { |
1755 | if (extents_locked == -EAGAIN) | |
376cc685 | 1756 | goto again; |
8b62f87b | 1757 | btrfs_delalloc_release_extents(BTRFS_I(inode), |
8702ba93 | 1758 | reserve_bytes); |
79f015f2 | 1759 | ret = extents_locked; |
376cc685 | 1760 | break; |
376cc685 MX |
1761 | } |
1762 | ||
ee22f0c4 | 1763 | copied = btrfs_copy_from_user(pos, write_bytes, pages, i); |
b1bf862e | 1764 | |
0b246afa | 1765 | num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes); |
56244ef1 | 1766 | dirty_sectors = round_up(copied + sector_offset, |
0b246afa JM |
1767 | fs_info->sectorsize); |
1768 | dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors); | |
56244ef1 | 1769 | |
b1bf862e CM |
1770 | /* |
1771 | * if we have trouble faulting in the pages, fall | |
1772 | * back to one page at a time | |
1773 | */ | |
1774 | if (copied < write_bytes) | |
1775 | nrptrs = 1; | |
1776 | ||
b6316429 JB |
1777 | if (copied == 0) { |
1778 | force_page_uptodate = true; | |
56244ef1 | 1779 | dirty_sectors = 0; |
b1bf862e | 1780 | dirty_pages = 0; |
b6316429 JB |
1781 | } else { |
1782 | force_page_uptodate = false; | |
ed6078f7 | 1783 | dirty_pages = DIV_ROUND_UP(copied + offset, |
09cbfeaf | 1784 | PAGE_SIZE); |
b6316429 | 1785 | } |
914ee295 | 1786 | |
2e78c927 | 1787 | if (num_sectors > dirty_sectors) { |
8b8b08cb | 1788 | /* release everything except the sectors we dirtied */ |
265fdfa6 | 1789 | release_bytes -= dirty_sectors << fs_info->sectorsize_bits; |
485290a7 | 1790 | if (only_release_metadata) { |
691fa059 | 1791 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
43b18595 | 1792 | release_bytes, true); |
485290a7 QW |
1793 | } else { |
1794 | u64 __pos; | |
1795 | ||
da17066c | 1796 | __pos = round_down(pos, |
0b246afa | 1797 | fs_info->sectorsize) + |
09cbfeaf | 1798 | (dirty_pages << PAGE_SHIFT); |
86d52921 | 1799 | btrfs_delalloc_release_space(BTRFS_I(inode), |
bc42bda2 | 1800 | data_reserved, __pos, |
43b18595 | 1801 | release_bytes, true); |
485290a7 | 1802 | } |
914ee295 XZ |
1803 | } |
1804 | ||
2e78c927 | 1805 | release_bytes = round_up(copied + sector_offset, |
0b246afa | 1806 | fs_info->sectorsize); |
376cc685 | 1807 | |
aa8c1a41 GR |
1808 | ret = btrfs_dirty_pages(BTRFS_I(inode), pages, |
1809 | dirty_pages, pos, copied, | |
1810 | &cached_state, only_release_metadata); | |
c67d970f FM |
1811 | |
1812 | /* | |
1813 | * If we have not locked the extent range, because the range's | |
1814 | * start offset is >= i_size, we might still have a non-NULL | |
1815 | * cached extent state, acquired while marking the extent range | |
1816 | * as delalloc through btrfs_dirty_pages(). Therefore free any | |
1817 | * possible cached extent state to avoid a memory leak. | |
1818 | */ | |
79f015f2 | 1819 | if (extents_locked) |
376cc685 | 1820 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
e43bbe5e | 1821 | lockstart, lockend, &cached_state); |
c67d970f FM |
1822 | else |
1823 | free_extent_state(cached_state); | |
1824 | ||
8702ba93 | 1825 | btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes); |
f1de9683 | 1826 | if (ret) { |
e4f94347 | 1827 | btrfs_drop_pages(fs_info, pages, num_pages, pos, copied); |
376cc685 | 1828 | break; |
f1de9683 | 1829 | } |
39279cc3 | 1830 | |
376cc685 | 1831 | release_bytes = 0; |
8257b2dc | 1832 | if (only_release_metadata) |
38d37aa9 | 1833 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
8257b2dc | 1834 | |
e4f94347 | 1835 | btrfs_drop_pages(fs_info, pages, num_pages, pos, copied); |
f1de9683 | 1836 | |
d0215f3e JB |
1837 | cond_resched(); |
1838 | ||
d0e1d66b | 1839 | balance_dirty_pages_ratelimited(inode->i_mapping); |
cb843a6f | 1840 | |
914ee295 XZ |
1841 | pos += copied; |
1842 | num_written += copied; | |
d0215f3e | 1843 | } |
39279cc3 | 1844 | |
d0215f3e JB |
1845 | kfree(pages); |
1846 | ||
7ee9e440 | 1847 | if (release_bytes) { |
8257b2dc | 1848 | if (only_release_metadata) { |
38d37aa9 | 1849 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
691fa059 | 1850 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
43b18595 | 1851 | release_bytes, true); |
8257b2dc | 1852 | } else { |
86d52921 NB |
1853 | btrfs_delalloc_release_space(BTRFS_I(inode), |
1854 | data_reserved, | |
bc42bda2 | 1855 | round_down(pos, fs_info->sectorsize), |
43b18595 | 1856 | release_bytes, true); |
8257b2dc | 1857 | } |
7ee9e440 JB |
1858 | } |
1859 | ||
364ecf36 | 1860 | extent_changeset_free(data_reserved); |
5e8b9ef3 GR |
1861 | if (num_written > 0) { |
1862 | pagecache_isize_extended(inode, old_isize, iocb->ki_pos); | |
1863 | iocb->ki_pos += num_written; | |
1864 | } | |
c3523706 GR |
1865 | out: |
1866 | btrfs_inode_unlock(inode, ilock_flags); | |
d0215f3e JB |
1867 | return num_written ? num_written : ret; |
1868 | } | |
1869 | ||
4e4cabec GR |
1870 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
1871 | const struct iov_iter *iter, loff_t offset) | |
1872 | { | |
1873 | const u32 blocksize_mask = fs_info->sectorsize - 1; | |
1874 | ||
1875 | if (offset & blocksize_mask) | |
1876 | return -EINVAL; | |
1877 | ||
1878 | if (iov_iter_alignment(iter) & blocksize_mask) | |
1879 | return -EINVAL; | |
1880 | ||
1881 | return 0; | |
1882 | } | |
1883 | ||
1884 | static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) | |
d0215f3e | 1885 | { |
51bd9563 | 1886 | const bool is_sync_write = (iocb->ki_flags & IOCB_DSYNC); |
d0215f3e | 1887 | struct file *file = iocb->ki_filp; |
728404da | 1888 | struct inode *inode = file_inode(file); |
4e4cabec | 1889 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
c3523706 | 1890 | loff_t pos; |
4e4cabec | 1891 | ssize_t written = 0; |
d0215f3e | 1892 | ssize_t written_buffered; |
51bd9563 | 1893 | size_t prev_left = 0; |
d0215f3e | 1894 | loff_t endbyte; |
c3523706 GR |
1895 | ssize_t err; |
1896 | unsigned int ilock_flags = 0; | |
1897 | ||
1898 | if (iocb->ki_flags & IOCB_NOWAIT) | |
1899 | ilock_flags |= BTRFS_ILOCK_TRY; | |
1900 | ||
e9adabb9 GR |
1901 | /* If the write DIO is within EOF, use a shared lock */ |
1902 | if (iocb->ki_pos + iov_iter_count(from) <= i_size_read(inode)) | |
1903 | ilock_flags |= BTRFS_ILOCK_SHARED; | |
1904 | ||
1905 | relock: | |
c3523706 GR |
1906 | err = btrfs_inode_lock(inode, ilock_flags); |
1907 | if (err < 0) | |
1908 | return err; | |
1909 | ||
1910 | err = generic_write_checks(iocb, from); | |
1911 | if (err <= 0) { | |
1912 | btrfs_inode_unlock(inode, ilock_flags); | |
1913 | return err; | |
1914 | } | |
d0215f3e | 1915 | |
c3523706 GR |
1916 | err = btrfs_write_check(iocb, from, err); |
1917 | if (err < 0) { | |
1918 | btrfs_inode_unlock(inode, ilock_flags); | |
1919 | goto out; | |
1920 | } | |
1921 | ||
1922 | pos = iocb->ki_pos; | |
e9adabb9 GR |
1923 | /* |
1924 | * Re-check since file size may have changed just before taking the | |
1925 | * lock or pos may have changed because of O_APPEND in generic_write_check() | |
1926 | */ | |
1927 | if ((ilock_flags & BTRFS_ILOCK_SHARED) && | |
1928 | pos + iov_iter_count(from) > i_size_read(inode)) { | |
1929 | btrfs_inode_unlock(inode, ilock_flags); | |
1930 | ilock_flags &= ~BTRFS_ILOCK_SHARED; | |
1931 | goto relock; | |
1932 | } | |
c3523706 GR |
1933 | |
1934 | if (check_direct_IO(fs_info, from, pos)) { | |
1935 | btrfs_inode_unlock(inode, ilock_flags); | |
4e4cabec | 1936 | goto buffered; |
c3523706 | 1937 | } |
4e4cabec | 1938 | |
51bd9563 FM |
1939 | /* |
1940 | * We remove IOCB_DSYNC so that we don't deadlock when iomap_dio_rw() | |
1941 | * calls generic_write_sync() (through iomap_dio_complete()), because | |
1942 | * that results in calling fsync (btrfs_sync_file()) which will try to | |
1943 | * lock the inode in exclusive/write mode. | |
1944 | */ | |
1945 | if (is_sync_write) | |
1946 | iocb->ki_flags &= ~IOCB_DSYNC; | |
4e4cabec | 1947 | |
51bd9563 FM |
1948 | /* |
1949 | * The iov_iter can be mapped to the same file range we are writing to. | |
1950 | * If that's the case, then we will deadlock in the iomap code, because | |
1951 | * it first calls our callback btrfs_dio_iomap_begin(), which will create | |
1952 | * an ordered extent, and after that it will fault in the pages that the | |
1953 | * iov_iter refers to. During the fault in we end up in the readahead | |
1954 | * pages code (starting at btrfs_readahead()), which will lock the range, | |
1955 | * find that ordered extent and then wait for it to complete (at | |
1956 | * btrfs_lock_and_flush_ordered_range()), resulting in a deadlock since | |
1957 | * obviously the ordered extent can never complete as we didn't submit | |
1958 | * yet the respective bio(s). This always happens when the buffer is | |
1959 | * memory mapped to the same file range, since the iomap DIO code always | |
1960 | * invalidates pages in the target file range (after starting and waiting | |
1961 | * for any writeback). | |
1962 | * | |
1963 | * So here we disable page faults in the iov_iter and then retry if we | |
1964 | * got -EFAULT, faulting in the pages before the retry. | |
1965 | */ | |
1966 | again: | |
1967 | from->nofault = true; | |
1968 | err = iomap_dio_rw(iocb, from, &btrfs_dio_iomap_ops, &btrfs_dio_ops, | |
1969 | IOMAP_DIO_PARTIAL, written); | |
1970 | from->nofault = false; | |
d0215f3e | 1971 | |
51bd9563 FM |
1972 | /* No increment (+=) because iomap returns a cumulative value. */ |
1973 | if (err > 0) | |
1974 | written = err; | |
1975 | ||
1976 | if (iov_iter_count(from) > 0 && (err == -EFAULT || err > 0)) { | |
1977 | const size_t left = iov_iter_count(from); | |
1978 | /* | |
1979 | * We have more data left to write. Try to fault in as many as | |
1980 | * possible of the remainder pages and retry. We do this without | |
1981 | * releasing and locking again the inode, to prevent races with | |
1982 | * truncate. | |
1983 | * | |
1984 | * Also, in case the iov refers to pages in the file range of the | |
1985 | * file we want to write to (due to a mmap), we could enter an | |
1986 | * infinite loop if we retry after faulting the pages in, since | |
1987 | * iomap will invalidate any pages in the range early on, before | |
1988 | * it tries to fault in the pages of the iov. So we keep track of | |
1989 | * how much was left of iov in the previous EFAULT and fallback | |
1990 | * to buffered IO in case we haven't made any progress. | |
1991 | */ | |
1992 | if (left == prev_left) { | |
1993 | err = -ENOTBLK; | |
1994 | } else { | |
1995 | fault_in_iov_iter_readable(from, left); | |
1996 | prev_left = left; | |
1997 | goto again; | |
1998 | } | |
a42fa643 GR |
1999 | } |
2000 | ||
51bd9563 FM |
2001 | btrfs_inode_unlock(inode, ilock_flags); |
2002 | ||
2003 | /* | |
2004 | * Add back IOCB_DSYNC. Our caller, btrfs_file_write_iter(), will do | |
2005 | * the fsync (call generic_write_sync()). | |
2006 | */ | |
2007 | if (is_sync_write) | |
2008 | iocb->ki_flags |= IOCB_DSYNC; | |
2009 | ||
2010 | /* If 'err' is -ENOTBLK then it means we must fallback to buffered IO. */ | |
2011 | if ((err < 0 && err != -ENOTBLK) || !iov_iter_count(from)) | |
c3523706 | 2012 | goto out; |
d0215f3e | 2013 | |
4e4cabec | 2014 | buffered: |
e4af400a GR |
2015 | pos = iocb->ki_pos; |
2016 | written_buffered = btrfs_buffered_write(iocb, from); | |
d0215f3e JB |
2017 | if (written_buffered < 0) { |
2018 | err = written_buffered; | |
2019 | goto out; | |
39279cc3 | 2020 | } |
075bdbdb FM |
2021 | /* |
2022 | * Ensure all data is persisted. We want the next direct IO read to be | |
2023 | * able to read what was just written. | |
2024 | */ | |
d0215f3e | 2025 | endbyte = pos + written_buffered - 1; |
728404da | 2026 | err = btrfs_fdatawrite_range(inode, pos, endbyte); |
075bdbdb FM |
2027 | if (err) |
2028 | goto out; | |
728404da | 2029 | err = filemap_fdatawait_range(inode->i_mapping, pos, endbyte); |
d0215f3e JB |
2030 | if (err) |
2031 | goto out; | |
2032 | written += written_buffered; | |
867c4f93 | 2033 | iocb->ki_pos = pos + written_buffered; |
09cbfeaf KS |
2034 | invalidate_mapping_pages(file->f_mapping, pos >> PAGE_SHIFT, |
2035 | endbyte >> PAGE_SHIFT); | |
39279cc3 | 2036 | out: |
51bd9563 | 2037 | return err < 0 ? err : written; |
d0215f3e | 2038 | } |
5b92ee72 | 2039 | |
7c0c7269 OS |
2040 | static ssize_t btrfs_encoded_write(struct kiocb *iocb, struct iov_iter *from, |
2041 | const struct btrfs_ioctl_encoded_io_args *encoded) | |
2042 | { | |
2043 | struct file *file = iocb->ki_filp; | |
2044 | struct inode *inode = file_inode(file); | |
2045 | loff_t count; | |
2046 | ssize_t ret; | |
2047 | ||
2048 | btrfs_inode_lock(inode, 0); | |
2049 | count = encoded->len; | |
2050 | ret = generic_write_checks_count(iocb, &count); | |
2051 | if (ret == 0 && count != encoded->len) { | |
2052 | /* | |
2053 | * The write got truncated by generic_write_checks_count(). We | |
2054 | * can't do a partial encoded write. | |
2055 | */ | |
2056 | ret = -EFBIG; | |
2057 | } | |
2058 | if (ret || encoded->len == 0) | |
2059 | goto out; | |
2060 | ||
2061 | ret = btrfs_write_check(iocb, from, encoded->len); | |
2062 | if (ret < 0) | |
2063 | goto out; | |
2064 | ||
2065 | ret = btrfs_do_encoded_write(iocb, from, encoded); | |
2066 | out: | |
2067 | btrfs_inode_unlock(inode, 0); | |
2068 | return ret; | |
2069 | } | |
2070 | ||
2071 | ssize_t btrfs_do_write_iter(struct kiocb *iocb, struct iov_iter *from, | |
2072 | const struct btrfs_ioctl_encoded_io_args *encoded) | |
d0215f3e JB |
2073 | { |
2074 | struct file *file = iocb->ki_filp; | |
14971657 | 2075 | struct btrfs_inode *inode = BTRFS_I(file_inode(file)); |
7c0c7269 | 2076 | ssize_t num_written, num_sync; |
f50cb7af | 2077 | const bool sync = iocb->ki_flags & IOCB_DSYNC; |
d0215f3e | 2078 | |
c86537a4 GR |
2079 | /* |
2080 | * If the fs flips readonly due to some impossible error, although we | |
2081 | * have opened a file as writable, we have to stop this write operation | |
2082 | * to ensure consistency. | |
2083 | */ | |
84961539 | 2084 | if (BTRFS_FS_ERROR(inode->root->fs_info)) |
c86537a4 GR |
2085 | return -EROFS; |
2086 | ||
7c0c7269 | 2087 | if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT)) |
91f9943e CH |
2088 | return -EOPNOTSUPP; |
2089 | ||
b812ce28 | 2090 | if (sync) |
14971657 | 2091 | atomic_inc(&inode->sync_writers); |
b812ce28 | 2092 | |
7c0c7269 OS |
2093 | if (encoded) { |
2094 | num_written = btrfs_encoded_write(iocb, from, encoded); | |
2095 | num_sync = encoded->len; | |
2096 | } else if (iocb->ki_flags & IOCB_DIRECT) { | |
2097 | num_written = num_sync = btrfs_direct_write(iocb, from); | |
2098 | } else { | |
2099 | num_written = num_sync = btrfs_buffered_write(iocb, from); | |
2100 | } | |
d0215f3e | 2101 | |
bc0939fc FM |
2102 | btrfs_set_inode_last_sub_trans(inode); |
2103 | ||
7c0c7269 OS |
2104 | if (num_sync > 0) { |
2105 | num_sync = generic_write_sync(iocb, num_sync); | |
2106 | if (num_sync < 0) | |
2107 | num_written = num_sync; | |
2108 | } | |
0a3404dc | 2109 | |
b812ce28 | 2110 | if (sync) |
14971657 | 2111 | atomic_dec(&inode->sync_writers); |
b8d8e1fd | 2112 | |
39279cc3 | 2113 | current->backing_dev_info = NULL; |
c3523706 | 2114 | return num_written; |
39279cc3 CM |
2115 | } |
2116 | ||
7c0c7269 OS |
2117 | static ssize_t btrfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
2118 | { | |
2119 | return btrfs_do_write_iter(iocb, from, NULL); | |
2120 | } | |
2121 | ||
d397712b | 2122 | int btrfs_release_file(struct inode *inode, struct file *filp) |
e1b81e67 | 2123 | { |
23b5ec74 JB |
2124 | struct btrfs_file_private *private = filp->private_data; |
2125 | ||
23b5ec74 JB |
2126 | if (private && private->filldir_buf) |
2127 | kfree(private->filldir_buf); | |
2128 | kfree(private); | |
2129 | filp->private_data = NULL; | |
2130 | ||
f6dc45c7 | 2131 | /* |
1fd4033d NB |
2132 | * Set by setattr when we are about to truncate a file from a non-zero |
2133 | * size to a zero size. This tries to flush down new bytes that may | |
2134 | * have been written if the application were using truncate to replace | |
2135 | * a file in place. | |
f6dc45c7 | 2136 | */ |
1fd4033d | 2137 | if (test_and_clear_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
f6dc45c7 CM |
2138 | &BTRFS_I(inode)->runtime_flags)) |
2139 | filemap_flush(inode->i_mapping); | |
e1b81e67 M |
2140 | return 0; |
2141 | } | |
2142 | ||
669249ee FM |
2143 | static int start_ordered_ops(struct inode *inode, loff_t start, loff_t end) |
2144 | { | |
2145 | int ret; | |
343e4fc1 | 2146 | struct blk_plug plug; |
669249ee | 2147 | |
343e4fc1 LB |
2148 | /* |
2149 | * This is only called in fsync, which would do synchronous writes, so | |
2150 | * a plug can merge adjacent IOs as much as possible. Esp. in case of | |
2151 | * multiple disks using raid profile, a large IO can be split to | |
2152 | * several segments of stripe length (currently 64K). | |
2153 | */ | |
2154 | blk_start_plug(&plug); | |
669249ee | 2155 | atomic_inc(&BTRFS_I(inode)->sync_writers); |
728404da | 2156 | ret = btrfs_fdatawrite_range(inode, start, end); |
669249ee | 2157 | atomic_dec(&BTRFS_I(inode)->sync_writers); |
343e4fc1 | 2158 | blk_finish_plug(&plug); |
669249ee FM |
2159 | |
2160 | return ret; | |
2161 | } | |
2162 | ||
626e9f41 FM |
2163 | static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx) |
2164 | { | |
2165 | struct btrfs_inode *inode = BTRFS_I(ctx->inode); | |
2166 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
2167 | ||
2168 | if (btrfs_inode_in_log(inode, fs_info->generation) && | |
2169 | list_empty(&ctx->ordered_extents)) | |
2170 | return true; | |
2171 | ||
2172 | /* | |
2173 | * If we are doing a fast fsync we can not bail out if the inode's | |
2174 | * last_trans is <= then the last committed transaction, because we only | |
2175 | * update the last_trans of the inode during ordered extent completion, | |
2176 | * and for a fast fsync we don't wait for that, we only wait for the | |
2177 | * writeback to complete. | |
2178 | */ | |
2179 | if (inode->last_trans <= fs_info->last_trans_committed && | |
2180 | (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) || | |
2181 | list_empty(&ctx->ordered_extents))) | |
2182 | return true; | |
2183 | ||
2184 | return false; | |
2185 | } | |
2186 | ||
d352ac68 CM |
2187 | /* |
2188 | * fsync call for both files and directories. This logs the inode into | |
2189 | * the tree log instead of forcing full commits whenever possible. | |
2190 | * | |
2191 | * It needs to call filemap_fdatawait so that all ordered extent updates are | |
2192 | * in the metadata btree are up to date for copying to the log. | |
2193 | * | |
2194 | * It drops the inode mutex before doing the tree log commit. This is an | |
2195 | * important optimization for directories because holding the mutex prevents | |
2196 | * new operations on the dir while we write to disk. | |
2197 | */ | |
02c24a82 | 2198 | int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) |
39279cc3 | 2199 | { |
de17e793 | 2200 | struct dentry *dentry = file_dentry(file); |
2b0143b5 | 2201 | struct inode *inode = d_inode(dentry); |
0b246afa | 2202 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
39279cc3 | 2203 | struct btrfs_root *root = BTRFS_I(inode)->root; |
39279cc3 | 2204 | struct btrfs_trans_handle *trans; |
8b050d35 | 2205 | struct btrfs_log_ctx ctx; |
333427a5 | 2206 | int ret = 0, err; |
48778179 FM |
2207 | u64 len; |
2208 | bool full_sync; | |
39279cc3 | 2209 | |
1abe9b8a | 2210 | trace_btrfs_sync_file(file, datasync); |
257c62e1 | 2211 | |
ebb70442 LB |
2212 | btrfs_init_log_ctx(&ctx, inode); |
2213 | ||
95418ed1 | 2214 | /* |
48778179 FM |
2215 | * Always set the range to a full range, otherwise we can get into |
2216 | * several problems, from missing file extent items to represent holes | |
2217 | * when not using the NO_HOLES feature, to log tree corruption due to | |
2218 | * races between hole detection during logging and completion of ordered | |
2219 | * extents outside the range, to missing checksums due to ordered extents | |
2220 | * for which we flushed only a subset of their pages. | |
95418ed1 | 2221 | */ |
48778179 FM |
2222 | start = 0; |
2223 | end = LLONG_MAX; | |
2224 | len = (u64)LLONG_MAX + 1; | |
95418ed1 | 2225 | |
90abccf2 MX |
2226 | /* |
2227 | * We write the dirty pages in the range and wait until they complete | |
2228 | * out of the ->i_mutex. If so, we can flush the dirty pages by | |
2ab28f32 JB |
2229 | * multi-task, and make the performance up. See |
2230 | * btrfs_wait_ordered_range for an explanation of the ASYNC check. | |
90abccf2 | 2231 | */ |
669249ee | 2232 | ret = start_ordered_ops(inode, start, end); |
90abccf2 | 2233 | if (ret) |
333427a5 | 2234 | goto out; |
90abccf2 | 2235 | |
885f46d8 | 2236 | btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP); |
c495144b | 2237 | |
2ecb7923 | 2238 | atomic_inc(&root->log_batch); |
b5e6c3e1 | 2239 | |
7af59743 | 2240 | /* |
48778179 FM |
2241 | * Always check for the full sync flag while holding the inode's lock, |
2242 | * to avoid races with other tasks. The flag must be either set all the | |
2243 | * time during logging or always off all the time while logging. | |
7af59743 | 2244 | */ |
48778179 FM |
2245 | full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
2246 | &BTRFS_I(inode)->runtime_flags); | |
7af59743 | 2247 | |
aab15e8e | 2248 | /* |
885f46d8 FM |
2249 | * Before we acquired the inode's lock and the mmap lock, someone may |
2250 | * have dirtied more pages in the target range. We need to make sure | |
2251 | * that writeback for any such pages does not start while we are logging | |
2252 | * the inode, because if it does, any of the following might happen when | |
2253 | * we are not doing a full inode sync: | |
aab15e8e FM |
2254 | * |
2255 | * 1) We log an extent after its writeback finishes but before its | |
2256 | * checksums are added to the csum tree, leading to -EIO errors | |
2257 | * when attempting to read the extent after a log replay. | |
2258 | * | |
2259 | * 2) We can end up logging an extent before its writeback finishes. | |
2260 | * Therefore after the log replay we will have a file extent item | |
2261 | * pointing to an unwritten extent (and no data checksums as well). | |
2262 | * | |
2263 | * So trigger writeback for any eventual new dirty pages and then we | |
2264 | * wait for all ordered extents to complete below. | |
2265 | */ | |
2266 | ret = start_ordered_ops(inode, start, end); | |
2267 | if (ret) { | |
885f46d8 | 2268 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
aab15e8e FM |
2269 | goto out; |
2270 | } | |
2271 | ||
669249ee | 2272 | /* |
b5e6c3e1 | 2273 | * We have to do this here to avoid the priority inversion of waiting on |
52042d8e | 2274 | * IO of a lower priority task while holding a transaction open. |
ba0b084a | 2275 | * |
48778179 FM |
2276 | * For a full fsync we wait for the ordered extents to complete while |
2277 | * for a fast fsync we wait just for writeback to complete, and then | |
2278 | * attach the ordered extents to the transaction so that a transaction | |
2279 | * commit waits for their completion, to avoid data loss if we fsync, | |
2280 | * the current transaction commits before the ordered extents complete | |
2281 | * and a power failure happens right after that. | |
d8e3fb10 NA |
2282 | * |
2283 | * For zoned filesystem, if a write IO uses a ZONE_APPEND command, the | |
2284 | * logical address recorded in the ordered extent may change. We need | |
2285 | * to wait for the IO to stabilize the logical address. | |
669249ee | 2286 | */ |
d8e3fb10 | 2287 | if (full_sync || btrfs_is_zoned(fs_info)) { |
48778179 FM |
2288 | ret = btrfs_wait_ordered_range(inode, start, len); |
2289 | } else { | |
2290 | /* | |
2291 | * Get our ordered extents as soon as possible to avoid doing | |
2292 | * checksum lookups in the csum tree, and use instead the | |
2293 | * checksums attached to the ordered extents. | |
2294 | */ | |
2295 | btrfs_get_ordered_extents_for_logging(BTRFS_I(inode), | |
2296 | &ctx.ordered_extents); | |
2297 | ret = filemap_fdatawait_range(inode->i_mapping, start, end); | |
0ef8b726 | 2298 | } |
48778179 FM |
2299 | |
2300 | if (ret) | |
2301 | goto out_release_extents; | |
2302 | ||
2ecb7923 | 2303 | atomic_inc(&root->log_batch); |
257c62e1 | 2304 | |
a4abeea4 | 2305 | smp_mb(); |
626e9f41 | 2306 | if (skip_inode_logging(&ctx)) { |
5dc562c5 | 2307 | /* |
01327610 | 2308 | * We've had everything committed since the last time we were |
5dc562c5 JB |
2309 | * modified so clear this flag in case it was set for whatever |
2310 | * reason, it's no longer relevant. | |
2311 | */ | |
2312 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
2313 | &BTRFS_I(inode)->runtime_flags); | |
0596a904 FM |
2314 | /* |
2315 | * An ordered extent might have started before and completed | |
2316 | * already with io errors, in which case the inode was not | |
2317 | * updated and we end up here. So check the inode's mapping | |
333427a5 JL |
2318 | * for any errors that might have happened since we last |
2319 | * checked called fsync. | |
0596a904 | 2320 | */ |
333427a5 | 2321 | ret = filemap_check_wb_err(inode->i_mapping, file->f_wb_err); |
48778179 | 2322 | goto out_release_extents; |
15ee9bc7 | 2323 | } |
15ee9bc7 | 2324 | |
5039eddc JB |
2325 | /* |
2326 | * We use start here because we will need to wait on the IO to complete | |
2327 | * in btrfs_sync_log, which could require joining a transaction (for | |
2328 | * example checking cross references in the nocow path). If we use join | |
2329 | * here we could get into a situation where we're waiting on IO to | |
2330 | * happen that is blocked on a transaction trying to commit. With start | |
2331 | * we inc the extwriter counter, so we wait for all extwriters to exit | |
52042d8e | 2332 | * before we start blocking joiners. This comment is to keep somebody |
5039eddc JB |
2333 | * from thinking they are super smart and changing this to |
2334 | * btrfs_join_transaction *cough*Josef*cough*. | |
2335 | */ | |
a22285a6 YZ |
2336 | trans = btrfs_start_transaction(root, 0); |
2337 | if (IS_ERR(trans)) { | |
2338 | ret = PTR_ERR(trans); | |
48778179 | 2339 | goto out_release_extents; |
39279cc3 | 2340 | } |
d0c2f4fa | 2341 | trans->in_fsync = true; |
e02119d5 | 2342 | |
48778179 FM |
2343 | ret = btrfs_log_dentry_safe(trans, dentry, &ctx); |
2344 | btrfs_release_log_ctx_extents(&ctx); | |
02c24a82 | 2345 | if (ret < 0) { |
a0634be5 FDBM |
2346 | /* Fallthrough and commit/free transaction. */ |
2347 | ret = 1; | |
02c24a82 | 2348 | } |
49eb7e46 CM |
2349 | |
2350 | /* we've logged all the items and now have a consistent | |
2351 | * version of the file in the log. It is possible that | |
2352 | * someone will come in and modify the file, but that's | |
2353 | * fine because the log is consistent on disk, and we | |
2354 | * have references to all of the file's extents | |
2355 | * | |
2356 | * It is possible that someone will come in and log the | |
2357 | * file again, but that will end up using the synchronization | |
2358 | * inside btrfs_sync_log to keep things safe. | |
2359 | */ | |
885f46d8 | 2360 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
49eb7e46 | 2361 | |
257c62e1 | 2362 | if (ret != BTRFS_NO_LOG_SYNC) { |
0ef8b726 | 2363 | if (!ret) { |
8b050d35 | 2364 | ret = btrfs_sync_log(trans, root, &ctx); |
0ef8b726 | 2365 | if (!ret) { |
3a45bb20 | 2366 | ret = btrfs_end_transaction(trans); |
0ef8b726 | 2367 | goto out; |
2ab28f32 | 2368 | } |
257c62e1 | 2369 | } |
48778179 FM |
2370 | if (!full_sync) { |
2371 | ret = btrfs_wait_ordered_range(inode, start, len); | |
2372 | if (ret) { | |
2373 | btrfs_end_transaction(trans); | |
2374 | goto out; | |
2375 | } | |
2376 | } | |
3a45bb20 | 2377 | ret = btrfs_commit_transaction(trans); |
257c62e1 | 2378 | } else { |
3a45bb20 | 2379 | ret = btrfs_end_transaction(trans); |
e02119d5 | 2380 | } |
39279cc3 | 2381 | out: |
ebb70442 | 2382 | ASSERT(list_empty(&ctx.list)); |
333427a5 JL |
2383 | err = file_check_and_advance_wb_err(file); |
2384 | if (!ret) | |
2385 | ret = err; | |
014e4ac4 | 2386 | return ret > 0 ? -EIO : ret; |
48778179 FM |
2387 | |
2388 | out_release_extents: | |
2389 | btrfs_release_log_ctx_extents(&ctx); | |
885f46d8 | 2390 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
48778179 | 2391 | goto out; |
39279cc3 CM |
2392 | } |
2393 | ||
f0f37e2f | 2394 | static const struct vm_operations_struct btrfs_file_vm_ops = { |
92fee66d | 2395 | .fault = filemap_fault, |
f1820361 | 2396 | .map_pages = filemap_map_pages, |
9ebefb18 CM |
2397 | .page_mkwrite = btrfs_page_mkwrite, |
2398 | }; | |
2399 | ||
2400 | static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma) | |
2401 | { | |
058a457e MX |
2402 | struct address_space *mapping = filp->f_mapping; |
2403 | ||
2404 | if (!mapping->a_ops->readpage) | |
2405 | return -ENOEXEC; | |
2406 | ||
9ebefb18 | 2407 | file_accessed(filp); |
058a457e | 2408 | vma->vm_ops = &btrfs_file_vm_ops; |
058a457e | 2409 | |
9ebefb18 CM |
2410 | return 0; |
2411 | } | |
2412 | ||
35339c24 | 2413 | static int hole_mergeable(struct btrfs_inode *inode, struct extent_buffer *leaf, |
2aaa6655 JB |
2414 | int slot, u64 start, u64 end) |
2415 | { | |
2416 | struct btrfs_file_extent_item *fi; | |
2417 | struct btrfs_key key; | |
2418 | ||
2419 | if (slot < 0 || slot >= btrfs_header_nritems(leaf)) | |
2420 | return 0; | |
2421 | ||
2422 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
35339c24 | 2423 | if (key.objectid != btrfs_ino(inode) || |
2aaa6655 JB |
2424 | key.type != BTRFS_EXTENT_DATA_KEY) |
2425 | return 0; | |
2426 | ||
2427 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
2428 | ||
2429 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2430 | return 0; | |
2431 | ||
2432 | if (btrfs_file_extent_disk_bytenr(leaf, fi)) | |
2433 | return 0; | |
2434 | ||
2435 | if (key.offset == end) | |
2436 | return 1; | |
2437 | if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start) | |
2438 | return 1; | |
2439 | return 0; | |
2440 | } | |
2441 | ||
a012a74e NB |
2442 | static int fill_holes(struct btrfs_trans_handle *trans, |
2443 | struct btrfs_inode *inode, | |
2444 | struct btrfs_path *path, u64 offset, u64 end) | |
2aaa6655 | 2445 | { |
3ffbd68c | 2446 | struct btrfs_fs_info *fs_info = trans->fs_info; |
a012a74e | 2447 | struct btrfs_root *root = inode->root; |
2aaa6655 JB |
2448 | struct extent_buffer *leaf; |
2449 | struct btrfs_file_extent_item *fi; | |
2450 | struct extent_map *hole_em; | |
a012a74e | 2451 | struct extent_map_tree *em_tree = &inode->extent_tree; |
2aaa6655 JB |
2452 | struct btrfs_key key; |
2453 | int ret; | |
2454 | ||
0b246afa | 2455 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) |
16e7549f JB |
2456 | goto out; |
2457 | ||
a012a74e | 2458 | key.objectid = btrfs_ino(inode); |
2aaa6655 JB |
2459 | key.type = BTRFS_EXTENT_DATA_KEY; |
2460 | key.offset = offset; | |
2461 | ||
2aaa6655 | 2462 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
f94480bd JB |
2463 | if (ret <= 0) { |
2464 | /* | |
2465 | * We should have dropped this offset, so if we find it then | |
2466 | * something has gone horribly wrong. | |
2467 | */ | |
2468 | if (ret == 0) | |
2469 | ret = -EINVAL; | |
2aaa6655 | 2470 | return ret; |
f94480bd | 2471 | } |
2aaa6655 JB |
2472 | |
2473 | leaf = path->nodes[0]; | |
a012a74e | 2474 | if (hole_mergeable(inode, leaf, path->slots[0] - 1, offset, end)) { |
2aaa6655 JB |
2475 | u64 num_bytes; |
2476 | ||
2477 | path->slots[0]--; | |
2478 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2479 | struct btrfs_file_extent_item); | |
2480 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + | |
2481 | end - offset; | |
2482 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2483 | btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes); | |
2484 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2485 | btrfs_mark_buffer_dirty(leaf); | |
2486 | goto out; | |
2487 | } | |
2488 | ||
1707e26d | 2489 | if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) { |
2aaa6655 JB |
2490 | u64 num_bytes; |
2491 | ||
2aaa6655 | 2492 | key.offset = offset; |
0b246afa | 2493 | btrfs_set_item_key_safe(fs_info, path, &key); |
2aaa6655 JB |
2494 | fi = btrfs_item_ptr(leaf, path->slots[0], |
2495 | struct btrfs_file_extent_item); | |
2496 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end - | |
2497 | offset; | |
2498 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2499 | btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes); | |
2500 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2501 | btrfs_mark_buffer_dirty(leaf); | |
2502 | goto out; | |
2503 | } | |
2504 | btrfs_release_path(path); | |
2505 | ||
a012a74e | 2506 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), |
f85b7379 | 2507 | offset, 0, 0, end - offset, 0, end - offset, 0, 0, 0); |
2aaa6655 JB |
2508 | if (ret) |
2509 | return ret; | |
2510 | ||
2511 | out: | |
2512 | btrfs_release_path(path); | |
2513 | ||
2514 | hole_em = alloc_extent_map(); | |
2515 | if (!hole_em) { | |
2516 | btrfs_drop_extent_cache(inode, offset, end - 1, 0); | |
23e3337f | 2517 | btrfs_set_inode_full_sync(inode); |
2aaa6655 JB |
2518 | } else { |
2519 | hole_em->start = offset; | |
2520 | hole_em->len = end - offset; | |
cc95bef6 | 2521 | hole_em->ram_bytes = hole_em->len; |
2aaa6655 JB |
2522 | hole_em->orig_start = offset; |
2523 | ||
2524 | hole_em->block_start = EXTENT_MAP_HOLE; | |
2525 | hole_em->block_len = 0; | |
b4939680 | 2526 | hole_em->orig_block_len = 0; |
2aaa6655 JB |
2527 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
2528 | hole_em->generation = trans->transid; | |
2529 | ||
2530 | do { | |
2531 | btrfs_drop_extent_cache(inode, offset, end - 1, 0); | |
2532 | write_lock(&em_tree->lock); | |
09a2a8f9 | 2533 | ret = add_extent_mapping(em_tree, hole_em, 1); |
2aaa6655 JB |
2534 | write_unlock(&em_tree->lock); |
2535 | } while (ret == -EEXIST); | |
2536 | free_extent_map(hole_em); | |
2537 | if (ret) | |
23e3337f | 2538 | btrfs_set_inode_full_sync(inode); |
2aaa6655 JB |
2539 | } |
2540 | ||
2541 | return 0; | |
2542 | } | |
2543 | ||
d7781546 QW |
2544 | /* |
2545 | * Find a hole extent on given inode and change start/len to the end of hole | |
2546 | * extent.(hole/vacuum extent whose em->start <= start && | |
2547 | * em->start + em->len > start) | |
2548 | * When a hole extent is found, return 1 and modify start/len. | |
2549 | */ | |
dea46d84 | 2550 | static int find_first_non_hole(struct btrfs_inode *inode, u64 *start, u64 *len) |
d7781546 | 2551 | { |
dea46d84 | 2552 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
d7781546 QW |
2553 | struct extent_map *em; |
2554 | int ret = 0; | |
2555 | ||
dea46d84 | 2556 | em = btrfs_get_extent(inode, NULL, 0, |
609805d8 | 2557 | round_down(*start, fs_info->sectorsize), |
39b07b5d | 2558 | round_up(*len, fs_info->sectorsize)); |
9986277e DC |
2559 | if (IS_ERR(em)) |
2560 | return PTR_ERR(em); | |
d7781546 QW |
2561 | |
2562 | /* Hole or vacuum extent(only exists in no-hole mode) */ | |
2563 | if (em->block_start == EXTENT_MAP_HOLE) { | |
2564 | ret = 1; | |
2565 | *len = em->start + em->len > *start + *len ? | |
2566 | 0 : *start + *len - em->start - em->len; | |
2567 | *start = em->start + em->len; | |
2568 | } | |
2569 | free_extent_map(em); | |
2570 | return ret; | |
2571 | } | |
2572 | ||
f27451f2 FM |
2573 | static int btrfs_punch_hole_lock_range(struct inode *inode, |
2574 | const u64 lockstart, | |
2575 | const u64 lockend, | |
2576 | struct extent_state **cached_state) | |
2577 | { | |
0528476b QW |
2578 | /* |
2579 | * For subpage case, if the range is not at page boundary, we could | |
2580 | * have pages at the leading/tailing part of the range. | |
2581 | * This could lead to dead loop since filemap_range_has_page() | |
2582 | * will always return true. | |
2583 | * So here we need to do extra page alignment for | |
2584 | * filemap_range_has_page(). | |
2585 | */ | |
2586 | const u64 page_lockstart = round_up(lockstart, PAGE_SIZE); | |
2587 | const u64 page_lockend = round_down(lockend + 1, PAGE_SIZE) - 1; | |
2588 | ||
f27451f2 FM |
2589 | while (1) { |
2590 | struct btrfs_ordered_extent *ordered; | |
2591 | int ret; | |
2592 | ||
2593 | truncate_pagecache_range(inode, lockstart, lockend); | |
2594 | ||
2595 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
2596 | cached_state); | |
6d072c8e NB |
2597 | ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode), |
2598 | lockend); | |
f27451f2 FM |
2599 | |
2600 | /* | |
2601 | * We need to make sure we have no ordered extents in this range | |
2602 | * and nobody raced in and read a page in this range, if we did | |
2603 | * we need to try again. | |
2604 | */ | |
2605 | if ((!ordered || | |
bffe633e | 2606 | (ordered->file_offset + ordered->num_bytes <= lockstart || |
f27451f2 | 2607 | ordered->file_offset > lockend)) && |
051c98eb | 2608 | !filemap_range_has_page(inode->i_mapping, |
0528476b | 2609 | page_lockstart, page_lockend)) { |
f27451f2 FM |
2610 | if (ordered) |
2611 | btrfs_put_ordered_extent(ordered); | |
2612 | break; | |
2613 | } | |
2614 | if (ordered) | |
2615 | btrfs_put_ordered_extent(ordered); | |
2616 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, | |
2617 | lockend, cached_state); | |
2618 | ret = btrfs_wait_ordered_range(inode, lockstart, | |
2619 | lockend - lockstart + 1); | |
2620 | if (ret) | |
2621 | return ret; | |
2622 | } | |
2623 | return 0; | |
2624 | } | |
2625 | ||
0cbb5bdf | 2626 | static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans, |
03fcb1ab | 2627 | struct btrfs_inode *inode, |
690a5dbf | 2628 | struct btrfs_path *path, |
bf385648 | 2629 | struct btrfs_replace_extent_info *extent_info, |
2766ff61 FM |
2630 | const u64 replace_len, |
2631 | const u64 bytes_to_drop) | |
690a5dbf | 2632 | { |
03fcb1ab NB |
2633 | struct btrfs_fs_info *fs_info = trans->fs_info; |
2634 | struct btrfs_root *root = inode->root; | |
690a5dbf FM |
2635 | struct btrfs_file_extent_item *extent; |
2636 | struct extent_buffer *leaf; | |
2637 | struct btrfs_key key; | |
2638 | int slot; | |
2639 | struct btrfs_ref ref = { 0 }; | |
690a5dbf FM |
2640 | int ret; |
2641 | ||
bf385648 | 2642 | if (replace_len == 0) |
690a5dbf FM |
2643 | return 0; |
2644 | ||
bf385648 | 2645 | if (extent_info->disk_offset == 0 && |
2766ff61 | 2646 | btrfs_fs_incompat(fs_info, NO_HOLES)) { |
03fcb1ab | 2647 | btrfs_update_inode_bytes(inode, 0, bytes_to_drop); |
690a5dbf | 2648 | return 0; |
2766ff61 | 2649 | } |
690a5dbf | 2650 | |
03fcb1ab | 2651 | key.objectid = btrfs_ino(inode); |
690a5dbf | 2652 | key.type = BTRFS_EXTENT_DATA_KEY; |
bf385648 | 2653 | key.offset = extent_info->file_offset; |
690a5dbf | 2654 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
fb870f6c | 2655 | sizeof(struct btrfs_file_extent_item)); |
690a5dbf FM |
2656 | if (ret) |
2657 | return ret; | |
2658 | leaf = path->nodes[0]; | |
2659 | slot = path->slots[0]; | |
bf385648 | 2660 | write_extent_buffer(leaf, extent_info->extent_buf, |
690a5dbf | 2661 | btrfs_item_ptr_offset(leaf, slot), |
fb870f6c | 2662 | sizeof(struct btrfs_file_extent_item)); |
690a5dbf | 2663 | extent = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
fb870f6c | 2664 | ASSERT(btrfs_file_extent_type(leaf, extent) != BTRFS_FILE_EXTENT_INLINE); |
bf385648 FM |
2665 | btrfs_set_file_extent_offset(leaf, extent, extent_info->data_offset); |
2666 | btrfs_set_file_extent_num_bytes(leaf, extent, replace_len); | |
2667 | if (extent_info->is_new_extent) | |
8fccebfa | 2668 | btrfs_set_file_extent_generation(leaf, extent, trans->transid); |
690a5dbf FM |
2669 | btrfs_mark_buffer_dirty(leaf); |
2670 | btrfs_release_path(path); | |
2671 | ||
03fcb1ab NB |
2672 | ret = btrfs_inode_set_file_extent_range(inode, extent_info->file_offset, |
2673 | replace_len); | |
9ddc959e JB |
2674 | if (ret) |
2675 | return ret; | |
2676 | ||
690a5dbf | 2677 | /* If it's a hole, nothing more needs to be done. */ |
2766ff61 | 2678 | if (extent_info->disk_offset == 0) { |
03fcb1ab | 2679 | btrfs_update_inode_bytes(inode, 0, bytes_to_drop); |
690a5dbf | 2680 | return 0; |
2766ff61 | 2681 | } |
690a5dbf | 2682 | |
03fcb1ab | 2683 | btrfs_update_inode_bytes(inode, replace_len, bytes_to_drop); |
8fccebfa | 2684 | |
bf385648 FM |
2685 | if (extent_info->is_new_extent && extent_info->insertions == 0) { |
2686 | key.objectid = extent_info->disk_offset; | |
8fccebfa | 2687 | key.type = BTRFS_EXTENT_ITEM_KEY; |
bf385648 | 2688 | key.offset = extent_info->disk_len; |
8fccebfa | 2689 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
03fcb1ab | 2690 | btrfs_ino(inode), |
bf385648 FM |
2691 | extent_info->file_offset, |
2692 | extent_info->qgroup_reserved, | |
8fccebfa FM |
2693 | &key); |
2694 | } else { | |
2695 | u64 ref_offset; | |
2696 | ||
2697 | btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, | |
bf385648 FM |
2698 | extent_info->disk_offset, |
2699 | extent_info->disk_len, 0); | |
2700 | ref_offset = extent_info->file_offset - extent_info->data_offset; | |
8fccebfa | 2701 | btrfs_init_data_ref(&ref, root->root_key.objectid, |
f42c5da6 | 2702 | btrfs_ino(inode), ref_offset, 0, false); |
8fccebfa FM |
2703 | ret = btrfs_inc_extent_ref(trans, &ref); |
2704 | } | |
2705 | ||
bf385648 | 2706 | extent_info->insertions++; |
690a5dbf FM |
2707 | |
2708 | return ret; | |
2709 | } | |
2710 | ||
9cba40a6 FM |
2711 | /* |
2712 | * The respective range must have been previously locked, as well as the inode. | |
2713 | * The end offset is inclusive (last byte of the range). | |
bf385648 FM |
2714 | * @extent_info is NULL for fallocate's hole punching and non-NULL when replacing |
2715 | * the file range with an extent. | |
2716 | * When not punching a hole, we don't want to end up in a state where we dropped | |
2717 | * extents without inserting a new one, so we must abort the transaction to avoid | |
2718 | * a corruption. | |
9cba40a6 | 2719 | */ |
bfc78479 NB |
2720 | int btrfs_replace_file_extents(struct btrfs_inode *inode, |
2721 | struct btrfs_path *path, const u64 start, | |
2722 | const u64 end, | |
2723 | struct btrfs_replace_extent_info *extent_info, | |
2724 | struct btrfs_trans_handle **trans_out) | |
9cba40a6 | 2725 | { |
5893dfb9 | 2726 | struct btrfs_drop_extents_args drop_args = { 0 }; |
bfc78479 NB |
2727 | struct btrfs_root *root = inode->root; |
2728 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2bd36e7b | 2729 | u64 min_size = btrfs_calc_insert_metadata_size(fs_info, 1); |
bfc78479 | 2730 | u64 ino_size = round_up(inode->vfs_inode.i_size, fs_info->sectorsize); |
9cba40a6 FM |
2731 | struct btrfs_trans_handle *trans = NULL; |
2732 | struct btrfs_block_rsv *rsv; | |
2733 | unsigned int rsv_count; | |
2734 | u64 cur_offset; | |
9cba40a6 FM |
2735 | u64 len = end - start; |
2736 | int ret = 0; | |
2737 | ||
2738 | if (end <= start) | |
2739 | return -EINVAL; | |
2740 | ||
2741 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); | |
2742 | if (!rsv) { | |
2743 | ret = -ENOMEM; | |
2744 | goto out; | |
2745 | } | |
2bd36e7b | 2746 | rsv->size = btrfs_calc_insert_metadata_size(fs_info, 1); |
9cba40a6 FM |
2747 | rsv->failfast = 1; |
2748 | ||
2749 | /* | |
2750 | * 1 - update the inode | |
2751 | * 1 - removing the extents in the range | |
bf385648 FM |
2752 | * 1 - adding the hole extent if no_holes isn't set or if we are |
2753 | * replacing the range with a new extent | |
9cba40a6 | 2754 | */ |
bf385648 | 2755 | if (!btrfs_fs_incompat(fs_info, NO_HOLES) || extent_info) |
690a5dbf FM |
2756 | rsv_count = 3; |
2757 | else | |
2758 | rsv_count = 2; | |
2759 | ||
9cba40a6 FM |
2760 | trans = btrfs_start_transaction(root, rsv_count); |
2761 | if (IS_ERR(trans)) { | |
2762 | ret = PTR_ERR(trans); | |
2763 | trans = NULL; | |
2764 | goto out_free; | |
2765 | } | |
2766 | ||
2767 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, | |
2768 | min_size, false); | |
2769 | BUG_ON(ret); | |
2770 | trans->block_rsv = rsv; | |
2771 | ||
2772 | cur_offset = start; | |
5893dfb9 FM |
2773 | drop_args.path = path; |
2774 | drop_args.end = end + 1; | |
2775 | drop_args.drop_cache = true; | |
9cba40a6 | 2776 | while (cur_offset < end) { |
5893dfb9 | 2777 | drop_args.start = cur_offset; |
bfc78479 | 2778 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
2766ff61 FM |
2779 | /* If we are punching a hole decrement the inode's byte count */ |
2780 | if (!extent_info) | |
bfc78479 | 2781 | btrfs_update_inode_bytes(inode, 0, |
2766ff61 | 2782 | drop_args.bytes_found); |
690a5dbf FM |
2783 | if (ret != -ENOSPC) { |
2784 | /* | |
4afb912f JB |
2785 | * The only time we don't want to abort is if we are |
2786 | * attempting to clone a partial inline extent, in which | |
2787 | * case we'll get EOPNOTSUPP. However if we aren't | |
2788 | * clone we need to abort no matter what, because if we | |
2789 | * got EOPNOTSUPP via prealloc then we messed up and | |
2790 | * need to abort. | |
690a5dbf | 2791 | */ |
4afb912f JB |
2792 | if (ret && |
2793 | (ret != -EOPNOTSUPP || | |
2794 | (extent_info && extent_info->is_new_extent))) | |
690a5dbf | 2795 | btrfs_abort_transaction(trans, ret); |
9cba40a6 | 2796 | break; |
690a5dbf | 2797 | } |
9cba40a6 FM |
2798 | |
2799 | trans->block_rsv = &fs_info->trans_block_rsv; | |
2800 | ||
5893dfb9 | 2801 | if (!extent_info && cur_offset < drop_args.drop_end && |
690a5dbf | 2802 | cur_offset < ino_size) { |
bfc78479 NB |
2803 | ret = fill_holes(trans, inode, path, cur_offset, |
2804 | drop_args.drop_end); | |
9cba40a6 FM |
2805 | if (ret) { |
2806 | /* | |
2807 | * If we failed then we didn't insert our hole | |
2808 | * entries for the area we dropped, so now the | |
2809 | * fs is corrupted, so we must abort the | |
2810 | * transaction. | |
2811 | */ | |
2812 | btrfs_abort_transaction(trans, ret); | |
2813 | break; | |
2814 | } | |
5893dfb9 | 2815 | } else if (!extent_info && cur_offset < drop_args.drop_end) { |
9ddc959e JB |
2816 | /* |
2817 | * We are past the i_size here, but since we didn't | |
2818 | * insert holes we need to clear the mapped area so we | |
2819 | * know to not set disk_i_size in this area until a new | |
2820 | * file extent is inserted here. | |
2821 | */ | |
bfc78479 | 2822 | ret = btrfs_inode_clear_file_extent_range(inode, |
5893dfb9 FM |
2823 | cur_offset, |
2824 | drop_args.drop_end - cur_offset); | |
9ddc959e JB |
2825 | if (ret) { |
2826 | /* | |
2827 | * We couldn't clear our area, so we could | |
2828 | * presumably adjust up and corrupt the fs, so | |
2829 | * we need to abort. | |
2830 | */ | |
2831 | btrfs_abort_transaction(trans, ret); | |
2832 | break; | |
2833 | } | |
9cba40a6 FM |
2834 | } |
2835 | ||
5893dfb9 FM |
2836 | if (extent_info && |
2837 | drop_args.drop_end > extent_info->file_offset) { | |
2838 | u64 replace_len = drop_args.drop_end - | |
2839 | extent_info->file_offset; | |
690a5dbf | 2840 | |
bfc78479 NB |
2841 | ret = btrfs_insert_replace_extent(trans, inode, path, |
2842 | extent_info, replace_len, | |
03fcb1ab | 2843 | drop_args.bytes_found); |
690a5dbf FM |
2844 | if (ret) { |
2845 | btrfs_abort_transaction(trans, ret); | |
2846 | break; | |
2847 | } | |
bf385648 FM |
2848 | extent_info->data_len -= replace_len; |
2849 | extent_info->data_offset += replace_len; | |
2850 | extent_info->file_offset += replace_len; | |
690a5dbf FM |
2851 | } |
2852 | ||
bfc78479 | 2853 | ret = btrfs_update_inode(trans, root, inode); |
9cba40a6 FM |
2854 | if (ret) |
2855 | break; | |
2856 | ||
2857 | btrfs_end_transaction(trans); | |
2858 | btrfs_btree_balance_dirty(fs_info); | |
2859 | ||
2860 | trans = btrfs_start_transaction(root, rsv_count); | |
2861 | if (IS_ERR(trans)) { | |
2862 | ret = PTR_ERR(trans); | |
2863 | trans = NULL; | |
2864 | break; | |
2865 | } | |
2866 | ||
2867 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, | |
2868 | rsv, min_size, false); | |
2869 | BUG_ON(ret); /* shouldn't happen */ | |
2870 | trans->block_rsv = rsv; | |
2871 | ||
3227788c BC |
2872 | cur_offset = drop_args.drop_end; |
2873 | len = end - cur_offset; | |
2874 | if (!extent_info && len) { | |
bfc78479 | 2875 | ret = find_first_non_hole(inode, &cur_offset, &len); |
690a5dbf FM |
2876 | if (unlikely(ret < 0)) |
2877 | break; | |
2878 | if (ret && !len) { | |
2879 | ret = 0; | |
2880 | break; | |
2881 | } | |
9cba40a6 FM |
2882 | } |
2883 | } | |
2884 | ||
690a5dbf FM |
2885 | /* |
2886 | * If we were cloning, force the next fsync to be a full one since we | |
2887 | * we replaced (or just dropped in the case of cloning holes when | |
e2b84217 FM |
2888 | * NO_HOLES is enabled) file extent items and did not setup new extent |
2889 | * maps for the replacement extents (or holes). | |
690a5dbf | 2890 | */ |
bf385648 | 2891 | if (extent_info && !extent_info->is_new_extent) |
23e3337f | 2892 | btrfs_set_inode_full_sync(inode); |
690a5dbf | 2893 | |
9cba40a6 FM |
2894 | if (ret) |
2895 | goto out_trans; | |
2896 | ||
2897 | trans->block_rsv = &fs_info->trans_block_rsv; | |
2898 | /* | |
2899 | * If we are using the NO_HOLES feature we might have had already an | |
2900 | * hole that overlaps a part of the region [lockstart, lockend] and | |
2901 | * ends at (or beyond) lockend. Since we have no file extent items to | |
2902 | * represent holes, drop_end can be less than lockend and so we must | |
2903 | * make sure we have an extent map representing the existing hole (the | |
2904 | * call to __btrfs_drop_extents() might have dropped the existing extent | |
2905 | * map representing the existing hole), otherwise the fast fsync path | |
2906 | * will not record the existence of the hole region | |
2907 | * [existing_hole_start, lockend]. | |
2908 | */ | |
5893dfb9 FM |
2909 | if (drop_args.drop_end <= end) |
2910 | drop_args.drop_end = end + 1; | |
9cba40a6 FM |
2911 | /* |
2912 | * Don't insert file hole extent item if it's for a range beyond eof | |
2913 | * (because it's useless) or if it represents a 0 bytes range (when | |
2914 | * cur_offset == drop_end). | |
2915 | */ | |
5893dfb9 FM |
2916 | if (!extent_info && cur_offset < ino_size && |
2917 | cur_offset < drop_args.drop_end) { | |
bfc78479 NB |
2918 | ret = fill_holes(trans, inode, path, cur_offset, |
2919 | drop_args.drop_end); | |
9cba40a6 FM |
2920 | if (ret) { |
2921 | /* Same comment as above. */ | |
2922 | btrfs_abort_transaction(trans, ret); | |
2923 | goto out_trans; | |
2924 | } | |
5893dfb9 | 2925 | } else if (!extent_info && cur_offset < drop_args.drop_end) { |
9ddc959e | 2926 | /* See the comment in the loop above for the reasoning here. */ |
bfc78479 NB |
2927 | ret = btrfs_inode_clear_file_extent_range(inode, cur_offset, |
2928 | drop_args.drop_end - cur_offset); | |
9ddc959e JB |
2929 | if (ret) { |
2930 | btrfs_abort_transaction(trans, ret); | |
2931 | goto out_trans; | |
2932 | } | |
2933 | ||
9cba40a6 | 2934 | } |
bf385648 | 2935 | if (extent_info) { |
bfc78479 | 2936 | ret = btrfs_insert_replace_extent(trans, inode, path, |
03fcb1ab NB |
2937 | extent_info, extent_info->data_len, |
2938 | drop_args.bytes_found); | |
690a5dbf FM |
2939 | if (ret) { |
2940 | btrfs_abort_transaction(trans, ret); | |
2941 | goto out_trans; | |
2942 | } | |
2943 | } | |
9cba40a6 FM |
2944 | |
2945 | out_trans: | |
2946 | if (!trans) | |
2947 | goto out_free; | |
2948 | ||
2949 | trans->block_rsv = &fs_info->trans_block_rsv; | |
2950 | if (ret) | |
2951 | btrfs_end_transaction(trans); | |
2952 | else | |
2953 | *trans_out = trans; | |
2954 | out_free: | |
2955 | btrfs_free_block_rsv(fs_info, rsv); | |
2956 | out: | |
2957 | return ret; | |
2958 | } | |
2959 | ||
05fd9564 | 2960 | static int btrfs_punch_hole(struct file *file, loff_t offset, loff_t len) |
2aaa6655 | 2961 | { |
05fd9564 | 2962 | struct inode *inode = file_inode(file); |
0b246afa | 2963 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
2aaa6655 JB |
2964 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2965 | struct extent_state *cached_state = NULL; | |
2966 | struct btrfs_path *path; | |
9cba40a6 | 2967 | struct btrfs_trans_handle *trans = NULL; |
d7781546 QW |
2968 | u64 lockstart; |
2969 | u64 lockend; | |
2970 | u64 tail_start; | |
2971 | u64 tail_len; | |
2972 | u64 orig_start = offset; | |
2aaa6655 | 2973 | int ret = 0; |
9703fefe | 2974 | bool same_block; |
a1a50f60 | 2975 | u64 ino_size; |
9703fefe | 2976 | bool truncated_block = false; |
e8c1c76e | 2977 | bool updated_inode = false; |
2aaa6655 | 2978 | |
bd6526d0 FM |
2979 | btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP); |
2980 | ||
0ef8b726 JB |
2981 | ret = btrfs_wait_ordered_range(inode, offset, len); |
2982 | if (ret) | |
bd6526d0 | 2983 | goto out_only_mutex; |
2aaa6655 | 2984 | |
0b246afa | 2985 | ino_size = round_up(inode->i_size, fs_info->sectorsize); |
dea46d84 | 2986 | ret = find_first_non_hole(BTRFS_I(inode), &offset, &len); |
d7781546 QW |
2987 | if (ret < 0) |
2988 | goto out_only_mutex; | |
2989 | if (ret && !len) { | |
2990 | /* Already in a large hole */ | |
2991 | ret = 0; | |
2992 | goto out_only_mutex; | |
2993 | } | |
2994 | ||
05fd9564 DW |
2995 | ret = file_modified(file); |
2996 | if (ret) | |
2997 | goto out_only_mutex; | |
2998 | ||
6fee248d | 2999 | lockstart = round_up(offset, btrfs_inode_sectorsize(BTRFS_I(inode))); |
d7781546 | 3000 | lockend = round_down(offset + len, |
6fee248d | 3001 | btrfs_inode_sectorsize(BTRFS_I(inode))) - 1; |
0b246afa JM |
3002 | same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset)) |
3003 | == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)); | |
7426cc04 | 3004 | /* |
9703fefe | 3005 | * We needn't truncate any block which is beyond the end of the file |
7426cc04 MX |
3006 | * because we are sure there is no data there. |
3007 | */ | |
2aaa6655 | 3008 | /* |
9703fefe CR |
3009 | * Only do this if we are in the same block and we aren't doing the |
3010 | * entire block. | |
2aaa6655 | 3011 | */ |
0b246afa | 3012 | if (same_block && len < fs_info->sectorsize) { |
e8c1c76e | 3013 | if (offset < ino_size) { |
9703fefe | 3014 | truncated_block = true; |
217f42eb NB |
3015 | ret = btrfs_truncate_block(BTRFS_I(inode), offset, len, |
3016 | 0); | |
e8c1c76e FM |
3017 | } else { |
3018 | ret = 0; | |
3019 | } | |
d7781546 | 3020 | goto out_only_mutex; |
2aaa6655 JB |
3021 | } |
3022 | ||
9703fefe | 3023 | /* zero back part of the first block */ |
12870f1c | 3024 | if (offset < ino_size) { |
9703fefe | 3025 | truncated_block = true; |
217f42eb | 3026 | ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0); |
7426cc04 | 3027 | if (ret) { |
8d9b4a16 | 3028 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
7426cc04 MX |
3029 | return ret; |
3030 | } | |
2aaa6655 JB |
3031 | } |
3032 | ||
d7781546 QW |
3033 | /* Check the aligned pages after the first unaligned page, |
3034 | * if offset != orig_start, which means the first unaligned page | |
01327610 | 3035 | * including several following pages are already in holes, |
d7781546 QW |
3036 | * the extra check can be skipped */ |
3037 | if (offset == orig_start) { | |
3038 | /* after truncate page, check hole again */ | |
3039 | len = offset + len - lockstart; | |
3040 | offset = lockstart; | |
dea46d84 | 3041 | ret = find_first_non_hole(BTRFS_I(inode), &offset, &len); |
d7781546 QW |
3042 | if (ret < 0) |
3043 | goto out_only_mutex; | |
3044 | if (ret && !len) { | |
3045 | ret = 0; | |
3046 | goto out_only_mutex; | |
3047 | } | |
3048 | lockstart = offset; | |
3049 | } | |
3050 | ||
3051 | /* Check the tail unaligned part is in a hole */ | |
3052 | tail_start = lockend + 1; | |
3053 | tail_len = offset + len - tail_start; | |
3054 | if (tail_len) { | |
dea46d84 | 3055 | ret = find_first_non_hole(BTRFS_I(inode), &tail_start, &tail_len); |
d7781546 QW |
3056 | if (unlikely(ret < 0)) |
3057 | goto out_only_mutex; | |
3058 | if (!ret) { | |
3059 | /* zero the front end of the last page */ | |
3060 | if (tail_start + tail_len < ino_size) { | |
9703fefe | 3061 | truncated_block = true; |
217f42eb | 3062 | ret = btrfs_truncate_block(BTRFS_I(inode), |
9703fefe CR |
3063 | tail_start + tail_len, |
3064 | 0, 1); | |
d7781546 QW |
3065 | if (ret) |
3066 | goto out_only_mutex; | |
51f395ad | 3067 | } |
0061280d | 3068 | } |
2aaa6655 JB |
3069 | } |
3070 | ||
3071 | if (lockend < lockstart) { | |
e8c1c76e FM |
3072 | ret = 0; |
3073 | goto out_only_mutex; | |
2aaa6655 JB |
3074 | } |
3075 | ||
f27451f2 FM |
3076 | ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend, |
3077 | &cached_state); | |
8fca9550 | 3078 | if (ret) |
f27451f2 | 3079 | goto out_only_mutex; |
2aaa6655 JB |
3080 | |
3081 | path = btrfs_alloc_path(); | |
3082 | if (!path) { | |
3083 | ret = -ENOMEM; | |
3084 | goto out; | |
3085 | } | |
3086 | ||
bfc78479 NB |
3087 | ret = btrfs_replace_file_extents(BTRFS_I(inode), path, lockstart, |
3088 | lockend, NULL, &trans); | |
9cba40a6 FM |
3089 | btrfs_free_path(path); |
3090 | if (ret) | |
3091 | goto out; | |
2aaa6655 | 3092 | |
9cba40a6 | 3093 | ASSERT(trans != NULL); |
e1f5790e | 3094 | inode_inc_iversion(inode); |
c2050a45 | 3095 | inode->i_mtime = inode->i_ctime = current_time(inode); |
9a56fcd1 | 3096 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
e8c1c76e | 3097 | updated_inode = true; |
3a45bb20 | 3098 | btrfs_end_transaction(trans); |
2ff7e61e | 3099 | btrfs_btree_balance_dirty(fs_info); |
2aaa6655 JB |
3100 | out: |
3101 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
e43bbe5e | 3102 | &cached_state); |
d7781546 | 3103 | out_only_mutex: |
9cba40a6 | 3104 | if (!updated_inode && truncated_block && !ret) { |
e8c1c76e FM |
3105 | /* |
3106 | * If we only end up zeroing part of a page, we still need to | |
3107 | * update the inode item, so that all the time fields are | |
3108 | * updated as well as the necessary btrfs inode in memory fields | |
3109 | * for detecting, at fsync time, if the inode isn't yet in the | |
3110 | * log tree or it's there but not up to date. | |
3111 | */ | |
17900668 FM |
3112 | struct timespec64 now = current_time(inode); |
3113 | ||
3114 | inode_inc_iversion(inode); | |
3115 | inode->i_mtime = now; | |
3116 | inode->i_ctime = now; | |
e8c1c76e FM |
3117 | trans = btrfs_start_transaction(root, 1); |
3118 | if (IS_ERR(trans)) { | |
9cba40a6 | 3119 | ret = PTR_ERR(trans); |
e8c1c76e | 3120 | } else { |
9cba40a6 FM |
3121 | int ret2; |
3122 | ||
9a56fcd1 | 3123 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
9cba40a6 FM |
3124 | ret2 = btrfs_end_transaction(trans); |
3125 | if (!ret) | |
3126 | ret = ret2; | |
e8c1c76e FM |
3127 | } |
3128 | } | |
8d9b4a16 | 3129 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
9cba40a6 | 3130 | return ret; |
2aaa6655 JB |
3131 | } |
3132 | ||
14524a84 QW |
3133 | /* Helper structure to record which range is already reserved */ |
3134 | struct falloc_range { | |
3135 | struct list_head list; | |
3136 | u64 start; | |
3137 | u64 len; | |
3138 | }; | |
3139 | ||
3140 | /* | |
3141 | * Helper function to add falloc range | |
3142 | * | |
3143 | * Caller should have locked the larger range of extent containing | |
3144 | * [start, len) | |
3145 | */ | |
3146 | static int add_falloc_range(struct list_head *head, u64 start, u64 len) | |
3147 | { | |
14524a84 QW |
3148 | struct falloc_range *range = NULL; |
3149 | ||
77d25534 NB |
3150 | if (!list_empty(head)) { |
3151 | /* | |
3152 | * As fallocate iterates by bytenr order, we only need to check | |
3153 | * the last range. | |
3154 | */ | |
3155 | range = list_last_entry(head, struct falloc_range, list); | |
3156 | if (range->start + range->len == start) { | |
3157 | range->len += len; | |
3158 | return 0; | |
3159 | } | |
14524a84 | 3160 | } |
77d25534 | 3161 | |
32fc932e | 3162 | range = kmalloc(sizeof(*range), GFP_KERNEL); |
14524a84 QW |
3163 | if (!range) |
3164 | return -ENOMEM; | |
3165 | range->start = start; | |
3166 | range->len = len; | |
3167 | list_add_tail(&range->list, head); | |
3168 | return 0; | |
3169 | } | |
3170 | ||
f27451f2 FM |
3171 | static int btrfs_fallocate_update_isize(struct inode *inode, |
3172 | const u64 end, | |
3173 | const int mode) | |
3174 | { | |
3175 | struct btrfs_trans_handle *trans; | |
3176 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3177 | int ret; | |
3178 | int ret2; | |
3179 | ||
3180 | if (mode & FALLOC_FL_KEEP_SIZE || end <= i_size_read(inode)) | |
3181 | return 0; | |
3182 | ||
3183 | trans = btrfs_start_transaction(root, 1); | |
3184 | if (IS_ERR(trans)) | |
3185 | return PTR_ERR(trans); | |
3186 | ||
3187 | inode->i_ctime = current_time(inode); | |
3188 | i_size_write(inode, end); | |
76aea537 | 3189 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
9a56fcd1 | 3190 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
f27451f2 FM |
3191 | ret2 = btrfs_end_transaction(trans); |
3192 | ||
3193 | return ret ? ret : ret2; | |
3194 | } | |
3195 | ||
81fdf638 | 3196 | enum { |
f262fa8d DS |
3197 | RANGE_BOUNDARY_WRITTEN_EXTENT, |
3198 | RANGE_BOUNDARY_PREALLOC_EXTENT, | |
3199 | RANGE_BOUNDARY_HOLE, | |
81fdf638 FM |
3200 | }; |
3201 | ||
948dfeb8 | 3202 | static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode, |
f27451f2 FM |
3203 | u64 offset) |
3204 | { | |
948dfeb8 | 3205 | const u64 sectorsize = btrfs_inode_sectorsize(inode); |
f27451f2 | 3206 | struct extent_map *em; |
81fdf638 | 3207 | int ret; |
f27451f2 FM |
3208 | |
3209 | offset = round_down(offset, sectorsize); | |
948dfeb8 | 3210 | em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize); |
f27451f2 FM |
3211 | if (IS_ERR(em)) |
3212 | return PTR_ERR(em); | |
3213 | ||
3214 | if (em->block_start == EXTENT_MAP_HOLE) | |
81fdf638 FM |
3215 | ret = RANGE_BOUNDARY_HOLE; |
3216 | else if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
3217 | ret = RANGE_BOUNDARY_PREALLOC_EXTENT; | |
3218 | else | |
3219 | ret = RANGE_BOUNDARY_WRITTEN_EXTENT; | |
f27451f2 FM |
3220 | |
3221 | free_extent_map(em); | |
3222 | return ret; | |
3223 | } | |
3224 | ||
3225 | static int btrfs_zero_range(struct inode *inode, | |
3226 | loff_t offset, | |
3227 | loff_t len, | |
3228 | const int mode) | |
3229 | { | |
3230 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
3231 | struct extent_map *em; | |
3232 | struct extent_changeset *data_reserved = NULL; | |
3233 | int ret; | |
3234 | u64 alloc_hint = 0; | |
6fee248d | 3235 | const u64 sectorsize = btrfs_inode_sectorsize(BTRFS_I(inode)); |
f27451f2 FM |
3236 | u64 alloc_start = round_down(offset, sectorsize); |
3237 | u64 alloc_end = round_up(offset + len, sectorsize); | |
3238 | u64 bytes_to_reserve = 0; | |
3239 | bool space_reserved = false; | |
3240 | ||
39b07b5d OS |
3241 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start, |
3242 | alloc_end - alloc_start); | |
f27451f2 FM |
3243 | if (IS_ERR(em)) { |
3244 | ret = PTR_ERR(em); | |
3245 | goto out; | |
3246 | } | |
3247 | ||
3248 | /* | |
3249 | * Avoid hole punching and extent allocation for some cases. More cases | |
3250 | * could be considered, but these are unlikely common and we keep things | |
3251 | * as simple as possible for now. Also, intentionally, if the target | |
3252 | * range contains one or more prealloc extents together with regular | |
3253 | * extents and holes, we drop all the existing extents and allocate a | |
3254 | * new prealloc extent, so that we get a larger contiguous disk extent. | |
3255 | */ | |
3256 | if (em->start <= alloc_start && | |
3257 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { | |
3258 | const u64 em_end = em->start + em->len; | |
3259 | ||
3260 | if (em_end >= offset + len) { | |
3261 | /* | |
3262 | * The whole range is already a prealloc extent, | |
3263 | * do nothing except updating the inode's i_size if | |
3264 | * needed. | |
3265 | */ | |
3266 | free_extent_map(em); | |
3267 | ret = btrfs_fallocate_update_isize(inode, offset + len, | |
3268 | mode); | |
3269 | goto out; | |
3270 | } | |
3271 | /* | |
3272 | * Part of the range is already a prealloc extent, so operate | |
3273 | * only on the remaining part of the range. | |
3274 | */ | |
3275 | alloc_start = em_end; | |
3276 | ASSERT(IS_ALIGNED(alloc_start, sectorsize)); | |
3277 | len = offset + len - alloc_start; | |
3278 | offset = alloc_start; | |
3279 | alloc_hint = em->block_start + em->len; | |
3280 | } | |
3281 | free_extent_map(em); | |
3282 | ||
3283 | if (BTRFS_BYTES_TO_BLKS(fs_info, offset) == | |
3284 | BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)) { | |
39b07b5d OS |
3285 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start, |
3286 | sectorsize); | |
f27451f2 FM |
3287 | if (IS_ERR(em)) { |
3288 | ret = PTR_ERR(em); | |
3289 | goto out; | |
3290 | } | |
3291 | ||
3292 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { | |
3293 | free_extent_map(em); | |
3294 | ret = btrfs_fallocate_update_isize(inode, offset + len, | |
3295 | mode); | |
3296 | goto out; | |
3297 | } | |
3298 | if (len < sectorsize && em->block_start != EXTENT_MAP_HOLE) { | |
3299 | free_extent_map(em); | |
217f42eb NB |
3300 | ret = btrfs_truncate_block(BTRFS_I(inode), offset, len, |
3301 | 0); | |
f27451f2 FM |
3302 | if (!ret) |
3303 | ret = btrfs_fallocate_update_isize(inode, | |
3304 | offset + len, | |
3305 | mode); | |
3306 | return ret; | |
3307 | } | |
3308 | free_extent_map(em); | |
3309 | alloc_start = round_down(offset, sectorsize); | |
3310 | alloc_end = alloc_start + sectorsize; | |
3311 | goto reserve_space; | |
3312 | } | |
3313 | ||
3314 | alloc_start = round_up(offset, sectorsize); | |
3315 | alloc_end = round_down(offset + len, sectorsize); | |
3316 | ||
3317 | /* | |
3318 | * For unaligned ranges, check the pages at the boundaries, they might | |
3319 | * map to an extent, in which case we need to partially zero them, or | |
3320 | * they might map to a hole, in which case we need our allocation range | |
3321 | * to cover them. | |
3322 | */ | |
3323 | if (!IS_ALIGNED(offset, sectorsize)) { | |
948dfeb8 NB |
3324 | ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode), |
3325 | offset); | |
f27451f2 FM |
3326 | if (ret < 0) |
3327 | goto out; | |
81fdf638 | 3328 | if (ret == RANGE_BOUNDARY_HOLE) { |
f27451f2 FM |
3329 | alloc_start = round_down(offset, sectorsize); |
3330 | ret = 0; | |
81fdf638 | 3331 | } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) { |
217f42eb | 3332 | ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0); |
f27451f2 FM |
3333 | if (ret) |
3334 | goto out; | |
81fdf638 FM |
3335 | } else { |
3336 | ret = 0; | |
f27451f2 FM |
3337 | } |
3338 | } | |
3339 | ||
3340 | if (!IS_ALIGNED(offset + len, sectorsize)) { | |
948dfeb8 | 3341 | ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode), |
f27451f2 FM |
3342 | offset + len); |
3343 | if (ret < 0) | |
3344 | goto out; | |
81fdf638 | 3345 | if (ret == RANGE_BOUNDARY_HOLE) { |
f27451f2 FM |
3346 | alloc_end = round_up(offset + len, sectorsize); |
3347 | ret = 0; | |
81fdf638 | 3348 | } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) { |
217f42eb NB |
3349 | ret = btrfs_truncate_block(BTRFS_I(inode), offset + len, |
3350 | 0, 1); | |
f27451f2 FM |
3351 | if (ret) |
3352 | goto out; | |
81fdf638 FM |
3353 | } else { |
3354 | ret = 0; | |
f27451f2 FM |
3355 | } |
3356 | } | |
3357 | ||
3358 | reserve_space: | |
3359 | if (alloc_start < alloc_end) { | |
3360 | struct extent_state *cached_state = NULL; | |
3361 | const u64 lockstart = alloc_start; | |
3362 | const u64 lockend = alloc_end - 1; | |
3363 | ||
3364 | bytes_to_reserve = alloc_end - alloc_start; | |
3365 | ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), | |
3366 | bytes_to_reserve); | |
3367 | if (ret < 0) | |
3368 | goto out; | |
3369 | space_reserved = true; | |
f27451f2 FM |
3370 | ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend, |
3371 | &cached_state); | |
3372 | if (ret) | |
3373 | goto out; | |
7661a3e0 | 3374 | ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), &data_reserved, |
a7f8b1c2 | 3375 | alloc_start, bytes_to_reserve); |
4f6a49de NB |
3376 | if (ret) { |
3377 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, | |
3378 | lockend, &cached_state); | |
a7f8b1c2 | 3379 | goto out; |
4f6a49de | 3380 | } |
f27451f2 FM |
3381 | ret = btrfs_prealloc_file_range(inode, mode, alloc_start, |
3382 | alloc_end - alloc_start, | |
3383 | i_blocksize(inode), | |
3384 | offset + len, &alloc_hint); | |
3385 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, | |
3386 | lockend, &cached_state); | |
3387 | /* btrfs_prealloc_file_range releases reserved space on error */ | |
9f13ce74 | 3388 | if (ret) { |
f27451f2 | 3389 | space_reserved = false; |
9f13ce74 FM |
3390 | goto out; |
3391 | } | |
f27451f2 | 3392 | } |
9f13ce74 | 3393 | ret = btrfs_fallocate_update_isize(inode, offset + len, mode); |
f27451f2 FM |
3394 | out: |
3395 | if (ret && space_reserved) | |
25ce28ca | 3396 | btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved, |
f27451f2 FM |
3397 | alloc_start, bytes_to_reserve); |
3398 | extent_changeset_free(data_reserved); | |
3399 | ||
3400 | return ret; | |
3401 | } | |
3402 | ||
2fe17c10 CH |
3403 | static long btrfs_fallocate(struct file *file, int mode, |
3404 | loff_t offset, loff_t len) | |
3405 | { | |
496ad9aa | 3406 | struct inode *inode = file_inode(file); |
2fe17c10 | 3407 | struct extent_state *cached_state = NULL; |
364ecf36 | 3408 | struct extent_changeset *data_reserved = NULL; |
14524a84 QW |
3409 | struct falloc_range *range; |
3410 | struct falloc_range *tmp; | |
3411 | struct list_head reserve_list; | |
2fe17c10 CH |
3412 | u64 cur_offset; |
3413 | u64 last_byte; | |
3414 | u64 alloc_start; | |
3415 | u64 alloc_end; | |
3416 | u64 alloc_hint = 0; | |
3417 | u64 locked_end; | |
14524a84 | 3418 | u64 actual_end = 0; |
47e1d1c7 FM |
3419 | u64 data_space_needed = 0; |
3420 | u64 data_space_reserved = 0; | |
3421 | u64 qgroup_reserved = 0; | |
2fe17c10 | 3422 | struct extent_map *em; |
6fee248d | 3423 | int blocksize = btrfs_inode_sectorsize(BTRFS_I(inode)); |
2fe17c10 CH |
3424 | int ret; |
3425 | ||
f1569c4c NA |
3426 | /* Do not allow fallocate in ZONED mode */ |
3427 | if (btrfs_is_zoned(btrfs_sb(inode->i_sb))) | |
3428 | return -EOPNOTSUPP; | |
3429 | ||
797f4277 MX |
3430 | alloc_start = round_down(offset, blocksize); |
3431 | alloc_end = round_up(offset + len, blocksize); | |
18513091 | 3432 | cur_offset = alloc_start; |
2fe17c10 | 3433 | |
2aaa6655 | 3434 | /* Make sure we aren't being give some crap mode */ |
f27451f2 FM |
3435 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
3436 | FALLOC_FL_ZERO_RANGE)) | |
2fe17c10 CH |
3437 | return -EOPNOTSUPP; |
3438 | ||
2aaa6655 | 3439 | if (mode & FALLOC_FL_PUNCH_HOLE) |
05fd9564 | 3440 | return btrfs_punch_hole(file, offset, len); |
2aaa6655 | 3441 | |
8d9b4a16 | 3442 | btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP); |
2a162ce9 DI |
3443 | |
3444 | if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) { | |
3445 | ret = inode_newsize_ok(inode, offset + len); | |
3446 | if (ret) | |
3447 | goto out; | |
3448 | } | |
2fe17c10 | 3449 | |
05fd9564 DW |
3450 | ret = file_modified(file); |
3451 | if (ret) | |
3452 | goto out; | |
3453 | ||
14524a84 QW |
3454 | /* |
3455 | * TODO: Move these two operations after we have checked | |
3456 | * accurate reserved space, or fallocate can still fail but | |
3457 | * with page truncated or size expanded. | |
3458 | * | |
3459 | * But that's a minor problem and won't do much harm BTW. | |
3460 | */ | |
2fe17c10 | 3461 | if (alloc_start > inode->i_size) { |
b06359a3 | 3462 | ret = btrfs_cont_expand(BTRFS_I(inode), i_size_read(inode), |
a41ad394 | 3463 | alloc_start); |
2fe17c10 CH |
3464 | if (ret) |
3465 | goto out; | |
0f6925fa | 3466 | } else if (offset + len > inode->i_size) { |
a71754fc JB |
3467 | /* |
3468 | * If we are fallocating from the end of the file onward we | |
9703fefe CR |
3469 | * need to zero out the end of the block if i_size lands in the |
3470 | * middle of a block. | |
a71754fc | 3471 | */ |
217f42eb | 3472 | ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0); |
a71754fc JB |
3473 | if (ret) |
3474 | goto out; | |
2fe17c10 CH |
3475 | } |
3476 | ||
a71754fc | 3477 | /* |
ffa8fc60 FM |
3478 | * We have locked the inode at the VFS level (in exclusive mode) and we |
3479 | * have locked the i_mmap_lock lock (in exclusive mode). Now before | |
3480 | * locking the file range, flush all dealloc in the range and wait for | |
3481 | * all ordered extents in the range to complete. After this we can lock | |
3482 | * the file range and, due to the previous locking we did, we know there | |
3483 | * can't be more delalloc or ordered extents in the range. | |
a71754fc | 3484 | */ |
0ef8b726 JB |
3485 | ret = btrfs_wait_ordered_range(inode, alloc_start, |
3486 | alloc_end - alloc_start); | |
3487 | if (ret) | |
3488 | goto out; | |
a71754fc | 3489 | |
f27451f2 FM |
3490 | if (mode & FALLOC_FL_ZERO_RANGE) { |
3491 | ret = btrfs_zero_range(inode, offset, len, mode); | |
8d9b4a16 | 3492 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
f27451f2 FM |
3493 | return ret; |
3494 | } | |
3495 | ||
2fe17c10 | 3496 | locked_end = alloc_end - 1; |
ffa8fc60 FM |
3497 | lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
3498 | &cached_state); | |
2fe17c10 | 3499 | |
14524a84 QW |
3500 | /* First, check if we exceed the qgroup limit */ |
3501 | INIT_LIST_HEAD(&reserve_list); | |
6b7d6e93 | 3502 | while (cur_offset < alloc_end) { |
fc4f21b1 | 3503 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
39b07b5d | 3504 | alloc_end - cur_offset); |
9986277e DC |
3505 | if (IS_ERR(em)) { |
3506 | ret = PTR_ERR(em); | |
79787eaa JM |
3507 | break; |
3508 | } | |
2fe17c10 | 3509 | last_byte = min(extent_map_end(em), alloc_end); |
f1e490a7 | 3510 | actual_end = min_t(u64, extent_map_end(em), offset + len); |
797f4277 | 3511 | last_byte = ALIGN(last_byte, blocksize); |
2fe17c10 CH |
3512 | if (em->block_start == EXTENT_MAP_HOLE || |
3513 | (cur_offset >= inode->i_size && | |
3514 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
47e1d1c7 FM |
3515 | const u64 range_len = last_byte - cur_offset; |
3516 | ||
3517 | ret = add_falloc_range(&reserve_list, cur_offset, range_len); | |
14524a84 QW |
3518 | if (ret < 0) { |
3519 | free_extent_map(em); | |
3520 | break; | |
3d850dd4 | 3521 | } |
7661a3e0 | 3522 | ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), |
47e1d1c7 | 3523 | &data_reserved, cur_offset, range_len); |
be2d253c FM |
3524 | if (ret < 0) { |
3525 | free_extent_map(em); | |
14524a84 | 3526 | break; |
be2d253c | 3527 | } |
47e1d1c7 FM |
3528 | qgroup_reserved += range_len; |
3529 | data_space_needed += range_len; | |
2fe17c10 CH |
3530 | } |
3531 | free_extent_map(em); | |
2fe17c10 | 3532 | cur_offset = last_byte; |
14524a84 QW |
3533 | } |
3534 | ||
47e1d1c7 FM |
3535 | if (!ret && data_space_needed > 0) { |
3536 | /* | |
3537 | * We are safe to reserve space here as we can't have delalloc | |
3538 | * in the range, see above. | |
3539 | */ | |
3540 | ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), | |
3541 | data_space_needed); | |
3542 | if (!ret) | |
3543 | data_space_reserved = data_space_needed; | |
3544 | } | |
3545 | ||
14524a84 QW |
3546 | /* |
3547 | * If ret is still 0, means we're OK to fallocate. | |
3548 | * Or just cleanup the list and exit. | |
3549 | */ | |
3550 | list_for_each_entry_safe(range, tmp, &reserve_list, list) { | |
47e1d1c7 | 3551 | if (!ret) { |
14524a84 QW |
3552 | ret = btrfs_prealloc_file_range(inode, mode, |
3553 | range->start, | |
93407472 | 3554 | range->len, i_blocksize(inode), |
14524a84 | 3555 | offset + len, &alloc_hint); |
47e1d1c7 FM |
3556 | /* |
3557 | * btrfs_prealloc_file_range() releases space even | |
3558 | * if it returns an error. | |
3559 | */ | |
3560 | data_space_reserved -= range->len; | |
3561 | qgroup_reserved -= range->len; | |
3562 | } else if (data_space_reserved > 0) { | |
25ce28ca | 3563 | btrfs_free_reserved_data_space(BTRFS_I(inode), |
47e1d1c7 FM |
3564 | data_reserved, range->start, |
3565 | range->len); | |
3566 | data_space_reserved -= range->len; | |
3567 | qgroup_reserved -= range->len; | |
3568 | } else if (qgroup_reserved > 0) { | |
3569 | btrfs_qgroup_free_data(BTRFS_I(inode), data_reserved, | |
3570 | range->start, range->len); | |
3571 | qgroup_reserved -= range->len; | |
3572 | } | |
14524a84 QW |
3573 | list_del(&range->list); |
3574 | kfree(range); | |
3575 | } | |
3576 | if (ret < 0) | |
3577 | goto out_unlock; | |
3578 | ||
f27451f2 FM |
3579 | /* |
3580 | * We didn't need to allocate any more space, but we still extended the | |
3581 | * size of the file so we need to update i_size and the inode item. | |
3582 | */ | |
3583 | ret = btrfs_fallocate_update_isize(inode, actual_end, mode); | |
14524a84 | 3584 | out_unlock: |
2fe17c10 | 3585 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
e43bbe5e | 3586 | &cached_state); |
2fe17c10 | 3587 | out: |
8d9b4a16 | 3588 | btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP); |
364ecf36 | 3589 | extent_changeset_free(data_reserved); |
2fe17c10 CH |
3590 | return ret; |
3591 | } | |
3592 | ||
cca5de97 | 3593 | static loff_t find_desired_extent(struct btrfs_inode *inode, loff_t offset, |
bc80230e | 3594 | int whence) |
b2675157 | 3595 | { |
cca5de97 | 3596 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
7f4ca37c | 3597 | struct extent_map *em = NULL; |
b2675157 | 3598 | struct extent_state *cached_state = NULL; |
cca5de97 | 3599 | loff_t i_size = inode->vfs_inode.i_size; |
4d1a40c6 LB |
3600 | u64 lockstart; |
3601 | u64 lockend; | |
3602 | u64 start; | |
3603 | u64 len; | |
b2675157 JB |
3604 | int ret = 0; |
3605 | ||
bc80230e | 3606 | if (i_size == 0 || offset >= i_size) |
4d1a40c6 LB |
3607 | return -ENXIO; |
3608 | ||
3609 | /* | |
bc80230e | 3610 | * offset can be negative, in this case we start finding DATA/HOLE from |
4d1a40c6 LB |
3611 | * the very start of the file. |
3612 | */ | |
bc80230e | 3613 | start = max_t(loff_t, 0, offset); |
4d1a40c6 | 3614 | |
0b246afa | 3615 | lockstart = round_down(start, fs_info->sectorsize); |
d79b7c26 | 3616 | lockend = round_up(i_size, fs_info->sectorsize); |
b2675157 | 3617 | if (lockend <= lockstart) |
0b246afa | 3618 | lockend = lockstart + fs_info->sectorsize; |
1214b53f | 3619 | lockend--; |
b2675157 JB |
3620 | len = lockend - lockstart + 1; |
3621 | ||
cca5de97 | 3622 | lock_extent_bits(&inode->io_tree, lockstart, lockend, &cached_state); |
b2675157 | 3623 | |
d79b7c26 | 3624 | while (start < i_size) { |
cca5de97 | 3625 | em = btrfs_get_extent_fiemap(inode, start, len); |
b2675157 | 3626 | if (IS_ERR(em)) { |
6af021d8 | 3627 | ret = PTR_ERR(em); |
7f4ca37c | 3628 | em = NULL; |
b2675157 JB |
3629 | break; |
3630 | } | |
3631 | ||
7f4ca37c JB |
3632 | if (whence == SEEK_HOLE && |
3633 | (em->block_start == EXTENT_MAP_HOLE || | |
3634 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) | |
3635 | break; | |
3636 | else if (whence == SEEK_DATA && | |
3637 | (em->block_start != EXTENT_MAP_HOLE && | |
3638 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) | |
3639 | break; | |
b2675157 JB |
3640 | |
3641 | start = em->start + em->len; | |
b2675157 | 3642 | free_extent_map(em); |
7f4ca37c | 3643 | em = NULL; |
b2675157 JB |
3644 | cond_resched(); |
3645 | } | |
7f4ca37c | 3646 | free_extent_map(em); |
cca5de97 | 3647 | unlock_extent_cached(&inode->io_tree, lockstart, lockend, |
bc80230e NB |
3648 | &cached_state); |
3649 | if (ret) { | |
3650 | offset = ret; | |
3651 | } else { | |
d79b7c26 | 3652 | if (whence == SEEK_DATA && start >= i_size) |
bc80230e | 3653 | offset = -ENXIO; |
7f4ca37c | 3654 | else |
bc80230e | 3655 | offset = min_t(loff_t, start, i_size); |
7f4ca37c | 3656 | } |
bc80230e NB |
3657 | |
3658 | return offset; | |
b2675157 JB |
3659 | } |
3660 | ||
965c8e59 | 3661 | static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence) |
b2675157 JB |
3662 | { |
3663 | struct inode *inode = file->f_mapping->host; | |
b2675157 | 3664 | |
965c8e59 | 3665 | switch (whence) { |
2034f3b4 NB |
3666 | default: |
3667 | return generic_file_llseek(file, offset, whence); | |
b2675157 JB |
3668 | case SEEK_DATA: |
3669 | case SEEK_HOLE: | |
a14b78ad | 3670 | btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED); |
cca5de97 | 3671 | offset = find_desired_extent(BTRFS_I(inode), offset, whence); |
a14b78ad | 3672 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
bc80230e | 3673 | break; |
b2675157 JB |
3674 | } |
3675 | ||
bc80230e NB |
3676 | if (offset < 0) |
3677 | return offset; | |
3678 | ||
2034f3b4 | 3679 | return vfs_setpos(file, offset, inode->i_sb->s_maxbytes); |
b2675157 JB |
3680 | } |
3681 | ||
edf064e7 GR |
3682 | static int btrfs_file_open(struct inode *inode, struct file *filp) |
3683 | { | |
14605409 BB |
3684 | int ret; |
3685 | ||
8730f12b | 3686 | filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC; |
14605409 BB |
3687 | |
3688 | ret = fsverity_file_open(inode, filp); | |
3689 | if (ret) | |
3690 | return ret; | |
edf064e7 GR |
3691 | return generic_file_open(inode, filp); |
3692 | } | |
3693 | ||
4e4cabec GR |
3694 | static int check_direct_read(struct btrfs_fs_info *fs_info, |
3695 | const struct iov_iter *iter, loff_t offset) | |
3696 | { | |
3697 | int ret; | |
3698 | int i, seg; | |
3699 | ||
3700 | ret = check_direct_IO(fs_info, iter, offset); | |
3701 | if (ret < 0) | |
3702 | return ret; | |
3703 | ||
3704 | if (!iter_is_iovec(iter)) | |
3705 | return 0; | |
3706 | ||
3707 | for (seg = 0; seg < iter->nr_segs; seg++) | |
3708 | for (i = seg + 1; i < iter->nr_segs; i++) | |
3709 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
3710 | return -EINVAL; | |
3711 | return 0; | |
3712 | } | |
3713 | ||
3714 | static ssize_t btrfs_direct_read(struct kiocb *iocb, struct iov_iter *to) | |
3715 | { | |
3716 | struct inode *inode = file_inode(iocb->ki_filp); | |
51bd9563 FM |
3717 | size_t prev_left = 0; |
3718 | ssize_t read = 0; | |
4e4cabec GR |
3719 | ssize_t ret; |
3720 | ||
14605409 BB |
3721 | if (fsverity_active(inode)) |
3722 | return 0; | |
3723 | ||
4e4cabec GR |
3724 | if (check_direct_read(btrfs_sb(inode->i_sb), to, iocb->ki_pos)) |
3725 | return 0; | |
3726 | ||
a14b78ad | 3727 | btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED); |
51bd9563 FM |
3728 | again: |
3729 | /* | |
3730 | * This is similar to what we do for direct IO writes, see the comment | |
3731 | * at btrfs_direct_write(), but we also disable page faults in addition | |
3732 | * to disabling them only at the iov_iter level. This is because when | |
3733 | * reading from a hole or prealloc extent, iomap calls iov_iter_zero(), | |
3734 | * which can still trigger page fault ins despite having set ->nofault | |
3735 | * to true of our 'to' iov_iter. | |
3736 | * | |
3737 | * The difference to direct IO writes is that we deadlock when trying | |
3738 | * to lock the extent range in the inode's tree during he page reads | |
3739 | * triggered by the fault in (while for writes it is due to waiting for | |
3740 | * our own ordered extent). This is because for direct IO reads, | |
3741 | * btrfs_dio_iomap_begin() returns with the extent range locked, which | |
3742 | * is only unlocked in the endio callback (end_bio_extent_readpage()). | |
3743 | */ | |
3744 | pagefault_disable(); | |
3745 | to->nofault = true; | |
4fdccaa0 | 3746 | ret = iomap_dio_rw(iocb, to, &btrfs_dio_iomap_ops, &btrfs_dio_ops, |
51bd9563 FM |
3747 | IOMAP_DIO_PARTIAL, read); |
3748 | to->nofault = false; | |
3749 | pagefault_enable(); | |
3750 | ||
3751 | /* No increment (+=) because iomap returns a cumulative value. */ | |
3752 | if (ret > 0) | |
3753 | read = ret; | |
3754 | ||
3755 | if (iov_iter_count(to) > 0 && (ret == -EFAULT || ret > 0)) { | |
3756 | const size_t left = iov_iter_count(to); | |
3757 | ||
3758 | if (left == prev_left) { | |
3759 | /* | |
3760 | * We didn't make any progress since the last attempt, | |
3761 | * fallback to a buffered read for the remainder of the | |
3762 | * range. This is just to avoid any possibility of looping | |
3763 | * for too long. | |
3764 | */ | |
3765 | ret = read; | |
3766 | } else { | |
3767 | /* | |
3768 | * We made some progress since the last retry or this is | |
3769 | * the first time we are retrying. Fault in as many pages | |
3770 | * as possible and retry. | |
3771 | */ | |
3772 | fault_in_iov_iter_writeable(to, left); | |
3773 | prev_left = left; | |
3774 | goto again; | |
3775 | } | |
3776 | } | |
a14b78ad | 3777 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
51bd9563 | 3778 | return ret < 0 ? ret : read; |
4e4cabec GR |
3779 | } |
3780 | ||
f85781fb GR |
3781 | static ssize_t btrfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
3782 | { | |
3783 | ssize_t ret = 0; | |
3784 | ||
3785 | if (iocb->ki_flags & IOCB_DIRECT) { | |
4e4cabec | 3786 | ret = btrfs_direct_read(iocb, to); |
0425e7ba JT |
3787 | if (ret < 0 || !iov_iter_count(to) || |
3788 | iocb->ki_pos >= i_size_read(file_inode(iocb->ki_filp))) | |
f85781fb GR |
3789 | return ret; |
3790 | } | |
3791 | ||
87fa0f3e | 3792 | return filemap_read(iocb, to, ret); |
f85781fb GR |
3793 | } |
3794 | ||
828c0950 | 3795 | const struct file_operations btrfs_file_operations = { |
b2675157 | 3796 | .llseek = btrfs_file_llseek, |
f85781fb | 3797 | .read_iter = btrfs_file_read_iter, |
e9906a98 | 3798 | .splice_read = generic_file_splice_read, |
b30ac0fc | 3799 | .write_iter = btrfs_file_write_iter, |
d7776591 | 3800 | .splice_write = iter_file_splice_write, |
9ebefb18 | 3801 | .mmap = btrfs_file_mmap, |
edf064e7 | 3802 | .open = btrfs_file_open, |
e1b81e67 | 3803 | .release = btrfs_release_file, |
39279cc3 | 3804 | .fsync = btrfs_sync_file, |
2fe17c10 | 3805 | .fallocate = btrfs_fallocate, |
34287aa3 | 3806 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 3807 | #ifdef CONFIG_COMPAT |
4c63c245 | 3808 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 3809 | #endif |
2e5dfc99 | 3810 | .remap_file_range = btrfs_remap_file_range, |
39279cc3 | 3811 | }; |
9247f317 | 3812 | |
e67c718b | 3813 | void __cold btrfs_auto_defrag_exit(void) |
9247f317 | 3814 | { |
5598e900 | 3815 | kmem_cache_destroy(btrfs_inode_defrag_cachep); |
9247f317 MX |
3816 | } |
3817 | ||
f5c29bd9 | 3818 | int __init btrfs_auto_defrag_init(void) |
9247f317 MX |
3819 | { |
3820 | btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag", | |
3821 | sizeof(struct inode_defrag), 0, | |
fba4b697 | 3822 | SLAB_MEM_SPREAD, |
9247f317 MX |
3823 | NULL); |
3824 | if (!btrfs_inode_defrag_cachep) | |
3825 | return -ENOMEM; | |
3826 | ||
3827 | return 0; | |
3828 | } | |
728404da FM |
3829 | |
3830 | int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end) | |
3831 | { | |
3832 | int ret; | |
3833 | ||
3834 | /* | |
3835 | * So with compression we will find and lock a dirty page and clear the | |
3836 | * first one as dirty, setup an async extent, and immediately return | |
3837 | * with the entire range locked but with nobody actually marked with | |
3838 | * writeback. So we can't just filemap_write_and_wait_range() and | |
3839 | * expect it to work since it will just kick off a thread to do the | |
3840 | * actual work. So we need to call filemap_fdatawrite_range _again_ | |
3841 | * since it will wait on the page lock, which won't be unlocked until | |
3842 | * after the pages have been marked as writeback and so we're good to go | |
3843 | * from there. We have to do this otherwise we'll miss the ordered | |
3844 | * extents and that results in badness. Please Josef, do not think you | |
3845 | * know better and pull this out at some point in the future, it is | |
3846 | * right and you are wrong. | |
3847 | */ | |
3848 | ret = filemap_fdatawrite_range(inode->i_mapping, start, end); | |
3849 | if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
3850 | &BTRFS_I(inode)->runtime_flags)) | |
3851 | ret = filemap_fdatawrite_range(inode->i_mapping, start, end); | |
3852 | ||
3853 | return ret; | |
3854 | } |