2 * fs/logfs/dev_bdev.c - Device access methods for block devices
4 * As should be obvious for Linux kernel code, license is GPLv2
6 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
10 #include <linux/blkdev.h>
11 #include <linux/buffer_head.h>
12 #include <linux/gfp.h>
13 #include <linux/prefetch.h>
15 #define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
17 static int sync_request(struct page *page, struct block_device *bdev, int rw)
20 struct bio_vec bio_vec;
24 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;
29 bio.bi_size = PAGE_SIZE;
31 bio.bi_sector = page->index * (PAGE_SIZE >> 9);
33 return submit_bio_wait(rw, &bio);
36 static int bdev_readpage(void *_sb, struct page *page)
38 struct super_block *sb = _sb;
39 struct block_device *bdev = logfs_super(sb)->s_bdev;
42 err = sync_request(page, bdev, READ);
44 ClearPageUptodate(page);
47 SetPageUptodate(page);
54 static DECLARE_WAIT_QUEUE_HEAD(wq);
56 static void writeseg_end_io(struct bio *bio, int err)
58 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
59 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
60 struct super_block *sb = bio->bi_private;
61 struct logfs_super *super = logfs_super(sb);
64 BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */
66 BUG_ON(bio->bi_vcnt == 0);
69 if (--bvec >= bio->bi_io_vec)
70 prefetchw(&bvec->bv_page->flags);
72 end_page_writeback(page);
73 page_cache_release(page);
74 } while (bvec >= bio->bi_io_vec);
76 if (atomic_dec_and_test(&super->s_pending_writes))
80 static int __bdev_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
83 struct logfs_super *super = logfs_super(sb);
84 struct address_space *mapping = super->s_mapping_inode->i_mapping;
87 unsigned int max_pages;
90 max_pages = min(nr_pages, (size_t) bio_get_nr_vecs(super->s_bdev));
92 bio = bio_alloc(GFP_NOFS, max_pages);
95 for (i = 0; i < nr_pages; i++) {
97 /* Block layer cannot split bios :( */
99 bio->bi_size = i * PAGE_SIZE;
100 bio->bi_bdev = super->s_bdev;
101 bio->bi_sector = ofs >> 9;
102 bio->bi_private = sb;
103 bio->bi_end_io = writeseg_end_io;
104 atomic_inc(&super->s_pending_writes);
105 submit_bio(WRITE, bio);
107 ofs += i * PAGE_SIZE;
112 bio = bio_alloc(GFP_NOFS, max_pages);
115 page = find_lock_page(mapping, index + i);
117 bio->bi_io_vec[i].bv_page = page;
118 bio->bi_io_vec[i].bv_len = PAGE_SIZE;
119 bio->bi_io_vec[i].bv_offset = 0;
121 BUG_ON(PageWriteback(page));
122 set_page_writeback(page);
125 bio->bi_vcnt = nr_pages;
126 bio->bi_size = nr_pages * PAGE_SIZE;
127 bio->bi_bdev = super->s_bdev;
128 bio->bi_sector = ofs >> 9;
129 bio->bi_private = sb;
130 bio->bi_end_io = writeseg_end_io;
131 atomic_inc(&super->s_pending_writes);
132 submit_bio(WRITE, bio);
136 static void bdev_writeseg(struct super_block *sb, u64 ofs, size_t len)
138 struct logfs_super *super = logfs_super(sb);
141 BUG_ON(super->s_flags & LOGFS_SB_FLAG_RO);
144 /* This can happen when the object fit perfectly into a
145 * segment, the segment gets written per sync and subsequently
150 head = ofs & (PAGE_SIZE - 1);
155 len = PAGE_ALIGN(len);
156 __bdev_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
160 static void erase_end_io(struct bio *bio, int err)
162 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
163 struct super_block *sb = bio->bi_private;
164 struct logfs_super *super = logfs_super(sb);
166 BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */
168 BUG_ON(bio->bi_vcnt == 0);
170 if (atomic_dec_and_test(&super->s_pending_writes))
174 static int do_erase(struct super_block *sb, u64 ofs, pgoff_t index,
177 struct logfs_super *super = logfs_super(sb);
179 unsigned int max_pages;
182 max_pages = min(nr_pages, (size_t) bio_get_nr_vecs(super->s_bdev));
184 bio = bio_alloc(GFP_NOFS, max_pages);
187 for (i = 0; i < nr_pages; i++) {
188 if (i >= max_pages) {
189 /* Block layer cannot split bios :( */
191 bio->bi_size = i * PAGE_SIZE;
192 bio->bi_bdev = super->s_bdev;
193 bio->bi_sector = ofs >> 9;
194 bio->bi_private = sb;
195 bio->bi_end_io = erase_end_io;
196 atomic_inc(&super->s_pending_writes);
197 submit_bio(WRITE, bio);
199 ofs += i * PAGE_SIZE;
204 bio = bio_alloc(GFP_NOFS, max_pages);
207 bio->bi_io_vec[i].bv_page = super->s_erase_page;
208 bio->bi_io_vec[i].bv_len = PAGE_SIZE;
209 bio->bi_io_vec[i].bv_offset = 0;
211 bio->bi_vcnt = nr_pages;
212 bio->bi_size = nr_pages * PAGE_SIZE;
213 bio->bi_bdev = super->s_bdev;
214 bio->bi_sector = ofs >> 9;
215 bio->bi_private = sb;
216 bio->bi_end_io = erase_end_io;
217 atomic_inc(&super->s_pending_writes);
218 submit_bio(WRITE, bio);
222 static int bdev_erase(struct super_block *sb, loff_t to, size_t len,
225 struct logfs_super *super = logfs_super(sb);
227 BUG_ON(to & (PAGE_SIZE - 1));
228 BUG_ON(len & (PAGE_SIZE - 1));
230 if (super->s_flags & LOGFS_SB_FLAG_RO)
235 * Object store doesn't care whether erases happen or not.
236 * But for the journal they are required. Otherwise a scan
237 * can find an old commit entry and assume it is the current
238 * one, travelling back in time.
240 do_erase(sb, to, to >> PAGE_SHIFT, len >> PAGE_SHIFT);
246 static void bdev_sync(struct super_block *sb)
248 struct logfs_super *super = logfs_super(sb);
250 wait_event(wq, atomic_read(&super->s_pending_writes) == 0);
253 static struct page *bdev_find_first_sb(struct super_block *sb, u64 *ofs)
255 struct logfs_super *super = logfs_super(sb);
256 struct address_space *mapping = super->s_mapping_inode->i_mapping;
257 filler_t *filler = bdev_readpage;
260 return read_cache_page(mapping, 0, filler, sb);
263 static struct page *bdev_find_last_sb(struct super_block *sb, u64 *ofs)
265 struct logfs_super *super = logfs_super(sb);
266 struct address_space *mapping = super->s_mapping_inode->i_mapping;
267 filler_t *filler = bdev_readpage;
268 u64 pos = (super->s_bdev->bd_inode->i_size & ~0xfffULL) - 0x1000;
269 pgoff_t index = pos >> PAGE_SHIFT;
272 return read_cache_page(mapping, index, filler, sb);
275 static int bdev_write_sb(struct super_block *sb, struct page *page)
277 struct block_device *bdev = logfs_super(sb)->s_bdev;
279 /* Nothing special to do for block devices. */
280 return sync_request(page, bdev, WRITE);
283 static void bdev_put_device(struct logfs_super *s)
285 blkdev_put(s->s_bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
288 static int bdev_can_write_buf(struct super_block *sb, u64 ofs)
293 static const struct logfs_device_ops bd_devops = {
294 .find_first_sb = bdev_find_first_sb,
295 .find_last_sb = bdev_find_last_sb,
296 .write_sb = bdev_write_sb,
297 .readpage = bdev_readpage,
298 .writeseg = bdev_writeseg,
300 .can_write_buf = bdev_can_write_buf,
302 .put_device = bdev_put_device,
305 int logfs_get_sb_bdev(struct logfs_super *p, struct file_system_type *type,
308 struct block_device *bdev;
310 bdev = blkdev_get_by_path(devname, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
313 return PTR_ERR(bdev);
315 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
316 int mtdnr = MINOR(bdev->bd_dev);
317 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
318 return logfs_get_sb_mtd(p, mtdnr);
323 p->s_devops = &bd_devops;