Commit | Line | Data |
---|---|---|
f5166768 | 1 | // SPDX-License-Identifier: LGPL-2.1 |
748de673 AF |
2 | /* |
3 | * Copyright (c) 2008,2009 NEC Software Tohoku, Ltd. | |
4 | * Written by Takashi Sato <t-sato@yk.jp.nec.com> | |
5 | * Akira Fujita <a-fujita@rs.jp.nec.com> | |
748de673 AF |
6 | */ |
7 | ||
8 | #include <linux/fs.h> | |
9 | #include <linux/quotaops.h> | |
5a0e3ad6 | 10 | #include <linux/slab.h> |
8f50c8b7 | 11 | #include <linux/sched/mm.h> |
748de673 | 12 | #include "ext4_jbd2.h" |
748de673 | 13 | #include "ext4.h" |
4a092d73 | 14 | #include "ext4_extents.h" |
748de673 | 15 | |
e8505970 | 16 | /** |
c60990b3 TT |
17 | * get_ext_path() - Find an extent path for designated logical block number. |
18 | * @inode: inode to be searched | |
e8505970 | 19 | * @lblock: logical block number to find an extent path |
c60990b3 | 20 | * @ppath: pointer to an extent path pointer (for output) |
e8505970 | 21 | * |
ed8a1a76 | 22 | * ext4_find_extent wrapper. Return 0 on success, or a negative error value |
e8505970 AF |
23 | * on failure. |
24 | */ | |
25 | static inline int | |
26 | get_ext_path(struct inode *inode, ext4_lblk_t lblock, | |
3bdf14b4 | 27 | struct ext4_ext_path **ppath) |
e8505970 | 28 | { |
0e401101 | 29 | struct ext4_ext_path *path; |
e8505970 | 30 | |
ed8a1a76 | 31 | path = ext4_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE); |
0e401101 | 32 | if (IS_ERR(path)) |
3bdf14b4 TT |
33 | return PTR_ERR(path); |
34 | if (path[ext_depth(inode)].p_ext == NULL) { | |
7ff5fdda | 35 | ext4_free_ext_path(path); |
3bdf14b4 TT |
36 | *ppath = NULL; |
37 | return -ENODATA; | |
38 | } | |
39 | *ppath = path; | |
40 | return 0; | |
e8505970 | 41 | } |
748de673 | 42 | |
748de673 | 43 | /** |
c60990b3 TT |
44 | * ext4_double_down_write_data_sem() - write lock two inodes's i_data_sem |
45 | * @first: inode to be locked | |
46 | * @second: inode to be locked | |
748de673 | 47 | * |
03bd8b9b | 48 | * Acquire write lock of i_data_sem of the two inodes |
748de673 | 49 | */ |
393d1d1d DTB |
50 | void |
51 | ext4_double_down_write_data_sem(struct inode *first, struct inode *second) | |
748de673 | 52 | { |
03bd8b9b DM |
53 | if (first < second) { |
54 | down_write(&EXT4_I(first)->i_data_sem); | |
daf647d2 | 55 | down_write_nested(&EXT4_I(second)->i_data_sem, I_DATA_SEM_OTHER); |
03bd8b9b DM |
56 | } else { |
57 | down_write(&EXT4_I(second)->i_data_sem); | |
daf647d2 | 58 | down_write_nested(&EXT4_I(first)->i_data_sem, I_DATA_SEM_OTHER); |
748de673 | 59 | |
748de673 | 60 | } |
748de673 AF |
61 | } |
62 | ||
63 | /** | |
393d1d1d | 64 | * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem |
748de673 AF |
65 | * |
66 | * @orig_inode: original inode structure to be released its lock first | |
67 | * @donor_inode: donor inode structure to be released its lock second | |
fc04cb49 | 68 | * Release write lock of i_data_sem of two inodes (orig and donor). |
748de673 | 69 | */ |
393d1d1d DTB |
70 | void |
71 | ext4_double_up_write_data_sem(struct inode *orig_inode, | |
72 | struct inode *donor_inode) | |
748de673 | 73 | { |
748de673 AF |
74 | up_write(&EXT4_I(orig_inode)->i_data_sem); |
75 | up_write(&EXT4_I(donor_inode)->i_data_sem); | |
76 | } | |
77 | ||
8c854473 DM |
78 | /** |
79 | * mext_check_coverage - Check that all extents in range has the same type | |
80 | * | |
81 | * @inode: inode in question | |
82 | * @from: block offset of inode | |
83 | * @count: block count to be checked | |
556615dc | 84 | * @unwritten: extents expected to be unwritten |
8c854473 DM |
85 | * @err: pointer to save error value |
86 | * | |
87 | * Return 1 if all extents in range has expected type, and zero otherwise. | |
88 | */ | |
89 | static int | |
90 | mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count, | |
556615dc | 91 | int unwritten, int *err) |
8c854473 DM |
92 | { |
93 | struct ext4_ext_path *path = NULL; | |
94 | struct ext4_extent *ext; | |
0e401101 | 95 | int ret = 0; |
8c854473 DM |
96 | ext4_lblk_t last = from + count; |
97 | while (from < last) { | |
98 | *err = get_ext_path(inode, from, &path); | |
99 | if (*err) | |
0e401101 | 100 | goto out; |
8c854473 | 101 | ext = path[ext_depth(inode)].p_ext; |
556615dc | 102 | if (unwritten != ext4_ext_is_unwritten(ext)) |
0e401101 | 103 | goto out; |
8c854473 | 104 | from += ext4_ext_get_actual_len(ext); |
8c854473 | 105 | } |
0e401101 DM |
106 | ret = 1; |
107 | out: | |
7ff5fdda | 108 | ext4_free_ext_path(path); |
0e401101 | 109 | return ret; |
8c854473 DM |
110 | } |
111 | ||
bb557488 | 112 | /** |
e8dfc854 | 113 | * mext_folio_double_lock - Grab and lock folio on both @inode1 and @inode2 |
bb557488 DM |
114 | * |
115 | * @inode1: the inode structure | |
116 | * @inode2: the inode structure | |
e8dfc854 VMO |
117 | * @index1: folio index |
118 | * @index2: folio index | |
119 | * @folio: result folio vector | |
bb557488 | 120 | * |
e8dfc854 | 121 | * Grab two locked folio for inode's by inode order |
bb557488 DM |
122 | */ |
123 | static int | |
e8dfc854 VMO |
124 | mext_folio_double_lock(struct inode *inode1, struct inode *inode2, |
125 | pgoff_t index1, pgoff_t index2, struct folio *folio[2]) | |
bb557488 DM |
126 | { |
127 | struct address_space *mapping[2]; | |
8f50c8b7 | 128 | unsigned int flags; |
bb557488 DM |
129 | |
130 | BUG_ON(!inode1 || !inode2); | |
131 | if (inode1 < inode2) { | |
132 | mapping[0] = inode1->i_mapping; | |
133 | mapping[1] = inode2->i_mapping; | |
134 | } else { | |
62bbdd99 | 135 | swap(index1, index2); |
bb557488 DM |
136 | mapping[0] = inode2->i_mapping; |
137 | mapping[1] = inode1->i_mapping; | |
138 | } | |
139 | ||
8f50c8b7 | 140 | flags = memalloc_nofs_save(); |
e999a5c5 | 141 | folio[0] = __filemap_get_folio(mapping[0], index1, FGP_WRITEBEGIN, |
e8dfc854 | 142 | mapping_gfp_mask(mapping[0])); |
66dabbb6 | 143 | if (IS_ERR(folio[0])) { |
8f50c8b7 | 144 | memalloc_nofs_restore(flags); |
66dabbb6 | 145 | return PTR_ERR(folio[0]); |
8f50c8b7 | 146 | } |
bb557488 | 147 | |
e999a5c5 | 148 | folio[1] = __filemap_get_folio(mapping[1], index2, FGP_WRITEBEGIN, |
e8dfc854 | 149 | mapping_gfp_mask(mapping[1])); |
8f50c8b7 | 150 | memalloc_nofs_restore(flags); |
66dabbb6 | 151 | if (IS_ERR(folio[1])) { |
e8dfc854 VMO |
152 | folio_unlock(folio[0]); |
153 | folio_put(folio[0]); | |
66dabbb6 | 154 | return PTR_ERR(folio[1]); |
bb557488 | 155 | } |
7e8b12c6 | 156 | /* |
e8dfc854 | 157 | * __filemap_get_folio() may not wait on folio's writeback if |
7e8b12c6 | 158 | * BDI not demand that. But it is reasonable to be very conservative |
e8dfc854 | 159 | * here and explicitly wait on folio's writeback |
7e8b12c6 | 160 | */ |
e8dfc854 VMO |
161 | folio_wait_writeback(folio[0]); |
162 | folio_wait_writeback(folio[1]); | |
bf865467 | 163 | if (inode1 > inode2) |
e8dfc854 | 164 | swap(folio[0], folio[1]); |
bf865467 | 165 | |
bb557488 DM |
166 | return 0; |
167 | } | |
168 | ||
169 | /* Force page buffers uptodate w/o dropping page's lock */ | |
170 | static int | |
3060b6ef | 171 | mext_page_mkuptodate(struct folio *folio, unsigned from, unsigned to) |
bb557488 | 172 | { |
3060b6ef | 173 | struct inode *inode = folio->mapping->host; |
bb557488 DM |
174 | sector_t block; |
175 | struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; | |
176 | unsigned int blocksize, block_start, block_end; | |
177 | int i, err, nr = 0, partial = 0; | |
3060b6ef MW |
178 | BUG_ON(!folio_test_locked(folio)); |
179 | BUG_ON(folio_test_writeback(folio)); | |
bb557488 | 180 | |
3060b6ef | 181 | if (folio_test_uptodate(folio)) |
bb557488 DM |
182 | return 0; |
183 | ||
93407472 | 184 | blocksize = i_blocksize(inode); |
3060b6ef MW |
185 | head = folio_buffers(folio); |
186 | if (!head) { | |
187 | create_empty_buffers(&folio->page, blocksize, 0); | |
188 | head = folio_buffers(folio); | |
189 | } | |
bb557488 | 190 | |
3060b6ef | 191 | block = (sector_t)folio->index << (PAGE_SHIFT - inode->i_blkbits); |
bb557488 DM |
192 | for (bh = head, block_start = 0; bh != head || !block_start; |
193 | block++, block_start = block_end, bh = bh->b_this_page) { | |
194 | block_end = block_start + blocksize; | |
195 | if (block_end <= from || block_start >= to) { | |
196 | if (!buffer_uptodate(bh)) | |
197 | partial = 1; | |
198 | continue; | |
199 | } | |
200 | if (buffer_uptodate(bh)) | |
201 | continue; | |
202 | if (!buffer_mapped(bh)) { | |
bb557488 DM |
203 | err = ext4_get_block(inode, block, bh, 0); |
204 | if (err) { | |
3060b6ef | 205 | folio_set_error(folio); |
bb557488 DM |
206 | return err; |
207 | } | |
208 | if (!buffer_mapped(bh)) { | |
3060b6ef | 209 | folio_zero_range(folio, block_start, blocksize); |
df3a98b0 | 210 | set_buffer_uptodate(bh); |
bb557488 DM |
211 | continue; |
212 | } | |
213 | } | |
214 | BUG_ON(nr >= MAX_BUF_PER_PAGE); | |
215 | arr[nr++] = bh; | |
216 | } | |
217 | /* No io required */ | |
218 | if (!nr) | |
219 | goto out; | |
220 | ||
221 | for (i = 0; i < nr; i++) { | |
222 | bh = arr[i]; | |
223 | if (!bh_uptodate_or_lock(bh)) { | |
2d069c08 | 224 | err = ext4_read_bh(bh, 0, NULL); |
bb557488 DM |
225 | if (err) |
226 | return err; | |
227 | } | |
228 | } | |
229 | out: | |
230 | if (!partial) | |
3060b6ef | 231 | folio_mark_uptodate(folio); |
bb557488 DM |
232 | return 0; |
233 | } | |
234 | ||
748de673 AF |
235 | /** |
236 | * move_extent_per_page - Move extent data per page | |
237 | * | |
238 | * @o_filp: file structure of original file | |
239 | * @donor_inode: donor inode | |
240 | * @orig_page_offset: page index on original file | |
65dd8327 | 241 | * @donor_page_offset: page index on donor file |
748de673 AF |
242 | * @data_offset_in_page: block index where data swapping starts |
243 | * @block_len_in_page: the number of blocks to be swapped | |
556615dc | 244 | * @unwritten: orig extent is unwritten or not |
f868a48d | 245 | * @err: pointer to save return value |
748de673 AF |
246 | * |
247 | * Save the data in original inode blocks and replace original inode extents | |
65dd8327 | 248 | * with donor inode extents by calling ext4_swap_extents(). |
f868a48d AF |
249 | * Finally, write out the saved data in new original inode blocks. Return |
250 | * replaced block count. | |
748de673 AF |
251 | */ |
252 | static int | |
44fc48f7 | 253 | move_extent_per_page(struct file *o_filp, struct inode *donor_inode, |
fcf6b1b7 DM |
254 | pgoff_t orig_page_offset, pgoff_t donor_page_offset, |
255 | int data_offset_in_page, | |
256 | int block_len_in_page, int unwritten, int *err) | |
748de673 | 257 | { |
496ad9aa | 258 | struct inode *orig_inode = file_inode(o_filp); |
6dd8fe86 | 259 | struct folio *folio[2] = {NULL, NULL}; |
748de673 | 260 | handle_t *handle; |
fcf6b1b7 | 261 | ext4_lblk_t orig_blk_offset, donor_blk_offset; |
748de673 | 262 | unsigned long blocksize = orig_inode->i_sb->s_blocksize; |
f868a48d | 263 | unsigned int tmp_data_size, data_size, replaced_size; |
bcff2488 | 264 | int i, err2, jblocks, retries = 0; |
f868a48d | 265 | int replaced_count = 0; |
bb557488 | 266 | int from = data_offset_in_page << orig_inode->i_blkbits; |
09cbfeaf | 267 | int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits; |
88c6b61f | 268 | struct super_block *sb = orig_inode->i_sb; |
bcff2488 | 269 | struct buffer_head *bh = NULL; |
748de673 AF |
270 | |
271 | /* | |
272 | * It needs twice the amount of ordinary journal buffers because | |
273 | * inode and donor_inode may change each different metadata blocks. | |
274 | */ | |
bb557488 DM |
275 | again: |
276 | *err = 0; | |
748de673 | 277 | jblocks = ext4_writepage_trans_blocks(orig_inode) * 2; |
9924a92a | 278 | handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks); |
748de673 | 279 | if (IS_ERR(handle)) { |
f868a48d AF |
280 | *err = PTR_ERR(handle); |
281 | return 0; | |
748de673 AF |
282 | } |
283 | ||
748de673 AF |
284 | orig_blk_offset = orig_page_offset * blocks_per_page + |
285 | data_offset_in_page; | |
286 | ||
fcf6b1b7 DM |
287 | donor_blk_offset = donor_page_offset * blocks_per_page + |
288 | data_offset_in_page; | |
289 | ||
f868a48d | 290 | /* Calculate data_size */ |
748de673 AF |
291 | if ((orig_blk_offset + block_len_in_page - 1) == |
292 | ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) { | |
293 | /* Replace the last block */ | |
f868a48d | 294 | tmp_data_size = orig_inode->i_size & (blocksize - 1); |
748de673 | 295 | /* |
f868a48d | 296 | * If data_size equal zero, it shows data_size is multiples of |
748de673 AF |
297 | * blocksize. So we set appropriate value. |
298 | */ | |
f868a48d AF |
299 | if (tmp_data_size == 0) |
300 | tmp_data_size = blocksize; | |
748de673 | 301 | |
f868a48d | 302 | data_size = tmp_data_size + |
748de673 | 303 | ((block_len_in_page - 1) << orig_inode->i_blkbits); |
f868a48d AF |
304 | } else |
305 | data_size = block_len_in_page << orig_inode->i_blkbits; | |
306 | ||
307 | replaced_size = data_size; | |
748de673 | 308 | |
e8dfc854 VMO |
309 | *err = mext_folio_double_lock(orig_inode, donor_inode, orig_page_offset, |
310 | donor_page_offset, folio); | |
f868a48d | 311 | if (unlikely(*err < 0)) |
bb557488 | 312 | goto stop_journal; |
8c854473 | 313 | /* |
556615dc | 314 | * If orig extent was unwritten it can become initialized |
8c854473 DM |
315 | * at any time after i_data_sem was dropped, in order to |
316 | * serialize with delalloc we have recheck extent while we | |
317 | * hold page's lock, if it is still the case data copy is not | |
318 | * necessary, just swap data blocks between orig and donor. | |
319 | */ | |
6dd8fe86 VMO |
320 | |
321 | VM_BUG_ON_FOLIO(folio_test_large(folio[0]), folio[0]); | |
322 | VM_BUG_ON_FOLIO(folio_test_large(folio[1]), folio[1]); | |
323 | VM_BUG_ON_FOLIO(folio_nr_pages(folio[0]) != folio_nr_pages(folio[1]), folio[1]); | |
324 | ||
556615dc | 325 | if (unwritten) { |
393d1d1d | 326 | ext4_double_down_write_data_sem(orig_inode, donor_inode); |
8c854473 DM |
327 | /* If any of extents in range became initialized we have to |
328 | * fallback to data copying */ | |
556615dc LC |
329 | unwritten = mext_check_coverage(orig_inode, orig_blk_offset, |
330 | block_len_in_page, 1, err); | |
8c854473 DM |
331 | if (*err) |
332 | goto drop_data_sem; | |
748de673 | 333 | |
fcf6b1b7 | 334 | unwritten &= mext_check_coverage(donor_inode, donor_blk_offset, |
556615dc | 335 | block_len_in_page, 1, err); |
8c854473 DM |
336 | if (*err) |
337 | goto drop_data_sem; | |
338 | ||
556615dc | 339 | if (!unwritten) { |
393d1d1d | 340 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
8c854473 DM |
341 | goto data_copy; |
342 | } | |
6dd8fe86 VMO |
343 | if ((folio_has_private(folio[0]) && |
344 | !filemap_release_folio(folio[0], 0)) || | |
345 | (folio_has_private(folio[1]) && | |
346 | !filemap_release_folio(folio[1], 0))) { | |
8c854473 DM |
347 | *err = -EBUSY; |
348 | goto drop_data_sem; | |
349 | } | |
fcf6b1b7 DM |
350 | replaced_count = ext4_swap_extents(handle, orig_inode, |
351 | donor_inode, orig_blk_offset, | |
352 | donor_blk_offset, | |
353 | block_len_in_page, 1, err); | |
8c854473 | 354 | drop_data_sem: |
393d1d1d | 355 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
6dd8fe86 | 356 | goto unlock_folios; |
8c854473 DM |
357 | } |
358 | data_copy: | |
3060b6ef | 359 | *err = mext_page_mkuptodate(folio[0], from, from + replaced_size); |
bb557488 | 360 | if (*err) |
6dd8fe86 | 361 | goto unlock_folios; |
bb557488 DM |
362 | |
363 | /* At this point all buffers in range are uptodate, old mapping layout | |
364 | * is no longer required, try to drop it now. */ | |
6dd8fe86 VMO |
365 | if ((folio_has_private(folio[0]) && |
366 | !filemap_release_folio(folio[0], 0)) || | |
367 | (folio_has_private(folio[1]) && | |
368 | !filemap_release_folio(folio[1], 0))) { | |
bb557488 | 369 | *err = -EBUSY; |
6dd8fe86 | 370 | goto unlock_folios; |
748de673 | 371 | } |
6e263146 | 372 | ext4_double_down_write_data_sem(orig_inode, donor_inode); |
fcf6b1b7 DM |
373 | replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode, |
374 | orig_blk_offset, donor_blk_offset, | |
375 | block_len_in_page, 1, err); | |
6e263146 | 376 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
bb557488 | 377 | if (*err) { |
f868a48d AF |
378 | if (replaced_count) { |
379 | block_len_in_page = replaced_count; | |
380 | replaced_size = | |
381 | block_len_in_page << orig_inode->i_blkbits; | |
ac48b0a1 | 382 | } else |
6dd8fe86 | 383 | goto unlock_folios; |
748de673 | 384 | } |
bb557488 DM |
385 | /* Perform all necessary steps similar write_begin()/write_end() |
386 | * but keeping in mind that i_size will not change */ | |
6dd8fe86 VMO |
387 | if (!folio_buffers(folio[0])) |
388 | create_empty_buffers(&folio[0]->page, 1 << orig_inode->i_blkbits, 0); | |
389 | bh = folio_buffers(folio[0]); | |
bcff2488 EG |
390 | for (i = 0; i < data_offset_in_page; i++) |
391 | bh = bh->b_this_page; | |
392 | for (i = 0; i < block_len_in_page; i++) { | |
393 | *err = ext4_get_block(orig_inode, orig_blk_offset + i, bh, 0); | |
394 | if (*err < 0) | |
395 | break; | |
6ffe77ba | 396 | bh = bh->b_this_page; |
bcff2488 | 397 | } |
bb557488 | 398 | if (!*err) |
6dd8fe86 | 399 | *err = block_commit_write(&folio[0]->page, from, from + replaced_size); |
748de673 | 400 | |
bb557488 DM |
401 | if (unlikely(*err < 0)) |
402 | goto repair_branches; | |
403 | ||
404 | /* Even in case of data=writeback it is reasonable to pin | |
405 | * inode to transaction, to prevent unexpected data loss */ | |
73131fbb RZ |
406 | *err = ext4_jbd2_inode_add_write(handle, orig_inode, |
407 | (loff_t)orig_page_offset << PAGE_SHIFT, replaced_size); | |
bb557488 | 408 | |
6dd8fe86 VMO |
409 | unlock_folios: |
410 | folio_unlock(folio[0]); | |
411 | folio_put(folio[0]); | |
412 | folio_unlock(folio[1]); | |
413 | folio_put(folio[1]); | |
bb557488 | 414 | stop_journal: |
748de673 | 415 | ext4_journal_stop(handle); |
88c6b61f DM |
416 | if (*err == -ENOSPC && |
417 | ext4_should_retry_alloc(sb, &retries)) | |
418 | goto again; | |
bb557488 DM |
419 | /* Buffer was busy because probably is pinned to journal transaction, |
420 | * force transaction commit may help to free it. */ | |
88c6b61f DM |
421 | if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal && |
422 | jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal)) | |
bb557488 | 423 | goto again; |
f868a48d | 424 | return replaced_count; |
bb557488 DM |
425 | |
426 | repair_branches: | |
427 | /* | |
428 | * This should never ever happen! | |
429 | * Extents are swapped already, but we are not able to copy data. | |
430 | * Try to swap extents to it's original places | |
431 | */ | |
393d1d1d | 432 | ext4_double_down_write_data_sem(orig_inode, donor_inode); |
fcf6b1b7 DM |
433 | replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode, |
434 | orig_blk_offset, donor_blk_offset, | |
435 | block_len_in_page, 0, &err2); | |
393d1d1d | 436 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
bb557488 | 437 | if (replaced_count != block_len_in_page) { |
54d3adbc TT |
438 | ext4_error_inode_block(orig_inode, (sector_t)(orig_blk_offset), |
439 | EIO, "Unable to copy data block," | |
bb557488 DM |
440 | " data will be lost."); |
441 | *err = -EIO; | |
442 | } | |
443 | replaced_count = 0; | |
6dd8fe86 | 444 | goto unlock_folios; |
748de673 AF |
445 | } |
446 | ||
447 | /** | |
c437b273 | 448 | * mext_check_arguments - Check whether move extent can be done |
748de673 AF |
449 | * |
450 | * @orig_inode: original inode | |
451 | * @donor_inode: donor inode | |
452 | * @orig_start: logical start offset in block for orig | |
453 | * @donor_start: logical start offset in block for donor | |
454 | * @len: the number of blocks to be moved | |
748de673 AF |
455 | * |
456 | * Check the arguments of ext4_move_extents() whether the files can be | |
457 | * exchanged with each other. | |
458 | * Return 0 on success, or a negative error value on failure. | |
459 | */ | |
460 | static int | |
461 | mext_check_arguments(struct inode *orig_inode, | |
446aaa6e KM |
462 | struct inode *donor_inode, __u64 orig_start, |
463 | __u64 donor_start, __u64 *len) | |
748de673 | 464 | { |
fcf6b1b7 | 465 | __u64 orig_eof, donor_eof; |
70d5d3dc AF |
466 | unsigned int blkbits = orig_inode->i_blkbits; |
467 | unsigned int blocksize = 1 << blkbits; | |
468 | ||
fcf6b1b7 DM |
469 | orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits; |
470 | donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits; | |
471 | ||
472 | ||
4a58579b AF |
473 | if (donor_inode->i_mode & (S_ISUID|S_ISGID)) { |
474 | ext4_debug("ext4 move extent: suid or sgid is set" | |
475 | " to donor file [ino:orig %lu, donor %lu]\n", | |
476 | orig_inode->i_ino, donor_inode->i_ino); | |
477 | return -EINVAL; | |
478 | } | |
479 | ||
1f5a81e4 TT |
480 | if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode)) |
481 | return -EPERM; | |
482 | ||
d412df53 | 483 | /* Ext4 move extent does not support swap files */ |
748de673 | 484 | if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) { |
d412df53 | 485 | ext4_debug("ext4 move extent: The argument files should not be swap files [ino:orig %lu, donor %lu]\n", |
748de673 | 486 | orig_inode->i_ino, donor_inode->i_ino); |
d412df53 | 487 | return -ETXTBSY; |
748de673 AF |
488 | } |
489 | ||
02749a4c | 490 | if (ext4_is_quota_file(orig_inode) && ext4_is_quota_file(donor_inode)) { |
d412df53 | 491 | ext4_debug("ext4 move extent: The argument files should not be quota files [ino:orig %lu, donor %lu]\n", |
daf647d2 | 492 | orig_inode->i_ino, donor_inode->i_ino); |
d412df53 | 493 | return -EOPNOTSUPP; |
daf647d2 TT |
494 | } |
495 | ||
748de673 | 496 | /* Ext4 move extent supports only extent based file */ |
12e9b892 | 497 | if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) { |
748de673 AF |
498 | ext4_debug("ext4 move extent: orig file is not extents " |
499 | "based file [ino:orig %lu]\n", orig_inode->i_ino); | |
500 | return -EOPNOTSUPP; | |
12e9b892 | 501 | } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) { |
748de673 AF |
502 | ext4_debug("ext4 move extent: donor file is not extents " |
503 | "based file [ino:donor %lu]\n", donor_inode->i_ino); | |
504 | return -EOPNOTSUPP; | |
505 | } | |
506 | ||
507 | if ((!orig_inode->i_size) || (!donor_inode->i_size)) { | |
508 | ext4_debug("ext4 move extent: File size is 0 byte\n"); | |
509 | return -EINVAL; | |
510 | } | |
511 | ||
512 | /* Start offset should be same */ | |
fcf6b1b7 DM |
513 | if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) != |
514 | (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) { | |
748de673 | 515 | ext4_debug("ext4 move extent: orig and donor's start " |
d67d64f4 | 516 | "offsets are not aligned [ino:orig %lu, donor %lu]\n", |
748de673 AF |
517 | orig_inode->i_ino, donor_inode->i_ino); |
518 | return -EINVAL; | |
519 | } | |
520 | ||
f17722f9 | 521 | if ((orig_start >= EXT_MAX_BLOCKS) || |
fcf6b1b7 | 522 | (donor_start >= EXT_MAX_BLOCKS) || |
f17722f9 | 523 | (*len > EXT_MAX_BLOCKS) || |
fcf6b1b7 | 524 | (donor_start + *len >= EXT_MAX_BLOCKS) || |
f17722f9 | 525 | (orig_start + *len >= EXT_MAX_BLOCKS)) { |
0a80e986 | 526 | ext4_debug("ext4 move extent: Can't handle over [%u] blocks " |
f17722f9 | 527 | "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS, |
748de673 AF |
528 | orig_inode->i_ino, donor_inode->i_ino); |
529 | return -EINVAL; | |
530 | } | |
f18b2b83 TT |
531 | if (orig_eof <= orig_start) |
532 | *len = 0; | |
533 | else if (orig_eof < orig_start + *len - 1) | |
fcf6b1b7 | 534 | *len = orig_eof - orig_start; |
f18b2b83 TT |
535 | if (donor_eof <= donor_start) |
536 | *len = 0; | |
537 | else if (donor_eof < donor_start + *len - 1) | |
fcf6b1b7 | 538 | *len = donor_eof - donor_start; |
748de673 | 539 | if (!*len) { |
92c28159 | 540 | ext4_debug("ext4 move extent: len should not be 0 " |
748de673 AF |
541 | "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino, |
542 | donor_inode->i_ino); | |
543 | return -EINVAL; | |
544 | } | |
545 | ||
546 | return 0; | |
547 | } | |
548 | ||
748de673 AF |
549 | /** |
550 | * ext4_move_extents - Exchange the specified range of a file | |
551 | * | |
552 | * @o_filp: file structure of the original file | |
553 | * @d_filp: file structure of the donor file | |
65dd8327 XW |
554 | * @orig_blk: start offset in block for orig |
555 | * @donor_blk: start offset in block for donor | |
748de673 AF |
556 | * @len: the number of blocks to be moved |
557 | * @moved_len: moved block length | |
558 | * | |
559 | * This function returns 0 and moved block length is set in moved_len | |
560 | * if succeed, otherwise returns error value. | |
561 | * | |
748de673 AF |
562 | */ |
563 | int | |
fcf6b1b7 DM |
564 | ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk, |
565 | __u64 donor_blk, __u64 len, __u64 *moved_len) | |
748de673 | 566 | { |
496ad9aa AV |
567 | struct inode *orig_inode = file_inode(o_filp); |
568 | struct inode *donor_inode = file_inode(d_filp); | |
fcf6b1b7 | 569 | struct ext4_ext_path *path = NULL; |
09cbfeaf | 570 | int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits; |
fcf6b1b7 DM |
571 | ext4_lblk_t o_end, o_start = orig_blk; |
572 | ext4_lblk_t d_start = donor_blk; | |
573 | int ret; | |
748de673 | 574 | |
03bd8b9b DM |
575 | if (orig_inode->i_sb != donor_inode->i_sb) { |
576 | ext4_debug("ext4 move extent: The argument files " | |
577 | "should be in same FS [ino:orig %lu, donor %lu]\n", | |
578 | orig_inode->i_ino, donor_inode->i_ino); | |
579 | return -EINVAL; | |
580 | } | |
581 | ||
582 | /* orig and donor should be different inodes */ | |
583 | if (orig_inode == donor_inode) { | |
f3ce8064 | 584 | ext4_debug("ext4 move extent: The argument files should not " |
03bd8b9b | 585 | "be same inode [ino:orig %lu, donor %lu]\n", |
f3ce8064 TT |
586 | orig_inode->i_ino, donor_inode->i_ino); |
587 | return -EINVAL; | |
588 | } | |
589 | ||
7247c0ca AF |
590 | /* Regular file check */ |
591 | if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) { | |
592 | ext4_debug("ext4 move extent: The argument files should be " | |
593 | "regular file [ino:orig %lu, donor %lu]\n", | |
594 | orig_inode->i_ino, donor_inode->i_ino); | |
595 | return -EINVAL; | |
596 | } | |
04e22412 EW |
597 | |
598 | /* TODO: it's not obvious how to swap blocks for inodes with full | |
599 | journaling enabled */ | |
f066055a DM |
600 | if (ext4_should_journal_data(orig_inode) || |
601 | ext4_should_journal_data(donor_inode)) { | |
04e22412 EW |
602 | ext4_msg(orig_inode->i_sb, KERN_ERR, |
603 | "Online defrag not supported with data journaling"); | |
604 | return -EOPNOTSUPP; | |
f066055a | 605 | } |
04e22412 | 606 | |
592ddec7 | 607 | if (IS_ENCRYPTED(orig_inode) || IS_ENCRYPTED(donor_inode)) { |
14fbd4aa EW |
608 | ext4_msg(orig_inode->i_sb, KERN_ERR, |
609 | "Online defrag not supported for encrypted files"); | |
610 | return -EOPNOTSUPP; | |
611 | } | |
612 | ||
fc04cb49 | 613 | /* Protect orig and donor inodes against a truncate */ |
375e289e | 614 | lock_two_nondirectories(orig_inode, donor_inode); |
748de673 | 615 | |
17335dcc | 616 | /* Wait for all existing dio workers */ |
17335dcc DM |
617 | inode_dio_wait(orig_inode); |
618 | inode_dio_wait(donor_inode); | |
619 | ||
fc04cb49 | 620 | /* Protect extent tree against block allocations via delalloc */ |
393d1d1d | 621 | ext4_double_down_write_data_sem(orig_inode, donor_inode); |
748de673 | 622 | /* Check the filesystem environment whether move_extent can be done */ |
fcf6b1b7 DM |
623 | ret = mext_check_arguments(orig_inode, donor_inode, orig_blk, |
624 | donor_blk, &len); | |
03bd8b9b | 625 | if (ret) |
347fa6f1 | 626 | goto out; |
fcf6b1b7 | 627 | o_end = o_start + len; |
748de673 | 628 | |
fcf6b1b7 DM |
629 | while (o_start < o_end) { |
630 | struct ext4_extent *ex; | |
631 | ext4_lblk_t cur_blk, next_blk; | |
632 | pgoff_t orig_page_index, donor_page_index; | |
633 | int offset_in_page; | |
634 | int unwritten, cur_len; | |
748de673 | 635 | |
fcf6b1b7 DM |
636 | ret = get_ext_path(orig_inode, o_start, &path); |
637 | if (ret) | |
748de673 | 638 | goto out; |
fcf6b1b7 | 639 | ex = path[path->p_depth].p_ext; |
fcf6b1b7 DM |
640 | cur_blk = le32_to_cpu(ex->ee_block); |
641 | cur_len = ext4_ext_get_actual_len(ex); | |
642 | /* Check hole before the start pos */ | |
643 | if (cur_blk + cur_len - 1 < o_start) { | |
d412df53 | 644 | next_blk = ext4_ext_next_allocated_block(path); |
fcf6b1b7 | 645 | if (next_blk == EXT_MAX_BLOCKS) { |
fcf6b1b7 DM |
646 | ret = -ENODATA; |
647 | goto out; | |
648 | } | |
649 | d_start += next_blk - o_start; | |
650 | o_start = next_blk; | |
3bdf14b4 | 651 | continue; |
fcf6b1b7 DM |
652 | /* Check hole after the start pos */ |
653 | } else if (cur_blk > o_start) { | |
654 | /* Skip hole */ | |
655 | d_start += cur_blk - o_start; | |
656 | o_start = cur_blk; | |
657 | /* Extent inside requested range ?*/ | |
658 | if (cur_blk >= o_end) | |
659 | goto out; | |
660 | } else { /* in_range(o_start, o_blk, o_len) */ | |
661 | cur_len += cur_blk - o_start; | |
748de673 | 662 | } |
fcf6b1b7 DM |
663 | unwritten = ext4_ext_is_unwritten(ex); |
664 | if (o_end - o_start < cur_len) | |
665 | cur_len = o_end - o_start; | |
666 | ||
09cbfeaf | 667 | orig_page_index = o_start >> (PAGE_SHIFT - |
fcf6b1b7 | 668 | orig_inode->i_blkbits); |
09cbfeaf | 669 | donor_page_index = d_start >> (PAGE_SHIFT - |
fcf6b1b7 DM |
670 | donor_inode->i_blkbits); |
671 | offset_in_page = o_start % blocks_per_page; | |
d412df53 | 672 | if (cur_len > blocks_per_page - offset_in_page) |
fcf6b1b7 | 673 | cur_len = blocks_per_page - offset_in_page; |
fc04cb49 AF |
674 | /* |
675 | * Up semaphore to avoid following problems: | |
676 | * a. transaction deadlock among ext4_journal_start, | |
677 | * ->write_begin via pagefault, and jbd2_journal_commit | |
fe5ddf6b MWO |
678 | * b. racing with ->read_folio, ->write_begin, and |
679 | * ext4_get_block in move_extent_per_page | |
fc04cb49 | 680 | */ |
393d1d1d | 681 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
fcf6b1b7 DM |
682 | /* Swap original branches with new branches */ |
683 | move_extent_per_page(o_filp, donor_inode, | |
684 | orig_page_index, donor_page_index, | |
685 | offset_in_page, cur_len, | |
686 | unwritten, &ret); | |
393d1d1d | 687 | ext4_double_down_write_data_sem(orig_inode, donor_inode); |
03bd8b9b | 688 | if (ret < 0) |
fc04cb49 | 689 | break; |
fcf6b1b7 DM |
690 | o_start += cur_len; |
691 | d_start += cur_len; | |
748de673 | 692 | } |
fcf6b1b7 DM |
693 | *moved_len = o_start - orig_blk; |
694 | if (*moved_len > len) | |
695 | *moved_len = len; | |
696 | ||
748de673 | 697 | out: |
94d7c16c | 698 | if (*moved_len) { |
27bc446e | 699 | ext4_discard_preallocations(orig_inode, 0); |
700 | ext4_discard_preallocations(donor_inode, 0); | |
94d7c16c AF |
701 | } |
702 | ||
7ff5fdda | 703 | ext4_free_ext_path(path); |
393d1d1d | 704 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
375e289e | 705 | unlock_two_nondirectories(orig_inode, donor_inode); |
748de673 | 706 | |
03bd8b9b | 707 | return ret; |
748de673 | 708 | } |