if (node) { if (btrfs_inode->delayed_node) {
refcount_inc(&node->refs); /* can be accessed */
BUG_ON(btrfs_inode->delayed_node != node);
xa_unlock(&root->delayed_nodes); return node;
}
/* * It's possible that we're racing into the middle of removing * this node from the xarray. In this case, the refcount * was zero and it should never go back to one. Just return * NULL like it was never in the xarray at all; our release * function is in the process of removing it. * * Some implementations of refcount_inc refuse to bump the * refcount once it has hit zero. If we don't do this dance * here, refcount_inc() may decide to just WARN_ONCE() instead * of actually bumping the refcount. * * If this node is properly in the xarray, we want to bump the * refcount twice, once for the inode and once for this get * operation.
*/ if (refcount_inc_not_zero(&node->refs)) {
refcount_inc(&node->refs);
btrfs_inode->delayed_node = node;
} else {
node = NULL;
}
/* * Look up an existing delayed node associated with @btrfs_inode or create a new * one and insert it to the delayed nodes of the root. * * Return the delayed node, or error pointer on failure.
*/ staticstruct btrfs_delayed_node *btrfs_get_or_create_delayed_node( struct btrfs_inode *btrfs_inode)
{ struct btrfs_delayed_node *node; struct btrfs_root *root = btrfs_inode->root;
u64 ino = btrfs_ino(btrfs_inode); int ret; void *ptr;
again:
node = btrfs_get_delayed_node(btrfs_inode); if (node) return node;
/* Cached in the inode and can be accessed. */
refcount_set(&node->refs, 2);
/* Allocate and reserve the slot, from now it can return a NULL from xa_load(). */
ret = xa_reserve(&root->delayed_nodes, ino, GFP_NOFS); if (ret == -ENOMEM) {
kmem_cache_free(delayed_node_cache, node); return ERR_PTR(-ENOMEM);
}
xa_lock(&root->delayed_nodes);
ptr = xa_load(&root->delayed_nodes, ino); if (ptr) { /* Somebody inserted it, go back and read it. */
xa_unlock(&root->delayed_nodes);
kmem_cache_free(delayed_node_cache, node);
node = NULL; goto again;
}
ptr = __xa_store(&root->delayed_nodes, ino, node, GFP_ATOMIC);
ASSERT(xa_err(ptr) != -EINVAL);
ASSERT(xa_err(ptr) != -ENOMEM);
ASSERT(ptr == NULL);
btrfs_inode->delayed_node = node;
xa_unlock(&root->delayed_nodes);
return node;
}
/* * Call it when holding delayed_node->mutex * * If mod = 1, add this node into the prepared list.
*/ staticvoid btrfs_queue_delayed_node(struct btrfs_delayed_root *root, struct btrfs_delayed_node *node, int mod)
{
spin_lock(&root->lock); if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { if (!list_empty(&node->p_list))
list_move_tail(&node->p_list, &root->prepare_list); elseif (mod)
list_add_tail(&node->p_list, &root->prepare_list);
} else {
list_add_tail(&node->n_list, &root->node_list);
list_add_tail(&node->p_list, &root->prepare_list);
refcount_inc(&node->refs); /* inserted into list */
root->nodes++;
set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
}
spin_unlock(&root->lock);
}
/* Call it when holding delayed_node->mutex */ staticvoid btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root, struct btrfs_delayed_node *node)
{
spin_lock(&root->lock); if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
root->nodes--;
refcount_dec(&node->refs); /* not in the list */
list_del_init(&node->n_list); if (!list_empty(&node->p_list))
list_del_init(&node->p_list);
clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
}
spin_unlock(&root->lock);
}
delayed_root = node->root->fs_info->delayed_root;
spin_lock(&delayed_root->lock); if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { /* not in the list */ if (list_empty(&delayed_root->node_list)) goto out;
p = delayed_root->node_list.next;
} elseif (list_is_last(&node->n_list, &delayed_root->node_list)) goto out; else
p = node->n_list.next;
next = list_entry(p, struct btrfs_delayed_node, n_list);
refcount_inc(&next->refs);
out:
spin_unlock(&delayed_root->lock);
mutex_lock(&delayed_node->mutex); if (delayed_node->count)
btrfs_queue_delayed_node(delayed_root, delayed_node, mod); else
btrfs_dequeue_delayed_node(delayed_root, delayed_node);
mutex_unlock(&delayed_node->mutex);
if (refcount_dec_and_test(&delayed_node->refs)) { struct btrfs_root *root = delayed_node->root;
xa_erase(&root->delayed_nodes, delayed_node->inode_id); /* * Once our refcount goes to zero, nobody is allowed to bump it * back up. We can delete it now.
*/
ASSERT(refcount_read(&delayed_node->refs) == 0);
kmem_cache_free(delayed_node_cache, delayed_node);
}
}
/* * Look up the delayed item by key. * * @delayed_node: pointer to the delayed node * @index: the dir index value to lookup (offset of a dir index key) * * Note: if we don't find the right item, we will return the prev item and * the next item.
*/ staticstruct btrfs_delayed_item *__btrfs_lookup_delayed_item( struct rb_root *root,
u64 index)
{ struct rb_node *node;
/* * Here we migrate space rsv from transaction rsv, since have already * reserved space when starting a transaction. So no need to reserve * qgroup space here.
*/
ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true); if (!ret) {
trace_btrfs_space_reservation(fs_info, "delayed_item",
item->delayed_node->inode_id,
num_bytes, 1); /* * For insertions we track reserved metadata space by accounting * for the number of leaves that will be used, based on the delayed * node's curr_index_batch_size and index_item_leaves fields.
*/ if (item->type == BTRFS_DELAYED_DELETION_ITEM)
item->bytes_reserved = num_bytes;
}
/* * btrfs_dirty_inode will update the inode under btrfs_join_transaction * which doesn't reserve space for speed. This is a problem since we * still need to reserve space for this update, so try to reserve the * space. * * Now if src_rsv == delalloc_block_rsv we'll let it just steal since * we always reserve enough to update the inode item.
*/ if (!src_rsv || (!trans->bytes_reserved &&
src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
ret = btrfs_qgroup_reserve_meta(root, num_bytes,
BTRFS_QGROUP_RSV_META_PREALLOC, true); if (ret < 0) return ret;
ret = btrfs_block_rsv_add(fs_info, dst_rsv, num_bytes,
BTRFS_RESERVE_NO_FLUSH); /* NO_FLUSH could only fail with -ENOSPC */
ASSERT(ret == 0 || ret == -ENOSPC); if (ret)
btrfs_qgroup_free_meta_prealloc(root, num_bytes);
} else {
ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true);
}
/* * Insert a single delayed item or a batch of delayed items, as many as possible * that fit in a leaf. The delayed items (dir index keys) are sorted by their key * in the rbtree, and if there's a gap between two consecutive dir index items, * then it means at some point we had delayed dir indexes to add but they got * removed (by btrfs_delete_delayed_dir_index()) before we attempted to flush them * into the subvolume tree. Dir index keys also have their offsets coming from a * monotonically increasing counter, so we can't get new keys with an offset that * fits within a gap between delayed dir index items.
*/ staticint btrfs_insert_delayed_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, struct btrfs_delayed_item *first_item)
{ struct btrfs_fs_info *fs_info = root->fs_info; struct btrfs_delayed_node *node = first_item->delayed_node;
LIST_HEAD(item_list); struct btrfs_delayed_item *curr; struct btrfs_delayed_item *next; constint max_size = BTRFS_LEAF_DATA_SIZE(fs_info); struct btrfs_item_batch batch; struct btrfs_key first_key; const u32 first_data_size = first_item->data_len; int total_size; char *ins_data = NULL; int ret; bool continuous_keys_only = false;
lockdep_assert_held(&node->mutex);
/* * During normal operation the delayed index offset is continuously * increasing, so we can batch insert all items as there will not be any * overlapping keys in the tree. * * The exception to this is log replay, where we may have interleaved * offsets in the tree, so our batch needs to be continuous keys only in * order to ensure we do not end up with out of order items in our leaf.
*/ if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
continuous_keys_only = true;
/* * For delayed items to insert, we track reserved metadata bytes based * on the number of leaves that we will use. * See btrfs_insert_delayed_dir_index() and * btrfs_delayed_item_reserve_metadata()).
*/
ASSERT(first_item->bytes_reserved == 0);
/* * Now release our path before releasing the delayed items and their * metadata reservations, so that we don't block other tasks for more * time than needed.
*/
btrfs_release_path(path);
ASSERT(node->index_item_leaves > 0);
/* * For normal operations we will batch an entire leaf's worth of delayed * items, so if there are more items to process we can decrement * index_item_leaves by 1 as we inserted 1 leaf's worth of items. * * However for log replay we may not have inserted an entire leaf's * worth of items, we may have not had continuous items, so decrementing * here would mess up the index_item_leaves accounting. For this case * only clean up the accounting when there are no items left.
*/ if (next && !continuous_keys_only) { /* * We inserted one batch of items into a leaf a there are more * items to flush in a future batch, now release one unit of * metadata space from the delayed block reserve, corresponding * the leaf we just flushed to.
*/
btrfs_delayed_item_release_leaves(node, 1);
node->index_item_leaves--;
} elseif (!next) { /* * There are no more items to insert. We can have a number of * reserved leaves > 1 here - this happens when many dir index * items are added and then removed before they are flushed (file * names with a very short life, never span a transaction). So * release all remaining leaves.
*/
btrfs_delayed_item_release_leaves(node, node->index_item_leaves);
node->index_item_leaves = 0;
}
slot = path->slots[0];
last_slot = btrfs_header_nritems(leaf) - 1; /* * Our caller always gives us a path pointing to an existing item, so * this can not happen.
*/
ASSERT(slot <= last_slot); if (WARN_ON(slot > last_slot)) return -ENOENT;
/* * Keep checking if the next delayed item matches the next item in the * leaf - if so, we can add it to the batch of items to delete from the * leaf.
*/ while (slot < last_slot) { struct btrfs_key key;
next = __btrfs_next_delayed_item(curr); if (!next) break;
ret = btrfs_del_items(trans, root, path, path->slots[0], nitems); if (ret) return ret;
/* In case of BTRFS_FS_LOG_RECOVERING items won't have reserved space */ if (total_reserved_size > 0) { /* * Check btrfs_delayed_item_reserve_metadata() to see why we * don't need to release/reserve qgroup space.
*/
trace_btrfs_space_reservation(fs_info, "delayed_item", ino,
total_reserved_size, 0);
btrfs_block_rsv_release(fs_info, &fs_info->delayed_block_rsv,
total_reserved_size, NULL);
}
while (ret == 0) { struct btrfs_delayed_item *item;
mutex_lock(&node->mutex);
item = __btrfs_first_delayed_deletion_item(node); if (!item) {
mutex_unlock(&node->mutex); break;
}
key.offset = item->index;
ret = btrfs_search_slot(trans, root, &key, path, -1, 1); if (ret > 0) { /* * There's no matching item in the leaf. This means we * have already deleted this item in a past run of the * delayed items. We ignore errors when running delayed * items from an async context, through a work queue job * running btrfs_async_run_delayed_root(), and don't * release delayed items that failed to complete. This * is because we will retry later, and at transaction * commit time we always run delayed items and will * then deal with errors if they fail to run again. * * So just release delayed items for which we can't find * an item in the tree, and move to the next item.
*/
btrfs_release_path(path);
btrfs_release_delayed_item(item);
ret = 0;
} elseif (ret == 0) {
ret = btrfs_batch_delete_items(trans, root, path, item);
btrfs_release_path(path);
}
/* * We unlock and relock on each iteration, this is to prevent * blocking other tasks for too long while we are being run from * the async context (work queue job). Those tasks are typically * running system calls like creat/mkdir/rename/unlink/etc which * need to add delayed items to this delayed node.
*/
mutex_unlock(&node->mutex);
}
if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
mod = -1; else
mod = 1;
ret = btrfs_lookup_inode(trans, root, path, &key, mod); if (ret > 0)
ret = -ENOENT; if (ret < 0) { /* * If we fail to update the delayed inode we need to abort the * transaction, because we could leave the inode with the * improper counts behind.
*/ if (ret != -ENOENT)
btrfs_abort_transaction(trans, ret); goto out;
}
if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags)) goto out;
/* * Now we're going to delete the INODE_REF/EXTREF, which should be the * only one ref left. Check if the next item is an INODE_REF/EXTREF. * * But if we're the last item already, release and search for the last * INODE_REF/EXTREF.
*/ if (path->slots[0] + 1 >= btrfs_header_nritems(leaf)) {
key.objectid = node->inode_id;
key.type = BTRFS_INODE_EXTREF_KEY;
key.offset = (u64)-1;
/* * Delayed iref deletion is for the inode who has only one link, * so there is only one iref. The case that several irefs are * in the same item doesn't exist.
*/
ret = btrfs_del_item(trans, root, path); if (ret < 0)
btrfs_abort_transaction(trans, ret);
out:
btrfs_release_delayed_iref(node);
btrfs_release_path(path);
err_out:
btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0));
btrfs_release_delayed_inode(node); return ret;
}
curr_node = btrfs_first_delayed_node(delayed_root); while (curr_node && (!count || nr--)) {
ret = __btrfs_commit_inode_delayed_items(trans, path,
curr_node); if (ret) {
btrfs_abort_transaction(trans, ret); break;
}
prev_node = curr_node;
curr_node = btrfs_next_delayed_node(curr_node); /* * See the comment below about releasing path before releasing * node. If the commit of delayed items was successful the path * should always be released, but in case of an error, it may * point to locked extent buffers (a leaf at the very least).
*/
ASSERT(path->nodes[0] == NULL);
btrfs_release_delayed_node(prev_node);
}
/* * Release the path to avoid a potential deadlock and lockdep splat when * releasing the delayed node, as that requires taking the delayed node's * mutex. If another task starts running delayed items before we take * the mutex, it will first lock the mutex and then it may try to lock * the same btree path (leaf).
*/
btrfs_free_path(path);
if (curr_node)
btrfs_release_delayed_node(curr_node);
trans->block_rsv = block_rsv;
return ret;
}
int btrfs_run_delayed_items(struct btrfs_trans_handle *trans)
{ return __btrfs_run_delayed_items(trans, -1);
}
int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr)
{ return __btrfs_run_delayed_items(trans, nr);
}
int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, struct btrfs_inode *inode)
{ struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
BTRFS_PATH_AUTO_FREE(path); struct btrfs_block_rsv *block_rsv; int ret;
if (!delayed_node) return 0;
mutex_lock(&delayed_node->mutex); if (!delayed_node->count) {
mutex_unlock(&delayed_node->mutex);
btrfs_release_delayed_node(delayed_node); return 0;
}
mutex_unlock(&delayed_node->mutex);
path = btrfs_alloc_path(); if (!path) {
btrfs_release_delayed_node(delayed_node); return -ENOMEM;
}
mutex_lock(&delayed_node->mutex); if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
path, delayed_node); else
ret = 0;
mutex_unlock(&delayed_node->mutex);
if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) return;
/* * Adding the new dir index item does not require touching another * leaf, so we can release 1 unit of metadata that was previously * reserved when starting the transaction. This applies only to * the case where we had a transaction start and excludes the * transaction join case (when replaying log trees).
*/
trace_btrfs_space_reservation(fs_info, "transaction",
trans->transid, bytes, 0);
btrfs_block_rsv_release(fs_info, trans->block_rsv, bytes, NULL);
ASSERT(trans->bytes_reserved >= bytes);
trans->bytes_reserved -= bytes;
}
/* Will return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */ int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans, constchar *name, int name_len, struct btrfs_inode *dir, conststruct btrfs_disk_key *disk_key, u8 flags,
u64 index)
{ struct btrfs_fs_info *fs_info = trans->fs_info; constunsignedint leaf_data_size = BTRFS_LEAF_DATA_SIZE(fs_info); struct btrfs_delayed_node *delayed_node; struct btrfs_delayed_item *delayed_item; struct btrfs_dir_item *dir_item; bool reserve_leaf_space;
u32 data_len; int ret;
delayed_node = btrfs_get_or_create_delayed_node(dir); if (IS_ERR(delayed_node)) return PTR_ERR(delayed_node);
delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len,
delayed_node,
BTRFS_DELAYED_INSERTION_ITEM); if (!delayed_item) {
ret = -ENOMEM; goto release_node;
}
/* * First attempt to insert the delayed item. This is to make the error * handling path simpler in case we fail (-EEXIST). There's no risk of * any other task coming in and running the delayed item before we do * the metadata space reservation below, because we are holding the * delayed node's mutex and that mutex must also be locked before the * node's delayed items can be run.
*/
ret = __btrfs_add_delayed_item(delayed_node, delayed_item); if (unlikely(ret)) {
btrfs_err(trans->fs_info, "error adding delayed dir index item, name: %.*s, index: %llu, root: %llu, dir: %llu, dir->index_cnt: %llu, delayed_node->index_cnt: %llu, error: %d",
name_len, name, index, btrfs_root_id(delayed_node->root),
delayed_node->inode_id, dir->index_cnt,
delayed_node->index_cnt, ret);
btrfs_release_delayed_item(delayed_item);
btrfs_release_dir_index_item_space(trans);
mutex_unlock(&delayed_node->mutex); goto release_node;
}
if (reserve_leaf_space) {
ret = btrfs_delayed_item_reserve_metadata(trans, delayed_item); /* * Space was reserved for a dir index item insertion when we * started the transaction, so getting a failure here should be * impossible.
*/ if (WARN_ON(ret)) {
btrfs_release_delayed_item(delayed_item);
mutex_unlock(&delayed_node->mutex); goto release_node;
}
/* * For delayed items to insert, we track reserved metadata bytes based * on the number of leaves that we will use. * See btrfs_insert_delayed_dir_index() and * btrfs_delayed_item_reserve_metadata()).
*/
ASSERT(item->bytes_reserved == 0);
ASSERT(node->index_item_leaves > 0);
/* * If there's only one leaf reserved, we can decrement this item from the * current batch, otherwise we can not because we don't know which leaf * it belongs to. With the current limit on delayed items, we rarely * accumulate enough dir index items to fill more than one leaf (even * when using a leaf size of 4K).
*/ if (node->index_item_leaves == 1) { const u32 data_len = item->data_len + sizeof(struct btrfs_item);
/* If we now have no more dir index items, we can release all leaves. */ if (RB_EMPTY_ROOT(&node->ins_root.rb_root)) {
btrfs_delayed_item_release_leaves(node, node->index_item_leaves);
node->index_item_leaves = 0;
}
mutex_unlock(&node->mutex); returntrue;
}
int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans, struct btrfs_inode *dir, u64 index)
{ struct btrfs_delayed_node *node; struct btrfs_delayed_item *item; int ret;
node = btrfs_get_or_create_delayed_node(dir); if (IS_ERR(node)) return PTR_ERR(node);
if (btrfs_delete_delayed_insertion_item(node, index)) {
ret = 0; goto end;
}
item = btrfs_alloc_delayed_item(0, node, BTRFS_DELAYED_DELETION_ITEM); if (!item) {
ret = -ENOMEM; goto end;
}
item->index = index;
ret = btrfs_delayed_item_reserve_metadata(trans, item); /* * we have reserved enough space when we start a new transaction, * so reserving metadata failure is impossible.
*/ if (ret < 0) {
btrfs_err(trans->fs_info, "metadata reservation failed for delayed dir item deletion, index: %llu, root: %llu, inode: %llu, error: %d",
index, btrfs_root_id(node->root), node->inode_id, ret);
btrfs_release_delayed_item(item); goto end;
}
mutex_lock(&node->mutex);
ret = __btrfs_add_delayed_item(node, item); if (unlikely(ret)) {
btrfs_err(trans->fs_info, "failed to add delayed dir index item, root: %llu, inode: %llu, index: %llu, error: %d",
index, btrfs_root_id(node->root), node->inode_id, ret);
btrfs_delayed_item_release_metadata(dir->root, item);
btrfs_release_delayed_item(item);
}
mutex_unlock(&node->mutex);
end:
btrfs_release_delayed_node(node); return ret;
}
int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode)
{ struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
if (!delayed_node) return -ENOENT;
/* * Since we have held i_mutex of this directory, it is impossible that * a new directory index is added into the delayed node and index_cnt * is updated now. So we needn't lock the delayed node.
*/ if (!delayed_node->index_cnt) {
btrfs_release_delayed_node(delayed_node); return -EINVAL;
}
delayed_node = btrfs_get_delayed_node(inode); if (!delayed_node) returnfalse;
/* * We can only do one readdir with delayed items at a time because of * item->readdir_list.
*/
btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
btrfs_inode_lock(inode, 0);
item = __btrfs_first_delayed_deletion_item(delayed_node); while (item && item->index <= last_index) {
refcount_inc(&item->refs);
list_add_tail(&item->readdir_list, del_list);
item = __btrfs_next_delayed_item(item);
}
mutex_unlock(&delayed_node->mutex); /* * This delayed node is still cached in the btrfs inode, so refs * must be > 1 now, and we needn't check it is going to be freed * or not. * * Besides that, this function is used to read dir, we do not * insert/delete delayed items in this period. So we also needn't * requeue or dequeue this delayed node.
*/
refcount_dec(&delayed_node->refs);
list_for_each_entry(curr, del_list, readdir_list) { if (curr->index > index) break; if (curr->index == index) {
ret = true; break;
}
} return ret;
}
/* * Read dir info stored in the delayed tree.
*/ bool btrfs_readdir_delayed_dir_index(struct dir_context *ctx, conststruct list_head *ins_list)
{ struct btrfs_dir_item *di; struct btrfs_delayed_item *curr, *next; struct btrfs_key location; char *name; int name_len; unsignedchar d_type;
/* * Changing the data of the delayed item is impossible. So * we needn't lock them. And we have held i_mutex of the * directory, nobody can delete any directory indexes now.
*/
list_for_each_entry_safe(curr, next, ins_list, readdir_list) { bool over;
list_del(&curr->readdir_list);
if (curr->index < ctx->pos) { if (refcount_dec_and_test(&curr->refs))
kfree(curr); continue;
}
ctx->pos = curr->index;
di = (struct btrfs_dir_item *)curr->data;
name = (char *)(di + 1);
name_len = btrfs_stack_dir_name_len(di);
/* * we don't do delayed inode updates during log recovery because it * leads to enospc problems. This means we also can't do * delayed inode refs
*/ if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) return -EAGAIN;
delayed_node = btrfs_get_or_create_delayed_node(inode); if (IS_ERR(delayed_node)) return PTR_ERR(delayed_node);
/* * We don't reserve space for inode ref deletion is because: * - We ONLY do async inode ref deletion for the inode who has only * one link(i_nlink == 1), it means there is only one inode ref. * And in most case, the inode ref and the inode item are in the * same leaf, and we will deal with them at the same time. * Since we are sure we will reserve the space for the inode item, * it is unnecessary to reserve space for inode ref deletion. * - If the inode ref and the inode item are not in the same leaf, * We also needn't worry about enospc problem, because we reserve * much more space for the inode update than it needs. * - At the worst, we can steal some space from the global reservation. * It is very rare.
*/
mutex_lock(&delayed_node->mutex); if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) goto release_node;
while (1) { struct btrfs_delayed_node *node; int count;
xa_lock(&root->delayed_nodes); if (xa_empty(&root->delayed_nodes)) {
xa_unlock(&root->delayed_nodes); return;
}
count = 0;
xa_for_each_start(&root->delayed_nodes, index, node, index) { /* * Don't increase refs in case the node is dead and * about to be removed from the tree in the loop below
*/ if (refcount_inc_not_zero(&node->refs)) {
delayed_nodes[count] = node;
count++;
} if (count >= ARRAY_SIZE(delayed_nodes)) break;
}
xa_unlock(&root->delayed_nodes);
index++;
for (int i = 0; i < count; i++) {
__btrfs_kill_delayed_node(delayed_nodes[i]);
btrfs_release_delayed_node(delayed_nodes[i]);
}
}
}
node = btrfs_get_delayed_node(inode); if (!node) return;
mutex_lock(&node->mutex);
item = __btrfs_first_delayed_insertion_item(node); while (item) { /* * It's possible that the item is already in a log list. This * can happen in case two tasks are trying to log the same * directory. For example if we have tasks A and task B: * * Task A collected the delayed items into a log list while * under the inode's log_mutex (at btrfs_log_inode()), but it * only releases the items after logging the inodes they point * to (if they are new inodes), which happens after unlocking * the log mutex; * * Task B enters btrfs_log_inode() and acquires the log_mutex * of the same directory inode, before task B releases the * delayed items. This can happen for example when logging some * inode we need to trigger logging of its parent directory, so * logging two files that have the same parent directory can * lead to this. * * If this happens, just ignore delayed items already in a log * list. All the tasks logging the directory are under a log * transaction and whichever finishes first can not sync the log * before the other completes and leaves the log transaction.
*/ if (!item->logged && list_empty(&item->log_list)) {
refcount_inc(&item->refs);
list_add_tail(&item->log_list, ins_list);
}
item = __btrfs_next_delayed_item(item);
}
item = __btrfs_first_delayed_deletion_item(node); while (item) { /* It may be non-empty, for the same reason mentioned above. */ if (!item->logged && list_empty(&item->log_list)) {
refcount_inc(&item->refs);
list_add_tail(&item->log_list, del_list);
}
item = __btrfs_next_delayed_item(item);
}
mutex_unlock(&node->mutex);
/* * We are called during inode logging, which means the inode is in use * and can not be evicted before we finish logging the inode. So we never * have the last reference on the delayed inode. * Also, we don't use btrfs_release_delayed_node() because that would * requeue the delayed inode (change its order in the list of prepared * nodes) and we don't want to do such change because we don't create or * delete delayed items.
*/
ASSERT(refcount_read(&node->refs) > 1);
refcount_dec(&node->refs);
}
/* * We are called during inode logging, which means the inode is in use * and can not be evicted before we finish logging the inode. So we never * have the last reference on the delayed inode. * Also, we don't use btrfs_release_delayed_node() because that would * requeue the delayed inode (change its order in the list of prepared * nodes) and we don't want to do such change because we don't create or * delete delayed items.
*/
ASSERT(refcount_read(&node->refs) > 1);
refcount_dec(&node->refs);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.