Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (C) 2011 Fujitsu.  All rights reserved.
0004  * Written by Miao Xie <miaox@cn.fujitsu.com>
0005  */
0006 
0007 #ifndef BTRFS_DELAYED_INODE_H
0008 #define BTRFS_DELAYED_INODE_H
0009 
0010 #include <linux/rbtree.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/mutex.h>
0013 #include <linux/list.h>
0014 #include <linux/wait.h>
0015 #include <linux/atomic.h>
0016 #include <linux/refcount.h>
0017 #include "ctree.h"
0018 
0019 /* types of the delayed item */
0020 #define BTRFS_DELAYED_INSERTION_ITEM    1
0021 #define BTRFS_DELAYED_DELETION_ITEM 2
0022 
0023 struct btrfs_delayed_root {
0024     spinlock_t lock;
0025     struct list_head node_list;
0026     /*
0027      * Used for delayed nodes which is waiting to be dealt with by the
0028      * worker. If the delayed node is inserted into the work queue, we
0029      * drop it from this list.
0030      */
0031     struct list_head prepare_list;
0032     atomic_t items;     /* for delayed items */
0033     atomic_t items_seq; /* for delayed items */
0034     int nodes;      /* for delayed nodes */
0035     wait_queue_head_t wait;
0036 };
0037 
0038 #define BTRFS_DELAYED_NODE_IN_LIST  0
0039 #define BTRFS_DELAYED_NODE_INODE_DIRTY  1
0040 #define BTRFS_DELAYED_NODE_DEL_IREF 2
0041 
0042 struct btrfs_delayed_node {
0043     u64 inode_id;
0044     u64 bytes_reserved;
0045     struct btrfs_root *root;
0046     /* Used to add the node into the delayed root's node list. */
0047     struct list_head n_list;
0048     /*
0049      * Used to add the node into the prepare list, the nodes in this list
0050      * is waiting to be dealt with by the async worker.
0051      */
0052     struct list_head p_list;
0053     struct rb_root_cached ins_root;
0054     struct rb_root_cached del_root;
0055     struct mutex mutex;
0056     struct btrfs_inode_item inode_item;
0057     refcount_t refs;
0058     u64 index_cnt;
0059     unsigned long flags;
0060     int count;
0061     /*
0062      * The size of the next batch of dir index items to insert (if this
0063      * node is from a directory inode). Protected by @mutex.
0064      */
0065     u32 curr_index_batch_size;
0066     /*
0067      * Number of leaves reserved for inserting dir index items (if this
0068      * node belongs to a directory inode). This may be larger then the
0069      * actual number of leaves we end up using. Protected by @mutex.
0070      */
0071     u32 index_item_leaves;
0072 };
0073 
0074 struct btrfs_delayed_item {
0075     struct rb_node rb_node;
0076     struct btrfs_key key;
0077     struct list_head tree_list; /* used for batch insert/delete items */
0078     struct list_head readdir_list;  /* used for readdir items */
0079     u64 bytes_reserved;
0080     struct btrfs_delayed_node *delayed_node;
0081     refcount_t refs;
0082     int ins_or_del;
0083     u32 data_len;
0084     char data[];
0085 };
0086 
0087 static inline void btrfs_init_delayed_root(
0088                 struct btrfs_delayed_root *delayed_root)
0089 {
0090     atomic_set(&delayed_root->items, 0);
0091     atomic_set(&delayed_root->items_seq, 0);
0092     delayed_root->nodes = 0;
0093     spin_lock_init(&delayed_root->lock);
0094     init_waitqueue_head(&delayed_root->wait);
0095     INIT_LIST_HEAD(&delayed_root->node_list);
0096     INIT_LIST_HEAD(&delayed_root->prepare_list);
0097 }
0098 
0099 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
0100                    const char *name, int name_len,
0101                    struct btrfs_inode *dir,
0102                    struct btrfs_disk_key *disk_key, u8 type,
0103                    u64 index);
0104 
0105 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
0106                    struct btrfs_inode *dir, u64 index);
0107 
0108 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode);
0109 
0110 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans);
0111 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr);
0112 
0113 void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info);
0114 
0115 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
0116                      struct btrfs_inode *inode);
0117 /* Used for evicting the inode. */
0118 void btrfs_remove_delayed_node(struct btrfs_inode *inode);
0119 void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode);
0120 int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode);
0121 
0122 
0123 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
0124                    struct btrfs_root *root,
0125                    struct btrfs_inode *inode);
0126 int btrfs_fill_inode(struct inode *inode, u32 *rdev);
0127 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode);
0128 
0129 /* Used for drop dead root */
0130 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root);
0131 
0132 /* Used for clean the transaction */
0133 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info);
0134 
0135 /* Used for readdir() */
0136 bool btrfs_readdir_get_delayed_items(struct inode *inode,
0137                      struct list_head *ins_list,
0138                      struct list_head *del_list);
0139 void btrfs_readdir_put_delayed_items(struct inode *inode,
0140                      struct list_head *ins_list,
0141                      struct list_head *del_list);
0142 int btrfs_should_delete_dir_index(struct list_head *del_list,
0143                   u64 index);
0144 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
0145                     struct list_head *ins_list);
0146 
0147 /* for init */
0148 int __init btrfs_delayed_inode_init(void);
0149 void __cold btrfs_delayed_inode_exit(void);
0150 
0151 /* for debugging */
0152 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info);
0153 
0154 #endif