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