1 ext4: Add callback support for bio read completion
3 From: Michael Halcrow <mhalcrow@google.com>
5 Add callback support for bio read completion. This supports data
6 transformation such as encryption.
8 This change deliberately targets only file systems that use struct
9 bio, including ext4. A more invasive change would be required for
10 other file systems such as NFS.
12 ext4 encryption patches depend on this one. Those patches need to
13 create page buffers, and so we're exporting that functionality in this
16 Signed-off-by: Michael Halcrow <mhalcrow@google.com>
17 Signed-off-by: Ildar Muslukhov <ildarm@google.com>
18 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
20 fs/buffer.c | 43 ++++++++++++++++++++++++++++++++++++++-----
21 include/linux/blk_types.h | 4 ++++
22 include/linux/buffer_head.h | 8 ++++++++
23 3 files changed, 50 insertions(+), 5 deletions(-)
25 diff --git a/fs/buffer.c b/fs/buffer.c
26 index 6c48f20..3790a53 100644
29 @@ -289,7 +289,7 @@ static void free_more_memory(void)
30 * I/O completion handler for block_read_full_page() - pages
31 * which come unlocked at the end of I/O.
33 -static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
34 +void end_buffer_async_read(struct buffer_head *bh, int uptodate)
37 struct buffer_head *first;
38 @@ -332,6 +332,13 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
39 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
40 local_irq_restore(flags);
42 + if (bh->b_private) {
43 + struct bio *bio = (struct bio *)bh->b_private;
44 + BUG_ON(!bio->bi_cb);
45 + if (!bio->bi_cb(bio, !(page_uptodate && !PageError(page))))
50 * If none of the buffers had errors and they are all
51 * uptodate then we can set the page uptodate.
52 @@ -339,6 +346,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
53 if (page_uptodate && !PageError(page))
54 SetPageUptodate(page);
60 @@ -346,6 +354,7 @@ still_busy:
61 local_irq_restore(flags);
64 +EXPORT_SYMBOL_GPL(end_buffer_async_read);
67 * Completion handler for block_write_full_page() - pages which are unlocked
68 @@ -424,11 +433,12 @@ EXPORT_SYMBOL(end_buffer_async_write);
69 * PageLocked prevents anyone from starting writeback of a page which is
70 * under read I/O (PageWriteback is only ever set against a locked page).
72 -static void mark_buffer_async_read(struct buffer_head *bh)
73 +void mark_buffer_async_read(struct buffer_head *bh)
75 bh->b_end_io = end_buffer_async_read;
76 set_buffer_async_read(bh);
78 +EXPORT_SYMBOL_GPL(mark_buffer_async_read);
80 static void mark_buffer_async_write_endio(struct buffer_head *bh,
82 @@ -1656,7 +1666,8 @@ static inline int block_size_bits(unsigned int blocksize)
83 return ilog2(blocksize);
86 -static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
87 +struct buffer_head *create_page_buffers(struct page *page, struct inode *inode,
88 + unsigned int b_state)
90 BUG_ON(!PageLocked(page));
92 @@ -1664,6 +1675,7 @@ static struct buffer_head *create_page_buffers(struct page *page, struct inode *
93 create_empty_buffers(page, 1 << ACCESS_ONCE(inode->i_blkbits), b_state);
94 return page_buffers(page);
96 +EXPORT_SYMBOL_GPL(create_page_buffers);
99 * NOTE! All mapped/uptodate combinations are valid:
100 @@ -3017,7 +3029,8 @@ void guard_bio_eod(int rw, struct bio *bio)
104 -int _submit_bh(int rw, struct buffer_head *bh, unsigned long bio_flags)
105 +int _submit_bh_cb(int rw, struct buffer_head *bh, unsigned long bio_flags,
106 + bio_completion_cb_t *cb, void *cb_ctx)
110 @@ -3051,6 +3064,8 @@ int _submit_bh(int rw, struct buffer_head *bh, unsigned long bio_flags)
112 bio->bi_end_io = end_bio_bh_io_sync;
113 bio->bi_private = bh;
115 + bio->bi_cb_ctx = cb_ctx;
116 bio->bi_flags |= bio_flags;
118 /* Take care of bh's that straddle the end of the device */
119 @@ -3062,6 +3077,12 @@ int _submit_bh(int rw, struct buffer_head *bh, unsigned long bio_flags)
125 + BUG_ON(bh->b_private);
126 + bh->b_private = bio;
131 if (bio_flagged(bio, BIO_EOPNOTSUPP))
132 @@ -3070,14 +3091,26 @@ int _submit_bh(int rw, struct buffer_head *bh, unsigned long bio_flags)
137 +int _submit_bh(int rw, struct buffer_head *bh, unsigned long bio_flags)
139 + return _submit_bh_cb(rw, bh, bio_flags, NULL, NULL);
141 EXPORT_SYMBOL_GPL(_submit_bh);
143 int submit_bh(int rw, struct buffer_head *bh)
145 - return _submit_bh(rw, bh, 0);
146 + return submit_bh_cb(rw, bh, NULL, NULL);
148 EXPORT_SYMBOL(submit_bh);
150 +int submit_bh_cb(int rw, struct buffer_head *bh, bio_completion_cb_t *cb,
153 + return _submit_bh_cb(rw, bh, 0, cb, cb_ctx);
155 +EXPORT_SYMBOL_GPL(submit_bh_cb);
158 * ll_rw_block: low-level access to block devices (DEPRECATED)
159 * @rw: whether to %READ or %WRITE or maybe %READA (readahead)
160 diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h
161 index 445d592..6df4c7b 100644
162 --- a/include/linux/blk_types.h
163 +++ b/include/linux/blk_types.h
164 @@ -16,6 +16,7 @@ struct io_context;
165 struct cgroup_subsys_state;
166 typedef void (bio_end_io_t) (struct bio *, int);
167 typedef void (bio_destructor_t) (struct bio *);
168 +typedef int (bio_completion_cb_t) (struct bio *, int);
171 * was unsigned short, but we might as well be ready for > 64kB I/O pages
172 @@ -98,6 +99,9 @@ struct bio {
174 struct bio_set *bi_pool;
176 + bio_completion_cb_t *bi_cb; /* completion callback */
177 + void *bi_cb_ctx; /* callback context */
180 * We can inline a number of vecs at the end of the bio, to avoid
181 * double allocations for a small number of bio_vecs. This member
182 diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h
183 index 73b4522..eecde95 100644
184 --- a/include/linux/buffer_head.h
185 +++ b/include/linux/buffer_head.h
186 @@ -160,7 +160,9 @@ void create_empty_buffers(struct page *, unsigned long,
187 unsigned long b_state);
188 void end_buffer_read_sync(struct buffer_head *bh, int uptodate);
189 void end_buffer_write_sync(struct buffer_head *bh, int uptodate);
190 +void end_buffer_async_read(struct buffer_head *bh, int uptodate);
191 void end_buffer_async_write(struct buffer_head *bh, int uptodate);
192 +void mark_buffer_async_read(struct buffer_head *bh);
194 /* Things to do with buffers at mapping->private_list */
195 void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode);
196 @@ -169,6 +171,8 @@ void invalidate_inode_buffers(struct inode *);
197 int remove_inode_buffers(struct inode *inode);
198 int sync_mapping_buffers(struct address_space *mapping);
199 void unmap_underlying_metadata(struct block_device *bdev, sector_t block);
200 +struct buffer_head *create_page_buffers(struct page *page, struct inode *inode,
201 + unsigned int b_state);
203 void mark_buffer_async_write(struct buffer_head *bh);
204 void __wait_on_buffer(struct buffer_head *);
205 @@ -192,7 +196,11 @@ int sync_dirty_buffer(struct buffer_head *bh);
206 int __sync_dirty_buffer(struct buffer_head *bh, int rw);
207 void write_dirty_buffer(struct buffer_head *bh, int rw);
208 int _submit_bh(int rw, struct buffer_head *bh, unsigned long bio_flags);
209 +int _submit_bh_cb(int rw, struct buffer_head *bh, unsigned long bio_flags,
210 + bio_completion_cb_t *cb, void *cb_ctx);
211 int submit_bh(int, struct buffer_head *);
212 +int submit_bh_cb(int rw, struct buffer_head *bh, bio_completion_cb_t *cb,
214 void write_boundary_block(struct block_device *bdev,
215 sector_t bblock, unsigned blocksize);
216 int bh_uptodate_or_lock(struct buffer_head *bh);
218 2.1.0.rc2.206.gedb03e5