]>
Commit | Line | Data |
---|---|---|
748de673 AF |
1 | /* |
2 | * Copyright (c) 2008,2009 NEC Software Tohoku, Ltd. | |
3 | * Written by Takashi Sato <[email protected]> | |
4 | * Akira Fujita <[email protected]> | |
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 AF |
20 | #include "ext4.h" |
21 | ||
e8505970 AF |
22 | /** |
23 | * get_ext_path - Find an extent path for designated logical block number. | |
24 | * | |
25 | * @inode: an inode which is searched | |
26 | * @lblock: logical block number to find an extent path | |
27 | * @path: pointer to an extent path pointer (for output) | |
28 | * | |
29 | * ext4_ext_find_extent wrapper. Return 0 on success, or a negative error value | |
30 | * on failure. | |
31 | */ | |
32 | static inline int | |
33 | get_ext_path(struct inode *inode, ext4_lblk_t lblock, | |
34 | struct ext4_ext_path **path) | |
35 | { | |
36 | int ret = 0; | |
37 | ||
38 | *path = ext4_ext_find_extent(inode, lblock, *path); | |
39 | if (IS_ERR(*path)) { | |
40 | ret = PTR_ERR(*path); | |
41 | *path = NULL; | |
347fa6f1 AF |
42 | } else if ((*path)[ext_depth(inode)].p_ext == NULL) |
43 | ret = -ENODATA; | |
44 | ||
e8505970 AF |
45 | return ret; |
46 | } | |
748de673 AF |
47 | |
48 | /** | |
49 | * copy_extent_status - Copy the extent's initialization status | |
50 | * | |
51 | * @src: an extent for getting initialize status | |
52 | * @dest: an extent to be set the status | |
53 | */ | |
54 | static void | |
55 | copy_extent_status(struct ext4_extent *src, struct ext4_extent *dest) | |
56 | { | |
57 | if (ext4_ext_is_uninitialized(src)) | |
58 | ext4_ext_mark_uninitialized(dest); | |
59 | else | |
60 | dest->ee_len = cpu_to_le16(ext4_ext_get_actual_len(dest)); | |
61 | } | |
62 | ||
63 | /** | |
64 | * mext_next_extent - Search for the next extent and set it to "extent" | |
65 | * | |
66 | * @inode: inode which is searched | |
67 | * @path: this will obtain data for the next extent | |
68 | * @extent: pointer to the next extent we have just gotten | |
69 | * | |
70 | * Search the next extent in the array of ext4_ext_path structure (@path) | |
71 | * and set it to ext4_extent structure (@extent). In addition, the member of | |
72 | * @path (->p_ext) also points the next extent. Return 0 on success, 1 if | |
73 | * ext4_ext_path structure refers to the last extent, or a negative error | |
74 | * value on failure. | |
75 | */ | |
76 | static int | |
77 | mext_next_extent(struct inode *inode, struct ext4_ext_path *path, | |
78 | struct ext4_extent **extent) | |
79 | { | |
fc04cb49 | 80 | struct ext4_extent_header *eh; |
748de673 AF |
81 | int ppos, leaf_ppos = path->p_depth; |
82 | ||
83 | ppos = leaf_ppos; | |
84 | if (EXT_LAST_EXTENT(path[ppos].p_hdr) > path[ppos].p_ext) { | |
85 | /* leaf block */ | |
86 | *extent = ++path[ppos].p_ext; | |
bf89d16f | 87 | path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); |
748de673 AF |
88 | return 0; |
89 | } | |
90 | ||
91 | while (--ppos >= 0) { | |
92 | if (EXT_LAST_INDEX(path[ppos].p_hdr) > | |
93 | path[ppos].p_idx) { | |
94 | int cur_ppos = ppos; | |
95 | ||
96 | /* index block */ | |
97 | path[ppos].p_idx++; | |
bf89d16f | 98 | path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); |
748de673 AF |
99 | if (path[ppos+1].p_bh) |
100 | brelse(path[ppos+1].p_bh); | |
101 | path[ppos+1].p_bh = | |
102 | sb_bread(inode->i_sb, path[ppos].p_block); | |
103 | if (!path[ppos+1].p_bh) | |
104 | return -EIO; | |
105 | path[ppos+1].p_hdr = | |
106 | ext_block_hdr(path[ppos+1].p_bh); | |
107 | ||
108 | /* Halfway index block */ | |
109 | while (++cur_ppos < leaf_ppos) { | |
110 | path[cur_ppos].p_idx = | |
111 | EXT_FIRST_INDEX(path[cur_ppos].p_hdr); | |
112 | path[cur_ppos].p_block = | |
bf89d16f | 113 | ext4_idx_pblock(path[cur_ppos].p_idx); |
748de673 AF |
114 | if (path[cur_ppos+1].p_bh) |
115 | brelse(path[cur_ppos+1].p_bh); | |
116 | path[cur_ppos+1].p_bh = sb_bread(inode->i_sb, | |
117 | path[cur_ppos].p_block); | |
118 | if (!path[cur_ppos+1].p_bh) | |
119 | return -EIO; | |
120 | path[cur_ppos+1].p_hdr = | |
121 | ext_block_hdr(path[cur_ppos+1].p_bh); | |
122 | } | |
123 | ||
fc04cb49 AF |
124 | path[leaf_ppos].p_ext = *extent = NULL; |
125 | ||
126 | eh = path[leaf_ppos].p_hdr; | |
127 | if (le16_to_cpu(eh->eh_entries) == 0) | |
128 | /* empty leaf is found */ | |
129 | return -ENODATA; | |
130 | ||
748de673 AF |
131 | /* leaf block */ |
132 | path[leaf_ppos].p_ext = *extent = | |
133 | EXT_FIRST_EXTENT(path[leaf_ppos].p_hdr); | |
fc04cb49 | 134 | path[leaf_ppos].p_block = |
bf89d16f | 135 | ext4_ext_pblock(path[leaf_ppos].p_ext); |
748de673 AF |
136 | return 0; |
137 | } | |
138 | } | |
139 | /* We found the last extent */ | |
140 | return 1; | |
141 | } | |
142 | ||
143 | /** | |
fc04cb49 | 144 | * double_down_write_data_sem - Acquire two inodes' write lock of i_data_sem |
748de673 | 145 | * |
03bd8b9b | 146 | * Acquire write lock of i_data_sem of the two inodes |
748de673 AF |
147 | */ |
148 | static void | |
03bd8b9b | 149 | double_down_write_data_sem(struct inode *first, struct inode *second) |
748de673 | 150 | { |
03bd8b9b DM |
151 | if (first < second) { |
152 | down_write(&EXT4_I(first)->i_data_sem); | |
153 | down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING); | |
154 | } else { | |
155 | down_write(&EXT4_I(second)->i_data_sem); | |
156 | down_write_nested(&EXT4_I(first)->i_data_sem, SINGLE_DEPTH_NESTING); | |
748de673 | 157 | |
748de673 | 158 | } |
748de673 AF |
159 | } |
160 | ||
161 | /** | |
fc04cb49 | 162 | * double_up_write_data_sem - Release two inodes' write lock of i_data_sem |
748de673 AF |
163 | * |
164 | * @orig_inode: original inode structure to be released its lock first | |
165 | * @donor_inode: donor inode structure to be released its lock second | |
fc04cb49 | 166 | * Release write lock of i_data_sem of two inodes (orig and donor). |
748de673 AF |
167 | */ |
168 | static void | |
fc04cb49 | 169 | double_up_write_data_sem(struct inode *orig_inode, struct inode *donor_inode) |
748de673 | 170 | { |
748de673 AF |
171 | up_write(&EXT4_I(orig_inode)->i_data_sem); |
172 | up_write(&EXT4_I(donor_inode)->i_data_sem); | |
173 | } | |
174 | ||
175 | /** | |
176 | * mext_insert_across_blocks - Insert extents across leaf block | |
177 | * | |
178 | * @handle: journal handle | |
179 | * @orig_inode: original inode | |
180 | * @o_start: first original extent to be changed | |
181 | * @o_end: last original extent to be changed | |
182 | * @start_ext: first new extent to be inserted | |
183 | * @new_ext: middle of new extent to be inserted | |
184 | * @end_ext: last new extent to be inserted | |
185 | * | |
186 | * Allocate a new leaf block and insert extents into it. Return 0 on success, | |
187 | * or a negative error value on failure. | |
188 | */ | |
189 | static int | |
190 | mext_insert_across_blocks(handle_t *handle, struct inode *orig_inode, | |
191 | struct ext4_extent *o_start, struct ext4_extent *o_end, | |
192 | struct ext4_extent *start_ext, struct ext4_extent *new_ext, | |
193 | struct ext4_extent *end_ext) | |
194 | { | |
195 | struct ext4_ext_path *orig_path = NULL; | |
196 | ext4_lblk_t eblock = 0; | |
197 | int new_flag = 0; | |
198 | int end_flag = 0; | |
199 | int err = 0; | |
200 | ||
201 | if (start_ext->ee_len && new_ext->ee_len && end_ext->ee_len) { | |
202 | if (o_start == o_end) { | |
203 | ||
204 | /* start_ext new_ext end_ext | |
205 | * donor |---------|-----------|--------| | |
206 | * orig |------------------------------| | |
207 | */ | |
208 | end_flag = 1; | |
209 | } else { | |
210 | ||
211 | /* start_ext new_ext end_ext | |
212 | * donor |---------|----------|---------| | |
213 | * orig |---------------|--------------| | |
214 | */ | |
215 | o_end->ee_block = end_ext->ee_block; | |
216 | o_end->ee_len = end_ext->ee_len; | |
bf89d16f | 217 | ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext)); |
748de673 AF |
218 | } |
219 | ||
220 | o_start->ee_len = start_ext->ee_len; | |
5fd5249a | 221 | eblock = le32_to_cpu(start_ext->ee_block); |
748de673 AF |
222 | new_flag = 1; |
223 | ||
224 | } else if (start_ext->ee_len && new_ext->ee_len && | |
225 | !end_ext->ee_len && o_start == o_end) { | |
226 | ||
227 | /* start_ext new_ext | |
228 | * donor |--------------|---------------| | |
229 | * orig |------------------------------| | |
230 | */ | |
231 | o_start->ee_len = start_ext->ee_len; | |
5fd5249a | 232 | eblock = le32_to_cpu(start_ext->ee_block); |
748de673 AF |
233 | new_flag = 1; |
234 | ||
235 | } else if (!start_ext->ee_len && new_ext->ee_len && | |
236 | end_ext->ee_len && o_start == o_end) { | |
237 | ||
238 | /* new_ext end_ext | |
239 | * donor |--------------|---------------| | |
240 | * orig |------------------------------| | |
241 | */ | |
242 | o_end->ee_block = end_ext->ee_block; | |
243 | o_end->ee_len = end_ext->ee_len; | |
bf89d16f | 244 | ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext)); |
748de673 AF |
245 | |
246 | /* | |
247 | * Set 0 to the extent block if new_ext was | |
248 | * the first block. | |
249 | */ | |
250 | if (new_ext->ee_block) | |
251 | eblock = le32_to_cpu(new_ext->ee_block); | |
252 | ||
253 | new_flag = 1; | |
254 | } else { | |
255 | ext4_debug("ext4 move extent: Unexpected insert case\n"); | |
256 | return -EIO; | |
257 | } | |
258 | ||
259 | if (new_flag) { | |
e8505970 | 260 | err = get_ext_path(orig_inode, eblock, &orig_path); |
347fa6f1 | 261 | if (err) |
748de673 AF |
262 | goto out; |
263 | ||
264 | if (ext4_ext_insert_extent(handle, orig_inode, | |
0031462b | 265 | orig_path, new_ext, 0)) |
748de673 AF |
266 | goto out; |
267 | } | |
268 | ||
269 | if (end_flag) { | |
e8505970 AF |
270 | err = get_ext_path(orig_inode, |
271 | le32_to_cpu(end_ext->ee_block) - 1, &orig_path); | |
347fa6f1 | 272 | if (err) |
748de673 AF |
273 | goto out; |
274 | ||
275 | if (ext4_ext_insert_extent(handle, orig_inode, | |
0031462b | 276 | orig_path, end_ext, 0)) |
748de673 AF |
277 | goto out; |
278 | } | |
279 | out: | |
280 | if (orig_path) { | |
281 | ext4_ext_drop_refs(orig_path); | |
282 | kfree(orig_path); | |
283 | } | |
284 | ||
285 | return err; | |
286 | ||
287 | } | |
288 | ||
289 | /** | |
290 | * mext_insert_inside_block - Insert new extent to the extent block | |
291 | * | |
292 | * @o_start: first original extent to be moved | |
293 | * @o_end: last original extent to be moved | |
294 | * @start_ext: first new extent to be inserted | |
295 | * @new_ext: middle of new extent to be inserted | |
296 | * @end_ext: last new extent to be inserted | |
297 | * @eh: extent header of target leaf block | |
298 | * @range_to_move: used to decide how to insert extent | |
299 | * | |
300 | * Insert extents into the leaf block. The extent (@o_start) is overwritten | |
301 | * by inserted extents. | |
302 | */ | |
303 | static void | |
304 | mext_insert_inside_block(struct ext4_extent *o_start, | |
305 | struct ext4_extent *o_end, | |
306 | struct ext4_extent *start_ext, | |
307 | struct ext4_extent *new_ext, | |
308 | struct ext4_extent *end_ext, | |
309 | struct ext4_extent_header *eh, | |
310 | int range_to_move) | |
311 | { | |
312 | int i = 0; | |
313 | unsigned long len; | |
314 | ||
315 | /* Move the existing extents */ | |
316 | if (range_to_move && o_end < EXT_LAST_EXTENT(eh)) { | |
317 | len = (unsigned long)(EXT_LAST_EXTENT(eh) + 1) - | |
318 | (unsigned long)(o_end + 1); | |
319 | memmove(o_end + 1 + range_to_move, o_end + 1, len); | |
320 | } | |
321 | ||
322 | /* Insert start entry */ | |
323 | if (start_ext->ee_len) | |
324 | o_start[i++].ee_len = start_ext->ee_len; | |
325 | ||
326 | /* Insert new entry */ | |
327 | if (new_ext->ee_len) { | |
328 | o_start[i] = *new_ext; | |
bf89d16f | 329 | ext4_ext_store_pblock(&o_start[i++], ext4_ext_pblock(new_ext)); |
748de673 AF |
330 | } |
331 | ||
332 | /* Insert end entry */ | |
333 | if (end_ext->ee_len) | |
334 | o_start[i] = *end_ext; | |
335 | ||
336 | /* Increment the total entries counter on the extent block */ | |
337 | le16_add_cpu(&eh->eh_entries, range_to_move); | |
338 | } | |
339 | ||
340 | /** | |
341 | * mext_insert_extents - Insert new extent | |
342 | * | |
343 | * @handle: journal handle | |
344 | * @orig_inode: original inode | |
345 | * @orig_path: path indicates first extent to be changed | |
346 | * @o_start: first original extent to be changed | |
347 | * @o_end: last original extent to be changed | |
348 | * @start_ext: first new extent to be inserted | |
349 | * @new_ext: middle of new extent to be inserted | |
350 | * @end_ext: last new extent to be inserted | |
351 | * | |
352 | * Call the function to insert extents. If we cannot add more extents into | |
353 | * the leaf block, we call mext_insert_across_blocks() to create a | |
354 | * new leaf block. Otherwise call mext_insert_inside_block(). Return 0 | |
355 | * on success, or a negative error value on failure. | |
356 | */ | |
357 | static int | |
358 | mext_insert_extents(handle_t *handle, struct inode *orig_inode, | |
359 | struct ext4_ext_path *orig_path, | |
360 | struct ext4_extent *o_start, | |
361 | struct ext4_extent *o_end, | |
362 | struct ext4_extent *start_ext, | |
363 | struct ext4_extent *new_ext, | |
364 | struct ext4_extent *end_ext) | |
365 | { | |
366 | struct ext4_extent_header *eh; | |
367 | unsigned long need_slots, slots_range; | |
368 | int range_to_move, depth, ret; | |
369 | ||
370 | /* | |
371 | * The extents need to be inserted | |
372 | * start_extent + new_extent + end_extent. | |
373 | */ | |
374 | need_slots = (start_ext->ee_len ? 1 : 0) + (end_ext->ee_len ? 1 : 0) + | |
375 | (new_ext->ee_len ? 1 : 0); | |
376 | ||
377 | /* The number of slots between start and end */ | |
378 | slots_range = ((unsigned long)(o_end + 1) - (unsigned long)o_start + 1) | |
379 | / sizeof(struct ext4_extent); | |
380 | ||
381 | /* Range to move the end of extent */ | |
382 | range_to_move = need_slots - slots_range; | |
383 | depth = orig_path->p_depth; | |
384 | orig_path += depth; | |
385 | eh = orig_path->p_hdr; | |
386 | ||
387 | if (depth) { | |
388 | /* Register to journal */ | |
389 | ret = ext4_journal_get_write_access(handle, orig_path->p_bh); | |
390 | if (ret) | |
391 | return ret; | |
392 | } | |
393 | ||
394 | /* Expansion */ | |
395 | if (range_to_move > 0 && | |
396 | (range_to_move > le16_to_cpu(eh->eh_max) | |
397 | - le16_to_cpu(eh->eh_entries))) { | |
398 | ||
399 | ret = mext_insert_across_blocks(handle, orig_inode, o_start, | |
400 | o_end, start_ext, new_ext, end_ext); | |
401 | if (ret < 0) | |
402 | return ret; | |
403 | } else | |
404 | mext_insert_inside_block(o_start, o_end, start_ext, new_ext, | |
405 | end_ext, eh, range_to_move); | |
406 | ||
407 | if (depth) { | |
408 | ret = ext4_handle_dirty_metadata(handle, orig_inode, | |
409 | orig_path->p_bh); | |
410 | if (ret) | |
411 | return ret; | |
412 | } else { | |
413 | ret = ext4_mark_inode_dirty(handle, orig_inode); | |
414 | if (ret < 0) | |
415 | return ret; | |
416 | } | |
417 | ||
418 | return 0; | |
419 | } | |
420 | ||
421 | /** | |
422 | * mext_leaf_block - Move one leaf extent block into the inode. | |
423 | * | |
424 | * @handle: journal handle | |
425 | * @orig_inode: original inode | |
426 | * @orig_path: path indicates first extent to be changed | |
427 | * @dext: donor extent | |
428 | * @from: start offset on the target file | |
429 | * | |
430 | * In order to insert extents into the leaf block, we must divide the extent | |
431 | * in the leaf block into three extents. The one is located to be inserted | |
432 | * extents, and the others are located around it. | |
433 | * | |
434 | * Therefore, this function creates structures to save extents of the leaf | |
435 | * block, and inserts extents by calling mext_insert_extents() with | |
436 | * created extents. Return 0 on success, or a negative error value on failure. | |
437 | */ | |
438 | static int | |
439 | mext_leaf_block(handle_t *handle, struct inode *orig_inode, | |
440 | struct ext4_ext_path *orig_path, struct ext4_extent *dext, | |
441 | ext4_lblk_t *from) | |
442 | { | |
443 | struct ext4_extent *oext, *o_start, *o_end, *prev_ext; | |
444 | struct ext4_extent new_ext, start_ext, end_ext; | |
445 | ext4_lblk_t new_ext_end; | |
748de673 AF |
446 | int oext_alen, new_ext_alen, end_ext_alen; |
447 | int depth = ext_depth(orig_inode); | |
448 | int ret; | |
449 | ||
c26d0bad | 450 | start_ext.ee_block = end_ext.ee_block = 0; |
748de673 AF |
451 | o_start = o_end = oext = orig_path[depth].p_ext; |
452 | oext_alen = ext4_ext_get_actual_len(oext); | |
453 | start_ext.ee_len = end_ext.ee_len = 0; | |
454 | ||
455 | new_ext.ee_block = cpu_to_le32(*from); | |
bf89d16f | 456 | ext4_ext_store_pblock(&new_ext, ext4_ext_pblock(dext)); |
748de673 AF |
457 | new_ext.ee_len = dext->ee_len; |
458 | new_ext_alen = ext4_ext_get_actual_len(&new_ext); | |
459 | new_ext_end = le32_to_cpu(new_ext.ee_block) + new_ext_alen - 1; | |
748de673 AF |
460 | |
461 | /* | |
462 | * Case: original extent is first | |
463 | * oext |--------| | |
464 | * new_ext |--| | |
465 | * start_ext |--| | |
466 | */ | |
467 | if (le32_to_cpu(oext->ee_block) < le32_to_cpu(new_ext.ee_block) && | |
468 | le32_to_cpu(new_ext.ee_block) < | |
469 | le32_to_cpu(oext->ee_block) + oext_alen) { | |
470 | start_ext.ee_len = cpu_to_le16(le32_to_cpu(new_ext.ee_block) - | |
471 | le32_to_cpu(oext->ee_block)); | |
5fd5249a | 472 | start_ext.ee_block = oext->ee_block; |
748de673 AF |
473 | copy_extent_status(oext, &start_ext); |
474 | } else if (oext > EXT_FIRST_EXTENT(orig_path[depth].p_hdr)) { | |
475 | prev_ext = oext - 1; | |
476 | /* | |
477 | * We can merge new_ext into previous extent, | |
478 | * if these are contiguous and same extent type. | |
479 | */ | |
480 | if (ext4_can_extents_be_merged(orig_inode, prev_ext, | |
481 | &new_ext)) { | |
482 | o_start = prev_ext; | |
483 | start_ext.ee_len = cpu_to_le16( | |
484 | ext4_ext_get_actual_len(prev_ext) + | |
485 | new_ext_alen); | |
5fd5249a | 486 | start_ext.ee_block = oext->ee_block; |
748de673 AF |
487 | copy_extent_status(prev_ext, &start_ext); |
488 | new_ext.ee_len = 0; | |
489 | } | |
490 | } | |
491 | ||
492 | /* | |
493 | * Case: new_ext_end must be less than oext | |
494 | * oext |-----------| | |
495 | * new_ext |-------| | |
496 | */ | |
2147b1a6 | 497 | if (le32_to_cpu(oext->ee_block) + oext_alen - 1 < new_ext_end) { |
24676da4 | 498 | EXT4_ERROR_INODE(orig_inode, |
2147b1a6 AF |
499 | "new_ext_end(%u) should be less than or equal to " |
500 | "oext->ee_block(%u) + oext_alen(%d) - 1", | |
501 | new_ext_end, le32_to_cpu(oext->ee_block), | |
502 | oext_alen); | |
503 | ret = -EIO; | |
504 | goto out; | |
505 | } | |
748de673 AF |
506 | |
507 | /* | |
508 | * Case: new_ext is smaller than original extent | |
509 | * oext |---------------| | |
510 | * new_ext |-----------| | |
511 | * end_ext |---| | |
512 | */ | |
513 | if (le32_to_cpu(oext->ee_block) <= new_ext_end && | |
514 | new_ext_end < le32_to_cpu(oext->ee_block) + oext_alen - 1) { | |
515 | end_ext.ee_len = | |
516 | cpu_to_le16(le32_to_cpu(oext->ee_block) + | |
517 | oext_alen - 1 - new_ext_end); | |
518 | copy_extent_status(oext, &end_ext); | |
519 | end_ext_alen = ext4_ext_get_actual_len(&end_ext); | |
520 | ext4_ext_store_pblock(&end_ext, | |
bf89d16f | 521 | (ext4_ext_pblock(o_end) + oext_alen - end_ext_alen)); |
748de673 AF |
522 | end_ext.ee_block = |
523 | cpu_to_le32(le32_to_cpu(o_end->ee_block) + | |
524 | oext_alen - end_ext_alen); | |
525 | } | |
526 | ||
527 | ret = mext_insert_extents(handle, orig_inode, orig_path, o_start, | |
528 | o_end, &start_ext, &new_ext, &end_ext); | |
2147b1a6 | 529 | out: |
748de673 AF |
530 | return ret; |
531 | } | |
532 | ||
533 | /** | |
534 | * mext_calc_swap_extents - Calculate extents for extent swapping. | |
535 | * | |
536 | * @tmp_dext: the extent that will belong to the original inode | |
537 | * @tmp_oext: the extent that will belong to the donor inode | |
538 | * @orig_off: block offset of original inode | |
539 | * @donor_off: block offset of donor inode | |
92c28159 | 540 | * @max_count: the maximum length of extents |
c40ce3c9 AF |
541 | * |
542 | * Return 0 on success, or a negative error value on failure. | |
748de673 | 543 | */ |
c40ce3c9 | 544 | static int |
748de673 AF |
545 | mext_calc_swap_extents(struct ext4_extent *tmp_dext, |
546 | struct ext4_extent *tmp_oext, | |
547 | ext4_lblk_t orig_off, ext4_lblk_t donor_off, | |
548 | ext4_lblk_t max_count) | |
549 | { | |
550 | ext4_lblk_t diff, orig_diff; | |
551 | struct ext4_extent dext_old, oext_old; | |
552 | ||
c40ce3c9 AF |
553 | BUG_ON(orig_off != donor_off); |
554 | ||
555 | /* original and donor extents have to cover the same block offset */ | |
556 | if (orig_off < le32_to_cpu(tmp_oext->ee_block) || | |
557 | le32_to_cpu(tmp_oext->ee_block) + | |
558 | ext4_ext_get_actual_len(tmp_oext) - 1 < orig_off) | |
559 | return -ENODATA; | |
560 | ||
561 | if (orig_off < le32_to_cpu(tmp_dext->ee_block) || | |
562 | le32_to_cpu(tmp_dext->ee_block) + | |
563 | ext4_ext_get_actual_len(tmp_dext) - 1 < orig_off) | |
564 | return -ENODATA; | |
565 | ||
748de673 AF |
566 | dext_old = *tmp_dext; |
567 | oext_old = *tmp_oext; | |
568 | ||
569 | /* When tmp_dext is too large, pick up the target range. */ | |
570 | diff = donor_off - le32_to_cpu(tmp_dext->ee_block); | |
571 | ||
bf89d16f | 572 | ext4_ext_store_pblock(tmp_dext, ext4_ext_pblock(tmp_dext) + diff); |
ba39ebb6 WY |
573 | le32_add_cpu(&tmp_dext->ee_block, diff); |
574 | le16_add_cpu(&tmp_dext->ee_len, -diff); | |
748de673 AF |
575 | |
576 | if (max_count < ext4_ext_get_actual_len(tmp_dext)) | |
577 | tmp_dext->ee_len = cpu_to_le16(max_count); | |
578 | ||
579 | orig_diff = orig_off - le32_to_cpu(tmp_oext->ee_block); | |
bf89d16f | 580 | ext4_ext_store_pblock(tmp_oext, ext4_ext_pblock(tmp_oext) + orig_diff); |
748de673 AF |
581 | |
582 | /* Adjust extent length if donor extent is larger than orig */ | |
583 | if (ext4_ext_get_actual_len(tmp_dext) > | |
584 | ext4_ext_get_actual_len(tmp_oext) - orig_diff) | |
585 | tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_oext->ee_len) - | |
586 | orig_diff); | |
587 | ||
588 | tmp_oext->ee_len = cpu_to_le16(ext4_ext_get_actual_len(tmp_dext)); | |
589 | ||
590 | copy_extent_status(&oext_old, tmp_dext); | |
591 | copy_extent_status(&dext_old, tmp_oext); | |
c40ce3c9 AF |
592 | |
593 | return 0; | |
748de673 AF |
594 | } |
595 | ||
8c854473 DM |
596 | /** |
597 | * mext_check_coverage - Check that all extents in range has the same type | |
598 | * | |
599 | * @inode: inode in question | |
600 | * @from: block offset of inode | |
601 | * @count: block count to be checked | |
602 | * @uninit: extents expected to be uninitialized | |
603 | * @err: pointer to save error value | |
604 | * | |
605 | * Return 1 if all extents in range has expected type, and zero otherwise. | |
606 | */ | |
607 | static int | |
608 | mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count, | |
609 | int uninit, int *err) | |
610 | { | |
611 | struct ext4_ext_path *path = NULL; | |
612 | struct ext4_extent *ext; | |
613 | ext4_lblk_t last = from + count; | |
614 | while (from < last) { | |
615 | *err = get_ext_path(inode, from, &path); | |
616 | if (*err) | |
617 | return 0; | |
618 | ext = path[ext_depth(inode)].p_ext; | |
619 | if (!ext) { | |
620 | ext4_ext_drop_refs(path); | |
621 | return 0; | |
622 | } | |
623 | if (uninit != ext4_ext_is_uninitialized(ext)) { | |
624 | ext4_ext_drop_refs(path); | |
625 | return 0; | |
626 | } | |
627 | from += ext4_ext_get_actual_len(ext); | |
628 | ext4_ext_drop_refs(path); | |
629 | } | |
630 | return 1; | |
631 | } | |
632 | ||
748de673 AF |
633 | /** |
634 | * mext_replace_branches - Replace original extents with new extents | |
635 | * | |
636 | * @handle: journal handle | |
637 | * @orig_inode: original inode | |
638 | * @donor_inode: donor inode | |
639 | * @from: block offset of orig_inode | |
640 | * @count: block count to be replaced | |
f868a48d | 641 | * @err: pointer to save return value |
748de673 AF |
642 | * |
643 | * Replace original inode extents and donor inode extents page by page. | |
644 | * We implement this replacement in the following three steps: | |
645 | * 1. Save the block information of original and donor inodes into | |
646 | * dummy extents. | |
647 | * 2. Change the block information of original inode to point at the | |
648 | * donor inode blocks. | |
649 | * 3. Change the block information of donor inode to point at the saved | |
650 | * original inode blocks in the dummy extents. | |
651 | * | |
f868a48d | 652 | * Return replaced block count. |
748de673 AF |
653 | */ |
654 | static int | |
655 | mext_replace_branches(handle_t *handle, struct inode *orig_inode, | |
656 | struct inode *donor_inode, ext4_lblk_t from, | |
f868a48d | 657 | ext4_lblk_t count, int *err) |
748de673 AF |
658 | { |
659 | struct ext4_ext_path *orig_path = NULL; | |
660 | struct ext4_ext_path *donor_path = NULL; | |
661 | struct ext4_extent *oext, *dext; | |
662 | struct ext4_extent tmp_dext, tmp_oext; | |
663 | ext4_lblk_t orig_off = from, donor_off = from; | |
748de673 AF |
664 | int depth; |
665 | int replaced_count = 0; | |
666 | int dext_alen; | |
667 | ||
748de673 | 668 | /* Get the original extent for the block "orig_off" */ |
f868a48d AF |
669 | *err = get_ext_path(orig_inode, orig_off, &orig_path); |
670 | if (*err) | |
748de673 AF |
671 | goto out; |
672 | ||
673 | /* Get the donor extent for the head */ | |
f868a48d AF |
674 | *err = get_ext_path(donor_inode, donor_off, &donor_path); |
675 | if (*err) | |
748de673 AF |
676 | goto out; |
677 | depth = ext_depth(orig_inode); | |
678 | oext = orig_path[depth].p_ext; | |
679 | tmp_oext = *oext; | |
680 | ||
681 | depth = ext_depth(donor_inode); | |
682 | dext = donor_path[depth].p_ext; | |
683 | tmp_dext = *dext; | |
684 | ||
f868a48d | 685 | *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off, |
748de673 | 686 | donor_off, count); |
f868a48d | 687 | if (*err) |
c40ce3c9 | 688 | goto out; |
748de673 AF |
689 | |
690 | /* Loop for the donor extents */ | |
691 | while (1) { | |
692 | /* The extent for donor must be found. */ | |
2147b1a6 | 693 | if (!dext) { |
24676da4 | 694 | EXT4_ERROR_INODE(donor_inode, |
2147b1a6 | 695 | "The extent for donor must be found"); |
f868a48d | 696 | *err = -EIO; |
2147b1a6 AF |
697 | goto out; |
698 | } else if (donor_off != le32_to_cpu(tmp_dext.ee_block)) { | |
24676da4 | 699 | EXT4_ERROR_INODE(donor_inode, |
2147b1a6 AF |
700 | "Donor offset(%u) and the first block of donor " |
701 | "extent(%u) should be equal", | |
702 | donor_off, | |
703 | le32_to_cpu(tmp_dext.ee_block)); | |
f868a48d | 704 | *err = -EIO; |
2147b1a6 AF |
705 | goto out; |
706 | } | |
748de673 AF |
707 | |
708 | /* Set donor extent to orig extent */ | |
f868a48d | 709 | *err = mext_leaf_block(handle, orig_inode, |
748de673 | 710 | orig_path, &tmp_dext, &orig_off); |
f868a48d | 711 | if (*err) |
748de673 AF |
712 | goto out; |
713 | ||
714 | /* Set orig extent to donor extent */ | |
f868a48d | 715 | *err = mext_leaf_block(handle, donor_inode, |
748de673 | 716 | donor_path, &tmp_oext, &donor_off); |
f868a48d | 717 | if (*err) |
748de673 AF |
718 | goto out; |
719 | ||
720 | dext_alen = ext4_ext_get_actual_len(&tmp_dext); | |
721 | replaced_count += dext_alen; | |
722 | donor_off += dext_alen; | |
723 | orig_off += dext_alen; | |
724 | ||
725 | /* Already moved the expected blocks */ | |
726 | if (replaced_count >= count) | |
727 | break; | |
728 | ||
729 | if (orig_path) | |
730 | ext4_ext_drop_refs(orig_path); | |
f868a48d AF |
731 | *err = get_ext_path(orig_inode, orig_off, &orig_path); |
732 | if (*err) | |
748de673 AF |
733 | goto out; |
734 | depth = ext_depth(orig_inode); | |
735 | oext = orig_path[depth].p_ext; | |
748de673 AF |
736 | tmp_oext = *oext; |
737 | ||
738 | if (donor_path) | |
739 | ext4_ext_drop_refs(donor_path); | |
f868a48d AF |
740 | *err = get_ext_path(donor_inode, donor_off, &donor_path); |
741 | if (*err) | |
748de673 AF |
742 | goto out; |
743 | depth = ext_depth(donor_inode); | |
744 | dext = donor_path[depth].p_ext; | |
748de673 AF |
745 | tmp_dext = *dext; |
746 | ||
f868a48d | 747 | *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off, |
c40ce3c9 | 748 | donor_off, count - replaced_count); |
f868a48d | 749 | if (*err) |
c40ce3c9 | 750 | goto out; |
748de673 AF |
751 | } |
752 | ||
753 | out: | |
754 | if (orig_path) { | |
755 | ext4_ext_drop_refs(orig_path); | |
756 | kfree(orig_path); | |
757 | } | |
758 | if (donor_path) { | |
759 | ext4_ext_drop_refs(donor_path); | |
760 | kfree(donor_path); | |
761 | } | |
762 | ||
ac48b0a1 AF |
763 | ext4_ext_invalidate_cache(orig_inode); |
764 | ext4_ext_invalidate_cache(donor_inode); | |
765 | ||
f868a48d | 766 | return replaced_count; |
748de673 AF |
767 | } |
768 | ||
bb557488 DM |
769 | /** |
770 | * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2 | |
771 | * | |
772 | * @inode1: the inode structure | |
773 | * @inode2: the inode structure | |
774 | * @index: page index | |
775 | * @page: result page vector | |
776 | * | |
777 | * Grab two locked pages for inode's by inode order | |
778 | */ | |
779 | static int | |
780 | mext_page_double_lock(struct inode *inode1, struct inode *inode2, | |
781 | pgoff_t index, struct page *page[2]) | |
782 | { | |
783 | struct address_space *mapping[2]; | |
784 | unsigned fl = AOP_FLAG_NOFS; | |
785 | ||
786 | BUG_ON(!inode1 || !inode2); | |
787 | if (inode1 < inode2) { | |
788 | mapping[0] = inode1->i_mapping; | |
789 | mapping[1] = inode2->i_mapping; | |
790 | } else { | |
791 | mapping[0] = inode2->i_mapping; | |
792 | mapping[1] = inode1->i_mapping; | |
793 | } | |
794 | ||
795 | page[0] = grab_cache_page_write_begin(mapping[0], index, fl); | |
796 | if (!page[0]) | |
797 | return -ENOMEM; | |
798 | ||
799 | page[1] = grab_cache_page_write_begin(mapping[1], index, fl); | |
800 | if (!page[1]) { | |
801 | unlock_page(page[0]); | |
802 | page_cache_release(page[0]); | |
803 | return -ENOMEM; | |
804 | } | |
805 | ||
806 | if (inode1 > inode2) { | |
807 | struct page *tmp; | |
808 | tmp = page[0]; | |
809 | page[0] = page[1]; | |
810 | page[1] = tmp; | |
811 | } | |
812 | return 0; | |
813 | } | |
814 | ||
815 | /* Force page buffers uptodate w/o dropping page's lock */ | |
816 | static int | |
817 | mext_page_mkuptodate(struct page *page, unsigned from, unsigned to) | |
818 | { | |
819 | struct inode *inode = page->mapping->host; | |
820 | sector_t block; | |
821 | struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; | |
822 | unsigned int blocksize, block_start, block_end; | |
823 | int i, err, nr = 0, partial = 0; | |
824 | BUG_ON(!PageLocked(page)); | |
825 | BUG_ON(PageWriteback(page)); | |
826 | ||
827 | if (PageUptodate(page)) | |
828 | return 0; | |
829 | ||
830 | blocksize = 1 << inode->i_blkbits; | |
831 | if (!page_has_buffers(page)) | |
832 | create_empty_buffers(page, blocksize, 0); | |
833 | ||
834 | head = page_buffers(page); | |
835 | block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
836 | for (bh = head, block_start = 0; bh != head || !block_start; | |
837 | block++, block_start = block_end, bh = bh->b_this_page) { | |
838 | block_end = block_start + blocksize; | |
839 | if (block_end <= from || block_start >= to) { | |
840 | if (!buffer_uptodate(bh)) | |
841 | partial = 1; | |
842 | continue; | |
843 | } | |
844 | if (buffer_uptodate(bh)) | |
845 | continue; | |
846 | if (!buffer_mapped(bh)) { | |
847 | int err = 0; | |
848 | err = ext4_get_block(inode, block, bh, 0); | |
849 | if (err) { | |
850 | SetPageError(page); | |
851 | return err; | |
852 | } | |
853 | if (!buffer_mapped(bh)) { | |
854 | zero_user(page, block_start, blocksize); | |
855 | if (!err) | |
856 | set_buffer_uptodate(bh); | |
857 | continue; | |
858 | } | |
859 | } | |
860 | BUG_ON(nr >= MAX_BUF_PER_PAGE); | |
861 | arr[nr++] = bh; | |
862 | } | |
863 | /* No io required */ | |
864 | if (!nr) | |
865 | goto out; | |
866 | ||
867 | for (i = 0; i < nr; i++) { | |
868 | bh = arr[i]; | |
869 | if (!bh_uptodate_or_lock(bh)) { | |
870 | err = bh_submit_read(bh); | |
871 | if (err) | |
872 | return err; | |
873 | } | |
874 | } | |
875 | out: | |
876 | if (!partial) | |
877 | SetPageUptodate(page); | |
878 | return 0; | |
879 | } | |
880 | ||
748de673 AF |
881 | /** |
882 | * move_extent_per_page - Move extent data per page | |
883 | * | |
884 | * @o_filp: file structure of original file | |
885 | * @donor_inode: donor inode | |
886 | * @orig_page_offset: page index on original file | |
887 | * @data_offset_in_page: block index where data swapping starts | |
888 | * @block_len_in_page: the number of blocks to be swapped | |
889 | * @uninit: orig extent is uninitialized or not | |
f868a48d | 890 | * @err: pointer to save return value |
748de673 AF |
891 | * |
892 | * Save the data in original inode blocks and replace original inode extents | |
893 | * with donor inode extents by calling mext_replace_branches(). | |
f868a48d AF |
894 | * Finally, write out the saved data in new original inode blocks. Return |
895 | * replaced block count. | |
748de673 AF |
896 | */ |
897 | static int | |
44fc48f7 | 898 | move_extent_per_page(struct file *o_filp, struct inode *donor_inode, |
748de673 | 899 | pgoff_t orig_page_offset, int data_offset_in_page, |
f868a48d | 900 | int block_len_in_page, int uninit, int *err) |
748de673 AF |
901 | { |
902 | struct inode *orig_inode = o_filp->f_dentry->d_inode; | |
bb557488 | 903 | struct page *pagep[2] = {NULL, NULL}; |
748de673 AF |
904 | handle_t *handle; |
905 | ext4_lblk_t orig_blk_offset; | |
906 | long long offs = orig_page_offset << PAGE_CACHE_SHIFT; | |
907 | unsigned long blocksize = orig_inode->i_sb->s_blocksize; | |
908 | unsigned int w_flags = 0; | |
f868a48d | 909 | unsigned int tmp_data_size, data_size, replaced_size; |
bb557488 | 910 | int err2, jblocks, retries = 0; |
f868a48d | 911 | int replaced_count = 0; |
bb557488 | 912 | int from = data_offset_in_page << orig_inode->i_blkbits; |
748de673 AF |
913 | int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits; |
914 | ||
915 | /* | |
916 | * It needs twice the amount of ordinary journal buffers because | |
917 | * inode and donor_inode may change each different metadata blocks. | |
918 | */ | |
bb557488 DM |
919 | again: |
920 | *err = 0; | |
748de673 AF |
921 | jblocks = ext4_writepage_trans_blocks(orig_inode) * 2; |
922 | handle = ext4_journal_start(orig_inode, jblocks); | |
923 | if (IS_ERR(handle)) { | |
f868a48d AF |
924 | *err = PTR_ERR(handle); |
925 | return 0; | |
748de673 AF |
926 | } |
927 | ||
928 | if (segment_eq(get_fs(), KERNEL_DS)) | |
929 | w_flags |= AOP_FLAG_UNINTERRUPTIBLE; | |
930 | ||
931 | orig_blk_offset = orig_page_offset * blocks_per_page + | |
932 | data_offset_in_page; | |
933 | ||
748de673 AF |
934 | offs = (long long)orig_blk_offset << orig_inode->i_blkbits; |
935 | ||
f868a48d | 936 | /* Calculate data_size */ |
748de673 AF |
937 | if ((orig_blk_offset + block_len_in_page - 1) == |
938 | ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) { | |
939 | /* Replace the last block */ | |
f868a48d | 940 | tmp_data_size = orig_inode->i_size & (blocksize - 1); |
748de673 | 941 | /* |
f868a48d | 942 | * If data_size equal zero, it shows data_size is multiples of |
748de673 AF |
943 | * blocksize. So we set appropriate value. |
944 | */ | |
f868a48d AF |
945 | if (tmp_data_size == 0) |
946 | tmp_data_size = blocksize; | |
748de673 | 947 | |
f868a48d | 948 | data_size = tmp_data_size + |
748de673 | 949 | ((block_len_in_page - 1) << orig_inode->i_blkbits); |
f868a48d AF |
950 | } else |
951 | data_size = block_len_in_page << orig_inode->i_blkbits; | |
952 | ||
953 | replaced_size = data_size; | |
748de673 | 954 | |
bb557488 DM |
955 | *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset, |
956 | pagep); | |
f868a48d | 957 | if (unlikely(*err < 0)) |
bb557488 | 958 | goto stop_journal; |
8c854473 DM |
959 | /* |
960 | * If orig extent was uninitialized it can become initialized | |
961 | * at any time after i_data_sem was dropped, in order to | |
962 | * serialize with delalloc we have recheck extent while we | |
963 | * hold page's lock, if it is still the case data copy is not | |
964 | * necessary, just swap data blocks between orig and donor. | |
965 | */ | |
966 | if (uninit) { | |
967 | double_down_write_data_sem(orig_inode, donor_inode); | |
968 | /* If any of extents in range became initialized we have to | |
969 | * fallback to data copying */ | |
970 | uninit = mext_check_coverage(orig_inode, orig_blk_offset, | |
971 | block_len_in_page, 1, err); | |
972 | if (*err) | |
973 | goto drop_data_sem; | |
748de673 | 974 | |
8c854473 DM |
975 | uninit &= mext_check_coverage(donor_inode, orig_blk_offset, |
976 | block_len_in_page, 1, err); | |
977 | if (*err) | |
978 | goto drop_data_sem; | |
979 | ||
980 | if (!uninit) { | |
981 | double_up_write_data_sem(orig_inode, donor_inode); | |
982 | goto data_copy; | |
983 | } | |
984 | if ((page_has_private(pagep[0]) && | |
985 | !try_to_release_page(pagep[0], 0)) || | |
986 | (page_has_private(pagep[1]) && | |
987 | !try_to_release_page(pagep[1], 0))) { | |
988 | *err = -EBUSY; | |
989 | goto drop_data_sem; | |
990 | } | |
991 | replaced_count = mext_replace_branches(handle, orig_inode, | |
992 | donor_inode, orig_blk_offset, | |
993 | block_len_in_page, err); | |
994 | drop_data_sem: | |
995 | double_up_write_data_sem(orig_inode, donor_inode); | |
996 | goto unlock_pages; | |
997 | } | |
998 | data_copy: | |
bb557488 DM |
999 | *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size); |
1000 | if (*err) | |
1001 | goto unlock_pages; | |
1002 | ||
1003 | /* At this point all buffers in range are uptodate, old mapping layout | |
1004 | * is no longer required, try to drop it now. */ | |
1005 | if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) || | |
1006 | (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) { | |
1007 | *err = -EBUSY; | |
1008 | goto unlock_pages; | |
748de673 AF |
1009 | } |
1010 | ||
f868a48d | 1011 | replaced_count = mext_replace_branches(handle, orig_inode, donor_inode, |
bb557488 DM |
1012 | orig_blk_offset, |
1013 | block_len_in_page, err); | |
1014 | if (*err) { | |
f868a48d AF |
1015 | if (replaced_count) { |
1016 | block_len_in_page = replaced_count; | |
1017 | replaced_size = | |
1018 | block_len_in_page << orig_inode->i_blkbits; | |
ac48b0a1 | 1019 | } else |
bb557488 | 1020 | goto unlock_pages; |
748de673 | 1021 | } |
bb557488 DM |
1022 | /* Perform all necessary steps similar write_begin()/write_end() |
1023 | * but keeping in mind that i_size will not change */ | |
1024 | *err = __block_write_begin(pagep[0], from, from + replaced_size, | |
1025 | ext4_get_block); | |
1026 | if (!*err) | |
1027 | *err = block_commit_write(pagep[0], from, from + replaced_size); | |
748de673 | 1028 | |
bb557488 DM |
1029 | if (unlikely(*err < 0)) |
1030 | goto repair_branches; | |
1031 | ||
1032 | /* Even in case of data=writeback it is reasonable to pin | |
1033 | * inode to transaction, to prevent unexpected data loss */ | |
1034 | *err = ext4_jbd2_file_inode(handle, orig_inode); | |
1035 | ||
1036 | unlock_pages: | |
1037 | unlock_page(pagep[0]); | |
1038 | page_cache_release(pagep[0]); | |
1039 | unlock_page(pagep[1]); | |
1040 | page_cache_release(pagep[1]); | |
1041 | stop_journal: | |
748de673 | 1042 | ext4_journal_stop(handle); |
bb557488 DM |
1043 | /* Buffer was busy because probably is pinned to journal transaction, |
1044 | * force transaction commit may help to free it. */ | |
1045 | if (*err == -EBUSY && ext4_should_retry_alloc(orig_inode->i_sb, | |
1046 | &retries)) | |
1047 | goto again; | |
f868a48d | 1048 | return replaced_count; |
bb557488 DM |
1049 | |
1050 | repair_branches: | |
1051 | /* | |
1052 | * This should never ever happen! | |
1053 | * Extents are swapped already, but we are not able to copy data. | |
1054 | * Try to swap extents to it's original places | |
1055 | */ | |
1056 | double_down_write_data_sem(orig_inode, donor_inode); | |
1057 | replaced_count = mext_replace_branches(handle, donor_inode, orig_inode, | |
1058 | orig_blk_offset, | |
1059 | block_len_in_page, &err2); | |
1060 | double_up_write_data_sem(orig_inode, donor_inode); | |
1061 | if (replaced_count != block_len_in_page) { | |
1062 | EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset), | |
1063 | "Unable to copy data block," | |
1064 | " data will be lost."); | |
1065 | *err = -EIO; | |
1066 | } | |
1067 | replaced_count = 0; | |
1068 | goto unlock_pages; | |
748de673 AF |
1069 | } |
1070 | ||
1071 | /** | |
c437b273 | 1072 | * mext_check_arguments - Check whether move extent can be done |
748de673 AF |
1073 | * |
1074 | * @orig_inode: original inode | |
1075 | * @donor_inode: donor inode | |
1076 | * @orig_start: logical start offset in block for orig | |
1077 | * @donor_start: logical start offset in block for donor | |
1078 | * @len: the number of blocks to be moved | |
748de673 AF |
1079 | * |
1080 | * Check the arguments of ext4_move_extents() whether the files can be | |
1081 | * exchanged with each other. | |
1082 | * Return 0 on success, or a negative error value on failure. | |
1083 | */ | |
1084 | static int | |
1085 | mext_check_arguments(struct inode *orig_inode, | |
446aaa6e KM |
1086 | struct inode *donor_inode, __u64 orig_start, |
1087 | __u64 donor_start, __u64 *len) | |
748de673 | 1088 | { |
70d5d3dc AF |
1089 | ext4_lblk_t orig_blocks, donor_blocks; |
1090 | unsigned int blkbits = orig_inode->i_blkbits; | |
1091 | unsigned int blocksize = 1 << blkbits; | |
1092 | ||
4a58579b AF |
1093 | if (donor_inode->i_mode & (S_ISUID|S_ISGID)) { |
1094 | ext4_debug("ext4 move extent: suid or sgid is set" | |
1095 | " to donor file [ino:orig %lu, donor %lu]\n", | |
1096 | orig_inode->i_ino, donor_inode->i_ino); | |
1097 | return -EINVAL; | |
1098 | } | |
1099 | ||
1f5a81e4 TT |
1100 | if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode)) |
1101 | return -EPERM; | |
1102 | ||
748de673 AF |
1103 | /* Ext4 move extent does not support swapfile */ |
1104 | if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) { | |
1105 | ext4_debug("ext4 move extent: The argument files should " | |
1106 | "not be swapfile [ino:orig %lu, donor %lu]\n", | |
1107 | orig_inode->i_ino, donor_inode->i_ino); | |
1108 | return -EINVAL; | |
1109 | } | |
1110 | ||
748de673 | 1111 | /* Ext4 move extent supports only extent based file */ |
12e9b892 | 1112 | if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) { |
748de673 AF |
1113 | ext4_debug("ext4 move extent: orig file is not extents " |
1114 | "based file [ino:orig %lu]\n", orig_inode->i_ino); | |
1115 | return -EOPNOTSUPP; | |
12e9b892 | 1116 | } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) { |
748de673 AF |
1117 | ext4_debug("ext4 move extent: donor file is not extents " |
1118 | "based file [ino:donor %lu]\n", donor_inode->i_ino); | |
1119 | return -EOPNOTSUPP; | |
1120 | } | |
1121 | ||
1122 | if ((!orig_inode->i_size) || (!donor_inode->i_size)) { | |
1123 | ext4_debug("ext4 move extent: File size is 0 byte\n"); | |
1124 | return -EINVAL; | |
1125 | } | |
1126 | ||
1127 | /* Start offset should be same */ | |
1128 | if (orig_start != donor_start) { | |
1129 | ext4_debug("ext4 move extent: orig and donor's start " | |
1130 | "offset are not same [ino:orig %lu, donor %lu]\n", | |
1131 | orig_inode->i_ino, donor_inode->i_ino); | |
1132 | return -EINVAL; | |
1133 | } | |
1134 | ||
f17722f9 | 1135 | if ((orig_start >= EXT_MAX_BLOCKS) || |
f17722f9 LC |
1136 | (*len > EXT_MAX_BLOCKS) || |
1137 | (orig_start + *len >= EXT_MAX_BLOCKS)) { | |
0a80e986 | 1138 | ext4_debug("ext4 move extent: Can't handle over [%u] blocks " |
f17722f9 | 1139 | "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS, |
748de673 AF |
1140 | orig_inode->i_ino, donor_inode->i_ino); |
1141 | return -EINVAL; | |
1142 | } | |
1143 | ||
1144 | if (orig_inode->i_size > donor_inode->i_size) { | |
70d5d3dc AF |
1145 | donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits; |
1146 | /* TODO: eliminate this artificial restriction */ | |
1147 | if (orig_start >= donor_blocks) { | |
748de673 | 1148 | ext4_debug("ext4 move extent: orig start offset " |
70d5d3dc AF |
1149 | "[%llu] should be less than donor file blocks " |
1150 | "[%u] [ino:orig %lu, donor %lu]\n", | |
1151 | orig_start, donor_blocks, | |
748de673 AF |
1152 | orig_inode->i_ino, donor_inode->i_ino); |
1153 | return -EINVAL; | |
1154 | } | |
1155 | ||
70d5d3dc AF |
1156 | /* TODO: eliminate this artificial restriction */ |
1157 | if (orig_start + *len > donor_blocks) { | |
748de673 | 1158 | ext4_debug("ext4 move extent: End offset [%llu] should " |
70d5d3dc AF |
1159 | "be less than donor file blocks [%u]." |
1160 | "So adjust length from %llu to %llu " | |
748de673 | 1161 | "[ino:orig %lu, donor %lu]\n", |
70d5d3dc AF |
1162 | orig_start + *len, donor_blocks, |
1163 | *len, donor_blocks - orig_start, | |
748de673 | 1164 | orig_inode->i_ino, donor_inode->i_ino); |
70d5d3dc | 1165 | *len = donor_blocks - orig_start; |
748de673 AF |
1166 | } |
1167 | } else { | |
70d5d3dc AF |
1168 | orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits; |
1169 | if (orig_start >= orig_blocks) { | |
748de673 | 1170 | ext4_debug("ext4 move extent: start offset [%llu] " |
70d5d3dc AF |
1171 | "should be less than original file blocks " |
1172 | "[%u] [ino:orig %lu, donor %lu]\n", | |
1173 | orig_start, orig_blocks, | |
748de673 AF |
1174 | orig_inode->i_ino, donor_inode->i_ino); |
1175 | return -EINVAL; | |
1176 | } | |
1177 | ||
70d5d3dc | 1178 | if (orig_start + *len > orig_blocks) { |
748de673 | 1179 | ext4_debug("ext4 move extent: Adjust length " |
70d5d3dc AF |
1180 | "from %llu to %llu. Because it should be " |
1181 | "less than original file blocks " | |
748de673 | 1182 | "[ino:orig %lu, donor %lu]\n", |
70d5d3dc | 1183 | *len, orig_blocks - orig_start, |
748de673 | 1184 | orig_inode->i_ino, donor_inode->i_ino); |
70d5d3dc | 1185 | *len = orig_blocks - orig_start; |
748de673 AF |
1186 | } |
1187 | } | |
1188 | ||
1189 | if (!*len) { | |
92c28159 | 1190 | ext4_debug("ext4 move extent: len should not be 0 " |
748de673 AF |
1191 | "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino, |
1192 | donor_inode->i_ino); | |
1193 | return -EINVAL; | |
1194 | } | |
1195 | ||
1196 | return 0; | |
1197 | } | |
1198 | ||
1199 | /** | |
1200 | * mext_inode_double_lock - Lock i_mutex on both @inode1 and @inode2 | |
1201 | * | |
1202 | * @inode1: the inode structure | |
1203 | * @inode2: the inode structure | |
1204 | * | |
03bd8b9b | 1205 | * Lock two inodes' i_mutex |
748de673 | 1206 | */ |
03bd8b9b | 1207 | static void |
748de673 AF |
1208 | mext_inode_double_lock(struct inode *inode1, struct inode *inode2) |
1209 | { | |
03bd8b9b DM |
1210 | BUG_ON(inode1 == inode2); |
1211 | if (inode1 < inode2) { | |
748de673 AF |
1212 | mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT); |
1213 | mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD); | |
1214 | } else { | |
1215 | mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT); | |
1216 | mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD); | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | /** | |
1221 | * mext_inode_double_unlock - Release i_mutex on both @inode1 and @inode2 | |
1222 | * | |
1223 | * @inode1: the inode that is released first | |
1224 | * @inode2: the inode that is released second | |
1225 | * | |
748de673 AF |
1226 | */ |
1227 | ||
03bd8b9b | 1228 | static void |
748de673 AF |
1229 | mext_inode_double_unlock(struct inode *inode1, struct inode *inode2) |
1230 | { | |
03bd8b9b DM |
1231 | mutex_unlock(&inode1->i_mutex); |
1232 | mutex_unlock(&inode2->i_mutex); | |
748de673 AF |
1233 | } |
1234 | ||
1235 | /** | |
1236 | * ext4_move_extents - Exchange the specified range of a file | |
1237 | * | |
1238 | * @o_filp: file structure of the original file | |
1239 | * @d_filp: file structure of the donor file | |
1240 | * @orig_start: start offset in block for orig | |
1241 | * @donor_start: start offset in block for donor | |
1242 | * @len: the number of blocks to be moved | |
1243 | * @moved_len: moved block length | |
1244 | * | |
1245 | * This function returns 0 and moved block length is set in moved_len | |
1246 | * if succeed, otherwise returns error value. | |
1247 | * | |
1248 | * Note: ext4_move_extents() proceeds the following order. | |
1249 | * 1:ext4_move_extents() calculates the last block number of moving extent | |
1250 | * function by the start block number (orig_start) and the number of blocks | |
1251 | * to be moved (len) specified as arguments. | |
1252 | * If the {orig, donor}_start points a hole, the extent's start offset | |
1253 | * pointed by ext_cur (current extent), holecheck_path, orig_path are set | |
1254 | * after hole behind. | |
1255 | * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent | |
1256 | * or the ext_cur exceeds the block_end which is last logical block number. | |
1257 | * 3:To get the length of continues area, call mext_next_extent() | |
1258 | * specified with the ext_cur (initial value is holecheck_path) re-cursive, | |
1259 | * until find un-continuous extent, the start logical block number exceeds | |
1260 | * the block_end or the extent points to the last extent. | |
1261 | * 4:Exchange the original inode data with donor inode data | |
1262 | * from orig_page_offset to seq_end_page. | |
1263 | * The start indexes of data are specified as arguments. | |
1264 | * That of the original inode is orig_page_offset, | |
1265 | * and the donor inode is also orig_page_offset | |
1266 | * (To easily handle blocksize != pagesize case, the offset for the | |
1267 | * donor inode is block unit). | |
1268 | * 5:Update holecheck_path and orig_path to points a next proceeding extent, | |
1269 | * then returns to step 2. | |
1270 | * 6:Release holecheck_path, orig_path and set the len to moved_len | |
1271 | * which shows the number of moved blocks. | |
1272 | * The moved_len is useful for the command to calculate the file offset | |
1273 | * for starting next move extent ioctl. | |
1274 | * 7:Return 0 on success, or a negative error value on failure. | |
1275 | */ | |
1276 | int | |
1277 | ext4_move_extents(struct file *o_filp, struct file *d_filp, | |
1278 | __u64 orig_start, __u64 donor_start, __u64 len, | |
1279 | __u64 *moved_len) | |
1280 | { | |
1281 | struct inode *orig_inode = o_filp->f_dentry->d_inode; | |
1282 | struct inode *donor_inode = d_filp->f_dentry->d_inode; | |
1283 | struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL; | |
1284 | struct ext4_extent *ext_prev, *ext_cur, *ext_dummy; | |
1285 | ext4_lblk_t block_start = orig_start; | |
1286 | ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0; | |
1287 | ext4_lblk_t rest_blocks; | |
1288 | pgoff_t orig_page_offset = 0, seq_end_page; | |
03bd8b9b | 1289 | int ret, depth, last_extent = 0; |
748de673 AF |
1290 | int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits; |
1291 | int data_offset_in_page; | |
1292 | int block_len_in_page; | |
1293 | int uninit; | |
1294 | ||
03bd8b9b DM |
1295 | if (orig_inode->i_sb != donor_inode->i_sb) { |
1296 | ext4_debug("ext4 move extent: The argument files " | |
1297 | "should be in same FS [ino:orig %lu, donor %lu]\n", | |
1298 | orig_inode->i_ino, donor_inode->i_ino); | |
1299 | return -EINVAL; | |
1300 | } | |
1301 | ||
1302 | /* orig and donor should be different inodes */ | |
1303 | if (orig_inode == donor_inode) { | |
f3ce8064 | 1304 | ext4_debug("ext4 move extent: The argument files should not " |
03bd8b9b | 1305 | "be same inode [ino:orig %lu, donor %lu]\n", |
f3ce8064 TT |
1306 | orig_inode->i_ino, donor_inode->i_ino); |
1307 | return -EINVAL; | |
1308 | } | |
1309 | ||
7247c0ca AF |
1310 | /* Regular file check */ |
1311 | if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) { | |
1312 | ext4_debug("ext4 move extent: The argument files should be " | |
1313 | "regular file [ino:orig %lu, donor %lu]\n", | |
1314 | orig_inode->i_ino, donor_inode->i_ino); | |
1315 | return -EINVAL; | |
1316 | } | |
f066055a DM |
1317 | /* TODO: This is non obvious task to swap blocks for inodes with full |
1318 | jornaling enabled */ | |
1319 | if (ext4_should_journal_data(orig_inode) || | |
1320 | ext4_should_journal_data(donor_inode)) { | |
1321 | return -EINVAL; | |
1322 | } | |
fc04cb49 | 1323 | /* Protect orig and donor inodes against a truncate */ |
03bd8b9b | 1324 | mext_inode_double_lock(orig_inode, donor_inode); |
748de673 | 1325 | |
17335dcc DM |
1326 | /* Wait for all existing dio workers */ |
1327 | ext4_inode_block_unlocked_dio(orig_inode); | |
1328 | ext4_inode_block_unlocked_dio(donor_inode); | |
1329 | inode_dio_wait(orig_inode); | |
1330 | inode_dio_wait(donor_inode); | |
1331 | ||
fc04cb49 AF |
1332 | /* Protect extent tree against block allocations via delalloc */ |
1333 | double_down_write_data_sem(orig_inode, donor_inode); | |
748de673 | 1334 | /* Check the filesystem environment whether move_extent can be done */ |
03bd8b9b | 1335 | ret = mext_check_arguments(orig_inode, donor_inode, orig_start, |
446aaa6e | 1336 | donor_start, &len); |
03bd8b9b | 1337 | if (ret) |
347fa6f1 | 1338 | goto out; |
748de673 AF |
1339 | |
1340 | file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits; | |
1341 | block_end = block_start + len - 1; | |
1342 | if (file_end < block_end) | |
1343 | len -= block_end - file_end; | |
1344 | ||
03bd8b9b DM |
1345 | ret = get_ext_path(orig_inode, block_start, &orig_path); |
1346 | if (ret) | |
347fa6f1 | 1347 | goto out; |
748de673 AF |
1348 | |
1349 | /* Get path structure to check the hole */ | |
03bd8b9b DM |
1350 | ret = get_ext_path(orig_inode, block_start, &holecheck_path); |
1351 | if (ret) | |
748de673 AF |
1352 | goto out; |
1353 | ||
1354 | depth = ext_depth(orig_inode); | |
1355 | ext_cur = holecheck_path[depth].p_ext; | |
748de673 AF |
1356 | |
1357 | /* | |
c40ce3c9 AF |
1358 | * Get proper starting location of block replacement if block_start was |
1359 | * within the hole. | |
748de673 AF |
1360 | */ |
1361 | if (le32_to_cpu(ext_cur->ee_block) + | |
1362 | ext4_ext_get_actual_len(ext_cur) - 1 < block_start) { | |
c40ce3c9 AF |
1363 | /* |
1364 | * The hole exists between extents or the tail of | |
1365 | * original file. | |
1366 | */ | |
748de673 AF |
1367 | last_extent = mext_next_extent(orig_inode, |
1368 | holecheck_path, &ext_cur); | |
1369 | if (last_extent < 0) { | |
03bd8b9b | 1370 | ret = last_extent; |
748de673 AF |
1371 | goto out; |
1372 | } | |
1373 | last_extent = mext_next_extent(orig_inode, orig_path, | |
1374 | &ext_dummy); | |
1375 | if (last_extent < 0) { | |
03bd8b9b | 1376 | ret = last_extent; |
748de673 AF |
1377 | goto out; |
1378 | } | |
c40ce3c9 AF |
1379 | seq_start = le32_to_cpu(ext_cur->ee_block); |
1380 | } else if (le32_to_cpu(ext_cur->ee_block) > block_start) | |
1381 | /* The hole exists at the beginning of original file. */ | |
1382 | seq_start = le32_to_cpu(ext_cur->ee_block); | |
1383 | else | |
1384 | seq_start = block_start; | |
748de673 AF |
1385 | |
1386 | /* No blocks within the specified range. */ | |
1387 | if (le32_to_cpu(ext_cur->ee_block) > block_end) { | |
1388 | ext4_debug("ext4 move extent: The specified range of file " | |
1389 | "may be the hole\n"); | |
03bd8b9b | 1390 | ret = -EINVAL; |
748de673 AF |
1391 | goto out; |
1392 | } | |
1393 | ||
1394 | /* Adjust start blocks */ | |
1395 | add_blocks = min(le32_to_cpu(ext_cur->ee_block) + | |
1396 | ext4_ext_get_actual_len(ext_cur), block_end + 1) - | |
1397 | max(le32_to_cpu(ext_cur->ee_block), block_start); | |
1398 | ||
1399 | while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) { | |
1400 | seq_blocks += add_blocks; | |
1401 | ||
1402 | /* Adjust tail blocks */ | |
1403 | if (seq_start + seq_blocks - 1 > block_end) | |
1404 | seq_blocks = block_end - seq_start + 1; | |
1405 | ||
1406 | ext_prev = ext_cur; | |
1407 | last_extent = mext_next_extent(orig_inode, holecheck_path, | |
1408 | &ext_cur); | |
1409 | if (last_extent < 0) { | |
03bd8b9b | 1410 | ret = last_extent; |
748de673 AF |
1411 | break; |
1412 | } | |
1413 | add_blocks = ext4_ext_get_actual_len(ext_cur); | |
1414 | ||
1415 | /* | |
1416 | * Extend the length of contiguous block (seq_blocks) | |
1417 | * if extents are contiguous. | |
1418 | */ | |
1419 | if (ext4_can_extents_be_merged(orig_inode, | |
1420 | ext_prev, ext_cur) && | |
1421 | block_end >= le32_to_cpu(ext_cur->ee_block) && | |
1422 | !last_extent) | |
1423 | continue; | |
1424 | ||
1425 | /* Is original extent is uninitialized */ | |
1426 | uninit = ext4_ext_is_uninitialized(ext_prev); | |
1427 | ||
1428 | data_offset_in_page = seq_start % blocks_per_page; | |
1429 | ||
1430 | /* | |
1431 | * Calculate data blocks count that should be swapped | |
1432 | * at the first page. | |
1433 | */ | |
1434 | if (data_offset_in_page + seq_blocks > blocks_per_page) { | |
1435 | /* Swapped blocks are across pages */ | |
1436 | block_len_in_page = | |
1437 | blocks_per_page - data_offset_in_page; | |
1438 | } else { | |
1439 | /* Swapped blocks are in a page */ | |
1440 | block_len_in_page = seq_blocks; | |
1441 | } | |
1442 | ||
1443 | orig_page_offset = seq_start >> | |
1444 | (PAGE_CACHE_SHIFT - orig_inode->i_blkbits); | |
1445 | seq_end_page = (seq_start + seq_blocks - 1) >> | |
1446 | (PAGE_CACHE_SHIFT - orig_inode->i_blkbits); | |
1447 | seq_start = le32_to_cpu(ext_cur->ee_block); | |
1448 | rest_blocks = seq_blocks; | |
1449 | ||
fc04cb49 AF |
1450 | /* |
1451 | * Up semaphore to avoid following problems: | |
1452 | * a. transaction deadlock among ext4_journal_start, | |
1453 | * ->write_begin via pagefault, and jbd2_journal_commit | |
1454 | * b. racing with ->readpage, ->write_begin, and ext4_get_block | |
1455 | * in move_extent_per_page | |
1456 | */ | |
1457 | double_up_write_data_sem(orig_inode, donor_inode); | |
748de673 AF |
1458 | |
1459 | while (orig_page_offset <= seq_end_page) { | |
1460 | ||
1461 | /* Swap original branches with new branches */ | |
f868a48d AF |
1462 | block_len_in_page = move_extent_per_page( |
1463 | o_filp, donor_inode, | |
748de673 AF |
1464 | orig_page_offset, |
1465 | data_offset_in_page, | |
f868a48d | 1466 | block_len_in_page, uninit, |
03bd8b9b | 1467 | &ret); |
f868a48d | 1468 | |
748de673 AF |
1469 | /* Count how many blocks we have exchanged */ |
1470 | *moved_len += block_len_in_page; | |
03bd8b9b | 1471 | if (ret < 0) |
fc04cb49 | 1472 | break; |
2147b1a6 | 1473 | if (*moved_len > len) { |
24676da4 | 1474 | EXT4_ERROR_INODE(orig_inode, |
2147b1a6 AF |
1475 | "We replaced blocks too much! " |
1476 | "sum of replaced: %llu requested: %llu", | |
1477 | *moved_len, len); | |
03bd8b9b | 1478 | ret = -EIO; |
fc04cb49 | 1479 | break; |
2147b1a6 | 1480 | } |
748de673 | 1481 | |
f868a48d | 1482 | orig_page_offset++; |
748de673 AF |
1483 | data_offset_in_page = 0; |
1484 | rest_blocks -= block_len_in_page; | |
1485 | if (rest_blocks > blocks_per_page) | |
1486 | block_len_in_page = blocks_per_page; | |
1487 | else | |
1488 | block_len_in_page = rest_blocks; | |
1489 | } | |
1490 | ||
fc04cb49 | 1491 | double_down_write_data_sem(orig_inode, donor_inode); |
03bd8b9b | 1492 | if (ret < 0) |
fc04cb49 AF |
1493 | break; |
1494 | ||
748de673 AF |
1495 | /* Decrease buffer counter */ |
1496 | if (holecheck_path) | |
1497 | ext4_ext_drop_refs(holecheck_path); | |
03bd8b9b DM |
1498 | ret = get_ext_path(orig_inode, seq_start, &holecheck_path); |
1499 | if (ret) | |
748de673 AF |
1500 | break; |
1501 | depth = holecheck_path->p_depth; | |
1502 | ||
1503 | /* Decrease buffer counter */ | |
1504 | if (orig_path) | |
1505 | ext4_ext_drop_refs(orig_path); | |
03bd8b9b DM |
1506 | ret = get_ext_path(orig_inode, seq_start, &orig_path); |
1507 | if (ret) | |
748de673 AF |
1508 | break; |
1509 | ||
1510 | ext_cur = holecheck_path[depth].p_ext; | |
1511 | add_blocks = ext4_ext_get_actual_len(ext_cur); | |
1512 | seq_blocks = 0; | |
1513 | ||
1514 | } | |
1515 | out: | |
94d7c16c AF |
1516 | if (*moved_len) { |
1517 | ext4_discard_preallocations(orig_inode); | |
1518 | ext4_discard_preallocations(donor_inode); | |
1519 | } | |
1520 | ||
748de673 AF |
1521 | if (orig_path) { |
1522 | ext4_ext_drop_refs(orig_path); | |
1523 | kfree(orig_path); | |
1524 | } | |
1525 | if (holecheck_path) { | |
1526 | ext4_ext_drop_refs(holecheck_path); | |
1527 | kfree(holecheck_path); | |
1528 | } | |
fc04cb49 | 1529 | double_up_write_data_sem(orig_inode, donor_inode); |
17335dcc DM |
1530 | ext4_inode_resume_unlocked_dio(orig_inode); |
1531 | ext4_inode_resume_unlocked_dio(donor_inode); | |
03bd8b9b | 1532 | mext_inode_double_unlock(orig_inode, donor_inode); |
748de673 | 1533 | |
03bd8b9b | 1534 | return ret; |
748de673 | 1535 | } |