xref: /linux/include/linux/buffer_head.h (revision c5fd5e67c7812bc40cd897422c1e0d6dd9ed1e2e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * include/linux/buffer_head.h
4  *
5  * Everything to do with buffer_heads.
6  */
7 
8 #ifndef _LINUX_BUFFER_HEAD_H
9 #define _LINUX_BUFFER_HEAD_H
10 
11 #include <linux/types.h>
12 #include <linux/blk_types.h>
13 #include <linux/fs.h>
14 #include <linux/linkage.h>
15 #include <linux/pagemap.h>
16 #include <linux/wait.h>
17 #include <linux/atomic.h>
18 
19 enum bh_state_bits {
20 	BH_Uptodate,	/* Contains valid data */
21 	BH_Dirty,	/* Is dirty */
22 	BH_Lock,	/* Is locked */
23 	BH_Req,		/* Has been submitted for I/O */
24 
25 	BH_Mapped,	/* Has a disk mapping */
26 	BH_New,		/* Disk mapping was newly created by get_block */
27 	BH_Async_Read,	/* Is under end_buffer_async_read I/O */
28 	BH_Async_Write,	/* Is under end_buffer_async_write I/O */
29 	BH_Delay,	/* Buffer is not yet allocated on disk */
30 	BH_Boundary,	/* Block is followed by a discontiguity */
31 	BH_Write_EIO,	/* I/O error on write */
32 	BH_Unwritten,	/* Buffer is allocated on disk but not written */
33 	BH_Quiet,	/* Buffer Error Prinks to be quiet */
34 	BH_Meta,	/* Buffer contains metadata */
35 	BH_Prio,	/* Buffer should be submitted with REQ_PRIO */
36 	BH_Defer_Completion, /* Defer AIO completion to workqueue */
37 	BH_Migrate,     /* Buffer is being migrated (norefs) */
38 
39 	BH_PrivateStart,/* not a state bit, but the first bit available
40 			 * for private allocation by other entities
41 			 */
42 };
43 
44 #define MAX_BUF_PER_PAGE (PAGE_SIZE / 512)
45 
46 struct page;
47 struct buffer_head;
48 struct address_space;
49 
50 /*
51  * Historically, a buffer_head was used to map a single block
52  * within a page, and of course as the unit of I/O through the
53  * filesystem and block layers.  Nowadays the basic I/O unit
54  * is the bio, and buffer_heads are used for extracting block
55  * mappings (via a get_block_t call), for tracking state within
56  * a folio (via a folio_mapping) and for wrapping bio submission
57  * for backward compatibility reasons (e.g. bh_submit).
58  */
59 struct buffer_head {
60 	unsigned long b_state;		/* buffer state bitmap (see above) */
61 	struct buffer_head *b_this_page;/* circular list of page's buffers */
62 	struct folio *b_folio;		/* the folio this bh is mapped to */
63 
64 	sector_t b_blocknr;		/* start block number */
65 	size_t b_size;			/* size of mapping */
66 	char *b_data;			/* pointer to data within the page */
67 
68 	struct block_device *b_bdev;
69 	void *b_private;		/* reserved for bio_end_io */
70 	struct list_head b_assoc_buffers; /* associated with another mapping */
71 	struct mapping_metadata_bhs *b_mmb; /* head of the list of metadata bhs
72 					     * this buffer is associated with */
73 	atomic_t b_count;		/* users using this buffer_head */
74 	spinlock_t b_uptodate_lock;	/* Used by the first bh in a page, to
75 					 * serialise IO completion of other
76 					 * buffers in the page */
77 };
78 
79 /*
80  * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
81  * and buffer_foo() functions.
82  * To avoid reset buffer flags that are already set, because that causes
83  * a costly cache line transition, check the flag first.
84  */
85 #define BUFFER_FNS(bit, name)						\
86 static __always_inline void set_buffer_##name(struct buffer_head *bh)	\
87 {									\
88 	if (!test_bit(BH_##bit, &(bh)->b_state))			\
89 		set_bit(BH_##bit, &(bh)->b_state);			\
90 }									\
91 static __always_inline void clear_buffer_##name(struct buffer_head *bh)	\
92 {									\
93 	clear_bit(BH_##bit, &(bh)->b_state);				\
94 }									\
95 static __always_inline int buffer_##name(const struct buffer_head *bh)	\
96 {									\
97 	return test_bit(BH_##bit, &(bh)->b_state);			\
98 }
99 
100 /*
101  * test_set_buffer_foo() and test_clear_buffer_foo()
102  */
103 #define TAS_BUFFER_FNS(bit, name)					\
104 static __always_inline int test_set_buffer_##name(struct buffer_head *bh) \
105 {									\
106 	return test_and_set_bit(BH_##bit, &(bh)->b_state);		\
107 }									\
108 static __always_inline int test_clear_buffer_##name(struct buffer_head *bh) \
109 {									\
110 	return test_and_clear_bit(BH_##bit, &(bh)->b_state);		\
111 }									\
112 
113 /*
114  * Emit the buffer bitops functions.   Note that there are also functions
115  * of the form "mark_buffer_foo()".  These are higher-level functions which
116  * do something in addition to setting a b_state bit.
117  */
118 BUFFER_FNS(Dirty, dirty)
119 TAS_BUFFER_FNS(Dirty, dirty)
120 BUFFER_FNS(Lock, locked)
121 BUFFER_FNS(Req, req)
122 TAS_BUFFER_FNS(Req, req)
123 BUFFER_FNS(Mapped, mapped)
124 BUFFER_FNS(New, new)
125 BUFFER_FNS(Async_Read, async_read)
126 BUFFER_FNS(Async_Write, async_write)
127 BUFFER_FNS(Delay, delay)
128 BUFFER_FNS(Boundary, boundary)
129 BUFFER_FNS(Write_EIO, write_io_error)
130 BUFFER_FNS(Unwritten, unwritten)
131 BUFFER_FNS(Meta, meta)
132 BUFFER_FNS(Prio, prio)
133 BUFFER_FNS(Defer_Completion, defer_completion)
134 
135 static __always_inline void set_buffer_uptodate(struct buffer_head *bh)
136 {
137 	/*
138 	 * If somebody else already set this uptodate, they will
139 	 * have done the memory barrier, and a reader will thus
140 	 * see *some* valid buffer state.
141 	 *
142 	 * Any other serialization (with IO errors or whatever that
143 	 * might clear the bit) has to come from other state (eg BH_Lock).
144 	 */
145 	if (test_bit(BH_Uptodate, &bh->b_state))
146 		return;
147 
148 	/*
149 	 * make it consistent with folio_mark_uptodate
150 	 * pairs with smp_load_acquire in buffer_uptodate
151 	 */
152 	smp_mb__before_atomic();
153 	set_bit(BH_Uptodate, &bh->b_state);
154 }
155 
156 static __always_inline void clear_buffer_uptodate(struct buffer_head *bh)
157 {
158 	clear_bit(BH_Uptodate, &bh->b_state);
159 }
160 
161 static __always_inline int buffer_uptodate(const struct buffer_head *bh)
162 {
163 	/*
164 	 * make it consistent with folio_test_uptodate
165 	 * pairs with smp_mb__before_atomic in set_buffer_uptodate
166 	 */
167 	return test_bit_acquire(BH_Uptodate, &bh->b_state);
168 }
169 
170 static inline unsigned long bh_offset(const struct buffer_head *bh)
171 {
172 	return (unsigned long)(bh)->b_data & (folio_size(bh->b_folio) - 1);
173 }
174 
175 /**
176  * kmap_local_bh - Map the data of a buffer.
177  * @bh: The buffer.
178  *
179  * Buffers usually live in the page cache, but a few are built over memory
180  * which is not.  Those carry no folio and b_data is already a kernel address
181  * which is always mapped, so there is nothing to do for them.  Pair with
182  * kunmap_local_bh().
183  *
184  * Return: A pointer to the buffer's data.
185  */
186 static inline void *kmap_local_bh(const struct buffer_head *bh)
187 {
188 	if (!bh->b_folio)
189 		return bh->b_data;
190 	return kmap_local_folio(bh->b_folio, bh_offset(bh));
191 }
192 
193 /**
194  * kunmap_local_bh - Unmap the data of a buffer.
195  * @bh: The buffer.
196  * @addr: The address returned by kmap_local_bh().
197  */
198 static inline void kunmap_local_bh(const struct buffer_head *bh, void *addr)
199 {
200 	if (bh->b_folio)
201 		kunmap_local(addr);
202 }
203 
204 /* If we *know* folio->private refers to buffer_heads */
205 #define folio_buffers(folio)		folio_get_private(folio)
206 
207 void buffer_check_dirty_writeback(struct folio *folio,
208 				     bool *dirty, bool *writeback);
209 
210 /*
211  * Declarations
212  */
213 
214 void mark_buffer_dirty(struct buffer_head *bh);
215 void mark_buffer_write_io_error(struct buffer_head *bh);
216 void touch_buffer(struct buffer_head *bh);
217 void folio_set_bh(struct buffer_head *bh, struct folio *folio,
218 		  unsigned long offset);
219 struct buffer_head *folio_alloc_buffers(struct folio *folio, unsigned long size,
220 					gfp_t gfp);
221 struct buffer_head *create_empty_buffers(struct folio *folio,
222 		unsigned long blocksize, unsigned long b_state);
223 void end_buffer_read_sync(struct buffer_head *bh, int uptodate);
224 bool bio_endio_bh(struct bio *bio, struct buffer_head **bhp);
225 
226 /* Completion routines suitable for passing to bh_submit() */
227 void bh_end_read(struct bio *bio);
228 void bh_end_write(struct bio *bio);
229 void bh_end_async_write(struct bio *bio);
230 
231 /* Things to do with metadata buffers list */
232 void mmb_mark_buffer_dirty(struct buffer_head *bh, struct mapping_metadata_bhs *mmb);
233 void clean_bdev_aliases(struct block_device *bdev, sector_t block,
234 			sector_t len);
235 static inline void clean_bdev_bh_alias(struct buffer_head *bh)
236 {
237 	clean_bdev_aliases(bh->b_bdev, bh->b_blocknr, 1);
238 }
239 
240 void __wait_on_buffer(struct buffer_head *);
241 wait_queue_head_t *bh_waitq_head(struct buffer_head *bh);
242 struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block,
243 			unsigned size);
244 struct buffer_head *__find_get_block_nonatomic(struct block_device *bdev,
245 			sector_t block, unsigned size);
246 struct buffer_head *bdev_getblk(struct block_device *bdev, sector_t block,
247 		unsigned size, gfp_t gfp);
248 void __brelse(struct buffer_head *);
249 void __bforget(struct buffer_head *);
250 void __breadahead(struct block_device *, sector_t block, unsigned int size);
251 struct buffer_head *__bread_gfp(struct block_device *,
252 				sector_t block, unsigned size, gfp_t gfp);
253 struct buffer_head *alloc_buffer_head(gfp_t gfp_flags);
254 void free_buffer_head(struct buffer_head * bh);
255 void unlock_buffer(struct buffer_head *bh);
256 void __lock_buffer(struct buffer_head *bh);
257 int sync_dirty_buffer(struct buffer_head *bh);
258 int __sync_dirty_buffer(struct buffer_head *bh, blk_opf_t op_flags);
259 void write_dirty_buffer(struct buffer_head *bh, blk_opf_t op_flags);
260 void bh_submit(struct buffer_head *, blk_opf_t, bio_end_io_t);
261 void write_boundary_block(struct block_device *bdev,
262 			sector_t bblock, unsigned blocksize);
263 int bh_uptodate_or_lock(struct buffer_head *bh);
264 int __bh_read(struct buffer_head *bh, blk_opf_t op_flags, bool wait);
265 void __bh_read_batch(int nr, struct buffer_head *bhs[],
266 		     blk_opf_t op_flags, bool force_lock);
267 
268 /*
269  * Generic address_space_operations implementations for buffer_head-backed
270  * address_spaces.
271  */
272 void block_invalidate_folio(struct folio *folio, size_t offset, size_t length);
273 int block_write_full_folio(struct folio *folio, struct writeback_control *wbc,
274 		void *get_block);
275 int __block_write_full_folio(struct inode *inode, struct folio *folio,
276 		get_block_t *get_block, struct writeback_control *wbc);
277 int block_read_full_folio(struct folio *, get_block_t *);
278 bool block_is_partially_uptodate(struct folio *, size_t from, size_t count);
279 int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
280 		struct folio **foliop, get_block_t *get_block);
281 int __block_write_begin(struct folio *folio, loff_t pos, unsigned len,
282 		get_block_t *get_block);
283 int block_write_end(loff_t pos, unsigned len, unsigned copied, struct folio *);
284 int generic_write_end(const struct kiocb *, struct address_space *,
285 				loff_t, unsigned len, unsigned copied,
286 				struct folio *, void *);
287 void folio_zero_new_buffers(struct folio *folio, size_t from, size_t to);
288 int cont_write_begin(const struct kiocb *, struct address_space *, loff_t,
289 			unsigned, struct folio **, void **,
290 			get_block_t *, loff_t *);
291 int generic_cont_expand_simple(struct inode *inode, loff_t size);
292 void block_commit_write(struct folio *folio, size_t from, size_t to);
293 int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
294 				get_block_t get_block);
295 sector_t generic_block_bmap(struct address_space *, sector_t, get_block_t *);
296 int block_truncate_page(struct address_space *, loff_t, get_block_t *);
297 
298 #ifdef CONFIG_MIGRATION
299 extern int buffer_migrate_folio(struct address_space *,
300 		struct folio *dst, struct folio *src, enum migrate_mode);
301 extern int buffer_migrate_folio_norefs(struct address_space *,
302 		struct folio *dst, struct folio *src, enum migrate_mode);
303 #else
304 #define buffer_migrate_folio NULL
305 #define buffer_migrate_folio_norefs NULL
306 #endif
307 
308 /*
309  * inline definitions
310  */
311 
312 static inline void get_bh(struct buffer_head *bh)
313 {
314         atomic_inc(&bh->b_count);
315 }
316 
317 static inline void put_bh(struct buffer_head *bh)
318 {
319         smp_mb__before_atomic();
320         atomic_dec(&bh->b_count);
321 }
322 
323 /**
324  * brelse - Release a buffer.
325  * @bh: The buffer to release.
326  *
327  * Decrement a buffer_head's reference count.  If @bh is NULL, this
328  * function is a no-op.
329  *
330  * If all buffers on a folio have zero reference count, are clean
331  * and unlocked, and if the folio is unlocked and not under writeback
332  * then try_to_free_buffers() may strip the buffers from the folio in
333  * preparation for freeing it (sometimes, rarely, buffers are removed
334  * from a folio but it ends up not being freed, and buffers may later
335  * be reattached).
336  *
337  * Context: Any context.
338  */
339 static inline void brelse(struct buffer_head *bh)
340 {
341 	if (bh)
342 		__brelse(bh);
343 }
344 
345 /**
346  * bforget - Discard any dirty data in a buffer.
347  * @bh: The buffer to forget.
348  *
349  * Call this function instead of brelse() if the data written to a buffer
350  * no longer needs to be written back.  It will clear the buffer's dirty
351  * flag so writeback of this buffer will be skipped.
352  *
353  * Context: Any context.
354  */
355 static inline void bforget(struct buffer_head *bh)
356 {
357 	if (bh)
358 		__bforget(bh);
359 }
360 
361 /**
362  * sb_bread - Read a block.
363  * @sb: The superblock to read from.
364  * @block: Block number in units of block size.
365  *
366  * Read a specified block, and return the buffer head that refers
367  * to it.  The memory is allocated from the movable area so that it can
368  * be migrated.  The returned buffer head has its refcount increased.
369  * The caller should call brelse() when it has finished with the buffer.
370  *
371  * Context: May sleep waiting for I/O.
372  * Return: NULL if the block was unreadable.
373  */
374 static inline
375 struct buffer_head *sb_bread(struct super_block *sb, sector_t block)
376 {
377 	return __bread_gfp(sb->s_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
378 }
379 
380 /**
381  * sb_bread_unmovable - Read a block.
382  * @sb: The superblock to read from.
383  * @block: Block number in units of block size.
384  *
385  * Read a specified block, and return the buffer head that refers to it.
386  * The memory is allocated from the unmovable area so that pointers into
387  * it remain valid after compaction runs.  The returned buffer head has
388  * its refcount increased.  The caller should call brelse() when it has
389  * finished with the buffer.
390  *
391  * Context: May sleep waiting for I/O.
392  * Return: NULL if the block was unreadable.
393  */
394 static inline
395 struct buffer_head *sb_bread_unmovable(struct super_block *sb, sector_t block)
396 {
397 	return __bread_gfp(sb->s_bdev, block, sb->s_blocksize, 0);
398 }
399 
400 /**
401  * sb_breadahead - Start readahead.
402  * @sb: Superblock identifying the block device.
403  * @block: The block to read.
404  *
405  * Read this block.  The I/O will be flagged as being readahead rather
406  * than immediate read, but (unlike the page cache), surrounding blocks
407  * will not be read.
408  *
409  * Context: May sleep in order to allocate memory.
410  */
411 static inline
412 void sb_breadahead(struct super_block *sb, sector_t block)
413 {
414 	__breadahead(sb->s_bdev, block, sb->s_blocksize);
415 }
416 
417 static inline struct buffer_head *getblk_unmovable(struct block_device *bdev,
418 		sector_t block, unsigned size)
419 {
420 	gfp_t gfp;
421 
422 	gfp = mapping_gfp_constraint(bdev->bd_mapping, ~__GFP_FS);
423 	gfp |= __GFP_NOFAIL;
424 
425 	return bdev_getblk(bdev, block, size, gfp);
426 }
427 
428 static inline struct buffer_head *__getblk(struct block_device *bdev,
429 		sector_t block, unsigned size)
430 {
431 	gfp_t gfp;
432 
433 	gfp = mapping_gfp_constraint(bdev->bd_mapping, ~__GFP_FS);
434 	gfp |= __GFP_MOVABLE | __GFP_NOFAIL;
435 
436 	return bdev_getblk(bdev, block, size, gfp);
437 }
438 
439 static inline struct buffer_head *sb_getblk(struct super_block *sb,
440 		sector_t block)
441 {
442 	return __getblk(sb->s_bdev, block, sb->s_blocksize);
443 }
444 
445 static inline struct buffer_head *sb_getblk_gfp(struct super_block *sb,
446 		sector_t block, gfp_t gfp)
447 {
448 	return bdev_getblk(sb->s_bdev, block, sb->s_blocksize, gfp);
449 }
450 
451 static inline struct buffer_head *
452 sb_find_get_block(struct super_block *sb, sector_t block)
453 {
454 	return __find_get_block(sb->s_bdev, block, sb->s_blocksize);
455 }
456 
457 static inline struct buffer_head *
458 sb_find_get_block_nonatomic(struct super_block *sb, sector_t block)
459 {
460 	return __find_get_block_nonatomic(sb->s_bdev, block, sb->s_blocksize);
461 }
462 
463 static inline void
464 map_bh(struct buffer_head *bh, struct super_block *sb, sector_t block)
465 {
466 	set_buffer_mapped(bh);
467 	bh->b_bdev = sb->s_bdev;
468 	bh->b_blocknr = block;
469 	bh->b_size = sb->s_blocksize;
470 }
471 
472 static inline void wait_on_buffer(struct buffer_head *bh)
473 {
474 	might_sleep();
475 	if (buffer_locked(bh))
476 		__wait_on_buffer(bh);
477 }
478 
479 static inline int trylock_buffer(struct buffer_head *bh)
480 {
481 	return likely(!test_and_set_bit_lock(BH_Lock, &bh->b_state));
482 }
483 
484 static inline void lock_buffer(struct buffer_head *bh)
485 {
486 	might_sleep();
487 	if (!trylock_buffer(bh))
488 		__lock_buffer(bh);
489 }
490 
491 static inline void bh_readahead(struct buffer_head *bh, blk_opf_t op_flags)
492 {
493 	if (!buffer_uptodate(bh) && trylock_buffer(bh)) {
494 		if (!buffer_uptodate(bh))
495 			__bh_read(bh, op_flags, false);
496 		else
497 			unlock_buffer(bh);
498 	}
499 }
500 
501 static inline void bh_read_nowait(struct buffer_head *bh, blk_opf_t op_flags)
502 {
503 	if (!bh_uptodate_or_lock(bh))
504 		__bh_read(bh, op_flags, false);
505 }
506 
507 /* Returns 1 if buffer uptodated, 0 on success, and -EIO on error. */
508 static inline int bh_read(struct buffer_head *bh, blk_opf_t op_flags)
509 {
510 	if (bh_uptodate_or_lock(bh))
511 		return 1;
512 	return __bh_read(bh, op_flags, true);
513 }
514 
515 static inline void bh_read_batch(int nr, struct buffer_head *bhs[])
516 {
517 	__bh_read_batch(nr, bhs, 0, true);
518 }
519 
520 static inline void bh_readahead_batch(int nr, struct buffer_head *bhs[],
521 				      blk_opf_t op_flags)
522 {
523 	__bh_read_batch(nr, bhs, op_flags, false);
524 }
525 
526 /**
527  * __bread() - Read a block.
528  * @bdev: The block device to read from.
529  * @block: Block number in units of block size.
530  * @size: The block size of this device in bytes.
531  *
532  * Read a specified block, and return the buffer head that refers
533  * to it.  The memory is allocated from the movable area so that it can
534  * be migrated.  The returned buffer head has its refcount increased.
535  * The caller should call brelse() when it has finished with the buffer.
536  *
537  * Context: May sleep waiting for I/O.
538  * Return: NULL if the block was unreadable.
539  */
540 static inline struct buffer_head *__bread(struct block_device *bdev,
541 		sector_t block, unsigned size)
542 {
543 	return __bread_gfp(bdev, block, size, __GFP_MOVABLE);
544 }
545 
546 /**
547  * get_nth_bh - Get a reference on the n'th buffer after this one.
548  * @bh: The buffer to start counting from.
549  * @count: How many buffers to skip.
550  *
551  * This is primarily useful for finding the nth buffer in a folio; in
552  * that case you pass the head buffer and the byte offset in the folio
553  * divided by the block size.  It can be used for other purposes, but
554  * it will wrap at the end of the folio rather than returning NULL or
555  * proceeding to the next folio for you.
556  *
557  * Return: The requested buffer with an elevated refcount.
558  */
559 static inline __must_check
560 struct buffer_head *get_nth_bh(struct buffer_head *bh, unsigned int count)
561 {
562 	while (count--)
563 		bh = bh->b_this_page;
564 	get_bh(bh);
565 	return bh;
566 }
567 
568 bool block_dirty_folio(struct address_space *mapping, struct folio *folio);
569 
570 #ifdef CONFIG_BUFFER_HEAD
571 
572 void buffer_init(void);
573 bool try_to_free_buffers(struct folio *folio);
574 void mmb_init(struct mapping_metadata_bhs *mmb, struct address_space *mapping);
575 bool mmb_has_buffers(struct mapping_metadata_bhs *mmb);
576 void mmb_invalidate(struct mapping_metadata_bhs *mmb);
577 int mmb_sync(struct mapping_metadata_bhs *mmb);
578 void invalidate_bh_lrus(void);
579 void invalidate_bh_lrus_cpu(void);
580 bool has_bh_in_lru(int cpu, void *dummy);
581 extern int buffer_heads_over_limit;
582 
583 #else /* CONFIG_BUFFER_HEAD */
584 
585 static inline void buffer_init(void) {}
586 static inline bool try_to_free_buffers(struct folio *folio) { return true; }
587 static inline int mmb_sync(struct mapping_metadata_bhs *mmb) { return 0; }
588 static inline void invalidate_bh_lrus(void) {}
589 static inline void invalidate_bh_lrus_cpu(void) {}
590 static inline bool has_bh_in_lru(int cpu, void *dummy) { return false; }
591 #define buffer_heads_over_limit 0
592 
593 #endif /* CONFIG_BUFFER_HEAD */
594 #endif /* _LINUX_BUFFER_HEAD_H */
595