blob: 33e1c086858b54ad704e80345c92b946ae3c565b [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,
Dmitry Monakhov0e401102013-03-18 11:40:19 -040035 struct ext4_ext_path **orig_path)
Akira Fujitae8505972009-09-16 13:46:38 -040036{
37 int ret = 0;
Dmitry Monakhov0e401102013-03-18 11:40:19 -040038 struct ext4_ext_path *path;
Akira Fujitae8505972009-09-16 13:46:38 -040039
Dmitry Monakhov0e401102013-03-18 11:40:19 -040040 path = ext4_ext_find_extent(inode, lblock, *orig_path);
41 if (IS_ERR(path))
42 ret = PTR_ERR(path);
43 else if (path[ext_depth(inode)].p_ext == NULL)
Akira Fujita347fa6f2009-09-16 14:25:07 -040044 ret = -ENODATA;
Dmitry Monakhov0e401102013-03-18 11:40:19 -040045 else
46 *orig_path = path;
Akira Fujita347fa6f2009-09-16 14:25:07 -040047
Akira Fujitae8505972009-09-16 13:46:38 -040048 return ret;
49}
Akira Fujita748de672009-06-17 19:24:03 -040050
51/**
52 * copy_extent_status - Copy the extent's initialization status
53 *
54 * @src: an extent for getting initialize status
55 * @dest: an extent to be set the status
56 */
57static void
58copy_extent_status(struct ext4_extent *src, struct ext4_extent *dest)
59{
60 if (ext4_ext_is_uninitialized(src))
61 ext4_ext_mark_uninitialized(dest);
62 else
63 dest->ee_len = cpu_to_le16(ext4_ext_get_actual_len(dest));
64}
65
66/**
67 * mext_next_extent - Search for the next extent and set it to "extent"
68 *
69 * @inode: inode which is searched
70 * @path: this will obtain data for the next extent
71 * @extent: pointer to the next extent we have just gotten
72 *
73 * Search the next extent in the array of ext4_ext_path structure (@path)
74 * and set it to ext4_extent structure (@extent). In addition, the member of
75 * @path (->p_ext) also points the next extent. Return 0 on success, 1 if
76 * ext4_ext_path structure refers to the last extent, or a negative error
77 * value on failure.
78 */
79static int
80mext_next_extent(struct inode *inode, struct ext4_ext_path *path,
81 struct ext4_extent **extent)
82{
Akira Fujitafc04cb42009-11-23 07:24:43 -050083 struct ext4_extent_header *eh;
Akira Fujita748de672009-06-17 19:24:03 -040084 int ppos, leaf_ppos = path->p_depth;
85
86 ppos = leaf_ppos;
87 if (EXT_LAST_EXTENT(path[ppos].p_hdr) > path[ppos].p_ext) {
88 /* leaf block */
89 *extent = ++path[ppos].p_ext;
Theodore Ts'obf89d162010-10-27 21:30:14 -040090 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Akira Fujita748de672009-06-17 19:24:03 -040091 return 0;
92 }
93
94 while (--ppos >= 0) {
95 if (EXT_LAST_INDEX(path[ppos].p_hdr) >
96 path[ppos].p_idx) {
97 int cur_ppos = ppos;
98
99 /* index block */
100 path[ppos].p_idx++;
Theodore Ts'obf89d162010-10-27 21:30:14 -0400101 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Akira Fujita748de672009-06-17 19:24:03 -0400102 if (path[ppos+1].p_bh)
103 brelse(path[ppos+1].p_bh);
104 path[ppos+1].p_bh =
105 sb_bread(inode->i_sb, path[ppos].p_block);
106 if (!path[ppos+1].p_bh)
107 return -EIO;
108 path[ppos+1].p_hdr =
109 ext_block_hdr(path[ppos+1].p_bh);
110
111 /* Halfway index block */
112 while (++cur_ppos < leaf_ppos) {
113 path[cur_ppos].p_idx =
114 EXT_FIRST_INDEX(path[cur_ppos].p_hdr);
115 path[cur_ppos].p_block =
Theodore Ts'obf89d162010-10-27 21:30:14 -0400116 ext4_idx_pblock(path[cur_ppos].p_idx);
Akira Fujita748de672009-06-17 19:24:03 -0400117 if (path[cur_ppos+1].p_bh)
118 brelse(path[cur_ppos+1].p_bh);
119 path[cur_ppos+1].p_bh = sb_bread(inode->i_sb,
120 path[cur_ppos].p_block);
121 if (!path[cur_ppos+1].p_bh)
122 return -EIO;
123 path[cur_ppos+1].p_hdr =
124 ext_block_hdr(path[cur_ppos+1].p_bh);
125 }
126
Akira Fujitafc04cb42009-11-23 07:24:43 -0500127 path[leaf_ppos].p_ext = *extent = NULL;
128
129 eh = path[leaf_ppos].p_hdr;
130 if (le16_to_cpu(eh->eh_entries) == 0)
131 /* empty leaf is found */
132 return -ENODATA;
133
Akira Fujita748de672009-06-17 19:24:03 -0400134 /* leaf block */
135 path[leaf_ppos].p_ext = *extent =
136 EXT_FIRST_EXTENT(path[leaf_ppos].p_hdr);
Akira Fujitafc04cb42009-11-23 07:24:43 -0500137 path[leaf_ppos].p_block =
Theodore Ts'obf89d162010-10-27 21:30:14 -0400138 ext4_ext_pblock(path[leaf_ppos].p_ext);
Akira Fujita748de672009-06-17 19:24:03 -0400139 return 0;
140 }
141 }
142 /* We found the last extent */
143 return 1;
144}
145
146/**
Akira Fujitafc04cb42009-11-23 07:24:43 -0500147 * double_down_write_data_sem - Acquire two inodes' write lock of i_data_sem
Akira Fujita748de672009-06-17 19:24:03 -0400148 *
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400149 * Acquire write lock of i_data_sem of the two inodes
Akira Fujita748de672009-06-17 19:24:03 -0400150 */
151static void
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400152double_down_write_data_sem(struct inode *first, struct inode *second)
Akira Fujita748de672009-06-17 19:24:03 -0400153{
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400154 if (first < second) {
155 down_write(&EXT4_I(first)->i_data_sem);
156 down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING);
157 } else {
158 down_write(&EXT4_I(second)->i_data_sem);
159 down_write_nested(&EXT4_I(first)->i_data_sem, SINGLE_DEPTH_NESTING);
Akira Fujita748de672009-06-17 19:24:03 -0400160
Akira Fujita748de672009-06-17 19:24:03 -0400161 }
Akira Fujita748de672009-06-17 19:24:03 -0400162}
163
164/**
Akira Fujitafc04cb42009-11-23 07:24:43 -0500165 * double_up_write_data_sem - Release two inodes' write lock of i_data_sem
Akira Fujita748de672009-06-17 19:24:03 -0400166 *
167 * @orig_inode: original inode structure to be released its lock first
168 * @donor_inode: donor inode structure to be released its lock second
Akira Fujitafc04cb42009-11-23 07:24:43 -0500169 * Release write lock of i_data_sem of two inodes (orig and donor).
Akira Fujita748de672009-06-17 19:24:03 -0400170 */
171static void
Akira Fujitafc04cb42009-11-23 07:24:43 -0500172double_up_write_data_sem(struct inode *orig_inode, struct inode *donor_inode)
Akira Fujita748de672009-06-17 19:24:03 -0400173{
Akira Fujita748de672009-06-17 19:24:03 -0400174 up_write(&EXT4_I(orig_inode)->i_data_sem);
175 up_write(&EXT4_I(donor_inode)->i_data_sem);
176}
177
178/**
179 * mext_insert_across_blocks - Insert extents across leaf block
180 *
181 * @handle: journal handle
182 * @orig_inode: original inode
183 * @o_start: first original extent to be changed
184 * @o_end: last original extent to be changed
185 * @start_ext: first new extent to be inserted
186 * @new_ext: middle of new extent to be inserted
187 * @end_ext: last new extent to be inserted
188 *
189 * Allocate a new leaf block and insert extents into it. Return 0 on success,
190 * or a negative error value on failure.
191 */
192static int
193mext_insert_across_blocks(handle_t *handle, struct inode *orig_inode,
194 struct ext4_extent *o_start, struct ext4_extent *o_end,
195 struct ext4_extent *start_ext, struct ext4_extent *new_ext,
196 struct ext4_extent *end_ext)
197{
198 struct ext4_ext_path *orig_path = NULL;
199 ext4_lblk_t eblock = 0;
200 int new_flag = 0;
201 int end_flag = 0;
202 int err = 0;
203
204 if (start_ext->ee_len && new_ext->ee_len && end_ext->ee_len) {
205 if (o_start == o_end) {
206
207 /* start_ext new_ext end_ext
208 * donor |---------|-----------|--------|
209 * orig |------------------------------|
210 */
211 end_flag = 1;
212 } else {
213
214 /* start_ext new_ext end_ext
215 * donor |---------|----------|---------|
216 * orig |---------------|--------------|
217 */
218 o_end->ee_block = end_ext->ee_block;
219 o_end->ee_len = end_ext->ee_len;
Theodore Ts'obf89d162010-10-27 21:30:14 -0400220 ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
Akira Fujita748de672009-06-17 19:24:03 -0400221 }
222
223 o_start->ee_len = start_ext->ee_len;
Akira Fujita5fd52492010-03-04 00:31:06 -0500224 eblock = le32_to_cpu(start_ext->ee_block);
Akira Fujita748de672009-06-17 19:24:03 -0400225 new_flag = 1;
226
227 } else if (start_ext->ee_len && new_ext->ee_len &&
228 !end_ext->ee_len && o_start == o_end) {
229
230 /* start_ext new_ext
231 * donor |--------------|---------------|
232 * orig |------------------------------|
233 */
234 o_start->ee_len = start_ext->ee_len;
Akira Fujita5fd52492010-03-04 00:31:06 -0500235 eblock = le32_to_cpu(start_ext->ee_block);
Akira Fujita748de672009-06-17 19:24:03 -0400236 new_flag = 1;
237
238 } else if (!start_ext->ee_len && new_ext->ee_len &&
239 end_ext->ee_len && o_start == o_end) {
240
241 /* new_ext end_ext
242 * donor |--------------|---------------|
243 * orig |------------------------------|
244 */
245 o_end->ee_block = end_ext->ee_block;
246 o_end->ee_len = end_ext->ee_len;
Theodore Ts'obf89d162010-10-27 21:30:14 -0400247 ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
Akira Fujita748de672009-06-17 19:24:03 -0400248
249 /*
250 * Set 0 to the extent block if new_ext was
251 * the first block.
252 */
253 if (new_ext->ee_block)
254 eblock = le32_to_cpu(new_ext->ee_block);
255
256 new_flag = 1;
257 } else {
258 ext4_debug("ext4 move extent: Unexpected insert case\n");
259 return -EIO;
260 }
261
262 if (new_flag) {
Akira Fujitae8505972009-09-16 13:46:38 -0400263 err = get_ext_path(orig_inode, eblock, &orig_path);
Akira Fujita347fa6f2009-09-16 14:25:07 -0400264 if (err)
Akira Fujita748de672009-06-17 19:24:03 -0400265 goto out;
266
267 if (ext4_ext_insert_extent(handle, orig_inode,
Mingming Cao00314622009-09-28 15:49:08 -0400268 orig_path, new_ext, 0))
Akira Fujita748de672009-06-17 19:24:03 -0400269 goto out;
270 }
271
272 if (end_flag) {
Akira Fujitae8505972009-09-16 13:46:38 -0400273 err = get_ext_path(orig_inode,
274 le32_to_cpu(end_ext->ee_block) - 1, &orig_path);
Akira Fujita347fa6f2009-09-16 14:25:07 -0400275 if (err)
Akira Fujita748de672009-06-17 19:24:03 -0400276 goto out;
277
278 if (ext4_ext_insert_extent(handle, orig_inode,
Mingming Cao00314622009-09-28 15:49:08 -0400279 orig_path, end_ext, 0))
Akira Fujita748de672009-06-17 19:24:03 -0400280 goto out;
281 }
282out:
283 if (orig_path) {
284 ext4_ext_drop_refs(orig_path);
285 kfree(orig_path);
286 }
287
288 return err;
289
290}
291
292/**
293 * mext_insert_inside_block - Insert new extent to the extent block
294 *
295 * @o_start: first original extent to be moved
296 * @o_end: last original extent to be moved
297 * @start_ext: first new extent to be inserted
298 * @new_ext: middle of new extent to be inserted
299 * @end_ext: last new extent to be inserted
300 * @eh: extent header of target leaf block
301 * @range_to_move: used to decide how to insert extent
302 *
303 * Insert extents into the leaf block. The extent (@o_start) is overwritten
304 * by inserted extents.
305 */
306static void
307mext_insert_inside_block(struct ext4_extent *o_start,
308 struct ext4_extent *o_end,
309 struct ext4_extent *start_ext,
310 struct ext4_extent *new_ext,
311 struct ext4_extent *end_ext,
312 struct ext4_extent_header *eh,
313 int range_to_move)
314{
315 int i = 0;
316 unsigned long len;
317
318 /* Move the existing extents */
319 if (range_to_move && o_end < EXT_LAST_EXTENT(eh)) {
320 len = (unsigned long)(EXT_LAST_EXTENT(eh) + 1) -
321 (unsigned long)(o_end + 1);
322 memmove(o_end + 1 + range_to_move, o_end + 1, len);
323 }
324
325 /* Insert start entry */
326 if (start_ext->ee_len)
327 o_start[i++].ee_len = start_ext->ee_len;
328
329 /* Insert new entry */
330 if (new_ext->ee_len) {
331 o_start[i] = *new_ext;
Theodore Ts'obf89d162010-10-27 21:30:14 -0400332 ext4_ext_store_pblock(&o_start[i++], ext4_ext_pblock(new_ext));
Akira Fujita748de672009-06-17 19:24:03 -0400333 }
334
335 /* Insert end entry */
336 if (end_ext->ee_len)
337 o_start[i] = *end_ext;
338
339 /* Increment the total entries counter on the extent block */
340 le16_add_cpu(&eh->eh_entries, range_to_move);
341}
342
343/**
344 * mext_insert_extents - Insert new extent
345 *
346 * @handle: journal handle
347 * @orig_inode: original inode
348 * @orig_path: path indicates first extent to be changed
349 * @o_start: first original extent to be changed
350 * @o_end: last original extent to be changed
351 * @start_ext: first new extent to be inserted
352 * @new_ext: middle of new extent to be inserted
353 * @end_ext: last new extent to be inserted
354 *
355 * Call the function to insert extents. If we cannot add more extents into
356 * the leaf block, we call mext_insert_across_blocks() to create a
357 * new leaf block. Otherwise call mext_insert_inside_block(). Return 0
358 * on success, or a negative error value on failure.
359 */
360static int
361mext_insert_extents(handle_t *handle, struct inode *orig_inode,
362 struct ext4_ext_path *orig_path,
363 struct ext4_extent *o_start,
364 struct ext4_extent *o_end,
365 struct ext4_extent *start_ext,
366 struct ext4_extent *new_ext,
367 struct ext4_extent *end_ext)
368{
369 struct ext4_extent_header *eh;
370 unsigned long need_slots, slots_range;
371 int range_to_move, depth, ret;
372
373 /*
374 * The extents need to be inserted
375 * start_extent + new_extent + end_extent.
376 */
377 need_slots = (start_ext->ee_len ? 1 : 0) + (end_ext->ee_len ? 1 : 0) +
378 (new_ext->ee_len ? 1 : 0);
379
380 /* The number of slots between start and end */
381 slots_range = ((unsigned long)(o_end + 1) - (unsigned long)o_start + 1)
382 / sizeof(struct ext4_extent);
383
384 /* Range to move the end of extent */
385 range_to_move = need_slots - slots_range;
386 depth = orig_path->p_depth;
387 orig_path += depth;
388 eh = orig_path->p_hdr;
389
390 if (depth) {
391 /* Register to journal */
392 ret = ext4_journal_get_write_access(handle, orig_path->p_bh);
393 if (ret)
394 return ret;
395 }
396
397 /* Expansion */
398 if (range_to_move > 0 &&
399 (range_to_move > le16_to_cpu(eh->eh_max)
400 - le16_to_cpu(eh->eh_entries))) {
401
402 ret = mext_insert_across_blocks(handle, orig_inode, o_start,
403 o_end, start_ext, new_ext, end_ext);
404 if (ret < 0)
405 return ret;
406 } else
407 mext_insert_inside_block(o_start, o_end, start_ext, new_ext,
408 end_ext, eh, range_to_move);
409
410 if (depth) {
411 ret = ext4_handle_dirty_metadata(handle, orig_inode,
412 orig_path->p_bh);
413 if (ret)
414 return ret;
415 } else {
416 ret = ext4_mark_inode_dirty(handle, orig_inode);
417 if (ret < 0)
418 return ret;
419 }
420
421 return 0;
422}
423
424/**
425 * mext_leaf_block - Move one leaf extent block into the inode.
426 *
427 * @handle: journal handle
428 * @orig_inode: original inode
429 * @orig_path: path indicates first extent to be changed
430 * @dext: donor extent
431 * @from: start offset on the target file
432 *
433 * In order to insert extents into the leaf block, we must divide the extent
434 * in the leaf block into three extents. The one is located to be inserted
435 * extents, and the others are located around it.
436 *
437 * Therefore, this function creates structures to save extents of the leaf
438 * block, and inserts extents by calling mext_insert_extents() with
439 * created extents. Return 0 on success, or a negative error value on failure.
440 */
441static int
442mext_leaf_block(handle_t *handle, struct inode *orig_inode,
443 struct ext4_ext_path *orig_path, struct ext4_extent *dext,
444 ext4_lblk_t *from)
445{
446 struct ext4_extent *oext, *o_start, *o_end, *prev_ext;
447 struct ext4_extent new_ext, start_ext, end_ext;
448 ext4_lblk_t new_ext_end;
Akira Fujita748de672009-06-17 19:24:03 -0400449 int oext_alen, new_ext_alen, end_ext_alen;
450 int depth = ext_depth(orig_inode);
451 int ret;
452
Steven Liuc26d0ba2010-05-11 00:00:00 -0400453 start_ext.ee_block = end_ext.ee_block = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400454 o_start = o_end = oext = orig_path[depth].p_ext;
455 oext_alen = ext4_ext_get_actual_len(oext);
456 start_ext.ee_len = end_ext.ee_len = 0;
457
458 new_ext.ee_block = cpu_to_le32(*from);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400459 ext4_ext_store_pblock(&new_ext, ext4_ext_pblock(dext));
Akira Fujita748de672009-06-17 19:24:03 -0400460 new_ext.ee_len = dext->ee_len;
461 new_ext_alen = ext4_ext_get_actual_len(&new_ext);
462 new_ext_end = le32_to_cpu(new_ext.ee_block) + new_ext_alen - 1;
Akira Fujita748de672009-06-17 19:24:03 -0400463
464 /*
465 * Case: original extent is first
466 * oext |--------|
467 * new_ext |--|
468 * start_ext |--|
469 */
470 if (le32_to_cpu(oext->ee_block) < le32_to_cpu(new_ext.ee_block) &&
471 le32_to_cpu(new_ext.ee_block) <
472 le32_to_cpu(oext->ee_block) + oext_alen) {
473 start_ext.ee_len = cpu_to_le16(le32_to_cpu(new_ext.ee_block) -
474 le32_to_cpu(oext->ee_block));
Akira Fujita5fd52492010-03-04 00:31:06 -0500475 start_ext.ee_block = oext->ee_block;
Akira Fujita748de672009-06-17 19:24:03 -0400476 copy_extent_status(oext, &start_ext);
477 } else if (oext > EXT_FIRST_EXTENT(orig_path[depth].p_hdr)) {
478 prev_ext = oext - 1;
479 /*
480 * We can merge new_ext into previous extent,
481 * if these are contiguous and same extent type.
482 */
483 if (ext4_can_extents_be_merged(orig_inode, prev_ext,
484 &new_ext)) {
485 o_start = prev_ext;
486 start_ext.ee_len = cpu_to_le16(
487 ext4_ext_get_actual_len(prev_ext) +
488 new_ext_alen);
Akira Fujita5fd52492010-03-04 00:31:06 -0500489 start_ext.ee_block = oext->ee_block;
Akira Fujita748de672009-06-17 19:24:03 -0400490 copy_extent_status(prev_ext, &start_ext);
491 new_ext.ee_len = 0;
492 }
493 }
494
495 /*
496 * Case: new_ext_end must be less than oext
497 * oext |-----------|
498 * new_ext |-------|
499 */
Akira Fujita2147b1a2009-09-16 13:46:35 -0400500 if (le32_to_cpu(oext->ee_block) + oext_alen - 1 < new_ext_end) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400501 EXT4_ERROR_INODE(orig_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -0400502 "new_ext_end(%u) should be less than or equal to "
503 "oext->ee_block(%u) + oext_alen(%d) - 1",
504 new_ext_end, le32_to_cpu(oext->ee_block),
505 oext_alen);
506 ret = -EIO;
507 goto out;
508 }
Akira Fujita748de672009-06-17 19:24:03 -0400509
510 /*
511 * Case: new_ext is smaller than original extent
512 * oext |---------------|
513 * new_ext |-----------|
514 * end_ext |---|
515 */
516 if (le32_to_cpu(oext->ee_block) <= new_ext_end &&
517 new_ext_end < le32_to_cpu(oext->ee_block) + oext_alen - 1) {
518 end_ext.ee_len =
519 cpu_to_le16(le32_to_cpu(oext->ee_block) +
520 oext_alen - 1 - new_ext_end);
521 copy_extent_status(oext, &end_ext);
522 end_ext_alen = ext4_ext_get_actual_len(&end_ext);
523 ext4_ext_store_pblock(&end_ext,
Theodore Ts'obf89d162010-10-27 21:30:14 -0400524 (ext4_ext_pblock(o_end) + oext_alen - end_ext_alen));
Akira Fujita748de672009-06-17 19:24:03 -0400525 end_ext.ee_block =
526 cpu_to_le32(le32_to_cpu(o_end->ee_block) +
527 oext_alen - end_ext_alen);
528 }
529
530 ret = mext_insert_extents(handle, orig_inode, orig_path, o_start,
531 o_end, &start_ext, &new_ext, &end_ext);
Akira Fujita2147b1a2009-09-16 13:46:35 -0400532out:
Akira Fujita748de672009-06-17 19:24:03 -0400533 return ret;
534}
535
536/**
537 * mext_calc_swap_extents - Calculate extents for extent swapping.
538 *
539 * @tmp_dext: the extent that will belong to the original inode
540 * @tmp_oext: the extent that will belong to the donor inode
541 * @orig_off: block offset of original inode
542 * @donor_off: block offset of donor inode
Akira Fujita92c28152009-11-23 07:24:50 -0500543 * @max_count: the maximum length of extents
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400544 *
545 * Return 0 on success, or a negative error value on failure.
Akira Fujita748de672009-06-17 19:24:03 -0400546 */
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400547static int
Akira Fujita748de672009-06-17 19:24:03 -0400548mext_calc_swap_extents(struct ext4_extent *tmp_dext,
549 struct ext4_extent *tmp_oext,
550 ext4_lblk_t orig_off, ext4_lblk_t donor_off,
551 ext4_lblk_t max_count)
552{
553 ext4_lblk_t diff, orig_diff;
554 struct ext4_extent dext_old, oext_old;
555
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400556 BUG_ON(orig_off != donor_off);
557
558 /* original and donor extents have to cover the same block offset */
559 if (orig_off < le32_to_cpu(tmp_oext->ee_block) ||
560 le32_to_cpu(tmp_oext->ee_block) +
561 ext4_ext_get_actual_len(tmp_oext) - 1 < orig_off)
562 return -ENODATA;
563
564 if (orig_off < le32_to_cpu(tmp_dext->ee_block) ||
565 le32_to_cpu(tmp_dext->ee_block) +
566 ext4_ext_get_actual_len(tmp_dext) - 1 < orig_off)
567 return -ENODATA;
568
Akira Fujita748de672009-06-17 19:24:03 -0400569 dext_old = *tmp_dext;
570 oext_old = *tmp_oext;
571
572 /* When tmp_dext is too large, pick up the target range. */
573 diff = donor_off - le32_to_cpu(tmp_dext->ee_block);
574
Theodore Ts'obf89d162010-10-27 21:30:14 -0400575 ext4_ext_store_pblock(tmp_dext, ext4_ext_pblock(tmp_dext) + diff);
Wei Yongjunba39ebb2012-09-27 09:37:53 -0400576 le32_add_cpu(&tmp_dext->ee_block, diff);
577 le16_add_cpu(&tmp_dext->ee_len, -diff);
Akira Fujita748de672009-06-17 19:24:03 -0400578
579 if (max_count < ext4_ext_get_actual_len(tmp_dext))
580 tmp_dext->ee_len = cpu_to_le16(max_count);
581
582 orig_diff = orig_off - le32_to_cpu(tmp_oext->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400583 ext4_ext_store_pblock(tmp_oext, ext4_ext_pblock(tmp_oext) + orig_diff);
Akira Fujita748de672009-06-17 19:24:03 -0400584
585 /* Adjust extent length if donor extent is larger than orig */
586 if (ext4_ext_get_actual_len(tmp_dext) >
587 ext4_ext_get_actual_len(tmp_oext) - orig_diff)
588 tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_oext->ee_len) -
589 orig_diff);
590
591 tmp_oext->ee_len = cpu_to_le16(ext4_ext_get_actual_len(tmp_dext));
592
593 copy_extent_status(&oext_old, tmp_dext);
594 copy_extent_status(&dext_old, tmp_oext);
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400595
596 return 0;
Akira Fujita748de672009-06-17 19:24:03 -0400597}
598
599/**
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400600 * mext_check_coverage - Check that all extents in range has the same type
601 *
602 * @inode: inode in question
603 * @from: block offset of inode
604 * @count: block count to be checked
605 * @uninit: extents expected to be uninitialized
606 * @err: pointer to save error value
607 *
608 * Return 1 if all extents in range has expected type, and zero otherwise.
609 */
610static int
611mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
612 int uninit, int *err)
613{
614 struct ext4_ext_path *path = NULL;
615 struct ext4_extent *ext;
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400616 int ret = 0;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400617 ext4_lblk_t last = from + count;
618 while (from < last) {
619 *err = get_ext_path(inode, from, &path);
620 if (*err)
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400621 goto out;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400622 ext = path[ext_depth(inode)].p_ext;
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400623 if (uninit != ext4_ext_is_uninitialized(ext))
624 goto out;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400625 from += ext4_ext_get_actual_len(ext);
626 ext4_ext_drop_refs(path);
627 }
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400628 ret = 1;
629out:
630 if (path) {
631 ext4_ext_drop_refs(path);
632 kfree(path);
633 }
634 return ret;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400635}
636
637/**
Akira Fujita748de672009-06-17 19:24:03 -0400638 * mext_replace_branches - Replace original extents with new extents
639 *
640 * @handle: journal handle
641 * @orig_inode: original inode
642 * @donor_inode: donor inode
643 * @from: block offset of orig_inode
644 * @count: block count to be replaced
Akira Fujitaf868a482009-11-23 07:25:48 -0500645 * @err: pointer to save return value
Akira Fujita748de672009-06-17 19:24:03 -0400646 *
647 * Replace original inode extents and donor inode extents page by page.
648 * We implement this replacement in the following three steps:
649 * 1. Save the block information of original and donor inodes into
650 * dummy extents.
651 * 2. Change the block information of original inode to point at the
652 * donor inode blocks.
653 * 3. Change the block information of donor inode to point at the saved
654 * original inode blocks in the dummy extents.
655 *
Akira Fujitaf868a482009-11-23 07:25:48 -0500656 * Return replaced block count.
Akira Fujita748de672009-06-17 19:24:03 -0400657 */
658static int
659mext_replace_branches(handle_t *handle, struct inode *orig_inode,
660 struct inode *donor_inode, ext4_lblk_t from,
Akira Fujitaf868a482009-11-23 07:25:48 -0500661 ext4_lblk_t count, int *err)
Akira Fujita748de672009-06-17 19:24:03 -0400662{
663 struct ext4_ext_path *orig_path = NULL;
664 struct ext4_ext_path *donor_path = NULL;
665 struct ext4_extent *oext, *dext;
666 struct ext4_extent tmp_dext, tmp_oext;
667 ext4_lblk_t orig_off = from, donor_off = from;
Akira Fujita748de672009-06-17 19:24:03 -0400668 int depth;
669 int replaced_count = 0;
670 int dext_alen;
671
Dmitry Monakhov6ca470d72013-03-04 00:50:47 -0500672 *err = ext4_es_remove_extent(orig_inode, from, count);
673 if (*err)
674 goto out;
675
676 *err = ext4_es_remove_extent(donor_inode, from, count);
677 if (*err)
678 goto out;
679
Akira Fujita748de672009-06-17 19:24:03 -0400680 /* Get the original extent for the block "orig_off" */
Akira Fujitaf868a482009-11-23 07:25:48 -0500681 *err = get_ext_path(orig_inode, orig_off, &orig_path);
682 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400683 goto out;
684
685 /* Get the donor extent for the head */
Akira Fujitaf868a482009-11-23 07:25:48 -0500686 *err = get_ext_path(donor_inode, donor_off, &donor_path);
687 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400688 goto out;
689 depth = ext_depth(orig_inode);
690 oext = orig_path[depth].p_ext;
691 tmp_oext = *oext;
692
693 depth = ext_depth(donor_inode);
694 dext = donor_path[depth].p_ext;
Akria Fujita87e69872013-02-01 20:52:46 -0500695 if (unlikely(!dext))
696 goto missing_donor_extent;
Akira Fujita748de672009-06-17 19:24:03 -0400697 tmp_dext = *dext;
698
Akira Fujitaf868a482009-11-23 07:25:48 -0500699 *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
Akira Fujita748de672009-06-17 19:24:03 -0400700 donor_off, count);
Akira Fujitaf868a482009-11-23 07:25:48 -0500701 if (*err)
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400702 goto out;
Akira Fujita748de672009-06-17 19:24:03 -0400703
704 /* Loop for the donor extents */
705 while (1) {
706 /* The extent for donor must be found. */
Akria Fujita87e69872013-02-01 20:52:46 -0500707 if (unlikely(!dext)) {
708 missing_donor_extent:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400709 EXT4_ERROR_INODE(donor_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -0400710 "The extent for donor must be found");
Akira Fujitaf868a482009-11-23 07:25:48 -0500711 *err = -EIO;
Akira Fujita2147b1a2009-09-16 13:46:35 -0400712 goto out;
713 } else if (donor_off != le32_to_cpu(tmp_dext.ee_block)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400714 EXT4_ERROR_INODE(donor_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -0400715 "Donor offset(%u) and the first block of donor "
716 "extent(%u) should be equal",
717 donor_off,
718 le32_to_cpu(tmp_dext.ee_block));
Akira Fujitaf868a482009-11-23 07:25:48 -0500719 *err = -EIO;
Akira Fujita2147b1a2009-09-16 13:46:35 -0400720 goto out;
721 }
Akira Fujita748de672009-06-17 19:24:03 -0400722
723 /* Set donor extent to orig extent */
Akira Fujitaf868a482009-11-23 07:25:48 -0500724 *err = mext_leaf_block(handle, orig_inode,
Akira Fujita748de672009-06-17 19:24:03 -0400725 orig_path, &tmp_dext, &orig_off);
Akira Fujitaf868a482009-11-23 07:25:48 -0500726 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400727 goto out;
728
729 /* Set orig extent to donor extent */
Akira Fujitaf868a482009-11-23 07:25:48 -0500730 *err = mext_leaf_block(handle, donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -0400731 donor_path, &tmp_oext, &donor_off);
Akira Fujitaf868a482009-11-23 07:25:48 -0500732 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400733 goto out;
734
735 dext_alen = ext4_ext_get_actual_len(&tmp_dext);
736 replaced_count += dext_alen;
737 donor_off += dext_alen;
738 orig_off += dext_alen;
739
740 /* Already moved the expected blocks */
741 if (replaced_count >= count)
742 break;
743
744 if (orig_path)
745 ext4_ext_drop_refs(orig_path);
Akira Fujitaf868a482009-11-23 07:25:48 -0500746 *err = get_ext_path(orig_inode, orig_off, &orig_path);
747 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400748 goto out;
749 depth = ext_depth(orig_inode);
750 oext = orig_path[depth].p_ext;
Akira Fujita748de672009-06-17 19:24:03 -0400751 tmp_oext = *oext;
752
753 if (donor_path)
754 ext4_ext_drop_refs(donor_path);
Akira Fujitaf868a482009-11-23 07:25:48 -0500755 *err = get_ext_path(donor_inode, donor_off, &donor_path);
756 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400757 goto out;
758 depth = ext_depth(donor_inode);
759 dext = donor_path[depth].p_ext;
Akira Fujita748de672009-06-17 19:24:03 -0400760 tmp_dext = *dext;
761
Akira Fujitaf868a482009-11-23 07:25:48 -0500762 *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400763 donor_off, count - replaced_count);
Akira Fujitaf868a482009-11-23 07:25:48 -0500764 if (*err)
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400765 goto out;
Akira Fujita748de672009-06-17 19:24:03 -0400766 }
767
768out:
769 if (orig_path) {
770 ext4_ext_drop_refs(orig_path);
771 kfree(orig_path);
772 }
773 if (donor_path) {
774 ext4_ext_drop_refs(donor_path);
775 kfree(donor_path);
776 }
777
Akira Fujitaf868a482009-11-23 07:25:48 -0500778 return replaced_count;
Akira Fujita748de672009-06-17 19:24:03 -0400779}
780
781/**
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400782 * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
783 *
784 * @inode1: the inode structure
785 * @inode2: the inode structure
786 * @index: page index
787 * @page: result page vector
788 *
789 * Grab two locked pages for inode's by inode order
790 */
791static int
792mext_page_double_lock(struct inode *inode1, struct inode *inode2,
793 pgoff_t index, struct page *page[2])
794{
795 struct address_space *mapping[2];
796 unsigned fl = AOP_FLAG_NOFS;
797
798 BUG_ON(!inode1 || !inode2);
799 if (inode1 < inode2) {
800 mapping[0] = inode1->i_mapping;
801 mapping[1] = inode2->i_mapping;
802 } else {
803 mapping[0] = inode2->i_mapping;
804 mapping[1] = inode1->i_mapping;
805 }
806
807 page[0] = grab_cache_page_write_begin(mapping[0], index, fl);
808 if (!page[0])
809 return -ENOMEM;
810
811 page[1] = grab_cache_page_write_begin(mapping[1], index, fl);
812 if (!page[1]) {
813 unlock_page(page[0]);
814 page_cache_release(page[0]);
815 return -ENOMEM;
816 }
817
818 if (inode1 > inode2) {
819 struct page *tmp;
820 tmp = page[0];
821 page[0] = page[1];
822 page[1] = tmp;
823 }
824 return 0;
825}
826
827/* Force page buffers uptodate w/o dropping page's lock */
828static int
829mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
830{
831 struct inode *inode = page->mapping->host;
832 sector_t block;
833 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
834 unsigned int blocksize, block_start, block_end;
835 int i, err, nr = 0, partial = 0;
836 BUG_ON(!PageLocked(page));
837 BUG_ON(PageWriteback(page));
838
839 if (PageUptodate(page))
840 return 0;
841
842 blocksize = 1 << inode->i_blkbits;
843 if (!page_has_buffers(page))
844 create_empty_buffers(page, blocksize, 0);
845
846 head = page_buffers(page);
847 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
848 for (bh = head, block_start = 0; bh != head || !block_start;
849 block++, block_start = block_end, bh = bh->b_this_page) {
850 block_end = block_start + blocksize;
851 if (block_end <= from || block_start >= to) {
852 if (!buffer_uptodate(bh))
853 partial = 1;
854 continue;
855 }
856 if (buffer_uptodate(bh))
857 continue;
858 if (!buffer_mapped(bh)) {
859 int err = 0;
860 err = ext4_get_block(inode, block, bh, 0);
861 if (err) {
862 SetPageError(page);
863 return err;
864 }
865 if (!buffer_mapped(bh)) {
866 zero_user(page, block_start, blocksize);
867 if (!err)
868 set_buffer_uptodate(bh);
869 continue;
870 }
871 }
872 BUG_ON(nr >= MAX_BUF_PER_PAGE);
873 arr[nr++] = bh;
874 }
875 /* No io required */
876 if (!nr)
877 goto out;
878
879 for (i = 0; i < nr; i++) {
880 bh = arr[i];
881 if (!bh_uptodate_or_lock(bh)) {
882 err = bh_submit_read(bh);
883 if (err)
884 return err;
885 }
886 }
887out:
888 if (!partial)
889 SetPageUptodate(page);
890 return 0;
891}
892
893/**
Akira Fujita748de672009-06-17 19:24:03 -0400894 * move_extent_per_page - Move extent data per page
895 *
896 * @o_filp: file structure of original file
897 * @donor_inode: donor inode
898 * @orig_page_offset: page index on original file
899 * @data_offset_in_page: block index where data swapping starts
900 * @block_len_in_page: the number of blocks to be swapped
901 * @uninit: orig extent is uninitialized or not
Akira Fujitaf868a482009-11-23 07:25:48 -0500902 * @err: pointer to save return value
Akira Fujita748de672009-06-17 19:24:03 -0400903 *
904 * Save the data in original inode blocks and replace original inode extents
905 * with donor inode extents by calling mext_replace_branches().
Akira Fujitaf868a482009-11-23 07:25:48 -0500906 * Finally, write out the saved data in new original inode blocks. Return
907 * replaced block count.
Akira Fujita748de672009-06-17 19:24:03 -0400908 */
909static int
Akira Fujita44fc48f2009-09-05 23:12:41 -0400910move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -0400911 pgoff_t orig_page_offset, int data_offset_in_page,
Akira Fujitaf868a482009-11-23 07:25:48 -0500912 int block_len_in_page, int uninit, int *err)
Akira Fujita748de672009-06-17 19:24:03 -0400913{
Al Viro496ad9a2013-01-23 17:07:38 -0500914 struct inode *orig_inode = file_inode(o_filp);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400915 struct page *pagep[2] = {NULL, NULL};
Akira Fujita748de672009-06-17 19:24:03 -0400916 handle_t *handle;
917 ext4_lblk_t orig_blk_offset;
918 long long offs = orig_page_offset << PAGE_CACHE_SHIFT;
919 unsigned long blocksize = orig_inode->i_sb->s_blocksize;
920 unsigned int w_flags = 0;
Akira Fujitaf868a482009-11-23 07:25:48 -0500921 unsigned int tmp_data_size, data_size, replaced_size;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400922 int err2, jblocks, retries = 0;
Akira Fujitaf868a482009-11-23 07:25:48 -0500923 int replaced_count = 0;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400924 int from = data_offset_in_page << orig_inode->i_blkbits;
Akira Fujita748de672009-06-17 19:24:03 -0400925 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
926
927 /*
928 * It needs twice the amount of ordinary journal buffers because
929 * inode and donor_inode may change each different metadata blocks.
930 */
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400931again:
932 *err = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400933 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
Theodore Ts'o9924a922013-02-08 21:59:22 -0500934 handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
Akira Fujita748de672009-06-17 19:24:03 -0400935 if (IS_ERR(handle)) {
Akira Fujitaf868a482009-11-23 07:25:48 -0500936 *err = PTR_ERR(handle);
937 return 0;
Akira Fujita748de672009-06-17 19:24:03 -0400938 }
939
940 if (segment_eq(get_fs(), KERNEL_DS))
941 w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
942
943 orig_blk_offset = orig_page_offset * blocks_per_page +
944 data_offset_in_page;
945
Akira Fujita748de672009-06-17 19:24:03 -0400946 offs = (long long)orig_blk_offset << orig_inode->i_blkbits;
947
Akira Fujitaf868a482009-11-23 07:25:48 -0500948 /* Calculate data_size */
Akira Fujita748de672009-06-17 19:24:03 -0400949 if ((orig_blk_offset + block_len_in_page - 1) ==
950 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
951 /* Replace the last block */
Akira Fujitaf868a482009-11-23 07:25:48 -0500952 tmp_data_size = orig_inode->i_size & (blocksize - 1);
Akira Fujita748de672009-06-17 19:24:03 -0400953 /*
Akira Fujitaf868a482009-11-23 07:25:48 -0500954 * If data_size equal zero, it shows data_size is multiples of
Akira Fujita748de672009-06-17 19:24:03 -0400955 * blocksize. So we set appropriate value.
956 */
Akira Fujitaf868a482009-11-23 07:25:48 -0500957 if (tmp_data_size == 0)
958 tmp_data_size = blocksize;
Akira Fujita748de672009-06-17 19:24:03 -0400959
Akira Fujitaf868a482009-11-23 07:25:48 -0500960 data_size = tmp_data_size +
Akira Fujita748de672009-06-17 19:24:03 -0400961 ((block_len_in_page - 1) << orig_inode->i_blkbits);
Akira Fujitaf868a482009-11-23 07:25:48 -0500962 } else
963 data_size = block_len_in_page << orig_inode->i_blkbits;
Akira Fujita748de672009-06-17 19:24:03 -0400964
Akira Fujitaf868a482009-11-23 07:25:48 -0500965 replaced_size = data_size;
966
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400967 *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
968 pagep);
Akira Fujitaf868a482009-11-23 07:25:48 -0500969 if (unlikely(*err < 0))
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400970 goto stop_journal;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400971 /*
972 * If orig extent was uninitialized it can become initialized
973 * at any time after i_data_sem was dropped, in order to
974 * serialize with delalloc we have recheck extent while we
975 * hold page's lock, if it is still the case data copy is not
976 * necessary, just swap data blocks between orig and donor.
977 */
978 if (uninit) {
979 double_down_write_data_sem(orig_inode, donor_inode);
980 /* If any of extents in range became initialized we have to
981 * fallback to data copying */
982 uninit = mext_check_coverage(orig_inode, orig_blk_offset,
983 block_len_in_page, 1, err);
984 if (*err)
985 goto drop_data_sem;
Akira Fujita748de672009-06-17 19:24:03 -0400986
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400987 uninit &= mext_check_coverage(donor_inode, orig_blk_offset,
988 block_len_in_page, 1, err);
989 if (*err)
990 goto drop_data_sem;
991
992 if (!uninit) {
993 double_up_write_data_sem(orig_inode, donor_inode);
994 goto data_copy;
995 }
996 if ((page_has_private(pagep[0]) &&
997 !try_to_release_page(pagep[0], 0)) ||
998 (page_has_private(pagep[1]) &&
999 !try_to_release_page(pagep[1], 0))) {
1000 *err = -EBUSY;
1001 goto drop_data_sem;
1002 }
1003 replaced_count = mext_replace_branches(handle, orig_inode,
1004 donor_inode, orig_blk_offset,
1005 block_len_in_page, err);
1006 drop_data_sem:
1007 double_up_write_data_sem(orig_inode, donor_inode);
1008 goto unlock_pages;
1009 }
1010data_copy:
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001011 *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
1012 if (*err)
1013 goto unlock_pages;
1014
1015 /* At this point all buffers in range are uptodate, old mapping layout
1016 * is no longer required, try to drop it now. */
1017 if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
1018 (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
1019 *err = -EBUSY;
1020 goto unlock_pages;
Akira Fujita748de672009-06-17 19:24:03 -04001021 }
1022
Akira Fujitaf868a482009-11-23 07:25:48 -05001023 replaced_count = mext_replace_branches(handle, orig_inode, donor_inode,
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001024 orig_blk_offset,
1025 block_len_in_page, err);
1026 if (*err) {
Akira Fujitaf868a482009-11-23 07:25:48 -05001027 if (replaced_count) {
1028 block_len_in_page = replaced_count;
1029 replaced_size =
1030 block_len_in_page << orig_inode->i_blkbits;
Akira Fujitaac48b0a2009-11-24 10:31:56 -05001031 } else
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001032 goto unlock_pages;
Akira Fujitaf868a482009-11-23 07:25:48 -05001033 }
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001034 /* Perform all necessary steps similar write_begin()/write_end()
1035 * but keeping in mind that i_size will not change */
1036 *err = __block_write_begin(pagep[0], from, from + replaced_size,
1037 ext4_get_block);
1038 if (!*err)
1039 *err = block_commit_write(pagep[0], from, from + replaced_size);
Akira Fujita748de672009-06-17 19:24:03 -04001040
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001041 if (unlikely(*err < 0))
1042 goto repair_branches;
Akira Fujita748de672009-06-17 19:24:03 -04001043
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001044 /* Even in case of data=writeback it is reasonable to pin
1045 * inode to transaction, to prevent unexpected data loss */
1046 *err = ext4_jbd2_file_inode(handle, orig_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001047
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001048unlock_pages:
1049 unlock_page(pagep[0]);
1050 page_cache_release(pagep[0]);
1051 unlock_page(pagep[1]);
1052 page_cache_release(pagep[1]);
1053stop_journal:
Akira Fujita748de672009-06-17 19:24:03 -04001054 ext4_journal_stop(handle);
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001055 /* Buffer was busy because probably is pinned to journal transaction,
1056 * force transaction commit may help to free it. */
1057 if (*err == -EBUSY && ext4_should_retry_alloc(orig_inode->i_sb,
1058 &retries))
1059 goto again;
Akira Fujitaf868a482009-11-23 07:25:48 -05001060 return replaced_count;
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001061
1062repair_branches:
1063 /*
1064 * This should never ever happen!
1065 * Extents are swapped already, but we are not able to copy data.
1066 * Try to swap extents to it's original places
1067 */
1068 double_down_write_data_sem(orig_inode, donor_inode);
1069 replaced_count = mext_replace_branches(handle, donor_inode, orig_inode,
1070 orig_blk_offset,
1071 block_len_in_page, &err2);
1072 double_up_write_data_sem(orig_inode, donor_inode);
1073 if (replaced_count != block_len_in_page) {
1074 EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
1075 "Unable to copy data block,"
1076 " data will be lost.");
1077 *err = -EIO;
1078 }
1079 replaced_count = 0;
1080 goto unlock_pages;
Akira Fujita748de672009-06-17 19:24:03 -04001081}
1082
1083/**
Akira Fujitac437b272010-03-04 00:39:24 -05001084 * mext_check_arguments - Check whether move extent can be done
Akira Fujita748de672009-06-17 19:24:03 -04001085 *
1086 * @orig_inode: original inode
1087 * @donor_inode: donor inode
1088 * @orig_start: logical start offset in block for orig
1089 * @donor_start: logical start offset in block for donor
1090 * @len: the number of blocks to be moved
Akira Fujita748de672009-06-17 19:24:03 -04001091 *
1092 * Check the arguments of ext4_move_extents() whether the files can be
1093 * exchanged with each other.
1094 * Return 0 on success, or a negative error value on failure.
1095 */
1096static int
1097mext_check_arguments(struct inode *orig_inode,
Kazuya Mio446aaa62009-11-24 10:28:48 -05001098 struct inode *donor_inode, __u64 orig_start,
1099 __u64 donor_start, __u64 *len)
Akira Fujita748de672009-06-17 19:24:03 -04001100{
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001101 ext4_lblk_t orig_blocks, donor_blocks;
1102 unsigned int blkbits = orig_inode->i_blkbits;
1103 unsigned int blocksize = 1 << blkbits;
1104
Akira Fujita4a585792009-12-06 23:38:31 -05001105 if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
1106 ext4_debug("ext4 move extent: suid or sgid is set"
1107 " to donor file [ino:orig %lu, donor %lu]\n",
1108 orig_inode->i_ino, donor_inode->i_ino);
1109 return -EINVAL;
1110 }
1111
Theodore Ts'o1f5a81e2010-06-02 22:04:39 -04001112 if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
1113 return -EPERM;
1114
Akira Fujita748de672009-06-17 19:24:03 -04001115 /* Ext4 move extent does not support swapfile */
1116 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
1117 ext4_debug("ext4 move extent: The argument files should "
1118 "not be swapfile [ino:orig %lu, donor %lu]\n",
1119 orig_inode->i_ino, donor_inode->i_ino);
1120 return -EINVAL;
1121 }
1122
Akira Fujita748de672009-06-17 19:24:03 -04001123 /* Ext4 move extent supports only extent based file */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001124 if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
Akira Fujita748de672009-06-17 19:24:03 -04001125 ext4_debug("ext4 move extent: orig file is not extents "
1126 "based file [ino:orig %lu]\n", orig_inode->i_ino);
1127 return -EOPNOTSUPP;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001128 } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
Akira Fujita748de672009-06-17 19:24:03 -04001129 ext4_debug("ext4 move extent: donor file is not extents "
1130 "based file [ino:donor %lu]\n", donor_inode->i_ino);
1131 return -EOPNOTSUPP;
1132 }
1133
1134 if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
1135 ext4_debug("ext4 move extent: File size is 0 byte\n");
1136 return -EINVAL;
1137 }
1138
1139 /* Start offset should be same */
1140 if (orig_start != donor_start) {
1141 ext4_debug("ext4 move extent: orig and donor's start "
1142 "offset are not same [ino:orig %lu, donor %lu]\n",
1143 orig_inode->i_ino, donor_inode->i_ino);
1144 return -EINVAL;
1145 }
1146
Lukas Czernerf17722f2011-06-06 00:05:17 -04001147 if ((orig_start >= EXT_MAX_BLOCKS) ||
Lukas Czernerf17722f2011-06-06 00:05:17 -04001148 (*len > EXT_MAX_BLOCKS) ||
1149 (orig_start + *len >= EXT_MAX_BLOCKS)) {
Eric Sandeen0a80e982009-09-17 11:55:58 -04001150 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
Lukas Czernerf17722f2011-06-06 00:05:17 -04001151 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
Akira Fujita748de672009-06-17 19:24:03 -04001152 orig_inode->i_ino, donor_inode->i_ino);
1153 return -EINVAL;
1154 }
1155
1156 if (orig_inode->i_size > donor_inode->i_size) {
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001157 donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits;
1158 /* TODO: eliminate this artificial restriction */
1159 if (orig_start >= donor_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001160 ext4_debug("ext4 move extent: orig start offset "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001161 "[%llu] should be less than donor file blocks "
1162 "[%u] [ino:orig %lu, donor %lu]\n",
1163 orig_start, donor_blocks,
Akira Fujita748de672009-06-17 19:24:03 -04001164 orig_inode->i_ino, donor_inode->i_ino);
1165 return -EINVAL;
1166 }
1167
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001168 /* TODO: eliminate this artificial restriction */
1169 if (orig_start + *len > donor_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001170 ext4_debug("ext4 move extent: End offset [%llu] should "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001171 "be less than donor file blocks [%u]."
1172 "So adjust length from %llu to %llu "
Akira Fujita748de672009-06-17 19:24:03 -04001173 "[ino:orig %lu, donor %lu]\n",
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001174 orig_start + *len, donor_blocks,
1175 *len, donor_blocks - orig_start,
Akira Fujita748de672009-06-17 19:24:03 -04001176 orig_inode->i_ino, donor_inode->i_ino);
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001177 *len = donor_blocks - orig_start;
Akira Fujita748de672009-06-17 19:24:03 -04001178 }
1179 } else {
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001180 orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits;
1181 if (orig_start >= orig_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001182 ext4_debug("ext4 move extent: start offset [%llu] "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001183 "should be less than original file blocks "
1184 "[%u] [ino:orig %lu, donor %lu]\n",
1185 orig_start, orig_blocks,
Akira Fujita748de672009-06-17 19:24:03 -04001186 orig_inode->i_ino, donor_inode->i_ino);
1187 return -EINVAL;
1188 }
1189
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001190 if (orig_start + *len > orig_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001191 ext4_debug("ext4 move extent: Adjust length "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001192 "from %llu to %llu. Because it should be "
1193 "less than original file blocks "
Akira Fujita748de672009-06-17 19:24:03 -04001194 "[ino:orig %lu, donor %lu]\n",
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001195 *len, orig_blocks - orig_start,
Akira Fujita748de672009-06-17 19:24:03 -04001196 orig_inode->i_ino, donor_inode->i_ino);
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001197 *len = orig_blocks - orig_start;
Akira Fujita748de672009-06-17 19:24:03 -04001198 }
1199 }
1200
1201 if (!*len) {
Akira Fujita92c28152009-11-23 07:24:50 -05001202 ext4_debug("ext4 move extent: len should not be 0 "
Akira Fujita748de672009-06-17 19:24:03 -04001203 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
1204 donor_inode->i_ino);
1205 return -EINVAL;
1206 }
1207
1208 return 0;
1209}
1210
1211/**
1212 * mext_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
1213 *
1214 * @inode1: the inode structure
1215 * @inode2: the inode structure
1216 *
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001217 * Lock two inodes' i_mutex
Akira Fujita748de672009-06-17 19:24:03 -04001218 */
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001219static void
Akira Fujita748de672009-06-17 19:24:03 -04001220mext_inode_double_lock(struct inode *inode1, struct inode *inode2)
1221{
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001222 BUG_ON(inode1 == inode2);
1223 if (inode1 < inode2) {
Akira Fujita748de672009-06-17 19:24:03 -04001224 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
1225 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
1226 } else {
1227 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
1228 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
1229 }
1230}
1231
1232/**
1233 * mext_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
1234 *
1235 * @inode1: the inode that is released first
1236 * @inode2: the inode that is released second
1237 *
Akira Fujita748de672009-06-17 19:24:03 -04001238 */
1239
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001240static void
Akira Fujita748de672009-06-17 19:24:03 -04001241mext_inode_double_unlock(struct inode *inode1, struct inode *inode2)
1242{
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001243 mutex_unlock(&inode1->i_mutex);
1244 mutex_unlock(&inode2->i_mutex);
Akira Fujita748de672009-06-17 19:24:03 -04001245}
1246
1247/**
1248 * ext4_move_extents - Exchange the specified range of a file
1249 *
1250 * @o_filp: file structure of the original file
1251 * @d_filp: file structure of the donor file
1252 * @orig_start: start offset in block for orig
1253 * @donor_start: start offset in block for donor
1254 * @len: the number of blocks to be moved
1255 * @moved_len: moved block length
1256 *
1257 * This function returns 0 and moved block length is set in moved_len
1258 * if succeed, otherwise returns error value.
1259 *
1260 * Note: ext4_move_extents() proceeds the following order.
1261 * 1:ext4_move_extents() calculates the last block number of moving extent
1262 * function by the start block number (orig_start) and the number of blocks
1263 * to be moved (len) specified as arguments.
1264 * If the {orig, donor}_start points a hole, the extent's start offset
1265 * pointed by ext_cur (current extent), holecheck_path, orig_path are set
1266 * after hole behind.
1267 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
1268 * or the ext_cur exceeds the block_end which is last logical block number.
1269 * 3:To get the length of continues area, call mext_next_extent()
1270 * specified with the ext_cur (initial value is holecheck_path) re-cursive,
1271 * until find un-continuous extent, the start logical block number exceeds
1272 * the block_end or the extent points to the last extent.
1273 * 4:Exchange the original inode data with donor inode data
1274 * from orig_page_offset to seq_end_page.
1275 * The start indexes of data are specified as arguments.
1276 * That of the original inode is orig_page_offset,
1277 * and the donor inode is also orig_page_offset
1278 * (To easily handle blocksize != pagesize case, the offset for the
1279 * donor inode is block unit).
1280 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
1281 * then returns to step 2.
1282 * 6:Release holecheck_path, orig_path and set the len to moved_len
1283 * which shows the number of moved blocks.
1284 * The moved_len is useful for the command to calculate the file offset
1285 * for starting next move extent ioctl.
1286 * 7:Return 0 on success, or a negative error value on failure.
1287 */
1288int
1289ext4_move_extents(struct file *o_filp, struct file *d_filp,
1290 __u64 orig_start, __u64 donor_start, __u64 len,
1291 __u64 *moved_len)
1292{
Al Viro496ad9a2013-01-23 17:07:38 -05001293 struct inode *orig_inode = file_inode(o_filp);
1294 struct inode *donor_inode = file_inode(d_filp);
Akira Fujita748de672009-06-17 19:24:03 -04001295 struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL;
1296 struct ext4_extent *ext_prev, *ext_cur, *ext_dummy;
1297 ext4_lblk_t block_start = orig_start;
1298 ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0;
1299 ext4_lblk_t rest_blocks;
1300 pgoff_t orig_page_offset = 0, seq_end_page;
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001301 int ret, depth, last_extent = 0;
Akira Fujita748de672009-06-17 19:24:03 -04001302 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
1303 int data_offset_in_page;
1304 int block_len_in_page;
1305 int uninit;
1306
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001307 if (orig_inode->i_sb != donor_inode->i_sb) {
1308 ext4_debug("ext4 move extent: The argument files "
1309 "should be in same FS [ino:orig %lu, donor %lu]\n",
1310 orig_inode->i_ino, donor_inode->i_ino);
1311 return -EINVAL;
1312 }
1313
1314 /* orig and donor should be different inodes */
1315 if (orig_inode == donor_inode) {
Theodore Ts'of3ce8062009-09-28 15:58:29 -04001316 ext4_debug("ext4 move extent: The argument files should not "
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001317 "be same inode [ino:orig %lu, donor %lu]\n",
Theodore Ts'of3ce8062009-09-28 15:58:29 -04001318 orig_inode->i_ino, donor_inode->i_ino);
1319 return -EINVAL;
1320 }
1321
Akira Fujita7247c0c2010-03-04 00:34:58 -05001322 /* Regular file check */
1323 if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
1324 ext4_debug("ext4 move extent: The argument files should be "
1325 "regular file [ino:orig %lu, donor %lu]\n",
1326 orig_inode->i_ino, donor_inode->i_ino);
1327 return -EINVAL;
1328 }
Dmitry Monakhovf0660552012-09-26 12:32:54 -04001329 /* TODO: This is non obvious task to swap blocks for inodes with full
1330 jornaling enabled */
1331 if (ext4_should_journal_data(orig_inode) ||
1332 ext4_should_journal_data(donor_inode)) {
1333 return -EINVAL;
1334 }
Akira Fujitafc04cb42009-11-23 07:24:43 -05001335 /* Protect orig and donor inodes against a truncate */
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001336 mext_inode_double_lock(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001337
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04001338 /* Wait for all existing dio workers */
1339 ext4_inode_block_unlocked_dio(orig_inode);
1340 ext4_inode_block_unlocked_dio(donor_inode);
1341 inode_dio_wait(orig_inode);
1342 inode_dio_wait(donor_inode);
1343
Akira Fujitafc04cb42009-11-23 07:24:43 -05001344 /* Protect extent tree against block allocations via delalloc */
1345 double_down_write_data_sem(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001346 /* Check the filesystem environment whether move_extent can be done */
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001347 ret = mext_check_arguments(orig_inode, donor_inode, orig_start,
Kazuya Mio446aaa62009-11-24 10:28:48 -05001348 donor_start, &len);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001349 if (ret)
Akira Fujita347fa6f2009-09-16 14:25:07 -04001350 goto out;
Akira Fujita748de672009-06-17 19:24:03 -04001351
1352 file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits;
1353 block_end = block_start + len - 1;
1354 if (file_end < block_end)
1355 len -= block_end - file_end;
1356
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001357 ret = get_ext_path(orig_inode, block_start, &orig_path);
1358 if (ret)
Akira Fujita347fa6f2009-09-16 14:25:07 -04001359 goto out;
Akira Fujita748de672009-06-17 19:24:03 -04001360
1361 /* Get path structure to check the hole */
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001362 ret = get_ext_path(orig_inode, block_start, &holecheck_path);
1363 if (ret)
Akira Fujita748de672009-06-17 19:24:03 -04001364 goto out;
1365
1366 depth = ext_depth(orig_inode);
1367 ext_cur = holecheck_path[depth].p_ext;
Akira Fujita748de672009-06-17 19:24:03 -04001368
1369 /*
Akira Fujitac40ce3c2009-09-16 14:25:39 -04001370 * Get proper starting location of block replacement if block_start was
1371 * within the hole.
Akira Fujita748de672009-06-17 19:24:03 -04001372 */
1373 if (le32_to_cpu(ext_cur->ee_block) +
1374 ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
Akira Fujitac40ce3c2009-09-16 14:25:39 -04001375 /*
1376 * The hole exists between extents or the tail of
1377 * original file.
1378 */
Akira Fujita748de672009-06-17 19:24:03 -04001379 last_extent = mext_next_extent(orig_inode,
1380 holecheck_path, &ext_cur);
1381 if (last_extent < 0) {
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001382 ret = last_extent;
Akira Fujita748de672009-06-17 19:24:03 -04001383 goto out;
1384 }
1385 last_extent = mext_next_extent(orig_inode, orig_path,
1386 &ext_dummy);
1387 if (last_extent < 0) {
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001388 ret = last_extent;
Akira Fujita748de672009-06-17 19:24:03 -04001389 goto out;
1390 }
Akira Fujitac40ce3c2009-09-16 14:25:39 -04001391 seq_start = le32_to_cpu(ext_cur->ee_block);
1392 } else if (le32_to_cpu(ext_cur->ee_block) > block_start)
1393 /* The hole exists at the beginning of original file. */
1394 seq_start = le32_to_cpu(ext_cur->ee_block);
1395 else
1396 seq_start = block_start;
Akira Fujita748de672009-06-17 19:24:03 -04001397
1398 /* No blocks within the specified range. */
1399 if (le32_to_cpu(ext_cur->ee_block) > block_end) {
1400 ext4_debug("ext4 move extent: The specified range of file "
1401 "may be the hole\n");
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001402 ret = -EINVAL;
Akira Fujita748de672009-06-17 19:24:03 -04001403 goto out;
1404 }
1405
1406 /* Adjust start blocks */
1407 add_blocks = min(le32_to_cpu(ext_cur->ee_block) +
1408 ext4_ext_get_actual_len(ext_cur), block_end + 1) -
1409 max(le32_to_cpu(ext_cur->ee_block), block_start);
1410
1411 while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
1412 seq_blocks += add_blocks;
1413
1414 /* Adjust tail blocks */
1415 if (seq_start + seq_blocks - 1 > block_end)
1416 seq_blocks = block_end - seq_start + 1;
1417
1418 ext_prev = ext_cur;
1419 last_extent = mext_next_extent(orig_inode, holecheck_path,
1420 &ext_cur);
1421 if (last_extent < 0) {
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001422 ret = last_extent;
Akira Fujita748de672009-06-17 19:24:03 -04001423 break;
1424 }
1425 add_blocks = ext4_ext_get_actual_len(ext_cur);
1426
1427 /*
1428 * Extend the length of contiguous block (seq_blocks)
1429 * if extents are contiguous.
1430 */
1431 if (ext4_can_extents_be_merged(orig_inode,
1432 ext_prev, ext_cur) &&
1433 block_end >= le32_to_cpu(ext_cur->ee_block) &&
1434 !last_extent)
1435 continue;
1436
1437 /* Is original extent is uninitialized */
1438 uninit = ext4_ext_is_uninitialized(ext_prev);
1439
1440 data_offset_in_page = seq_start % blocks_per_page;
1441
1442 /*
1443 * Calculate data blocks count that should be swapped
1444 * at the first page.
1445 */
1446 if (data_offset_in_page + seq_blocks > blocks_per_page) {
1447 /* Swapped blocks are across pages */
1448 block_len_in_page =
1449 blocks_per_page - data_offset_in_page;
1450 } else {
1451 /* Swapped blocks are in a page */
1452 block_len_in_page = seq_blocks;
1453 }
1454
1455 orig_page_offset = seq_start >>
1456 (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1457 seq_end_page = (seq_start + seq_blocks - 1) >>
1458 (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1459 seq_start = le32_to_cpu(ext_cur->ee_block);
1460 rest_blocks = seq_blocks;
1461
Akira Fujitafc04cb42009-11-23 07:24:43 -05001462 /*
1463 * Up semaphore to avoid following problems:
1464 * a. transaction deadlock among ext4_journal_start,
1465 * ->write_begin via pagefault, and jbd2_journal_commit
1466 * b. racing with ->readpage, ->write_begin, and ext4_get_block
1467 * in move_extent_per_page
1468 */
1469 double_up_write_data_sem(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001470
1471 while (orig_page_offset <= seq_end_page) {
1472
1473 /* Swap original branches with new branches */
Akira Fujitaf868a482009-11-23 07:25:48 -05001474 block_len_in_page = move_extent_per_page(
1475 o_filp, donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -04001476 orig_page_offset,
1477 data_offset_in_page,
Akira Fujitaf868a482009-11-23 07:25:48 -05001478 block_len_in_page, uninit,
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001479 &ret);
Akira Fujitaf868a482009-11-23 07:25:48 -05001480
Akira Fujita748de672009-06-17 19:24:03 -04001481 /* Count how many blocks we have exchanged */
1482 *moved_len += block_len_in_page;
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001483 if (ret < 0)
Akira Fujitafc04cb42009-11-23 07:24:43 -05001484 break;
Akira Fujita2147b1a2009-09-16 13:46:35 -04001485 if (*moved_len > len) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001486 EXT4_ERROR_INODE(orig_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -04001487 "We replaced blocks too much! "
1488 "sum of replaced: %llu requested: %llu",
1489 *moved_len, len);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001490 ret = -EIO;
Akira Fujitafc04cb42009-11-23 07:24:43 -05001491 break;
Akira Fujita2147b1a2009-09-16 13:46:35 -04001492 }
Akira Fujita748de672009-06-17 19:24:03 -04001493
Akira Fujitaf868a482009-11-23 07:25:48 -05001494 orig_page_offset++;
Akira Fujita748de672009-06-17 19:24:03 -04001495 data_offset_in_page = 0;
1496 rest_blocks -= block_len_in_page;
1497 if (rest_blocks > blocks_per_page)
1498 block_len_in_page = blocks_per_page;
1499 else
1500 block_len_in_page = rest_blocks;
1501 }
1502
Akira Fujitafc04cb42009-11-23 07:24:43 -05001503 double_down_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001504 if (ret < 0)
Akira Fujitafc04cb42009-11-23 07:24:43 -05001505 break;
1506
Akira Fujita748de672009-06-17 19:24:03 -04001507 /* Decrease buffer counter */
1508 if (holecheck_path)
1509 ext4_ext_drop_refs(holecheck_path);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001510 ret = get_ext_path(orig_inode, seq_start, &holecheck_path);
1511 if (ret)
Akira Fujita748de672009-06-17 19:24:03 -04001512 break;
1513 depth = holecheck_path->p_depth;
1514
1515 /* Decrease buffer counter */
1516 if (orig_path)
1517 ext4_ext_drop_refs(orig_path);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001518 ret = get_ext_path(orig_inode, seq_start, &orig_path);
1519 if (ret)
Akira Fujita748de672009-06-17 19:24:03 -04001520 break;
1521
1522 ext_cur = holecheck_path[depth].p_ext;
1523 add_blocks = ext4_ext_get_actual_len(ext_cur);
1524 seq_blocks = 0;
1525
1526 }
1527out:
Akira Fujita94d7c162009-11-24 10:19:57 -05001528 if (*moved_len) {
1529 ext4_discard_preallocations(orig_inode);
1530 ext4_discard_preallocations(donor_inode);
1531 }
1532
Akira Fujita748de672009-06-17 19:24:03 -04001533 if (orig_path) {
1534 ext4_ext_drop_refs(orig_path);
1535 kfree(orig_path);
1536 }
1537 if (holecheck_path) {
1538 ext4_ext_drop_refs(holecheck_path);
1539 kfree(holecheck_path);
1540 }
Akira Fujitafc04cb42009-11-23 07:24:43 -05001541 double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04001542 ext4_inode_resume_unlocked_dio(orig_inode);
1543 ext4_inode_resume_unlocked_dio(donor_inode);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001544 mext_inode_double_unlock(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001545
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001546 return ret;
Akira Fujita748de672009-06-17 19:24:03 -04001547}