Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame^] | 1 | |
| 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 | |
| 14 | enum 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 | |
| 36 | struct page; |
| 37 | struct buffer_head; |
| 38 | struct address_space; |
| 39 | typedef void (bh_end_io_t)(struct buffer_head *bh, int uptodate); |
| 40 | |
| 41 | struct 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) \ |
| 59 | static inline void set_buffer_##name(struct buffer_head *bh) \ |
| 60 | { \ |
| 61 | set_bit(BH_##bit, &(bh)->b_state); \ |
| 62 | } \ |
| 63 | static inline void clear_buffer_##name(struct buffer_head *bh) \ |
| 64 | { \ |
| 65 | clear_bit(BH_##bit, &(bh)->b_state); \ |
| 66 | } \ |
| 67 | static 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) \ |
| 73 | static inline int test_set_buffer_##name(struct buffer_head *bh) \ |
| 74 | { \ |
| 75 | return test_and_set_bit(BH_##bit, &(bh)->b_state); \ |
| 76 | } \ |
| 77 | static inline int test_clear_buffer_##name(struct buffer_head *bh) \ |
| 78 | { \ |
| 79 | return test_and_clear_bit(BH_##bit, &(bh)->b_state); \ |
| 80 | } \ |
| 81 | |
| 82 | BUFFER_FNS(Uptodate, uptodate) |
| 83 | BUFFER_FNS(Dirty, dirty) |
| 84 | TAS_BUFFER_FNS(Dirty, dirty) |
| 85 | BUFFER_FNS(Lock, locked) |
| 86 | BUFFER_FNS(Req, req) |
| 87 | TAS_BUFFER_FNS(Req, req) |
| 88 | BUFFER_FNS(Mapped, mapped) |
| 89 | BUFFER_FNS(New, new) |
| 90 | BUFFER_FNS(Async_Read, async_read) |
| 91 | BUFFER_FNS(Async_Write, async_write) |
| 92 | BUFFER_FNS(Delay, delay) |
| 93 | BUFFER_FNS(Boundary, boundary) |
| 94 | BUFFER_FNS(Write_EIO, write_io_error) |
| 95 | BUFFER_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 | |
| 108 | void mark_buffer_dirty(struct buffer_head *bh); |
| 109 | void init_buffer(struct buffer_head *, bh_end_io_t *, void *); |
| 110 | void set_bh_page(struct buffer_head *bh, |
| 111 | struct page *page, unsigned long offset); |
| 112 | int try_to_free_buffers(struct page *); |
| 113 | struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size, |
| 114 | int retry); |
| 115 | void create_empty_buffers(struct page *, unsigned long, |
| 116 | unsigned long b_state); |
| 117 | void end_buffer_read_sync(struct buffer_head *bh, int uptodate); |
| 118 | void end_buffer_write_sync(struct buffer_head *bh, int uptodate); |
| 119 | void end_buffer_async_write(struct buffer_head *bh, int uptodate); |
| 120 | |
| 121 | void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode); |
| 122 | int inode_has_buffers(struct inode *); |
| 123 | void invalidate_inode_buffers(struct inode *); |
| 124 | int remove_inode_buffers(struct inode *inode); |
| 125 | int sync_mapping_buffers(struct address_space *mapping); |
| 126 | void unmap_underlying_metadata(struct block_device *bdev, sector_t block); |
| 127 | |
| 128 | void mark_buffer_async_write(struct buffer_head *bh); |
| 129 | void __wait_on_buffer(struct buffer_head *); |
| 130 | wait_queue_head_t *bh_waitq_head(struct buffer_head *bh); |
| 131 | struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block, |
| 132 | unsigned size); |
| 133 | struct buffer_head *__getblk(struct block_device *bdev, sector_t block, |
| 134 | unsigned size); |
| 135 | void __brelse(struct buffer_head *); |
| 136 | void __bforget(struct buffer_head *); |
| 137 | void __breadahead(struct block_device *, sector_t block, unsigned int size); |
| 138 | struct buffer_head *__bread(struct block_device *, sector_t block, unsigned size); |
| 139 | void invalidate_bh_lrus(void); |
| 140 | struct buffer_head *alloc_buffer_head(gfp_t gfp_flags); |
| 141 | void free_buffer_head(struct buffer_head * bh); |
| 142 | void unlock_buffer(struct buffer_head *bh); |
| 143 | void __lock_buffer(struct buffer_head *bh); |
| 144 | void ll_rw_block(int, int, struct buffer_head * bh[]); |
| 145 | int sync_dirty_buffer(struct buffer_head *bh); |
| 146 | int __sync_dirty_buffer(struct buffer_head *bh, int rw); |
| 147 | void write_dirty_buffer(struct buffer_head *bh, int rw); |
| 148 | int submit_bh(int, struct buffer_head *); |
| 149 | void write_boundary_block(struct block_device *bdev, |
| 150 | sector_t bblock, unsigned blocksize); |
| 151 | int bh_uptodate_or_lock(struct buffer_head *bh); |
| 152 | int bh_submit_read(struct buffer_head *bh); |
| 153 | |
| 154 | extern int buffer_heads_over_limit; |
| 155 | |
| 156 | void block_invalidatepage(struct page *page, unsigned long offset); |
| 157 | int block_write_full_page(struct page *page, get_block_t *get_block, |
| 158 | struct writeback_control *wbc); |
| 159 | int block_write_full_page_endio(struct page *page, get_block_t *get_block, |
| 160 | struct writeback_control *wbc, bh_end_io_t *handler); |
| 161 | int block_read_full_page(struct page*, get_block_t*); |
| 162 | int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc, |
| 163 | unsigned long from); |
| 164 | int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len, |
| 165 | unsigned flags, struct page **pagep, get_block_t *get_block); |
| 166 | int __block_write_begin(struct page *page, loff_t pos, unsigned len, |
| 167 | get_block_t *get_block); |
| 168 | int block_write_end(struct file *, struct address_space *, |
| 169 | loff_t, unsigned, unsigned, |
| 170 | struct page *, void *); |
| 171 | int generic_write_end(struct file *, struct address_space *, |
| 172 | loff_t, unsigned, unsigned, |
| 173 | struct page *, void *); |
| 174 | void page_zero_new_buffers(struct page *page, unsigned from, unsigned to); |
| 175 | int cont_write_begin(struct file *, struct address_space *, loff_t, |
| 176 | unsigned, unsigned, struct page **, void **, |
| 177 | get_block_t *, loff_t *); |
| 178 | int generic_cont_expand_simple(struct inode *inode, loff_t size); |
| 179 | int block_commit_write(struct page *page, unsigned from, unsigned to); |
| 180 | int __block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf, |
| 181 | get_block_t get_block); |
| 182 | int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf, |
| 183 | get_block_t get_block); |
| 184 | static 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 | } |
| 197 | sector_t generic_block_bmap(struct address_space *, sector_t, get_block_t *); |
| 198 | int block_truncate_page(struct address_space *, loff_t, get_block_t *); |
| 199 | int nobh_write_begin(struct address_space *, loff_t, unsigned, unsigned, |
| 200 | struct page **, void **, get_block_t*); |
| 201 | int nobh_write_end(struct file *, struct address_space *, |
| 202 | loff_t, unsigned, unsigned, |
| 203 | struct page *, void *); |
| 204 | int nobh_truncate_page(struct address_space *, loff_t, get_block_t *); |
| 205 | int nobh_writepage(struct page *page, get_block_t *get_block, |
| 206 | struct writeback_control *wbc); |
| 207 | |
| 208 | void buffer_init(void); |
| 209 | |
| 210 | |
| 211 | static 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 | |
| 219 | static inline void get_bh(struct buffer_head *bh) |
| 220 | { |
| 221 | atomic_inc(&bh->b_count); |
| 222 | } |
| 223 | |
| 224 | static inline void put_bh(struct buffer_head *bh) |
| 225 | { |
| 226 | smp_mb__before_atomic_dec(); |
| 227 | atomic_dec(&bh->b_count); |
| 228 | } |
| 229 | |
| 230 | static inline void brelse(struct buffer_head *bh) |
| 231 | { |
| 232 | if (bh) |
| 233 | __brelse(bh); |
| 234 | } |
| 235 | |
| 236 | static inline void bforget(struct buffer_head *bh) |
| 237 | { |
| 238 | if (bh) |
| 239 | __bforget(bh); |
| 240 | } |
| 241 | |
| 242 | static inline struct buffer_head * |
| 243 | sb_bread(struct super_block *sb, sector_t block) |
| 244 | { |
| 245 | return __bread(sb->s_bdev, block, sb->s_blocksize); |
| 246 | } |
| 247 | |
| 248 | static inline void |
| 249 | sb_breadahead(struct super_block *sb, sector_t block) |
| 250 | { |
| 251 | __breadahead(sb->s_bdev, block, sb->s_blocksize); |
| 252 | } |
| 253 | |
| 254 | static inline struct buffer_head * |
| 255 | sb_getblk(struct super_block *sb, sector_t block) |
| 256 | { |
| 257 | return __getblk(sb->s_bdev, block, sb->s_blocksize); |
| 258 | } |
| 259 | |
| 260 | static inline struct buffer_head * |
| 261 | sb_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 | |
| 266 | static inline void |
| 267 | map_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 | |
| 275 | static 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 | |
| 282 | static inline int trylock_buffer(struct buffer_head *bh) |
| 283 | { |
| 284 | return likely(!test_and_set_bit_lock(BH_Lock, &bh->b_state)); |
| 285 | } |
| 286 | |
| 287 | static inline void lock_buffer(struct buffer_head *bh) |
| 288 | { |
| 289 | might_sleep(); |
| 290 | if (!trylock_buffer(bh)) |
| 291 | __lock_buffer(bh); |
| 292 | } |
| 293 | |
| 294 | extern int __set_page_dirty_buffers(struct page *page); |
| 295 | |
| 296 | #else |
| 297 | |
| 298 | static inline void buffer_init(void) {} |
| 299 | static inline int try_to_free_buffers(struct page *page) { return 1; } |
| 300 | static inline int inode_has_buffers(struct inode *inode) { return 0; } |
| 301 | static inline void invalidate_inode_buffers(struct inode *inode) {} |
| 302 | static inline int remove_inode_buffers(struct inode *inode) { return 1; } |
| 303 | static inline int sync_mapping_buffers(struct address_space *mapping) { return 0; } |
| 304 | |
| 305 | #endif |
| 306 | #endif |