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