20 #include <linux/slab.h>
25 #define BTRFS_DELAYED_WRITEBACK 400
26 #define BTRFS_DELAYED_BACKGROUND 100
37 if (!delayed_node_cache)
44 if (delayed_node_cache)
48 static inline void btrfs_init_delayed_node(
52 delayed_node->
root = root;
55 delayed_node->
count = 0;
62 INIT_LIST_HEAD(&delayed_node->
n_list);
63 INIT_LIST_HEAD(&delayed_node->
p_list);
68 static inline int btrfs_is_continuous_delayed_item(
73 item1->
key.objectid == item2->
key.objectid &&
74 item1->
key.type == item2->
key.type &&
75 item1->
key.offset + 1 == item2->
key.offset)
83 return root->
fs_info->delayed_root;
90 u64 ino = btrfs_ino(inode);
124 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
126 u64 ino = btrfs_ino(inode);
130 node = btrfs_get_delayed_node(inode);
137 btrfs_init_delayed_node(node, root, ino);
153 radix_tree_preload_end();
158 radix_tree_preload_end();
172 spin_lock(&root->
lock);
174 if (!list_empty(&node->
p_list))
185 spin_unlock(&root->
lock);
192 spin_lock(&root->
lock);
196 list_del_init(&node->
n_list);
197 if (!list_empty(&node->
p_list))
198 list_del_init(&node->
p_list);
201 spin_unlock(&root->
lock);
210 spin_lock(&delayed_root->
lock);
211 if (list_empty(&delayed_root->
node_list))
218 spin_unlock(&delayed_root->
lock);
230 delayed_root = node->
root->fs_info->delayed_root;
231 spin_lock(&delayed_root->
lock);
233 if (list_empty(&delayed_root->
node_list))
244 spin_unlock(&delayed_root->
lock);
249 static void __btrfs_release_delayed_node(
258 delayed_root = delayed_node->
root->fs_info->delayed_root;
261 if (delayed_node->
count)
262 btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
264 btrfs_dequeue_delayed_node(delayed_root, delayed_node);
281 __btrfs_release_delayed_node(node, 0);
290 spin_lock(&delayed_root->
lock);
299 spin_unlock(&delayed_root->
lock);
304 static inline void btrfs_release_prepared_delayed_node(
307 __btrfs_release_delayed_node(node, 1);
363 *prev = delayed_item;
375 *next = delayed_item;
391 item = __btrfs_lookup_delayed_item(&delayed_node->
ins_root, key,
402 item = __btrfs_lookup_delayed_item(&delayed_node->
del_root, key,
413 item = __btrfs_lookup_delayed_item(&delayed_node->
ins_root, key,
427 item = __btrfs_lookup_delayed_item(&delayed_node->
del_root, key,
468 rb_link_node(node, parent_node, p);
478 delayed_node->
count++;
486 return __btrfs_add_delayed_item(node, item,
493 return __btrfs_add_delayed_item(node, item,
502 delayed_root = delayed_item->
delayed_node->root->fs_info->delayed_root;
517 waitqueue_active(&delayed_root->
wait))
524 __btrfs_remove_delayed_item(item);
596 dst_rsv = &root->
fs_info->delayed_block_rsv;
598 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
601 trace_btrfs_space_reservation(root->
fs_info,
"delayed_item",
610 static void btrfs_delayed_item_release_metadata(
struct btrfs_root *root,
618 rsv = &root->
fs_info->delayed_block_rsv;
619 trace_btrfs_space_reservation(root->
fs_info,
"delayed_item",
626 static int btrfs_delayed_inode_reserve_metadata(
639 dst_rsv = &root->
fs_info->delayed_block_rsv;
641 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
665 trace_btrfs_space_reservation(root->
fs_info,
672 spin_lock(&BTRFS_I(inode)->
lock);
674 &BTRFS_I(inode)->runtime_flags)) {
675 spin_unlock(&BTRFS_I(inode)->
lock);
679 spin_unlock(&BTRFS_I(inode)->
lock);
725 trace_btrfs_space_reservation(root->
fs_info,
"delayed_inode",
726 btrfs_ino(inode), num_bytes, 1);
731 trace_btrfs_space_reservation(root->
fs_info,
"delalloc",
732 btrfs_ino(inode), num_bytes, 0);
739 static void btrfs_delayed_inode_release_metadata(
struct btrfs_root *root,
747 rsv = &root->
fs_info->delayed_block_rsv;
748 trace_btrfs_space_reservation(root->
fs_info,
"delayed_inode",
779 leaf = path->
nodes[0];
781 INIT_LIST_HEAD(&
head);
801 if (!btrfs_is_continuous_delayed_item(curr, next))
845 slot = path->
slots[0];
849 (
unsigned long)data_ptr,
853 btrfs_delayed_item_release_metadata(root, curr);
856 btrfs_release_delayed_item(curr);
880 ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->
key,
882 if (ret < 0 && ret != -
EEXIST)
885 leaf = path->
nodes[0];
887 item = btrfs_item_nr(leaf, path->
slots[0]);
894 btrfs_delayed_item_release_metadata(root, delayed_item);
916 ret = btrfs_insert_delayed_item(trans, root, path, curr);
924 if (curr && btrfs_is_continuous_delayed_item(prev, curr)) {
927 btrfs_batch_insert_items(trans, root, path, curr);
929 btrfs_release_delayed_item(prev);
950 int nitems,
i, last_item;
955 leaf = path->
nodes[0];
958 last_item = btrfs_header_nritems(leaf) - 1;
963 INIT_LIST_HEAD(&
head);
964 btrfs_item_key_to_cpu(leaf, &key, i);
978 if (!btrfs_is_continuous_delayed_item(curr, next))
984 btrfs_item_key_to_cpu(leaf, &key, i);
995 btrfs_delayed_item_release_metadata(root, curr);
997 btrfs_release_delayed_item(curr);
1028 btrfs_release_delayed_item(prev);
1038 btrfs_batch_delete_items(trans, root, path, curr);
1056 delayed_node->
count--;
1058 delayed_root = delayed_node->
root->fs_info->delayed_root;
1061 waitqueue_active(&delayed_root->
wait))
1090 }
else if (ret < 0) {
1096 leaf = path->
nodes[0];
1104 btrfs_delayed_inode_release_metadata(root, node);
1105 btrfs_release_delayed_inode(node);
1126 bool count = (nr > 0);
1139 delayed_root = btrfs_get_delayed_root(root);
1142 while (curr_node && (!count || (count && nr--))) {
1143 curr_root = curr_node->
root;
1144 ret = btrfs_insert_delayed_items(trans, path, curr_root,
1147 ret = btrfs_delete_delayed_items(trans, path,
1148 curr_root, curr_node);
1150 ret = btrfs_update_delayed_inode(trans, curr_root,
1153 btrfs_release_delayed_node(curr_node);
1159 prev_node = curr_node;
1161 btrfs_release_delayed_node(prev_node);
1165 btrfs_release_delayed_node(curr_node);
1175 return __btrfs_run_delayed_items(trans, root, -1);
1181 return __btrfs_run_delayed_items(trans, root, nr);
1199 ret = btrfs_insert_delayed_items(trans, path, node->
root, node);
1201 ret = btrfs_delete_delayed_items(trans, path, node->
root, node);
1203 ret = btrfs_update_delayed_inode(trans, node->
root, path, node);
1211 struct inode *inode)
1220 if (!delayed_node->
count) {
1222 btrfs_release_delayed_node(delayed_node);
1227 ret = __btrfs_commit_inode_delayed_items(trans, delayed_node);
1228 btrfs_release_delayed_node(delayed_node);
1236 delayed_node =
ACCESS_ONCE(BTRFS_I(inode)->delayed_node);
1240 BTRFS_I(inode)->delayed_node =
NULL;
1241 btrfs_release_delayed_node(delayed_node);
1250 static void btrfs_async_run_delayed_node_done(
struct btrfs_work *
work)
1258 unsigned long nr = 0;
1259 int need_requeue = 0;
1270 root = delayed_node->
root;
1279 ret = btrfs_insert_delayed_items(trans, path, root, delayed_node);
1281 ret = btrfs_delete_delayed_items(trans, path, root,
1285 btrfs_update_delayed_inode(trans, root, path, delayed_node);
1312 if (delayed_node->
count)
1315 btrfs_dequeue_delayed_node(root->
fs_info->delayed_root,
1330 btrfs_release_prepared_delayed_node(delayed_node);
1349 btrfs_release_prepared_delayed_node(curr);
1356 async_node->
work.func = btrfs_async_run_delayed_node_done;
1357 async_node->
work.flags = 0;
1362 if (all || count < 4)
1371 delayed_root = btrfs_get_delayed_root(root);
1379 delayed_root = btrfs_get_delayed_root(root);
1386 ret = btrfs_wq_run_delayed_node(delayed_root, root, 1);
1398 btrfs_wq_run_delayed_node(delayed_root, root, 0);
1413 delayed_node = btrfs_get_or_create_delayed_node(dir);
1414 if (IS_ERR(delayed_node))
1415 return PTR_ERR(delayed_node);
1418 if (!delayed_item) {
1423 delayed_item->
key.objectid = btrfs_ino(dir);
1433 memcpy((
char *)(dir_item + 1), name, name_len);
1435 ret = btrfs_delayed_item_reserve_metadata(trans, root, delayed_item);
1444 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1447 "the insertion tree of the delayed node"
1448 "(root id: %llu, inode id: %llu, errno: %d)\n",
1450 (
unsigned long long)delayed_node->
root->objectid,
1451 (
unsigned long long)delayed_node->
inode_id,
1458 btrfs_release_delayed_node(delayed_node);
1462 static int btrfs_delete_delayed_insertion_item(
struct btrfs_root *root,
1475 btrfs_delayed_item_release_metadata(root, item);
1476 btrfs_release_delayed_item(item);
1490 node = btrfs_get_or_create_delayed_node(dir);
1492 return PTR_ERR(node);
1494 item_key.
objectid = btrfs_ino(dir);
1498 ret = btrfs_delete_delayed_insertion_item(root, node, &item_key);
1508 item->
key = item_key;
1510 ret = btrfs_delayed_item_reserve_metadata(trans, root, item);
1518 ret = __btrfs_add_delayed_deletion_item(node, item);
1521 "into the deletion tree of the delayed node"
1522 "(root id: %llu, inode id: %llu, errno: %d)\n",
1523 (
unsigned long long)index,
1524 (
unsigned long long)node->
root->objectid,
1525 (
unsigned long long)node->
inode_id,
1531 btrfs_release_delayed_node(node);
1548 btrfs_release_delayed_node(delayed_node);
1552 BTRFS_I(inode)->index_cnt = delayed_node->
index_cnt;
1553 btrfs_release_delayed_node(delayed_node);
1563 delayed_node = btrfs_get_delayed_node(inode);
1618 if (list_empty(del_list))
1622 if (curr->
key.offset > index)
1653 unsigned char d_type;
1655 if (list_empty(ins_list))
1666 if (curr->
key.offset < filp->
f_pos) {
1675 name = (
char *)(di + 1);
1679 btrfs_disk_key_to_cpu(&location, &di->
location);
1681 over = filldir(dirent, name, name_len, curr->
key.offset,
1716 struct inode *inode)
1718 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1719 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
1720 btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
1721 btrfs_set_stack_inode_mode(inode_item, inode->
i_mode);
1722 btrfs_set_stack_inode_nlink(inode_item, inode->
i_nlink);
1724 btrfs_set_stack_inode_generation(inode_item,
1726 btrfs_set_stack_inode_sequence(inode_item, inode->
i_version);
1727 btrfs_set_stack_inode_transid(inode_item, trans->
transid);
1728 btrfs_set_stack_inode_rdev(inode_item, inode->
i_rdev);
1729 btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->
flags);
1730 btrfs_set_stack_inode_block_group(inode_item, 0);
1732 btrfs_set_stack_timespec_sec(btrfs_inode_atime(inode_item),
1734 btrfs_set_stack_timespec_nsec(btrfs_inode_atime(inode_item),
1737 btrfs_set_stack_timespec_sec(btrfs_inode_mtime(inode_item),
1739 btrfs_set_stack_timespec_nsec(btrfs_inode_mtime(inode_item),
1742 btrfs_set_stack_timespec_sec(btrfs_inode_ctime(inode_item),
1744 btrfs_set_stack_timespec_nsec(btrfs_inode_ctime(inode_item),
1754 delayed_node = btrfs_get_delayed_node(inode);
1761 btrfs_release_delayed_node(delayed_node);
1767 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1768 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
1769 btrfs_i_size_write(inode, btrfs_stack_inode_size(inode_item));
1770 inode->
i_mode = btrfs_stack_inode_mode(inode_item);
1771 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
1773 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
1774 inode->
i_version = btrfs_stack_inode_sequence(inode_item);
1776 *rdev = btrfs_stack_inode_rdev(inode_item);
1777 BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item);
1779 tspec = btrfs_inode_atime(inode_item);
1780 inode->
i_atime.tv_sec = btrfs_stack_timespec_sec(tspec);
1781 inode->
i_atime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1783 tspec = btrfs_inode_mtime(inode_item);
1784 inode->
i_mtime.tv_sec = btrfs_stack_timespec_sec(tspec);
1785 inode->
i_mtime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1787 tspec = btrfs_inode_ctime(inode_item);
1788 inode->
i_ctime.tv_sec = btrfs_stack_timespec_sec(tspec);
1789 inode->
i_ctime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1792 BTRFS_I(inode)->index_cnt = (
u64)-1;
1795 btrfs_release_delayed_node(delayed_node);
1800 struct btrfs_root *root,
struct inode *inode)
1805 delayed_node = btrfs_get_or_create_delayed_node(inode);
1806 if (IS_ERR(delayed_node))
1807 return PTR_ERR(delayed_node);
1811 fill_stack_inode_item(trans, &delayed_node->
inode_item, inode);
1815 ret = btrfs_delayed_inode_reserve_metadata(trans, root, inode,
1820 fill_stack_inode_item(trans, &delayed_node->
inode_item, inode);
1822 delayed_node->
count++;
1826 btrfs_release_delayed_node(delayed_node);
1838 btrfs_delayed_item_release_metadata(root, curr_item);
1839 prev_item = curr_item;
1841 btrfs_release_delayed_item(prev_item);
1846 btrfs_delayed_item_release_metadata(root, curr_item);
1847 prev_item = curr_item;
1849 btrfs_release_delayed_item(prev_item);
1853 btrfs_delayed_inode_release_metadata(root, delayed_node);
1854 btrfs_release_delayed_inode(delayed_node);
1863 delayed_node = btrfs_get_delayed_node(inode);
1867 __btrfs_kill_delayed_node(delayed_node);
1868 btrfs_release_delayed_node(delayed_node);
1880 (
void **)delayed_nodes, inode_id,
1887 inode_id = delayed_nodes[n - 1]->
inode_id + 1;
1889 for (i = 0; i <
n; i++)
1893 for (i = 0; i <
n; i++) {
1894 __btrfs_kill_delayed_node(delayed_nodes[i]);
1895 btrfs_release_delayed_node(delayed_nodes[i]);
1905 delayed_root = btrfs_get_delayed_root(root);
1909 __btrfs_kill_delayed_node(curr_node);
1911 prev_node = curr_node;
1913 btrfs_release_delayed_node(prev_node);