1
2
3
4
5
6
7#ifndef _LINUX_BUFFER_HEAD_H
8#define _LINUX_BUFFER_HEAD_H
9
10#include <linux/types.h>
11#include <linux/fs.h>
12#include <linux/linkage.h>
13#include <linux/pagemap.h>
14#include <linux/wait.h>
15#include <linux/atomic.h>
16
17#ifdef CONFIG_BLOCK
18
19enum bh_state_bits {
20 BH_Uptodate,
21 BH_Dirty,
22 BH_Lock,
23 BH_Req,
24 BH_Uptodate_Lock,
25
26
27
28 BH_Mapped,
29 BH_New,
30 BH_Async_Read,
31 BH_Async_Write,
32 BH_Delay,
33 BH_Boundary,
34 BH_Write_EIO,
35 BH_Unwritten,
36 BH_Quiet,
37 BH_Meta,
38 BH_Prio,
39 BH_Defer_Completion,
40
41 BH_PrivateStart,
42
43
44};
45
46#define MAX_BUF_PER_PAGE (PAGE_SIZE / 512)
47
48struct page;
49struct buffer_head;
50struct address_space;
51typedef void (bh_end_io_t)(struct buffer_head *bh, int uptodate);
52
53
54
55
56
57
58
59
60
61
62struct buffer_head {
63 unsigned long b_state;
64 struct buffer_head *b_this_page;
65 struct page *b_page;
66
67 sector_t b_blocknr;
68 size_t b_size;
69 char *b_data;
70
71 struct block_device *b_bdev;
72 bh_end_io_t *b_end_io;
73 void *b_private;
74 struct list_head b_assoc_buffers;
75 struct address_space *b_assoc_map;
76
77 atomic_t b_count;
78};
79
80
81
82
83
84#define BUFFER_FNS(bit, name) \
85static __always_inline void set_buffer_##name(struct buffer_head *bh) \
86{ \
87 set_bit(BH_##bit, &(bh)->b_state); \
88} \
89static __always_inline void clear_buffer_##name(struct buffer_head *bh) \
90{ \
91 clear_bit(BH_##bit, &(bh)->b_state); \
92} \
93static __always_inline int buffer_##name(const struct buffer_head *bh) \
94{ \
95 return test_bit(BH_##bit, &(bh)->b_state); \
96}
97
98
99
100
101#define TAS_BUFFER_FNS(bit, name) \
102static __always_inline int test_set_buffer_##name(struct buffer_head *bh) \
103{ \
104 return test_and_set_bit(BH_##bit, &(bh)->b_state); \
105} \
106static __always_inline int test_clear_buffer_##name(struct buffer_head *bh) \
107{ \
108 return test_and_clear_bit(BH_##bit, &(bh)->b_state); \
109} \
110
111
112
113
114
115
116BUFFER_FNS(Uptodate, uptodate)
117BUFFER_FNS(Dirty, dirty)
118TAS_BUFFER_FNS(Dirty, dirty)
119BUFFER_FNS(Lock, locked)
120BUFFER_FNS(Req, req)
121TAS_BUFFER_FNS(Req, req)
122BUFFER_FNS(Mapped, mapped)
123BUFFER_FNS(New, new)
124BUFFER_FNS(Async_Read, async_read)
125BUFFER_FNS(Async_Write, async_write)
126BUFFER_FNS(Delay, delay)
127BUFFER_FNS(Boundary, boundary)
128BUFFER_FNS(Write_EIO, write_io_error)
129BUFFER_FNS(Unwritten, unwritten)
130BUFFER_FNS(Meta, meta)
131BUFFER_FNS(Prio, prio)
132BUFFER_FNS(Defer_Completion, defer_completion)
133
134#define bh_offset(bh) ((unsigned long)(bh)->b_data & ~PAGE_MASK)
135
136
137#define page_buffers(page) \
138 ({ \
139 BUG_ON(!PagePrivate(page)); \
140 ((struct buffer_head *)page_private(page)); \
141 })
142#define page_has_buffers(page) PagePrivate(page)
143
144void buffer_check_dirty_writeback(struct page *page,
145 bool *dirty, bool *writeback);
146
147
148
149
150
151void mark_buffer_dirty(struct buffer_head *bh);
152void init_buffer(struct buffer_head *, bh_end_io_t *, void *);
153void touch_buffer(struct buffer_head *bh);
154void set_bh_page(struct buffer_head *bh,
155 struct page *page, unsigned long offset);
156int try_to_free_buffers(struct page *);
157struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
158 int retry);
159void create_empty_buffers(struct page *, unsigned long,
160 unsigned long b_state);
161void end_buffer_read_sync(struct buffer_head *bh, int uptodate);
162void end_buffer_write_sync(struct buffer_head *bh, int uptodate);
163void end_buffer_async_write(struct buffer_head *bh, int uptodate);
164
165
166void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode);
167int inode_has_buffers(struct inode *);
168void invalidate_inode_buffers(struct inode *);
169int remove_inode_buffers(struct inode *inode);
170int sync_mapping_buffers(struct address_space *mapping);
171void clean_bdev_aliases(struct block_device *bdev, sector_t block,
172 sector_t len);
173static inline void clean_bdev_bh_alias(struct buffer_head *bh)
174{
175 clean_bdev_aliases(bh->b_bdev, bh->b_blocknr, 1);
176}
177
178void mark_buffer_async_write(struct buffer_head *bh);
179void __wait_on_buffer(struct buffer_head *);
180wait_queue_head_t *bh_waitq_head(struct buffer_head *bh);
181struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block,
182 unsigned size);
183struct buffer_head *__getblk_gfp(struct block_device *bdev, sector_t block,
184 unsigned size, gfp_t gfp);
185void __brelse(struct buffer_head *);
186void __bforget(struct buffer_head *);
187void __breadahead(struct block_device *, sector_t block, unsigned int size);
188struct buffer_head *__bread_gfp(struct block_device *,
189 sector_t block, unsigned size, gfp_t gfp);
190void invalidate_bh_lrus(void);
191struct buffer_head *alloc_buffer_head(gfp_t gfp_flags);
192void free_buffer_head(struct buffer_head * bh);
193void unlock_buffer(struct buffer_head *bh);
194void __lock_buffer(struct buffer_head *bh);
195void ll_rw_block(int, int, int, struct buffer_head * bh[]);
196int sync_dirty_buffer(struct buffer_head *bh);
197int __sync_dirty_buffer(struct buffer_head *bh, int op_flags);
198void write_dirty_buffer(struct buffer_head *bh, int op_flags);
199int submit_bh(int, int, struct buffer_head *);
200void write_boundary_block(struct block_device *bdev,
201 sector_t bblock, unsigned blocksize);
202int bh_uptodate_or_lock(struct buffer_head *bh);
203int bh_submit_read(struct buffer_head *bh);
204
205extern int buffer_heads_over_limit;
206
207
208
209
210
211void block_invalidatepage(struct page *page, unsigned int offset,
212 unsigned int length);
213int block_write_full_page(struct page *page, get_block_t *get_block,
214 struct writeback_control *wbc);
215int __block_write_full_page(struct inode *inode, struct page *page,
216 get_block_t *get_block, struct writeback_control *wbc,
217 bh_end_io_t *handler);
218int block_read_full_page(struct page*, get_block_t*);
219int block_is_partially_uptodate(struct page *page, unsigned long from,
220 unsigned long count);
221int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
222 unsigned flags, struct page **pagep, get_block_t *get_block);
223int __block_write_begin(struct page *page, loff_t pos, unsigned len,
224 get_block_t *get_block);
225int block_write_end(struct file *, struct address_space *,
226 loff_t, unsigned, unsigned,
227 struct page *, void *);
228int generic_write_end(struct file *, struct address_space *,
229 loff_t, unsigned, unsigned,
230 struct page *, void *);
231void page_zero_new_buffers(struct page *page, unsigned from, unsigned to);
232int cont_write_begin(struct file *, struct address_space *, loff_t,
233 unsigned, unsigned, struct page **, void **,
234 get_block_t *, loff_t *);
235int generic_cont_expand_simple(struct inode *inode, loff_t size);
236int block_commit_write(struct page *page, unsigned from, unsigned to);
237int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
238 get_block_t get_block);
239
240static inline int block_page_mkwrite_return(int err)
241{
242 if (err == 0)
243 return VM_FAULT_LOCKED;
244 if (err == -EFAULT || err == -EAGAIN)
245 return VM_FAULT_NOPAGE;
246 if (err == -ENOMEM)
247 return VM_FAULT_OOM;
248
249 return VM_FAULT_SIGBUS;
250}
251sector_t generic_block_bmap(struct address_space *, sector_t, get_block_t *);
252int block_truncate_page(struct address_space *, loff_t, get_block_t *);
253int nobh_write_begin(struct address_space *, loff_t, unsigned, unsigned,
254 struct page **, void **, get_block_t*);
255int nobh_write_end(struct file *, struct address_space *,
256 loff_t, unsigned, unsigned,
257 struct page *, void *);
258int nobh_truncate_page(struct address_space *, loff_t, get_block_t *);
259int nobh_writepage(struct page *page, get_block_t *get_block,
260 struct writeback_control *wbc);
261
262void buffer_init(void);
263
264
265
266
267
268static inline void attach_page_buffers(struct page *page,
269 struct buffer_head *head)
270{
271 get_page(page);
272 SetPagePrivate(page);
273 set_page_private(page, (unsigned long)head);
274}
275
276static inline void get_bh(struct buffer_head *bh)
277{
278 atomic_inc(&bh->b_count);
279}
280
281static inline void put_bh(struct buffer_head *bh)
282{
283 smp_mb__before_atomic();
284 atomic_dec(&bh->b_count);
285}
286
287static inline void brelse(struct buffer_head *bh)
288{
289 if (bh)
290 __brelse(bh);
291}
292
293static inline void bforget(struct buffer_head *bh)
294{
295 if (bh)
296 __bforget(bh);
297}
298
299static inline struct buffer_head *
300sb_bread(struct super_block *sb, sector_t block)
301{
302 return __bread_gfp(sb->s_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
303}
304
305static inline struct buffer_head *
306sb_bread_unmovable(struct super_block *sb, sector_t block)
307{
308 return __bread_gfp(sb->s_bdev, block, sb->s_blocksize, 0);
309}
310
311static inline void
312sb_breadahead(struct super_block *sb, sector_t block)
313{
314 __breadahead(sb->s_bdev, block, sb->s_blocksize);
315}
316
317static inline struct buffer_head *
318sb_getblk(struct super_block *sb, sector_t block)
319{
320 return __getblk_gfp(sb->s_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
321}
322
323
324static inline struct buffer_head *
325sb_getblk_gfp(struct super_block *sb, sector_t block, gfp_t gfp)
326{
327 return __getblk_gfp(sb->s_bdev, block, sb->s_blocksize, gfp);
328}
329
330static inline struct buffer_head *
331sb_find_get_block(struct super_block *sb, sector_t block)
332{
333 return __find_get_block(sb->s_bdev, block, sb->s_blocksize);
334}
335
336static inline void
337map_bh(struct buffer_head *bh, struct super_block *sb, sector_t block)
338{
339 set_buffer_mapped(bh);
340 bh->b_bdev = sb->s_bdev;
341 bh->b_blocknr = block;
342 bh->b_size = sb->s_blocksize;
343}
344
345static inline void wait_on_buffer(struct buffer_head *bh)
346{
347 might_sleep();
348 if (buffer_locked(bh))
349 __wait_on_buffer(bh);
350}
351
352static inline int trylock_buffer(struct buffer_head *bh)
353{
354 return likely(!test_and_set_bit_lock(BH_Lock, &bh->b_state));
355}
356
357static inline void lock_buffer(struct buffer_head *bh)
358{
359 might_sleep();
360 if (!trylock_buffer(bh))
361 __lock_buffer(bh);
362}
363
364static inline struct buffer_head *getblk_unmovable(struct block_device *bdev,
365 sector_t block,
366 unsigned size)
367{
368 return __getblk_gfp(bdev, block, size, 0);
369}
370
371static inline struct buffer_head *__getblk(struct block_device *bdev,
372 sector_t block,
373 unsigned size)
374{
375 return __getblk_gfp(bdev, block, size, __GFP_MOVABLE);
376}
377
378
379
380
381
382
383
384
385
386
387
388static inline struct buffer_head *
389__bread(struct block_device *bdev, sector_t block, unsigned size)
390{
391 return __bread_gfp(bdev, block, size, __GFP_MOVABLE);
392}
393
394extern int __set_page_dirty_buffers(struct page *page);
395
396#else
397
398static inline void buffer_init(void) {}
399static inline int try_to_free_buffers(struct page *page) { return 1; }
400static inline int inode_has_buffers(struct inode *inode) { return 0; }
401static inline void invalidate_inode_buffers(struct inode *inode) {}
402static inline int remove_inode_buffers(struct inode *inode) { return 1; }
403static inline int sync_mapping_buffers(struct address_space *mapping) { return 0; }
404
405#endif
406#endif
407