22 #include <linux/time.h>
24 #include <linux/string.h>
27 #include <linux/falloc.h>
32 #include <linux/slab.h>
69 static int __compare_inode_defrag(
struct inode_defrag *defrag1,
74 else if (defrag1->
root < defrag2->
root)
76 else if (defrag1->
ino > defrag2->
ino)
78 else if (defrag1->
ino < defrag2->
ino)
93 static void __btrfs_add_inode_defrag(
struct inode *
inode,
102 p = &root->
fs_info->defrag_inodes.rb_node;
107 ret = __compare_inode_defrag(defrag, entry);
125 rb_link_node(&defrag->
rb_node, parent, p);
142 struct btrfs_root *root = BTRFS_I(inode)->root;
149 if (btrfs_fs_closing(root->
fs_info))
158 transid = BTRFS_I(inode)->root->last_trans;
160 defrag = kzalloc(
sizeof(*defrag),
GFP_NOFS);
164 defrag->
ino = btrfs_ino(inode);
168 spin_lock(&root->
fs_info->defrag_inodes_lock);
170 __btrfs_add_inode_defrag(inode, defrag);
173 spin_unlock(&root->
fs_info->defrag_inodes_lock);
198 ret = __compare_inode_defrag(&tmp, entry);
208 while (parent && __compare_inode_defrag(&tmp, entry) > 0) {
232 int defrag_batch = 1024;
234 memset(&range, 0,
sizeof(range));
249 }
else if (root_objectid || first_ino) {
259 first_ino = defrag->
ino + 1;
260 root_objectid = defrag->
root;
263 if (btrfs_fs_closing(fs_info))
273 if (IS_ERR(inode_root))
294 if (num_defrag == defrag_batch) {
296 __btrfs_add_inode_defrag(inode, defrag);
310 __btrfs_add_inode_defrag(inode, defrag);
337 struct page **prepared_pages,
341 size_t total_copied = 0;
345 while (write_bytes > 0) {
371 if (!PageUptodate(page) && copied < count)
375 write_bytes -= copied;
376 total_copied += copied;
403 ClearPageChecked(pages[i]);
419 struct page **
pages,
size_t num_pages,
420 loff_t pos,
size_t write_bytes,
427 u64 end_of_last_block;
428 u64 end_pos = pos + write_bytes;
429 loff_t isize = i_size_read(inode);
432 num_bytes = (write_bytes + pos - start_pos +
435 end_of_last_block = start_pos + num_bytes - 1;
442 struct page *p = pages[
i];
454 i_size_write(inode, end_pos);
469 u64 len = end - start + 1;
477 if (end == (
u64)-1) {
488 if (!split || !split2)
500 if (testend && em->
start + em->
len >= start + len) {
507 len = start + len - (em->
start + em->
len);
541 testend && em->
start + em->
len > start + len) {
544 split->
start = start + len;
593 u64 *drop_end,
int drop_cache)
599 u64 ino = btrfs_ino(inode);
603 u64 extent_offset = 0;
610 int modify_tree = -1;
611 int update_refs = (root->
ref_cows || root == root->
fs_info->tree_root);
617 if (start >= BTRFS_I(inode)->disk_i_size)
623 search_start, modify_tree);
626 if (ret > 0 && path->
slots[0] > 0 && search_start == start) {
627 leaf = path->
nodes[0];
628 btrfs_item_key_to_cpu(leaf, &key, path->
slots[0] - 1);
635 leaf = path->
nodes[0];
636 if (path->
slots[0] >= btrfs_header_nritems(leaf)) {
645 leaf = path->
nodes[0];
649 btrfs_item_key_to_cpu(leaf, &key, path->
slots[0]);
656 extent_type = btrfs_file_extent_type(leaf, fi);
660 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
661 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
662 extent_offset = btrfs_file_extent_offset(leaf, fi);
664 btrfs_file_extent_num_bytes(leaf, fi);
667 btrfs_file_extent_inline_len(leaf, fi);
670 extent_end = search_start;
673 if (extent_end <= search_start) {
680 if (recow || !modify_tree) {
690 if (start > key.
offset && end < extent_end) {
694 memcpy(&new_key, &key,
sizeof(new_key));
705 leaf = path->
nodes[0];
708 btrfs_set_file_extent_num_bytes(leaf, fi,
714 extent_offset += start - key.
offset;
715 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
716 btrfs_set_file_extent_num_bytes(leaf, fi,
720 if (update_refs && disk_bytenr > 0) {
722 disk_bytenr, num_bytes, 0,
725 start - extent_offset, 0);
734 if (start <= key.
offset && end < extent_end) {
737 memcpy(&new_key, &key,
sizeof(new_key));
741 extent_offset += end - key.
offset;
742 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
743 btrfs_set_file_extent_num_bytes(leaf, fi,
746 if (update_refs && disk_bytenr > 0)
751 search_start = extent_end;
756 if (start > key.
offset && end >= extent_end) {
760 btrfs_set_file_extent_num_bytes(leaf, fi,
763 if (update_refs && disk_bytenr > 0)
765 if (end == extent_end)
776 if (start <= key.offset && end >= extent_end) {
778 del_slot = path->
slots[0];
789 extent_end =
ALIGN(extent_end,
791 }
else if (update_refs && disk_bytenr > 0) {
793 disk_bytenr, num_bytes, 0,
802 if (end == extent_end)
805 if (path->
slots[0] + 1 < btrfs_header_nritems(leaf)) {
827 if (!ret && del_nr > 0) {
834 *drop_end = found ?
min(end, extent_end) :
end;
866 btrfs_item_key_to_cpu(leaf, &
key, slot);
872 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
873 btrfs_file_extent_offset(leaf, fi) !=
key.offset - orig_offset ||
874 btrfs_file_extent_compression(leaf, fi) ||
875 btrfs_file_extent_encryption(leaf, fi) ||
876 btrfs_file_extent_other_encoding(leaf, fi))
879 extent_end =
key.offset + btrfs_file_extent_num_bytes(leaf, fi);
880 if ((*start && *start !=
key.offset) || (*end && *end != extent_end))
896 struct inode *inode,
u64 start,
u64 end)
898 struct btrfs_root *root = BTRFS_I(inode)->root;
915 u64 ino = btrfs_ino(inode);
930 if (ret > 0 && path->
slots[0] > 0)
933 leaf = path->
nodes[0];
934 btrfs_item_key_to_cpu(leaf, &key, path->
slots[0]);
938 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
940 extent_end = key.
offset + btrfs_file_extent_num_bytes(leaf, fi);
943 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
944 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
945 orig_offset = key.
offset - btrfs_file_extent_offset(leaf, fi);
946 memcpy(&new_key, &key,
sizeof(new_key));
948 if (start == key.
offset && end < extent_end) {
951 if (extent_mergeable(leaf, path->
slots[0] - 1,
952 ino, bytenr, orig_offset,
953 &other_start, &other_end)) {
958 btrfs_set_file_extent_generation(leaf, fi,
960 btrfs_set_file_extent_num_bytes(leaf, fi,
962 btrfs_set_file_extent_offset(leaf, fi,
966 btrfs_set_file_extent_generation(leaf, fi,
968 btrfs_set_file_extent_num_bytes(leaf, fi,
975 if (start > key.
offset && end == extent_end) {
978 if (extent_mergeable(leaf, path->
slots[0] + 1,
979 ino, bytenr, orig_offset,
980 &other_start, &other_end)) {
983 btrfs_set_file_extent_num_bytes(leaf, fi,
985 btrfs_set_file_extent_generation(leaf, fi,
993 btrfs_set_file_extent_generation(leaf, fi,
995 btrfs_set_file_extent_num_bytes(leaf, fi,
997 btrfs_set_file_extent_offset(leaf, fi,
998 start - orig_offset);
1004 while (start > key.
offset || end < extent_end) {
1019 leaf = path->
nodes[0];
1022 btrfs_set_file_extent_generation(leaf, fi, trans->
transid);
1023 btrfs_set_file_extent_num_bytes(leaf, fi,
1029 btrfs_set_file_extent_generation(leaf, fi, trans->
transid);
1030 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1031 btrfs_set_file_extent_num_bytes(leaf, fi,
1032 extent_end - split);
1037 ino, orig_offset, 0);
1040 if (split == start) {
1052 if (extent_mergeable(leaf, path->
slots[0] + 1,
1053 ino, bytenr, orig_offset,
1054 &other_start, &other_end)) {
1059 extent_end = other_end;
1060 del_slot = path->
slots[0] + 1;
1064 ino, orig_offset, 0);
1069 if (extent_mergeable(leaf, path->
slots[0] - 1,
1070 ino, bytenr, orig_offset,
1071 &other_start, &other_end)) {
1076 key.
offset = other_start;
1077 del_slot = path->
slots[0];
1081 ino, orig_offset, 0);
1087 btrfs_set_file_extent_type(leaf, fi,
1089 btrfs_set_file_extent_generation(leaf, fi, trans->
transid);
1094 btrfs_set_file_extent_type(leaf, fi,
1096 btrfs_set_file_extent_generation(leaf, fi, trans->
transid);
1097 btrfs_set_file_extent_num_bytes(leaf, fi,
1098 extent_end - key.
offset);
1116 static int prepare_uptodate_page(
struct page *page,
u64 pos,
1117 bool force_uptodate)
1122 !PageUptodate(page)) {
1127 if (!PageUptodate(page)) {
1141 struct page **
pages,
size_t num_pages,
1142 loff_t pos,
unsigned long first_index,
1143 size_t write_bytes,
bool force_uptodate)
1148 struct inode *inode = fdentry(file)->d_inode;
1169 err = prepare_uptodate_page(pages[i], pos,
1171 if (i == num_pages - 1)
1172 err = prepare_uptodate_page(pages[i],
1173 pos + write_bytes,
false);
1179 wait_on_page_writeback(pages[i]);
1182 if (start_pos < inode->
i_size) {
1185 start_pos, last_pos - 1, 0, &cached_state);
1193 start_pos, last_pos - 1,
1200 last_pos - start_pos);
1211 start_pos, last_pos - 1, &cached_state,
1218 WARN_ON(!PageLocked(pages[i]));
1222 while (faili >= 0) {
1235 struct inode *inode = fdentry(file)->d_inode;
1236 struct btrfs_root *root = BTRFS_I(inode)->root;
1237 struct page **pages =
NULL;
1238 unsigned long first_index;
1242 bool force_page_uptodate =
false;
1246 (
sizeof(
struct page *)));
1248 nrptrs =
max(nrptrs, 8);
1255 while (iov_iter_count(i) > 0) {
1257 size_t write_bytes =
min(iov_iter_count(i),
1260 size_t num_pages = (write_bytes + offset +
1277 num_pages << PAGE_CACHE_SHIFT);
1286 ret = prepare_pages(root, file, pages, num_pages,
1287 pos, first_index, write_bytes,
1288 force_page_uptodate);
1291 num_pages << PAGE_CACHE_SHIFT);
1295 copied = btrfs_copy_from_user(pos, num_pages,
1296 write_bytes, pages, i);
1302 if (copied < write_bytes)
1306 force_page_uptodate =
true;
1309 force_page_uptodate =
false;
1310 dirty_pages = (copied + offset +
1322 if (num_pages > dirty_pages) {
1324 spin_lock(&BTRFS_I(inode)->lock);
1325 BTRFS_I(inode)->outstanding_extents++;
1326 spin_unlock(&BTRFS_I(inode)->lock);
1329 (num_pages - dirty_pages) <<
1335 dirty_pages, pos, copied,
1339 dirty_pages << PAGE_CACHE_SHIFT);
1351 if (dirty_pages < (root->
leafsize >> PAGE_CACHE_SHIFT) + 1)
1355 num_written += copied;
1360 return num_written ? num_written :
ret;
1364 const struct iovec *iov,
1365 unsigned long nr_segs, loff_t pos,
1366 loff_t *ppos,
size_t count,
size_t ocount)
1368 struct file *file = iocb->
ki_filp;
1378 if (written < 0 || written == count)
1383 iov_iter_init(&i, iov, nr_segs, count, written);
1384 written_buffered = __btrfs_buffered_write(file, &i, pos);
1385 if (written_buffered < 0) {
1386 err = written_buffered;
1389 endbyte = pos + written_buffered - 1;
1393 written += written_buffered;
1394 *ppos = pos + written_buffered;
1396 endbyte >> PAGE_CACHE_SHIFT);
1398 return written ? written :
err;
1401 static ssize_t btrfs_file_aio_write(
struct kiocb *iocb,
1402 const struct iovec *iov,
1403 unsigned long nr_segs, loff_t pos)
1405 struct file *file = iocb->
ki_filp;
1406 struct inode *inode = fdentry(file)->d_inode;
1407 struct btrfs_root *root = BTRFS_I(inode)->root;
1408 loff_t *ppos = &iocb->
ki_pos;
1412 size_t count, ocount;
1414 sb_start_write(inode->
i_sb);
1462 if (start_pos > i_size_read(inode)) {
1471 num_written = __btrfs_direct_write(iocb, iov, nr_segs,
1472 pos, ppos, count, ocount);
1476 iov_iter_init(&i, iov, nr_segs, count, num_written);
1478 num_written = __btrfs_buffered_write(file, &i, pos);
1479 if (num_written > 0)
1497 BTRFS_I(inode)->last_trans = root->
fs_info->generation + 1;
1498 if (num_written > 0 || num_written == -
EIOCBQUEUED) {
1500 if (err < 0 && num_written > 0)
1504 sb_end_write(inode->
i_sb);
1506 return num_written ? num_written :
err;
1518 &BTRFS_I(inode)->runtime_flags)) {
1542 struct inode *inode = dentry->
d_inode;
1543 struct btrfs_root *root = BTRFS_I(inode)->root;
1547 trace_btrfs_sync_file(file, datasync);
1572 if (!BTRFS_I(inode)->last_trans) {
1583 if (btrfs_inode_in_log(inode, root->
fs_info->generation) ||
1584 BTRFS_I(inode)->last_trans <=
1585 root->
fs_info->last_trans_committed) {
1586 BTRFS_I(inode)->last_trans = 0;
1594 &BTRFS_I(inode)->runtime_flags);
1606 if (IS_ERR(trans)) {
1607 ret = PTR_ERR(trans);
1644 return ret > 0 ? -
EIO :
ret;
1647 static const struct vm_operations_struct btrfs_file_vm_ops = {
1653 static int btrfs_file_mmap(
struct file *filp,
struct vm_area_struct *vma)
1657 if (!mapping->
a_ops->readpage)
1660 file_accessed(filp);
1661 vma->
vm_ops = &btrfs_file_vm_ops;
1666 static int hole_mergeable(
struct inode *inode,
struct extent_buffer *leaf,
1667 int slot,
u64 start,
u64 end)
1675 btrfs_item_key_to_cpu(leaf, &
key, slot);
1676 if (
key.objectid != btrfs_ino(inode) ||
1685 if (btrfs_file_extent_disk_bytenr(leaf, fi))
1688 if (
key.offset == end)
1690 if (
key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
1698 struct btrfs_root *root = BTRFS_I(inode)->root;
1706 key.objectid = btrfs_ino(inode);
1716 leaf = path->
nodes[0];
1717 if (hole_mergeable(inode, leaf, path->
slots[0]-1, offset, end)) {
1723 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
1725 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1726 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1727 btrfs_set_file_extent_offset(leaf, fi, 0);
1732 if (hole_mergeable(inode, leaf, path->
slots[0]+1, offset, end)) {
1740 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
1742 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1743 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1744 btrfs_set_file_extent_offset(leaf, fi, 0);
1751 0, 0, end - offset, 0, end - offset,
1763 &BTRFS_I(inode)->runtime_flags);
1771 hole_em->
bdev = root->
fs_info->fs_devices->latest_bdev;
1780 list_move(&hole_em->
list,
1783 }
while (ret == -
EEXIST);
1787 &BTRFS_I(inode)->runtime_flags);
1793 static int btrfs_punch_hole(
struct inode *inode, loff_t offset, loff_t len)
1795 struct btrfs_root *root = BTRFS_I(inode)->root;
1800 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
1801 u64 lockstart = (offset +
mask) & ~mask;
1802 u64 lockend = ((offset + len) & ~mask) - 1;
1803 u64 cur_offset = lockstart;
1804 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
1815 if (offset >= inode->
i_size) {
1844 if (lockend < lockstart) {
1878 lockend - lockstart + 1);
1892 rsv->
size = btrfs_calc_trunc_metadata_size(root, 1);
1901 if (IS_ERR(trans)) {
1902 err = PTR_ERR(trans);
1911 while (cur_offset < lockend) {
1913 cur_offset, lockend + 1,
1920 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
1926 cur_offset = drop_end;
1939 if (IS_ERR(trans)) {
1940 ret = PTR_ERR(trans);
1957 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
1984 static long btrfs_fallocate(
struct file *file,
int mode,
1985 loff_t offset, loff_t len)
1987 struct inode *inode = file->
f_path.dentry->d_inode;
1995 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
1999 alloc_start = offset & ~mask;
2000 alloc_end = (offset + len +
mask) & ~mask;
2007 return btrfs_punch_hole(inode, offset, len);
2028 if (alloc_start > inode->
i_size) {
2035 locked_end = alloc_end - 1;
2043 locked_end, 0, &cached_state);
2051 alloc_start, locked_end,
2058 alloc_end - alloc_start);
2066 cur_offset = alloc_start;
2071 alloc_end - cur_offset, 0);
2072 if (IS_ERR_OR_NULL(em)) {
2079 last_byte =
min(extent_map_end(em), alloc_end);
2080 actual_end =
min_t(
u64, extent_map_end(em), offset + len);
2081 last_byte = (last_byte +
mask) & ~mask;
2084 (cur_offset >= inode->
i_size &&
2087 last_byte - cur_offset,
2096 }
else if (actual_end > inode->
i_size &&
2104 i_size_write(inode, actual_end);
2109 cur_offset = last_byte;
2110 if (cur_offset >= alloc_end) {
2124 static int find_desired_extent(
struct inode *inode, loff_t *offset,
int origin)
2126 struct btrfs_root *root = BTRFS_I(inode)->root;
2130 u64 lockend = i_size_read(inode);
2133 u64 len = i_size_read(inode);
2138 if (lockend <= lockstart)
2141 len = lockend - lockstart + 1;
2158 if (origin ==
SEEK_HOLE && start != 0) {
2185 if (last_end <= orig_start) {
2200 if (start >= inode->
i_size) {
2235 static loff_t btrfs_file_llseek(
struct file *file, loff_t offset,
int origin)
2237 struct inode *inode = file->
f_mapping->host;
2248 if (offset >= i_size_read(inode)) {
2253 ret = find_desired_extent(inode, &offset, origin);
2264 if (offset > inode->
i_sb->s_maxbytes) {
2270 if (offset != file->
f_pos) {
2280 .llseek = btrfs_file_llseek,
2285 .aio_write = btrfs_file_aio_write,
2286 .mmap = btrfs_file_mmap,
2290 .fallocate = btrfs_fallocate,
2292 #ifdef CONFIG_COMPAT