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>
13 #define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
15 static void request_complete(struct bio
*bio
, int err
)
17 complete((struct completion
*)bio
->bi_private
);
20 static int sync_request(struct page
*page
, struct block_device
*bdev
, int rw
)
23 struct bio_vec bio_vec
;
24 struct completion complete
;
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;
33 bio
.bi_size
= PAGE_SIZE
;
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
;
41 generic_unplug_device(bdev_get_queue(bdev
));
42 wait_for_completion(&complete
);
43 return test_bit(BIO_UPTODATE
, &bio
.bi_flags
) ? 0 : -EIO
;
46 static int bdev_readpage(void *_sb
, struct page
*page
)
48 struct super_block
*sb
= _sb
;
49 struct block_device
*bdev
= logfs_super(sb
)->s_bdev
;
52 err
= sync_request(page
, bdev
, READ
);
54 ClearPageUptodate(page
);
57 SetPageUptodate(page
);
64 static DECLARE_WAIT_QUEUE_HEAD(wq
);
66 static void writeseg_end_io(struct bio
*bio
, int err
)
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
);
74 BUG_ON(!uptodate
); /* FIXME: Retry io or write elsewhere */
76 BUG_ON(bio
->bi_vcnt
== 0);
79 if (--bvec
>= bio
->bi_io_vec
)
80 prefetchw(&bvec
->bv_page
->flags
);
82 end_page_writeback(page
);
83 } while (bvec
>= bio
->bi_io_vec
);
85 if (atomic_dec_and_test(&super
->s_pending_writes
))
89 static int __bdev_writeseg(struct super_block
*sb
, u64 ofs
, pgoff_t index
,
92 struct logfs_super
*super
= logfs_super(sb
);
93 struct address_space
*mapping
= super
->s_mapping_inode
->i_mapping
;
96 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
97 unsigned int max_pages
= queue_max_hw_sectors(q
) >> (PAGE_SHIFT
- 9);
100 bio
= bio_alloc(GFP_NOFS
, max_pages
);
101 BUG_ON(!bio
); /* FIXME: handle this */
103 for (i
= 0; i
< nr_pages
; i
++) {
104 if (i
>= max_pages
) {
105 /* Block layer cannot split bios :( */
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
);
116 ofs
+= i
* PAGE_SIZE
;
121 bio
= bio_alloc(GFP_NOFS
, max_pages
);
124 page
= find_lock_page(mapping
, index
+ i
);
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;
130 BUG_ON(PageWriteback(page
));
131 set_page_writeback(page
);
134 bio
->bi_vcnt
= nr_pages
;
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
);
146 static void bdev_writeseg(struct super_block
*sb
, u64 ofs
, size_t len
)
148 struct logfs_super
*super
= logfs_super(sb
);
151 BUG_ON(super
->s_flags
& LOGFS_SB_FLAG_RO
);
154 /* This can happen when the object fit perfectly into a
155 * segment, the segment gets written per sync and subsequently
160 head
= ofs
& (PAGE_SIZE
- 1);
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
));
171 static void erase_end_io(struct bio
*bio
, int err
)
173 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
174 struct super_block
*sb
= bio
->bi_private
;
175 struct logfs_super
*super
= logfs_super(sb
);
177 BUG_ON(!uptodate
); /* FIXME: Retry io or write elsewhere */
179 BUG_ON(bio
->bi_vcnt
== 0);
181 if (atomic_dec_and_test(&super
->s_pending_writes
))
185 static int do_erase(struct super_block
*sb
, u64 ofs
, pgoff_t index
,
188 struct logfs_super
*super
= logfs_super(sb
);
190 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
191 unsigned int max_pages
= queue_max_hw_sectors(q
) >> (PAGE_SHIFT
- 9);
194 bio
= bio_alloc(GFP_NOFS
, max_pages
);
195 BUG_ON(!bio
); /* FIXME: handle this */
197 for (i
= 0; i
< nr_pages
; i
++) {
198 if (i
>= max_pages
) {
199 /* Block layer cannot split bios :( */
202 bio
->bi_size
= i
* PAGE_SIZE
;
203 bio
->bi_bdev
= super
->s_bdev
;
204 bio
->bi_sector
= ofs
>> 9;
205 bio
->bi_private
= sb
;
206 bio
->bi_end_io
= erase_end_io
;
207 atomic_inc(&super
->s_pending_writes
);
208 submit_bio(WRITE
, bio
);
210 ofs
+= i
* PAGE_SIZE
;
215 bio
= bio_alloc(GFP_NOFS
, max_pages
);
218 bio
->bi_io_vec
[i
].bv_page
= super
->s_erase_page
;
219 bio
->bi_io_vec
[i
].bv_len
= PAGE_SIZE
;
220 bio
->bi_io_vec
[i
].bv_offset
= 0;
222 bio
->bi_vcnt
= nr_pages
;
224 bio
->bi_size
= nr_pages
* PAGE_SIZE
;
225 bio
->bi_bdev
= super
->s_bdev
;
226 bio
->bi_sector
= ofs
>> 9;
227 bio
->bi_private
= sb
;
228 bio
->bi_end_io
= erase_end_io
;
229 atomic_inc(&super
->s_pending_writes
);
230 submit_bio(WRITE
, bio
);
234 static int bdev_erase(struct super_block
*sb
, loff_t to
, size_t len
,
237 struct logfs_super
*super
= logfs_super(sb
);
239 BUG_ON(to
& (PAGE_SIZE
- 1));
240 BUG_ON(len
& (PAGE_SIZE
- 1));
242 if (super
->s_flags
& LOGFS_SB_FLAG_RO
)
247 * Object store doesn't care whether erases happen or not.
248 * But for the journal they are required. Otherwise a scan
249 * can find an old commit entry and assume it is the current
250 * one, travelling back in time.
252 do_erase(sb
, to
, to
>> PAGE_SHIFT
, len
>> PAGE_SHIFT
);
258 static void bdev_sync(struct super_block
*sb
)
260 struct logfs_super
*super
= logfs_super(sb
);
262 wait_event(wq
, atomic_read(&super
->s_pending_writes
) == 0);
265 static struct page
*bdev_find_first_sb(struct super_block
*sb
, u64
*ofs
)
267 struct logfs_super
*super
= logfs_super(sb
);
268 struct address_space
*mapping
= super
->s_mapping_inode
->i_mapping
;
269 filler_t
*filler
= bdev_readpage
;
272 return read_cache_page(mapping
, 0, filler
, sb
);
275 static struct page
*bdev_find_last_sb(struct super_block
*sb
, u64
*ofs
)
277 struct logfs_super
*super
= logfs_super(sb
);
278 struct address_space
*mapping
= super
->s_mapping_inode
->i_mapping
;
279 filler_t
*filler
= bdev_readpage
;
280 u64 pos
= (super
->s_bdev
->bd_inode
->i_size
& ~0xfffULL
) - 0x1000;
281 pgoff_t index
= pos
>> PAGE_SHIFT
;
284 return read_cache_page(mapping
, index
, filler
, sb
);
287 static int bdev_write_sb(struct super_block
*sb
, struct page
*page
)
289 struct block_device
*bdev
= logfs_super(sb
)->s_bdev
;
291 /* Nothing special to do for block devices. */
292 return sync_request(page
, bdev
, WRITE
);
295 static void bdev_put_device(struct super_block
*sb
)
297 close_bdev_exclusive(logfs_super(sb
)->s_bdev
, FMODE_READ
|FMODE_WRITE
);
300 static const struct logfs_device_ops bd_devops
= {
301 .find_first_sb
= bdev_find_first_sb
,
302 .find_last_sb
= bdev_find_last_sb
,
303 .write_sb
= bdev_write_sb
,
304 .readpage
= bdev_readpage
,
305 .writeseg
= bdev_writeseg
,
308 .put_device
= bdev_put_device
,
311 int logfs_get_sb_bdev(struct file_system_type
*type
, int flags
,
312 const char *devname
, struct vfsmount
*mnt
)
314 struct block_device
*bdev
;
316 bdev
= open_bdev_exclusive(devname
, FMODE_READ
|FMODE_WRITE
, type
);
318 return PTR_ERR(bdev
);
320 if (MAJOR(bdev
->bd_dev
) == MTD_BLOCK_MAJOR
) {
321 int mtdnr
= MINOR(bdev
->bd_dev
);
322 close_bdev_exclusive(bdev
, FMODE_READ
|FMODE_WRITE
);
323 return logfs_get_sb_mtd(type
, flags
, mtdnr
, mnt
);
326 return logfs_get_sb_device(type
, flags
, NULL
, bdev
, &bd_devops
, mnt
);