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