]>
Commit | Line | Data |
---|---|---|
6a177381 FM |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
05a5a762 | 3 | #include <linux/blkdev.h> |
6a177381 | 4 | #include <linux/iversion.h> |
05a5a762 | 5 | #include "compression.h" |
6a177381 | 6 | #include "ctree.h" |
05a5a762 | 7 | #include "delalloc-space.h" |
6a177381 FM |
8 | #include "reflink.h" |
9 | #include "transaction.h" | |
10 | ||
11 | #define BTRFS_MAX_DEDUPE_LEN SZ_16M | |
12 | ||
13 | static int clone_finish_inode_update(struct btrfs_trans_handle *trans, | |
14 | struct inode *inode, | |
15 | u64 endoff, | |
16 | const u64 destoff, | |
17 | const u64 olen, | |
18 | int no_time_update) | |
19 | { | |
20 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
21 | int ret; | |
22 | ||
23 | inode_inc_iversion(inode); | |
24 | if (!no_time_update) | |
25 | inode->i_mtime = inode->i_ctime = current_time(inode); | |
26 | /* | |
27 | * We round up to the block size at eof when determining which | |
28 | * extents to clone above, but shouldn't round up the file size. | |
29 | */ | |
30 | if (endoff > destoff + olen) | |
31 | endoff = destoff + olen; | |
32 | if (endoff > inode->i_size) { | |
33 | i_size_write(inode, endoff); | |
76aea537 | 34 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
6a177381 FM |
35 | } |
36 | ||
9a56fcd1 | 37 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
6a177381 FM |
38 | if (ret) { |
39 | btrfs_abort_transaction(trans, ret); | |
40 | btrfs_end_transaction(trans); | |
41 | goto out; | |
42 | } | |
43 | ret = btrfs_end_transaction(trans); | |
44 | out: | |
45 | return ret; | |
46 | } | |
47 | ||
998acfe8 | 48 | static int copy_inline_to_page(struct btrfs_inode *inode, |
05a5a762 FM |
49 | const u64 file_offset, |
50 | char *inline_data, | |
51 | const u64 size, | |
52 | const u64 datal, | |
53 | const u8 comp_type) | |
54 | { | |
998acfe8 | 55 | const u64 block_size = btrfs_inode_sectorsize(inode); |
05a5a762 FM |
56 | const u64 range_end = file_offset + block_size - 1; |
57 | const size_t inline_size = size - btrfs_file_extent_calc_inline_size(0); | |
58 | char *data_start = inline_data + btrfs_file_extent_calc_inline_size(0); | |
59 | struct extent_changeset *data_reserved = NULL; | |
60 | struct page *page = NULL; | |
998acfe8 | 61 | struct address_space *mapping = inode->vfs_inode.i_mapping; |
05a5a762 FM |
62 | int ret; |
63 | ||
64 | ASSERT(IS_ALIGNED(file_offset, block_size)); | |
65 | ||
66 | /* | |
67 | * We have flushed and locked the ranges of the source and destination | |
68 | * inodes, we also have locked the inodes, so we are safe to do a | |
69 | * reservation here. Also we must not do the reservation while holding | |
70 | * a transaction open, otherwise we would deadlock. | |
71 | */ | |
998acfe8 NB |
72 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, file_offset, |
73 | block_size); | |
05a5a762 FM |
74 | if (ret) |
75 | goto out; | |
76 | ||
998acfe8 NB |
77 | page = find_or_create_page(mapping, file_offset >> PAGE_SHIFT, |
78 | btrfs_alloc_write_mask(mapping)); | |
05a5a762 FM |
79 | if (!page) { |
80 | ret = -ENOMEM; | |
81 | goto out_unlock; | |
82 | } | |
83 | ||
84 | set_page_extent_mapped(page); | |
998acfe8 | 85 | clear_extent_bit(&inode->io_tree, file_offset, range_end, |
05a5a762 FM |
86 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
87 | 0, 0, NULL); | |
998acfe8 | 88 | ret = btrfs_set_extent_delalloc(inode, file_offset, range_end, 0, NULL); |
05a5a762 FM |
89 | if (ret) |
90 | goto out_unlock; | |
91 | ||
3d45f221 FM |
92 | /* |
93 | * After dirtying the page our caller will need to start a transaction, | |
94 | * and if we are low on metadata free space, that can cause flushing of | |
95 | * delalloc for all inodes in order to get metadata space released. | |
96 | * However we are holding the range locked for the whole duration of | |
97 | * the clone/dedupe operation, so we may deadlock if that happens and no | |
98 | * other task releases enough space. So mark this inode as not being | |
99 | * possible to flush to avoid such deadlock. We will clear that flag | |
100 | * when we finish cloning all extents, since a transaction is started | |
101 | * after finding each extent to clone. | |
102 | */ | |
103 | set_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &inode->runtime_flags); | |
104 | ||
05a5a762 FM |
105 | if (comp_type == BTRFS_COMPRESS_NONE) { |
106 | char *map; | |
107 | ||
108 | map = kmap(page); | |
109 | memcpy(map, data_start, datal); | |
110 | flush_dcache_page(page); | |
111 | kunmap(page); | |
112 | } else { | |
113 | ret = btrfs_decompress(comp_type, data_start, page, 0, | |
114 | inline_size, datal); | |
115 | if (ret) | |
116 | goto out_unlock; | |
117 | flush_dcache_page(page); | |
118 | } | |
119 | ||
120 | /* | |
121 | * If our inline data is smaller then the block/page size, then the | |
122 | * remaining of the block/page is equivalent to zeroes. We had something | |
123 | * like the following done: | |
124 | * | |
125 | * $ xfs_io -f -c "pwrite -S 0xab 0 500" file | |
126 | * $ sync # (or fsync) | |
127 | * $ xfs_io -c "falloc 0 4K" file | |
128 | * $ xfs_io -c "pwrite -S 0xcd 4K 4K" | |
129 | * | |
130 | * So what's in the range [500, 4095] corresponds to zeroes. | |
131 | */ | |
132 | if (datal < block_size) { | |
133 | char *map; | |
134 | ||
135 | map = kmap(page); | |
136 | memset(map + datal, 0, block_size - datal); | |
137 | flush_dcache_page(page); | |
138 | kunmap(page); | |
139 | } | |
140 | ||
141 | SetPageUptodate(page); | |
142 | ClearPageChecked(page); | |
143 | set_page_dirty(page); | |
144 | out_unlock: | |
145 | if (page) { | |
146 | unlock_page(page); | |
147 | put_page(page); | |
148 | } | |
149 | if (ret) | |
998acfe8 NB |
150 | btrfs_delalloc_release_space(inode, data_reserved, file_offset, |
151 | block_size, true); | |
152 | btrfs_delalloc_release_extents(inode, block_size); | |
05a5a762 FM |
153 | out: |
154 | extent_changeset_free(data_reserved); | |
155 | ||
156 | return ret; | |
157 | } | |
158 | ||
6a177381 | 159 | /* |
05a5a762 FM |
160 | * Deal with cloning of inline extents. We try to copy the inline extent from |
161 | * the source inode to destination inode when possible. When not possible we | |
162 | * copy the inline extent's data into the respective page of the inode. | |
6a177381 FM |
163 | */ |
164 | static int clone_copy_inline_extent(struct inode *dst, | |
6a177381 FM |
165 | struct btrfs_path *path, |
166 | struct btrfs_key *new_key, | |
167 | const u64 drop_start, | |
168 | const u64 datal, | |
6a177381 | 169 | const u64 size, |
05a5a762 FM |
170 | const u8 comp_type, |
171 | char *inline_data, | |
172 | struct btrfs_trans_handle **trans_out) | |
6a177381 FM |
173 | { |
174 | struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb); | |
175 | struct btrfs_root *root = BTRFS_I(dst)->root; | |
176 | const u64 aligned_end = ALIGN(new_key->offset + datal, | |
177 | fs_info->sectorsize); | |
05a5a762 | 178 | struct btrfs_trans_handle *trans = NULL; |
5893dfb9 | 179 | struct btrfs_drop_extents_args drop_args = { 0 }; |
6a177381 FM |
180 | int ret; |
181 | struct btrfs_key key; | |
182 | ||
05a5a762 | 183 | if (new_key->offset > 0) { |
998acfe8 NB |
184 | ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset, |
185 | inline_data, size, datal, comp_type); | |
05a5a762 FM |
186 | goto out; |
187 | } | |
6a177381 FM |
188 | |
189 | key.objectid = btrfs_ino(BTRFS_I(dst)); | |
190 | key.type = BTRFS_EXTENT_DATA_KEY; | |
191 | key.offset = 0; | |
192 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
193 | if (ret < 0) { | |
194 | return ret; | |
195 | } else if (ret > 0) { | |
196 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | |
197 | ret = btrfs_next_leaf(root, path); | |
198 | if (ret < 0) | |
199 | return ret; | |
200 | else if (ret > 0) | |
201 | goto copy_inline_extent; | |
202 | } | |
203 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
204 | if (key.objectid == btrfs_ino(BTRFS_I(dst)) && | |
205 | key.type == BTRFS_EXTENT_DATA_KEY) { | |
05a5a762 FM |
206 | /* |
207 | * There's an implicit hole at file offset 0, copy the | |
208 | * inline extent's data to the page. | |
209 | */ | |
6a177381 | 210 | ASSERT(key.offset > 0); |
c43575db | 211 | goto copy_to_page; |
6a177381 FM |
212 | } |
213 | } else if (i_size_read(dst) <= datal) { | |
214 | struct btrfs_file_extent_item *ei; | |
6a177381 | 215 | |
6a177381 FM |
216 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
217 | struct btrfs_file_extent_item); | |
218 | /* | |
05a5a762 FM |
219 | * If it's an inline extent replace it with the source inline |
220 | * extent, otherwise copy the source inline extent data into | |
221 | * the respective page at the destination inode. | |
6a177381 FM |
222 | */ |
223 | if (btrfs_file_extent_type(path->nodes[0], ei) == | |
224 | BTRFS_FILE_EXTENT_INLINE) | |
225 | goto copy_inline_extent; | |
226 | ||
c43575db | 227 | goto copy_to_page; |
6a177381 FM |
228 | } |
229 | ||
230 | copy_inline_extent: | |
231 | /* | |
232 | * We have no extent items, or we have an extent at offset 0 which may | |
233 | * or may not be inlined. All these cases are dealt the same way. | |
234 | */ | |
235 | if (i_size_read(dst) > datal) { | |
236 | /* | |
05a5a762 FM |
237 | * At the destination offset 0 we have either a hole, a regular |
238 | * extent or an inline extent larger then the one we want to | |
239 | * clone. Deal with all these cases by copying the inline extent | |
240 | * data into the respective page at the destination inode. | |
6a177381 | 241 | */ |
c43575db | 242 | goto copy_to_page; |
6a177381 FM |
243 | } |
244 | ||
c43575db FM |
245 | /* |
246 | * Release path before starting a new transaction so we don't hold locks | |
247 | * that would confuse lockdep. | |
248 | */ | |
6a177381 | 249 | btrfs_release_path(path); |
05a5a762 FM |
250 | /* |
251 | * If we end up here it means were copy the inline extent into a leaf | |
252 | * of the destination inode. We know we will drop or adjust at most one | |
253 | * extent item in the destination root. | |
254 | * | |
255 | * 1 unit - adjusting old extent (we may have to split it) | |
256 | * 1 unit - add new extent | |
257 | * 1 unit - inode update | |
258 | */ | |
259 | trans = btrfs_start_transaction(root, 3); | |
260 | if (IS_ERR(trans)) { | |
261 | ret = PTR_ERR(trans); | |
262 | trans = NULL; | |
263 | goto out; | |
264 | } | |
5893dfb9 FM |
265 | drop_args.path = path; |
266 | drop_args.start = drop_start; | |
267 | drop_args.end = aligned_end; | |
268 | drop_args.drop_cache = true; | |
269 | ret = btrfs_drop_extents(trans, root, BTRFS_I(dst), &drop_args); | |
6a177381 | 270 | if (ret) |
05a5a762 | 271 | goto out; |
6a177381 FM |
272 | ret = btrfs_insert_empty_item(trans, root, path, new_key, size); |
273 | if (ret) | |
05a5a762 | 274 | goto out; |
6a177381 | 275 | |
6a177381 FM |
276 | write_extent_buffer(path->nodes[0], inline_data, |
277 | btrfs_item_ptr_offset(path->nodes[0], | |
278 | path->slots[0]), | |
279 | size); | |
2766ff61 | 280 | btrfs_update_inode_bytes(BTRFS_I(dst), datal, drop_args.bytes_found); |
6a177381 | 281 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags); |
4fdb688c | 282 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(dst), 0, aligned_end); |
05a5a762 FM |
283 | out: |
284 | if (!ret && !trans) { | |
285 | /* | |
286 | * No transaction here means we copied the inline extent into a | |
287 | * page of the destination inode. | |
288 | * | |
289 | * 1 unit to update inode item | |
290 | */ | |
291 | trans = btrfs_start_transaction(root, 1); | |
292 | if (IS_ERR(trans)) { | |
293 | ret = PTR_ERR(trans); | |
294 | trans = NULL; | |
295 | } | |
296 | } | |
297 | if (ret && trans) { | |
298 | btrfs_abort_transaction(trans, ret); | |
299 | btrfs_end_transaction(trans); | |
300 | } | |
301 | if (!ret) | |
302 | *trans_out = trans; | |
6a177381 | 303 | |
05a5a762 | 304 | return ret; |
c43575db FM |
305 | |
306 | copy_to_page: | |
307 | /* | |
308 | * Release our path because we don't need it anymore and also because | |
309 | * copy_inline_to_page() needs to reserve data and metadata, which may | |
310 | * need to flush delalloc when we are low on available space and | |
311 | * therefore cause a deadlock if writeback of an inline extent needs to | |
312 | * write to the same leaf or an ordered extent completion needs to write | |
313 | * to the same leaf. | |
314 | */ | |
315 | btrfs_release_path(path); | |
316 | ||
317 | ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset, | |
318 | inline_data, size, datal, comp_type); | |
319 | goto out; | |
6a177381 FM |
320 | } |
321 | ||
322 | /** | |
323 | * btrfs_clone() - clone a range from inode file to another | |
324 | * | |
325 | * @src: Inode to clone from | |
326 | * @inode: Inode to clone to | |
327 | * @off: Offset within source to start clone from | |
328 | * @olen: Original length, passed by user, of range to clone | |
329 | * @olen_aligned: Block-aligned value of olen | |
330 | * @destoff: Offset within @inode to start clone | |
331 | * @no_time_update: Whether to update mtime/ctime on the target inode | |
332 | */ | |
333 | static int btrfs_clone(struct inode *src, struct inode *inode, | |
334 | const u64 off, const u64 olen, const u64 olen_aligned, | |
335 | const u64 destoff, int no_time_update) | |
336 | { | |
337 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
6a177381 FM |
338 | struct btrfs_path *path = NULL; |
339 | struct extent_buffer *leaf; | |
340 | struct btrfs_trans_handle *trans; | |
341 | char *buf = NULL; | |
342 | struct btrfs_key key; | |
343 | u32 nritems; | |
344 | int slot; | |
345 | int ret; | |
346 | const u64 len = olen_aligned; | |
347 | u64 last_dest_end = destoff; | |
348 | ||
349 | ret = -ENOMEM; | |
350 | buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); | |
351 | if (!buf) | |
352 | return ret; | |
353 | ||
354 | path = btrfs_alloc_path(); | |
355 | if (!path) { | |
356 | kvfree(buf); | |
357 | return ret; | |
358 | } | |
359 | ||
360 | path->reada = READA_FORWARD; | |
361 | /* Clone data */ | |
362 | key.objectid = btrfs_ino(BTRFS_I(src)); | |
363 | key.type = BTRFS_EXTENT_DATA_KEY; | |
364 | key.offset = off; | |
365 | ||
366 | while (1) { | |
367 | u64 next_key_min_offset = key.offset + 1; | |
368 | struct btrfs_file_extent_item *extent; | |
3ebac17c | 369 | u64 extent_gen; |
6a177381 FM |
370 | int type; |
371 | u32 size; | |
372 | struct btrfs_key new_key; | |
373 | u64 disko = 0, diskl = 0; | |
374 | u64 datao = 0, datal = 0; | |
05a5a762 | 375 | u8 comp; |
6a177381 FM |
376 | u64 drop_start; |
377 | ||
378 | /* Note the key will change type as we walk through the tree */ | |
6a177381 FM |
379 | ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, |
380 | 0, 0); | |
381 | if (ret < 0) | |
382 | goto out; | |
383 | /* | |
384 | * First search, if no extent item that starts at offset off was | |
385 | * found but the previous item is an extent item, it's possible | |
386 | * it might overlap our target range, therefore process it. | |
387 | */ | |
388 | if (key.offset == off && ret > 0 && path->slots[0] > 0) { | |
389 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
390 | path->slots[0] - 1); | |
391 | if (key.type == BTRFS_EXTENT_DATA_KEY) | |
392 | path->slots[0]--; | |
393 | } | |
394 | ||
395 | nritems = btrfs_header_nritems(path->nodes[0]); | |
396 | process_slot: | |
397 | if (path->slots[0] >= nritems) { | |
398 | ret = btrfs_next_leaf(BTRFS_I(src)->root, path); | |
399 | if (ret < 0) | |
400 | goto out; | |
401 | if (ret > 0) | |
402 | break; | |
403 | nritems = btrfs_header_nritems(path->nodes[0]); | |
404 | } | |
405 | leaf = path->nodes[0]; | |
406 | slot = path->slots[0]; | |
407 | ||
408 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
409 | if (key.type > BTRFS_EXTENT_DATA_KEY || | |
410 | key.objectid != btrfs_ino(BTRFS_I(src))) | |
411 | break; | |
412 | ||
413 | ASSERT(key.type == BTRFS_EXTENT_DATA_KEY); | |
414 | ||
415 | extent = btrfs_item_ptr(leaf, slot, | |
416 | struct btrfs_file_extent_item); | |
3ebac17c | 417 | extent_gen = btrfs_file_extent_generation(leaf, extent); |
05a5a762 | 418 | comp = btrfs_file_extent_compression(leaf, extent); |
6a177381 FM |
419 | type = btrfs_file_extent_type(leaf, extent); |
420 | if (type == BTRFS_FILE_EXTENT_REG || | |
421 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
422 | disko = btrfs_file_extent_disk_bytenr(leaf, extent); | |
423 | diskl = btrfs_file_extent_disk_num_bytes(leaf, extent); | |
424 | datao = btrfs_file_extent_offset(leaf, extent); | |
425 | datal = btrfs_file_extent_num_bytes(leaf, extent); | |
426 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { | |
427 | /* Take upper bound, may be compressed */ | |
428 | datal = btrfs_file_extent_ram_bytes(leaf, extent); | |
429 | } | |
430 | ||
431 | /* | |
432 | * The first search might have left us at an extent item that | |
433 | * ends before our target range's start, can happen if we have | |
434 | * holes and NO_HOLES feature enabled. | |
435 | */ | |
436 | if (key.offset + datal <= off) { | |
437 | path->slots[0]++; | |
438 | goto process_slot; | |
439 | } else if (key.offset >= off + len) { | |
440 | break; | |
441 | } | |
442 | next_key_min_offset = key.offset + datal; | |
443 | size = btrfs_item_size_nr(leaf, slot); | |
444 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot), | |
445 | size); | |
446 | ||
447 | btrfs_release_path(path); | |
6a177381 FM |
448 | |
449 | memcpy(&new_key, &key, sizeof(new_key)); | |
450 | new_key.objectid = btrfs_ino(BTRFS_I(inode)); | |
451 | if (off <= key.offset) | |
452 | new_key.offset = key.offset + destoff - off; | |
453 | else | |
454 | new_key.offset = destoff; | |
455 | ||
456 | /* | |
457 | * Deal with a hole that doesn't have an extent item that | |
458 | * represents it (NO_HOLES feature enabled). | |
459 | * This hole is either in the middle of the cloning range or at | |
460 | * the beginning (fully overlaps it or partially overlaps it). | |
461 | */ | |
462 | if (new_key.offset != last_dest_end) | |
463 | drop_start = last_dest_end; | |
464 | else | |
465 | drop_start = new_key.offset; | |
466 | ||
467 | if (type == BTRFS_FILE_EXTENT_REG || | |
468 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
bf385648 | 469 | struct btrfs_replace_extent_info clone_info; |
6a177381 FM |
470 | |
471 | /* | |
472 | * a | --- range to clone ---| b | |
473 | * | ------------- extent ------------- | | |
474 | */ | |
475 | ||
476 | /* Subtract range b */ | |
477 | if (key.offset + datal > off + len) | |
478 | datal = off + len - key.offset; | |
479 | ||
480 | /* Subtract range a */ | |
481 | if (off > key.offset) { | |
482 | datao += off - key.offset; | |
483 | datal -= off - key.offset; | |
484 | } | |
485 | ||
486 | clone_info.disk_offset = disko; | |
487 | clone_info.disk_len = diskl; | |
488 | clone_info.data_offset = datao; | |
489 | clone_info.data_len = datal; | |
490 | clone_info.file_offset = new_key.offset; | |
491 | clone_info.extent_buf = buf; | |
8fccebfa | 492 | clone_info.is_new_extent = false; |
306bfec0 | 493 | ret = btrfs_replace_file_extents(inode, path, drop_start, |
6a177381 FM |
494 | new_key.offset + datal - 1, &clone_info, |
495 | &trans); | |
496 | if (ret) | |
497 | goto out; | |
498 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { | |
a61e1e0d FM |
499 | /* |
500 | * Inline extents always have to start at file offset 0 | |
501 | * and can never be bigger then the sector size. We can | |
502 | * never clone only parts of an inline extent, since all | |
503 | * reflink operations must start at a sector size aligned | |
504 | * offset, and the length must be aligned too or end at | |
505 | * the i_size (which implies the whole inlined data). | |
506 | */ | |
507 | ASSERT(key.offset == 0); | |
508 | ASSERT(datal <= fs_info->sectorsize); | |
509 | if (key.offset != 0 || datal > fs_info->sectorsize) | |
510 | return -EUCLEAN; | |
6a177381 | 511 | |
05a5a762 FM |
512 | ret = clone_copy_inline_extent(inode, path, &new_key, |
513 | drop_start, datal, size, | |
514 | comp, buf, &trans); | |
515 | if (ret) | |
6a177381 | 516 | goto out; |
6a177381 FM |
517 | } |
518 | ||
519 | btrfs_release_path(path); | |
520 | ||
3ebac17c FM |
521 | /* |
522 | * If this is a new extent update the last_reflink_trans of both | |
523 | * inodes. This is used by fsync to make sure it does not log | |
524 | * multiple checksum items with overlapping ranges. For older | |
525 | * extents we don't need to do it since inode logging skips the | |
526 | * checksums for older extents. Also ignore holes and inline | |
527 | * extents because they don't have checksums in the csum tree. | |
528 | */ | |
529 | if (extent_gen == trans->transid && disko > 0) { | |
530 | BTRFS_I(src)->last_reflink_trans = trans->transid; | |
531 | BTRFS_I(inode)->last_reflink_trans = trans->transid; | |
532 | } | |
533 | ||
6a177381 FM |
534 | last_dest_end = ALIGN(new_key.offset + datal, |
535 | fs_info->sectorsize); | |
536 | ret = clone_finish_inode_update(trans, inode, last_dest_end, | |
537 | destoff, olen, no_time_update); | |
538 | if (ret) | |
539 | goto out; | |
540 | if (new_key.offset + datal >= destoff + len) | |
541 | break; | |
542 | ||
543 | btrfs_release_path(path); | |
544 | key.offset = next_key_min_offset; | |
545 | ||
546 | if (fatal_signal_pending(current)) { | |
547 | ret = -EINTR; | |
548 | goto out; | |
549 | } | |
6b613cc9 JT |
550 | |
551 | cond_resched(); | |
6a177381 FM |
552 | } |
553 | ret = 0; | |
554 | ||
555 | if (last_dest_end < destoff + len) { | |
556 | /* | |
557 | * We have an implicit hole that fully or partially overlaps our | |
558 | * cloning range at its end. This means that we either have the | |
559 | * NO_HOLES feature enabled or the implicit hole happened due to | |
560 | * mixing buffered and direct IO writes against this file. | |
561 | */ | |
562 | btrfs_release_path(path); | |
6a177381 | 563 | |
348f0ca3 FM |
564 | /* |
565 | * When using NO_HOLES and we are cloning a range that covers | |
566 | * only a hole (no extents) into a range beyond the current | |
567 | * i_size, punching a hole in the target range will not create | |
568 | * an extent map defining a hole, because the range starts at or | |
569 | * beyond current i_size. If the file previously had an i_size | |
570 | * greater than the new i_size set by this clone operation, we | |
571 | * need to make sure the next fsync is a full fsync, so that it | |
572 | * detects and logs a hole covering a range from the current | |
573 | * i_size to the new i_size. If the clone range covers extents, | |
574 | * besides a hole, then we know the full sync flag was already | |
575 | * set by previous calls to btrfs_replace_file_extents() that | |
576 | * replaced file extent items. | |
577 | */ | |
578 | if (last_dest_end >= i_size_read(inode)) | |
579 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
580 | &BTRFS_I(inode)->runtime_flags); | |
581 | ||
306bfec0 | 582 | ret = btrfs_replace_file_extents(inode, path, last_dest_end, |
6a177381 FM |
583 | destoff + len - 1, NULL, &trans); |
584 | if (ret) | |
585 | goto out; | |
586 | ||
587 | ret = clone_finish_inode_update(trans, inode, destoff + len, | |
588 | destoff, olen, no_time_update); | |
589 | } | |
590 | ||
591 | out: | |
592 | btrfs_free_path(path); | |
593 | kvfree(buf); | |
3d45f221 FM |
594 | clear_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &BTRFS_I(inode)->runtime_flags); |
595 | ||
6a177381 FM |
596 | return ret; |
597 | } | |
598 | ||
599 | static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, | |
600 | struct inode *inode2, u64 loff2, u64 len) | |
601 | { | |
602 | unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); | |
603 | unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); | |
604 | } | |
605 | ||
606 | static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1, | |
607 | struct inode *inode2, u64 loff2, u64 len) | |
608 | { | |
609 | if (inode1 < inode2) { | |
610 | swap(inode1, inode2); | |
611 | swap(loff1, loff2); | |
612 | } else if (inode1 == inode2 && loff2 < loff1) { | |
613 | swap(loff1, loff2); | |
614 | } | |
615 | lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); | |
616 | lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); | |
617 | } | |
618 | ||
619 | static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len, | |
620 | struct inode *dst, u64 dst_loff) | |
621 | { | |
622 | const u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; | |
623 | int ret; | |
624 | ||
625 | /* | |
626 | * Lock destination range to serialize with concurrent readpages() and | |
627 | * source range to serialize with relocation. | |
628 | */ | |
629 | btrfs_double_extent_lock(src, loff, dst, dst_loff, len); | |
630 | ret = btrfs_clone(src, dst, loff, len, ALIGN(len, bs), dst_loff, 1); | |
631 | btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); | |
632 | ||
633 | return ret; | |
634 | } | |
635 | ||
636 | static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, | |
637 | struct inode *dst, u64 dst_loff) | |
638 | { | |
639 | int ret; | |
640 | u64 i, tail_len, chunk_count; | |
641 | struct btrfs_root *root_dst = BTRFS_I(dst)->root; | |
642 | ||
643 | spin_lock(&root_dst->root_item_lock); | |
644 | if (root_dst->send_in_progress) { | |
645 | btrfs_warn_rl(root_dst->fs_info, | |
646 | "cannot deduplicate to root %llu while send operations are using it (%d in progress)", | |
647 | root_dst->root_key.objectid, | |
648 | root_dst->send_in_progress); | |
649 | spin_unlock(&root_dst->root_item_lock); | |
650 | return -EAGAIN; | |
651 | } | |
652 | root_dst->dedupe_in_progress++; | |
653 | spin_unlock(&root_dst->root_item_lock); | |
654 | ||
655 | tail_len = olen % BTRFS_MAX_DEDUPE_LEN; | |
656 | chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN); | |
657 | ||
658 | for (i = 0; i < chunk_count; i++) { | |
659 | ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN, | |
660 | dst, dst_loff); | |
661 | if (ret) | |
662 | goto out; | |
663 | ||
664 | loff += BTRFS_MAX_DEDUPE_LEN; | |
665 | dst_loff += BTRFS_MAX_DEDUPE_LEN; | |
666 | } | |
667 | ||
668 | if (tail_len > 0) | |
669 | ret = btrfs_extent_same_range(src, loff, tail_len, dst, dst_loff); | |
670 | out: | |
671 | spin_lock(&root_dst->root_item_lock); | |
672 | root_dst->dedupe_in_progress--; | |
673 | spin_unlock(&root_dst->root_item_lock); | |
674 | ||
675 | return ret; | |
676 | } | |
677 | ||
678 | static noinline int btrfs_clone_files(struct file *file, struct file *file_src, | |
679 | u64 off, u64 olen, u64 destoff) | |
680 | { | |
681 | struct inode *inode = file_inode(file); | |
682 | struct inode *src = file_inode(file_src); | |
683 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
684 | int ret; | |
05a5a762 | 685 | int wb_ret; |
6a177381 FM |
686 | u64 len = olen; |
687 | u64 bs = fs_info->sb->s_blocksize; | |
688 | ||
6a177381 FM |
689 | /* |
690 | * VFS's generic_remap_file_range_prep() protects us from cloning the | |
691 | * eof block into the middle of a file, which would result in corruption | |
692 | * if the file size is not blocksize aligned. So we don't need to check | |
693 | * for that case here. | |
694 | */ | |
695 | if (off + len == src->i_size) | |
696 | len = ALIGN(src->i_size, bs) - off; | |
697 | ||
698 | if (destoff > inode->i_size) { | |
699 | const u64 wb_start = ALIGN_DOWN(inode->i_size, bs); | |
700 | ||
b06359a3 | 701 | ret = btrfs_cont_expand(BTRFS_I(inode), inode->i_size, destoff); |
6a177381 FM |
702 | if (ret) |
703 | return ret; | |
704 | /* | |
705 | * We may have truncated the last block if the inode's size is | |
706 | * not sector size aligned, so we need to wait for writeback to | |
707 | * complete before proceeding further, otherwise we can race | |
708 | * with cloning and attempt to increment a reference to an | |
709 | * extent that no longer exists (writeback completed right after | |
710 | * we found the previous extent covering eof and before we | |
711 | * attempted to increment its reference count). | |
712 | */ | |
713 | ret = btrfs_wait_ordered_range(inode, wb_start, | |
714 | destoff - wb_start); | |
715 | if (ret) | |
716 | return ret; | |
717 | } | |
718 | ||
719 | /* | |
720 | * Lock destination range to serialize with concurrent readpages() and | |
721 | * source range to serialize with relocation. | |
722 | */ | |
723 | btrfs_double_extent_lock(src, off, inode, destoff, len); | |
724 | ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); | |
725 | btrfs_double_extent_unlock(src, off, inode, destoff, len); | |
05a5a762 FM |
726 | |
727 | /* | |
728 | * We may have copied an inline extent into a page of the destination | |
729 | * range, so wait for writeback to complete before truncating pages | |
730 | * from the page cache. This is a rare case. | |
731 | */ | |
732 | wb_ret = btrfs_wait_ordered_range(inode, destoff, len); | |
733 | ret = ret ? ret : wb_ret; | |
6a177381 FM |
734 | /* |
735 | * Truncate page cache pages so that future reads will see the cloned | |
736 | * data immediately and not the previous data. | |
737 | */ | |
738 | truncate_inode_pages_range(&inode->i_data, | |
739 | round_down(destoff, PAGE_SIZE), | |
740 | round_up(destoff + len, PAGE_SIZE) - 1); | |
741 | ||
742 | return ret; | |
743 | } | |
744 | ||
745 | static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in, | |
746 | struct file *file_out, loff_t pos_out, | |
747 | loff_t *len, unsigned int remap_flags) | |
748 | { | |
749 | struct inode *inode_in = file_inode(file_in); | |
750 | struct inode *inode_out = file_inode(file_out); | |
751 | u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize; | |
752 | bool same_inode = inode_out == inode_in; | |
753 | u64 wb_len; | |
754 | int ret; | |
755 | ||
756 | if (!(remap_flags & REMAP_FILE_DEDUP)) { | |
757 | struct btrfs_root *root_out = BTRFS_I(inode_out)->root; | |
758 | ||
759 | if (btrfs_root_readonly(root_out)) | |
760 | return -EROFS; | |
761 | ||
762 | if (file_in->f_path.mnt != file_out->f_path.mnt || | |
763 | inode_in->i_sb != inode_out->i_sb) | |
764 | return -EXDEV; | |
765 | } | |
766 | ||
767 | /* Don't make the dst file partly checksummed */ | |
768 | if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) != | |
769 | (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) { | |
770 | return -EINVAL; | |
771 | } | |
772 | ||
773 | /* | |
774 | * Now that the inodes are locked, we need to start writeback ourselves | |
775 | * and can not rely on the writeback from the VFS's generic helper | |
776 | * generic_remap_file_range_prep() because: | |
777 | * | |
778 | * 1) For compression we must call filemap_fdatawrite_range() range | |
779 | * twice (btrfs_fdatawrite_range() does it for us), and the generic | |
780 | * helper only calls it once; | |
781 | * | |
782 | * 2) filemap_fdatawrite_range(), called by the generic helper only | |
783 | * waits for the writeback to complete, i.e. for IO to be done, and | |
784 | * not for the ordered extents to complete. We need to wait for them | |
785 | * to complete so that new file extent items are in the fs tree. | |
786 | */ | |
787 | if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP)) | |
788 | wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs); | |
789 | else | |
790 | wb_len = ALIGN(*len, bs); | |
791 | ||
792 | /* | |
793 | * Since we don't lock ranges, wait for ongoing lockless dio writes (as | |
794 | * any in progress could create its ordered extents after we wait for | |
795 | * existing ordered extents below). | |
796 | */ | |
797 | inode_dio_wait(inode_in); | |
798 | if (!same_inode) | |
799 | inode_dio_wait(inode_out); | |
800 | ||
801 | /* | |
802 | * Workaround to make sure NOCOW buffered write reach disk as NOCOW. | |
803 | * | |
804 | * Btrfs' back references do not have a block level granularity, they | |
805 | * work at the whole extent level. | |
806 | * NOCOW buffered write without data space reserved may not be able | |
807 | * to fall back to CoW due to lack of data space, thus could cause | |
808 | * data loss. | |
809 | * | |
810 | * Here we take a shortcut by flushing the whole inode, so that all | |
811 | * nocow write should reach disk as nocow before we increase the | |
812 | * reference of the extent. We could do better by only flushing NOCOW | |
813 | * data, but that needs extra accounting. | |
814 | * | |
815 | * Also we don't need to check ASYNC_EXTENT, as async extent will be | |
816 | * CoWed anyway, not affecting nocow part. | |
817 | */ | |
818 | ret = filemap_flush(inode_in->i_mapping); | |
819 | if (ret < 0) | |
820 | return ret; | |
821 | ||
822 | ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs), | |
823 | wb_len); | |
824 | if (ret < 0) | |
825 | return ret; | |
826 | ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs), | |
827 | wb_len); | |
828 | if (ret < 0) | |
829 | return ret; | |
830 | ||
831 | return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out, | |
832 | len, remap_flags); | |
833 | } | |
834 | ||
835 | loff_t btrfs_remap_file_range(struct file *src_file, loff_t off, | |
836 | struct file *dst_file, loff_t destoff, loff_t len, | |
837 | unsigned int remap_flags) | |
838 | { | |
839 | struct inode *src_inode = file_inode(src_file); | |
840 | struct inode *dst_inode = file_inode(dst_file); | |
841 | bool same_inode = dst_inode == src_inode; | |
842 | int ret; | |
843 | ||
844 | if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) | |
845 | return -EINVAL; | |
846 | ||
847 | if (same_inode) | |
848 | inode_lock(src_inode); | |
849 | else | |
850 | lock_two_nondirectories(src_inode, dst_inode); | |
851 | ||
852 | ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff, | |
853 | &len, remap_flags); | |
854 | if (ret < 0 || len == 0) | |
855 | goto out_unlock; | |
856 | ||
857 | if (remap_flags & REMAP_FILE_DEDUP) | |
858 | ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff); | |
859 | else | |
860 | ret = btrfs_clone_files(dst_file, src_file, off, len, destoff); | |
861 | ||
862 | out_unlock: | |
863 | if (same_inode) | |
864 | inode_unlock(src_inode); | |
865 | else | |
866 | unlock_two_nondirectories(src_inode, dst_inode); | |
867 | ||
868 | return ret < 0 ? ret : len; | |
869 | } |