@@ -1332,15 +1332,13 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
13321332 u64 alloc_hint = 0 ;
13331333 u64 orig_start = start ;
13341334 u64 num_bytes ;
1335- unsigned long ram_size ;
13361335 u64 cur_alloc_size = 0 ;
13371336 u64 min_alloc_size ;
13381337 u64 blocksize = fs_info -> sectorsize ;
13391338 struct btrfs_key ins ;
13401339 struct extent_map * em ;
13411340 unsigned clear_bits ;
13421341 unsigned long page_ops ;
1343- bool extent_reserved = false;
13441342 int ret = 0 ;
13451343
13461344 if (btrfs_is_free_space_inode (inode )) {
@@ -1394,8 +1392,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
13941392 struct btrfs_ordered_extent * ordered ;
13951393 struct btrfs_file_extent file_extent ;
13961394
1397- cur_alloc_size = num_bytes ;
1398- ret = btrfs_reserve_extent (root , cur_alloc_size , cur_alloc_size ,
1395+ ret = btrfs_reserve_extent (root , num_bytes , num_bytes ,
13991396 min_alloc_size , 0 , alloc_hint ,
14001397 & ins , 1 , 1 );
14011398 if (ret == - EAGAIN ) {
@@ -1426,24 +1423,22 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
14261423 if (ret < 0 )
14271424 goto out_unlock ;
14281425 cur_alloc_size = ins .offset ;
1429- extent_reserved = true;
14301426
1431- ram_size = ins .offset ;
14321427 file_extent .disk_bytenr = ins .objectid ;
14331428 file_extent .disk_num_bytes = ins .offset ;
14341429 file_extent .num_bytes = ins .offset ;
14351430 file_extent .ram_bytes = ins .offset ;
14361431 file_extent .offset = 0 ;
14371432 file_extent .compression = BTRFS_COMPRESS_NONE ;
14381433
1439- lock_extent (& inode -> io_tree , start , start + ram_size - 1 ,
1434+ lock_extent (& inode -> io_tree , start , start + cur_alloc_size - 1 ,
14401435 & cached );
14411436
14421437 em = btrfs_create_io_em (inode , start , & file_extent ,
14431438 BTRFS_ORDERED_REGULAR );
14441439 if (IS_ERR (em )) {
14451440 unlock_extent (& inode -> io_tree , start ,
1446- start + ram_size - 1 , & cached );
1441+ start + cur_alloc_size - 1 , & cached );
14471442 ret = PTR_ERR (em );
14481443 goto out_reserve ;
14491444 }
@@ -1453,7 +1448,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
14531448 1 << BTRFS_ORDERED_REGULAR );
14541449 if (IS_ERR (ordered )) {
14551450 unlock_extent (& inode -> io_tree , start ,
1456- start + ram_size - 1 , & cached );
1451+ start + cur_alloc_size - 1 , & cached );
14571452 ret = PTR_ERR (ordered );
14581453 goto out_drop_extent_cache ;
14591454 }
@@ -1474,7 +1469,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
14741469 */
14751470 if (ret )
14761471 btrfs_drop_extent_map_range (inode , start ,
1477- start + ram_size - 1 ,
1472+ start + cur_alloc_size - 1 ,
14781473 false);
14791474 }
14801475 btrfs_put_ordered_extent (ordered );
@@ -1492,7 +1487,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
14921487 page_ops = (keep_locked ? 0 : PAGE_UNLOCK );
14931488 page_ops |= PAGE_SET_ORDERED ;
14941489
1495- extent_clear_unlock_delalloc (inode , start , start + ram_size - 1 ,
1490+ extent_clear_unlock_delalloc (inode , start , start + cur_alloc_size - 1 ,
14961491 locked_folio , & cached ,
14971492 EXTENT_LOCKED | EXTENT_DELALLOC ,
14981493 page_ops );
@@ -1502,7 +1497,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
15021497 num_bytes -= cur_alloc_size ;
15031498 alloc_hint = ins .objectid + ins .offset ;
15041499 start += cur_alloc_size ;
1505- extent_reserved = false ;
1500+ cur_alloc_size = 0 ;
15061501
15071502 /*
15081503 * btrfs_reloc_clone_csums() error, since start is increased
@@ -1518,7 +1513,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
15181513 return ret ;
15191514
15201515out_drop_extent_cache :
1521- btrfs_drop_extent_map_range (inode , start , start + ram_size - 1 , false);
1516+ btrfs_drop_extent_map_range (inode , start , start + cur_alloc_size - 1 , false);
15221517out_reserve :
15231518 btrfs_dec_block_group_reservations (fs_info , ins .objectid );
15241519 btrfs_free_reserved_extent (fs_info , ins .objectid , ins .offset , 1 );
@@ -1572,13 +1567,12 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
15721567 * to decrement again the data space_info's bytes_may_use counter,
15731568 * therefore we do not pass it the flag EXTENT_CLEAR_DATA_RESV.
15741569 */
1575- if (extent_reserved ) {
1570+ if (cur_alloc_size ) {
15761571 extent_clear_unlock_delalloc (inode , start ,
15771572 start + cur_alloc_size - 1 ,
15781573 locked_folio , & cached , clear_bits ,
15791574 page_ops );
15801575 btrfs_qgroup_free_data (inode , NULL , start , cur_alloc_size , NULL );
1581- start += cur_alloc_size ;
15821576 }
15831577
15841578 /*
@@ -1587,11 +1581,13 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
15871581 * space_info's bytes_may_use counter, reserved in
15881582 * btrfs_check_data_free_space().
15891583 */
1590- if (start < end ) {
1584+ if (start + cur_alloc_size < end ) {
15911585 clear_bits |= EXTENT_CLEAR_DATA_RESV ;
1592- extent_clear_unlock_delalloc (inode , start , end , locked_folio ,
1586+ extent_clear_unlock_delalloc (inode , start + cur_alloc_size ,
1587+ end , locked_folio ,
15931588 & cached , clear_bits , page_ops );
1594- btrfs_qgroup_free_data (inode , NULL , start , end - start + 1 , NULL );
1589+ btrfs_qgroup_free_data (inode , NULL , start + cur_alloc_size ,
1590+ end - start - cur_alloc_size + 1 , NULL );
15951591 }
15961592 return ret ;
15971593}
0 commit comments