buffer_head.h 13.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/fs.h>
#include <linux/linkage.h>
#include <linux/pagemap.h>
#include <linux/wait.h>
A
Arun Sharma 已提交
15
#include <linux/atomic.h>
L
Linus Torvalds 已提交
16

17 18
#ifdef CONFIG_BLOCK

L
Linus Torvalds 已提交
19 20 21 22 23
enum bh_state_bits {
	BH_Uptodate,	/* Contains valid data */
	BH_Dirty,	/* Is dirty */
	BH_Lock,	/* Is locked */
	BH_Req,		/* Has been submitted for I/O */
24 25 26
	BH_Uptodate_Lock,/* Used by the first bh in a page, to serialise
			  * IO completion of other buffers in the page
			  */
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34

	BH_Mapped,	/* Has a disk mapping */
	BH_New,		/* Disk mapping was newly created by get_block */
	BH_Async_Read,	/* Is under end_buffer_async_read I/O */
	BH_Async_Write,	/* Is under end_buffer_async_write I/O */
	BH_Delay,	/* Buffer is not yet allocated on disk */
	BH_Boundary,	/* Block is followed by a discontiguity */
	BH_Write_EIO,	/* I/O error on write */
35
	BH_Unwritten,	/* Buffer is allocated on disk but not written */
36
	BH_Quiet,	/* Buffer Error Prinks to be quiet */
37 38
	BH_Meta,	/* Buffer contains metadata */
	BH_Prio,	/* Buffer should be submitted with REQ_PRIO */
39
	BH_Defer_Completion, /* Defer AIO completion to workqueue */
L
Linus Torvalds 已提交
40 41 42 43 44 45

	BH_PrivateStart,/* not a state bit, but the first bit available
			 * for private allocation by other entities
			 */
};

46
#define MAX_BUF_PER_PAGE (PAGE_SIZE / 512)
L
Linus Torvalds 已提交
47 48 49 50 51 52 53

struct page;
struct buffer_head;
struct address_space;
typedef void (bh_end_io_t)(struct buffer_head *bh, int uptodate);

/*
54 55 56 57 58 59 60
 * 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 page (via a page_mapping) and for wrapping bio submission
 * for backward compatibility reasons (e.g. submit_bh).
L
Linus Torvalds 已提交
61 62 63 64 65 66
 */
struct buffer_head {
	unsigned long b_state;		/* buffer state bitmap (see above) */
	struct buffer_head *b_this_page;/* circular list of page's buffers */
	struct page *b_page;		/* the page this bh is mapped to */

67 68 69
	sector_t b_blocknr;		/* start block number */
	size_t b_size;			/* size of mapping */
	char *b_data;			/* pointer to data within the page */
L
Linus Torvalds 已提交
70 71 72 73 74

	struct block_device *b_bdev;
	bh_end_io_t *b_end_io;		/* I/O completion */
 	void *b_private;		/* reserved for b_end_io */
	struct list_head b_assoc_buffers; /* associated with another mapping */
75 76
	struct address_space *b_assoc_map;	/* mapping this buffer is
						   associated with */
77
	atomic_t b_count;		/* users using this buffer_head */
L
Linus Torvalds 已提交
78 79 80 81 82 83 84
};

/*
 * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
 * and buffer_foo() functions.
 */
