blob: c47f282ae3dd4853289db55f58e10dbb3ed6af5d [file] [log] [blame]
Nicholas Flintham1e3d3112013-04-10 10:48:38 +01001
2#ifndef _LINUX_BUFFER_HEAD_H
3#define _LINUX_BUFFER_HEAD_H
4
5#include <linux/types.h>
6#include <linux/fs.h>
7#include <linux/linkage.h>
8#include <linux/pagemap.h>
9#include <linux/wait.h>
10#include <linux/atomic.h>
11
12#ifdef CONFIG_BLOCK
13
14enum bh_state_bits {
15 BH_Uptodate,
16 BH_Dirty,
17 BH_Lock,
18 BH_Req,
19 BH_Uptodate_Lock,
20
21 BH_Mapped,
22 BH_New,
23 BH_Async_Read,
24 BH_Async_Write,
25 BH_Delay,
26 BH_Boundary,
27 BH_Write_EIO,
28 BH_Unwritten, /* Buffer is allocated on disk but not written */
29 BH_Quiet,
30
31 BH_PrivateStart,
32};
33
34#define MAX_BUF_PER_PAGE (PAGE_CACHE_SIZE / 512)
35
36struct page;
37struct buffer_head;
38struct address_space;
39typedef void (bh_end_io_t)(struct buffer_head *bh, int uptodate);
40
41struct buffer_head {
42 unsigned long b_state;
43 struct buffer_head *b_this_page;
44 struct page *b_page;
45
46 sector_t b_blocknr;
47 size_t b_size;
48 char *b_data;
49
50 struct block_device *b_bdev;
51 bh_end_io_t *b_end_io;
52 void *b_private;
53 struct list_head b_assoc_buffers;
54 struct address_space *b_assoc_map;
55 atomic_t b_count;
56};
57
58#define BUFFER_FNS(bit, name) \
59static inline void set_buffer_##name(struct buffer_head *bh) \
60{ \
61 set_bit(BH_##bit, &(bh)->b_state); \
62} \
63static inline void clear_buffer_##name(struct buffer_head *bh) \
64{ \
65 clear_bit(BH_##bit, &(bh)->b_state); \
66} \
67static inline int buffer_##name(const struct buffer_head *bh) \
68{ \
69 return test_bit(BH_##bit, &(bh)->b_state); \
70}
71
72#define TAS_BUFFER_FNS(bit, name) \
73static inline int test_set_buffer_##name(struct buffer_head *bh) \
74{ \
75 return test_and_set_bit(BH_##bit, &(bh)->b_state); \
76} \
77static inline int test_clear_buffer_##name(struct buffer_head *bh) \
78{ \
79 return test_and_clear_bit(BH_##bit, &(bh)->b_state); \
80} \
81
82BUFFER_FNS(Uptodate, uptodate)
83BUFFER_FNS(Dirty, dirty)
84TAS_BUFFER_FNS(Dirty, dirty)
85BUFFER_FNS(Lock, locked)
86BUFFER_FNS(Req, req)
87TAS_BUFFER_FNS(Req, req)
88BUFFER_FNS(Mapped, mapped)
89BUFFER_FNS(New, new)
90BUFFER_FNS(Async_Read, async_read)
91BUFFER_FNS(Async_Write, async_write)
92BUFFER_FNS(Delay, delay)
93BUFFER_FNS(Boundary, boundary)
94BUFFER_FNS(Write_EIO, write_io_error)
95BUFFER_FNS(Unwritten, unwritten)
96
97#define bh_offset(bh) ((unsigned long)(bh)->b_data & ~PAGE_MASK)
98#define touch_buffer(bh) mark_page_accessed(bh->b_page)
99
100#define page_buffers(page) \
101 ({ \
102 BUG_ON(!PagePrivate(page)); \
103 ((struct buffer_head *)page_private(page)); \
104 })
105#define page_has_buffers(page) PagePrivate(page)
106
107
108void mark_buffer_dirty(struct buffer_head *bh);
109void init_buffer(struct buffer_head *, bh_end_io_t *, void *);
110void set_bh_page(struct buffer_head *bh,
111 struct page *page, unsigned long offset);
112int try_to_free_buffers(struct page *);
113struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
114 int retry);
115void create_empty_buffers(struct page *, unsigned long,
116 unsigned long b_state);
117void end_buffer_read_sync(struct buffer_head *bh, int uptodate);
118void end_buffer_write_sync(struct buffer_head *bh, int uptodate);
119void end_buffer_async_write(struct buffer_head *bh, int uptodate);
120
121void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode);
122int inode_has_buffers(struct inode *);
123void invalidate_inode_buffers(struct inode *);
124int remove_inode_buffers(struct inode *inode);
125int sync_mapping_buffers(struct address_space *mapping);
126void unmap_underlying_metadata(struct block_device *bdev, sector_t block);
127
128void mark_buffer_async_write(struct buffer_head *bh);
129void __wait_on_buffer(struct buffer_head *);
130wait_queue_head_t *bh_waitq_head(struct buffer_head *bh);
131struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block,
132 unsigned size);
133struct buffer_head *__getblk(struct block_device *bdev, sector_t block,
134 unsigned size);
135void __brelse(struct buffer_head *);
136void __bforget(struct buffer_head *);
137void __breadahead(struct block_device *, sector_t block, unsigned int size);
138struct buffer_head *__bread(struct block_device *, sector_t block, unsigned size);
139void invalidate_bh_lrus(void);
140struct buffer_head *alloc_buffer_head(gfp_t gfp_flags);
141void free_buffer_head(struct buffer_head * bh);
142void unlock_buffer(struct buffer_head *bh);
143void __lock_buffer(struct buffer_head *bh);
144void ll_rw_block(int, int, struct buffer_head * bh[]);
145int sync_dirty_buffer(struct buffer_head *bh);
146int __sync_dirty_buffer(struct buffer_head *bh, int rw);
147void write_dirty_buffer(struct buffer_head *bh, int rw);
148int submit_bh(int, struct buffer_head *);
149void write_boundary_block(struct block_device *bdev,
150 sector_t bblock, unsigned blocksize);
151int bh_uptodate_or_lock(struct buffer_head *bh);
152int bh_submit_read(struct buffer_head *bh);
153
154extern int buffer_heads_over_limit;
155
156void block_invalidatepage(struct page *page, unsigned long offset);
157int block_write_full_page(struct page *page, get_block_t *get_block,
158 struct writeback_control *wbc);
159int block_write_full_page_endio(struct page *page, get_block_t *get_block,
160 struct writeback_control *wbc, bh_end_io_t *handler);
161int block_read_full_page(struct page*, get_block_t*);
162int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
163 unsigned long from);
164int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
165 unsigned flags, struct page **pagep, get_block_t *get_block);
166int __block_write_begin(struct page *page, loff_t pos, unsigned len,
167 get_block_t *get_block);
168int block_write_end(struct file *, struct address_space *,
169 loff_t, unsigned, unsigned,
170 struct page *, void *);
171int generic_write_end(struct file *, struct address_space *,
172 loff_t, unsigned, unsigned,
173 struct page *, void *);
174void page_zero_new_buffers(struct page *page, unsigned from, unsigned to);
175int cont_write_begin(struct file *, struct address_space *, loff_t,
176 unsigned, unsigned, struct page **, void **,
177 get_block_t *, loff_t *);
178int generic_cont_expand_simple(struct inode *inode, loff_t size);
179int block_commit_write(struct page *page, unsigned from, unsigned to);
180int __block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
181 get_block_t get_block);
182int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
183 get_block_t get_block);
184static inline int block_page_mkwrite_return(int err)
185{
186 if (err == 0)
187 return VM_FAULT_LOCKED;
188 if (err == -EFAULT)
189 return VM_FAULT_NOPAGE;
190 if (err == -ENOMEM)
191 return VM_FAULT_OOM;
192 if (err == -EAGAIN)
193 return VM_FAULT_RETRY;
194
195 return VM_FAULT_SIGBUS;
196}
197sector_t generic_block_bmap(struct address_space *, sector_t, get_block_t *);
198int block_truncate_page(struct address_space *, loff_t, get_block_t *);
199int nobh_write_begin(struct address_space *, loff_t, unsigned, unsigned,
200 struct page **, void **, get_block_t*);
201int nobh_write_end(struct file *, struct address_space *,
202 loff_t, unsigned, unsigned,
203 struct page *, void *);
204int nobh_truncate_page(struct address_space *, loff_t, get_block_t *);
205int nobh_writepage(struct page *page, get_block_t *get_block,
206 struct writeback_control *wbc);
207
208void buffer_init(void);
209
210
211static inline void attach_page_buffers(struct page *page,
212 struct buffer_head *head)
213{
214 page_cache_get(page);
215 SetPagePrivate(page);
216 set_page_private(page, (unsigned long)head);
217}
218
219static inline void get_bh(struct buffer_head *bh)
220{
221 atomic_inc(&bh->b_count);
222}
223
224static inline void put_bh(struct buffer_head *bh)
225{
226 smp_mb__before_atomic_dec();
227 atomic_dec(&bh->b_count);
228}
229
230static inline void brelse(struct buffer_head *bh)
231{
232 if (bh)
233 __brelse(bh);
234}
235
236static inline void bforget(struct buffer_head *bh)
237{
238 if (bh)
239 __bforget(bh);
240}
241
242static inline struct buffer_head *
243sb_bread(struct super_block *sb, sector_t block)
244{
245 return __bread(sb->s_bdev, block, sb->s_blocksize);
246}
247
248static inline void
249sb_breadahead(struct super_block *sb, sector_t block)
250{
251 __breadahead(sb->s_bdev, block, sb->s_blocksize);
252}
253
254static inline struct buffer_head *
255sb_getblk(struct super_block *sb, sector_t block)
256{
257 return __getblk(sb->s_bdev, block, sb->s_blocksize);
258}
259
260static inline struct buffer_head *
261sb_find_get_block(struct super_block *sb, sector_t block)
262{
263 return __find_get_block(sb->s_bdev, block, sb->s_blocksize);
264}
265
266static inline void
267map_bh(struct buffer_head *bh, struct super_block *sb, sector_t block)
268{
269 set_buffer_mapped(bh);
270 bh->b_bdev = sb->s_bdev;
271 bh->b_blocknr = block;
272 bh->b_size = sb->s_blocksize;
273}
274
275static inline void wait_on_buffer(struct buffer_head *bh)
276{
277 might_sleep();
278 if (buffer_locked(bh))
279 __wait_on_buffer(bh);
280}
281
282static inline int trylock_buffer(struct buffer_head *bh)
283{
284 return likely(!test_and_set_bit_lock(BH_Lock, &bh->b_state));
285}
286
287static inline void lock_buffer(struct buffer_head *bh)
288{
289 might_sleep();
290 if (!trylock_buffer(bh))
291 __lock_buffer(bh);
292}
293
294extern int __set_page_dirty_buffers(struct page *page);
295
296#else
297
298static inline void buffer_init(void) {}
299static inline int try_to_free_buffers(struct page *page) { return 1; }
300static inline int inode_has_buffers(struct inode *inode) { return 0; }
301static inline void invalidate_inode_buffers(struct inode *inode) {}
302static inline int remove_inode_buffers(struct inode *inode) { return 1; }
303static inline int sync_mapping_buffers(struct address_space *mapping) { return 0; }
304
305#endif
306#endif