blob: 76279e11982d854c9b22312cf51b3bd3a97e256d [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
Joern Engel5db53f32009-11-20 20:13:39 +010017static int sync_request(struct page *page, struct block_device *bdev, int rw)
18{
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;
Joern Engel5db53f32009-11-20 20:13:39 +010032
Kent Overstreet33879d42013-11-23 22:33:32 -080033 return submit_bio_wait(rw, &bio);
Joern Engel5db53f32009-11-20 20:13:39 +010034}
35
36static int bdev_readpage(void *_sb, struct page *page)
37{
38 struct super_block *sb = _sb;
39 struct block_device *bdev = logfs_super(sb)->s_bdev;
40 int err;
41
42 err = sync_request(page, bdev, READ);
43 if (err) {
44 ClearPageUptodate(page);
45 SetPageError(page);
46 } else {
47 SetPageUptodate(page);
48 ClearPageError(page);
49 }
50 unlock_page(page);
51 return err;
52}
53
54static DECLARE_WAIT_QUEUE_HEAD(wq);
55
56static void writeseg_end_io(struct bio *bio, int err)
57{
58 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
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
64 BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */
65 BUG_ON(err);
Joern Engel5db53f32009-11-20 20:13:39 +010066
Kent Overstreet2c30c712013-11-07 12:20:26 -080067 bio_for_each_segment_all(bvec, bio, i) {
68 end_page_writeback(bvec->bv_page);
69 page_cache_release(bvec->bv_page);
70 }
Joern Engel5db53f32009-11-20 20:13:39 +010071 bio_put(bio);
72 if (atomic_dec_and_test(&super->s_pending_writes))
73 wake_up(&wq);
74}
75
76static int __bdev_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
77 size_t nr_pages)
78{
79 struct logfs_super *super = logfs_super(sb);
80 struct address_space *mapping = super->s_mapping_inode->i_mapping;
81 struct bio *bio;
82 struct page *page;
Prasad Joshi9f0bbd82012-07-23 10:32:11 +053083 unsigned int max_pages;
Joern Engel5db53f32009-11-20 20:13:39 +010084 int i;
85
Prasad Joshi9f0bbd82012-07-23 10:32:11 +053086 max_pages = min(nr_pages, (size_t) bio_get_nr_vecs(super->s_bdev));
87
Joern Engel5db53f32009-11-20 20:13:39 +010088 bio = bio_alloc(GFP_NOFS, max_pages);
Joern Engel59fe27c2010-03-17 13:47:45 +010089 BUG_ON(!bio);
Joern Engel5db53f32009-11-20 20:13:39 +010090
91 for (i = 0; i < nr_pages; i++) {
92 if (i >= max_pages) {
93 /* Block layer cannot split bios :( */
94 bio->bi_vcnt = i;
Kent Overstreet4f024f32013-10-11 15:44:27 -070095 bio->bi_iter.bi_size = i * PAGE_SIZE;
Joern Engel5db53f32009-11-20 20:13:39 +010096 bio->bi_bdev = super->s_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -070097 bio->bi_iter.bi_sector = ofs >> 9;
Joern Engel5db53f32009-11-20 20:13:39 +010098 bio->bi_private = sb;
99 bio->bi_end_io = writeseg_end_io;
100 atomic_inc(&super->s_pending_writes);
101 submit_bio(WRITE, bio);
102
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;
127 atomic_inc(&super->s_pending_writes);
128 submit_bio(WRITE, bio);
129 return 0;
130}
131
132static void bdev_writeseg(struct super_block *sb, u64 ofs, size_t len)
133{
134 struct logfs_super *super = logfs_super(sb);
135 int head;
136
137 BUG_ON(super->s_flags & LOGFS_SB_FLAG_RO);
138
139 if (len == 0) {
140 /* This can happen when the object fit perfectly into a
141 * segment, the segment gets written per sync and subsequently
142 * closed.
143 */
144 return;
145 }
146 head = ofs & (PAGE_SIZE - 1);
147 if (head) {
148 ofs -= head;
149 len += head;
150 }
151 len = PAGE_ALIGN(len);
152 __bdev_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
Joern Engel5db53f32009-11-20 20:13:39 +0100153}
154
Joern Engel94215022010-03-04 21:30:58 +0100155
156static void erase_end_io(struct bio *bio, int err)
157{
158 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
159 struct super_block *sb = bio->bi_private;
160 struct logfs_super *super = logfs_super(sb);
161
162 BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */
163 BUG_ON(err);
164 BUG_ON(bio->bi_vcnt == 0);
165 bio_put(bio);
166 if (atomic_dec_and_test(&super->s_pending_writes))
167 wake_up(&wq);
168}
169
170static int do_erase(struct super_block *sb, u64 ofs, pgoff_t index,
171 size_t nr_pages)
Joern Engel5db53f32009-11-20 20:13:39 +0100172{
173 struct logfs_super *super = logfs_super(sb);
Joern Engel94215022010-03-04 21:30:58 +0100174 struct bio *bio;
Prasad Joshi9f0bbd82012-07-23 10:32:11 +0530175 unsigned int max_pages;
Joern Engel94215022010-03-04 21:30:58 +0100176 int i;
177
Prasad Joshi9f0bbd82012-07-23 10:32:11 +0530178 max_pages = min(nr_pages, (size_t) bio_get_nr_vecs(super->s_bdev));
179
Joern Engel94215022010-03-04 21:30:58 +0100180 bio = bio_alloc(GFP_NOFS, max_pages);
Joern Engel59fe27c2010-03-17 13:47:45 +0100181 BUG_ON(!bio);
Joern Engel94215022010-03-04 21:30:58 +0100182
183 for (i = 0; i < nr_pages; i++) {
184 if (i >= max_pages) {
185 /* Block layer cannot split bios :( */
186 bio->bi_vcnt = i;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700187 bio->bi_iter.bi_size = i * PAGE_SIZE;
Joern Engel94215022010-03-04 21:30:58 +0100188 bio->bi_bdev = super->s_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700189 bio->bi_iter.bi_sector = ofs >> 9;
Joern Engel94215022010-03-04 21:30:58 +0100190 bio->bi_private = sb;
191 bio->bi_end_io = erase_end_io;
192 atomic_inc(&super->s_pending_writes);
193 submit_bio(WRITE, bio);
194
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;
213 atomic_inc(&super->s_pending_writes);
214 submit_bio(WRITE, bio);
215 return 0;
216}
217
218static int bdev_erase(struct super_block *sb, loff_t to, size_t len,
219 int ensure_write)
220{
221 struct logfs_super *super = logfs_super(sb);
Joern Engel5db53f32009-11-20 20:13:39 +0100222
223 BUG_ON(to & (PAGE_SIZE - 1));
224 BUG_ON(len & (PAGE_SIZE - 1));
225
Joern Engel94215022010-03-04 21:30:58 +0100226 if (super->s_flags & LOGFS_SB_FLAG_RO)
Joern Engel5db53f32009-11-20 20:13:39 +0100227 return -EROFS;
228
Joern Engel94215022010-03-04 21:30:58 +0100229 if (ensure_write) {
230 /*
231 * Object store doesn't care whether erases happen or not.
232 * But for the journal they are required. Otherwise a scan
233 * can find an old commit entry and assume it is the current
234 * one, travelling back in time.
235 */
236 do_erase(sb, to, to >> PAGE_SHIFT, len >> PAGE_SHIFT);
Joern Engel5db53f32009-11-20 20:13:39 +0100237 }
Joern Engel94215022010-03-04 21:30:58 +0100238
Joern Engel5db53f32009-11-20 20:13:39 +0100239 return 0;
240}
241
242static void bdev_sync(struct super_block *sb)
243{
244 struct logfs_super *super = logfs_super(sb);
245
246 wait_event(wq, atomic_read(&super->s_pending_writes) == 0);
247}
248
249static struct page *bdev_find_first_sb(struct super_block *sb, u64 *ofs)
250{
251 struct logfs_super *super = logfs_super(sb);
252 struct address_space *mapping = super->s_mapping_inode->i_mapping;
253 filler_t *filler = bdev_readpage;
254
255 *ofs = 0;
256 return read_cache_page(mapping, 0, filler, sb);
257}
258
259static struct page *bdev_find_last_sb(struct super_block *sb, u64 *ofs)
260{
261 struct logfs_super *super = logfs_super(sb);
262 struct address_space *mapping = super->s_mapping_inode->i_mapping;
263 filler_t *filler = bdev_readpage;
264 u64 pos = (super->s_bdev->bd_inode->i_size & ~0xfffULL) - 0x1000;
265 pgoff_t index = pos >> PAGE_SHIFT;
266
267 *ofs = pos;
268 return read_cache_page(mapping, index, filler, sb);
269}
270
271static int bdev_write_sb(struct super_block *sb, struct page *page)
272{
273 struct block_device *bdev = logfs_super(sb)->s_bdev;
274
275 /* Nothing special to do for block devices. */
276 return sync_request(page, bdev, WRITE);
277}
278
Al Viroe5a07262010-07-26 12:06:00 +0400279static void bdev_put_device(struct logfs_super *s)
Joern Engel5db53f32009-11-20 20:13:39 +0100280{
Tejun Heoe525fd82010-11-13 11:55:17 +0100281 blkdev_put(s->s_bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Joern Engel5db53f32009-11-20 20:13:39 +0100282}
283
Joern Engel6f485b412010-05-07 19:38:40 +0200284static int bdev_can_write_buf(struct super_block *sb, u64 ofs)
285{
286 return 0;
287}
288
Joern Engel5db53f32009-11-20 20:13:39 +0100289static const struct logfs_device_ops bd_devops = {
290 .find_first_sb = bdev_find_first_sb,
291 .find_last_sb = bdev_find_last_sb,
292 .write_sb = bdev_write_sb,
293 .readpage = bdev_readpage,
294 .writeseg = bdev_writeseg,
295 .erase = bdev_erase,
Joern Engel6f485b412010-05-07 19:38:40 +0200296 .can_write_buf = bdev_can_write_buf,
Joern Engel5db53f32009-11-20 20:13:39 +0100297 .sync = bdev_sync,
298 .put_device = bdev_put_device,
299};
300
Al Viro7d945a32010-07-26 11:53:30 +0400301int logfs_get_sb_bdev(struct logfs_super *p, struct file_system_type *type,
302 const char *devname)
Joern Engel5db53f32009-11-20 20:13:39 +0100303{
304 struct block_device *bdev;
305
Tejun Heod4d77622010-11-13 11:55:18 +0100306 bdev = blkdev_get_by_path(devname, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
307 type);
Al Viro7d945a32010-07-26 11:53:30 +0400308 if (IS_ERR(bdev))
Joern Engel5db53f32009-11-20 20:13:39 +0100309 return PTR_ERR(bdev);
310
311 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
312 int mtdnr = MINOR(bdev->bd_dev);
Tejun Heoe525fd82010-11-13 11:55:17 +0100313 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Al Viro7d945a32010-07-26 11:53:30 +0400314 return logfs_get_sb_mtd(p, mtdnr);
Joern Engel5db53f32009-11-20 20:13:39 +0100315 }
316
Al Viro0d85c792010-07-26 11:33:39 +0400317 p->s_bdev = bdev;
318 p->s_mtd = NULL;
319 p->s_devops = &bd_devops;
Al Viro7d945a32010-07-26 11:53:30 +0400320 return 0;
Joern Engel5db53f32009-11-20 20:13:39 +0100321}