blob: 2d5f67065b697afcb687ff61ddbbf5e9d7c774e4 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#ifndef __EXTENTIO__
2#define __EXTENTIO__
3
4#include <linux/rbtree.h>
5
6/* bits for the extent state */
7#define EXTENT_DIRTY 1
8#define EXTENT_WRITEBACK (1 << 1)
9#define EXTENT_UPTODATE (1 << 2)
10#define EXTENT_LOCKED (1 << 3)
11#define EXTENT_NEW (1 << 4)
12#define EXTENT_DELALLOC (1 << 5)
13#define EXTENT_DEFRAG (1 << 6)
14#define EXTENT_DEFRAG_DONE (1 << 7)
15#define EXTENT_BUFFER_FILLED (1 << 8)
Chris Masone6dcd2d2008-07-17 12:53:50 -040016#define EXTENT_ORDERED (1 << 9)
17#define EXTENT_ORDERED_METADATA (1 << 10)
Zheng Yan5b21f2e2008-09-26 10:05:38 -040018#define EXTENT_BOUNDARY (1 << 11)
Chris Masond1310b22008-01-24 16:13:08 -050019#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
20
Chris Masonc8b97812008-10-29 14:49:59 -040021/* flags for bio submission */
22#define EXTENT_BIO_COMPRESSED 1
23
Chris Masond1310b22008-01-24 16:13:08 -050024/*
25 * page->private values. Every page that is controlled by the extent
26 * map has page->private set to one.
27 */
28#define EXTENT_PAGE_PRIVATE 1
29#define EXTENT_PAGE_PRIVATE_FIRST_PAGE 3
30
Chris Mason70dec802008-01-29 09:59:12 -050031struct extent_state;
32
Chris Mason44b8bd72008-04-16 11:14:51 -040033typedef int (extent_submit_bio_hook_t)(struct inode *inode, int rw,
Chris Masonc8b97812008-10-29 14:49:59 -040034 struct bio *bio, int mirror_num,
35 unsigned long bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -050036struct extent_io_ops {
Chris Masonc8b97812008-10-29 14:49:59 -040037 int (*fill_delalloc)(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -050038 u64 start, u64 end, int *page_started,
39 unsigned long *nr_written);
Chris Mason247e7432008-07-17 12:53:51 -040040 int (*writepage_start_hook)(struct page *page, u64 start, u64 end);
Chris Masond1310b22008-01-24 16:13:08 -050041 int (*writepage_io_hook)(struct page *page, u64 start, u64 end);
Chris Mason44b8bd72008-04-16 11:14:51 -040042 extent_submit_bio_hook_t *submit_bio_hook;
Chris Mason239b14b2008-03-24 15:02:07 -040043 int (*merge_bio_hook)(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -040044 size_t size, struct bio *bio,
45 unsigned long bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -050046 int (*readpage_io_hook)(struct page *page, u64 start, u64 end);
Chris Mason7e383262008-04-09 16:28:12 -040047 int (*readpage_io_failed_hook)(struct bio *bio, struct page *page,
48 u64 start, u64 end,
49 struct extent_state *state);
Chris Mason1259ab72008-05-12 13:39:03 -040050 int (*writepage_io_failed_hook)(struct bio *bio, struct page *page,
51 u64 start, u64 end,
52 struct extent_state *state);
Chris Mason70dec802008-01-29 09:59:12 -050053 int (*readpage_end_io_hook)(struct page *page, u64 start, u64 end,
54 struct extent_state *state);
Chris Mason1259ab72008-05-12 13:39:03 -040055 int (*writepage_end_io_hook)(struct page *page, u64 start, u64 end,
Chris Masone6dcd2d2008-07-17 12:53:50 -040056 struct extent_state *state, int uptodate);
Chris Mason291d6732008-01-29 15:55:23 -050057 int (*set_bit_hook)(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -050058 unsigned long old, unsigned long bits);
Chris Mason291d6732008-01-29 15:55:23 -050059 int (*clear_bit_hook)(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -050060 unsigned long old, unsigned long bits);
Chris Mason4bef0842008-09-08 11:18:08 -040061 int (*write_cache_pages_lock_hook)(struct page *page);
Chris Masond1310b22008-01-24 16:13:08 -050062};
63
64struct extent_io_tree {
65 struct rb_root state;
Chris Mason6af118ce2008-07-22 11:18:07 -040066 struct rb_root buffer;
Chris Masond1310b22008-01-24 16:13:08 -050067 struct address_space *mapping;
68 u64 dirty_bytes;
Chris Mason70dec802008-01-29 09:59:12 -050069 spinlock_t lock;
Chris Mason6af118ce2008-07-22 11:18:07 -040070 spinlock_t buffer_lock;
Chris Masond1310b22008-01-24 16:13:08 -050071 struct extent_io_ops *ops;
Chris Masond1310b22008-01-24 16:13:08 -050072};
73
74struct extent_state {
75 u64 start;
76 u64 end; /* inclusive */
Chris Masond1310b22008-01-24 16:13:08 -050077 struct rb_node rb_node;
Chris Mason70dec802008-01-29 09:59:12 -050078 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -050079 wait_queue_head_t wq;
80 atomic_t refs;
81 unsigned long state;
82
83 /* for use by the FS */
84 u64 private;
85
Chris Mason2d2ae542008-03-26 16:24:23 -040086 struct list_head leak_list;
Chris Masond1310b22008-01-24 16:13:08 -050087};
88
89struct extent_buffer {
90 u64 start;
91 unsigned long len;
92 char *map_token;
93 char *kaddr;
94 unsigned long map_start;
95 unsigned long map_len;
96 struct page *first_page;
Chris Masond1310b22008-01-24 16:13:08 -050097 atomic_t refs;
98 int flags;
Chris Mason2d2ae542008-03-26 16:24:23 -040099 struct list_head leak_list;
Chris Mason6af118ce2008-07-22 11:18:07 -0400100 struct rb_node rb_node;
Chris Masona61e6f22008-07-22 11:18:08 -0400101 struct mutex mutex;
Chris Masond1310b22008-01-24 16:13:08 -0500102};
103
104struct extent_map_tree;
105
Chris Masond7fc6402008-02-18 12:12:38 -0500106static inline struct extent_state *extent_state_next(struct extent_state *state)
107{
108 struct rb_node *node;
109 node = rb_next(&state->rb_node);
110 if (!node)
111 return NULL;
112 return rb_entry(node, struct extent_state, rb_node);
113}
114
Chris Masond1310b22008-01-24 16:13:08 -0500115typedef struct extent_map *(get_extent_t)(struct inode *inode,
116 struct page *page,
117 size_t page_offset,
118 u64 start, u64 len,
119 int create);
120
121void extent_io_tree_init(struct extent_io_tree *tree,
122 struct address_space *mapping, gfp_t mask);
Chris Masond1310b22008-01-24 16:13:08 -0500123int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -0500124 struct extent_io_tree *tree, struct page *page,
125 gfp_t mask);
Chris Mason6af118ce2008-07-22 11:18:07 -0400126int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page);
Chris Mason7b13b7b2008-04-18 10:29:50 -0400127int try_release_extent_state(struct extent_map_tree *map,
128 struct extent_io_tree *tree, struct page *page,
129 gfp_t mask);
Chris Masond1310b22008-01-24 16:13:08 -0500130int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask);
131int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask);
Josef Bacik25179202008-10-29 14:49:05 -0400132int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
133 gfp_t mask);
Chris Masond1310b22008-01-24 16:13:08 -0500134int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
135 get_extent_t *get_extent);
136int __init extent_io_init(void);
137void extent_io_exit(void);
138
139u64 count_range_bits(struct extent_io_tree *tree,
140 u64 *start, u64 search_end,
141 u64 max_bytes, unsigned long bits);
142
143int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
144 int bits, int filled);
145int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
146 int bits, gfp_t mask);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400147int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
148 int bits, int wake, int delete, gfp_t mask);
Chris Masond1310b22008-01-24 16:13:08 -0500149int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
150 int bits, gfp_t mask);
151int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
152 gfp_t mask);
153int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
154 gfp_t mask);
155int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
156 gfp_t mask);
157int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
158 gfp_t mask);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400159int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
160 gfp_t mask);
161int clear_extent_ordered_metadata(struct extent_io_tree *tree, u64 start,
162 u64 end, gfp_t mask);
Chris Masond1310b22008-01-24 16:13:08 -0500163int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
164 gfp_t mask);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400165int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
166 gfp_t mask);
Chris Masond1310b22008-01-24 16:13:08 -0500167int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
168 u64 *start_ret, u64 *end_ret, int bits);
Chris Masond7fc6402008-02-18 12:12:38 -0500169struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
170 u64 start, int bits);
Chris Masond1310b22008-01-24 16:13:08 -0500171int extent_invalidatepage(struct extent_io_tree *tree,
172 struct page *page, unsigned long offset);
173int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
174 get_extent_t *get_extent,
175 struct writeback_control *wbc);
Chris Mason771ed682008-11-06 22:02:51 -0500176int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
177 u64 start, u64 end, get_extent_t *get_extent,
178 int mode);
Chris Masond1310b22008-01-24 16:13:08 -0500179int extent_writepages(struct extent_io_tree *tree,
180 struct address_space *mapping,
181 get_extent_t *get_extent,
182 struct writeback_control *wbc);
183int extent_readpages(struct extent_io_tree *tree,
184 struct address_space *mapping,
185 struct list_head *pages, unsigned nr_pages,
186 get_extent_t get_extent);
187int extent_prepare_write(struct extent_io_tree *tree,
188 struct inode *inode, struct page *page,
189 unsigned from, unsigned to, get_extent_t *get_extent);
190int extent_commit_write(struct extent_io_tree *tree,
191 struct inode *inode, struct page *page,
192 unsigned from, unsigned to);
193sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
194 get_extent_t *get_extent);
195int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end);
196int set_state_private(struct extent_io_tree *tree, u64 start, u64 private);
197int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private);
198void set_page_extent_mapped(struct page *page);
199
200struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
201 u64 start, unsigned long len,
202 struct page *page0,
203 gfp_t mask);
204struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
205 u64 start, unsigned long len,
206 gfp_t mask);
207void free_extent_buffer(struct extent_buffer *eb);
208int read_extent_buffer_pages(struct extent_io_tree *tree,
Chris Masona86c12c2008-02-07 10:50:54 -0500209 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -0400210 get_extent_t *get_extent, int mirror_num);
Chris Masond1310b22008-01-24 16:13:08 -0500211
212static inline void extent_buffer_get(struct extent_buffer *eb)
213{
214 atomic_inc(&eb->refs);
215}
216
217int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
218 unsigned long start,
219 unsigned long len);
220void read_extent_buffer(struct extent_buffer *eb, void *dst,
221 unsigned long start,
222 unsigned long len);
223void write_extent_buffer(struct extent_buffer *eb, const void *src,
224 unsigned long start, unsigned long len);
225void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
226 unsigned long dst_offset, unsigned long src_offset,
227 unsigned long len);
228void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
229 unsigned long src_offset, unsigned long len);
230void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
231 unsigned long src_offset, unsigned long len);
232void memset_extent_buffer(struct extent_buffer *eb, char c,
233 unsigned long start, unsigned long len);
234int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
235 struct extent_buffer *eb);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400236int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end);
237int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits);
Chris Masond1310b22008-01-24 16:13:08 -0500238int clear_extent_buffer_dirty(struct extent_io_tree *tree,
239 struct extent_buffer *eb);
240int set_extent_buffer_dirty(struct extent_io_tree *tree,
241 struct extent_buffer *eb);
242int set_extent_buffer_uptodate(struct extent_io_tree *tree,
243 struct extent_buffer *eb);
Chris Mason1259ab72008-05-12 13:39:03 -0400244int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
245 struct extent_buffer *eb);
Chris Masond1310b22008-01-24 16:13:08 -0500246int extent_buffer_uptodate(struct extent_io_tree *tree,
247 struct extent_buffer *eb);
248int map_extent_buffer(struct extent_buffer *eb, unsigned long offset,
249 unsigned long min_len, char **token, char **map,
250 unsigned long *map_start,
251 unsigned long *map_len, int km);
252int map_private_extent_buffer(struct extent_buffer *eb, unsigned long offset,
253 unsigned long min_len, char **token, char **map,
254 unsigned long *map_start,
255 unsigned long *map_len, int km);
256void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km);
Chris Masonce9adaa2008-04-09 16:28:12 -0400257int release_extent_buffer_tail_pages(struct extent_buffer *eb);
258int extent_range_uptodate(struct extent_io_tree *tree,
259 u64 start, u64 end);
Chris Masonc8b97812008-10-29 14:49:59 -0400260int extent_clear_unlock_delalloc(struct inode *inode,
261 struct extent_io_tree *tree,
262 u64 start, u64 end, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500263 int unlock_page,
264 int clear_unlock,
265 int clear_delalloc, int clear_dirty,
266 int set_writeback,
267 int end_writeback);
Chris Masond1310b22008-01-24 16:13:08 -0500268#endif