linux/include/linux/buffer_head.h

/* SPDX-License-Identifier: GPL-2.0 */
/*
 * include/linux/buffer_head.h
 *
 * Everything to do with buffer_heads.
 */

#ifndef _LINUX_BUFFER_HEAD_H
#define _LINUX_BUFFER_HEAD_H

#include <linux/types.h>
#include <linux/blk_types.h>
#include <linux/fs.h>
#include <linux/linkage.h>
#include <linux/pagemap.h>
#include <linux/wait.h>
#include <linux/atomic.h>

enum bh_state_bits {};

#define MAX_BUF_PER_PAGE

struct page;
struct buffer_head;
struct address_space;
bh_end_io_t;

/*
 * Historically, a buffer_head was used to map a single block
 * within a page, and of course as the unit of I/O through the
 * filesystem and block layers.  Nowadays the basic I/O unit
 * is the bio, and buffer_heads are used for extracting block
 * mappings (via a get_block_t call), for tracking state within
 * a folio (via a folio_mapping) and for wrapping bio submission
 * for backward compatibility reasons (e.g. submit_bh).
 */
struct buffer_head {};

/*
 * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
 * and buffer_foo() functions.
 * To avoid reset buffer flags that are already set, because that causes
 * a costly cache line transition, check the flag first.
 */
#define BUFFER_FNS(bit, name)

/*
 * test_set_buffer_foo() and test_clear_buffer_foo()
 */
#define TAS_BUFFER_FNS(bit, name)									\

/*
 * Emit the buffer bitops functions.   Note that there are also functions
 * of the form "mark_buffer_foo()".  These are higher-level functions which
 * do something in addition to setting a b_state bit.
 */
BUFFER_FNS(Dirty, dirty)
TAS_BUFFER_FNS(Dirty, dirty)
BUFFER_FNS(Lock, locked)
BUFFER_FNS(Req, req)
TAS_BUFFER_FNS(Req, req)
BUFFER_FNS(Mapped, mapped)
BUFFER_FNS(New, new)
BUFFER_FNS(Async_Read, async_read)
BUFFER_FNS(Async_Write, async_write)
BUFFER_FNS(Delay, delay)
BUFFER_FNS(Boundary, boundary)
BUFFER_FNS(Write_EIO, write_io_error)
BUFFER_FNS(Unwritten, unwritten)
BUFFER_FNS(Meta, meta)
BUFFER_FNS(Prio, prio)
BUFFER_FNS(Defer_Completion, defer_completion)

static __always_inline void set_buffer_uptodate(struct buffer_head *bh)
{}

static __always_inline void clear_buffer_uptodate(struct buffer_head *bh)
{}

static __always_inline int buffer_uptodate(const struct buffer_head *bh)
{}

static inline unsigned long bh_offset(const struct buffer_head *bh)
{}

/* If we *know* page->private refers to buffer_heads */
#define page_buffers(page)
#define page_has_buffers(page)
#define folio_buffers(folio)

void buffer_check_dirty_writeback(struct folio *folio,
				     bool *dirty, bool *writeback);

/*
 * Declarations
 */

void mark_buffer_dirty(struct buffer_head *bh);
void mark_buffer_write_io_error(struct buffer_head *bh);
void touch_buffer(struct buffer_head *bh);
void folio_set_bh(struct buffer_head *bh, struct folio *folio,
		  unsigned long offset);
struct buffer_head *folio_alloc_buffers(struct folio *folio, unsigned long size,
					gfp_t gfp);
struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size);
struct buffer_head *create_empty_buffers(struct folio *folio,
		unsigned long blocksize, unsigned long b_state);
void end_buffer_read_sync(struct buffer_head *bh, int uptodate);
void end_buffer_write_sync(struct buffer_head *bh, int uptodate);

/* Things to do with buffers at mapping->private_list */
void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode);
int generic_buffers_fsync_noflush(struct file *file, loff_t start, loff_t end,
				  bool datasync);
int generic_buffers_fsync(struct file *file, loff_t start, loff_t end,
			  bool datasync);
void clean_bdev_aliases(struct block_device *bdev, sector_t block,
			sector_t len);
static inline void clean_bdev_bh_alias(struct buffer_head *bh)
{}

void mark_buffer_async_write(struct buffer_head *bh);
void __wait_on_buffer(struct buffer_head *);
wait_queue_head_t *bh_waitq_head(struct buffer_head *bh);
struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block,
			unsigned size);
