aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/file.c')
-rw-r--r--fs/btrfs/file.c227
1 files changed, 170 insertions, 57 deletions
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 4205ba752d40..8e686a427ce2 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -309,10 +309,6 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
309 ret = PTR_ERR(inode_root); 309 ret = PTR_ERR(inode_root);
310 goto cleanup; 310 goto cleanup;
311 } 311 }
312 if (btrfs_root_refs(&inode_root->root_item) == 0) {
313 ret = -ENOENT;
314 goto cleanup;
315 }
316 312
317 key.objectid = defrag->ino; 313 key.objectid = defrag->ino;
318 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 314 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
@@ -600,20 +596,29 @@ void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
600 if (no_splits) 596 if (no_splits)
601 goto next; 597 goto next;
602 598
603 if (em->block_start < EXTENT_MAP_LAST_BYTE && 599 if (em->start < start) {
604 em->start < start) {
605 split->start = em->start; 600 split->start = em->start;
606 split->len = start - em->start; 601 split->len = start - em->start;
607 split->orig_start = em->orig_start;
608 split->block_start = em->block_start;
609 602
610 if (compressed) 603 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
611 split->block_len = em->block_len; 604 split->orig_start = em->orig_start;
612 else 605 split->block_start = em->block_start;
613 split->block_len = split->len; 606
614 split->ram_bytes = em->ram_bytes; 607 if (compressed)
615 split->orig_block_len = max(split->block_len, 608 split->block_len = em->block_len;
616 em->orig_block_len); 609 else
610 split->block_len = split->len;
611 split->orig_block_len = max(split->block_len,
612 em->orig_block_len);
613 split->ram_bytes = em->ram_bytes;
614 } else {
615 split->orig_start = split->start;
616 split->block_len = 0;
617 split->block_start = em->block_start;
618 split->orig_block_len = 0;
619 split->ram_bytes = split->len;
620 }
621
617 split->generation = gen; 622 split->generation = gen;
618 split->bdev = em->bdev; 623 split->bdev = em->bdev;
619 split->flags = flags; 624 split->flags = flags;
@@ -624,8 +629,7 @@ void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
624 split = split2; 629 split = split2;
625 split2 = NULL; 630 split2 = NULL;
626 } 631 }
627 if (em->block_start < EXTENT_MAP_LAST_BYTE && 632 if (testend && em->start + em->len > start + len) {
628 testend && em->start + em->len > start + len) {
629 u64 diff = start + len - em->start; 633 u64 diff = start + len - em->start;
630 634
631 split->start = start + len; 635 split->start = start + len;
@@ -634,18 +638,28 @@ void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
634 split->flags = flags; 638 split->flags = flags;
635 split->compress_type = em->compress_type; 639 split->compress_type = em->compress_type;
636 split->generation = gen; 640 split->generation = gen;
637 split->orig_block_len = max(em->block_len, 641
642 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
643 split->orig_block_len = max(em->block_len,
638 em->orig_block_len); 644 em->orig_block_len);
639 split->ram_bytes = em->ram_bytes;
640 645
641 if (compressed) { 646 split->ram_bytes = em->ram_bytes;
642 split->block_len = em->block_len; 647 if (compressed) {
643 split->block_start = em->block_start; 648 split->block_len = em->block_len;
644 split->orig_start = em->orig_start; 649 split->block_start = em->block_start;
650 split->orig_start = em->orig_start;
651 } else {
652 split->block_len = split->len;
653 split->block_start = em->block_start
654 + diff;
655 split->orig_start = em->orig_start;
656 }
645 } else { 657 } else {
646 split->block_len = split->len; 658 split->ram_bytes = split->len;
647 split->block_start = em->block_start + diff; 659 split->orig_start = split->start;
648 split->orig_start = em->orig_start; 660 split->block_len = 0;
661 split->block_start = em->block_start;
662 split->orig_block_len = 0;
649 } 663 }
650 664
651 ret = add_extent_mapping(em_tree, split, modified); 665 ret = add_extent_mapping(em_tree, split, modified);
@@ -1317,6 +1331,56 @@ fail:
1317 1331
1318} 1332}
1319 1333
1334static noinline int check_can_nocow(struct inode *inode, loff_t pos,
1335 size_t *write_bytes)
1336{
1337 struct btrfs_trans_handle *trans;
1338 struct btrfs_root *root = BTRFS_I(inode)->root;
1339 struct btrfs_ordered_extent *ordered;
1340 u64 lockstart, lockend;
1341 u64 num_bytes;
1342 int ret;
1343
1344 lockstart = round_down(pos, root->sectorsize);
1345 lockend = lockstart + round_up(*write_bytes, root->sectorsize) - 1;
1346
1347 while (1) {
1348 lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1349 ordered = btrfs_lookup_ordered_range(inode, lockstart,
1350 lockend - lockstart + 1);
1351 if (!ordered) {
1352 break;
1353 }
1354 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1355 btrfs_start_ordered_extent(inode, ordered, 1);
1356 btrfs_put_ordered_extent(ordered);
1357 }
1358
1359 trans = btrfs_join_transaction(root);
1360 if (IS_ERR(trans)) {
1361 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1362 return PTR_ERR(trans);
1363 }
1364
1365 num_bytes = lockend - lockstart + 1;
1366 ret = can_nocow_extent(trans, inode, lockstart, &num_bytes, NULL, NULL,
1367 NULL);
1368 btrfs_end_transaction(trans, root);
1369 if (ret <= 0) {
1370 ret = 0;
1371 } else {
1372 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
1373 EXTENT_DIRTY | EXTENT_DELALLOC |
1374 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1375 NULL, GFP_NOFS);
1376 *write_bytes = min_t(size_t, *write_bytes, num_bytes);
1377 }
1378
1379 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1380
1381 return ret;
1382}
1383
1320static noinline ssize_t __btrfs_buffered_write(struct file *file, 1384static noinline ssize_t __btrfs_buffered_write(struct file *file,
1321 struct iov_iter *i, 1385 struct iov_iter *i,
1322 loff_t pos) 1386 loff_t pos)
@@ -1324,10 +1388,12 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1324 struct inode *inode = file_inode(file); 1388 struct inode *inode = file_inode(file);
1325 struct btrfs_root *root = BTRFS_I(inode)->root; 1389 struct btrfs_root *root = BTRFS_I(inode)->root;
1326 struct page **pages = NULL; 1390 struct page **pages = NULL;
1391 u64 release_bytes = 0;
1327 unsigned long first_index; 1392 unsigned long first_index;
1328 size_t num_written = 0; 1393 size_t num_written = 0;
1329 int nrptrs; 1394 int nrptrs;
1330 int ret = 0; 1395 int ret = 0;
1396 bool only_release_metadata = false;
1331 bool force_page_uptodate = false; 1397 bool force_page_uptodate = false;
1332 1398
1333 nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) / 1399 nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
@@ -1348,6 +1414,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1348 offset); 1414 offset);
1349 size_t num_pages = (write_bytes + offset + 1415 size_t num_pages = (write_bytes + offset +
1350 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1416 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1417 size_t reserve_bytes;
1351 size_t dirty_pages; 1418 size_t dirty_pages;
1352 size_t copied; 1419 size_t copied;
1353 1420
@@ -1362,11 +1429,41 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1362 break; 1429 break;
1363 } 1430 }
1364 1431
1365 ret = btrfs_delalloc_reserve_space(inode, 1432 reserve_bytes = num_pages << PAGE_CACHE_SHIFT;
1366 num_pages << PAGE_CACHE_SHIFT); 1433 ret = btrfs_check_data_free_space(inode, reserve_bytes);
1434 if (ret == -ENOSPC &&
1435 (BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW |
1436 BTRFS_INODE_PREALLOC))) {
1437 ret = check_can_nocow(inode, pos, &write_bytes);
1438 if (ret > 0) {
1439 only_release_metadata = true;
1440 /*
1441 * our prealloc extent may be smaller than
1442 * write_bytes, so scale down.
1443 */
1444 num_pages = (write_bytes + offset +
1445 PAGE_CACHE_SIZE - 1) >>
1446 PAGE_CACHE_SHIFT;
1447 reserve_bytes = num_pages << PAGE_CACHE_SHIFT;
1448 ret = 0;
1449 } else {
1450 ret = -ENOSPC;
1451 }
1452 }
1453
1367 if (ret) 1454 if (ret)
1368 break; 1455 break;
1369 1456
1457 ret = btrfs_delalloc_reserve_metadata(inode, reserve_bytes);
1458 if (ret) {
1459 if (!only_release_metadata)
1460 btrfs_free_reserved_data_space(inode,
1461 reserve_bytes);
1462 break;
1463 }
1464
1465 release_bytes = reserve_bytes;
1466
1370 /* 1467 /*
1371 * This is going to setup the pages array with the number of 1468 * This is going to setup the pages array with the number of
1372 * pages we want, so we don't really need to worry about the 1469 * pages we want, so we don't really need to worry about the
@@ -1375,11 +1472,8 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1375 ret = prepare_pages(root, file, pages, num_pages, 1472 ret = prepare_pages(root, file, pages, num_pages,
1376 pos, first_index, write_bytes, 1473 pos, first_index, write_bytes,
1377 force_page_uptodate); 1474 force_page_uptodate);
1378 if (ret) { 1475 if (ret)
1379 btrfs_delalloc_release_space(inode,
1380 num_pages << PAGE_CACHE_SHIFT);
1381 break; 1476 break;
1382 }
1383 1477
1384 copied = btrfs_copy_from_user(pos, num_pages, 1478 copied = btrfs_copy_from_user(pos, num_pages,
1385 write_bytes, pages, i); 1479 write_bytes, pages, i);
@@ -1409,30 +1503,46 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1409 * managed to copy. 1503 * managed to copy.
1410 */ 1504 */
1411 if (num_pages > dirty_pages) { 1505 if (num_pages > dirty_pages) {
1506 release_bytes = (num_pages - dirty_pages) <<
1507 PAGE_CACHE_SHIFT;
1412 if (copied > 0) { 1508 if (copied > 0) {
1413 spin_lock(&BTRFS_I(inode)->lock); 1509 spin_lock(&BTRFS_I(inode)->lock);
1414 BTRFS_I(inode)->outstanding_extents++; 1510 BTRFS_I(inode)->outstanding_extents++;
1415 spin_unlock(&BTRFS_I(inode)->lock); 1511 spin_unlock(&BTRFS_I(inode)->lock);
1416 } 1512 }
1417 btrfs_delalloc_release_space(inode, 1513 if (only_release_metadata)
1418 (num_pages - dirty_pages) << 1514 btrfs_delalloc_release_metadata(inode,
1419 PAGE_CACHE_SHIFT); 1515 release_bytes);
1516 else
1517 btrfs_delalloc_release_space(inode,
1518 release_bytes);
1420 } 1519 }
1421 1520
1521 release_bytes = dirty_pages << PAGE_CACHE_SHIFT;
1422 if (copied > 0) { 1522 if (copied > 0) {
1423 ret = btrfs_dirty_pages(root, inode, pages, 1523 ret = btrfs_dirty_pages(root, inode, pages,
1424 dirty_pages, pos, copied, 1524 dirty_pages, pos, copied,
1425 NULL); 1525 NULL);
1426 if (ret) { 1526 if (ret) {
1427 btrfs_delalloc_release_space(inode,
1428 dirty_pages << PAGE_CACHE_SHIFT);
1429 btrfs_drop_pages(pages, num_pages); 1527 btrfs_drop_pages(pages, num_pages);
1430 break; 1528 break;
1431 } 1529 }
1432 } 1530 }
1433 1531
1532 release_bytes = 0;
1434 btrfs_drop_pages(pages, num_pages); 1533 btrfs_drop_pages(pages, num_pages);
1435 1534
1535 if (only_release_metadata && copied > 0) {
1536 u64 lockstart = round_down(pos, root->sectorsize);
1537 u64 lockend = lockstart +
1538 (dirty_pages << PAGE_CACHE_SHIFT) - 1;
1539
1540 set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
1541 lockend, EXTENT_NORESERVE, NULL,
1542 NULL, GFP_NOFS);
1543 only_release_metadata = false;
1544 }
1545
1436 cond_resched(); 1546 cond_resched();
1437 1547
1438 balance_dirty_pages_ratelimited(inode->i_mapping); 1548 balance_dirty_pages_ratelimited(inode->i_mapping);
@@ -1445,6 +1555,13 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1445 1555
1446 kfree(pages); 1556 kfree(pages);
1447 1557
1558 if (release_bytes) {
1559 if (only_release_metadata)
1560 btrfs_delalloc_release_metadata(inode, release_bytes);
1561 else
1562 btrfs_delalloc_release_space(inode, release_bytes);
1563 }
1564
1448 return num_written ? num_written : ret; 1565 return num_written ? num_written : ret;
1449} 1566}
1450 1567
@@ -2175,12 +2292,6 @@ static long btrfs_fallocate(struct file *file, int mode,
2175 goto out_reserve_fail; 2292 goto out_reserve_fail;
2176 } 2293 }
2177 2294
2178 /*
2179 * wait for ordered IO before we have any locks. We'll loop again
2180 * below with the locks held.
2181 */
2182 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
2183
2184 mutex_lock(&inode->i_mutex); 2295 mutex_lock(&inode->i_mutex);
2185 ret = inode_newsize_ok(inode, alloc_end); 2296 ret = inode_newsize_ok(inode, alloc_end);
2186 if (ret) 2297 if (ret)
@@ -2191,8 +2302,23 @@ static long btrfs_fallocate(struct file *file, int mode,
2191 alloc_start); 2302 alloc_start);
2192 if (ret) 2303 if (ret)
2193 goto out; 2304 goto out;
2305 } else {
2306 /*
2307 * If we are fallocating from the end of the file onward we
2308 * need to zero out the end of the page if i_size lands in the
2309 * middle of a page.
2310 */
2311 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
2312 if (ret)
2313 goto out;
2194 } 2314 }
2195 2315
2316 /*
2317 * wait for ordered IO before we have any locks. We'll loop again
2318 * below with the locks held.
2319 */
2320 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
2321
2196 locked_end = alloc_end - 1; 2322 locked_end = alloc_end - 1;
2197 while (1) { 2323 while (1) {
2198 struct btrfs_ordered_extent *ordered; 2324 struct btrfs_ordered_extent *ordered;
@@ -2425,20 +2551,7 @@ static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
2425 } 2551 }
2426 } 2552 }
2427 2553
2428 if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) { 2554 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
2429 offset = -EINVAL;
2430 goto out;
2431 }
2432 if (offset > inode->i_sb->s_maxbytes) {
2433 offset = -EINVAL;
2434 goto out;
2435 }
2436
2437 /* Special lock needed here? */
2438 if (offset != file->f_pos) {
2439 file->f_pos = offset;
2440 file->f_version = 0;
2441 }
2442out: 2555out:
2443 mutex_unlock(&inode->i_mutex); 2556 mutex_unlock(&inode->i_mutex);
2444 return offset; 2557 return offset;