#define BUFFER_FNS(bit, name)						\
85
static __always_inline void set_buffer_##name(struct buffer_head *bh)	\
L
Linus Torvalds 已提交
86 87 88
{									\
	set_bit(BH_##bit, &(bh)->b_state);				\
}									\
89
static __always_inline void clear_buffer_##name(struct buffer_head *bh)	\
L
Linus Torvalds 已提交
90 91 92
{									\
	clear_bit(BH_##bit, &(bh)->b_state);				\
}									\
93
static __always_inline int buffer_##name(const struct buffer_head *bh)	\
L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101
{									\
	return test_bit(BH_##bit, &(bh)->b_state);			\
}

/*
 * test_set_buffer_foo() and test_clear_buffer_foo()
 */
#define TAS_BUFFER_FNS(bit, name)					\
102
static __always_inline int test_set_buffer_##name(struct buffer_head *bh) \
L
Linus Torvalds 已提交
103 104 105
{									\
	return test_and_set_bit(BH_##bit, &(bh)->b_state);		\
}									\
106
static __always_inline int test_clear_buffer_##name(struct buffer_head *bh) \
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
{									\
	return test_and_clear_bit(BH_##bit, &(bh)->b_state);		\
}									\

/*
 * 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(Uptodate, uptodate)
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)
129
BUFFER_FNS(Unwritten, unwritten)
130 131
BUFFER_FNS(Meta, meta)
BUFFER_FNS(Prio, prio)
132
BUFFER_FNS(Defer_Completion, defer_completion)
L
Linus Torvalds 已提交
133 134 135 136 137 138

#define bh_offset(bh)		((unsigned long)(bh)->b_data & ~PAGE_MASK)

/* If we *know* page->private refers to buffer_heads */
#define page_buffers(page)					\
	({							\
H
Hugh Dickins 已提交
139 140
		BUG_ON(!PagePrivate(page));			\
		((struct buffer_head *)page_private(page));	\
L
Linus Torvalds 已提交
141 142 143
	})
#define page_has_buffers(page)	PagePrivate(page)

144 145 146
void buffer_check_dirty_writeback(struct page *page,
				     bool *dirty, bool *writeback);

L
Linus Torvalds 已提交
147 148 149 150
/*
 * Declarations
 */

151
void mark_buffer_dirty(struct buffer_head *bh);
152
void mark_buffer_write_io_error(struct buffer_head *bh);
L
Linus Torvalds 已提交
153
void init_buffer(struct buffer_head *, bh_end_io_t *, void *);
154
void touch_buffer(struct buffer_head *bh);
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163
void set_bh_page(struct buffer_head *bh,
		struct page *page, unsigned long offset);
int try_to_free_buffers(struct page *);
struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
		int retry);
void create_empty_buffers(struct page *, unsigned long,
			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);
164
void end_buffer_async_write(struct buffer_head *bh, int uptodate);
L
Linus Torvalds 已提交
165 166 167 168 169 170 171

/* Things to do with buffers at mapping->private_list */
void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode);
int inode_has_buffers(struct inode *);
void invalidate_inode_buffers(struct inode *);
int remove_inode_buffers(struct inode *inode);
int sync_mapping_buffers(struct address_space *mapping);
172 173
void clean_bdev_aliases(struct block_device *bdev, sector_t block,
			sector_t len);
174 175 176 177
static inline void clean_bdev_bh_alias(struct buffer_head *bh)
{
	clean_bdev_aliases(bh->b_bdev, bh->b_blocknr, 1);
}
L
Linus Torvalds 已提交
178 179 180 181

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);
182 183
struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block,
			unsigned size);
184 185
struct buffer_head *__getblk_gfp(struct block_device *bdev, sector_t block,
				  unsigned size, gfp_t gfp);
L
Linus Torvalds 已提交
186 187
void __brelse(struct buffer_head *);
void __bforget(struct buffer_head *);
188
void __breadahead(struct block_device *, sector_t block, unsigned int size);
189 190
struct buffer_head *__bread_gfp(struct block_device *,
				sector_t block, unsigned size, gfp_t gfp);
P
Peter Zijlstra 已提交
191
void invalidate_bh_lrus(void);
A
Al Viro 已提交
192
struct buffer_head *alloc_buffer_head(gfp_t gfp_flags);
L
Linus Torvalds 已提交
193
void free_buffer_head(struct buffer_head * bh);
194 195
void unlock_buffer(struct buffer_head *bh);
void __lock_buffer(struct buffer_head *bh);
196
void ll_rw_block(int, int, int, struct buffer_head * bh[]);
L
Linus Torvalds 已提交
197
int sync_dirty_buffer(struct buffer_head *bh);
198 199 200
int __sync_dirty_buffer(struct buffer_head *bh, int op_flags);
void write_dirty_buffer(struct buffer_head *bh, int op_flags);
int submit_bh(int, int, struct buffer_head *);
L
Linus Torvalds 已提交
201 202
void write_boundary_block(struct block_device *bdev,
			sector_t bblock, unsigned blocksize);
203 204
int bh_uptodate_or_lock(struct buffer_head *bh);
int bh_submit_read(struct buffer_head *bh);
205 206
loff_t page_cache_seek_hole_data(struct inode *inode, loff_t offset,
				 loff_t length, int whence);
L
Linus Torvalds 已提交
207 208 209 210 211 212 213

extern int buffer_heads_over_limit;

/*
 * Generic address_space_operations implementations for buffer_head-backed
 * address_spaces.
 */
214 215
void block_invalidatepage(struct page *page, unsigned int offset,
			  unsigned int length);
L
Linus Torvalds 已提交
216 217
int block_write_full_page(struct page *page, get_block_t *get_block,
				struct writeback_control *wbc);
218 219 220
int __block_write_full_page(struct inode *inode, struct page *page,
			get_block_t *get_block, struct writeback_control *wbc,
			bh_end_io_t *handler);
L
Linus Torvalds 已提交
221
int block_read_full_page(struct page*, get_block_t*);
222 223
int block_is_partially_uptodate(struct page *page, unsigned long from,
				unsigned long count);
224 225
int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
		unsigned flags, struct page **pagep, get_block_t *get_block);
