buffer_head.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * include/linux/buffer_head.h
  4. *
  5. * Everything to do with buffer_heads.
  6. */
  7. #ifndef _LINUX_BUFFER_HEAD_H
  8. #define _LINUX_BUFFER_HEAD_H
  9. #include <linux/types.h>
  10. #include <linux/fs.h>
  11. #include <linux/linkage.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/wait.h>
  14. #include <linux/atomic.h>
  15. #ifdef CONFIG_BLOCK
  16. enum bh_state_bits {
  17. BH_Uptodate, /* Contains valid data */
  18. BH_Dirty, /* Is dirty */
  19. BH_Lock, /* Is locked */
  20. BH_Req, /* Has been submitted for I/O */
  21. BH_Uptodate_Lock,/* Used by the first bh in a page, to serialise
  22. * IO completion of other buffers in the page
  23. */
  24. BH_Mapped, /* Has a disk mapping */
  25. BH_New, /* Disk mapping was newly created by get_block */
  26. BH_Async_Read, /* Is under end_buffer_async_read I/O */
  27. BH_Async_Write, /* Is under end_buffer_async_write I/O */
  28. BH_Delay, /* Buffer is not yet allocated on disk */
  29. BH_Boundary, /* Block is followed by a discontiguity */
  30. BH_Write_EIO, /* I/O error on write */
  31. BH_Unwritten, /* Buffer is allocated on disk but not written */
  32. BH_Quiet, /* Buffer Error Prinks to be quiet */
  33. BH_Meta, /* Buffer contains metadata */
  34. BH_Prio, /* Buffer should be submitted with REQ_PRIO */
  35. BH_Defer_Completion, /* Defer AIO completion to workqueue */
  36. BH_PrivateStart,/* not a state bit, but the first bit available
  37. * for private allocation by other entities
  38. */
  39. };
  40. #define MAX_BUF_PER_PAGE (PAGE_SIZE / 512)
  41. struct page;
  42. struct buffer_head;
  43. struct address_space;
  44. typedef void (bh_end_io_t)(struct buffer_head *bh, int uptodate);
  45. /*
  46. * Historically, a buffer_head was used to map a single block
  47. * within a page, and of course as the unit of I/O through the
  48. * filesystem and block layers. Nowadays the basic I/O unit
  49. * is the bio, and buffer_heads are used for extracting block
  50. * mappings (via a get_block_t call), for tracking state within
  51. * a page (via a page_mapping) and for wrapping bio submission
  52. * for backward compatibility reasons (e.g. submit_bh).
  53. */
  54. struct buffer_head {
  55. unsigned long b_state; /* buffer state bitmap (see above) */
  56. struct buffer_head *b_this_page;/* circular list of page's buffers */
  57. struct page *b_page; /* the page this bh is mapped to */
  58. sector_t b_blocknr; /* start block number */
  59. size_t b_size; /* size of mapping */
  60. char *b_data; /* pointer to data within the page */
  61. struct block_device *b_bdev;
  62. bh_end_io_t *b_end_io; /* I/O completion */
  63. void *b_private; /* reserved for b_end_io */
  64. struct list_head b_assoc_buffers; /* associated with another mapping */
  65. struct address_space *b_assoc_map; /* mapping this buffer is
  66. associated with */
  67. atomic_t b_count; /* users using this buffer_head */
  68. };
  69. /*
  70. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  71. * and buffer_foo() functions.
  72. * To avoid reset buffer flags that are already set, because that causes
  73. * a costly cache line transition, check the flag first.
  74. */
  75. #define BUFFER_FNS(bit, name) \
  76. static __always_inline void set_buffer_##name(struct buffer_head *bh) \
  77. { \
  78. if (!test_bit(BH_##bit, &(bh)->b_state)) \
  79. set_bit(BH_##bit, &(bh)->b_state); \
  80. } \
  81. static __always_inline void clear_buffer_##name(struct buffer_head *bh) \
  82. { \
  83. clear_bit(BH_##bit, &(bh)->b_state); \
  84. } \
  85. static __always_inline int buffer_##name(const struct buffer_head *bh) \
  86. { \
  87. return test_bit(BH_##bit, &(bh)->b_state); \
  88. }
  89. /*
  90. * test_set_buffer_foo() and test_clear_buffer_foo()
  91. */
  92. #define TAS_BUFFER_FNS(bit, name) \
  93. static __always_inline int test_set_buffer_##name(struct buffer_head *bh) \
  94. { \
  95. return test_and_set_bit(BH_##bit, &(bh)->b_state); \
  96. } \
  97. static __always_inline int test_clear_buffer_##name(struct buffer_head *bh) \
  98. { \
  99. return test_and_clear_bit(BH_##bit, &(bh)->b_state); \
  100. } \
  101. /*
  102. * Emit the buffer bitops functions. Note that there are also functions
  103. * of the form "mark_buffer_foo()". These are higher-level functions which
  104. * do something in addition to setting a b_state bit.
  105. */
  106. BUFFER_FNS(Uptodate, uptodate)
  107. BUFFER_FNS(Dirty, dirty)
  108. TAS_BUFFER_FNS(Dirty, dirty)
  109. BUFFER_FNS(Lock, locked)
  110. BUFFER_FNS(Req, req)
  111. TAS_BUFFER_FNS(Req, req)
  112. BUFFER_FNS(Mapped, mapped)
  113. BUFFER_FNS(New, new)
  114. BUFFER_FNS(Async_Read, async_read)
  115. BUFFER_FNS(Async_Write, async_write)
  116. BUFFER_FNS(Delay, delay)
  117. BUFFER_FNS(Boundary, boundary)
  118. BUFFER_FNS(Write_EIO, write_io_error)
  119. BUFFER_FNS(Unwritten, unwritten)
  120. BUFFER_FNS(Meta, meta)
  121. BUFFER_FNS(Prio, prio)
  122. BUFFER_FNS(Defer_Completion, defer_completion)
  123. #define bh_offset(bh) ((unsigned long)(bh)->b_data & ~PAGE_MASK)
  124. /* If we *know* page->private refers to buffer_heads */
  125. #define page_buffers(page) \
  126. ({ \
  127. BUG_ON(!PagePrivate(page)); \
  128. ((struct buffer_head *)page_private(page)); \
  129. })
  130. #define page_has_buffers(page) PagePrivate(page)
  131. void buffer_check_dirty_writeback(struct page *page,
  132. bool *dirty, bool *writeback);
  133. /*
  134. * Declarations
  135. */
  136. void mark_buffer_dirty(struct buffer_head *bh);
  137. void mark_buffer_write_io_error(struct buffer_head *bh);
  138. void touch_buffer(struct buffer_head *bh);
  139. void set_bh_page(struct buffer_head *bh,
  140. struct page *page, unsigned long offset);
  141. int try_to_free_buffers(struct page *);
  142. struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
  143. bool retry);
  144. void create_empty_buffers(struct page *, unsigned long,
  145. unsigned long b_state);
  146. void end_buffer_read_sync(struct buffer_head *bh, int uptodate);
  147. void end_buffer_write_sync(struct buffer_head *bh, int uptodate);
  148. void end_buffer_async_write(struct buffer_head *bh, int uptodate);
  149. /* Things to do with buffers at mapping->private_list */
  150. void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode);
  151. int inode_has_buffers(struct inode *);
  152. void invalidate_inode_buffers(struct inode *);
  153. int remove_inode_buffers(struct inode *inode);
  154. int sync_mapping_buffers(struct address_space *mapping);
  155. void clean_bdev_aliases(struct block_device *bdev, sector_t block,
  156. sector_t len);
  157. static inline void clean_bdev_bh_alias(struct buffer_head *bh)
  158. {
  159. clean_bdev_aliases(bh->b_bdev, bh->b_blocknr, 1);
  160. }
  161. void mark_buffer_async_write(struct buffer_head *bh);
  162. void __wait_on_buffer(struct buffer_head *);
  163. wait_queue_head_t *bh_waitq_head(struct buffer_head *bh);
  164. struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block,
  165. unsigned size);
  166. struct buffer_head *__getblk_gfp(struct block_device *bdev, sector_t block,
  167. unsigned size, gfp_t gfp);
  168. void __brelse(struct buffer_head *);
  169. void __bforget(struct buffer_head *);
  170. void __breadahead(struct block_device *, sector_t block, unsigned int size);
  171. struct buffer_head *__bread_gfp(struct block_device *,
  172. sector_t block, unsigned size, gfp_t gfp);
  173. void invalidate_bh_lrus(void);
  174. struct buffer_head *alloc_buffer_head(gfp_t gfp_flags);
  175. void free_buffer_head(struct buffer_head * bh);
  176. void unlock_buffer(struct buffer_head *bh);
  177. void __lock_buffer(struct buffer_head *bh);
  178. void ll_rw_block(int, int, int, struct buffer_head * bh[]);
  179. int sync_dirty_buffer(struct buffer_head *bh);
  180. int __sync_dirty_buffer(struct buffer_head *bh, int op_flags);
  181. void write_dirty_buffer(struct buffer_head *bh, int op_flags);
  182. int submit_bh(int, int, struct buffer_head *);
  183. void write_boundary_block(struct block_device *bdev,
  184. sector_t bblock, unsigned blocksize);
  185. int bh_uptodate_or_lock(struct buffer_head *bh);
  186. int bh_submit_read(struct buffer_head *bh);
  187. extern int buffer_heads_over_limit;
  188. /*
  189. * Generic address_space_operations implementations for buffer_head-backed
  190. * address_spaces.
  191. */
  192. void block_invalidatepage(struct page *page, unsigned int offset,
  193. unsigned int length);
  194. int block_write_full_page(struct page *page, get_block_t *get_block,
  195. struct writeback_control *wbc);
  196. int __block_write_full_page(struct inode *inode, struct page *page,
  197. get_block_t *get_block, struct writeback_control *wbc,
  198. bh_end_io_t *handler);
  199. int block_read_full_page(struct page*, get_block_t*);
  200. int block_is_partially_uptodate(struct page *page, unsigned long from,
  201. unsigned long count);
  202. int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
  203. unsigned flags, struct page **pagep, get_block_t *get_block);
  204. int __block_write_begin(struct page *page, loff_t pos, unsigned len,
  205. get_block_t *get_block);
  206. int block_write_end(struct file *, struct address_space *,
  207. loff_t, unsigned, unsigned,
  208. struct page *, void *);
  209. int generic_write_end(struct file *, struct address_space *,
  210. loff_t, unsigned, unsigned,
  211. struct page *, void *);
  212. void page_zero_new_buffers(struct page *page, unsigned from, unsigned to);
  213. void clean_page_buffers(struct page *page);
  214. int cont_write_begin(struct file *, struct address_space *, loff_t,
  215. unsigned, unsigned, struct page **, void **,
  216. get_block_t *, loff_t *);
  217. int generic_cont_expand_simple(struct inode *inode, loff_t size);
  218. int block_commit_write(struct page *page, unsigned from, unsigned to);
  219. int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  220. get_block_t get_block);
  221. /* Convert errno to return value from ->page_mkwrite() call */
  222. static inline int block_page_mkwrite_return(int err)
  223. {
  224. if (err == 0)
  225. return VM_FAULT_LOCKED;
  226. if (err == -EFAULT || err == -EAGAIN)
  227. return VM_FAULT_NOPAGE;
  228. if (err == -ENOMEM)
  229. return VM_FAULT_OOM;
  230. /* -ENOSPC, -EDQUOT, -EIO ... */
  231. return VM_FAULT_SIGBUS;
  232. }
  233. sector_t generic_block_bmap(struct address_space *, sector_t, get_block_t *);
  234. int block_truncate_page(struct address_space *, loff_t, get_block_t *);
  235. int nobh_write_begin(struct address_space *, loff_t, unsigned, unsigned,
  236. struct page **, void **, get_block_t*);
  237. int nobh_write_end(struct file *, struct address_space *,
  238. loff_t, unsigned, unsigned,
  239. struct page *, void *);
  240. int nobh_truncate_page(struct address_space *, loff_t, get_block_t *);
  241. int nobh_writepage(struct page *page, get_block_t *get_block,
  242. struct writeback_control *wbc);
  243. void buffer_init(void);
  244. /*
  245. * inline definitions
  246. */
  247. static inline void attach_page_buffers(struct page *page,
  248. struct buffer_head *head)
  249. {
  250. get_page(page);
  251. SetPagePrivate(page);
  252. set_page_private(page, (unsigned long)head);
  253. }
  254. static inline void get_bh(struct buffer_head *bh)
  255. {
  256. atomic_inc(&bh->b_count);
  257. }
  258. static inline void put_bh(struct buffer_head *bh)
  259. {
  260. smp_mb__before_atomic();
  261. atomic_dec(&bh->b_count);
  262. }
  263. static inline void brelse(struct buffer_head *bh)
  264. {
  265. if (bh)
  266. __brelse(bh);
  267. }
  268. static inline void bforget(struct buffer_head *bh)
  269. {
  270. if (bh)
  271. __bforget(bh);
  272. }
  273. static inline struct buffer_head *
  274. sb_bread(struct super_block *sb, sector_t block)
  275. {
  276. return __bread_gfp(sb->s_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
  277. }
  278. static inline struct buffer_head *
  279. sb_bread_unmovable(struct super_block *sb, sector_t block)
  280. {
  281. return __bread_gfp(sb->s_bdev, block, sb->s_blocksize, 0);
  282. }
  283. static inline void
  284. sb_breadahead(struct super_block *sb, sector_t block)
  285. {
  286. __breadahead(sb->s_bdev, block, sb->s_blocksize);
  287. }
  288. static inline struct buffer_head *
  289. sb_getblk(struct super_block *sb, sector_t block)
  290. {
  291. return __getblk_gfp(sb->s_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
  292. }
  293. static inline struct buffer_head *
  294. sb_getblk_gfp(struct super_block *sb, sector_t block, gfp_t gfp)
  295. {
  296. return __getblk_gfp(sb->s_bdev, block, sb->s_blocksize, gfp);
  297. }
  298. static inline struct buffer_head *
  299. sb_find_get_block(struct super_block *sb, sector_t block)
  300. {
  301. return __find_get_block(sb->s_bdev, block, sb->s_blocksize);
  302. }
  303. static inline void
  304. map_bh(struct buffer_head *bh, struct super_block *sb, sector_t block)
  305. {
  306. set_buffer_mapped(bh);
  307. bh->b_bdev = sb->s_bdev;
  308. bh->b_blocknr = block;
  309. bh->b_size = sb->s_blocksize;
  310. }
  311. static inline void wait_on_buffer(struct buffer_head *bh)
  312. {
  313. might_sleep();
  314. if (buffer_locked(bh))
  315. __wait_on_buffer(bh);
  316. }
  317. static inline int trylock_buffer(struct buffer_head *bh)
  318. {
  319. return likely(!test_and_set_bit_lock(BH_Lock, &bh->b_state));
  320. }
  321. static inline void lock_buffer(struct buffer_head *bh)
  322. {
  323. might_sleep();
  324. if (!trylock_buffer(bh))
  325. __lock_buffer(bh);
  326. }
  327. static inline struct buffer_head *getblk_unmovable(struct block_device *bdev,
  328. sector_t block,
  329. unsigned size)
  330. {
  331. return __getblk_gfp(bdev, block, size, 0);
  332. }
  333. static inline struct buffer_head *__getblk(struct block_device *bdev,
  334. sector_t block,
  335. unsigned size)
  336. {
  337. return __getblk_gfp(bdev, block, size, __GFP_MOVABLE);
  338. }
  339. /**
  340. * __bread() - reads a specified block and returns the bh
  341. * @bdev: the block_device to read from
  342. * @block: number of block
  343. * @size: size (in bytes) to read
  344. *
  345. * Reads a specified block, and returns buffer head that contains it.
  346. * The page cache is allocated from movable area so that it can be migrated.
  347. * It returns NULL if the block was unreadable.
  348. */
  349. static inline struct buffer_head *
  350. __bread(struct block_device *bdev, sector_t block, unsigned size)
  351. {
  352. return __bread_gfp(bdev, block, size, __GFP_MOVABLE);
  353. }
  354. extern int __set_page_dirty_buffers(struct page *page);
  355. #else /* CONFIG_BLOCK */
  356. static inline void buffer_init(void) {}
  357. static inline int try_to_free_buffers(struct page *page) { return 1; }
  358. static inline int inode_has_buffers(struct inode *inode) { return 0; }
  359. static inline void invalidate_inode_buffers(struct inode *inode) {}
  360. static inline int remove_inode_buffers(struct inode *inode) { return 1; }
  361. static inline int sync_mapping_buffers(struct address_space *mapping) { return 0; }
  362. #endif /* CONFIG_BLOCK */
  363. #endif /* _LINUX_BUFFER_HEAD_H */