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