buffer_head.h 9.8 KB

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