blob: 4e81d47aa8cb8af0553b93c1cb7dcbbea2d5d0a8 [file] [log] [blame]
Akira Fujita748de672009-06-17 19:24:03 -04001/*
2 * Copyright (c) 2008,2009 NEC Software Tohoku, Ltd.
3 * Written by Takashi Sato <t-sato@yk.jp.nec.com>
4 * Akira Fujita <a-fujita@rs.jp.nec.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/fs.h>
17#include <linux/quotaops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Akira Fujita748de672009-06-17 19:24:03 -040019#include "ext4_jbd2.h"
Akira Fujita748de672009-06-17 19:24:03 -040020#include "ext4.h"
Theodore Ts'o4a092d72012-11-28 13:03:30 -050021#include "ext4_extents.h"
Akira Fujita748de672009-06-17 19:24:03 -040022
Akira Fujitae8505972009-09-16 13:46:38 -040023/**
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 */
33static inline int
34get_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;
Akira Fujita347fa6f2009-09-16 14:25:07 -040043 } else if ((*path)[ext_depth(inode)].p_ext == NULL)
44 ret = -ENODATA;
45
Akira Fujitae8505972009-09-16 13:46:38 -040046 return ret;
47}
Akira Fujita748de672009-06-17 19:24:03 -040048
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 */
55static void
56copy_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 */
77static int
78mext_next_extent(struct inode *inode, struct ext4_ext_path *path,
79 struct ext4_extent **extent)
80{
Akira Fujitafc04cb42009-11-23 07:24:43 -050081 struct ext4_extent_header *eh;
Akira Fujita748de672009-06-17 19:24:03 -040082 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;
Theodore Ts'obf89d162010-10-27 21:30:14 -040088 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Akira Fujita748de672009-06-17 19:24:03 -040089 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++;
Theodore Ts'obf89d162010-10-27 21:30:14 -040099 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Akira Fujita748de672009-06-17 19:24:03 -0400100 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 =
Theodore Ts'obf89d162010-10-27 21:30:14 -0400114 ext4_idx_pblock(path[cur_ppos].p_idx);
Akira Fujita748de672009-06-17 19:24:03 -0400115 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
Akira Fujitafc04cb42009-11-23 07:24:43 -0500125 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
Akira Fujita748de672009-06-17 19:24:03 -0400132 /* leaf block */
133 path[leaf_ppos].p_ext = *extent =
134 EXT_FIRST_EXTENT(path[leaf_ppos].p_hdr);
Akira Fujitafc04cb42009-11-23 07:24:43 -0500135 path[leaf_ppos].p_block =
Theodore Ts'obf89d162010-10-27 21:30:14 -0400136 ext4_ext_pblock(path[leaf_ppos].p_ext);
Akira Fujita748de672009-06-17 19:24:03 -0400137 return 0;
138 }
139 }
140 /* We found the last extent */
141 return 1;
142}
143
144/**
Akira Fujitafc04cb42009-11-23 07:24:43 -0500145 * double_down_write_data_sem - Acquire two inodes' write lock of i_data_sem
Akira Fujita748de672009-06-17 19:24:03 -0400146 *
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400147 * Acquire write lock of i_data_sem of the two inodes
Akira Fujita748de672009-06-17 19:24:03 -0400148 */
149static void
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400150double_down_write_data_sem(struct inode *first, struct inode *second)
Akira Fujita748de672009-06-17 19:24:03 -0400151{
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400152 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);
Akira Fujita748de672009-06-17 19:24:03 -0400158
Akira Fujita748de672009-06-17 19:24:03 -0400159 }
Akira Fujita748de672009-06-17 19:24:03 -0400160}
161
162/**
Akira Fujitafc04cb42009-11-23 07:24:43 -0500163 * double_up_write_data_sem - Release two inodes' write lock of i_data_sem
Akira Fujita748de672009-06-17 19:24:03 -0400164 *
165 * @orig_inode: original inode structure to be released its lock first
166 * @donor_inode: donor inode structure to be released its lock second
Akira Fujitafc04cb42009-11-23 07:24:43 -0500167 * Release write lock of i_data_sem of two inodes (orig and donor).
Akira Fujita748de672009-06-17 19:24:03 -0400168 */
169static void
Akira Fujitafc04cb42009-11-23 07:24:43 -0500170double_up_write_data_sem(struct inode *orig_inode, struct inode *donor_inode)
Akira Fujita748de672009-06-17 19:24:03 -0400171{
Akira Fujita748de672009-06-17 19:24:03 -0400172 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 */
190static int
191mext_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;
Theodore Ts'obf89d162010-10-27 21:30:14 -0400218 ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
Akira Fujita748de672009-06-17 19:24:03 -0400219 }
220
221 o_start->ee_len = start_ext->ee_len;
Akira Fujita5fd52492010-03-04 00:31:06 -0500222 eblock = le32_to_cpu(start_ext->ee_block);
Akira Fujita748de672009-06-17 19:24:03 -0400223 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;
Akira Fujita5fd52492010-03-04 00:31:06 -0500233 eblock = le32_to_cpu(start_ext->ee_block);
Akira Fujita748de672009-06-17 19:24:03 -0400234 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;
Theodore Ts'obf89d162010-10-27 21:30:14 -0400245 ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
Akira Fujita748de672009-06-17 19:24:03 -0400246
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) {
Akira Fujitae8505972009-09-16 13:46:38 -0400261 err = get_ext_path(orig_inode, eblock, &orig_path);
Akira Fujita347fa6f2009-09-16 14:25:07 -0400262 if (err)
Akira Fujita748de672009-06-17 19:24:03 -0400263 goto out;
264
265 if (ext4_ext_insert_extent(handle, orig_inode,
Mingming Cao00314622009-09-28 15:49:08 -0400266 orig_path, new_ext, 0))
Akira Fujita748de672009-06-17 19:24:03 -0400267 goto out;
268 }
269
270 if (end_flag) {
Akira Fujitae8505972009-09-16 13:46:38 -0400271 err = get_ext_path(orig_inode,
272 le32_to_cpu(end_ext->ee_block) - 1, &orig_path);
Akira Fujita347fa6f2009-09-16 14:25:07 -0400273 if (err)
Akira Fujita748de672009-06-17 19:24:03 -0400274 goto out;
275
276 if (ext4_ext_insert_extent(handle, orig_inode,
Mingming Cao00314622009-09-28 15:49:08 -0400277 orig_path, end_ext, 0))
Akira Fujita748de672009-06-17 19:24:03 -0400278 goto out;
279 }
280out:
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 */
304static void
305mext_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;
Theodore Ts'obf89d162010-10-27 21:30:14 -0400330 ext4_ext_store_pblock(&o_start[i++], ext4_ext_pblock(new_ext));
Akira Fujita748de672009-06-17 19:24:03 -0400331 }
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 */
358static int
359mext_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 */
439static int
440mext_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;
Akira Fujita748de672009-06-17 19:24:03 -0400447 int oext_alen, new_ext_alen, end_ext_alen;
448 int depth = ext_depth(orig_inode);
449 int ret;
450
Steven Liuc26d0ba2010-05-11 00:00:00 -0400451 start_ext.ee_block = end_ext.ee_block = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400452 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);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400457 ext4_ext_store_pblock(&new_ext, ext4_ext_pblock(dext));
Akira Fujita748de672009-06-17 19:24:03 -0400458 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;
Akira Fujita748de672009-06-17 19:24:03 -0400461
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));
Akira Fujita5fd52492010-03-04 00:31:06 -0500473 start_ext.ee_block = oext->ee_block;
Akira Fujita748de672009-06-17 19:24:03 -0400474 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);
Akira Fujita5fd52492010-03-04 00:31:06 -0500487 start_ext.ee_block = oext->ee_block;
Akira Fujita748de672009-06-17 19:24:03 -0400488 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 */
Akira Fujita2147b1a2009-09-16 13:46:35 -0400498 if (le32_to_cpu(oext->ee_block) + oext_alen - 1 < new_ext_end) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400499 EXT4_ERROR_INODE(orig_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -0400500 "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 }
Akira Fujita748de672009-06-17 19:24:03 -0400507
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,
Theodore Ts'obf89d162010-10-27 21:30:14 -0400522 (ext4_ext_pblock(o_end) + oext_alen - end_ext_alen));
Akira Fujita748de672009-06-17 19:24:03 -0400523 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);
Akira Fujita2147b1a2009-09-16 13:46:35 -0400530out:
Akira Fujita748de672009-06-17 19:24:03 -0400531 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
Akira Fujita92c28152009-11-23 07:24:50 -0500541 * @max_count: the maximum length of extents
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400542 *
543 * Return 0 on success, or a negative error value on failure.
Akira Fujita748de672009-06-17 19:24:03 -0400544 */
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400545static int
Akira Fujita748de672009-06-17 19:24:03 -0400546mext_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
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400554 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
Akira Fujita748de672009-06-17 19:24:03 -0400567 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
Theodore Ts'obf89d162010-10-27 21:30:14 -0400573 ext4_ext_store_pblock(tmp_dext, ext4_ext_pblock(tmp_dext) + diff);
Wei Yongjunba39ebb2012-09-27 09:37:53 -0400574 le32_add_cpu(&tmp_dext->ee_block, diff);
575 le16_add_cpu(&tmp_dext->ee_len, -diff);
Akira Fujita748de672009-06-17 19:24:03 -0400576
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);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400581 ext4_ext_store_pblock(tmp_oext, ext4_ext_pblock(tmp_oext) + orig_diff);
Akira Fujita748de672009-06-17 19:24:03 -0400582
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);
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400593
594 return 0;
Akira Fujita748de672009-06-17 19:24:03 -0400595}
596
597/**
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400598 * 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 */
608static int
609mext_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
634/**
Akira Fujita748de672009-06-17 19:24:03 -0400635 * 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
Akira Fujitaf868a482009-11-23 07:25:48 -0500642 * @err: pointer to save return value
Akira Fujita748de672009-06-17 19:24:03 -0400643 *
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 *
Akira Fujitaf868a482009-11-23 07:25:48 -0500653 * Return replaced block count.
Akira Fujita748de672009-06-17 19:24:03 -0400654 */
655static int
656mext_replace_branches(handle_t *handle, struct inode *orig_inode,
657 struct inode *donor_inode, ext4_lblk_t from,
Akira Fujitaf868a482009-11-23 07:25:48 -0500658 ext4_lblk_t count, int *err)
Akira Fujita748de672009-06-17 19:24:03 -0400659{
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;
Akira Fujita748de672009-06-17 19:24:03 -0400665 int depth;
666 int replaced_count = 0;
667 int dext_alen;
668
Akira Fujita748de672009-06-17 19:24:03 -0400669 /* Get the original extent for the block "orig_off" */
Akira Fujitaf868a482009-11-23 07:25:48 -0500670 *err = get_ext_path(orig_inode, orig_off, &orig_path);
671 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400672 goto out;
673
674 /* Get the donor extent for the head */
Akira Fujitaf868a482009-11-23 07:25:48 -0500675 *err = get_ext_path(donor_inode, donor_off, &donor_path);
676 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400677 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;
Akria Fujita87e69872013-02-01 20:52:46 -0500684 if (unlikely(!dext))
685 goto missing_donor_extent;
Akira Fujita748de672009-06-17 19:24:03 -0400686 tmp_dext = *dext;
687
Akira Fujitaf868a482009-11-23 07:25:48 -0500688 *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
Akira Fujita748de672009-06-17 19:24:03 -0400689 donor_off, count);
Akira Fujitaf868a482009-11-23 07:25:48 -0500690 if (*err)
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400691 goto out;
Akira Fujita748de672009-06-17 19:24:03 -0400692
693 /* Loop for the donor extents */
694 while (1) {
695 /* The extent for donor must be found. */
Akria Fujita87e69872013-02-01 20:52:46 -0500696 if (unlikely(!dext)) {
697 missing_donor_extent:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400698 EXT4_ERROR_INODE(donor_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -0400699 "The extent for donor must be found");
Akira Fujitaf868a482009-11-23 07:25:48 -0500700 *err = -EIO;
Akira Fujita2147b1a2009-09-16 13:46:35 -0400701 goto out;
702 } else if (donor_off != le32_to_cpu(tmp_dext.ee_block)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400703 EXT4_ERROR_INODE(donor_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -0400704 "Donor offset(%u) and the first block of donor "
705 "extent(%u) should be equal",
706 donor_off,
707 le32_to_cpu(tmp_dext.ee_block));
Akira Fujitaf868a482009-11-23 07:25:48 -0500708 *err = -EIO;
Akira Fujita2147b1a2009-09-16 13:46:35 -0400709 goto out;
710 }
Akira Fujita748de672009-06-17 19:24:03 -0400711
712 /* Set donor extent to orig extent */
Akira Fujitaf868a482009-11-23 07:25:48 -0500713 *err = mext_leaf_block(handle, orig_inode,
Akira Fujita748de672009-06-17 19:24:03 -0400714 orig_path, &tmp_dext, &orig_off);
Akira Fujitaf868a482009-11-23 07:25:48 -0500715 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400716 goto out;
717
718 /* Set orig extent to donor extent */
Akira Fujitaf868a482009-11-23 07:25:48 -0500719 *err = mext_leaf_block(handle, donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -0400720 donor_path, &tmp_oext, &donor_off);
Akira Fujitaf868a482009-11-23 07:25:48 -0500721 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400722 goto out;
723
724 dext_alen = ext4_ext_get_actual_len(&tmp_dext);
725 replaced_count += dext_alen;
726 donor_off += dext_alen;
727 orig_off += dext_alen;
728
729 /* Already moved the expected blocks */
730 if (replaced_count >= count)
731 break;
732
733 if (orig_path)
734 ext4_ext_drop_refs(orig_path);
Akira Fujitaf868a482009-11-23 07:25:48 -0500735 *err = get_ext_path(orig_inode, orig_off, &orig_path);
736 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400737 goto out;
738 depth = ext_depth(orig_inode);
739 oext = orig_path[depth].p_ext;
Akira Fujita748de672009-06-17 19:24:03 -0400740 tmp_oext = *oext;
741
742 if (donor_path)
743 ext4_ext_drop_refs(donor_path);
Akira Fujitaf868a482009-11-23 07:25:48 -0500744 *err = get_ext_path(donor_inode, donor_off, &donor_path);
745 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400746 goto out;
747 depth = ext_depth(donor_inode);
748 dext = donor_path[depth].p_ext;
Akira Fujita748de672009-06-17 19:24:03 -0400749 tmp_dext = *dext;
750
Akira Fujitaf868a482009-11-23 07:25:48 -0500751 *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400752 donor_off, count - replaced_count);
Akira Fujitaf868a482009-11-23 07:25:48 -0500753 if (*err)
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400754 goto out;
Akira Fujita748de672009-06-17 19:24:03 -0400755 }
756
757out:
758 if (orig_path) {
759 ext4_ext_drop_refs(orig_path);
760 kfree(orig_path);
761 }
762 if (donor_path) {
763 ext4_ext_drop_refs(donor_path);
764 kfree(donor_path);
765 }
766
Akira Fujitaf868a482009-11-23 07:25:48 -0500767 return replaced_count;
Akira Fujita748de672009-06-17 19:24:03 -0400768}
769
770/**
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400771 * 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 */
780static int
781mext_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 */
817static int
818mext_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 }
876out:
877 if (!partial)
878 SetPageUptodate(page);
879 return 0;
880}
881
882/**
Akira Fujita748de672009-06-17 19:24:03 -0400883 * 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
Akira Fujitaf868a482009-11-23 07:25:48 -0500891 * @err: pointer to save return value
Akira Fujita748de672009-06-17 19:24:03 -0400892 *
893 * Save the data in original inode blocks and replace original inode extents
894 * with donor inode extents by calling mext_replace_branches().
Akira Fujitaf868a482009-11-23 07:25:48 -0500895 * Finally, write out the saved data in new original inode blocks. Return
896 * replaced block count.
Akira Fujita748de672009-06-17 19:24:03 -0400897 */
898static int
Akira Fujita44fc48f2009-09-05 23:12:41 -0400899move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -0400900 pgoff_t orig_page_offset, int data_offset_in_page,
Akira Fujitaf868a482009-11-23 07:25:48 -0500901 int block_len_in_page, int uninit, int *err)
Akira Fujita748de672009-06-17 19:24:03 -0400902{
Al Viro496ad9a2013-01-23 17:07:38 -0500903 struct inode *orig_inode = file_inode(o_filp);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400904 struct page *pagep[2] = {NULL, NULL};
Akira Fujita748de672009-06-17 19:24:03 -0400905 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;
Akira Fujitaf868a482009-11-23 07:25:48 -0500910 unsigned int tmp_data_size, data_size, replaced_size;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400911 int err2, jblocks, retries = 0;
Akira Fujitaf868a482009-11-23 07:25:48 -0500912 int replaced_count = 0;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400913 int from = data_offset_in_page << orig_inode->i_blkbits;
Akira Fujita748de672009-06-17 19:24:03 -0400914 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 */
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400920again:
921 *err = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400922 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
Theodore Ts'o9924a922013-02-08 21:59:22 -0500923 handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
Akira Fujita748de672009-06-17 19:24:03 -0400924 if (IS_ERR(handle)) {
Akira Fujitaf868a482009-11-23 07:25:48 -0500925 *err = PTR_ERR(handle);
926 return 0;
Akira Fujita748de672009-06-17 19:24:03 -0400927 }
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
Akira Fujita748de672009-06-17 19:24:03 -0400935 offs = (long long)orig_blk_offset << orig_inode->i_blkbits;
936
Akira Fujitaf868a482009-11-23 07:25:48 -0500937 /* Calculate data_size */
Akira Fujita748de672009-06-17 19:24:03 -0400938 if ((orig_blk_offset + block_len_in_page - 1) ==
939 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
940 /* Replace the last block */
Akira Fujitaf868a482009-11-23 07:25:48 -0500941 tmp_data_size = orig_inode->i_size & (blocksize - 1);
Akira Fujita748de672009-06-17 19:24:03 -0400942 /*
Akira Fujitaf868a482009-11-23 07:25:48 -0500943 * If data_size equal zero, it shows data_size is multiples of
Akira Fujita748de672009-06-17 19:24:03 -0400944 * blocksize. So we set appropriate value.
945 */
Akira Fujitaf868a482009-11-23 07:25:48 -0500946 if (tmp_data_size == 0)
947 tmp_data_size = blocksize;
Akira Fujita748de672009-06-17 19:24:03 -0400948
Akira Fujitaf868a482009-11-23 07:25:48 -0500949 data_size = tmp_data_size +
Akira Fujita748de672009-06-17 19:24:03 -0400950 ((block_len_in_page - 1) << orig_inode->i_blkbits);
Akira Fujitaf868a482009-11-23 07:25:48 -0500951 } else
952 data_size = block_len_in_page << orig_inode->i_blkbits;
Akira Fujita748de672009-06-17 19:24:03 -0400953
Akira Fujitaf868a482009-11-23 07:25:48 -0500954 replaced_size = data_size;
955
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400956 *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
957 pagep);
Akira Fujitaf868a482009-11-23 07:25:48 -0500958 if (unlikely(*err < 0))
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400959 goto stop_journal;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400960 /*
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;
Akira Fujita748de672009-06-17 19:24:03 -0400975
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400976 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 }
999data_copy:
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001000 *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;
Akira Fujita748de672009-06-17 19:24:03 -04001010 }
1011
Akira Fujitaf868a482009-11-23 07:25:48 -05001012 replaced_count = mext_replace_branches(handle, orig_inode, donor_inode,
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001013 orig_blk_offset,
1014 block_len_in_page, err);
1015 if (*err) {
Akira Fujitaf868a482009-11-23 07:25:48 -05001016 if (replaced_count) {
1017 block_len_in_page = replaced_count;
1018 replaced_size =
1019 block_len_in_page << orig_inode->i_blkbits;
Akira Fujitaac48b0a2009-11-24 10:31:56 -05001020 } else
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001021 goto unlock_pages;
Akira Fujitaf868a482009-11-23 07:25:48 -05001022 }
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001023 /* 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);
Akira Fujita748de672009-06-17 19:24:03 -04001029
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001030 if (unlikely(*err < 0))
1031 goto repair_branches;
Akira Fujita748de672009-06-17 19:24:03 -04001032
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001033 /* 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);
Akira Fujita748de672009-06-17 19:24:03 -04001036
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001037unlock_pages:
1038 unlock_page(pagep[0]);
1039 page_cache_release(pagep[0]);
1040 unlock_page(pagep[1]);
1041 page_cache_release(pagep[1]);
1042stop_journal:
Akira Fujita748de672009-06-17 19:24:03 -04001043 ext4_journal_stop(handle);
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001044 /* 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;
Akira Fujitaf868a482009-11-23 07:25:48 -05001049 return replaced_count;
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001050
1051repair_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;
Akira Fujita748de672009-06-17 19:24:03 -04001070}
1071
1072/**
Akira Fujitac437b272010-03-04 00:39:24 -05001073 * mext_check_arguments - Check whether move extent can be done
Akira Fujita748de672009-06-17 19:24:03 -04001074 *
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
Akira Fujita748de672009-06-17 19:24:03 -04001080 *
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 */
1085static int
1086mext_check_arguments(struct inode *orig_inode,
Kazuya Mio446aaa62009-11-24 10:28:48 -05001087 struct inode *donor_inode, __u64 orig_start,
1088 __u64 donor_start, __u64 *len)
Akira Fujita748de672009-06-17 19:24:03 -04001089{
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001090 ext4_lblk_t orig_blocks, donor_blocks;
1091 unsigned int blkbits = orig_inode->i_blkbits;
1092 unsigned int blocksize = 1 << blkbits;
1093
Akira Fujita4a585792009-12-06 23:38:31 -05001094 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
Theodore Ts'o1f5a81e2010-06-02 22:04:39 -04001101 if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
1102 return -EPERM;
1103
Akira Fujita748de672009-06-17 19:24:03 -04001104 /* 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
Akira Fujita748de672009-06-17 19:24:03 -04001112 /* Ext4 move extent supports only extent based file */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001113 if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
Akira Fujita748de672009-06-17 19:24:03 -04001114 ext4_debug("ext4 move extent: orig file is not extents "
1115 "based file [ino:orig %lu]\n", orig_inode->i_ino);
1116 return -EOPNOTSUPP;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001117 } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
Akira Fujita748de672009-06-17 19:24:03 -04001118 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
Lukas Czernerf17722f2011-06-06 00:05:17 -04001136 if ((orig_start >= EXT_MAX_BLOCKS) ||
Lukas Czernerf17722f2011-06-06 00:05:17 -04001137 (*len > EXT_MAX_BLOCKS) ||
1138 (orig_start + *len >= EXT_MAX_BLOCKS)) {
Eric Sandeen0a80e982009-09-17 11:55:58 -04001139 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
Lukas Czernerf17722f2011-06-06 00:05:17 -04001140 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
Akira Fujita748de672009-06-17 19:24:03 -04001141 orig_inode->i_ino, donor_inode->i_ino);
1142 return -EINVAL;
1143 }
1144
1145 if (orig_inode->i_size > donor_inode->i_size) {
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001146 donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits;
1147 /* TODO: eliminate this artificial restriction */
1148 if (orig_start >= donor_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001149 ext4_debug("ext4 move extent: orig start offset "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001150 "[%llu] should be less than donor file blocks "
1151 "[%u] [ino:orig %lu, donor %lu]\n",
1152 orig_start, donor_blocks,
Akira Fujita748de672009-06-17 19:24:03 -04001153 orig_inode->i_ino, donor_inode->i_ino);
1154 return -EINVAL;
1155 }
1156
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001157 /* TODO: eliminate this artificial restriction */
1158 if (orig_start + *len > donor_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001159 ext4_debug("ext4 move extent: End offset [%llu] should "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001160 "be less than donor file blocks [%u]."
1161 "So adjust length from %llu to %llu "
Akira Fujita748de672009-06-17 19:24:03 -04001162 "[ino:orig %lu, donor %lu]\n",
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001163 orig_start + *len, donor_blocks,
1164 *len, donor_blocks - orig_start,
Akira Fujita748de672009-06-17 19:24:03 -04001165 orig_inode->i_ino, donor_inode->i_ino);
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001166 *len = donor_blocks - orig_start;
Akira Fujita748de672009-06-17 19:24:03 -04001167 }
1168 } else {
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001169 orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits;
1170 if (orig_start >= orig_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001171 ext4_debug("ext4 move extent: start offset [%llu] "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001172 "should be less than original file blocks "
1173 "[%u] [ino:orig %lu, donor %lu]\n",
1174 orig_start, orig_blocks,
Akira Fujita748de672009-06-17 19:24:03 -04001175 orig_inode->i_ino, donor_inode->i_ino);
1176 return -EINVAL;
1177 }
1178
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001179 if (orig_start + *len > orig_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001180 ext4_debug("ext4 move extent: Adjust length "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001181 "from %llu to %llu. Because it should be "
1182 "less than original file blocks "
Akira Fujita748de672009-06-17 19:24:03 -04001183 "[ino:orig %lu, donor %lu]\n",
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001184 *len, orig_blocks - orig_start,
Akira Fujita748de672009-06-17 19:24:03 -04001185 orig_inode->i_ino, donor_inode->i_ino);
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001186 *len = orig_blocks - orig_start;
Akira Fujita748de672009-06-17 19:24:03 -04001187 }
1188 }
1189
1190 if (!*len) {
Akira Fujita92c28152009-11-23 07:24:50 -05001191 ext4_debug("ext4 move extent: len should not be 0 "
Akira Fujita748de672009-06-17 19:24:03 -04001192 "[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 *
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001206 * Lock two inodes' i_mutex
Akira Fujita748de672009-06-17 19:24:03 -04001207 */
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001208static void
Akira Fujita748de672009-06-17 19:24:03 -04001209mext_inode_double_lock(struct inode *inode1, struct inode *inode2)
1210{
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001211 BUG_ON(inode1 == inode2);
1212 if (inode1 < inode2) {
Akira Fujita748de672009-06-17 19:24:03 -04001213 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 *
Akira Fujita748de672009-06-17 19:24:03 -04001227 */
1228
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001229static void
Akira Fujita748de672009-06-17 19:24:03 -04001230mext_inode_double_unlock(struct inode *inode1, struct inode *inode2)
1231{
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001232 mutex_unlock(&inode1->i_mutex);
1233 mutex_unlock(&inode2->i_mutex);
Akira Fujita748de672009-06-17 19:24:03 -04001234}
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 */
1277int
1278ext4_move_extents(struct file *o_filp, struct file *d_filp,
1279 __u64 orig_start, __u64 donor_start, __u64 len,
1280 __u64 *moved_len)
1281{
Al Viro496ad9a2013-01-23 17:07:38 -05001282 struct inode *orig_inode = file_inode(o_filp);
1283 struct inode *donor_inode = file_inode(d_filp);
Akira Fujita748de672009-06-17 19:24:03 -04001284 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;
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001290 int ret, depth, last_extent = 0;
Akira Fujita748de672009-06-17 19:24:03 -04001291 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
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001296 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) {
Theodore Ts'of3ce8062009-09-28 15:58:29 -04001305 ext4_debug("ext4 move extent: The argument files should not "
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001306 "be same inode [ino:orig %lu, donor %lu]\n",
Theodore Ts'of3ce8062009-09-28 15:58:29 -04001307 orig_inode->i_ino, donor_inode->i_ino);
1308 return -EINVAL;
1309 }
1310
Akira Fujita7247c0c2010-03-04 00:34:58 -05001311 /* 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 }
Dmitry Monakhovf0660552012-09-26 12:32:54 -04001318 /* 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 }
Akira Fujitafc04cb42009-11-23 07:24:43 -05001324 /* Protect orig and donor inodes against a truncate */
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001325 mext_inode_double_lock(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001326
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04001327 /* 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
Akira Fujitafc04cb42009-11-23 07:24:43 -05001333 /* Protect extent tree against block allocations via delalloc */
1334 double_down_write_data_sem(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001335 /* Check the filesystem environment whether move_extent can be done */
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001336 ret = mext_check_arguments(orig_inode, donor_inode, orig_start,
Kazuya Mio446aaa62009-11-24 10:28:48 -05001337 donor_start, &len);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001338 if (ret)
Akira Fujita347fa6f2009-09-16 14:25:07 -04001339 goto out;
Akira Fujita748de672009-06-17 19:24:03 -04001340
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
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001346 ret = get_ext_path(orig_inode, block_start, &orig_path);
1347 if (ret)
Akira Fujita347fa6f2009-09-16 14:25:07 -04001348 goto out;
Akira Fujita748de672009-06-17 19:24:03 -04001349
1350 /* Get path structure to check the hole */
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001351 ret = get_ext_path(orig_inode, block_start, &holecheck_path);
1352 if (ret)
Akira Fujita748de672009-06-17 19:24:03 -04001353 goto out;
1354
1355 depth = ext_depth(orig_inode);
1356 ext_cur = holecheck_path[depth].p_ext;
Akira Fujita748de672009-06-17 19:24:03 -04001357
1358 /*
Akira Fujitac40ce3c2009-09-16 14:25:39 -04001359 * Get proper starting location of block replacement if block_start was
1360 * within the hole.
Akira Fujita748de672009-06-17 19:24:03 -04001361 */
1362 if (le32_to_cpu(ext_cur->ee_block) +
1363 ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
Akira Fujitac40ce3c2009-09-16 14:25:39 -04001364 /*
1365 * The hole exists between extents or the tail of
1366 * original file.
1367 */
Akira Fujita748de672009-06-17 19:24:03 -04001368 last_extent = mext_next_extent(orig_inode,
1369 holecheck_path, &ext_cur);
1370 if (last_extent < 0) {
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001371 ret = last_extent;
Akira Fujita748de672009-06-17 19:24:03 -04001372 goto out;
1373 }
1374 last_extent = mext_next_extent(orig_inode, orig_path,
1375 &ext_dummy);
1376 if (last_extent < 0) {
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001377 ret = last_extent;
Akira Fujita748de672009-06-17 19:24:03 -04001378 goto out;
1379 }
Akira Fujitac40ce3c2009-09-16 14:25:39 -04001380 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;
Akira Fujita748de672009-06-17 19:24:03 -04001386
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");
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001391 ret = -EINVAL;
Akira Fujita748de672009-06-17 19:24:03 -04001392 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) {
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001411 ret = last_extent;
Akira Fujita748de672009-06-17 19:24:03 -04001412 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
Akira Fujitafc04cb42009-11-23 07:24:43 -05001451 /*
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);
Akira Fujita748de672009-06-17 19:24:03 -04001459
1460 while (orig_page_offset <= seq_end_page) {
1461
1462 /* Swap original branches with new branches */
Akira Fujitaf868a482009-11-23 07:25:48 -05001463 block_len_in_page = move_extent_per_page(
1464 o_filp, donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -04001465 orig_page_offset,
1466 data_offset_in_page,
Akira Fujitaf868a482009-11-23 07:25:48 -05001467 block_len_in_page, uninit,
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001468 &ret);
Akira Fujitaf868a482009-11-23 07:25:48 -05001469
Akira Fujita748de672009-06-17 19:24:03 -04001470 /* Count how many blocks we have exchanged */
1471 *moved_len += block_len_in_page;
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001472 if (ret < 0)
Akira Fujitafc04cb42009-11-23 07:24:43 -05001473 break;
Akira Fujita2147b1a2009-09-16 13:46:35 -04001474 if (*moved_len > len) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001475 EXT4_ERROR_INODE(orig_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -04001476 "We replaced blocks too much! "
1477 "sum of replaced: %llu requested: %llu",
1478 *moved_len, len);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001479 ret = -EIO;
Akira Fujitafc04cb42009-11-23 07:24:43 -05001480 break;
Akira Fujita2147b1a2009-09-16 13:46:35 -04001481 }
Akira Fujita748de672009-06-17 19:24:03 -04001482
Akira Fujitaf868a482009-11-23 07:25:48 -05001483 orig_page_offset++;
Akira Fujita748de672009-06-17 19:24:03 -04001484 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
Akira Fujitafc04cb42009-11-23 07:24:43 -05001492 double_down_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001493 if (ret < 0)
Akira Fujitafc04cb42009-11-23 07:24:43 -05001494 break;
1495
Akira Fujita748de672009-06-17 19:24:03 -04001496 /* Decrease buffer counter */
1497 if (holecheck_path)
1498 ext4_ext_drop_refs(holecheck_path);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001499 ret = get_ext_path(orig_inode, seq_start, &holecheck_path);
1500 if (ret)
Akira Fujita748de672009-06-17 19:24:03 -04001501 break;
1502 depth = holecheck_path->p_depth;
1503
1504 /* Decrease buffer counter */
1505 if (orig_path)
1506 ext4_ext_drop_refs(orig_path);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001507 ret = get_ext_path(orig_inode, seq_start, &orig_path);
1508 if (ret)
Akira Fujita748de672009-06-17 19:24:03 -04001509 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 }
1516out:
Akira Fujita94d7c162009-11-24 10:19:57 -05001517 if (*moved_len) {
1518 ext4_discard_preallocations(orig_inode);
1519 ext4_discard_preallocations(donor_inode);
1520 }
1521
Akira Fujita748de672009-06-17 19:24:03 -04001522 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 }
Akira Fujitafc04cb42009-11-23 07:24:43 -05001530 double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04001531 ext4_inode_resume_unlocked_dio(orig_inode);
1532 ext4_inode_resume_unlocked_dio(donor_inode);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001533 mext_inode_double_unlock(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001534
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001535 return ret;
Akira Fujita748de672009-06-17 19:24:03 -04001536}