1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * Copyright (c) 2008,2009 NEC Software Tohoku, Ltd. 4 * Written by Takashi Sato <t-sato@yk.jp.nec.com> 5 * Akira Fujita <a-fujita@rs.jp.nec.com> 6 */ 7 8 #include <linux/fs.h> 9 #include <linux/quotaops.h> 10 #include <linux/slab.h> 11 #include <linux/sched/mm.h> 12 #include "ext4_jbd2.h" 13 #include "ext4.h" 14 #include "ext4_extents.h" 15 16 /** 17 * get_ext_path() - Find an extent path for designated logical block number. 18 * @inode: inode to be searched 19 * @lblock: logical block number to find an extent path 20 * @ppath: pointer to an extent path pointer (for output) 21 * 22 * ext4_find_extent wrapper. Return 0 on success, or a negative error value 23 * on failure. 24 */ 25 static inline int 26 get_ext_path(struct inode *inode, ext4_lblk_t lblock, 27 struct ext4_ext_path **ppath) 28 { 29 struct ext4_ext_path *path; 30 31 path = ext4_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE); 32 if (IS_ERR(path)) 33 return PTR_ERR(path); 34 if (path[ext_depth(inode)].p_ext == NULL) { 35 ext4_free_ext_path(path); 36 *ppath = NULL; 37 return -ENODATA; 38 } 39 return 0; 40 } 41 42 /** 43 * ext4_double_down_write_data_sem() - write lock two inodes's i_data_sem 44 * @first: inode to be locked 45 * @second: inode to be locked 46 * 47 * Acquire write lock of i_data_sem of the two inodes 48 */ 49 void 50 ext4_double_down_write_data_sem(struct inode *first, struct inode *second) 51 { 52 if (first < second) { 53 down_write(&EXT4_I(first)->i_data_sem); 54 down_write_nested(&EXT4_I(second)->i_data_sem, I_DATA_SEM_OTHER); 55 } else { 56 down_write(&EXT4_I(second)->i_data_sem); 57 down_write_nested(&EXT4_I(first)->i_data_sem, I_DATA_SEM_OTHER); 58 59 } 60 } 61 62 /** 63 * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem 64 * 65 * @orig_inode: original inode structure to be released its lock first 66 * @donor_inode: donor inode structure to be released its lock second 67 * Release write lock of i_data_sem of two inodes (orig and donor). 68 */ 69 void 70 ext4_double_up_write_data_sem(struct inode *orig_inode, 71 struct inode *donor_inode) 72 { 73 up_write(&EXT4_I(orig_inode)->i_data_sem); 74 up_write(&EXT4_I(donor_inode)->i_data_sem); 75 } 76 77 /** 78 * mext_check_coverage - Check that all extents in range has the same type 79 * 80 * @inode: inode in question 81 * @from: block offset of inode 82 * @count: block count to be checked 83 * @unwritten: extents expected to be unwritten 84 * @err: pointer to save error value 85 * 86 * Return 1 if all extents in range has expected type, and zero otherwise. 87 */ 88 static int 89 mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count, 90 int unwritten, int *err) 91 { 92 struct ext4_ext_path *path = NULL; 93 struct ext4_extent *ext; 94 int ret = 0; 95 ext4_lblk_t last = from + count; 96 while (from < last) { 97 *err = get_ext_path(inode, from, &path); 98 if (*err) 99 goto out; 100 ext = path[ext_depth(inode)].p_ext; 101 if (unwritten != ext4_ext_is_unwritten(ext)) 102 goto out; 103 from += ext4_ext_get_actual_len(ext); 104 } 105 ret = 1; 106 out: 107 ext4_free_ext_path(path); 108 return ret; 109 } 110 111 /** 112 * mext_folio_double_lock - Grab and lock folio on both @inode1 and @inode2 113 * 114 * @inode1: the inode structure 115 * @inode2: the inode structure 116 * @index1: folio index 117 * @index2: folio index 118 * @folio: result folio vector 119 * 120 * Grab two locked folio for inode's by inode order 121 */ 122 static int 123 mext_folio_double_lock(struct inode *inode1, struct inode *inode2, 124 pgoff_t index1, pgoff_t index2, struct folio *folio[2]) 125 { 126 struct address_space *mapping[2]; 127 unsigned int flags; 128 129 BUG_ON(!inode1 || !inode2); 130 if (inode1 < inode2) { 131 mapping[0] = inode1->i_mapping; 132 mapping[1] = inode2->i_mapping; 133 } else { 134 swap(index1, index2); 135 mapping[0] = inode2->i_mapping; 136 mapping[1] = inode1->i_mapping; 137 } 138 139 flags = memalloc_nofs_save(); 140 folio[0] = __filemap_get_folio(mapping[0], index1, FGP_WRITEBEGIN, 141 mapping_gfp_mask(mapping[0])); 142 if (IS_ERR(folio[0])) { 143 memalloc_nofs_restore(flags); 144 return PTR_ERR(folio[0]); 145 } 146 147 folio[1] = __filemap_get_folio(mapping[1], index2, FGP_WRITEBEGIN, 148 mapping_gfp_mask(mapping[1])); 149 memalloc_nofs_restore(flags); 150 if (IS_ERR(folio[1])) { 151 folio_unlock(folio[0]); 152 folio_put(folio[0]); 153 return PTR_ERR(folio[1]); 154 } 155 /* 156 * __filemap_get_folio() may not wait on folio's writeback if 157 * BDI not demand that. But it is reasonable to be very conservative 158 * here and explicitly wait on folio's writeback 159 */ 160 folio_wait_writeback(folio[0]); 161 folio_wait_writeback(folio[1]); 162 if (inode1 > inode2) 163 swap(folio[0], folio[1]); 164 165 return 0; 166 } 167 168 /* Force folio buffers uptodate w/o dropping folio's lock */ 169 static int mext_page_mkuptodate(struct folio *folio, size_t from, size_t to) 170 { 171 struct inode *inode = folio->mapping->host; 172 sector_t block; 173 struct buffer_head *bh, *head; 174 unsigned int blocksize, block_start, block_end; 175 int nr = 0; 176 bool partial = false; 177 178 BUG_ON(!folio_test_locked(folio)); 179 BUG_ON(folio_test_writeback(folio)); 180 181 if (folio_test_uptodate(folio)) 182 return 0; 183 184 blocksize = i_blocksize(inode); 185 head = folio_buffers(folio); 186 if (!head) 187 head = create_empty_buffers(folio, blocksize, 0); 188 189 block = folio_pos(folio) >> inode->i_blkbits; 190 block_end = 0; 191 bh = head; 192 do { 193 block_start = block_end; 194 block_end = block_start + blocksize; 195 if (block_end <= from || block_start >= to) { 196 if (!buffer_uptodate(bh)) 197 partial = true; 198 continue; 199 } 200 if (buffer_uptodate(bh)) 201 continue; 202 if (!buffer_mapped(bh)) { 203 int err = ext4_get_block(inode, block, bh, 0); 204 if (err) 205 return err; 206 if (!buffer_mapped(bh)) { 207 folio_zero_range(folio, block_start, blocksize); 208 set_buffer_uptodate(bh); 209 continue; 210 } 211 } 212 lock_buffer(bh); 213 if (buffer_uptodate(bh)) { 214 unlock_buffer(bh); 215 continue; 216 } 217 ext4_read_bh_nowait(bh, 0, NULL); 218 nr++; 219 } while (block++, (bh = bh->b_this_page) != head); 220 221 /* No io required */ 222 if (!nr) 223 goto out; 224 225 bh = head; 226 do { 227 if (bh_offset(bh) + blocksize <= from) 228 continue; 229 if (bh_offset(bh) > to) 230 break; 231 wait_on_buffer(bh); 232 if (buffer_uptodate(bh)) 233 continue; 234 return -EIO; 235 } while ((bh = bh->b_this_page) != head); 236 out: 237 if (!partial) 238 folio_mark_uptodate(folio); 239 return 0; 240 } 241 242 /** 243 * move_extent_per_page - Move extent data per page 244 * 245 * @o_filp: file structure of original file 246 * @donor_inode: donor inode 247 * @orig_page_offset: page index on original file 248 * @donor_page_offset: page index on donor file 249 * @data_offset_in_page: block index where data swapping starts 250 * @block_len_in_page: the number of blocks to be swapped 251 * @unwritten: orig extent is unwritten or not 252 * @err: pointer to save return value 253 * 254 * Save the data in original inode blocks and replace original inode extents 255 * with donor inode extents by calling ext4_swap_extents(). 256 * Finally, write out the saved data in new original inode blocks. Return 257 * replaced block count. 258 */ 259 static int 260 move_extent_per_page(struct file *o_filp, struct inode *donor_inode, 261 pgoff_t orig_page_offset, pgoff_t donor_page_offset, 262 int data_offset_in_page, 263 int block_len_in_page, int unwritten, int *err) 264 { 265 struct inode *orig_inode = file_inode(o_filp); 266 struct folio *folio[2] = {NULL, NULL}; 267 handle_t *handle; 268 ext4_lblk_t orig_blk_offset, donor_blk_offset; 269 unsigned long blocksize = orig_inode->i_sb->s_blocksize; 270 unsigned int tmp_data_size, data_size, replaced_size; 271 int i, err2, jblocks, retries = 0; 272 int replaced_count = 0; 273 int from = data_offset_in_page << orig_inode->i_blkbits; 274 int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits; 275 struct super_block *sb = orig_inode->i_sb; 276 struct buffer_head *bh = NULL; 277 278 /* 279 * It needs twice the amount of ordinary journal buffers because 280 * inode and donor_inode may change each different metadata blocks. 281 */ 282 again: 283 *err = 0; 284 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2; 285 handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks); 286 if (IS_ERR(handle)) { 287 *err = PTR_ERR(handle); 288 return 0; 289 } 290 291 orig_blk_offset = orig_page_offset * blocks_per_page + 292 data_offset_in_page; 293 294 donor_blk_offset = donor_page_offset * blocks_per_page + 295 data_offset_in_page; 296 297 /* Calculate data_size */ 298 if ((orig_blk_offset + block_len_in_page - 1) == 299 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) { 300 /* Replace the last block */ 301 tmp_data_size = orig_inode->i_size & (blocksize - 1); 302 /* 303 * If data_size equal zero, it shows data_size is multiples of 304 * blocksize. So we set appropriate value. 305 */ 306 if (tmp_data_size == 0) 307 tmp_data_size = blocksize; 308 309 data_size = tmp_data_size + 310 ((block_len_in_page - 1) << orig_inode->i_blkbits); 311 } else 312 data_size = block_len_in_page << orig_inode->i_blkbits; 313 314 replaced_size = data_size; 315 316 *err = mext_folio_double_lock(orig_inode, donor_inode, orig_page_offset, 317 donor_page_offset, folio); 318 if (unlikely(*err < 0)) 319 goto stop_journal; 320 /* 321 * If orig extent was unwritten it can become initialized 322 * at any time after i_data_sem was dropped, in order to 323 * serialize with delalloc we have recheck extent while we 324 * hold page's lock, if it is still the case data copy is not 325 * necessary, just swap data blocks between orig and donor. 326 */ 327 328 VM_BUG_ON_FOLIO(folio_test_large(folio[0]), folio[0]); 329 VM_BUG_ON_FOLIO(folio_test_large(folio[1]), folio[1]); 330 VM_BUG_ON_FOLIO(folio_nr_pages(folio[0]) != folio_nr_pages(folio[1]), folio[1]); 331 332 if (unwritten) { 333 ext4_double_down_write_data_sem(orig_inode, donor_inode); 334 /* If any of extents in range became initialized we have to 335 * fallback to data copying */ 336 unwritten = mext_check_coverage(orig_inode, orig_blk_offset, 337 block_len_in_page, 1, err); 338 if (*err) 339 goto drop_data_sem; 340 341 unwritten &= mext_check_coverage(donor_inode, donor_blk_offset, 342 block_len_in_page, 1, err); 343 if (*err) 344 goto drop_data_sem; 345 346 if (!unwritten) { 347 ext4_double_up_write_data_sem(orig_inode, donor_inode); 348 goto data_copy; 349 } 350 if (!filemap_release_folio(folio[0], 0) || 351 !filemap_release_folio(folio[1], 0)) { 352 *err = -EBUSY; 353 goto drop_data_sem; 354 } 355 replaced_count = ext4_swap_extents(handle, orig_inode, 356 donor_inode, orig_blk_offset, 357 donor_blk_offset, 358 block_len_in_page, 1, err); 359 drop_data_sem: 360 ext4_double_up_write_data_sem(orig_inode, donor_inode); 361 goto unlock_folios; 362 } 363 data_copy: 364 *err = mext_page_mkuptodate(folio[0], from, from + replaced_size); 365 if (*err) 366 goto unlock_folios; 367 368 /* At this point all buffers in range are uptodate, old mapping layout 369 * is no longer required, try to drop it now. */ 370 if (!filemap_release_folio(folio[0], 0) || 371 !filemap_release_folio(folio[1], 0)) { 372 *err = -EBUSY; 373 goto unlock_folios; 374 } 375 ext4_double_down_write_data_sem(orig_inode, donor_inode); 376 replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode, 377 orig_blk_offset, donor_blk_offset, 378 block_len_in_page, 1, err); 379 ext4_double_up_write_data_sem(orig_inode, donor_inode); 380 if (*err) { 381 if (replaced_count) { 382 block_len_in_page = replaced_count; 383 replaced_size = 384 block_len_in_page << orig_inode->i_blkbits; 385 } else 386 goto unlock_folios; 387 } 388 /* Perform all necessary steps similar write_begin()/write_end() 389 * but keeping in mind that i_size will not change */ 390 bh = folio_buffers(folio[0]); 391 if (!bh) 392 bh = create_empty_buffers(folio[0], 393 1 << orig_inode->i_blkbits, 0); 394 for (i = 0; i < data_offset_in_page; i++) 395 bh = bh->b_this_page; 396 for (i = 0; i < block_len_in_page; i++) { 397 *err = ext4_get_block(orig_inode, orig_blk_offset + i, bh, 0); 398 if (*err < 0) 399 goto repair_branches; 400 bh = bh->b_this_page; 401 } 402 403 block_commit_write(&folio[0]->page, from, from + replaced_size); 404 405 /* Even in case of data=writeback it is reasonable to pin 406 * inode to transaction, to prevent unexpected data loss */ 407 *err = ext4_jbd2_inode_add_write(handle, orig_inode, 408 (loff_t)orig_page_offset << PAGE_SHIFT, replaced_size); 409 410 unlock_folios: 411 folio_unlock(folio[0]); 412 folio_put(folio[0]); 413 folio_unlock(folio[1]); 414 folio_put(folio[1]); 415 stop_journal: 416 ext4_journal_stop(handle); 417 if (*err == -ENOSPC && 418 ext4_should_retry_alloc(sb, &retries)) 419 goto again; 420 /* Buffer was busy because probably is pinned to journal transaction, 421 * force transaction commit may help to free it. */ 422 if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal && 423 jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal)) 424 goto again; 425 return replaced_count; 426 427 repair_branches: 428 /* 429 * This should never ever happen! 430 * Extents are swapped already, but we are not able to copy data. 431 * Try to swap extents to it's original places 432 */ 433 ext4_double_down_write_data_sem(orig_inode, donor_inode); 434 replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode, 435 orig_blk_offset, donor_blk_offset, 436 block_len_in_page, 0, &err2); 437 ext4_double_up_write_data_sem(orig_inode, donor_inode); 438 if (replaced_count != block_len_in_page) { 439 ext4_error_inode_block(orig_inode, (sector_t)(orig_blk_offset), 440 EIO, "Unable to copy data block," 441 " data will be lost."); 442 *err = -EIO; 443 } 444 replaced_count = 0; 445 goto unlock_folios; 446 } 447 448 /** 449 * mext_check_arguments - Check whether move extent can be done 450 * 451 * @orig_inode: original inode 452 * @donor_inode: donor inode 453 * @orig_start: logical start offset in block for orig 454 * @donor_start: logical start offset in block for donor 455 * @len: the number of blocks to be moved 456 * 457 * Check the arguments of ext4_move_extents() whether the files can be 458 * exchanged with each other. 459 * Return 0 on success, or a negative error value on failure. 460 */ 461 static int 462 mext_check_arguments(struct inode *orig_inode, 463 struct inode *donor_inode, __u64 orig_start, 464 __u64 donor_start, __u64 *len) 465 { 466 __u64 orig_eof, donor_eof; 467 unsigned int blkbits = orig_inode->i_blkbits; 468 unsigned int blocksize = 1 << blkbits; 469 470 orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits; 471 donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits; 472 473 474 if (donor_inode->i_mode & (S_ISUID|S_ISGID)) { 475 ext4_debug("ext4 move extent: suid or sgid is set" 476 " to donor file [ino:orig %lu, donor %lu]\n", 477 orig_inode->i_ino, donor_inode->i_ino); 478 return -EINVAL; 479 } 480 481 if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode)) 482 return -EPERM; 483 484 /* Ext4 move extent does not support swap files */ 485 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) { 486 ext4_debug("ext4 move extent: The argument files should not be swap files [ino:orig %lu, donor %lu]\n", 487 orig_inode->i_ino, donor_inode->i_ino); 488 return -ETXTBSY; 489 } 490 491 if (ext4_is_quota_file(orig_inode) && ext4_is_quota_file(donor_inode)) { 492 ext4_debug("ext4 move extent: The argument files should not be quota files [ino:orig %lu, donor %lu]\n", 493 orig_inode->i_ino, donor_inode->i_ino); 494 return -EOPNOTSUPP; 495 } 496 497 /* Ext4 move extent supports only extent based file */ 498 if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) { 499 ext4_debug("ext4 move extent: orig file is not extents " 500 "based file [ino:orig %lu]\n", orig_inode->i_ino); 501 return -EOPNOTSUPP; 502 } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) { 503 ext4_debug("ext4 move extent: donor file is not extents " 504 "based file [ino:donor %lu]\n", donor_inode->i_ino); 505 return -EOPNOTSUPP; 506 } 507 508 if ((!orig_inode->i_size) || (!donor_inode->i_size)) { 509 ext4_debug("ext4 move extent: File size is 0 byte\n"); 510 return -EINVAL; 511 } 512 513 /* Start offset should be same */ 514 if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) != 515 (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) { 516 ext4_debug("ext4 move extent: orig and donor's start " 517 "offsets are not aligned [ino:orig %lu, donor %lu]\n", 518 orig_inode->i_ino, donor_inode->i_ino); 519 return -EINVAL; 520 } 521 522 if ((orig_start >= EXT_MAX_BLOCKS) || 523 (donor_start >= EXT_MAX_BLOCKS) || 524 (*len > EXT_MAX_BLOCKS) || 525 (donor_start + *len >= EXT_MAX_BLOCKS) || 526 (orig_start + *len >= EXT_MAX_BLOCKS)) { 527 ext4_debug("ext4 move extent: Can't handle over [%u] blocks " 528 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS, 529 orig_inode->i_ino, donor_inode->i_ino); 530 return -EINVAL; 531 } 532 if (orig_eof <= orig_start) 533 *len = 0; 534 else if (orig_eof < orig_start + *len - 1) 535 *len = orig_eof - orig_start; 536 if (donor_eof <= donor_start) 537 *len = 0; 538 else if (donor_eof < donor_start + *len - 1) 539 *len = donor_eof - donor_start; 540 if (!*len) { 541 ext4_debug("ext4 move extent: len should not be 0 " 542 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino, 543 donor_inode->i_ino); 544 return -EINVAL; 545 } 546 547 return 0; 548 } 549 550 /** 551 * ext4_move_extents - Exchange the specified range of a file 552 * 553 * @o_filp: file structure of the original file 554 * @d_filp: file structure of the donor file 555 * @orig_blk: start offset in block for orig 556 * @donor_blk: start offset in block for donor 557 * @len: the number of blocks to be moved 558 * @moved_len: moved block length 559 * 560 * This function returns 0 and moved block length is set in moved_len 561 * if succeed, otherwise returns error value. 562 * 563 */ 564 int 565 ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk, 566 __u64 donor_blk, __u64 len, __u64 *moved_len) 567 { 568 struct inode *orig_inode = file_inode(o_filp); 569 struct inode *donor_inode = file_inode(d_filp); 570 struct ext4_ext_path *path = NULL; 571 int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits; 572 ext4_lblk_t o_end, o_start = orig_blk; 573 ext4_lblk_t d_start = donor_blk; 574 int ret; 575 576 if (orig_inode->i_sb != donor_inode->i_sb) { 577 ext4_debug("ext4 move extent: The argument files " 578 "should be in same FS [ino:orig %lu, donor %lu]\n", 579 orig_inode->i_ino, donor_inode->i_ino); 580 return -EINVAL; 581 } 582 583 /* orig and donor should be different inodes */ 584 if (orig_inode == donor_inode) { 585 ext4_debug("ext4 move extent: The argument files should not " 586 "be same inode [ino:orig %lu, donor %lu]\n", 587 orig_inode->i_ino, donor_inode->i_ino); 588 return -EINVAL; 589 } 590 591 /* Regular file check */ 592 if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) { 593 ext4_debug("ext4 move extent: The argument files should be " 594 "regular file [ino:orig %lu, donor %lu]\n", 595 orig_inode->i_ino, donor_inode->i_ino); 596 return -EINVAL; 597 } 598 599 /* TODO: it's not obvious how to swap blocks for inodes with full 600 journaling enabled */ 601 if (ext4_should_journal_data(orig_inode) || 602 ext4_should_journal_data(donor_inode)) { 603 ext4_msg(orig_inode->i_sb, KERN_ERR, 604 "Online defrag not supported with data journaling"); 605 return -EOPNOTSUPP; 606 } 607 608 if (IS_ENCRYPTED(orig_inode) || IS_ENCRYPTED(donor_inode)) { 609 ext4_msg(orig_inode->i_sb, KERN_ERR, 610 "Online defrag not supported for encrypted files"); 611 return -EOPNOTSUPP; 612 } 613 614 /* Protect orig and donor inodes against a truncate */ 615 lock_two_nondirectories(orig_inode, donor_inode); 616 617 /* Wait for all existing dio workers */ 618 inode_dio_wait(orig_inode); 619 inode_dio_wait(donor_inode); 620 621 /* Protect extent tree against block allocations via delalloc */ 622 ext4_double_down_write_data_sem(orig_inode, donor_inode); 623 /* Check the filesystem environment whether move_extent can be done */ 624 ret = mext_check_arguments(orig_inode, donor_inode, orig_blk, 625 donor_blk, &len); 626 if (ret) 627 goto out; 628 o_end = o_start + len; 629 630 *moved_len = 0; 631 while (o_start < o_end) { 632 struct ext4_extent *ex; 633 ext4_lblk_t cur_blk, next_blk; 634 pgoff_t orig_page_index, donor_page_index; 635 int offset_in_page; 636 int unwritten, cur_len; 637 638 ret = get_ext_path(orig_inode, o_start, &path); 639 if (ret) 640 goto out; 641 ex = path[path->p_depth].p_ext; 642 cur_blk = le32_to_cpu(ex->ee_block); 643 cur_len = ext4_ext_get_actual_len(ex); 644 /* Check hole before the start pos */ 645 if (cur_blk + cur_len - 1 < o_start) { 646 next_blk = ext4_ext_next_allocated_block(path); 647 if (next_blk == EXT_MAX_BLOCKS) { 648 ret = -ENODATA; 649 goto out; 650 } 651 d_start += next_blk - o_start; 652 o_start = next_blk; 653 continue; 654 /* Check hole after the start pos */ 655 } else if (cur_blk > o_start) { 656 /* Skip hole */ 657 d_start += cur_blk - o_start; 658 o_start = cur_blk; 659 /* Extent inside requested range ?*/ 660 if (cur_blk >= o_end) 661 goto out; 662 } else { /* in_range(o_start, o_blk, o_len) */ 663 cur_len += cur_blk - o_start; 664 } 665 unwritten = ext4_ext_is_unwritten(ex); 666 if (o_end - o_start < cur_len) 667 cur_len = o_end - o_start; 668 669 orig_page_index = o_start >> (PAGE_SHIFT - 670 orig_inode->i_blkbits); 671 donor_page_index = d_start >> (PAGE_SHIFT - 672 donor_inode->i_blkbits); 673 offset_in_page = o_start % blocks_per_page; 674 if (cur_len > blocks_per_page - offset_in_page) 675 cur_len = blocks_per_page - offset_in_page; 676 /* 677 * Up semaphore to avoid following problems: 678 * a. transaction deadlock among ext4_journal_start, 679 * ->write_begin via pagefault, and jbd2_journal_commit 680 * b. racing with ->read_folio, ->write_begin, and 681 * ext4_get_block in move_extent_per_page 682 */ 683 ext4_double_up_write_data_sem(orig_inode, donor_inode); 684 /* Swap original branches with new branches */ 685 *moved_len += move_extent_per_page(o_filp, donor_inode, 686 orig_page_index, donor_page_index, 687 offset_in_page, cur_len, 688 unwritten, &ret); 689 ext4_double_down_write_data_sem(orig_inode, donor_inode); 690 if (ret < 0) 691 break; 692 o_start += cur_len; 693 d_start += cur_len; 694 } 695 696 out: 697 if (*moved_len) { 698 ext4_discard_preallocations(orig_inode); 699 ext4_discard_preallocations(donor_inode); 700 } 701 702 ext4_free_ext_path(path); 703 ext4_double_up_write_data_sem(orig_inode, donor_inode); 704 unlock_two_nondirectories(orig_inode, donor_inode); 705 706 return ret; 707 } 708