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 void request_complete(struct bio
*bio
, int err
)
19 complete((struct completion
*)bio
->bi_private
);
22 static int sync_request(struct page
*page
, struct block_device
*bdev
, int rw
)
25 struct bio_vec bio_vec
;
26 struct completion complete
;
30 bio
.bi_io_vec
= &bio_vec
;
31 bio_vec
.bv_page
= page
;
32 bio_vec
.bv_len
= PAGE_SIZE
;
33 bio_vec
.bv_offset
= 0;
36 bio
.bi_size
= PAGE_SIZE
;
38 bio
.bi_sector
= page
->index
* (PAGE_SIZE
>> 9);
39 init_completion(&complete
);
40 bio
.bi_private
= &complete
;
41 bio
.bi_end_io
= request_complete
;
44 wait_for_completion(&complete
);
45 return test_bit(BIO_UPTODATE
, &bio
.bi_flags
) ? 0 : -EIO
;
48 static int bdev_readpage(void *_sb
, struct page
*page
)
50 struct super_block
*sb
= _sb
;
51 struct block_device
*bdev
= logfs_super(sb
)->s_bdev
;
54 err
= sync_request(page
, bdev
, READ
);
56 ClearPageUptodate(page
);
59 SetPageUptodate(page
);
66 static DECLARE_WAIT_QUEUE_HEAD(wq
);
68 static void writeseg_end_io(struct bio
*bio
, int err
)
70 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
71 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
72 struct super_block
*sb
= bio
->bi_private
;
73 struct logfs_super
*super
= logfs_super(sb
);
76 BUG_ON(!uptodate
); /* FIXME: Retry io or write elsewhere */
78 BUG_ON(bio
->bi_vcnt
== 0);
81 if (--bvec
>= bio
->bi_io_vec
)
82 prefetchw(&bvec
->bv_page
->flags
);
84 end_page_writeback(page
);
85 page_cache_release(page
);
86 } while (bvec
>= bio
->bi_io_vec
);
88 if (atomic_dec_and_test(&super
->s_pending_writes
))
92 static int __bdev_writeseg(struct super_block
*sb
, u64 ofs
, pgoff_t index
,
95 struct logfs_super
*super
= logfs_super(sb
);
96 struct address_space
*mapping
= super
->s_mapping_inode
->i_mapping
;
99 unsigned int max_pages
;
102 max_pages
= min(nr_pages
, (size_t) bio_get_nr_vecs(super
->s_bdev
));
104 bio
= bio_alloc(GFP_NOFS
, max_pages
);
107 for (i
= 0; i
< nr_pages
; i
++) {
108 if (i
>= max_pages
) {
109 /* Block layer cannot split bios :( */
112 bio
->bi_size
= i
* PAGE_SIZE
;
113 bio
->bi_bdev
= super
->s_bdev
;
114 bio
->bi_sector
= ofs
>> 9;
115 bio
->bi_private
= sb
;
116 bio
->bi_end_io
= writeseg_end_io
;
117 atomic_inc(&super
->s_pending_writes
);
118 submit_bio(WRITE
, bio
);
120 ofs
+= i
* PAGE_SIZE
;
125 bio
= bio_alloc(GFP_NOFS
, max_pages
);
128 page
= find_lock_page(mapping
, index
+ i
);
130 bio
->bi_io_vec
[i
].bv_page
= page
;
131 bio
->bi_io_vec
[i
].bv_len
= PAGE_SIZE
;
132 bio
->bi_io_vec
[i
].bv_offset
= 0;
134 BUG_ON(PageWriteback(page
));
135 set_page_writeback(page
);
138 bio
->bi_vcnt
= nr_pages
;
140 bio
->bi_size
= nr_pages
* PAGE_SIZE
;
141 bio
->bi_bdev
= super
->s_bdev
;
142 bio
->bi_sector
= ofs
>> 9;
143 bio
->bi_private
= sb
;
144 bio
->bi_end_io
= writeseg_end_io
;
145 atomic_inc(&super
->s_pending_writes
);
146 submit_bio(WRITE
, bio
);
150 static void bdev_writeseg(struct super_block
*sb
, u64 ofs
, size_t len
)
152 struct logfs_super
*super
= logfs_super(sb
);
155 BUG_ON(super
->s_flags
& LOGFS_SB_FLAG_RO
);
158 /* This can happen when the object fit perfectly into a
159 * segment, the segment gets written per sync and subsequently
164 head
= ofs
& (PAGE_SIZE
- 1);
169 len
= PAGE_ALIGN(len
);
170 __bdev_writeseg(sb
, ofs
, ofs
>> PAGE_SHIFT
, len
>> PAGE_SHIFT
);
174 static void erase_end_io(struct bio
*bio
, int err
)
176 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
177 struct super_block
*sb
= bio
->bi_private
;
178 struct logfs_super
*super
= logfs_super(sb
);
180 BUG_ON(!uptodate
); /* FIXME: Retry io or write elsewhere */
182 BUG_ON(bio
->bi_vcnt
== 0);
184 if (atomic_dec_and_test(&super
->s_pending_writes
))
188 static int do_erase(struct super_block
*sb
, u64 ofs
, pgoff_t index
,
191 struct logfs_super
*super
= logfs_super(sb
);
193 unsigned int max_pages
;
196 max_pages
= min(nr_pages
, (size_t) bio_get_nr_vecs(super
->s_bdev
));
198 bio
= bio_alloc(GFP_NOFS
, max_pages
);
201 for (i
= 0; i
< nr_pages
; i
++) {
202 if (i
>= max_pages
) {
203 /* Block layer cannot split bios :( */
206 bio
->bi_size
= i
* PAGE_SIZE
;
207 bio
->bi_bdev
= super
->s_bdev
;
208 bio
->bi_sector
= ofs
>> 9;
209 bio
->bi_private
= sb
;
210 bio
->bi_end_io
= erase_end_io
;
211 atomic_inc(&super
->s_pending_writes
);
212 submit_bio(WRITE
, bio
);
214 ofs
+= i
* PAGE_SIZE
;
219 bio
= bio_alloc(GFP_NOFS
, max_pages
);
222 bio
->bi_io_vec
[i
].bv_page
= super
->s_erase_page
;
223 bio
->bi_io_vec
[i
].bv_len
= PAGE_SIZE
;
224 bio
->bi_io_vec
[i
].bv_offset
= 0;
226 bio
->bi_vcnt
= nr_pages
;
228 bio
->bi_size
= nr_pages
* PAGE_SIZE
;
229 bio
->bi_bdev
= super
->s_bdev
;
230 bio
->bi_sector
= ofs
>> 9;
231 bio
->bi_private
= sb
;
232 bio
->bi_end_io
= erase_end_io
;
233 atomic_inc(&super
->s_pending_writes
);
234 submit_bio(WRITE
, bio
);
238 static int bdev_erase(struct super_block
*sb
, loff_t to
, size_t len
,
241 struct logfs_super
*super
= logfs_super(sb
);
243 BUG_ON(to
& (PAGE_SIZE
- 1));
244 BUG_ON(len
& (PAGE_SIZE
- 1));
246 if (super
->s_flags
& LOGFS_SB_FLAG_RO
)
251 * Object store doesn't care whether erases happen or not.
252 * But for the journal they are required. Otherwise a scan
253 * can find an old commit entry and assume it is the current
254 * one, travelling back in time.
256 do_erase(sb
, to
, to
>> PAGE_SHIFT
, len
>> PAGE_SHIFT
);
262 static void bdev_sync(struct super_block
*sb
)
264 struct logfs_super
*super
= logfs_super(sb
);
266 wait_event(wq
, atomic_read(&super
->s_pending_writes
) == 0);
269 static struct page
*bdev_find_first_sb(struct super_block
*sb
, u64
*ofs
)
271 struct logfs_super
*super
= logfs_super(sb
);
272 struct address_space
*mapping
= super
->s_mapping_inode
->i_mapping
;
273 filler_t
*filler
= bdev_readpage
;
276 return read_cache_page(mapping
, 0, filler
, sb
);
279 static struct page
*bdev_find_last_sb(struct super_block
*sb
, u64
*ofs
)
281 struct logfs_super
*super
= logfs_super(sb
);
282 struct address_space
*mapping
= super
->s_mapping_inode
->i_mapping
;
283 filler_t
*filler
= bdev_readpage
;
284 u64 pos
= (super
->s_bdev
->bd_inode
->i_size
& ~0xfffULL
) - 0x1000;
285 pgoff_t index
= pos
>> PAGE_SHIFT
;
288 return read_cache_page(mapping
, index
, filler
, sb
);
291 static int bdev_write_sb(struct super_block
*sb
, struct page
*page
)
293 struct block_device
*bdev
= logfs_super(sb
)->s_bdev
;
295 /* Nothing special to do for block devices. */
296 return sync_request(page
, bdev
, WRITE
);
299 static void bdev_put_device(struct logfs_super
*s
)
301 blkdev_put(s
->s_bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
304 static int bdev_can_write_buf(struct super_block
*sb
, u64 ofs
)
309 static const struct logfs_device_ops bd_devops
= {
310 .find_first_sb
= bdev_find_first_sb
,
311 .find_last_sb
= bdev_find_last_sb
,
312 .write_sb
= bdev_write_sb
,
313 .readpage
= bdev_readpage
,
314 .writeseg
= bdev_writeseg
,
316 .can_write_buf
= bdev_can_write_buf
,
318 .put_device
= bdev_put_device
,
321 int logfs_get_sb_bdev(struct logfs_super
*p
, struct file_system_type
*type
,
324 struct block_device
*bdev
;
326 bdev
= blkdev_get_by_path(devname
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
329 return PTR_ERR(bdev
);
331 if (MAJOR(bdev
->bd_dev
) == MTD_BLOCK_MAJOR
) {
332 int mtdnr
= MINOR(bdev
->bd_dev
);
333 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
334 return logfs_get_sb_mtd(p
, mtdnr
);
339 p
->s_devops
= &bd_devops
;