blob: 309ca899a7310f9b92402eedce6609867b18cb7b [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/**
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400147 * ext4_double_down_write_data_sem - Acquire two inodes' write lock
148 * of i_data_sem
Akira Fujita748de672009-06-17 19:24:03 -0400149 *
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400150 * Acquire write lock of i_data_sem of the two inodes
Akira Fujita748de672009-06-17 19:24:03 -0400151 */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400152void
153ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
Akira Fujita748de672009-06-17 19:24:03 -0400154{
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400155 if (first < second) {
156 down_write(&EXT4_I(first)->i_data_sem);
157 down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING);
158 } else {
159 down_write(&EXT4_I(second)->i_data_sem);
160 down_write_nested(&EXT4_I(first)->i_data_sem, SINGLE_DEPTH_NESTING);
Akira Fujita748de672009-06-17 19:24:03 -0400161
Akira Fujita748de672009-06-17 19:24:03 -0400162 }
Akira Fujita748de672009-06-17 19:24:03 -0400163}
164
165/**
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400166 * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
Akira Fujita748de672009-06-17 19:24:03 -0400167 *
168 * @orig_inode: original inode structure to be released its lock first
169 * @donor_inode: donor inode structure to be released its lock second
Akira Fujitafc04cb42009-11-23 07:24:43 -0500170 * Release write lock of i_data_sem of two inodes (orig and donor).
Akira Fujita748de672009-06-17 19:24:03 -0400171 */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400172void
173ext4_double_up_write_data_sem(struct inode *orig_inode,
174 struct inode *donor_inode)
Akira Fujita748de672009-06-17 19:24:03 -0400175{
Akira Fujita748de672009-06-17 19:24:03 -0400176 up_write(&EXT4_I(orig_inode)->i_data_sem);
177 up_write(&EXT4_I(donor_inode)->i_data_sem);
178}
179
180/**
181 * mext_insert_across_blocks - Insert extents across leaf block
182 *
183 * @handle: journal handle
184 * @orig_inode: original inode
185 * @o_start: first original extent to be changed
186 * @o_end: last original extent to be changed
187 * @start_ext: first new extent to be inserted
188 * @new_ext: middle of new extent to be inserted
189 * @end_ext: last new extent to be inserted
190 *
191 * Allocate a new leaf block and insert extents into it. Return 0 on success,
192 * or a negative error value on failure.
193 */
194static int
195mext_insert_across_blocks(handle_t *handle, struct inode *orig_inode,
196 struct ext4_extent *o_start, struct ext4_extent *o_end,
197 struct ext4_extent *start_ext, struct ext4_extent *new_ext,
198 struct ext4_extent *end_ext)
199{
200 struct ext4_ext_path *orig_path = NULL;
201 ext4_lblk_t eblock = 0;
202 int new_flag = 0;
203 int end_flag = 0;
204 int err = 0;
205
206 if (start_ext->ee_len && new_ext->ee_len && end_ext->ee_len) {
207 if (o_start == o_end) {
208
209 /* start_ext new_ext end_ext
210 * donor |---------|-----------|--------|
211 * orig |------------------------------|
212 */
213 end_flag = 1;
214 } else {
215
216 /* start_ext new_ext end_ext
217 * donor |---------|----------|---------|
218 * orig |---------------|--------------|
219 */
220 o_end->ee_block = end_ext->ee_block;
221 o_end->ee_len = end_ext->ee_len;
Theodore Ts'obf89d162010-10-27 21:30:14 -0400222 ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
Akira Fujita748de672009-06-17 19:24:03 -0400223 }
224
225 o_start->ee_len = start_ext->ee_len;
Akira Fujita5fd52492010-03-04 00:31:06 -0500226 eblock = le32_to_cpu(start_ext->ee_block);
Akira Fujita748de672009-06-17 19:24:03 -0400227 new_flag = 1;
228
229 } else if (start_ext->ee_len && new_ext->ee_len &&
230 !end_ext->ee_len && o_start == o_end) {
231
232 /* start_ext new_ext
233 * donor |--------------|---------------|
234 * orig |------------------------------|
235 */
236 o_start->ee_len = start_ext->ee_len;
Akira Fujita5fd52492010-03-04 00:31:06 -0500237 eblock = le32_to_cpu(start_ext->ee_block);
Akira Fujita748de672009-06-17 19:24:03 -0400238 new_flag = 1;
239
240 } else if (!start_ext->ee_len && new_ext->ee_len &&
241 end_ext->ee_len && o_start == o_end) {
242
243 /* new_ext end_ext
244 * donor |--------------|---------------|
245 * orig |------------------------------|
246 */
247 o_end->ee_block = end_ext->ee_block;
248 o_end->ee_len = end_ext->ee_len;
Theodore Ts'obf89d162010-10-27 21:30:14 -0400249 ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
Akira Fujita748de672009-06-17 19:24:03 -0400250
251 /*
252 * Set 0 to the extent block if new_ext was
253 * the first block.
254 */
255 if (new_ext->ee_block)
256 eblock = le32_to_cpu(new_ext->ee_block);
257
258 new_flag = 1;
259 } else {
260 ext4_debug("ext4 move extent: Unexpected insert case\n");
261 return -EIO;
262 }
263
264 if (new_flag) {
Akira Fujitae8505972009-09-16 13:46:38 -0400265 err = get_ext_path(orig_inode, eblock, &orig_path);
Akira Fujita347fa6f2009-09-16 14:25:07 -0400266 if (err)
Akira Fujita748de672009-06-17 19:24:03 -0400267 goto out;
268
269 if (ext4_ext_insert_extent(handle, orig_inode,
Mingming Cao00314622009-09-28 15:49:08 -0400270 orig_path, new_ext, 0))
Akira Fujita748de672009-06-17 19:24:03 -0400271 goto out;
272 }
273
274 if (end_flag) {
Akira Fujitae8505972009-09-16 13:46:38 -0400275 err = get_ext_path(orig_inode,
276 le32_to_cpu(end_ext->ee_block) - 1, &orig_path);
Akira Fujita347fa6f2009-09-16 14:25:07 -0400277 if (err)
Akira Fujita748de672009-06-17 19:24:03 -0400278 goto out;
279
280 if (ext4_ext_insert_extent(handle, orig_inode,
Mingming Cao00314622009-09-28 15:49:08 -0400281 orig_path, end_ext, 0))
Akira Fujita748de672009-06-17 19:24:03 -0400282 goto out;
283 }
284out:
285 if (orig_path) {
286 ext4_ext_drop_refs(orig_path);
287 kfree(orig_path);
288 }
289
290 return err;
291
292}
293
294/**
295 * mext_insert_inside_block - Insert new extent to the extent block
296 *
297 * @o_start: first original extent to be moved
298 * @o_end: last original extent to be moved
299 * @start_ext: first new extent to be inserted
300 * @new_ext: middle of new extent to be inserted
301 * @end_ext: last new extent to be inserted
302 * @eh: extent header of target leaf block
303 * @range_to_move: used to decide how to insert extent
304 *
305 * Insert extents into the leaf block. The extent (@o_start) is overwritten
306 * by inserted extents.
307 */
308static void
309mext_insert_inside_block(struct ext4_extent *o_start,
310 struct ext4_extent *o_end,
311 struct ext4_extent *start_ext,
312 struct ext4_extent *new_ext,
313 struct ext4_extent *end_ext,
314 struct ext4_extent_header *eh,
315 int range_to_move)
316{
317 int i = 0;
318 unsigned long len;
319
320 /* Move the existing extents */
321 if (range_to_move && o_end < EXT_LAST_EXTENT(eh)) {
322 len = (unsigned long)(EXT_LAST_EXTENT(eh) + 1) -
323 (unsigned long)(o_end + 1);
324 memmove(o_end + 1 + range_to_move, o_end + 1, len);
325 }
326
327 /* Insert start entry */
328 if (start_ext->ee_len)
329 o_start[i++].ee_len = start_ext->ee_len;
330
331 /* Insert new entry */
332 if (new_ext->ee_len) {
333 o_start[i] = *new_ext;
Theodore Ts'obf89d162010-10-27 21:30:14 -0400334 ext4_ext_store_pblock(&o_start[i++], ext4_ext_pblock(new_ext));
Akira Fujita748de672009-06-17 19:24:03 -0400335 }
336
337 /* Insert end entry */
338 if (end_ext->ee_len)
339 o_start[i] = *end_ext;
340
341 /* Increment the total entries counter on the extent block */
342 le16_add_cpu(&eh->eh_entries, range_to_move);
343}
344
345/**
346 * mext_insert_extents - Insert new extent
347 *
348 * @handle: journal handle
349 * @orig_inode: original inode
350 * @orig_path: path indicates first extent to be changed
351 * @o_start: first original extent to be changed
352 * @o_end: last original extent to be changed
353 * @start_ext: first new extent to be inserted
354 * @new_ext: middle of new extent to be inserted
355 * @end_ext: last new extent to be inserted
356 *
357 * Call the function to insert extents. If we cannot add more extents into
358 * the leaf block, we call mext_insert_across_blocks() to create a
359 * new leaf block. Otherwise call mext_insert_inside_block(). Return 0
360 * on success, or a negative error value on failure.
361 */
362static int
363mext_insert_extents(handle_t *handle, struct inode *orig_inode,
364 struct ext4_ext_path *orig_path,
365 struct ext4_extent *o_start,
366 struct ext4_extent *o_end,
367 struct ext4_extent *start_ext,
368 struct ext4_extent *new_ext,
369 struct ext4_extent *end_ext)
370{
371 struct ext4_extent_header *eh;
372 unsigned long need_slots, slots_range;
373 int range_to_move, depth, ret;
374
375 /*
376 * The extents need to be inserted
377 * start_extent + new_extent + end_extent.
378 */
379 need_slots = (start_ext->ee_len ? 1 : 0) + (end_ext->ee_len ? 1 : 0) +
380 (new_ext->ee_len ? 1 : 0);
381
382 /* The number of slots between start and end */
383 slots_range = ((unsigned long)(o_end + 1) - (unsigned long)o_start + 1)
384 / sizeof(struct ext4_extent);
385
386 /* Range to move the end of extent */
387 range_to_move = need_slots - slots_range;
388 depth = orig_path->p_depth;
389 orig_path += depth;
390 eh = orig_path->p_hdr;
391
392 if (depth) {
393 /* Register to journal */
394 ret = ext4_journal_get_write_access(handle, orig_path->p_bh);
395 if (ret)
396 return ret;
397 }
398
399 /* Expansion */
400 if (range_to_move > 0 &&
401 (range_to_move > le16_to_cpu(eh->eh_max)
402 - le16_to_cpu(eh->eh_entries))) {
403
404 ret = mext_insert_across_blocks(handle, orig_inode, o_start,
405 o_end, start_ext, new_ext, end_ext);
406 if (ret < 0)
407 return ret;
408 } else
409 mext_insert_inside_block(o_start, o_end, start_ext, new_ext,
410 end_ext, eh, range_to_move);
411
412 if (depth) {
413 ret = ext4_handle_dirty_metadata(handle, orig_inode,
414 orig_path->p_bh);
415 if (ret)
416 return ret;
417 } else {
418 ret = ext4_mark_inode_dirty(handle, orig_inode);
419 if (ret < 0)
420 return ret;
421 }
422
423 return 0;
424}
425
426/**
427 * mext_leaf_block - Move one leaf extent block into the inode.
428 *
429 * @handle: journal handle
430 * @orig_inode: original inode
431 * @orig_path: path indicates first extent to be changed
432 * @dext: donor extent
433 * @from: start offset on the target file
434 *
435 * In order to insert extents into the leaf block, we must divide the extent
436 * in the leaf block into three extents. The one is located to be inserted
437 * extents, and the others are located around it.
438 *
439 * Therefore, this function creates structures to save extents of the leaf
440 * block, and inserts extents by calling mext_insert_extents() with
441 * created extents. Return 0 on success, or a negative error value on failure.
442 */
443static int
444mext_leaf_block(handle_t *handle, struct inode *orig_inode,
445 struct ext4_ext_path *orig_path, struct ext4_extent *dext,
446 ext4_lblk_t *from)
447{
448 struct ext4_extent *oext, *o_start, *o_end, *prev_ext;
449 struct ext4_extent new_ext, start_ext, end_ext;
450 ext4_lblk_t new_ext_end;
Akira Fujita748de672009-06-17 19:24:03 -0400451 int oext_alen, new_ext_alen, end_ext_alen;
452 int depth = ext_depth(orig_inode);
453 int ret;
454
Steven Liuc26d0ba2010-05-11 00:00:00 -0400455 start_ext.ee_block = end_ext.ee_block = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400456 o_start = o_end = oext = orig_path[depth].p_ext;
457 oext_alen = ext4_ext_get_actual_len(oext);
458 start_ext.ee_len = end_ext.ee_len = 0;
459
460 new_ext.ee_block = cpu_to_le32(*from);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400461 ext4_ext_store_pblock(&new_ext, ext4_ext_pblock(dext));
Akira Fujita748de672009-06-17 19:24:03 -0400462 new_ext.ee_len = dext->ee_len;
463 new_ext_alen = ext4_ext_get_actual_len(&new_ext);
464 new_ext_end = le32_to_cpu(new_ext.ee_block) + new_ext_alen - 1;
Akira Fujita748de672009-06-17 19:24:03 -0400465
466 /*
467 * Case: original extent is first
468 * oext |--------|
469 * new_ext |--|
470 * start_ext |--|
471 */
472 if (le32_to_cpu(oext->ee_block) < le32_to_cpu(new_ext.ee_block) &&
473 le32_to_cpu(new_ext.ee_block) <
474 le32_to_cpu(oext->ee_block) + oext_alen) {
475 start_ext.ee_len = cpu_to_le16(le32_to_cpu(new_ext.ee_block) -
476 le32_to_cpu(oext->ee_block));
Akira Fujita5fd52492010-03-04 00:31:06 -0500477 start_ext.ee_block = oext->ee_block;
Akira Fujita748de672009-06-17 19:24:03 -0400478 copy_extent_status(oext, &start_ext);
479 } else if (oext > EXT_FIRST_EXTENT(orig_path[depth].p_hdr)) {
480 prev_ext = oext - 1;
481 /*
482 * We can merge new_ext into previous extent,
483 * if these are contiguous and same extent type.
484 */
485 if (ext4_can_extents_be_merged(orig_inode, prev_ext,
486 &new_ext)) {
487 o_start = prev_ext;
488 start_ext.ee_len = cpu_to_le16(
489 ext4_ext_get_actual_len(prev_ext) +
490 new_ext_alen);
Akira Fujita5fd52492010-03-04 00:31:06 -0500491 start_ext.ee_block = oext->ee_block;
Akira Fujita748de672009-06-17 19:24:03 -0400492 copy_extent_status(prev_ext, &start_ext);
493 new_ext.ee_len = 0;
494 }
495 }
496
497 /*
498 * Case: new_ext_end must be less than oext
499 * oext |-----------|
500 * new_ext |-------|
501 */
Akira Fujita2147b1a2009-09-16 13:46:35 -0400502 if (le32_to_cpu(oext->ee_block) + oext_alen - 1 < new_ext_end) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400503 EXT4_ERROR_INODE(orig_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -0400504 "new_ext_end(%u) should be less than or equal to "
505 "oext->ee_block(%u) + oext_alen(%d) - 1",
506 new_ext_end, le32_to_cpu(oext->ee_block),
507 oext_alen);
508 ret = -EIO;
509 goto out;
510 }
Akira Fujita748de672009-06-17 19:24:03 -0400511
512 /*
513 * Case: new_ext is smaller than original extent
514 * oext |---------------|
515 * new_ext |-----------|
516 * end_ext |---|
517 */
518 if (le32_to_cpu(oext->ee_block) <= new_ext_end &&
519 new_ext_end < le32_to_cpu(oext->ee_block) + oext_alen - 1) {
520 end_ext.ee_len =
521 cpu_to_le16(le32_to_cpu(oext->ee_block) +
522 oext_alen - 1 - new_ext_end);
523 copy_extent_status(oext, &end_ext);
524 end_ext_alen = ext4_ext_get_actual_len(&end_ext);
525 ext4_ext_store_pblock(&end_ext,
Theodore Ts'obf89d162010-10-27 21:30:14 -0400526 (ext4_ext_pblock(o_end) + oext_alen - end_ext_alen));
Akira Fujita748de672009-06-17 19:24:03 -0400527 end_ext.ee_block =
528 cpu_to_le32(le32_to_cpu(o_end->ee_block) +
529 oext_alen - end_ext_alen);
530 }
531
532 ret = mext_insert_extents(handle, orig_inode, orig_path, o_start,
533 o_end, &start_ext, &new_ext, &end_ext);
Akira Fujita2147b1a2009-09-16 13:46:35 -0400534out:
Akira Fujita748de672009-06-17 19:24:03 -0400535 return ret;
536}
537
538/**
539 * mext_calc_swap_extents - Calculate extents for extent swapping.
540 *
541 * @tmp_dext: the extent that will belong to the original inode
542 * @tmp_oext: the extent that will belong to the donor inode
543 * @orig_off: block offset of original inode
544 * @donor_off: block offset of donor inode
Akira Fujita92c28152009-11-23 07:24:50 -0500545 * @max_count: the maximum length of extents
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400546 *
547 * Return 0 on success, or a negative error value on failure.
Akira Fujita748de672009-06-17 19:24:03 -0400548 */
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400549static int
Akira Fujita748de672009-06-17 19:24:03 -0400550mext_calc_swap_extents(struct ext4_extent *tmp_dext,
551 struct ext4_extent *tmp_oext,
552 ext4_lblk_t orig_off, ext4_lblk_t donor_off,
553 ext4_lblk_t max_count)
554{
555 ext4_lblk_t diff, orig_diff;
556 struct ext4_extent dext_old, oext_old;
557
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400558 BUG_ON(orig_off != donor_off);
559
560 /* original and donor extents have to cover the same block offset */
561 if (orig_off < le32_to_cpu(tmp_oext->ee_block) ||
562 le32_to_cpu(tmp_oext->ee_block) +
563 ext4_ext_get_actual_len(tmp_oext) - 1 < orig_off)
564 return -ENODATA;
565
566 if (orig_off < le32_to_cpu(tmp_dext->ee_block) ||
567 le32_to_cpu(tmp_dext->ee_block) +
568 ext4_ext_get_actual_len(tmp_dext) - 1 < orig_off)
569 return -ENODATA;
570
Akira Fujita748de672009-06-17 19:24:03 -0400571 dext_old = *tmp_dext;
572 oext_old = *tmp_oext;
573
574 /* When tmp_dext is too large, pick up the target range. */
575 diff = donor_off - le32_to_cpu(tmp_dext->ee_block);
576
Theodore Ts'obf89d162010-10-27 21:30:14 -0400577 ext4_ext_store_pblock(tmp_dext, ext4_ext_pblock(tmp_dext) + diff);
Wei Yongjunba39ebb2012-09-27 09:37:53 -0400578 le32_add_cpu(&tmp_dext->ee_block, diff);
579 le16_add_cpu(&tmp_dext->ee_len, -diff);
Akira Fujita748de672009-06-17 19:24:03 -0400580
581 if (max_count < ext4_ext_get_actual_len(tmp_dext))
582 tmp_dext->ee_len = cpu_to_le16(max_count);
583
584 orig_diff = orig_off - le32_to_cpu(tmp_oext->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400585 ext4_ext_store_pblock(tmp_oext, ext4_ext_pblock(tmp_oext) + orig_diff);
Akira Fujita748de672009-06-17 19:24:03 -0400586
587 /* Adjust extent length if donor extent is larger than orig */
588 if (ext4_ext_get_actual_len(tmp_dext) >
589 ext4_ext_get_actual_len(tmp_oext) - orig_diff)
590 tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_oext->ee_len) -
591 orig_diff);
592
593 tmp_oext->ee_len = cpu_to_le16(ext4_ext_get_actual_len(tmp_dext));
594
595 copy_extent_status(&oext_old, tmp_dext);
596 copy_extent_status(&dext_old, tmp_oext);
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400597
598 return 0;
Akira Fujita748de672009-06-17 19:24:03 -0400599}
600
601/**
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400602 * mext_check_coverage - Check that all extents in range has the same type
603 *
604 * @inode: inode in question
605 * @from: block offset of inode
606 * @count: block count to be checked
607 * @uninit: extents expected to be uninitialized
608 * @err: pointer to save error value
609 *
610 * Return 1 if all extents in range has expected type, and zero otherwise.
611 */
612static int
613mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
614 int uninit, int *err)
615{
616 struct ext4_ext_path *path = NULL;
617 struct ext4_extent *ext;
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400618 int ret = 0;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400619 ext4_lblk_t last = from + count;
620 while (from < last) {
621 *err = get_ext_path(inode, from, &path);
622 if (*err)
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400623 goto out;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400624 ext = path[ext_depth(inode)].p_ext;
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400625 if (uninit != ext4_ext_is_uninitialized(ext))
626 goto out;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400627 from += ext4_ext_get_actual_len(ext);
628 ext4_ext_drop_refs(path);
629 }
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400630 ret = 1;
631out:
632 if (path) {
633 ext4_ext_drop_refs(path);
634 kfree(path);
635 }
636 return ret;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400637}
638
639/**
Akira Fujita748de672009-06-17 19:24:03 -0400640 * mext_replace_branches - Replace original extents with new extents
641 *
642 * @handle: journal handle
643 * @orig_inode: original inode
644 * @donor_inode: donor inode
645 * @from: block offset of orig_inode
646 * @count: block count to be replaced
Akira Fujitaf868a482009-11-23 07:25:48 -0500647 * @err: pointer to save return value
Akira Fujita748de672009-06-17 19:24:03 -0400648 *
649 * Replace original inode extents and donor inode extents page by page.
650 * We implement this replacement in the following three steps:
651 * 1. Save the block information of original and donor inodes into
652 * dummy extents.
653 * 2. Change the block information of original inode to point at the
654 * donor inode blocks.
655 * 3. Change the block information of donor inode to point at the saved
656 * original inode blocks in the dummy extents.
657 *
Akira Fujitaf868a482009-11-23 07:25:48 -0500658 * Return replaced block count.
Akira Fujita748de672009-06-17 19:24:03 -0400659 */
660static int
661mext_replace_branches(handle_t *handle, struct inode *orig_inode,
662 struct inode *donor_inode, ext4_lblk_t from,
Akira Fujitaf868a482009-11-23 07:25:48 -0500663 ext4_lblk_t count, int *err)
Akira Fujita748de672009-06-17 19:24:03 -0400664{
665 struct ext4_ext_path *orig_path = NULL;
666 struct ext4_ext_path *donor_path = NULL;
667 struct ext4_extent *oext, *dext;
668 struct ext4_extent tmp_dext, tmp_oext;
669 ext4_lblk_t orig_off = from, donor_off = from;
Akira Fujita748de672009-06-17 19:24:03 -0400670 int depth;
671 int replaced_count = 0;
672 int dext_alen;
673
Dmitry Monakhov6ca470d72013-03-04 00:50:47 -0500674 *err = ext4_es_remove_extent(orig_inode, from, count);
675 if (*err)
676 goto out;
677
678 *err = ext4_es_remove_extent(donor_inode, from, count);
679 if (*err)
680 goto out;
681
Akira Fujita748de672009-06-17 19:24:03 -0400682 /* Get the original extent for the block "orig_off" */
Akira Fujitaf868a482009-11-23 07:25:48 -0500683 *err = get_ext_path(orig_inode, orig_off, &orig_path);
684 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400685 goto out;
686
687 /* Get the donor extent for the head */
Akira Fujitaf868a482009-11-23 07:25:48 -0500688 *err = get_ext_path(donor_inode, donor_off, &donor_path);
689 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400690 goto out;
691 depth = ext_depth(orig_inode);
692 oext = orig_path[depth].p_ext;
693 tmp_oext = *oext;
694
695 depth = ext_depth(donor_inode);
696 dext = donor_path[depth].p_ext;
Akria Fujita87e69872013-02-01 20:52:46 -0500697 if (unlikely(!dext))
698 goto missing_donor_extent;
Akira Fujita748de672009-06-17 19:24:03 -0400699 tmp_dext = *dext;
700
Akira Fujitaf868a482009-11-23 07:25:48 -0500701 *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
Akira Fujita748de672009-06-17 19:24:03 -0400702 donor_off, count);
Akira Fujitaf868a482009-11-23 07:25:48 -0500703 if (*err)
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400704 goto out;
Akira Fujita748de672009-06-17 19:24:03 -0400705
706 /* Loop for the donor extents */
707 while (1) {
708 /* The extent for donor must be found. */
Akria Fujita87e69872013-02-01 20:52:46 -0500709 if (unlikely(!dext)) {
710 missing_donor_extent:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400711 EXT4_ERROR_INODE(donor_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -0400712 "The extent for donor must be found");
Akira Fujitaf868a482009-11-23 07:25:48 -0500713 *err = -EIO;
Akira Fujita2147b1a2009-09-16 13:46:35 -0400714 goto out;
715 } else if (donor_off != le32_to_cpu(tmp_dext.ee_block)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -0400716 EXT4_ERROR_INODE(donor_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -0400717 "Donor offset(%u) and the first block of donor "
718 "extent(%u) should be equal",
719 donor_off,
720 le32_to_cpu(tmp_dext.ee_block));
Akira Fujitaf868a482009-11-23 07:25:48 -0500721 *err = -EIO;
Akira Fujita2147b1a2009-09-16 13:46:35 -0400722 goto out;
723 }
Akira Fujita748de672009-06-17 19:24:03 -0400724
725 /* Set donor extent to orig extent */
Akira Fujitaf868a482009-11-23 07:25:48 -0500726 *err = mext_leaf_block(handle, orig_inode,
Akira Fujita748de672009-06-17 19:24:03 -0400727 orig_path, &tmp_dext, &orig_off);
Akira Fujitaf868a482009-11-23 07:25:48 -0500728 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400729 goto out;
730
731 /* Set orig extent to donor extent */
Akira Fujitaf868a482009-11-23 07:25:48 -0500732 *err = mext_leaf_block(handle, donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -0400733 donor_path, &tmp_oext, &donor_off);
Akira Fujitaf868a482009-11-23 07:25:48 -0500734 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400735 goto out;
736
737 dext_alen = ext4_ext_get_actual_len(&tmp_dext);
738 replaced_count += dext_alen;
739 donor_off += dext_alen;
740 orig_off += dext_alen;
741
Dmitry Monakhov7e8b12c2013-04-11 23:24:58 -0400742 BUG_ON(replaced_count > count);
Akira Fujita748de672009-06-17 19:24:03 -0400743 /* Already moved the expected blocks */
744 if (replaced_count >= count)
745 break;
746
747 if (orig_path)
748 ext4_ext_drop_refs(orig_path);
Akira Fujitaf868a482009-11-23 07:25:48 -0500749 *err = get_ext_path(orig_inode, orig_off, &orig_path);
750 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400751 goto out;
752 depth = ext_depth(orig_inode);
753 oext = orig_path[depth].p_ext;
Akira Fujita748de672009-06-17 19:24:03 -0400754 tmp_oext = *oext;
755
756 if (donor_path)
757 ext4_ext_drop_refs(donor_path);
Akira Fujitaf868a482009-11-23 07:25:48 -0500758 *err = get_ext_path(donor_inode, donor_off, &donor_path);
759 if (*err)
Akira Fujita748de672009-06-17 19:24:03 -0400760 goto out;
761 depth = ext_depth(donor_inode);
762 dext = donor_path[depth].p_ext;
Akira Fujita748de672009-06-17 19:24:03 -0400763 tmp_dext = *dext;
764
Akira Fujitaf868a482009-11-23 07:25:48 -0500765 *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400766 donor_off, count - replaced_count);
Akira Fujitaf868a482009-11-23 07:25:48 -0500767 if (*err)
Akira Fujitac40ce3c2009-09-16 14:25:39 -0400768 goto out;
Akira Fujita748de672009-06-17 19:24:03 -0400769 }
770
771out:
772 if (orig_path) {
773 ext4_ext_drop_refs(orig_path);
774 kfree(orig_path);
775 }
776 if (donor_path) {
777 ext4_ext_drop_refs(donor_path);
778 kfree(donor_path);
779 }
780
Akira Fujitaf868a482009-11-23 07:25:48 -0500781 return replaced_count;
Akira Fujita748de672009-06-17 19:24:03 -0400782}
783
784/**
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400785 * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
786 *
787 * @inode1: the inode structure
788 * @inode2: the inode structure
789 * @index: page index
790 * @page: result page vector
791 *
792 * Grab two locked pages for inode's by inode order
793 */
794static int
795mext_page_double_lock(struct inode *inode1, struct inode *inode2,
796 pgoff_t index, struct page *page[2])
797{
798 struct address_space *mapping[2];
799 unsigned fl = AOP_FLAG_NOFS;
800
801 BUG_ON(!inode1 || !inode2);
802 if (inode1 < inode2) {
803 mapping[0] = inode1->i_mapping;
804 mapping[1] = inode2->i_mapping;
805 } else {
806 mapping[0] = inode2->i_mapping;
807 mapping[1] = inode1->i_mapping;
808 }
809
810 page[0] = grab_cache_page_write_begin(mapping[0], index, fl);
811 if (!page[0])
812 return -ENOMEM;
813
814 page[1] = grab_cache_page_write_begin(mapping[1], index, fl);
815 if (!page[1]) {
816 unlock_page(page[0]);
817 page_cache_release(page[0]);
818 return -ENOMEM;
819 }
Dmitry Monakhov7e8b12c2013-04-11 23:24:58 -0400820 /*
821 * grab_cache_page_write_begin() may not wait on page's writeback if
822 * BDI not demand that. But it is reasonable to be very conservative
823 * here and explicitly wait on page's writeback
824 */
825 wait_on_page_writeback(page[0]);
826 wait_on_page_writeback(page[1]);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400827 if (inode1 > inode2) {
828 struct page *tmp;
829 tmp = page[0];
830 page[0] = page[1];
831 page[1] = tmp;
832 }
833 return 0;
834}
835
836/* Force page buffers uptodate w/o dropping page's lock */
837static int
838mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
839{
840 struct inode *inode = page->mapping->host;
841 sector_t block;
842 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
843 unsigned int blocksize, block_start, block_end;
844 int i, err, nr = 0, partial = 0;
845 BUG_ON(!PageLocked(page));
846 BUG_ON(PageWriteback(page));
847
848 if (PageUptodate(page))
849 return 0;
850
851 blocksize = 1 << inode->i_blkbits;
852 if (!page_has_buffers(page))
853 create_empty_buffers(page, blocksize, 0);
854
855 head = page_buffers(page);
856 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
857 for (bh = head, block_start = 0; bh != head || !block_start;
858 block++, block_start = block_end, bh = bh->b_this_page) {
859 block_end = block_start + blocksize;
860 if (block_end <= from || block_start >= to) {
861 if (!buffer_uptodate(bh))
862 partial = 1;
863 continue;
864 }
865 if (buffer_uptodate(bh))
866 continue;
867 if (!buffer_mapped(bh)) {
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400868 err = ext4_get_block(inode, block, bh, 0);
869 if (err) {
870 SetPageError(page);
871 return err;
872 }
873 if (!buffer_mapped(bh)) {
874 zero_user(page, block_start, blocksize);
875 if (!err)
876 set_buffer_uptodate(bh);
877 continue;
878 }
879 }
880 BUG_ON(nr >= MAX_BUF_PER_PAGE);
881 arr[nr++] = bh;
882 }
883 /* No io required */
884 if (!nr)
885 goto out;
886
887 for (i = 0; i < nr; i++) {
888 bh = arr[i];
889 if (!bh_uptodate_or_lock(bh)) {
890 err = bh_submit_read(bh);
891 if (err)
892 return err;
893 }
894 }
895out:
896 if (!partial)
897 SetPageUptodate(page);
898 return 0;
899}
900
901/**
Akira Fujita748de672009-06-17 19:24:03 -0400902 * move_extent_per_page - Move extent data per page
903 *
904 * @o_filp: file structure of original file
905 * @donor_inode: donor inode
906 * @orig_page_offset: page index on original file
907 * @data_offset_in_page: block index where data swapping starts
908 * @block_len_in_page: the number of blocks to be swapped
909 * @uninit: orig extent is uninitialized or not
Akira Fujitaf868a482009-11-23 07:25:48 -0500910 * @err: pointer to save return value
Akira Fujita748de672009-06-17 19:24:03 -0400911 *
912 * Save the data in original inode blocks and replace original inode extents
913 * with donor inode extents by calling mext_replace_branches().
Akira Fujitaf868a482009-11-23 07:25:48 -0500914 * Finally, write out the saved data in new original inode blocks. Return
915 * replaced block count.
Akira Fujita748de672009-06-17 19:24:03 -0400916 */
917static int
Akira Fujita44fc48f2009-09-05 23:12:41 -0400918move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -0400919 pgoff_t orig_page_offset, int data_offset_in_page,
Akira Fujitaf868a482009-11-23 07:25:48 -0500920 int block_len_in_page, int uninit, int *err)
Akira Fujita748de672009-06-17 19:24:03 -0400921{
Al Viro496ad9a2013-01-23 17:07:38 -0500922 struct inode *orig_inode = file_inode(o_filp);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400923 struct page *pagep[2] = {NULL, NULL};
Akira Fujita748de672009-06-17 19:24:03 -0400924 handle_t *handle;
925 ext4_lblk_t orig_blk_offset;
926 long long offs = orig_page_offset << PAGE_CACHE_SHIFT;
927 unsigned long blocksize = orig_inode->i_sb->s_blocksize;
928 unsigned int w_flags = 0;
Akira Fujitaf868a482009-11-23 07:25:48 -0500929 unsigned int tmp_data_size, data_size, replaced_size;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400930 int err2, jblocks, retries = 0;
Akira Fujitaf868a482009-11-23 07:25:48 -0500931 int replaced_count = 0;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400932 int from = data_offset_in_page << orig_inode->i_blkbits;
Akira Fujita748de672009-06-17 19:24:03 -0400933 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
934
935 /*
936 * It needs twice the amount of ordinary journal buffers because
937 * inode and donor_inode may change each different metadata blocks.
938 */
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400939again:
940 *err = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400941 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
Theodore Ts'o9924a922013-02-08 21:59:22 -0500942 handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
Akira Fujita748de672009-06-17 19:24:03 -0400943 if (IS_ERR(handle)) {
Akira Fujitaf868a482009-11-23 07:25:48 -0500944 *err = PTR_ERR(handle);
945 return 0;
Akira Fujita748de672009-06-17 19:24:03 -0400946 }
947
948 if (segment_eq(get_fs(), KERNEL_DS))
949 w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
950
951 orig_blk_offset = orig_page_offset * blocks_per_page +
952 data_offset_in_page;
953
Akira Fujita748de672009-06-17 19:24:03 -0400954 offs = (long long)orig_blk_offset << orig_inode->i_blkbits;
955
Akira Fujitaf868a482009-11-23 07:25:48 -0500956 /* Calculate data_size */
Akira Fujita748de672009-06-17 19:24:03 -0400957 if ((orig_blk_offset + block_len_in_page - 1) ==
958 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
959 /* Replace the last block */
Akira Fujitaf868a482009-11-23 07:25:48 -0500960 tmp_data_size = orig_inode->i_size & (blocksize - 1);
Akira Fujita748de672009-06-17 19:24:03 -0400961 /*
Akira Fujitaf868a482009-11-23 07:25:48 -0500962 * If data_size equal zero, it shows data_size is multiples of
Akira Fujita748de672009-06-17 19:24:03 -0400963 * blocksize. So we set appropriate value.
964 */
Akira Fujitaf868a482009-11-23 07:25:48 -0500965 if (tmp_data_size == 0)
966 tmp_data_size = blocksize;
Akira Fujita748de672009-06-17 19:24:03 -0400967
Akira Fujitaf868a482009-11-23 07:25:48 -0500968 data_size = tmp_data_size +
Akira Fujita748de672009-06-17 19:24:03 -0400969 ((block_len_in_page - 1) << orig_inode->i_blkbits);
Akira Fujitaf868a482009-11-23 07:25:48 -0500970 } else
971 data_size = block_len_in_page << orig_inode->i_blkbits;
Akira Fujita748de672009-06-17 19:24:03 -0400972
Akira Fujitaf868a482009-11-23 07:25:48 -0500973 replaced_size = data_size;
974
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400975 *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
976 pagep);
Akira Fujitaf868a482009-11-23 07:25:48 -0500977 if (unlikely(*err < 0))
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400978 goto stop_journal;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400979 /*
980 * If orig extent was uninitialized it can become initialized
981 * at any time after i_data_sem was dropped, in order to
982 * serialize with delalloc we have recheck extent while we
983 * hold page's lock, if it is still the case data copy is not
984 * necessary, just swap data blocks between orig and donor.
985 */
986 if (uninit) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400987 ext4_double_down_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400988 /* If any of extents in range became initialized we have to
989 * fallback to data copying */
990 uninit = mext_check_coverage(orig_inode, orig_blk_offset,
991 block_len_in_page, 1, err);
992 if (*err)
993 goto drop_data_sem;
Akira Fujita748de672009-06-17 19:24:03 -0400994
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400995 uninit &= mext_check_coverage(donor_inode, orig_blk_offset,
996 block_len_in_page, 1, err);
997 if (*err)
998 goto drop_data_sem;
999
1000 if (!uninit) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001001 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov8c854472012-09-26 12:54:52 -04001002 goto data_copy;
1003 }
1004 if ((page_has_private(pagep[0]) &&
1005 !try_to_release_page(pagep[0], 0)) ||
1006 (page_has_private(pagep[1]) &&
1007 !try_to_release_page(pagep[1], 0))) {
1008 *err = -EBUSY;
1009 goto drop_data_sem;
1010 }
1011 replaced_count = mext_replace_branches(handle, orig_inode,
1012 donor_inode, orig_blk_offset,
1013 block_len_in_page, err);
1014 drop_data_sem:
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001015 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov8c854472012-09-26 12:54:52 -04001016 goto unlock_pages;
1017 }
1018data_copy:
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001019 *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
1020 if (*err)
1021 goto unlock_pages;
1022
1023 /* At this point all buffers in range are uptodate, old mapping layout
1024 * is no longer required, try to drop it now. */
1025 if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
1026 (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
1027 *err = -EBUSY;
1028 goto unlock_pages;
Akira Fujita748de672009-06-17 19:24:03 -04001029 }
1030
Akira Fujitaf868a482009-11-23 07:25:48 -05001031 replaced_count = mext_replace_branches(handle, orig_inode, donor_inode,
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001032 orig_blk_offset,
1033 block_len_in_page, err);
1034 if (*err) {
Akira Fujitaf868a482009-11-23 07:25:48 -05001035 if (replaced_count) {
1036 block_len_in_page = replaced_count;
1037 replaced_size =
1038 block_len_in_page << orig_inode->i_blkbits;
Akira Fujitaac48b0a2009-11-24 10:31:56 -05001039 } else
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001040 goto unlock_pages;
Akira Fujitaf868a482009-11-23 07:25:48 -05001041 }
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001042 /* Perform all necessary steps similar write_begin()/write_end()
1043 * but keeping in mind that i_size will not change */
Dmitry Monakhov7e8b12c2013-04-11 23:24:58 -04001044 *err = __block_write_begin(pagep[0], from, replaced_size,
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001045 ext4_get_block);
1046 if (!*err)
1047 *err = block_commit_write(pagep[0], from, from + replaced_size);
Akira Fujita748de672009-06-17 19:24:03 -04001048
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001049 if (unlikely(*err < 0))
1050 goto repair_branches;
Akira Fujita748de672009-06-17 19:24:03 -04001051
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001052 /* Even in case of data=writeback it is reasonable to pin
1053 * inode to transaction, to prevent unexpected data loss */
1054 *err = ext4_jbd2_file_inode(handle, orig_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001055
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001056unlock_pages:
1057 unlock_page(pagep[0]);
1058 page_cache_release(pagep[0]);
1059 unlock_page(pagep[1]);
1060 page_cache_release(pagep[1]);
1061stop_journal:
Akira Fujita748de672009-06-17 19:24:03 -04001062 ext4_journal_stop(handle);
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001063 /* Buffer was busy because probably is pinned to journal transaction,
1064 * force transaction commit may help to free it. */
1065 if (*err == -EBUSY && ext4_should_retry_alloc(orig_inode->i_sb,
1066 &retries))
1067 goto again;
Akira Fujitaf868a482009-11-23 07:25:48 -05001068 return replaced_count;
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001069
1070repair_branches:
1071 /*
1072 * This should never ever happen!
1073 * Extents are swapped already, but we are not able to copy data.
1074 * Try to swap extents to it's original places
1075 */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001076 ext4_double_down_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001077 replaced_count = mext_replace_branches(handle, donor_inode, orig_inode,
1078 orig_blk_offset,
1079 block_len_in_page, &err2);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001080 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhovbb557482012-09-26 12:52:07 -04001081 if (replaced_count != block_len_in_page) {
1082 EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
1083 "Unable to copy data block,"
1084 " data will be lost.");
1085 *err = -EIO;
1086 }
1087 replaced_count = 0;
1088 goto unlock_pages;
Akira Fujita748de672009-06-17 19:24:03 -04001089}
1090
1091/**
Akira Fujitac437b272010-03-04 00:39:24 -05001092 * mext_check_arguments - Check whether move extent can be done
Akira Fujita748de672009-06-17 19:24:03 -04001093 *
1094 * @orig_inode: original inode
1095 * @donor_inode: donor inode
1096 * @orig_start: logical start offset in block for orig
1097 * @donor_start: logical start offset in block for donor
1098 * @len: the number of blocks to be moved
Akira Fujita748de672009-06-17 19:24:03 -04001099 *
1100 * Check the arguments of ext4_move_extents() whether the files can be
1101 * exchanged with each other.
1102 * Return 0 on success, or a negative error value on failure.
1103 */
1104static int
1105mext_check_arguments(struct inode *orig_inode,
Kazuya Mio446aaa62009-11-24 10:28:48 -05001106 struct inode *donor_inode, __u64 orig_start,
1107 __u64 donor_start, __u64 *len)
Akira Fujita748de672009-06-17 19:24:03 -04001108{
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001109 ext4_lblk_t orig_blocks, donor_blocks;
1110 unsigned int blkbits = orig_inode->i_blkbits;
1111 unsigned int blocksize = 1 << blkbits;
1112
Akira Fujita4a585792009-12-06 23:38:31 -05001113 if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
1114 ext4_debug("ext4 move extent: suid or sgid is set"
1115 " to donor file [ino:orig %lu, donor %lu]\n",
1116 orig_inode->i_ino, donor_inode->i_ino);
1117 return -EINVAL;
1118 }
1119
Theodore Ts'o1f5a81e2010-06-02 22:04:39 -04001120 if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
1121 return -EPERM;
1122
Akira Fujita748de672009-06-17 19:24:03 -04001123 /* Ext4 move extent does not support swapfile */
1124 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
1125 ext4_debug("ext4 move extent: The argument files should "
1126 "not be swapfile [ino:orig %lu, donor %lu]\n",
1127 orig_inode->i_ino, donor_inode->i_ino);
1128 return -EINVAL;
1129 }
1130
Akira Fujita748de672009-06-17 19:24:03 -04001131 /* Ext4 move extent supports only extent based file */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001132 if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
Akira Fujita748de672009-06-17 19:24:03 -04001133 ext4_debug("ext4 move extent: orig file is not extents "
1134 "based file [ino:orig %lu]\n", orig_inode->i_ino);
1135 return -EOPNOTSUPP;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001136 } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
Akira Fujita748de672009-06-17 19:24:03 -04001137 ext4_debug("ext4 move extent: donor file is not extents "
1138 "based file [ino:donor %lu]\n", donor_inode->i_ino);
1139 return -EOPNOTSUPP;
1140 }
1141
1142 if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
1143 ext4_debug("ext4 move extent: File size is 0 byte\n");
1144 return -EINVAL;
1145 }
1146
1147 /* Start offset should be same */
1148 if (orig_start != donor_start) {
1149 ext4_debug("ext4 move extent: orig and donor's start "
1150 "offset are not same [ino:orig %lu, donor %lu]\n",
1151 orig_inode->i_ino, donor_inode->i_ino);
1152 return -EINVAL;
1153 }
1154
Lukas Czernerf17722f2011-06-06 00:05:17 -04001155 if ((orig_start >= EXT_MAX_BLOCKS) ||
Lukas Czernerf17722f2011-06-06 00:05:17 -04001156 (*len > EXT_MAX_BLOCKS) ||
1157 (orig_start + *len >= EXT_MAX_BLOCKS)) {
Eric Sandeen0a80e982009-09-17 11:55:58 -04001158 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
Lukas Czernerf17722f2011-06-06 00:05:17 -04001159 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
Akira Fujita748de672009-06-17 19:24:03 -04001160 orig_inode->i_ino, donor_inode->i_ino);
1161 return -EINVAL;
1162 }
1163
1164 if (orig_inode->i_size > donor_inode->i_size) {
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001165 donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits;
1166 /* TODO: eliminate this artificial restriction */
1167 if (orig_start >= donor_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001168 ext4_debug("ext4 move extent: orig start offset "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001169 "[%llu] should be less than donor file blocks "
1170 "[%u] [ino:orig %lu, donor %lu]\n",
1171 orig_start, donor_blocks,
Akira Fujita748de672009-06-17 19:24:03 -04001172 orig_inode->i_ino, donor_inode->i_ino);
1173 return -EINVAL;
1174 }
1175
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001176 /* TODO: eliminate this artificial restriction */
1177 if (orig_start + *len > donor_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001178 ext4_debug("ext4 move extent: End offset [%llu] should "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001179 "be less than donor file blocks [%u]."
1180 "So adjust length from %llu to %llu "
Akira Fujita748de672009-06-17 19:24:03 -04001181 "[ino:orig %lu, donor %lu]\n",
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001182 orig_start + *len, donor_blocks,
1183 *len, donor_blocks - orig_start,
Akira Fujita748de672009-06-17 19:24:03 -04001184 orig_inode->i_ino, donor_inode->i_ino);
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001185 *len = donor_blocks - orig_start;
Akira Fujita748de672009-06-17 19:24:03 -04001186 }
1187 } else {
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001188 orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits;
1189 if (orig_start >= orig_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001190 ext4_debug("ext4 move extent: start offset [%llu] "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001191 "should be less than original file blocks "
1192 "[%u] [ino:orig %lu, donor %lu]\n",
1193 orig_start, orig_blocks,
Akira Fujita748de672009-06-17 19:24:03 -04001194 orig_inode->i_ino, donor_inode->i_ino);
1195 return -EINVAL;
1196 }
1197
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001198 if (orig_start + *len > orig_blocks) {
Akira Fujita748de672009-06-17 19:24:03 -04001199 ext4_debug("ext4 move extent: Adjust length "
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001200 "from %llu to %llu. Because it should be "
1201 "less than original file blocks "
Akira Fujita748de672009-06-17 19:24:03 -04001202 "[ino:orig %lu, donor %lu]\n",
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001203 *len, orig_blocks - orig_start,
Akira Fujita748de672009-06-17 19:24:03 -04001204 orig_inode->i_ino, donor_inode->i_ino);
Akira Fujita70d5d3d2009-09-16 14:28:22 -04001205 *len = orig_blocks - orig_start;
Akira Fujita748de672009-06-17 19:24:03 -04001206 }
1207 }
1208
1209 if (!*len) {
Akira Fujita92c28152009-11-23 07:24:50 -05001210 ext4_debug("ext4 move extent: len should not be 0 "
Akira Fujita748de672009-06-17 19:24:03 -04001211 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
1212 donor_inode->i_ino);
1213 return -EINVAL;
1214 }
1215
1216 return 0;
1217}
1218
1219/**
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001220 * ext4_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
Akira Fujita748de672009-06-17 19:24:03 -04001221 *
1222 * @inode1: the inode structure
1223 * @inode2: the inode structure
1224 *
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001225 * Lock two inodes' i_mutex
Akira Fujita748de672009-06-17 19:24:03 -04001226 */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001227void
1228ext4_inode_double_lock(struct inode *inode1, struct inode *inode2)
Akira Fujita748de672009-06-17 19:24:03 -04001229{
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001230 BUG_ON(inode1 == inode2);
1231 if (inode1 < inode2) {
Akira Fujita748de672009-06-17 19:24:03 -04001232 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
1233 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
1234 } else {
1235 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
1236 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
1237 }
1238}
1239
1240/**
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001241 * ext4_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
Akira Fujita748de672009-06-17 19:24:03 -04001242 *
1243 * @inode1: the inode that is released first
1244 * @inode2: the inode that is released second
1245 *
Akira Fujita748de672009-06-17 19:24:03 -04001246 */
1247
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001248void
1249ext4_inode_double_unlock(struct inode *inode1, struct inode *inode2)
Akira Fujita748de672009-06-17 19:24:03 -04001250{
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001251 mutex_unlock(&inode1->i_mutex);
1252 mutex_unlock(&inode2->i_mutex);
Akira Fujita748de672009-06-17 19:24:03 -04001253}
1254
1255/**
1256 * ext4_move_extents - Exchange the specified range of a file
1257 *
1258 * @o_filp: file structure of the original file
1259 * @d_filp: file structure of the donor file
1260 * @orig_start: start offset in block for orig
1261 * @donor_start: start offset in block for donor
1262 * @len: the number of blocks to be moved
1263 * @moved_len: moved block length
1264 *
1265 * This function returns 0 and moved block length is set in moved_len
1266 * if succeed, otherwise returns error value.
1267 *
1268 * Note: ext4_move_extents() proceeds the following order.
1269 * 1:ext4_move_extents() calculates the last block number of moving extent
1270 * function by the start block number (orig_start) and the number of blocks
1271 * to be moved (len) specified as arguments.
1272 * If the {orig, donor}_start points a hole, the extent's start offset
1273 * pointed by ext_cur (current extent), holecheck_path, orig_path are set
1274 * after hole behind.
1275 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
1276 * or the ext_cur exceeds the block_end which is last logical block number.
1277 * 3:To get the length of continues area, call mext_next_extent()
1278 * specified with the ext_cur (initial value is holecheck_path) re-cursive,
1279 * until find un-continuous extent, the start logical block number exceeds
1280 * the block_end or the extent points to the last extent.
1281 * 4:Exchange the original inode data with donor inode data
1282 * from orig_page_offset to seq_end_page.
1283 * The start indexes of data are specified as arguments.
1284 * That of the original inode is orig_page_offset,
1285 * and the donor inode is also orig_page_offset
1286 * (To easily handle blocksize != pagesize case, the offset for the
1287 * donor inode is block unit).
1288 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
1289 * then returns to step 2.
1290 * 6:Release holecheck_path, orig_path and set the len to moved_len
1291 * which shows the number of moved blocks.
1292 * The moved_len is useful for the command to calculate the file offset
1293 * for starting next move extent ioctl.
1294 * 7:Return 0 on success, or a negative error value on failure.
1295 */
1296int
1297ext4_move_extents(struct file *o_filp, struct file *d_filp,
1298 __u64 orig_start, __u64 donor_start, __u64 len,
1299 __u64 *moved_len)
1300{
Al Viro496ad9a2013-01-23 17:07:38 -05001301 struct inode *orig_inode = file_inode(o_filp);
1302 struct inode *donor_inode = file_inode(d_filp);
Akira Fujita748de672009-06-17 19:24:03 -04001303 struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL;
1304 struct ext4_extent *ext_prev, *ext_cur, *ext_dummy;
1305 ext4_lblk_t block_start = orig_start;
1306 ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0;
1307 ext4_lblk_t rest_blocks;
1308 pgoff_t orig_page_offset = 0, seq_end_page;
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001309 int ret, depth, last_extent = 0;
Akira Fujita748de672009-06-17 19:24:03 -04001310 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
1311 int data_offset_in_page;
1312 int block_len_in_page;
1313 int uninit;
1314
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001315 if (orig_inode->i_sb != donor_inode->i_sb) {
1316 ext4_debug("ext4 move extent: The argument files "
1317 "should be in same FS [ino:orig %lu, donor %lu]\n",
1318 orig_inode->i_ino, donor_inode->i_ino);
1319 return -EINVAL;
1320 }
1321
1322 /* orig and donor should be different inodes */
1323 if (orig_inode == donor_inode) {
Theodore Ts'of3ce8062009-09-28 15:58:29 -04001324 ext4_debug("ext4 move extent: The argument files should not "
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001325 "be same inode [ino:orig %lu, donor %lu]\n",
Theodore Ts'of3ce8062009-09-28 15:58:29 -04001326 orig_inode->i_ino, donor_inode->i_ino);
1327 return -EINVAL;
1328 }
1329
Akira Fujita7247c0c2010-03-04 00:34:58 -05001330 /* Regular file check */
1331 if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
1332 ext4_debug("ext4 move extent: The argument files should be "
1333 "regular file [ino:orig %lu, donor %lu]\n",
1334 orig_inode->i_ino, donor_inode->i_ino);
1335 return -EINVAL;
1336 }
Dmitry Monakhovf0660552012-09-26 12:32:54 -04001337 /* TODO: This is non obvious task to swap blocks for inodes with full
1338 jornaling enabled */
1339 if (ext4_should_journal_data(orig_inode) ||
1340 ext4_should_journal_data(donor_inode)) {
1341 return -EINVAL;
1342 }
Akira Fujitafc04cb42009-11-23 07:24:43 -05001343 /* Protect orig and donor inodes against a truncate */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001344 ext4_inode_double_lock(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001345
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04001346 /* Wait for all existing dio workers */
1347 ext4_inode_block_unlocked_dio(orig_inode);
1348 ext4_inode_block_unlocked_dio(donor_inode);
1349 inode_dio_wait(orig_inode);
1350 inode_dio_wait(donor_inode);
1351
Akira Fujitafc04cb42009-11-23 07:24:43 -05001352 /* Protect extent tree against block allocations via delalloc */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001353 ext4_double_down_write_data_sem(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001354 /* Check the filesystem environment whether move_extent can be done */
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001355 ret = mext_check_arguments(orig_inode, donor_inode, orig_start,
Kazuya Mio446aaa62009-11-24 10:28:48 -05001356 donor_start, &len);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001357 if (ret)
Akira Fujita347fa6f2009-09-16 14:25:07 -04001358 goto out;
Akira Fujita748de672009-06-17 19:24:03 -04001359
1360 file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits;
1361 block_end = block_start + len - 1;
1362 if (file_end < block_end)
1363 len -= block_end - file_end;
1364
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001365 ret = get_ext_path(orig_inode, block_start, &orig_path);
1366 if (ret)
Akira Fujita347fa6f2009-09-16 14:25:07 -04001367 goto out;
Akira Fujita748de672009-06-17 19:24:03 -04001368
1369 /* Get path structure to check the hole */
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001370 ret = get_ext_path(orig_inode, block_start, &holecheck_path);
1371 if (ret)
Akira Fujita748de672009-06-17 19:24:03 -04001372 goto out;
1373
1374 depth = ext_depth(orig_inode);
1375 ext_cur = holecheck_path[depth].p_ext;
Akira Fujita748de672009-06-17 19:24:03 -04001376
1377 /*
Akira Fujitac40ce3c2009-09-16 14:25:39 -04001378 * Get proper starting location of block replacement if block_start was
1379 * within the hole.
Akira Fujita748de672009-06-17 19:24:03 -04001380 */
1381 if (le32_to_cpu(ext_cur->ee_block) +
1382 ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
Akira Fujitac40ce3c2009-09-16 14:25:39 -04001383 /*
1384 * The hole exists between extents or the tail of
1385 * original file.
1386 */
Akira Fujita748de672009-06-17 19:24:03 -04001387 last_extent = mext_next_extent(orig_inode,
1388 holecheck_path, &ext_cur);
1389 if (last_extent < 0) {
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001390 ret = last_extent;
Akira Fujita748de672009-06-17 19:24:03 -04001391 goto out;
1392 }
1393 last_extent = mext_next_extent(orig_inode, orig_path,
1394 &ext_dummy);
1395 if (last_extent < 0) {
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001396 ret = last_extent;
Akira Fujita748de672009-06-17 19:24:03 -04001397 goto out;
1398 }
Akira Fujitac40ce3c2009-09-16 14:25:39 -04001399 seq_start = le32_to_cpu(ext_cur->ee_block);
1400 } else if (le32_to_cpu(ext_cur->ee_block) > block_start)
1401 /* The hole exists at the beginning of original file. */
1402 seq_start = le32_to_cpu(ext_cur->ee_block);
1403 else
1404 seq_start = block_start;
Akira Fujita748de672009-06-17 19:24:03 -04001405
1406 /* No blocks within the specified range. */
1407 if (le32_to_cpu(ext_cur->ee_block) > block_end) {
1408 ext4_debug("ext4 move extent: The specified range of file "
1409 "may be the hole\n");
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001410 ret = -EINVAL;
Akira Fujita748de672009-06-17 19:24:03 -04001411 goto out;
1412 }
1413
1414 /* Adjust start blocks */
1415 add_blocks = min(le32_to_cpu(ext_cur->ee_block) +
1416 ext4_ext_get_actual_len(ext_cur), block_end + 1) -
1417 max(le32_to_cpu(ext_cur->ee_block), block_start);
1418
1419 while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
1420 seq_blocks += add_blocks;
1421
1422 /* Adjust tail blocks */
1423 if (seq_start + seq_blocks - 1 > block_end)
1424 seq_blocks = block_end - seq_start + 1;
1425
1426 ext_prev = ext_cur;
1427 last_extent = mext_next_extent(orig_inode, holecheck_path,
1428 &ext_cur);
1429 if (last_extent < 0) {
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001430 ret = last_extent;
Akira Fujita748de672009-06-17 19:24:03 -04001431 break;
1432 }
1433 add_blocks = ext4_ext_get_actual_len(ext_cur);
1434
1435 /*
1436 * Extend the length of contiguous block (seq_blocks)
1437 * if extents are contiguous.
1438 */
1439 if (ext4_can_extents_be_merged(orig_inode,
1440 ext_prev, ext_cur) &&
1441 block_end >= le32_to_cpu(ext_cur->ee_block) &&
1442 !last_extent)
1443 continue;
1444
1445 /* Is original extent is uninitialized */
1446 uninit = ext4_ext_is_uninitialized(ext_prev);
1447
1448 data_offset_in_page = seq_start % blocks_per_page;
1449
1450 /*
1451 * Calculate data blocks count that should be swapped
1452 * at the first page.
1453 */
1454 if (data_offset_in_page + seq_blocks > blocks_per_page) {
1455 /* Swapped blocks are across pages */
1456 block_len_in_page =
1457 blocks_per_page - data_offset_in_page;
1458 } else {
1459 /* Swapped blocks are in a page */
1460 block_len_in_page = seq_blocks;
1461 }
1462
1463 orig_page_offset = seq_start >>
1464 (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1465 seq_end_page = (seq_start + seq_blocks - 1) >>
1466 (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1467 seq_start = le32_to_cpu(ext_cur->ee_block);
1468 rest_blocks = seq_blocks;
1469
Akira Fujitafc04cb42009-11-23 07:24:43 -05001470 /*
1471 * Up semaphore to avoid following problems:
1472 * a. transaction deadlock among ext4_journal_start,
1473 * ->write_begin via pagefault, and jbd2_journal_commit
1474 * b. racing with ->readpage, ->write_begin, and ext4_get_block
1475 * in move_extent_per_page
1476 */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001477 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001478
1479 while (orig_page_offset <= seq_end_page) {
1480
1481 /* Swap original branches with new branches */
Akira Fujitaf868a482009-11-23 07:25:48 -05001482 block_len_in_page = move_extent_per_page(
1483 o_filp, donor_inode,
Akira Fujita748de672009-06-17 19:24:03 -04001484 orig_page_offset,
1485 data_offset_in_page,
Akira Fujitaf868a482009-11-23 07:25:48 -05001486 block_len_in_page, uninit,
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001487 &ret);
Akira Fujitaf868a482009-11-23 07:25:48 -05001488
Akira Fujita748de672009-06-17 19:24:03 -04001489 /* Count how many blocks we have exchanged */
1490 *moved_len += block_len_in_page;
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001491 if (ret < 0)
Akira Fujitafc04cb42009-11-23 07:24:43 -05001492 break;
Akira Fujita2147b1a2009-09-16 13:46:35 -04001493 if (*moved_len > len) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001494 EXT4_ERROR_INODE(orig_inode,
Akira Fujita2147b1a2009-09-16 13:46:35 -04001495 "We replaced blocks too much! "
1496 "sum of replaced: %llu requested: %llu",
1497 *moved_len, len);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001498 ret = -EIO;
Akira Fujitafc04cb42009-11-23 07:24:43 -05001499 break;
Akira Fujita2147b1a2009-09-16 13:46:35 -04001500 }
Akira Fujita748de672009-06-17 19:24:03 -04001501
Akira Fujitaf868a482009-11-23 07:25:48 -05001502 orig_page_offset++;
Akira Fujita748de672009-06-17 19:24:03 -04001503 data_offset_in_page = 0;
1504 rest_blocks -= block_len_in_page;
1505 if (rest_blocks > blocks_per_page)
1506 block_len_in_page = blocks_per_page;
1507 else
1508 block_len_in_page = rest_blocks;
1509 }
1510
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001511 ext4_double_down_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001512 if (ret < 0)
Akira Fujitafc04cb42009-11-23 07:24:43 -05001513 break;
1514
Akira Fujita748de672009-06-17 19:24:03 -04001515 /* Decrease buffer counter */
1516 if (holecheck_path)
1517 ext4_ext_drop_refs(holecheck_path);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001518 ret = get_ext_path(orig_inode, seq_start, &holecheck_path);
1519 if (ret)
Akira Fujita748de672009-06-17 19:24:03 -04001520 break;
1521 depth = holecheck_path->p_depth;
1522
1523 /* Decrease buffer counter */
1524 if (orig_path)
1525 ext4_ext_drop_refs(orig_path);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001526 ret = get_ext_path(orig_inode, seq_start, &orig_path);
1527 if (ret)
Akira Fujita748de672009-06-17 19:24:03 -04001528 break;
1529
1530 ext_cur = holecheck_path[depth].p_ext;
1531 add_blocks = ext4_ext_get_actual_len(ext_cur);
1532 seq_blocks = 0;
1533
1534 }
1535out:
Akira Fujita94d7c162009-11-24 10:19:57 -05001536 if (*moved_len) {
1537 ext4_discard_preallocations(orig_inode);
1538 ext4_discard_preallocations(donor_inode);
1539 }
1540
Akira Fujita748de672009-06-17 19:24:03 -04001541 if (orig_path) {
1542 ext4_ext_drop_refs(orig_path);
1543 kfree(orig_path);
1544 }
1545 if (holecheck_path) {
1546 ext4_ext_drop_refs(holecheck_path);
1547 kfree(holecheck_path);
1548 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001549 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04001550 ext4_inode_resume_unlocked_dio(orig_inode);
1551 ext4_inode_resume_unlocked_dio(donor_inode);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04001552 ext4_inode_double_unlock(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -04001553
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -04001554 return ret;
Akira Fujita748de672009-06-17 19:24:03 -04001555}