@@ -1332,15 +1332,13 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
1332
1332
u64 alloc_hint = 0 ;
1333
1333
u64 orig_start = start ;
1334
1334
u64 num_bytes ;
1335
- unsigned long ram_size ;
1336
1335
u64 cur_alloc_size = 0 ;
1337
1336
u64 min_alloc_size ;
1338
1337
u64 blocksize = fs_info -> sectorsize ;
1339
1338
struct btrfs_key ins ;
1340
1339
struct extent_map * em ;
1341
1340
unsigned clear_bits ;
1342
1341
unsigned long page_ops ;
1343
- bool extent_reserved = false;
1344
1342
int ret = 0 ;
1345
1343
1346
1344
if (btrfs_is_free_space_inode (inode )) {
@@ -1394,8 +1392,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
1394
1392
struct btrfs_ordered_extent * ordered ;
1395
1393
struct btrfs_file_extent file_extent ;
1396
1394
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 ,
1399
1396
min_alloc_size , 0 , alloc_hint ,
1400
1397
& ins , 1 , 1 );
1401
1398
if (ret == - EAGAIN ) {
@@ -1426,24 +1423,22 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
1426
1423
if (ret < 0 )
1427
1424
goto out_unlock ;
1428
1425
cur_alloc_size = ins .offset ;
1429
- extent_reserved = true;
1430
1426
1431
- ram_size = ins .offset ;
1432
1427
file_extent .disk_bytenr = ins .objectid ;
1433
1428
file_extent .disk_num_bytes = ins .offset ;
1434
1429
file_extent .num_bytes = ins .offset ;
1435
1430
file_extent .ram_bytes = ins .offset ;
1436
1431
file_extent .offset = 0 ;
1437
1432
file_extent .compression = BTRFS_COMPRESS_NONE ;
1438
1433
1439
- lock_extent (& inode -> io_tree , start , start + ram_size - 1 ,
1434
+ lock_extent (& inode -> io_tree , start , start + cur_alloc_size - 1 ,
1440
1435
& cached );
1441
1436
1442
1437
em = btrfs_create_io_em (inode , start , & file_extent ,
1443
1438
BTRFS_ORDERED_REGULAR );
1444
1439
if (IS_ERR (em )) {
1445
1440
unlock_extent (& inode -> io_tree , start ,
1446
- start + ram_size - 1 , & cached );
1441
+ start + cur_alloc_size - 1 , & cached );
1447
1442
ret = PTR_ERR (em );
1448
1443
goto out_reserve ;
1449
1444
}
@@ -1453,7 +1448,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
1453
1448
1 << BTRFS_ORDERED_REGULAR );
1454
1449
if (IS_ERR (ordered )) {
1455
1450
unlock_extent (& inode -> io_tree , start ,
1456
- start + ram_size - 1 , & cached );
1451
+ start + cur_alloc_size - 1 , & cached );
1457
1452
ret = PTR_ERR (ordered );
1458
1453
goto out_drop_extent_cache ;
1459
1454
}
@@ -1474,7 +1469,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
1474
1469
*/
1475
1470
if (ret )
1476
1471
btrfs_drop_extent_map_range (inode , start ,
1477
- start + ram_size - 1 ,
1472
+ start + cur_alloc_size - 1 ,
1478
1473
false);
1479
1474
}
1480
1475
btrfs_put_ordered_extent (ordered );
@@ -1492,7 +1487,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
1492
1487
page_ops = (keep_locked ? 0 : PAGE_UNLOCK );
1493
1488
page_ops |= PAGE_SET_ORDERED ;
1494
1489
1495
- extent_clear_unlock_delalloc (inode , start , start + ram_size - 1 ,
1490
+ extent_clear_unlock_delalloc (inode , start , start + cur_alloc_size - 1 ,
1496
1491
locked_folio , & cached ,
1497
1492
EXTENT_LOCKED | EXTENT_DELALLOC ,
1498
1493
page_ops );
@@ -1502,7 +1497,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
1502
1497
num_bytes -= cur_alloc_size ;
1503
1498
alloc_hint = ins .objectid + ins .offset ;
1504
1499
start += cur_alloc_size ;
1505
- extent_reserved = false ;
1500
+ cur_alloc_size = 0 ;
1506
1501
1507
1502
/*
1508
1503
* btrfs_reloc_clone_csums() error, since start is increased
@@ -1518,7 +1513,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
1518
1513
return ret ;
1519
1514
1520
1515
out_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);
1522
1517
out_reserve :
1523
1518
btrfs_dec_block_group_reservations (fs_info , ins .objectid );
1524
1519
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,
1572
1567
* to decrement again the data space_info's bytes_may_use counter,
1573
1568
* therefore we do not pass it the flag EXTENT_CLEAR_DATA_RESV.
1574
1569
*/
1575
- if (extent_reserved ) {
1570
+ if (cur_alloc_size ) {
1576
1571
extent_clear_unlock_delalloc (inode , start ,
1577
1572
start + cur_alloc_size - 1 ,
1578
1573
locked_folio , & cached , clear_bits ,
1579
1574
page_ops );
1580
1575
btrfs_qgroup_free_data (inode , NULL , start , cur_alloc_size , NULL );
1581
- start += cur_alloc_size ;
1582
1576
}
1583
1577
1584
1578
/*
@@ -1587,11 +1581,13 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
1587
1581
* space_info's bytes_may_use counter, reserved in
1588
1582
* btrfs_check_data_free_space().
1589
1583
*/
1590
- if (start < end ) {
1584
+ if (start + cur_alloc_size < end ) {
1591
1585
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 ,
1593
1588
& 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 );
1595
1591
}
1596
1592
return ret ;
1597
1593
}
0 commit comments