1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2009-2011 Red Hat, Inc.
5 * Author: Mikulas Patocka <mpatocka@redhat.com>
7 * This file is released under the GPL.
10 #ifndef _LINUX_DM_BUFIO_H
11 #define _LINUX_DM_BUFIO_H
13 #include <linux/blkdev.h>
14 #include <linux/types.h>
16 /*----------------------------------------------------------------*/
18 struct dm_bufio_client;
22 * Flags for dm_bufio_client_create
24 #define DM_BUFIO_CLIENT_NO_SLEEP 0x1
27 * Create a buffered IO cache on a given device
29 struct dm_bufio_client *
30 dm_bufio_client_create(struct block_device *bdev, unsigned int block_size,
31 unsigned int reserved_buffers, unsigned int aux_size,
32 void (*alloc_callback)(struct dm_buffer *),
33 void (*write_callback)(struct dm_buffer *),
37 * Release a buffered IO cache.
39 void dm_bufio_client_destroy(struct dm_bufio_client *c);
41 void dm_bufio_client_reset(struct dm_bufio_client *c);
44 * Set the sector range.
45 * When this function is called, there must be no I/O in progress on the bufio
48 void dm_bufio_set_sector_offset(struct dm_bufio_client *c, sector_t start);
51 * WARNING: to avoid deadlocks, these conditions are observed:
53 * - At most one thread can hold at most "reserved_buffers" simultaneously.
54 * - Each other threads can hold at most one buffer.
55 * - Threads which call only dm_bufio_get can hold unlimited number of
60 * Read a given block from disk. Returns pointer to data. Returns a
61 * pointer to dm_buffer that can be used to release the buffer or to make
64 void *dm_bufio_read(struct dm_bufio_client *c, sector_t block,
65 struct dm_buffer **bp);
68 * Like dm_bufio_read, but return buffer from cache, don't read
69 * it. If the buffer is not in the cache, return NULL.
71 void *dm_bufio_get(struct dm_bufio_client *c, sector_t block,
72 struct dm_buffer **bp);
75 * Like dm_bufio_read, but don't read anything from the disk. It is
76 * expected that the caller initializes the buffer and marks it dirty.
78 void *dm_bufio_new(struct dm_bufio_client *c, sector_t block,
79 struct dm_buffer **bp);
82 * Prefetch the specified blocks to the cache.
83 * The function starts to read the blocks and returns without waiting for
86 void dm_bufio_prefetch(struct dm_bufio_client *c,
87 sector_t block, unsigned int n_blocks);
90 * Release a reference obtained with dm_bufio_{read,get,new}. The data
91 * pointer and dm_buffer pointer is no longer valid after this call.
93 void dm_bufio_release(struct dm_buffer *b);
96 * Mark a buffer dirty. It should be called after the buffer is modified.
98 * In case of memory pressure, the buffer may be written after
99 * dm_bufio_mark_buffer_dirty, but before dm_bufio_write_dirty_buffers. So
100 * dm_bufio_write_dirty_buffers guarantees that the buffer is on-disk but
101 * the actual writing may occur earlier.
103 void dm_bufio_mark_buffer_dirty(struct dm_buffer *b);
106 * Mark a part of the buffer dirty.
108 * The specified part of the buffer is scheduled to be written. dm-bufio may
109 * write the specified part of the buffer or it may write a larger superset.
111 void dm_bufio_mark_partial_buffer_dirty(struct dm_buffer *b,
112 unsigned int start, unsigned int end);
115 * Initiate writing of dirty buffers, without waiting for completion.
117 void dm_bufio_write_dirty_buffers_async(struct dm_bufio_client *c);
120 * Write all dirty buffers. Guarantees that all dirty buffers created prior
121 * to this call are on disk when this call exits.
123 int dm_bufio_write_dirty_buffers(struct dm_bufio_client *c);
126 * Send an empty write barrier to the device to flush hardware disk cache.
128 int dm_bufio_issue_flush(struct dm_bufio_client *c);
131 * Send a discard request to the underlying device.
133 int dm_bufio_issue_discard(struct dm_bufio_client *c, sector_t block, sector_t count);
136 * Free the given buffer.
137 * This is just a hint, if the buffer is in use or dirty, this function
140 void dm_bufio_forget(struct dm_bufio_client *c, sector_t block);
143 * Free the given range of buffers.
144 * This is just a hint, if the buffer is in use or dirty, this function
147 void dm_bufio_forget_buffers(struct dm_bufio_client *c, sector_t block, sector_t n_blocks);
150 * Set the minimum number of buffers before cleanup happens.
152 void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n);
154 unsigned int dm_bufio_get_block_size(struct dm_bufio_client *c);
155 sector_t dm_bufio_get_device_size(struct dm_bufio_client *c);
156 struct dm_io_client *dm_bufio_get_dm_io_client(struct dm_bufio_client *c);
157 sector_t dm_bufio_get_block_number(struct dm_buffer *b);
158 void *dm_bufio_get_block_data(struct dm_buffer *b);
159 void *dm_bufio_get_aux_data(struct dm_buffer *b);
160 struct dm_bufio_client *dm_bufio_get_client(struct dm_buffer *b);
162 /*----------------------------------------------------------------*/