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