buffer_head.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * include/linux/buffer_head.h
  3. *
  4. * Everything to do with buffer_heads.
  5. */
  6. #ifndef _LINUX_BUFFER_HEAD_H
  7. #define _LINUX_BUFFER_HEAD_H
  8. #include <linux/types.h>
  9. #include <linux/fs.h>
  10. #include <linux/linkage.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/wait.h>
  13. #include <asm/atomic.h>
  14. enum bh_state_bits {
  15. BH_Uptodate, /* Contains valid data */
  16. BH_Dirty, /* Is dirty */
  17. BH_Lock, /* Is locked */
  18. BH_Req, /* Has been submitted for I/O */
  19. BH_Mapped, /* Has a disk mapping */
  20. BH_New, /* Disk mapping was newly created by get_block */
  21. BH_Async_Read, /* Is under end_buffer_async_read I/O */
  22. BH_Async_Write, /* Is under end_buffer_async_write I/O */
  23. BH_Delay, /* Buffer is not yet allocated on disk */
  24. BH_Boundary, /* Block is followed by a discontiguity */
  25. BH_Write_EIO, /* I/O error on write */
  26. BH_Ordered, /* ordered write */
  27. BH_Eopnotsupp, /* operation not supported (barrier) */
  28. BH_PrivateStart,/* not a state bit, but the first bit available
  29. * for private allocation by other entities
  30. */
  31. };
  32. #define MAX_BUF_PER_PAGE (PAGE_CACHE_SIZE / 512)
  33. struct page;
  34. struct buffer_head;
  35. struct address_space;
  36. typedef void (bh_end_io_t)(struct buffer_head *bh, int uptodate);
  37. /*
  38. * Keep related fields in common cachelines. The most commonly accessed
  39. * field (b_state) goes at the start so the compiler does not generate
  40. * indexed addressing for it.
  41. */
  42. struct buffer_head {
  43. /* First cache line: */
  44. unsigned long b_state; /* buffer state bitmap (see above) */
  45. struct buffer_head *b_this_page;/* circular list of page's buffers */
  46. struct page *b_page; /* the page this bh is mapped to */
  47. atomic_t b_count; /* users using this block */
  48. u32 b_size; /* block size */
  49. sector_t b_blocknr; /* block number */
  50. char *b_data; /* pointer to data block */
  51. struct block_device *b_bdev;
  52. bh_end_io_t *b_end_io; /* I/O completion */
  53. void *b_private; /* reserved for b_end_io */
  54. struct list_head b_assoc_buffers; /* associated with another mapping */
  55. };
  56. /*
  57. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  58. * and buffer_foo() functions.
  59. */
  60. #define BUFFER_FNS(bit, name) \
  61. static inline void set_buffer_##name(struct buffer_head *bh) \
  62. { \
  63. set_bit(BH_##bit, &(bh)->b_state); \
  64. } \
  65. static inline void clear_buffer_##name(struct buffer_head *bh) \
  66. { \
  67. clear_bit(BH_##bit, &(bh)->b_state); \
  68. } \
  69. static inline int buffer_##name(const struct buffer_head *bh) \
  70. { \
  71. return test_bit(BH_##bit, &(bh)->b_state); \
  72. }
  73. /*
  74. * test_set_buffer_foo() and test_clear_buffer_foo()
  75. */
  76. #define TAS_BUFFER_FNS(bit, name) \
  77. static inline int test_set_buffer_##name(struct buffer_head *bh) \
  78. { \
  79. return test_and_set_bit(BH_##bit, &(bh)->b_state); \
  80. } \
  81. static inline int test_clear_buffer_##name(struct buffer_head *bh) \
  82. { \
  83. return test_and_clear_bit(BH_##bit, &(bh)->b_state); \
  84. } \
  85. /*
  86. * Emit the buffer bitops functions. Note that there are also functions
  87. * of the form "mark_buffer_foo()". These are higher-level functions which
  88. * do something in addition to setting a b_state bit.
  89. */
  90. BUFFER_FNS(Uptodate, uptodate)
  91. BUFFER_FNS(Dirty, dirty)
  92. TAS_BUFFER_FNS(Dirty, dirty)
  93. BUFFER_FNS(Lock, locked)
  94. TAS_BUFFER_FNS(Lock, locked)
  95. BUFFER_FNS(Req, req)
  96. TAS_BUFFER_FNS(Req, req)
  97. BUFFER_FNS(Mapped, mapped)
  98. BUFFER_FNS(New, new)
  99. BUFFER_FNS(Async_Read, async_read)
  100. BUFFER_FNS(Async_Write, async_write)
  101. BUFFER_FNS(Delay, delay)
  102. BUFFER_FNS(Boundary, boundary)
  103. BUFFER_FNS(Write_EIO, write_io_error)
  104. BUFFER_FNS(Ordered, ordered)
  105. BUFFER_FNS(Eopnotsupp, eopnotsupp)
  106. #define bh_offset(bh) ((unsigned long)(bh)->b_data & ~PAGE_MASK)
  107. #define touch_buffer(bh) mark_page_accessed(bh->b_page)
  108. /* If we *know* page->private refers to buffer_heads */
  109. #define page_buffers(page) \
  110. ({ \
  111. BUG_ON(!PagePrivate(page)); \
  112. ((struct buffer_head *)(page)->private); \
  113. })
  114. #define page_has_buffers(page) PagePrivate(page)
  115. /*
  116. * Declarations
  117. */
  118. void FASTCALL(mark_buffer_dirty(struct buffer_head *bh));
  119. void init_buffer(struct buffer_head *, bh_end_io_t *, void *);
  120. void set_bh_page(struct buffer_head *bh,
  121. struct page *page, unsigned long offset);
  122. int try_to_free_buffers(struct page *);
  123. struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
  124. int retry);
  125. void create_empty_buffers(struct page *, unsigned long,
  126. unsigned long b_state);
  127. void end_buffer_read_sync(struct buffer_head *bh, int uptodate);
  128. void end_buffer_write_sync(struct buffer_head *bh, int uptodate);
  129. void end_buffer_async_write(struct buffer_head *bh, int uptodate);
  130. /* Things to do with buffers at mapping->private_list */
  131. void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode);
  132. int inode_has_buffers(struct inode *);
  133. void invalidate_inode_buffers(struct inode *);
  134. int remove_inode_buffers(struct inode *inode);
  135. int sync_mapping_buffers(struct address_space *mapping);
  136. void unmap_underlying_metadata(struct block_device *bdev, sector_t block);
  137. void mark_buffer_async_write(struct buffer_head *bh);
  138. void invalidate_bdev(struct block_device *, int);
  139. int sync_blockdev(struct block_device *bdev);
  140. void __wait_on_buffer(struct buffer_head *);
  141. wait_queue_head_t *bh_waitq_head(struct buffer_head *bh);
  142. int fsync_bdev(struct block_device *);
  143. struct super_block *freeze_bdev(struct block_device *);
  144. void thaw_bdev(struct block_device *, struct super_block *);
  145. int fsync_super(struct super_block *);
  146. int fsync_no_super(struct block_device *);
  147. struct buffer_head *__find_get_block(struct block_device *, sector_t, int);
  148. struct buffer_head * __getblk(struct block_device *, sector_t, int);
  149. void __brelse(struct buffer_head *);
  150. void __bforget(struct buffer_head *);
  151. void __breadahead(struct block_device *, sector_t block, int size);
  152. struct buffer_head *__bread(struct block_device *, sector_t block, int size);
  153. struct buffer_head *alloc_buffer_head(unsigned int __nocast gfp_flags);
  154. void free_buffer_head(struct buffer_head * bh);
  155. void FASTCALL(unlock_buffer(struct buffer_head *bh));
  156. void FASTCALL(__lock_buffer(struct buffer_head *bh));
  157. void ll_rw_block(int, int, struct buffer_head * bh[]);
  158. int sync_dirty_buffer(struct buffer_head *bh);
  159. int submit_bh(int, struct buffer_head *);
  160. void write_boundary_block(struct block_device *bdev,
  161. sector_t bblock, unsigned blocksize);
  162. extern int buffer_heads_over_limit;
  163. /*
  164. * Generic address_space_operations implementations for buffer_head-backed
  165. * address_spaces.
  166. */
  167. int try_to_release_page(struct page * page, int gfp_mask);
  168. int block_invalidatepage(struct page *page, unsigned long offset);
  169. int block_write_full_page(struct page *page, get_block_t *get_block,
  170. struct writeback_control *wbc);
  171. int block_read_full_page(struct page*, get_block_t*);
  172. int block_prepare_write(struct page*, unsigned, unsigned, get_block_t*);
  173. int cont_prepare_write(struct page*, unsigned, unsigned, get_block_t*,
  174. loff_t *);
  175. int generic_cont_expand(struct inode *inode, loff_t size) ;
  176. int block_commit_write(struct page *page, unsigned from, unsigned to);
  177. int block_sync_page(struct page *);
  178. sector_t generic_block_bmap(struct address_space *, sector_t, get_block_t *);
  179. int generic_commit_write(struct file *, struct page *, unsigned, unsigned);
  180. int block_truncate_page(struct address_space *, loff_t, get_block_t *);
  181. int file_fsync(struct file *, struct dentry *, int);
  182. int nobh_prepare_write(struct page*, unsigned, unsigned, get_block_t*);
  183. int nobh_commit_write(struct file *, struct page *, unsigned, unsigned);
  184. int nobh_truncate_page(struct address_space *, loff_t);
  185. int nobh_writepage(struct page *page, get_block_t *get_block,
  186. struct writeback_control *wbc);
  187. /*
  188. * inline definitions
  189. */
  190. static inline void attach_page_buffers(struct page *page,
  191. struct buffer_head *head)
  192. {
  193. page_cache_get(page);
  194. SetPagePrivate(page);
  195. page->private = (unsigned long)head;
  196. }
  197. static inline void get_bh(struct buffer_head *bh)
  198. {
  199. atomic_inc(&bh->b_count);
  200. }
  201. static inline void put_bh(struct buffer_head *bh)
  202. {
  203. smp_mb__before_atomic_dec();
  204. atomic_dec(&bh->b_count);
  205. }
  206. static inline void brelse(struct buffer_head *bh)
  207. {
  208. if (bh)
  209. __brelse(bh);
  210. }
  211. static inline void bforget(struct buffer_head *bh)
  212. {
  213. if (bh)
  214. __bforget(bh);
  215. }
  216. static inline struct buffer_head *
  217. sb_bread(struct super_block *sb, sector_t block)
  218. {
  219. return __bread(sb->s_bdev, block, sb->s_blocksize);
  220. }
  221. static inline void
  222. sb_breadahead(struct super_block *sb, sector_t block)
  223. {
  224. __breadahead(sb->s_bdev, block, sb->s_blocksize);
  225. }
  226. static inline struct buffer_head *
  227. sb_getblk(struct super_block *sb, sector_t block)
  228. {
  229. return __getblk(sb->s_bdev, block, sb->s_blocksize);
  230. }
  231. static inline struct buffer_head *
  232. sb_find_get_block(struct super_block *sb, sector_t block)
  233. {
  234. return __find_get_block(sb->s_bdev, block, sb->s_blocksize);
  235. }
  236. static inline void
  237. map_bh(struct buffer_head *bh, struct super_block *sb, sector_t block)
  238. {
  239. set_buffer_mapped(bh);
  240. bh->b_bdev = sb->s_bdev;
  241. bh->b_blocknr = block;
  242. }
  243. /*
  244. * Calling wait_on_buffer() for a zero-ref buffer is illegal, so we call into
  245. * __wait_on_buffer() just to trip a debug check. Because debug code in inline
  246. * functions is bloaty.
  247. */
  248. static inline void wait_on_buffer(struct buffer_head *bh)
  249. {
  250. might_sleep();
  251. if (buffer_locked(bh) || atomic_read(&bh->b_count) == 0)
  252. __wait_on_buffer(bh);
  253. }
  254. static inline void lock_buffer(struct buffer_head *bh)
  255. {
  256. might_sleep();
  257. if (test_set_buffer_locked(bh))
  258. __lock_buffer(bh);
  259. }
  260. #endif /* _LINUX_BUFFER_HEAD_H */