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