blob: e4bd8761498a18e4d816607664b2cde3f252df21 [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>
18#include "ext4_jbd2.h"
19#include "ext4_extents.h"
20#include "ext4.h"
21
Akira Fujitae8505972009-09-16 13:46:38 -040022/**
23 * get_ext_path - Find an extent path for designated logical block number.
24 *
25 * @inode: an inode which is searched
26 * @lblock: logical block number to find an extent path
27 * @path: pointer to an extent path pointer (for output)
28 *
29 * ext4_ext_find_extent wrapper. Return 0 on success, or a negative error value
30 * on failure.
31 */
32static inline int
33get_ext_path(struct inode *inode, ext4_lblk_t lblock,
34 struct ext4_ext_path **path)
35{
36 int ret = 0;
37
38 *path = ext4_ext_find_extent(inode, lblock, *path);
39 if (IS_ERR(*path)) {
40 ret = PTR_ERR(*path);
41 *path = NULL;
42 }
43 return ret;
44}
Akira Fujita748de672009-06-17 19:24:03 -040045
46/**
47 * copy_extent_status - Copy the extent's initialization status
48 *
49 * @src: an extent for getting initialize status
50 * @dest: an extent to be set the status
51 */
52static void
53copy_extent_status(struct ext4_extent *src, struct ext4_extent *dest)
54{
55 if (ext4_ext_is_uninitialized(src))
56 ext4_ext_mark_uninitialized(dest);
57 else
58 dest->ee_len = cpu_to_le16(ext4_ext_get_actual_len(dest));
59}
60
61/**
62 * mext_next_extent - Search for the next extent and set it to "extent"
63 *
64 * @inode: inode which is searched
65 * @path: this will obtain data for the next extent
66 * @extent: pointer to the next extent we have just gotten
67 *
68 * Search the next extent in the array of ext4_ext_path structure (@path)
69 * and set it to ext4_extent structure (@extent). In addition, the member of
70 * @path (->p_ext) also points the next extent. Return 0 on success, 1 if
71 * ext4_ext_path structure refers to the last extent, or a negative error
72 * value on failure.
73 */
74static int
75mext_next_extent(struct inode *inode, struct ext4_ext_path *path,
76 struct ext4_extent **extent)
77{
78 int ppos, leaf_ppos = path->p_depth;
79
80 ppos = leaf_ppos;
81 if (EXT_LAST_EXTENT(path[ppos].p_hdr) > path[ppos].p_ext) {
82 /* leaf block */
83 *extent = ++path[ppos].p_ext;
84 return 0;
85 }
86
87 while (--ppos >= 0) {
88 if (EXT_LAST_INDEX(path[ppos].p_hdr) >
89 path[ppos].p_idx) {
90 int cur_ppos = ppos;
91
92 /* index block */
93 path[ppos].p_idx++;
94 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
95 if (path[ppos+1].p_bh)
96 brelse(path[ppos+1].p_bh);
97 path[ppos+1].p_bh =
98 sb_bread(inode->i_sb, path[ppos].p_block);
99 if (!path[ppos+1].p_bh)
100 return -EIO;
101 path[ppos+1].p_hdr =
102 ext_block_hdr(path[ppos+1].p_bh);
103
104 /* Halfway index block */
105 while (++cur_ppos < leaf_ppos) {
106 path[cur_ppos].p_idx =
107 EXT_FIRST_INDEX(path[cur_ppos].p_hdr);
108 path[cur_ppos].p_block =
109 idx_pblock(path[cur_ppos].p_idx);
110 if (path[cur_ppos+1].p_bh)
111 brelse(path[cur_ppos+1].p_bh);
112 path[cur_ppos+1].p_bh = sb_bread(inode->i_sb,
113 path[cur_ppos].p_block);
114 if (!path[cur_ppos+1].p_bh)
115 return -EIO;
116 path[cur_ppos+1].p_hdr =
117 ext_block_hdr(path[cur_ppos+1].p_bh);
118 }
119
120 /* leaf block */
121 path[leaf_ppos].p_ext = *extent =
122 EXT_FIRST_EXTENT(path[leaf_ppos].p_hdr);
123 return 0;
124 }
125 }
126 /* We found the last extent */
127 return 1;
128}
129
130/**
131 * mext_double_down_read - Acquire two inodes' read semaphore
132 *
133 * @orig_inode: original inode structure
134 * @donor_inode: donor inode structure
135 * Acquire read semaphore of the two inodes (orig and donor) by i_ino order.
136 */
137static void
138mext_double_down_read(struct inode *orig_inode, struct inode *donor_inode)
139{
140 struct inode *first = orig_inode, *second = donor_inode;
141
142 BUG_ON(orig_inode == NULL || donor_inode == NULL);
143
144 /*
145 * Use the inode number to provide the stable locking order instead
146 * of its address, because the C language doesn't guarantee you can
147 * compare pointers that don't come from the same array.
148 */
149 if (donor_inode->i_ino < orig_inode->i_ino) {
150 first = donor_inode;
151 second = orig_inode;
152 }
153
154 down_read(&EXT4_I(first)->i_data_sem);
155 down_read(&EXT4_I(second)->i_data_sem);
156}
157
158/**
159 * mext_double_down_write - Acquire two inodes' write semaphore
160 *
161 * @orig_inode: original inode structure
162 * @donor_inode: donor inode structure
163 * Acquire write semaphore of the two inodes (orig and donor) by i_ino order.
164 */
165static void
166mext_double_down_write(struct inode *orig_inode, struct inode *donor_inode)
167{
168 struct inode *first = orig_inode, *second = donor_inode;
169
170 BUG_ON(orig_inode == NULL || donor_inode == NULL);
171
172 /*
173 * Use the inode number to provide the stable locking order instead
174 * of its address, because the C language doesn't guarantee you can
175 * compare pointers that don't come from the same array.
176 */
177 if (donor_inode->i_ino < orig_inode->i_ino) {
178 first = donor_inode;
179 second = orig_inode;
180 }
181
182 down_write(&EXT4_I(first)->i_data_sem);
183 down_write(&EXT4_I(second)->i_data_sem);
184}
185
186/**
187 * mext_double_up_read - Release two inodes' read semaphore
188 *
189 * @orig_inode: original inode structure to be released its lock first
190 * @donor_inode: donor inode structure to be released its lock second
191 * Release read semaphore of two inodes (orig and donor).
192 */
193static void
194mext_double_up_read(struct inode *orig_inode, struct inode *donor_inode)
195{
196 BUG_ON(orig_inode == NULL || donor_inode == NULL);
197
198 up_read(&EXT4_I(orig_inode)->i_data_sem);
199 up_read(&EXT4_I(donor_inode)->i_data_sem);
200}
201
202/**
203 * mext_double_up_write - Release two inodes' write semaphore
204 *
205 * @orig_inode: original inode structure to be released its lock first
206 * @donor_inode: donor inode structure to be released its lock second
207 * Release write semaphore of two inodes (orig and donor).
208 */
209static void
210mext_double_up_write(struct inode *orig_inode, struct inode *donor_inode)
211{
212 BUG_ON(orig_inode == NULL || donor_inode == NULL);
213
214 up_write(&EXT4_I(orig_inode)->i_data_sem);
215 up_write(&EXT4_I(donor_inode)->i_data_sem);
216}
217
218/**
219 * mext_insert_across_blocks - Insert extents across leaf block
220 *
221 * @handle: journal handle
222 * @orig_inode: original inode
223 * @o_start: first original extent to be changed
224 * @o_end: last original extent to be changed
225 * @start_ext: first new extent to be inserted
226 * @new_ext: middle of new extent to be inserted
227 * @end_ext: last new extent to be inserted
228 *
229 * Allocate a new leaf block and insert extents into it. Return 0 on success,
230 * or a negative error value on failure.
231 */
232static int
233mext_insert_across_blocks(handle_t *handle, struct inode *orig_inode,
234 struct ext4_extent *o_start, struct ext4_extent *o_end,
235 struct ext4_extent *start_ext, struct ext4_extent *new_ext,
236 struct ext4_extent *end_ext)
237{
238 struct ext4_ext_path *orig_path = NULL;
239 ext4_lblk_t eblock = 0;
240 int new_flag = 0;
241 int end_flag = 0;
242 int err = 0;
243
244 if (start_ext->ee_len && new_ext->ee_len && end_ext->ee_len) {
245 if (o_start == o_end) {
246
247 /* start_ext new_ext end_ext
248 * donor |---------|-----------|--------|
249 * orig |------------------------------|
250 */
251 end_flag = 1;
252 } else {
253
254 /* start_ext new_ext end_ext
255 * donor |---------|----------|---------|
256 * orig |---------------|--------------|
257 */
258 o_end->ee_block = end_ext->ee_block;
259 o_end->ee_len = end_ext->ee_len;
260 ext4_ext_store_pblock(o_end, ext_pblock(end_ext));
261 }
262
263 o_start->ee_len = start_ext->ee_len;
264 new_flag = 1;
265
266 } else if (start_ext->ee_len && new_ext->ee_len &&
267 !end_ext->ee_len && o_start == o_end) {
268
269 /* start_ext new_ext
270 * donor |--------------|---------------|
271 * orig |------------------------------|
272 */
273 o_start->ee_len = start_ext->ee_len;
274 new_flag = 1;
275
276 } else if (!start_ext->ee_len && new_ext->ee_len &&
277 end_ext->ee_len && o_start == o_end) {
278
279 /* new_ext end_ext
280 * donor |--------------|---------------|
281 * orig |------------------------------|
282 */
283 o_end->ee_block = end_ext->ee_block;
284 o_end->ee_len = end_ext->ee_len;
285 ext4_ext_store_pblock(o_end, ext_pblock(end_ext));
286
287 /*
288 * Set 0 to the extent block if new_ext was
289 * the first block.
290 */
291 if (new_ext->ee_block)
292 eblock = le32_to_cpu(new_ext->ee_block);
293
294 new_flag = 1;
295 } else {
296 ext4_debug("ext4 move extent: Unexpected insert case\n");
297 return -EIO;
298 }
299
300 if (new_flag) {
Akira Fujitae8505972009-09-16 13:46:38 -0400301 err = get_ext_path(orig_inode, eblock, &orig_path);
Akira Fujita748de672009-06-17 19:24:03 -0400302 if (orig_path == NULL)
303 goto out;
304
305 if (ext4_ext_insert_extent(handle, orig_inode,
306 orig_path, new_ext))
307 goto out;
308 }
309
310 if (end_flag) {
Akira Fujitae8505972009-09-16 13:46:38 -0400311 err = get_ext_path(orig_inode,
312 le32_to_cpu(end_ext->ee_block) - 1, &orig_path);
Akira Fujita748de672009-06-17 19:24:03 -0400313 if (orig_path == NULL)
314 goto out;
315
316 if (ext4_ext_insert_extent(handle, orig_inode,
317 orig_path, end_ext))
318 goto out;
319 }
320out:
321 if (orig_path) {
322 ext4_ext_drop_refs(orig_path);
323 kfree(orig_path);
324 }
325
326 return err;
327
328}
329
330/**
331 * mext_insert_inside_block - Insert new extent to the extent block
332 *
333 * @o_start: first original extent to be moved
334 * @o_end: last original extent to be moved
335 * @start_ext: first new extent to be inserted
336 * @new_ext: middle of new extent to be inserted
337 * @end_ext: last new extent to be inserted
338 * @eh: extent header of target leaf block
339 * @range_to_move: used to decide how to insert extent
340 *
341 * Insert extents into the leaf block. The extent (@o_start) is overwritten
342 * by inserted extents.
343 */
344static void
345mext_insert_inside_block(struct ext4_extent *o_start,
346 struct ext4_extent *o_end,
347 struct ext4_extent *start_ext,
348 struct ext4_extent *new_ext,
349 struct ext4_extent *end_ext,
350 struct ext4_extent_header *eh,
351 int range_to_move)
352{
353 int i = 0;
354 unsigned long len;
355
356 /* Move the existing extents */
357 if (range_to_move && o_end < EXT_LAST_EXTENT(eh)) {
358 len = (unsigned long)(EXT_LAST_EXTENT(eh) + 1) -
359 (unsigned long)(o_end + 1);
360 memmove(o_end + 1 + range_to_move, o_end + 1, len);
361 }
362
363 /* Insert start entry */
364 if (start_ext->ee_len)
365 o_start[i++].ee_len = start_ext->ee_len;
366
367 /* Insert new entry */
368 if (new_ext->ee_len) {
369 o_start[i] = *new_ext;
370 ext4_ext_store_pblock(&o_start[i++], ext_pblock(new_ext));
371 }
372
373 /* Insert end entry */
374 if (end_ext->ee_len)
375 o_start[i] = *end_ext;
376
377 /* Increment the total entries counter on the extent block */
378 le16_add_cpu(&eh->eh_entries, range_to_move);
379}
380
381/**
382 * mext_insert_extents - Insert new extent
383 *
384 * @handle: journal handle
385 * @orig_inode: original inode
386 * @orig_path: path indicates first extent to be changed
387 * @o_start: first original extent to be changed
388 * @o_end: last original extent to be changed
389 * @start_ext: first new extent to be inserted
390 * @new_ext: middle of new extent to be inserted
391 * @end_ext: last new extent to be inserted
392 *
393 * Call the function to insert extents. If we cannot add more extents into
394 * the leaf block, we call mext_insert_across_blocks() to create a
395 * new leaf block. Otherwise call mext_insert_inside_block(). Return 0
396 * on success, or a negative error value on failure.
397 */
398static int
399mext_insert_extents(handle_t *handle, struct inode *orig_inode,
400 struct ext4_ext_path *orig_path,
401 struct ext4_extent *o_start,
402 struct ext4_extent *o_end,
403 struct ext4_extent *start_ext,
404 struct ext4_extent *new_ext,
405 struct ext4_extent *end_ext)
406{
407 struct ext4_extent_header *eh;
408 unsigned long need_slots, slots_range;
409 int range_to_move, depth, ret;
410
411 /*
412 * The extents need to be inserted
413 * start_extent + new_extent + end_extent.
414 */
415 need_slots = (start_ext->ee_len ? 1 : 0) + (end_ext->ee_len ? 1 : 0) +
416 (new_ext->ee_len ? 1 : 0);
417
418 /* The number of slots between start and end */
419 slots_range = ((unsigned long)(o_end + 1) - (unsigned long)o_start + 1)
420 / sizeof(struct ext4_extent);
421
422 /* Range to move the end of extent */
423 range_to_move = need_slots - slots_range;
424 depth = orig_path->p_depth;
425 orig_path += depth;
426 eh = orig_path->p_hdr;
427
428 if (depth) {
429 /* Register to journal */
430 ret = ext4_journal_get_write_access(handle, orig_path->p_bh);
431 if (ret)
432 return ret;
433 }
434
435 /* Expansion */
436 if (range_to_move > 0 &&
437 (range_to_move > le16_to_cpu(eh->eh_max)
438 - le16_to_cpu(eh->eh_entries))) {
439
440 ret = mext_insert_across_blocks(handle, orig_inode, o_start,
441 o_end, start_ext, new_ext, end_ext);
442 if (ret < 0)
443 return ret;
444 } else
445 mext_insert_inside_block(o_start, o_end, start_ext, new_ext,
446 end_ext, eh, range_to_move);
447
448 if (depth) {
449 ret = ext4_handle_dirty_metadata(handle, orig_inode,
450 orig_path->p_bh);
451 if (ret)
452 return ret;
453 } else {
454 ret = ext4_mark_inode_dirty(handle, orig_inode);
455 if (ret < 0)
456 return ret;
457 }
458
459 return 0;
460}
461
462/**
463 * mext_leaf_block - Move one leaf extent block into the inode.
464 *
465 * @handle: journal handle
466 * @orig_inode: original inode
467 * @orig_path: path indicates first extent to be changed
468 * @dext: donor extent
469 * @from: start offset on the target file
470 *
471 * In order to insert extents into the leaf block, we must divide the extent
472 * in the leaf block into three extents. The one is located to be inserted
473 * extents, and the others are located around it.
474 *
475 * Therefore, this function creates structures to save extents of the leaf
476 * block, and inserts extents by calling mext_insert_extents() with
477 * created extents. Return 0 on success, or a negative error value on failure.
478 */
479static int
480mext_leaf_block(handle_t *handle, struct inode *orig_inode,
481 struct ext4_ext_path *orig_path, struct ext4_extent *dext,
482 ext4_lblk_t *from)
483{
484 struct ext4_extent *oext, *o_start, *o_end, *prev_ext;
485 struct ext4_extent new_ext, start_ext, end_ext;
486 ext4_lblk_t new_ext_end;
487 ext4_fsblk_t new_phys_end;
488 int oext_alen, new_ext_alen, end_ext_alen;
489 int depth = ext_depth(orig_inode);
490 int ret;
491
492 o_start = o_end = oext = orig_path[depth].p_ext;
493 oext_alen = ext4_ext_get_actual_len(oext);
494 start_ext.ee_len = end_ext.ee_len = 0;
495
496 new_ext.ee_block = cpu_to_le32(*from);
497 ext4_ext_store_pblock(&new_ext, ext_pblock(dext));
498 new_ext.ee_len = dext->ee_len;
499 new_ext_alen = ext4_ext_get_actual_len(&new_ext);
500 new_ext_end = le32_to_cpu(new_ext.ee_block) + new_ext_alen - 1;
501 new_phys_end = ext_pblock(&new_ext) + new_ext_alen - 1;
502
503 /*
504 * Case: original extent is first
505 * oext |--------|
506 * new_ext |--|
507 * start_ext |--|
508 */
509 if (le32_to_cpu(oext->ee_block) < le32_to_cpu(new_ext.ee_block) &&
510 le32_to_cpu(new_ext.ee_block) <
511 le32_to_cpu(oext->ee_block) + oext_alen) {
512 start_ext.ee_len = cpu_to_le16(le32_to_cpu(new_ext.ee_block) -
513 le32_to_cpu(oext->ee_block));
514 copy_extent_status(oext, &start_ext);
515 } else if (oext > EXT_FIRST_EXTENT(orig_path[depth].p_hdr)) {
516 prev_ext = oext - 1;
517 /*
518 * We can merge new_ext into previous extent,
519 * if these are contiguous and same extent type.
520 */
521 if (ext4_can_extents_be_merged(orig_inode, prev_ext,
522 &new_ext)) {
523 o_start = prev_ext;
524 start_ext.ee_len = cpu_to_le16(
525 ext4_ext_get_actual_len(prev_ext) +
526 new_ext_alen);
527 copy_extent_status(prev_ext, &start_ext);
528 new_ext.ee_len = 0;
529 }
530 }
531
532 /*
533 * Case: new_ext_end must be less than oext
534 * oext |-----------|
535 * new_ext |-------|
536 */
537 BUG_ON(le32_to_cpu(oext->ee_block) + oext_alen - 1 < new_ext_end);
538
539 /*
540 * Case: new_ext is smaller than original extent
541 * oext |---------------|
542 * new_ext |-----------|
543 * end_ext |---|
544 */
545 if (le32_to_cpu(oext->ee_block) <= new_ext_end &&
546 new_ext_end < le32_to_cpu(oext->ee_block) + oext_alen - 1) {
547 end_ext.ee_len =
548 cpu_to_le16(le32_to_cpu(oext->ee_block) +
549 oext_alen - 1 - new_ext_end);
550 copy_extent_status(oext, &end_ext);
551 end_ext_alen = ext4_ext_get_actual_len(&end_ext);
552 ext4_ext_store_pblock(&end_ext,
553 (ext_pblock(o_end) + oext_alen - end_ext_alen));
554 end_ext.ee_block =
555 cpu_to_le32(le32_to_cpu(o_end->ee_block) +
556 oext_alen - end_ext_alen);
557 }
558
559 ret = mext_insert_extents(handle, orig_inode, orig_path, o_start,
560 o_end, &start_ext, &new_ext, &end_ext);
561 return ret;
562}
563
564/**
565 * mext_calc_swap_extents - Calculate extents for extent swapping.
566 *
567 * @tmp_dext: the extent that will belong to the original inode
568 * @tmp_oext: the extent that will belong to the donor inode
569 * @orig_off: block offset of original inode
570 * @donor_off: block offset of donor inode
571 * @max_count: the maximun length of extents
572 */
573static void
574mext_calc_swap_extents(struct ext4_extent *tmp_dext,
575 struct ext4_extent *tmp_oext,
576 ext4_lblk_t orig_off, ext4_lblk_t donor_off,
577 ext4_lblk_t max_count)
578{
579 ext4_lblk_t diff, orig_diff;
580 struct ext4_extent dext_old, oext_old;
581
582 dext_old = *tmp_dext;
583 oext_old = *tmp_oext;
584
585 /* When tmp_dext is too large, pick up the target range. */
586 diff = donor_off - le32_to_cpu(tmp_dext->ee_block);
587
588 ext4_ext_store_pblock(tmp_dext, ext_pblock(tmp_dext) + diff);
589 tmp_dext->ee_block =
590 cpu_to_le32(le32_to_cpu(tmp_dext->ee_block) + diff);
591 tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_dext->ee_len) - diff);
592
593 if (max_count < ext4_ext_get_actual_len(tmp_dext))
594 tmp_dext->ee_len = cpu_to_le16(max_count);
595
596 orig_diff = orig_off - le32_to_cpu(tmp_oext->ee_block);
597 ext4_ext_store_pblock(tmp_oext, ext_pblock(tmp_oext) + orig_diff);
598
599 /* Adjust extent length if donor extent is larger than orig */
600 if (ext4_ext_get_actual_len(tmp_dext) >
601 ext4_ext_get_actual_len(tmp_oext) - orig_diff)
602 tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_oext->ee_len) -
603 orig_diff);
604
605 tmp_oext->ee_len = cpu_to_le16(ext4_ext_get_actual_len(tmp_dext));
606
607 copy_extent_status(&oext_old, tmp_dext);
608 copy_extent_status(&dext_old, tmp_oext);
609}
610
611/**
612 * mext_replace_branches - Replace original extents with new extents
613 *
614 * @handle: journal handle
615 * @orig_inode: original inode
616 * @donor_inode: donor inode
617 * @from: block offset of orig_inode
618 * @count: block count to be replaced
619 *
620 * Replace original inode extents and donor inode extents page by page.
621 * We implement this replacement in the following three steps:
622 * 1. Save the block information of original and donor inodes into
623 * dummy extents.
624 * 2. Change the block information of original inode to point at the
625 * donor inode blocks.
626 * 3. Change the block information of donor inode to point at the saved
627 * original inode blocks in the dummy extents.
628 *
629 * Return 0 on success, or a negative error value on failure.
630 */
631static int
632mext_replace_branches(handle_t *handle, struct inode *orig_inode,
633 struct inode *donor_inode, ext4_lblk_t from,
634 ext4_lblk_t count)
635{
636 struct ext4_ext_path *orig_path = NULL;
637 struct ext4_ext_path *donor_path = NULL;
638 struct ext4_extent *oext, *dext;
639 struct ext4_extent tmp_dext, tmp_oext;
640 ext4_lblk_t orig_off = from, donor_off = from;
641 int err = 0;
642 int depth;
643 int replaced_count = 0;
644 int dext_alen;
645
646 mext_double_down_write(orig_inode, donor_inode);
647
648 /* Get the original extent for the block "orig_off" */
Akira Fujitae8505972009-09-16 13:46:38 -0400649 err = get_ext_path(orig_inode, orig_off, &orig_path);
Akira Fujita748de672009-06-17 19:24:03 -0400650 if (orig_path == NULL)
651 goto out;
652
653 /* Get the donor extent for the head */
Akira Fujitae8505972009-09-16 13:46:38 -0400654 err = get_ext_path(donor_inode, donor_off, &donor_path);
Akira Fujita748de672009-06-17 19:24:03 -0400655 if (donor_path == NULL)
656 goto out;
657 depth = ext_depth(orig_inode);
658 oext = orig_path[depth].p_ext;
659 tmp_oext = *oext;
660
661 depth = ext_depth(donor_inode);
662 dext = donor_path[depth].p_ext;
663 tmp_dext = *dext;
664
665 mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
666 donor_off, count);
667
668 /* Loop for the donor extents */
669 while (1) {
670 /* The extent for donor must be found. */
671 BUG_ON(!dext || donor_off != le32_to_cpu(tmp_dext.ee_block));
672
673 /* Set donor extent to orig extent */
674 err = mext_leaf_block(handle, orig_inode,
675 orig_path, &tmp_dext, &orig_off);
676 if (err < 0)
677 goto out;
678
679 /* Set orig extent to donor extent */
680 err = mext_leaf_block(handle, donor_inode,
681 donor_path, &tmp_oext, &donor_off);
682 if (err < 0)
683 goto out;
684
685 dext_alen = ext4_ext_get_actual_len(&tmp_dext);
686 replaced_count += dext_alen;
687 donor_off += dext_alen;
688 orig_off += dext_alen;
689
690 /* Already moved the expected blocks */
691 if (replaced_count >= count)
692 break;
693
694 if (orig_path)
695 ext4_ext_drop_refs(orig_path);
Akira Fujitae8505972009-09-16 13:46:38 -0400696 err = get_ext_path(orig_inode, orig_off, &orig_path);
Akira Fujita748de672009-06-17 19:24:03 -0400697 if (orig_path == NULL)
698 goto out;
699 depth = ext_depth(orig_inode);
700 oext = orig_path[depth].p_ext;
701 if (le32_to_cpu(oext->ee_block) +
702 ext4_ext_get_actual_len(oext) <= orig_off) {
703 err = 0;
704 goto out;
705 }
706 tmp_oext = *oext;
707
708 if (donor_path)
709 ext4_ext_drop_refs(donor_path);
Akira Fujitae8505972009-09-16 13:46:38 -0400710 err = get_ext_path(donor_inode, donor_off, &donor_path);
Akira Fujita748de672009-06-17 19:24:03 -0400711 if (donor_path == NULL)
712 goto out;
713 depth = ext_depth(donor_inode);
714 dext = donor_path[depth].p_ext;
715 if (le32_to_cpu(dext->ee_block) +
716 ext4_ext_get_actual_len(dext) <= donor_off) {
717 err = 0;
718 goto out;
719 }
720 tmp_dext = *dext;
721
722 mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
723 donor_off,
724 count - replaced_count);
725 }
726
727out:
728 if (orig_path) {
729 ext4_ext_drop_refs(orig_path);
730 kfree(orig_path);
731 }
732 if (donor_path) {
733 ext4_ext_drop_refs(donor_path);
734 kfree(donor_path);
735 }
736
737 mext_double_up_write(orig_inode, donor_inode);
738 return err;
739}
740
741/**
742 * move_extent_per_page - Move extent data per page
743 *
744 * @o_filp: file structure of original file
745 * @donor_inode: donor inode
746 * @orig_page_offset: page index on original file
747 * @data_offset_in_page: block index where data swapping starts
748 * @block_len_in_page: the number of blocks to be swapped
749 * @uninit: orig extent is uninitialized or not
750 *
751 * Save the data in original inode blocks and replace original inode extents
752 * with donor inode extents by calling mext_replace_branches().
753 * Finally, write out the saved data in new original inode blocks. Return 0
754 * on success, or a negative error value on failure.
755 */
756static int
Akira Fujita44fc48f2009-09-05 23:12:41 -0400757move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -0400758 pgoff_t orig_page_offset, int data_offset_in_page,
759 int block_len_in_page, int uninit)
760{
761 struct inode *orig_inode = o_filp->f_dentry->d_inode;
762 struct address_space *mapping = orig_inode->i_mapping;
763 struct buffer_head *bh;
764 struct page *page = NULL;
765 const struct address_space_operations *a_ops = mapping->a_ops;
766 handle_t *handle;
767 ext4_lblk_t orig_blk_offset;
768 long long offs = orig_page_offset << PAGE_CACHE_SHIFT;
769 unsigned long blocksize = orig_inode->i_sb->s_blocksize;
770 unsigned int w_flags = 0;
771 unsigned int tmp_data_len, data_len;
772 void *fsdata;
773 int ret, i, jblocks;
774 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
775
776 /*
777 * It needs twice the amount of ordinary journal buffers because
778 * inode and donor_inode may change each different metadata blocks.
779 */
780 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
781 handle = ext4_journal_start(orig_inode, jblocks);
782 if (IS_ERR(handle)) {
783 ret = PTR_ERR(handle);
784 return ret;
785 }
786
787 if (segment_eq(get_fs(), KERNEL_DS))
788 w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
789
790 orig_blk_offset = orig_page_offset * blocks_per_page +
791 data_offset_in_page;
792
793 /*
794 * If orig extent is uninitialized one,
795 * it's not necessary force the page into memory
796 * and then force it to be written out again.
797 * Just swap data blocks between orig and donor.
798 */
799 if (uninit) {
800 ret = mext_replace_branches(handle, orig_inode,
801 donor_inode, orig_blk_offset,
802 block_len_in_page);
803
804 /* Clear the inode cache not to refer to the old data */
805 ext4_ext_invalidate_cache(orig_inode);
806 ext4_ext_invalidate_cache(donor_inode);
807 goto out2;
808 }
809
810 offs = (long long)orig_blk_offset << orig_inode->i_blkbits;
811
812 /* Calculate data_len */
813 if ((orig_blk_offset + block_len_in_page - 1) ==
814 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
815 /* Replace the last block */
816 tmp_data_len = orig_inode->i_size & (blocksize - 1);
817 /*
818 * If data_len equal zero, it shows data_len is multiples of
819 * blocksize. So we set appropriate value.
820 */
821 if (tmp_data_len == 0)
822 tmp_data_len = blocksize;
823
824 data_len = tmp_data_len +
825 ((block_len_in_page - 1) << orig_inode->i_blkbits);
826 } else {
827 data_len = block_len_in_page << orig_inode->i_blkbits;
828 }
829
830 ret = a_ops->write_begin(o_filp, mapping, offs, data_len, w_flags,
831 &page, &fsdata);
832 if (unlikely(ret < 0))
833 goto out;
834
835 if (!PageUptodate(page)) {
836 mapping->a_ops->readpage(o_filp, page);
837 lock_page(page);
838 }
839
840 /*
841 * try_to_release_page() doesn't call releasepage in writeback mode.
842 * We should care about the order of writing to the same file
843 * by multiple move extent processes.
844 * It needs to call wait_on_page_writeback() to wait for the
845 * writeback of the page.
846 */
847 if (PageWriteback(page))
848 wait_on_page_writeback(page);
849
850 /* Release old bh and drop refs */
851 try_to_release_page(page, 0);
852
853 ret = mext_replace_branches(handle, orig_inode, donor_inode,
854 orig_blk_offset, block_len_in_page);
855 if (ret < 0)
856 goto out;
857
858 /* Clear the inode cache not to refer to the old data */
859 ext4_ext_invalidate_cache(orig_inode);
860 ext4_ext_invalidate_cache(donor_inode);
861
862 if (!page_has_buffers(page))
863 create_empty_buffers(page, 1 << orig_inode->i_blkbits, 0);
864
865 bh = page_buffers(page);
866 for (i = 0; i < data_offset_in_page; i++)
867 bh = bh->b_this_page;
868
869 for (i = 0; i < block_len_in_page; i++) {
870 ret = ext4_get_block(orig_inode,
871 (sector_t)(orig_blk_offset + i), bh, 0);
872 if (ret < 0)
873 goto out;
874
875 if (bh->b_this_page != NULL)
876 bh = bh->b_this_page;
877 }
878
879 ret = a_ops->write_end(o_filp, mapping, offs, data_len, data_len,
880 page, fsdata);
881 page = NULL;
882
883out:
884 if (unlikely(page)) {
885 if (PageLocked(page))
886 unlock_page(page);
887 page_cache_release(page);
Peng Tao91cc2192009-08-10 23:05:28 -0400888 ext4_journal_stop(handle);
Akira Fujita748de672009-06-17 19:24:03 -0400889 }
890out2:
891 ext4_journal_stop(handle);
892
893 return ret < 0 ? ret : 0;
894}
895
896/**
897 * mext_check_argumants - Check whether move extent can be done
898 *
899 * @orig_inode: original inode
900 * @donor_inode: donor inode
901 * @orig_start: logical start offset in block for orig
902 * @donor_start: logical start offset in block for donor
903 * @len: the number of blocks to be moved
904 * @moved_len: moved block length
905 *
906 * Check the arguments of ext4_move_extents() whether the files can be
907 * exchanged with each other.
908 * Return 0 on success, or a negative error value on failure.
909 */
910static int
911mext_check_arguments(struct inode *orig_inode,
912 struct inode *donor_inode, __u64 orig_start,
913 __u64 donor_start, __u64 *len, __u64 moved_len)
914{
Akira Fujita70d5d3d2009-09-16 14:28:22 -0400915 ext4_lblk_t orig_blocks, donor_blocks;
916 unsigned int blkbits = orig_inode->i_blkbits;
917 unsigned int blocksize = 1 << blkbits;
918
Akira Fujita748de672009-06-17 19:24:03 -0400919 /* Regular file check */
920 if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
921 ext4_debug("ext4 move extent: The argument files should be "
922 "regular file [ino:orig %lu, donor %lu]\n",
923 orig_inode->i_ino, donor_inode->i_ino);
924 return -EINVAL;
925 }
926
927 /* Ext4 move extent does not support swapfile */
928 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
929 ext4_debug("ext4 move extent: The argument files should "
930 "not be swapfile [ino:orig %lu, donor %lu]\n",
931 orig_inode->i_ino, donor_inode->i_ino);
932 return -EINVAL;
933 }
934
935 /* Files should be in the same ext4 FS */
936 if (orig_inode->i_sb != donor_inode->i_sb) {
937 ext4_debug("ext4 move extent: The argument files "
938 "should be in same FS [ino:orig %lu, donor %lu]\n",
939 orig_inode->i_ino, donor_inode->i_ino);
940 return -EINVAL;
941 }
942
943 /* orig and donor should be different file */
944 if (orig_inode->i_ino == donor_inode->i_ino) {
945 ext4_debug("ext4 move extent: The argument files should not "
946 "be same file [ino:orig %lu, donor %lu]\n",
947 orig_inode->i_ino, donor_inode->i_ino);
948 return -EINVAL;
949 }
950
951 /* Ext4 move extent supports only extent based file */
952 if (!(EXT4_I(orig_inode)->i_flags & EXT4_EXTENTS_FL)) {
953 ext4_debug("ext4 move extent: orig file is not extents "
954 "based file [ino:orig %lu]\n", orig_inode->i_ino);
955 return -EOPNOTSUPP;
956 } else if (!(EXT4_I(donor_inode)->i_flags & EXT4_EXTENTS_FL)) {
957 ext4_debug("ext4 move extent: donor file is not extents "
958 "based file [ino:donor %lu]\n", donor_inode->i_ino);
959 return -EOPNOTSUPP;
960 }
961
962 if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
963 ext4_debug("ext4 move extent: File size is 0 byte\n");
964 return -EINVAL;
965 }
966
967 /* Start offset should be same */
968 if (orig_start != donor_start) {
969 ext4_debug("ext4 move extent: orig and donor's start "
970 "offset are not same [ino:orig %lu, donor %lu]\n",
971 orig_inode->i_ino, donor_inode->i_ino);
972 return -EINVAL;
973 }
974
975 if (moved_len) {
976 ext4_debug("ext4 move extent: moved_len should be 0 "
977 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
978 donor_inode->i_ino);
979 return -EINVAL;
980 }
981
982 if ((orig_start > MAX_DEFRAG_SIZE) ||
983 (donor_start > MAX_DEFRAG_SIZE) ||
984 (*len > MAX_DEFRAG_SIZE) ||
985 (orig_start + *len > MAX_DEFRAG_SIZE)) {
986 ext4_debug("ext4 move extent: Can't handle over [%lu] blocks "
987 "[ino:orig %lu, donor %lu]\n", MAX_DEFRAG_SIZE,
988 orig_inode->i_ino, donor_inode->i_ino);
989 return -EINVAL;
990 }
991
992 if (orig_inode->i_size > donor_inode->i_size) {
Akira Fujita70d5d3d2009-09-16 14:28:22 -0400993 donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits;
994 /* TODO: eliminate this artificial restriction */
995 if (orig_start >= donor_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -0400996 ext4_debug("ext4 move extent: orig start offset "
Akira Fujita70d5d3d2009-09-16 14:28:22 -0400997 "[%llu] should be less than donor file blocks "
998 "[%u] [ino:orig %lu, donor %lu]\n",
999 orig_start, donor_blocks,
Akira Fujita748de672009-06-17 19:24:03 -04001000 orig_inode->i_ino, donor_inode->i_ino);
1001 return -EINVAL;
1002 }
1003
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001004 /* TODO: eliminate this artificial restriction */
1005 if (orig_start + *len > donor_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001006 ext4_debug("ext4 move extent: End offset [%llu] should "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001007 "be less than donor file blocks [%u]."
1008 "So adjust length from %llu to %llu "
Akira Fujita748de672009-06-17 19:24:03 -04001009 "[ino:orig %lu, donor %lu]\n",
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001010 orig_start + *len, donor_blocks,
1011 *len, donor_blocks - orig_start,
Akira Fujita748de672009-06-17 19:24:03 -04001012 orig_inode->i_ino, donor_inode->i_ino);
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001013 *len = donor_blocks - orig_start;
Akira Fujita748de672009-06-17 19:24:03 -04001014 }
1015 } else {
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001016 orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits;
1017 if (orig_start >= orig_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001018 ext4_debug("ext4 move extent: start offset [%llu] "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001019 "should be less than original file blocks "
1020 "[%u] [ino:orig %lu, donor %lu]\n",
1021 orig_start, orig_blocks,
Akira Fujita748de672009-06-17 19:24:03 -04001022 orig_inode->i_ino, donor_inode->i_ino);
1023 return -EINVAL;
1024 }
1025
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001026 if (orig_start + *len > orig_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001027 ext4_debug("ext4 move extent: Adjust length "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001028 "from %llu to %llu. Because it should be "
1029 "less than original file blocks "
Akira Fujita748de672009-06-17 19:24:03 -04001030 "[ino:orig %lu, donor %lu]\n",
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001031 *len, orig_blocks - orig_start,
Akira Fujita748de672009-06-17 19:24:03 -04001032 orig_inode->i_ino, donor_inode->i_ino);
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001033 *len = orig_blocks - orig_start;
Akira Fujita748de672009-06-17 19:24:03 -04001034 }
1035 }
1036
1037 if (!*len) {
1038 ext4_debug("ext4 move extent: len shoudld not be 0 "
1039 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
1040 donor_inode->i_ino);
1041 return -EINVAL;
1042 }
1043
1044 return 0;
1045}
1046
1047/**
1048 * mext_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
1049 *
1050 * @inode1: the inode structure
1051 * @inode2: the inode structure
1052 *
1053 * Lock two inodes' i_mutex by i_ino order. This function is moved from
1054 * fs/inode.c.
1055 */
1056static void
1057mext_inode_double_lock(struct inode *inode1, struct inode *inode2)
1058{
1059 if (inode1 == NULL || inode2 == NULL || inode1 == inode2) {
1060 if (inode1)
1061 mutex_lock(&inode1->i_mutex);
1062 else if (inode2)
1063 mutex_lock(&inode2->i_mutex);
1064 return;
1065 }
1066
1067 if (inode1->i_ino < inode2->i_ino) {
1068 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
1069 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
1070 } else {
1071 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
1072 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
1073 }
1074}
1075
1076/**
1077 * mext_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
1078 *
1079 * @inode1: the inode that is released first
1080 * @inode2: the inode that is released second
1081 *
1082 * This function is moved from fs/inode.c.
1083 */
1084
1085static void
1086mext_inode_double_unlock(struct inode *inode1, struct inode *inode2)
1087{
1088 if (inode1)
1089 mutex_unlock(&inode1->i_mutex);
1090
1091 if (inode2 && inode2 != inode1)
1092 mutex_unlock(&inode2->i_mutex);
1093}
1094
1095/**
1096 * ext4_move_extents - Exchange the specified range of a file
1097 *
1098 * @o_filp: file structure of the original file
1099 * @d_filp: file structure of the donor file
1100 * @orig_start: start offset in block for orig
1101 * @donor_start: start offset in block for donor
1102 * @len: the number of blocks to be moved
1103 * @moved_len: moved block length
1104 *
1105 * This function returns 0 and moved block length is set in moved_len
1106 * if succeed, otherwise returns error value.
1107 *
1108 * Note: ext4_move_extents() proceeds the following order.
1109 * 1:ext4_move_extents() calculates the last block number of moving extent
1110 * function by the start block number (orig_start) and the number of blocks
1111 * to be moved (len) specified as arguments.
1112 * If the {orig, donor}_start points a hole, the extent's start offset
1113 * pointed by ext_cur (current extent), holecheck_path, orig_path are set
1114 * after hole behind.
1115 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
1116 * or the ext_cur exceeds the block_end which is last logical block number.
1117 * 3:To get the length of continues area, call mext_next_extent()
1118 * specified with the ext_cur (initial value is holecheck_path) re-cursive,
1119 * until find un-continuous extent, the start logical block number exceeds
1120 * the block_end or the extent points to the last extent.
1121 * 4:Exchange the original inode data with donor inode data
1122 * from orig_page_offset to seq_end_page.
1123 * The start indexes of data are specified as arguments.
1124 * That of the original inode is orig_page_offset,
1125 * and the donor inode is also orig_page_offset
1126 * (To easily handle blocksize != pagesize case, the offset for the
1127 * donor inode is block unit).
1128 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
1129 * then returns to step 2.
1130 * 6:Release holecheck_path, orig_path and set the len to moved_len
1131 * which shows the number of moved blocks.
1132 * The moved_len is useful for the command to calculate the file offset
1133 * for starting next move extent ioctl.
1134 * 7:Return 0 on success, or a negative error value on failure.
1135 */
1136int
1137ext4_move_extents(struct file *o_filp, struct file *d_filp,
1138 __u64 orig_start, __u64 donor_start, __u64 len,
1139 __u64 *moved_len)
1140{
1141 struct inode *orig_inode = o_filp->f_dentry->d_inode;
1142 struct inode *donor_inode = d_filp->f_dentry->d_inode;
1143 struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL;
1144 struct ext4_extent *ext_prev, *ext_cur, *ext_dummy;
1145 ext4_lblk_t block_start = orig_start;
1146 ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0;
1147 ext4_lblk_t rest_blocks;
1148 pgoff_t orig_page_offset = 0, seq_end_page;
1149 int ret, depth, last_extent = 0;
1150 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
1151 int data_offset_in_page;
1152 int block_len_in_page;
1153 int uninit;
1154
1155 /* protect orig and donor against a truncate */
1156 mext_inode_double_lock(orig_inode, donor_inode);
1157
1158 mext_double_down_read(orig_inode, donor_inode);
1159 /* Check the filesystem environment whether move_extent can be done */
1160 ret = mext_check_arguments(orig_inode, donor_inode, orig_start,
1161 donor_start, &len, *moved_len);
1162 mext_double_up_read(orig_inode, donor_inode);
1163 if (ret)
1164 goto out2;
1165
1166 file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits;
1167 block_end = block_start + len - 1;
1168 if (file_end < block_end)
1169 len -= block_end - file_end;
1170
Akira Fujitae8505972009-09-16 13:46:38 -04001171 ret = get_ext_path(orig_inode, block_start, &orig_path);
Akira Fujita748de672009-06-17 19:24:03 -04001172 if (orig_path == NULL)
1173 goto out2;
1174
1175 /* Get path structure to check the hole */
Akira Fujitae8505972009-09-16 13:46:38 -04001176 ret = get_ext_path(orig_inode, block_start, &holecheck_path);
Akira Fujita748de672009-06-17 19:24:03 -04001177 if (holecheck_path == NULL)
1178 goto out;
1179
1180 depth = ext_depth(orig_inode);
1181 ext_cur = holecheck_path[depth].p_ext;
1182 if (ext_cur == NULL) {
1183 ret = -EINVAL;
1184 goto out;
1185 }
1186
1187 /*
1188 * Get proper extent whose ee_block is beyond block_start
1189 * if block_start was within the hole.
1190 */
1191 if (le32_to_cpu(ext_cur->ee_block) +
1192 ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
1193 last_extent = mext_next_extent(orig_inode,
1194 holecheck_path, &ext_cur);
1195 if (last_extent < 0) {
1196 ret = last_extent;
1197 goto out;
1198 }
1199 last_extent = mext_next_extent(orig_inode, orig_path,
1200 &ext_dummy);
1201 if (last_extent < 0) {
1202 ret = last_extent;
1203 goto out;
1204 }
1205 }
1206 seq_start = block_start;
1207
1208 /* No blocks within the specified range. */
1209 if (le32_to_cpu(ext_cur->ee_block) > block_end) {
1210 ext4_debug("ext4 move extent: The specified range of file "
1211 "may be the hole\n");
1212 ret = -EINVAL;
1213 goto out;
1214 }
1215
1216 /* Adjust start blocks */
1217 add_blocks = min(le32_to_cpu(ext_cur->ee_block) +
1218 ext4_ext_get_actual_len(ext_cur), block_end + 1) -
1219 max(le32_to_cpu(ext_cur->ee_block), block_start);
1220
1221 while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
1222 seq_blocks += add_blocks;
1223
1224 /* Adjust tail blocks */
1225 if (seq_start + seq_blocks - 1 > block_end)
1226 seq_blocks = block_end - seq_start + 1;
1227
1228 ext_prev = ext_cur;
1229 last_extent = mext_next_extent(orig_inode, holecheck_path,
1230 &ext_cur);
1231 if (last_extent < 0) {
1232 ret = last_extent;
1233 break;
1234 }
1235 add_blocks = ext4_ext_get_actual_len(ext_cur);
1236
1237 /*
1238 * Extend the length of contiguous block (seq_blocks)
1239 * if extents are contiguous.
1240 */
1241 if (ext4_can_extents_be_merged(orig_inode,
1242 ext_prev, ext_cur) &&
1243 block_end >= le32_to_cpu(ext_cur->ee_block) &&
1244 !last_extent)
1245 continue;
1246
1247 /* Is original extent is uninitialized */
1248 uninit = ext4_ext_is_uninitialized(ext_prev);
1249
1250 data_offset_in_page = seq_start % blocks_per_page;
1251
1252 /*
1253 * Calculate data blocks count that should be swapped
1254 * at the first page.
1255 */
1256 if (data_offset_in_page + seq_blocks > blocks_per_page) {
1257 /* Swapped blocks are across pages */
1258 block_len_in_page =
1259 blocks_per_page - data_offset_in_page;
1260 } else {
1261 /* Swapped blocks are in a page */
1262 block_len_in_page = seq_blocks;
1263 }
1264
1265 orig_page_offset = seq_start >>
1266 (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1267 seq_end_page = (seq_start + seq_blocks - 1) >>
1268 (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1269 seq_start = le32_to_cpu(ext_cur->ee_block);
1270 rest_blocks = seq_blocks;
1271
1272 /* Discard preallocations of two inodes */
1273 down_write(&EXT4_I(orig_inode)->i_data_sem);
1274 ext4_discard_preallocations(orig_inode);
1275 up_write(&EXT4_I(orig_inode)->i_data_sem);
1276
1277 down_write(&EXT4_I(donor_inode)->i_data_sem);
1278 ext4_discard_preallocations(donor_inode);
1279 up_write(&EXT4_I(donor_inode)->i_data_sem);
1280
1281 while (orig_page_offset <= seq_end_page) {
1282
1283 /* Swap original branches with new branches */
Akira Fujita44fc48f2009-09-05 23:12:41 -04001284 ret = move_extent_per_page(o_filp, donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -04001285 orig_page_offset,
1286 data_offset_in_page,
1287 block_len_in_page, uninit);
1288 if (ret < 0)
1289 goto out;
1290 orig_page_offset++;
1291 /* Count how many blocks we have exchanged */
1292 *moved_len += block_len_in_page;
1293 BUG_ON(*moved_len > len);
1294
1295 data_offset_in_page = 0;
1296 rest_blocks -= block_len_in_page;
1297 if (rest_blocks > blocks_per_page)
1298 block_len_in_page = blocks_per_page;
1299 else
1300 block_len_in_page = rest_blocks;
1301 }
1302
1303 /* Decrease buffer counter */
1304 if (holecheck_path)
1305 ext4_ext_drop_refs(holecheck_path);
Akira Fujitae8505972009-09-16 13:46:38 -04001306 ret = get_ext_path(orig_inode, seq_start, &holecheck_path);
Akira Fujita748de672009-06-17 19:24:03 -04001307 if (holecheck_path == NULL)
1308 break;
1309 depth = holecheck_path->p_depth;
1310
1311 /* Decrease buffer counter */
1312 if (orig_path)
1313 ext4_ext_drop_refs(orig_path);
Akira Fujitae8505972009-09-16 13:46:38 -04001314 ret = get_ext_path(orig_inode, seq_start, &orig_path);
Akira Fujita748de672009-06-17 19:24:03 -04001315 if (orig_path == NULL)
1316 break;
1317
1318 ext_cur = holecheck_path[depth].p_ext;
1319 add_blocks = ext4_ext_get_actual_len(ext_cur);
1320 seq_blocks = 0;
1321
1322 }
1323out:
1324 if (orig_path) {
1325 ext4_ext_drop_refs(orig_path);
1326 kfree(orig_path);
1327 }
1328 if (holecheck_path) {
1329 ext4_ext_drop_refs(holecheck_path);
1330 kfree(holecheck_path);
1331 }
1332out2:
1333 mext_inode_double_unlock(orig_inode, donor_inode);
1334
1335 if (ret)
1336 return ret;
1337
Akira Fujita748de672009-06-17 19:24:03 -04001338 return 0;
1339}