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