226 227
int __block_write_begin(struct page *page, loff_t pos, unsigned len,
		get_block_t *get_block);
228 229 230 231 232 233 234
int block_write_end(struct file *, struct address_space *,
				loff_t, unsigned, unsigned,
				struct page *, void *);
int generic_write_end(struct file *, struct address_space *,
				loff_t, unsigned, unsigned,
				struct page *, void *);
void page_zero_new_buffers(struct page *page, unsigned from, unsigned to);
N
Nick Piggin 已提交
235 236 237
int cont_write_begin(struct file *, struct address_space *, loff_t,
			unsigned, unsigned, struct page **, void **,
			get_block_t *, loff_t *);
238
int generic_cont_expand_simple(struct inode *inode, loff_t size);
L
Linus Torvalds 已提交
239
int block_commit_write(struct page *page, unsigned from, unsigned to);
240
int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
241
				get_block_t get_block);
242 243 244 245 246
/* Convert errno to return value from ->page_mkwrite() call */
static inline int block_page_mkwrite_return(int err)
{
	if (err == 0)
		return VM_FAULT_LOCKED;
247
	if (err == -EFAULT || err == -EAGAIN)
248 249 250 251 252 253
		return VM_FAULT_NOPAGE;
	if (err == -ENOMEM)
		return VM_FAULT_OOM;
	/* -ENOSPC, -EDQUOT, -EIO ... */
	return VM_FAULT_SIGBUS;
}
L
Linus Torvalds 已提交
254 255
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 *);
256
int nobh_write_begin(struct address_space *, loff_t, unsigned, unsigned,
N
Nick Piggin 已提交
257 258 259 260 261
				struct page **, void **, get_block_t*);
int nobh_write_end(struct file *, struct address_space *,
				loff_t, unsigned, unsigned,
				struct page *, void *);
int nobh_truncate_page(struct address_space *, loff_t, get_block_t *);
L
Linus Torvalds 已提交
262 263 264
int nobh_writepage(struct page *page, get_block_t *get_block,
                        struct writeback_control *wbc);

A
Adrian Bunk 已提交
265
void buffer_init(void);
L
Linus Torvalds 已提交
266 267 268 269 270 271 272 273

/*
 * inline definitions
 */

static inline void attach_page_buffers(struct page *page,
		struct buffer_head *head)
{
274
	get_page(page);
L
Linus Torvalds 已提交
275
	SetPagePrivate(page);
H
Hugh Dickins 已提交
276
	set_page_private(page, (unsigned long)head);
L
Linus Torvalds 已提交
277 278 279 280 281 282 283 284 285
}