struct buffer_head *bdev_getblk(struct block_device *bdev, sector_t block,
		unsigned size, gfp_t gfp);
void __brelse(struct buffer_head *);
void __bforget(struct buffer_head *);
void __breadahead(struct block_device *, sector_t block, unsigned int size);
struct buffer_head *__bread_gfp(struct block_device *,
				sector_t block, unsigned size, gfp_t gfp);
struct buffer_head *alloc_buffer_head(gfp_t gfp_flags);
void free_buffer_head(struct buffer_head * bh);
void unlock_buffer(struct buffer_head *bh);
void __lock_buffer(struct buffer_head *bh);
int sync_dirty_buffer(struct buffer_head *bh);
int __sync_dirty_buffer(struct buffer_head *bh, blk_opf_t op_flags);
void write_dirty_buffer(struct buffer_head *bh, blk_opf_t op_flags);
void submit_bh(blk_opf_t, struct buffer_head *);
void write_boundary_block(struct block_device *bdev,
			sector_t bblock, unsigned blocksize);
int bh_uptodate_or_lock(struct buffer_head *bh);
int __bh_read(struct buffer_head *bh, blk_opf_t op_flags, bool wait);
void __bh_read_batch(int nr, struct buffer_head *bhs[],
		     blk_opf_t op_flags, bool force_lock);

/*
 * Generic address_space_operations implementations for buffer_head-backed
 * address_spaces.
 */
void block_invalidate_folio(struct folio *folio, size_t offset, size_t length);
int block_write_full_folio(struct folio *folio, struct writeback_control *wbc,
		void *get_block);
int __block_write_full_folio(struct inode *inode, struct folio *folio,
		get_block_t *get_block, struct writeback_control *wbc);
int block_read_full_folio(struct folio *, get_block_t *);
bool block_is_partially_uptodate(struct folio *, size_t from, size_t count);
int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
		struct folio **foliop, get_block_t *get_block);
int __block_write_begin(struct folio *folio, loff_t pos, unsigned len,
		get_block_t *get_block);
int block_write_end(struct file *, struct address_space *,
				loff_t, unsigned len, unsigned copied,
				struct folio *, void *);
int generic_write_end(struct file *, struct address_space *,
				loff_t, unsigned len, unsigned copied,
				struct folio *, void *);
void folio_zero_new_buffers(struct folio *folio, size_t from, size_t to);
int cont_write_begin(struct file *, struct address_space *, loff_t,
			unsigned, struct folio **, void **,
			get_block_t *, loff_t *);
int generic_cont_expand_simple(struct inode *inode, loff_t size);
void block_commit_write(struct page *page, unsigned int from, unsigned int to);
int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
				get_block_t get_block);
sector_t generic_block_bmap(struct address_space *, sector_t, get_block_t *);
int block_truncate_page(struct address_space *, loff_t, get_block_t *);

#ifdef CONFIG_MIGRATION
extern int buffer_migrate_folio(struct address_space *,
		struct folio *dst, struct folio *src, enum migrate_mode);
extern int buffer_migrate_folio_norefs(struct address_space *,
		struct folio *dst, struct folio *src, enum migrate_mode);
#else
#define buffer_migrate_folio
#define buffer_migrate_folio_norefs
#endif

/*
 * inline definitions
 */

static inline void get_bh(struct buffer_head *bh)
{}

static inline void put_bh(struct buffer_head *bh)
{}

/**
 * brelse - Release a buffer.
 * @bh: The buffer to release.
 *
 * Decrement a buffer_head's reference count.  If @bh is NULL, this
 * function is a no-op.
 *
 * If all buffers on a folio have zero reference count, are clean
 * and unlocked, and if the folio is unlocked and not under writeback
 * then try_to_free_buffers() may strip the buffers from the folio in
 * preparation for freeing it (sometimes, rarely, buffers are removed
 * from a folio but it ends up not being freed, and buffers may later
 * be reattached).
 *
 * Context: Any context.
 */
static inline void brelse(struct buffer_head *bh)
{}

/**
 * bforget - Discard any dirty data in a buffer.
 * @bh: The buffer to forget.
 *
 * Call this function instead of brelse() if the data written to a buffer
 * no longer needs to be written back.  It will clear the buffer's dirty
 * flag so writeback of this buffer will be skipped.
 *
 * Context: Any context.
 */
