aboutsummaryrefslogtreecommitdiffstats
path: root/fs/logfs/dev_bdev.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/logfs/dev_bdev.c')
-rw-r--r--fs/logfs/dev_bdev.c263
1 files changed, 263 insertions, 0 deletions
diff --git a/fs/logfs/dev_bdev.c b/fs/logfs/dev_bdev.c
new file mode 100644
index 000000000000..58a057b6e1af
--- /dev/null
+++ b/fs/logfs/dev_bdev.c
@@ -0,0 +1,263 @@
1/*
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>
12
13#define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
14
15static void request_complete(struct bio *bio, int err)
16{
17 complete((struct completion *)bio->bi_private);
18}
19
20static int sync_request(struct page *page, struct block_device *bdev, int rw)
21{
22 struct bio bio;
23 struct bio_vec bio_vec;
24 struct completion complete;
25
26 bio_init(&bio);
27 bio.bi_io_vec = &bio_vec;
28 bio_vec.bv_page = page;
29 bio_vec.bv_len = PAGE_SIZE;
30 bio_vec.bv_offset = 0;
31 bio.bi_vcnt = 1;
32 bio.bi_idx = 0;
33 bio.bi_size = PAGE_SIZE;
34 bio.bi_bdev = bdev;
35 bio.bi_sector = page->index * (PAGE_SIZE >> 9);
36 init_completion(&complete);
37 bio.bi_private = &complete;
38 bio.bi_end_io = request_complete;
39
40 submit_bio(rw, &bio);
41 generic_unplug_device(bdev_get_queue(bdev));
42 wait_for_completion(&complete);
43 return test_bit(BIO_UPTODATE, &bio.bi_flags) ? 0 : -EIO;
44}
45
46static int bdev_readpage(void *_sb, struct page *page)
47{
48 struct super_block *sb = _sb;
49 struct block_device *bdev = logfs_super(sb)->s_bdev;
50 int err;
51
52 err = sync_request(page, bdev, READ);
53 if (err) {
54 ClearPageUptodate(page);
55 SetPageError(page);
56 } else {
57 SetPageUptodate(page);
58 ClearPageError(page);
59 }
60 unlock_page(page);
61 return err;
62}
63
64static DECLARE_WAIT_QUEUE_HEAD(wq);
65
66static void writeseg_end_io(struct bio *bio, int err)
67{
68 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
69 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
70 struct super_block *sb = bio->bi_private;
71 struct logfs_super *super = logfs_super(sb);
72 struct page *page;
73
74 BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */
75 BUG_ON(err);
76 BUG_ON(bio->bi_vcnt == 0);
77 do {
78 page = bvec->bv_page;
79 if (--bvec >= bio->bi_io_vec)
80 prefetchw(&bvec->bv_page->flags);
81
82 end_page_writeback(page);
83 } while (bvec >= bio->bi_io_vec);
84 bio_put(bio);
85 if (atomic_dec_and_test(&super->s_pending_writes))
86 wake_up(&wq);
87}
88
89static int __bdev_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
90 size_t nr_pages)
91{
92 struct logfs_super *super = logfs_super(sb);
93 struct address_space *mapping = super->s_mapping_inode->i_mapping;
94 struct bio *bio;
95 struct page *page;
96 struct request_queue *q = bdev_get_queue(sb->s_bdev);
97 unsigned int max_pages = queue_max_hw_sectors(q) >> (PAGE_SHIFT - 9);
98 int i;
99
100 bio = bio_alloc(GFP_NOFS, max_pages);
101 BUG_ON(!bio); /* FIXME: handle this */
102
103 for (i = 0; i < nr_pages; i++) {
104 if (i >= max_pages) {
105 /* Block layer cannot split bios :( */
106 bio->bi_vcnt = i;
107 bio->bi_idx = 0;
108 bio->bi_size = i * PAGE_SIZE;
109 bio->bi_bdev = super->s_bdev;
110 bio->bi_sector = ofs >> 9;
111 bio->bi_private = sb;
112 bio->bi_end_io = writeseg_end_io;
113 atomic_inc(&super->s_pending_writes);
114 submit_bio(WRITE, bio);
115
116 ofs += i * PAGE_SIZE;
117 index += i;
118 nr_pages -= i;
119 i = 0;
120
121 bio = bio_alloc(GFP_NOFS, max_pages);
122 BUG_ON(!bio);
123 }
124 page = find_lock_page(mapping, index + i);
125 BUG_ON(!page);
126 bio->bi_io_vec[i].bv_page = page;
127 bio->bi_io_vec[i].bv_len = PAGE_SIZE;
128 bio->bi_io_vec[i].bv_offset = 0;
129
130 BUG_ON(PageWriteback(page));
131 set_page_writeback(page);
132 unlock_page(page);
133 }
134 bio->bi_vcnt = nr_pages;
135 bio->bi_idx = 0;
136 bio->bi_size = nr_pages * PAGE_SIZE;
137 bio->bi_bdev = super->s_bdev;
138 bio->bi_sector = ofs >> 9;
139 bio->bi_private = sb;
140 bio->bi_end_io = writeseg_end_io;
141 atomic_inc(&super->s_pending_writes);
142 submit_bio(WRITE, bio);
143 return 0;
144}
145
146static void bdev_writeseg(struct super_block *sb, u64 ofs, size_t len)
147{
148 struct logfs_super *super = logfs_super(sb);
149 int head;
150
151 BUG_ON(super->s_flags & LOGFS_SB_FLAG_RO);
152
153 if (len == 0) {
154 /* This can happen when the object fit perfectly into a
155 * segment, the segment gets written per sync and subsequently
156 * closed.
157 */
158 return;
159 }
160 head = ofs & (PAGE_SIZE - 1);
161 if (head) {
162 ofs -= head;
163 len += head;
164 }
165 len = PAGE_ALIGN(len);
166 __bdev_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
167 generic_unplug_device(bdev_get_queue(logfs_super(sb)->s_bdev));
168}
169
170static int bdev_erase(struct super_block *sb, loff_t to, size_t len)
171{
172 struct logfs_super *super = logfs_super(sb);
173 struct address_space *mapping = super->s_mapping_inode->i_mapping;
174 struct page *page;
175 pgoff_t index = to >> PAGE_SHIFT;
176 int i, nr_pages = len >> PAGE_SHIFT;
177
178 BUG_ON(to & (PAGE_SIZE - 1));
179 BUG_ON(len & (PAGE_SIZE - 1));
180
181 if (logfs_super(sb)->s_flags & LOGFS_SB_FLAG_RO)
182 return -EROFS;
183
184 for (i = 0; i < nr_pages; i++) {
185 page = find_get_page(mapping, index + i);
186 if (page) {
187 memset(page_address(page), 0xFF, PAGE_SIZE);
188 page_cache_release(page);
189 }
190 }
191 return 0;
192}
193
194static void bdev_sync(struct super_block *sb)
195{
196 struct logfs_super *super = logfs_super(sb);
197
198 wait_event(wq, atomic_read(&super->s_pending_writes) == 0);
199}
200
201static struct page *bdev_find_first_sb(struct super_block *sb, u64 *ofs)
202{
203 struct logfs_super *super = logfs_super(sb);
204 struct address_space *mapping = super->s_mapping_inode->i_mapping;
205 filler_t *filler = bdev_readpage;
206
207 *ofs = 0;
208 return read_cache_page(mapping, 0, filler, sb);
209}
210
211static struct page *bdev_find_last_sb(struct super_block *sb, u64 *ofs)
212{
213 struct logfs_super *super = logfs_super(sb);
214 struct address_space *mapping = super->s_mapping_inode->i_mapping;
215 filler_t *filler = bdev_readpage;
216 u64 pos = (super->s_bdev->bd_inode->i_size & ~0xfffULL) - 0x1000;
217 pgoff_t index = pos >> PAGE_SHIFT;
218
219 *ofs = pos;
220 return read_cache_page(mapping, index, filler, sb);
221}
222
223static int bdev_write_sb(struct super_block *sb, struct page *page)
224{
225 struct block_device *bdev = logfs_super(sb)->s_bdev;
226
227 /* Nothing special to do for block devices. */
228 return sync_request(page, bdev, WRITE);
229}
230
231static void bdev_put_device(struct super_block *sb)
232{
233 close_bdev_exclusive(logfs_super(sb)->s_bdev, FMODE_READ|FMODE_WRITE);
234}
235
236static const struct logfs_device_ops bd_devops = {
237 .find_first_sb = bdev_find_first_sb,
238 .find_last_sb = bdev_find_last_sb,
239 .write_sb = bdev_write_sb,
240 .readpage = bdev_readpage,
241 .writeseg = bdev_writeseg,
242 .erase = bdev_erase,
243 .sync = bdev_sync,
244 .put_device = bdev_put_device,
245};
246
247int logfs_get_sb_bdev(struct file_system_type *type, int flags,
248 const char *devname, struct vfsmount *mnt)
249{
250 struct block_device *bdev;
251
252 bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, type);
253 if (IS_ERR(bdev))
254 return PTR_ERR(bdev);
255
256 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
257 int mtdnr = MINOR(bdev->bd_dev);
258 close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
259 return logfs_get_sb_mtd(type, flags, mtdnr, mnt);
260 }
261
262 return logfs_get_sb_device(type, flags, NULL, bdev, &bd_devops, mnt);
263}