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