static inline void get_bh(struct buffer_head *bh)
{
        atomic_inc(&bh->b_count);
}

static inline void put_bh(struct buffer_head *bh)
{
286
        smp_mb__before_atomic();
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        atomic_dec(&bh->b_count);
}

static inline void brelse(struct buffer_head *bh)
{
	if (bh)
		__brelse(bh);
}

static inline void bforget(struct buffer_head *bh)
{
	if (bh)
		__bforget(bh);
}

static inline struct buffer_head *
sb_bread(struct super_block *sb, sector_t block)
{
305 306 307 308 309 310 311
	return __bread_gfp(sb->s_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
}

static inline struct buffer_head *
sb_bread_unmovable(struct super_block *sb, sector_t block)
{
	return __bread_gfp(sb->s_bdev, block, sb->s_blocksize, 0);
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321 322
}

static inline void
sb_breadahead(struct super_block *sb, sector_t block)
{
	__breadahead(sb->s_bdev, block, sb->s_blocksize);
}

static inline struct buffer_head *
sb_getblk(struct super_block *sb, sector_t block)
{
323
	return __getblk_gfp(sb->s_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
L
Linus Torvalds 已提交
324 325
}

326 327 328 329 330 331 332

static inline struct buffer_head *
sb_getblk_gfp(struct super_block *sb, sector_t block, gfp_t gfp)
{
	return __getblk_gfp(sb->s_bdev, block, sb->s_blocksize, gfp);
}

L
Linus Torvalds 已提交
333 334 335 336 337 338 339 340 341 342 343 344
static inline struct buffer_head *
sb_find_get_block(struct super_block *sb, sector_t block)
{
	return __find_get_block(sb->s_bdev, block, sb->s_blocksize);
}

static inline void
map_bh(struct buffer_head *bh, struct super_block *sb, sector_t block)
{
	set_buffer_mapped(bh);
	bh->b_bdev = sb->s_bdev;
	bh->b_blocknr = block;
345
	bh->b_size = sb->s_blocksize;
L
Linus Torvalds 已提交
346 347 348 349 350
}

static inline void wait_on_buffer(struct buffer_head *bh)
{
	might_sleep();
351
	if (buffer_locked(bh))
L
Linus Torvalds 已提交
352 353 354
		__wait_on_buffer(bh);
}

N
Nick Piggin 已提交
355 356
static inline int trylock_buffer(struct buffer_head *bh)
{
N
Nick Piggin 已提交
357
	return likely(!test_and_set_bit_lock(BH_Lock, &bh->b_state));
N
Nick Piggin 已提交
358 359
}

L
Linus Torvalds 已提交
360 361 362
static inline void lock_buffer(struct buffer_head *bh)
{
	might_sleep();
N
Nick Piggin 已提交
363
	if (!trylock_buffer(bh))
L
Linus Torvalds 已提交
364 365 366
		__lock_buffer(bh);
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
static inline struct buffer_head *getblk_unmovable(struct block_device *bdev,
						   sector_t block,
						   unsigned size)
{
	return __getblk_gfp(bdev, block, size, 0);
}

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

/**
 *  __bread() - reads a specified block and returns the bh
 *  @bdev: the block_device to read from
 *  @block: number of block
 *  @size: size (in bytes) to read
 *
 *  Reads a specified block, and returns buffer head that contains it.
 *  The page cache is allocated from movable area so that it can be migrated.
 *  It returns NULL if the block was unreadable.
 */
static inline struct buffer_head *
__bread(struct block_device *bdev, sector_t block, unsigned size)
{
	return __bread_gfp(bdev, block, size, __GFP_MOVABLE);
}

397
extern int __set_page_dirty_buffers(struct page *page);
398 399 400 401 402 403 404 405 406 407 408

#else /* CONFIG_BLOCK */

static inline void buffer_init(void) {}
static inline int try_to_free_buffers(struct page *page) { return 1; }
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; }

#endif /* CONFIG_BLOCK */
L
Linus Torvalds 已提交
409
#endif /* _LINUX_BUFFER_HEAD_H */