blob: a8329cc47decd627ca26cf608e6f15276697a4f6 [file] [log] [blame]
Joern Engel5db53f32009-11-20 20:13:39 +01001/*
2 * fs/logfs/dev_bdev.c - Device access methods for block devices
3 *
4 * As should be obvious for Linux kernel code, license is GPLv2
5 *
6 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
7 */
8#include "logfs.h"
9#include <linux/bio.h>
10#include <linux/blkdev.h>
11#include <linux/buffer_head.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070013#include <linux/prefetch.h>
Joern Engel5db53f32009-11-20 20:13:39 +010014
15#define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
16
Mike Christie95fe6c12016-06-05 14:31:48 -050017static int sync_request(struct page *page, struct block_device *bdev, int op)
Joern Engel5db53f32009-11-20 20:13:39 +010018{
19 struct bio bio;
20 struct bio_vec bio_vec;
Joern Engel5db53f32009-11-20 20:13:39 +010021
22 bio_init(&bio);
Prasad Joshicd8bfa92012-04-02 09:23:04 +053023 bio.bi_max_vecs = 1;
Joern Engel5db53f32009-11-20 20:13:39 +010024 bio.bi_io_vec = &bio_vec;
25 bio_vec.bv_page = page;
26 bio_vec.bv_len = PAGE_SIZE;
27 bio_vec.bv_offset = 0;
28 bio.bi_vcnt = 1;
Joern Engel5db53f32009-11-20 20:13:39 +010029 bio.bi_bdev = bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -070030 bio.bi_iter.bi_sector = page->index * (PAGE_SIZE >> 9);
31 bio.bi_iter.bi_size = PAGE_SIZE;
Mike Christie95fe6c12016-06-05 14:31:48 -050032 bio_set_op_attrs(&bio, op, 0);
Joern Engel5db53f32009-11-20 20:13:39 +010033
Mike Christie4e49ea42016-06-05 14:31:41 -050034 return submit_bio_wait(&bio);
Joern Engel5db53f32009-11-20 20:13:39 +010035}
36
37static int bdev_readpage(void *_sb, struct page *page)
38{
39 struct super_block *sb = _sb;
40 struct block_device *bdev = logfs_super(sb)->s_bdev;
41 int err;
42
43 err = sync_request(page, bdev, READ);
44 if (err) {
45 ClearPageUptodate(page);
46 SetPageError(page);
47 } else {
48 SetPageUptodate(page);
49 ClearPageError(page);
50 }
51 unlock_page(page);
52 return err;
53}
54
55static DECLARE_WAIT_QUEUE_HEAD(wq);
56
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020057static void writeseg_end_io(struct bio *bio)
Joern Engel5db53f32009-11-20 20:13:39 +010058{
Kent Overstreet2c30c712013-11-07 12:20:26 -080059 struct bio_vec *bvec;
60 int i;
Joern Engel5db53f32009-11-20 20:13:39 +010061 struct super_block *sb = bio->bi_private;
62 struct logfs_super *super = logfs_super(sb);
Joern Engel5db53f32009-11-20 20:13:39 +010063
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020064 BUG_ON(bio->bi_error); /* FIXME: Retry io or write elsewhere */
Joern Engel5db53f32009-11-20 20:13:39 +010065
Kent Overstreet2c30c712013-11-07 12:20:26 -080066 bio_for_each_segment_all(bvec, bio, i) {
67 end_page_writeback(bvec->bv_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030068 put_page(bvec->bv_page);
Kent Overstreet2c30c712013-11-07 12:20:26 -080069 }
Joern Engel5db53f32009-11-20 20:13:39 +010070 bio_put(bio);
71 if (atomic_dec_and_test(&super->s_pending_writes))
72 wake_up(&wq);
73}
74
75static int __bdev_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
76 size_t nr_pages)
77{
78 struct logfs_super *super = logfs_super(sb);
79 struct address_space *mapping = super->s_mapping_inode->i_mapping;
80 struct bio *bio;
81 struct page *page;
Prasad Joshi9f0bbd82012-07-23 10:32:11 +053082 unsigned int max_pages;
Joern Engel5db53f32009-11-20 20:13:39 +010083 int i;
84
Sudip Mukherjeece4f2fd2015-11-05 18:43:49 -080085 max_pages = min_t(size_t, nr_pages, BIO_MAX_PAGES);
Prasad Joshi9f0bbd82012-07-23 10:32:11 +053086
Joern Engel5db53f32009-11-20 20:13:39 +010087 bio = bio_alloc(GFP_NOFS, max_pages);
Joern Engel59fe27c2010-03-17 13:47:45 +010088 BUG_ON(!bio);
Joern Engel5db53f32009-11-20 20:13:39 +010089
90 for (i = 0; i < nr_pages; i++) {
91 if (i >= max_pages) {
92 /* Block layer cannot split bios :( */
93 bio->bi_vcnt = i;
Kent Overstreet4f024f32013-10-11 15:44:27 -070094 bio->bi_iter.bi_size = i * PAGE_SIZE;
Joern Engel5db53f32009-11-20 20:13:39 +010095 bio->bi_bdev = super->s_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -070096 bio->bi_iter.bi_sector = ofs >> 9;
Joern Engel5db53f32009-11-20 20:13:39 +010097 bio->bi_private = sb;
98 bio->bi_end_io = writeseg_end_io;
Mike Christie95fe6c12016-06-05 14:31:48 -050099 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Joern Engel5db53f32009-11-20 20:13:39 +0100100 atomic_inc(&super->s_pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500101 submit_bio(bio);
Joern Engel5db53f32009-11-20 20:13:39 +0100102
103 ofs += i * PAGE_SIZE;
104 index += i;
105 nr_pages -= i;
106 i = 0;
107
108 bio = bio_alloc(GFP_NOFS, max_pages);
109 BUG_ON(!bio);
110 }
111 page = find_lock_page(mapping, index + i);
112 BUG_ON(!page);
113 bio->bi_io_vec[i].bv_page = page;
114 bio->bi_io_vec[i].bv_len = PAGE_SIZE;
115 bio->bi_io_vec[i].bv_offset = 0;
116
117 BUG_ON(PageWriteback(page));
118 set_page_writeback(page);
119 unlock_page(page);
120 }
121 bio->bi_vcnt = nr_pages;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700122 bio->bi_iter.bi_size = nr_pages * PAGE_SIZE;
Joern Engel5db53f32009-11-20 20:13:39 +0100123 bio->bi_bdev = super->s_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700124 bio->bi_iter.bi_sector = ofs >> 9;
Joern Engel5db53f32009-11-20 20:13:39 +0100125 bio->bi_private = sb;
126 bio->bi_end_io = writeseg_end_io;
Mike Christie95fe6c12016-06-05 14:31:48 -0500127 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Joern Engel5db53f32009-11-20 20:13:39 +0100128 atomic_inc(&super->s_pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500129 submit_bio(bio);
Joern Engel5db53f32009-11-20 20:13:39 +0100130 return 0;
131}
132
133static void bdev_writeseg(struct super_block *sb, u64 ofs, size_t len)
134{
135 struct logfs_super *super = logfs_super(sb);
136 int head;
137
138 BUG_ON(super->s_flags & LOGFS_SB_FLAG_RO);
139
140 if (len == 0) {
141 /* This can happen when the object fit perfectly into a
142 * segment, the segment gets written per sync and subsequently
143 * closed.
144 */
145 return;
146 }
147 head = ofs & (PAGE_SIZE - 1);
148 if (head) {
149 ofs -= head;
150 len += head;
151 }
152 len = PAGE_ALIGN(len);
153 __bdev_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
Joern Engel5db53f32009-11-20 20:13:39 +0100154}
155
Joern Engel94215022010-03-04 21:30:58 +0100156
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200157static void erase_end_io(struct bio *bio)
Joern Engel94215022010-03-04 21:30:58 +0100158{
Joern Engel94215022010-03-04 21:30:58 +0100159 struct super_block *sb = bio->bi_private;
160 struct logfs_super *super = logfs_super(sb);
161
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200162 BUG_ON(bio->bi_error); /* FIXME: Retry io or write elsewhere */
Joern Engel94215022010-03-04 21:30:58 +0100163 BUG_ON(bio->bi_vcnt == 0);
164 bio_put(bio);
165 if (atomic_dec_and_test(&super->s_pending_writes))
166 wake_up(&wq);
167}
168
169static int do_erase(struct super_block *sb, u64 ofs, pgoff_t index,
170 size_t nr_pages)
Joern Engel5db53f32009-11-20 20:13:39 +0100171{
172 struct logfs_super *super = logfs_super(sb);
Joern Engel94215022010-03-04 21:30:58 +0100173 struct bio *bio;
Prasad Joshi9f0bbd82012-07-23 10:32:11 +0530174 unsigned int max_pages;
Joern Engel94215022010-03-04 21:30:58 +0100175 int i;
176
Sudip Mukherjeece4f2fd2015-11-05 18:43:49 -0800177 max_pages = min_t(size_t, nr_pages, BIO_MAX_PAGES);
Prasad Joshi9f0bbd82012-07-23 10:32:11 +0530178
Joern Engel94215022010-03-04 21:30:58 +0100179 bio = bio_alloc(GFP_NOFS, max_pages);
Joern Engel59fe27c2010-03-17 13:47:45 +0100180 BUG_ON(!bio);
Joern Engel94215022010-03-04 21:30:58 +0100181
182 for (i = 0; i < nr_pages; i++) {
183 if (i >= max_pages) {
184 /* Block layer cannot split bios :( */
185 bio->bi_vcnt = i;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700186 bio->bi_iter.bi_size = i * PAGE_SIZE;
Joern Engel94215022010-03-04 21:30:58 +0100187 bio->bi_bdev = super->s_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700188 bio->bi_iter.bi_sector = ofs >> 9;
Joern Engel94215022010-03-04 21:30:58 +0100189 bio->bi_private = sb;
190 bio->bi_end_io = erase_end_io;
Mike Christie95fe6c12016-06-05 14:31:48 -0500191 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Joern Engel94215022010-03-04 21:30:58 +0100192 atomic_inc(&super->s_pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500193 submit_bio(bio);
Joern Engel94215022010-03-04 21:30:58 +0100194
195 ofs += i * PAGE_SIZE;
196 index += i;
197 nr_pages -= i;
198 i = 0;
199
200 bio = bio_alloc(GFP_NOFS, max_pages);
201 BUG_ON(!bio);
202 }
203 bio->bi_io_vec[i].bv_page = super->s_erase_page;
204 bio->bi_io_vec[i].bv_len = PAGE_SIZE;
205 bio->bi_io_vec[i].bv_offset = 0;
206 }
207 bio->bi_vcnt = nr_pages;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700208 bio->bi_iter.bi_size = nr_pages * PAGE_SIZE;
Joern Engel94215022010-03-04 21:30:58 +0100209 bio->bi_bdev = super->s_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700210 bio->bi_iter.bi_sector = ofs >> 9;
Joern Engel94215022010-03-04 21:30:58 +0100211 bio->bi_private = sb;
212 bio->bi_end_io = erase_end_io;
Mike Christie95fe6c12016-06-05 14:31:48 -0500213 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Joern Engel94215022010-03-04 21:30:58 +0100214 atomic_inc(&super->s_pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500215 submit_bio(bio);
Joern Engel94215022010-03-04 21:30:58 +0100216 return 0;
217}
218
219static int bdev_erase(struct super_block *sb, loff_t to, size_t len,
220 int ensure_write)
221{
222 struct logfs_super *super = logfs_super(sb);
Joern Engel5db53f32009-11-20 20:13:39 +0100223
224 BUG_ON(to & (PAGE_SIZE - 1));
225 BUG_ON(len & (PAGE_SIZE - 1));
226
Joern Engel94215022010-03-04 21:30:58 +0100227 if (super->s_flags & LOGFS_SB_FLAG_RO)
Joern Engel5db53f32009-11-20 20:13:39 +0100228 return -EROFS;
229
Joern Engel94215022010-03-04 21:30:58 +0100230 if (ensure_write) {
231 /*
232 * Object store doesn't care whether erases happen or not.
233 * But for the journal they are required. Otherwise a scan
234 * can find an old commit entry and assume it is the current
235 * one, travelling back in time.
236 */
237 do_erase(sb, to, to >> PAGE_SHIFT, len >> PAGE_SHIFT);
Joern Engel5db53f32009-11-20 20:13:39 +0100238 }
Joern Engel94215022010-03-04 21:30:58 +0100239
Joern Engel5db53f32009-11-20 20:13:39 +0100240 return 0;
241}
242
243static void bdev_sync(struct super_block *sb)
244{
245 struct logfs_super *super = logfs_super(sb);
246
247 wait_event(wq, atomic_read(&super->s_pending_writes) == 0);
248}
249
250static struct page *bdev_find_first_sb(struct super_block *sb, u64 *ofs)
251{
252 struct logfs_super *super = logfs_super(sb);
253 struct address_space *mapping = super->s_mapping_inode->i_mapping;
254 filler_t *filler = bdev_readpage;
255
256 *ofs = 0;
257 return read_cache_page(mapping, 0, filler, sb);
258}
259
260static struct page *bdev_find_last_sb(struct super_block *sb, u64 *ofs)
261{
262 struct logfs_super *super = logfs_super(sb);
263 struct address_space *mapping = super->s_mapping_inode->i_mapping;
264 filler_t *filler = bdev_readpage;
265 u64 pos = (super->s_bdev->bd_inode->i_size & ~0xfffULL) - 0x1000;
266 pgoff_t index = pos >> PAGE_SHIFT;
267
268 *ofs = pos;
269 return read_cache_page(mapping, index, filler, sb);
270}
271
272static int bdev_write_sb(struct super_block *sb, struct page *page)
273{
274 struct block_device *bdev = logfs_super(sb)->s_bdev;
275
276 /* Nothing special to do for block devices. */
277 return sync_request(page, bdev, WRITE);
278}
279
Al Viroe5a07262010-07-26 12:06:00 +0400280static void bdev_put_device(struct logfs_super *s)
Joern Engel5db53f32009-11-20 20:13:39 +0100281{
Tejun Heoe525fd82010-11-13 11:55:17 +0100282 blkdev_put(s->s_bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Joern Engel5db53f32009-11-20 20:13:39 +0100283}
284
Joern Engel6f485b412010-05-07 19:38:40 +0200285static int bdev_can_write_buf(struct super_block *sb, u64 ofs)
286{
287 return 0;
288}
289
Joern Engel5db53f32009-11-20 20:13:39 +0100290static const struct logfs_device_ops bd_devops = {
291 .find_first_sb = bdev_find_first_sb,
292 .find_last_sb = bdev_find_last_sb,
293 .write_sb = bdev_write_sb,
294 .readpage = bdev_readpage,
295 .writeseg = bdev_writeseg,
296 .erase = bdev_erase,
Joern Engel6f485b412010-05-07 19:38:40 +0200297 .can_write_buf = bdev_can_write_buf,
Joern Engel5db53f32009-11-20 20:13:39 +0100298 .sync = bdev_sync,
299 .put_device = bdev_put_device,
300};
301
Al Viro7d945a32010-07-26 11:53:30 +0400302int logfs_get_sb_bdev(struct logfs_super *p, struct file_system_type *type,
303 const char *devname)
Joern Engel5db53f32009-11-20 20:13:39 +0100304{
305 struct block_device *bdev;
306
Tejun Heod4d77622010-11-13 11:55:18 +0100307 bdev = blkdev_get_by_path(devname, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
308 type);
Al Viro7d945a32010-07-26 11:53:30 +0400309 if (IS_ERR(bdev))
Joern Engel5db53f32009-11-20 20:13:39 +0100310 return PTR_ERR(bdev);
311
312 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
313 int mtdnr = MINOR(bdev->bd_dev);
Tejun Heoe525fd82010-11-13 11:55:17 +0100314 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Al Viro7d945a32010-07-26 11:53:30 +0400315 return logfs_get_sb_mtd(p, mtdnr);
Joern Engel5db53f32009-11-20 20:13:39 +0100316 }
317
Al Viro0d85c792010-07-26 11:33:39 +0400318 p->s_bdev = bdev;
319 p->s_mtd = NULL;
320 p->s_devops = &bd_devops;
Al Viro7d945a32010-07-26 11:53:30 +0400321 return 0;
Joern Engel5db53f32009-11-20 20:13:39 +0100322}