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