buffer_head.h 10 KB

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