Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/gfp.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 13 | #include <linux/prefetch.h> |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 14 | |
| 15 | #define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1)) |
| 16 | |
| 17 | static void request_complete(struct bio *bio, int err) |
| 18 | { |
| 19 | complete((struct completion *)bio->bi_private); |
| 20 | } |
| 21 | |
| 22 | static int sync_request(struct page *page, struct block_device *bdev, int rw) |
| 23 | { |
| 24 | struct bio bio; |
| 25 | struct bio_vec bio_vec; |
| 26 | struct completion complete; |
| 27 | |
| 28 | bio_init(&bio); |
Prasad Joshi | cd8bfa9 | 2012-04-02 09:23:04 +0530 | [diff] [blame] | 29 | bio.bi_max_vecs = 1; |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 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; |
| 34 | bio.bi_vcnt = 1; |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 35 | bio.bi_size = PAGE_SIZE; |
| 36 | bio.bi_bdev = bdev; |
| 37 | bio.bi_sector = page->index * (PAGE_SIZE >> 9); |
| 38 | init_completion(&complete); |
| 39 | bio.bi_private = &complete; |
| 40 | bio.bi_end_io = request_complete; |
| 41 | |
| 42 | submit_bio(rw, &bio); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 43 | wait_for_completion(&complete); |
| 44 | return test_bit(BIO_UPTODATE, &bio.bi_flags) ? 0 : -EIO; |
| 45 | } |
| 46 | |
| 47 | static int bdev_readpage(void *_sb, struct page *page) |
| 48 | { |
| 49 | struct super_block *sb = _sb; |
| 50 | struct block_device *bdev = logfs_super(sb)->s_bdev; |
| 51 | int err; |
| 52 | |
| 53 | err = sync_request(page, bdev, READ); |
| 54 | if (err) { |
| 55 | ClearPageUptodate(page); |
| 56 | SetPageError(page); |
| 57 | } else { |
| 58 | SetPageUptodate(page); |
| 59 | ClearPageError(page); |
| 60 | } |
| 61 | unlock_page(page); |
| 62 | return err; |
| 63 | } |
| 64 | |
| 65 | static DECLARE_WAIT_QUEUE_HEAD(wq); |
| 66 | |
| 67 | static void writeseg_end_io(struct bio *bio, int err) |
| 68 | { |
| 69 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 70 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 71 | struct super_block *sb = bio->bi_private; |
| 72 | struct logfs_super *super = logfs_super(sb); |
| 73 | struct page *page; |
| 74 | |
| 75 | BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */ |
| 76 | BUG_ON(err); |
| 77 | BUG_ON(bio->bi_vcnt == 0); |
| 78 | do { |
| 79 | page = bvec->bv_page; |
| 80 | if (--bvec >= bio->bi_io_vec) |
| 81 | prefetchw(&bvec->bv_page->flags); |
| 82 | |
| 83 | end_page_writeback(page); |
Joern Engel | e07bf55 | 2010-03-17 15:29:38 +0100 | [diff] [blame] | 84 | page_cache_release(page); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 85 | } while (bvec >= bio->bi_io_vec); |
| 86 | bio_put(bio); |
| 87 | if (atomic_dec_and_test(&super->s_pending_writes)) |
| 88 | wake_up(&wq); |
| 89 | } |
| 90 | |
| 91 | static int __bdev_writeseg(struct super_block *sb, u64 ofs, pgoff_t index, |
| 92 | size_t nr_pages) |
| 93 | { |
| 94 | struct logfs_super *super = logfs_super(sb); |
| 95 | struct address_space *mapping = super->s_mapping_inode->i_mapping; |
| 96 | struct bio *bio; |
| 97 | struct page *page; |
Prasad Joshi | 9f0bbd8 | 2012-07-23 10:32:11 +0530 | [diff] [blame] | 98 | unsigned int max_pages; |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 99 | int i; |
| 100 | |
Prasad Joshi | 9f0bbd8 | 2012-07-23 10:32:11 +0530 | [diff] [blame] | 101 | max_pages = min(nr_pages, (size_t) bio_get_nr_vecs(super->s_bdev)); |
| 102 | |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 103 | bio = bio_alloc(GFP_NOFS, max_pages); |
Joern Engel | 59fe27c | 2010-03-17 13:47:45 +0100 | [diff] [blame] | 104 | BUG_ON(!bio); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 105 | |
| 106 | for (i = 0; i < nr_pages; i++) { |
| 107 | if (i >= max_pages) { |
| 108 | /* Block layer cannot split bios :( */ |
| 109 | bio->bi_vcnt = i; |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 110 | bio->bi_size = i * PAGE_SIZE; |
| 111 | bio->bi_bdev = super->s_bdev; |
| 112 | bio->bi_sector = ofs >> 9; |
| 113 | bio->bi_private = sb; |
| 114 | bio->bi_end_io = writeseg_end_io; |
| 115 | atomic_inc(&super->s_pending_writes); |
| 116 | submit_bio(WRITE, bio); |
| 117 | |
| 118 | ofs += i * PAGE_SIZE; |
| 119 | index += i; |
| 120 | nr_pages -= i; |
| 121 | i = 0; |
| 122 | |
| 123 | bio = bio_alloc(GFP_NOFS, max_pages); |
| 124 | BUG_ON(!bio); |
| 125 | } |
| 126 | page = find_lock_page(mapping, index + i); |
| 127 | BUG_ON(!page); |
| 128 | bio->bi_io_vec[i].bv_page = page; |
| 129 | bio->bi_io_vec[i].bv_len = PAGE_SIZE; |
| 130 | bio->bi_io_vec[i].bv_offset = 0; |
| 131 | |
| 132 | BUG_ON(PageWriteback(page)); |
| 133 | set_page_writeback(page); |
| 134 | unlock_page(page); |
| 135 | } |
| 136 | bio->bi_vcnt = nr_pages; |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 137 | bio->bi_size = nr_pages * PAGE_SIZE; |
| 138 | bio->bi_bdev = super->s_bdev; |
| 139 | bio->bi_sector = ofs >> 9; |
| 140 | bio->bi_private = sb; |
| 141 | bio->bi_end_io = writeseg_end_io; |
| 142 | atomic_inc(&super->s_pending_writes); |
| 143 | submit_bio(WRITE, bio); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static void bdev_writeseg(struct super_block *sb, u64 ofs, size_t len) |
| 148 | { |
| 149 | struct logfs_super *super = logfs_super(sb); |
| 150 | int head; |
| 151 | |
| 152 | BUG_ON(super->s_flags & LOGFS_SB_FLAG_RO); |
| 153 | |
| 154 | if (len == 0) { |
| 155 | /* This can happen when the object fit perfectly into a |
| 156 | * segment, the segment gets written per sync and subsequently |
| 157 | * closed. |
| 158 | */ |
| 159 | return; |
| 160 | } |
| 161 | head = ofs & (PAGE_SIZE - 1); |
| 162 | if (head) { |
| 163 | ofs -= head; |
| 164 | len += head; |
| 165 | } |
| 166 | len = PAGE_ALIGN(len); |
| 167 | __bdev_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 170 | |
| 171 | static void erase_end_io(struct bio *bio, int err) |
| 172 | { |
| 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); |
| 176 | |
| 177 | BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */ |
| 178 | BUG_ON(err); |
| 179 | BUG_ON(bio->bi_vcnt == 0); |
| 180 | bio_put(bio); |
| 181 | if (atomic_dec_and_test(&super->s_pending_writes)) |
| 182 | wake_up(&wq); |
| 183 | } |
| 184 | |
| 185 | static int do_erase(struct super_block *sb, u64 ofs, pgoff_t index, |
| 186 | size_t nr_pages) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 187 | { |
| 188 | struct logfs_super *super = logfs_super(sb); |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 189 | struct bio *bio; |
Prasad Joshi | 9f0bbd8 | 2012-07-23 10:32:11 +0530 | [diff] [blame] | 190 | unsigned int max_pages; |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 191 | int i; |
| 192 | |
Prasad Joshi | 9f0bbd8 | 2012-07-23 10:32:11 +0530 | [diff] [blame] | 193 | max_pages = min(nr_pages, (size_t) bio_get_nr_vecs(super->s_bdev)); |
| 194 | |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 195 | bio = bio_alloc(GFP_NOFS, max_pages); |
Joern Engel | 59fe27c | 2010-03-17 13:47:45 +0100 | [diff] [blame] | 196 | BUG_ON(!bio); |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 197 | |
| 198 | for (i = 0; i < nr_pages; i++) { |
| 199 | if (i >= max_pages) { |
| 200 | /* Block layer cannot split bios :( */ |
| 201 | bio->bi_vcnt = i; |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 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); |
| 209 | |
| 210 | ofs += i * PAGE_SIZE; |
| 211 | index += i; |
| 212 | nr_pages -= i; |
| 213 | i = 0; |
| 214 | |
| 215 | bio = bio_alloc(GFP_NOFS, max_pages); |
| 216 | BUG_ON(!bio); |
| 217 | } |
| 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; |
| 221 | } |
| 222 | bio->bi_vcnt = nr_pages; |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 223 | bio->bi_size = nr_pages * PAGE_SIZE; |
| 224 | bio->bi_bdev = super->s_bdev; |
| 225 | bio->bi_sector = ofs >> 9; |
| 226 | bio->bi_private = sb; |
| 227 | bio->bi_end_io = erase_end_io; |
| 228 | atomic_inc(&super->s_pending_writes); |
| 229 | submit_bio(WRITE, bio); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static int bdev_erase(struct super_block *sb, loff_t to, size_t len, |
| 234 | int ensure_write) |
| 235 | { |
| 236 | struct logfs_super *super = logfs_super(sb); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 237 | |
| 238 | BUG_ON(to & (PAGE_SIZE - 1)); |
| 239 | BUG_ON(len & (PAGE_SIZE - 1)); |
| 240 | |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 241 | if (super->s_flags & LOGFS_SB_FLAG_RO) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 242 | return -EROFS; |
| 243 | |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 244 | if (ensure_write) { |
| 245 | /* |
| 246 | * Object store doesn't care whether erases happen or not. |
| 247 | * But for the journal they are required. Otherwise a scan |
| 248 | * can find an old commit entry and assume it is the current |
| 249 | * one, travelling back in time. |
| 250 | */ |
| 251 | do_erase(sb, to, to >> PAGE_SHIFT, len >> PAGE_SHIFT); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 252 | } |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 253 | |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static void bdev_sync(struct super_block *sb) |
| 258 | { |
| 259 | struct logfs_super *super = logfs_super(sb); |
| 260 | |
| 261 | wait_event(wq, atomic_read(&super->s_pending_writes) == 0); |
| 262 | } |
| 263 | |
| 264 | static struct page *bdev_find_first_sb(struct super_block *sb, u64 *ofs) |
| 265 | { |
| 266 | struct logfs_super *super = logfs_super(sb); |
| 267 | struct address_space *mapping = super->s_mapping_inode->i_mapping; |
| 268 | filler_t *filler = bdev_readpage; |
| 269 | |
| 270 | *ofs = 0; |
| 271 | return read_cache_page(mapping, 0, filler, sb); |
| 272 | } |
| 273 | |
| 274 | static struct page *bdev_find_last_sb(struct super_block *sb, u64 *ofs) |
| 275 | { |
| 276 | struct logfs_super *super = logfs_super(sb); |
| 277 | struct address_space *mapping = super->s_mapping_inode->i_mapping; |
| 278 | filler_t *filler = bdev_readpage; |
| 279 | u64 pos = (super->s_bdev->bd_inode->i_size & ~0xfffULL) - 0x1000; |
| 280 | pgoff_t index = pos >> PAGE_SHIFT; |
| 281 | |
| 282 | *ofs = pos; |
| 283 | return read_cache_page(mapping, index, filler, sb); |
| 284 | } |
| 285 | |
| 286 | static int bdev_write_sb(struct super_block *sb, struct page *page) |
| 287 | { |
| 288 | struct block_device *bdev = logfs_super(sb)->s_bdev; |
| 289 | |
| 290 | /* Nothing special to do for block devices. */ |
| 291 | return sync_request(page, bdev, WRITE); |
| 292 | } |
| 293 | |
Al Viro | e5a0726 | 2010-07-26 12:06:00 +0400 | [diff] [blame] | 294 | static void bdev_put_device(struct logfs_super *s) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 295 | { |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 296 | blkdev_put(s->s_bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Joern Engel | 6f485b41 | 2010-05-07 19:38:40 +0200 | [diff] [blame] | 299 | static int bdev_can_write_buf(struct super_block *sb, u64 ofs) |
| 300 | { |
| 301 | return 0; |
| 302 | } |
| 303 | |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 304 | static const struct logfs_device_ops bd_devops = { |
| 305 | .find_first_sb = bdev_find_first_sb, |
| 306 | .find_last_sb = bdev_find_last_sb, |
| 307 | .write_sb = bdev_write_sb, |
| 308 | .readpage = bdev_readpage, |
| 309 | .writeseg = bdev_writeseg, |
| 310 | .erase = bdev_erase, |
Joern Engel | 6f485b41 | 2010-05-07 19:38:40 +0200 | [diff] [blame] | 311 | .can_write_buf = bdev_can_write_buf, |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 312 | .sync = bdev_sync, |
| 313 | .put_device = bdev_put_device, |
| 314 | }; |
| 315 | |
Al Viro | 7d945a3 | 2010-07-26 11:53:30 +0400 | [diff] [blame] | 316 | int logfs_get_sb_bdev(struct logfs_super *p, struct file_system_type *type, |
| 317 | const char *devname) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 318 | { |
| 319 | struct block_device *bdev; |
| 320 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 321 | bdev = blkdev_get_by_path(devname, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
| 322 | type); |
Al Viro | 7d945a3 | 2010-07-26 11:53:30 +0400 | [diff] [blame] | 323 | if (IS_ERR(bdev)) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 324 | return PTR_ERR(bdev); |
| 325 | |
| 326 | if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) { |
| 327 | int mtdnr = MINOR(bdev->bd_dev); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 328 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Al Viro | 7d945a3 | 2010-07-26 11:53:30 +0400 | [diff] [blame] | 329 | return logfs_get_sb_mtd(p, mtdnr); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Al Viro | 0d85c79 | 2010-07-26 11:33:39 +0400 | [diff] [blame] | 332 | p->s_bdev = bdev; |
| 333 | p->s_mtd = NULL; |
| 334 | p->s_devops = &bd_devops; |
Al Viro | 7d945a3 | 2010-07-26 11:53:30 +0400 | [diff] [blame] | 335 | return 0; |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 336 | } |