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;
30 bio
.bi_iter
.bi_sector
= page
->index
* (PAGE_SIZE
>> 9);
31 bio
.bi_iter
.bi_size
= PAGE_SIZE
;
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
);
61 struct super_block
*sb
= bio
->bi_private
;
62 struct logfs_super
*super
= logfs_super(sb
);
64 BUG_ON(!uptodate
); /* FIXME: Retry io or write elsewhere */
67 bio_for_each_segment_all(bvec
, bio
, i
) {
68 end_page_writeback(bvec
->bv_page
);
69 page_cache_release(bvec
->bv_page
);
72 if (atomic_dec_and_test(&super
->s_pending_writes
))
76 static int __bdev_writeseg(struct super_block
*sb
, u64 ofs
, pgoff_t index
,
79 struct logfs_super
*super
= logfs_super(sb
);
80 struct address_space
*mapping
= super
->s_mapping_inode
->i_mapping
;
83 unsigned int max_pages
;
86 max_pages
= min(nr_pages
, (size_t) bio_get_nr_vecs(super
->s_bdev
));
88 bio
= bio_alloc(GFP_NOFS
, max_pages
);
91 for (i
= 0; i
< nr_pages
; i
++) {
93 /* Block layer cannot split bios :( */
95 bio
->bi_iter
.bi_size
= i
* PAGE_SIZE
;
96 bio
->bi_bdev
= super
->s_bdev
;
97 bio
->bi_iter
.bi_sector
= ofs
>> 9;
99 bio
->bi_end_io
= writeseg_end_io
;
100 atomic_inc(&super
->s_pending_writes
);
101 submit_bio(WRITE
, bio
);
103 ofs
+= i
* PAGE_SIZE
;
108 bio
= bio_alloc(GFP_NOFS
, max_pages
);
111 page
= find_lock_page(mapping
, index
+ i
);
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;
117 BUG_ON(PageWriteback(page
));
118 set_page_writeback(page
);
121 bio
->bi_vcnt
= nr_pages
;
122 bio
->bi_iter
.bi_size
= nr_pages
* PAGE_SIZE
;
123 bio
->bi_bdev
= super
->s_bdev
;
124 bio
->bi_iter
.bi_sector
= ofs
>> 9;
125 bio
->bi_private
= sb
;
126 bio
->bi_end_io
= writeseg_end_io
;
127 atomic_inc(&super
->s_pending_writes
);
128 submit_bio(WRITE
, bio
);
132 static void bdev_writeseg(struct super_block
*sb
, u64 ofs
, size_t len
)
134 struct logfs_super
*super
= logfs_super(sb
);
137 BUG_ON(super
->s_flags
& LOGFS_SB_FLAG_RO
);
140 /* This can happen when the object fit perfectly into a
141 * segment, the segment gets written per sync and subsequently
146 head
= ofs
& (PAGE_SIZE
- 1);
151 len
= PAGE_ALIGN(len
);
152 __bdev_writeseg(sb
, ofs
, ofs
>> PAGE_SHIFT
, len
>> PAGE_SHIFT
);
156 static void erase_end_io(struct bio
*bio
, int err
)
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
);
162 BUG_ON(!uptodate
); /* FIXME: Retry io or write elsewhere */
164 BUG_ON(bio
->bi_vcnt
== 0);
166 if (atomic_dec_and_test(&super
->s_pending_writes
))
170 static int do_erase(struct super_block
*sb
, u64 ofs
, pgoff_t index
,
173 struct logfs_super
*super
= logfs_super(sb
);
175 unsigned int max_pages
;
178 max_pages
= min(nr_pages
, (size_t) bio_get_nr_vecs(super
->s_bdev
));
180 bio
= bio_alloc(GFP_NOFS
, max_pages
);
183 for (i
= 0; i
< nr_pages
; i
++) {
184 if (i
>= max_pages
) {
185 /* Block layer cannot split bios :( */
187 bio
->bi_iter
.bi_size
= i
* PAGE_SIZE
;
188 bio
->bi_bdev
= super
->s_bdev
;
189 bio
->bi_iter
.bi_sector
= ofs
>> 9;
190 bio
->bi_private
= sb
;
191 bio
->bi_end_io
= erase_end_io
;
192 atomic_inc(&super
->s_pending_writes
);
193 submit_bio(WRITE
, bio
);
195 ofs
+= i
* PAGE_SIZE
;
200 bio
= bio_alloc(GFP_NOFS
, max_pages
);
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;
207 bio
->bi_vcnt
= nr_pages
;
208 bio
->bi_iter
.bi_size
= nr_pages
* PAGE_SIZE
;
209 bio
->bi_bdev
= super
->s_bdev
;
210 bio
->bi_iter
.bi_sector
= ofs
>> 9;
211 bio
->bi_private
= sb
;
212 bio
->bi_end_io
= erase_end_io
;
213 atomic_inc(&super
->s_pending_writes
);
214 submit_bio(WRITE
, bio
);
218 static int bdev_erase(struct super_block
*sb
, loff_t to
, size_t len
,
221 struct logfs_super
*super
= logfs_super(sb
);
223 BUG_ON(to
& (PAGE_SIZE
- 1));
224 BUG_ON(len
& (PAGE_SIZE
- 1));
226 if (super
->s_flags
& LOGFS_SB_FLAG_RO
)
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.
236 do_erase(sb
, to
, to
>> PAGE_SHIFT
, len
>> PAGE_SHIFT
);
242 static void bdev_sync(struct super_block
*sb
)
244 struct logfs_super
*super
= logfs_super(sb
);
246 wait_event(wq
, atomic_read(&super
->s_pending_writes
) == 0);
249 static struct page
*bdev_find_first_sb(struct super_block
*sb
, u64
*ofs
)
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
;
256 return read_cache_page(mapping
, 0, filler
, sb
);
259 static struct page
*bdev_find_last_sb(struct super_block
*sb
, u64
*ofs
)
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
;
268 return read_cache_page(mapping
, index
, filler
, sb
);
271 static int bdev_write_sb(struct super_block
*sb
, struct page
*page
)
273 struct block_device
*bdev
= logfs_super(sb
)->s_bdev
;
275 /* Nothing special to do for block devices. */
276 return sync_request(page
, bdev
, WRITE
);
279 static void bdev_put_device(struct logfs_super
*s
)
281 blkdev_put(s
->s_bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
284 static int bdev_can_write_buf(struct super_block
*sb
, u64 ofs
)
289 static 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
,
296 .can_write_buf
= bdev_can_write_buf
,
298 .put_device
= bdev_put_device
,
301 int logfs_get_sb_bdev(struct logfs_super
*p
, struct file_system_type
*type
,
304 struct block_device
*bdev
;
306 bdev
= blkdev_get_by_path(devname
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
309 return PTR_ERR(bdev
);
311 if (MAJOR(bdev
->bd_dev
) == MTD_BLOCK_MAJOR
) {
312 int mtdnr
= MINOR(bdev
->bd_dev
);
313 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
314 return logfs_get_sb_mtd(p
, mtdnr
);
319 p
->s_devops
= &bd_devops
;