static inline void bforget(struct buffer_head *bh)
{}

static inline struct buffer_head *
sb_bread(struct super_block *sb, sector_t block)
{}

static inline struct buffer_head *
sb_bread_unmovable(struct super_block *sb, sector_t block)
{}

static inline void
sb_breadahead(struct super_block *sb, sector_t block)
{}

static inline struct buffer_head *getblk_unmovable(struct block_device *bdev,
		sector_t block, unsigned size)
{}

static inline struct buffer_head *__getblk(struct block_device *bdev,
		sector_t block, unsigned size)
{}

static inline struct buffer_head *sb_getblk(struct super_block *sb,
		sector_t block)
{}

static inline struct buffer_head *sb_getblk_gfp(struct super_block *sb,
		sector_t block, gfp_t gfp)
{}

static inline struct buffer_head *
sb_find_get_block(struct super_block *sb, sector_t block)
{}

static inline void
map_bh(struct buffer_head *bh, struct super_block *sb, sector_t block)
{}

static inline void wait_on_buffer(struct buffer_head *bh)
{}

static inline int trylock_buffer(struct buffer_head *bh)
{}

static inline void lock_buffer(struct buffer_head *bh)
{}

static inline void bh_readahead(struct buffer_head *bh, blk_opf_t op_flags)
{}

static inline void bh_read_nowait(struct buffer_head *bh, blk_opf_t op_flags)
{}

/* Returns 1 if buffer uptodated, 0 on success, and -EIO on error. */
static inline int bh_read(struct buffer_head *bh, blk_opf_t op_flags)
{}

static inline void bh_read_batch(int nr, struct buffer_head *bhs[])
{}

static inline void bh_readahead_batch(int nr, struct buffer_head *bhs[],
				      blk_opf_t op_flags)
{}

/**
 * __bread() - Read a block.
 * @bdev: The block device to read from.
 * @block: Block number in units of block size.
 * @size: The block size of this device in bytes.
 *
 * Read a specified block, and return the buffer head that refers
 * to it.  The memory is allocated from the movable area so that it can
 * be migrated.  The returned buffer head has its refcount increased.
 * The caller should call brelse() when it has finished with the buffer.
 *
 * Context: May sleep waiting for I/O.
 * Return: NULL if the block was unreadable.
 */
static inline struct buffer_head *__bread(struct block_device *bdev,
		sector_t block, unsigned size)
{}

/**
 * get_nth_bh - Get a reference on the n'th buffer after this one.
 * @bh: The buffer to start counting from.
 * @count: How many buffers to skip.
 *
 * This is primarily useful for finding the nth buffer in a folio; in
 * that case you pass the head buffer and the byte offset in the folio
 * divided by the block size.  It can be used for other purposes, but
 * it will wrap at the end of the folio rather than returning NULL or
 * proceeding to the next folio for you.
 *
 * Return: The requested buffer with an elevated refcount.
 */
static inline __must_check
struct buffer_head *get_nth_bh(struct buffer_head *bh, unsigned int count)
{}

bool block_dirty_folio(struct address_space *mapping, struct folio *folio);

#ifdef CONFIG_BUFFER_HEAD

void buffer_init(void);
bool try_to_free_buffers(struct folio *folio);
int inode_has_buffers(struct inode *inode);
void invalidate_inode_buffers(struct inode *inode);
int remove_inode_buffers(struct inode *inode);
int sync_mapping_buffers(struct address_space *mapping);
void invalidate_bh_lrus(void);
void invalidate_bh_lrus_cpu(void);
bool has_bh_in_lru(int cpu, void *dummy);
extern int buffer_heads_over_limit;

#else /* CONFIG_BUFFER_HEAD */

static inline void buffer_init(void) {}
static inline bool try_to_free_buffers(struct folio *folio) { return true; }
static inline int inode_has_buffers(struct inode *inode) { return 0; }
static inline void invalidate_inode_buffers(struct inode *inode) {}
static inline int remove_inode_buffers(struct inode *inode) { return 1; }
static inline int sync_mapping_buffers(struct address_space *mapping) { return 0; }
static inline void invalidate_bh_lrus(void) {}
static inline void invalidate_bh_lrus_cpu(void) {}
static inline bool has_bh_in_lru(int cpu, void *dummy) { return false; }
#define buffer_heads_over_limit

#endif /* CONFIG_BUFFER_HEAD */
#endif /* _LINUX_BUFFER_HEAD_H */