dev_bdev.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include <linux/gfp.h>
  13. #define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
  14. static void request_complete(struct bio *bio, int err)
  15. {
  16. complete((struct completion *)bio->bi_private);
  17. }
  18. static int sync_request(struct page *page, struct block_device *bdev, int rw)
  19. {
  20. struct bio bio;
  21. struct bio_vec bio_vec;
  22. struct completion complete;
  23. bio_init(&bio);
  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;
  28. bio.bi_vcnt = 1;
  29. bio.bi_idx = 0;
  30. bio.bi_size = PAGE_SIZE;
  31. bio.bi_bdev = bdev;
  32. bio.bi_sector = page->index * (PAGE_SIZE >> 9);
  33. init_completion(&complete);
  34. bio.bi_private = &complete;
  35. bio.bi_end_io = request_complete;
  36. submit_bio(rw, &bio);
  37. wait_for_completion(&complete);
  38. return test_bit(BIO_UPTODATE, &bio.bi_flags) ? 0 : -EIO;
  39. }
  40. static int bdev_readpage(void *_sb, struct page *page)
  41. {
  42. struct super_block *sb = _sb;
  43. struct block_device *bdev = logfs_super(sb)->s_bdev;
  44. int err;
  45. err = sync_request(page, bdev, READ);
  46. if (err) {
  47. ClearPageUptodate(page);
  48. SetPageError(page);
  49. } else {
  50. SetPageUptodate(page);
  51. ClearPageError(page);
  52. }
  53. unlock_page(page);
  54. return err;
  55. }
  56. static DECLARE_WAIT_QUEUE_HEAD(wq);
  57. static void writeseg_end_io(struct bio *bio, int err)
  58. {
  59. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  60. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  61. struct super_block *sb = bio->bi_private;
  62. struct logfs_super *super = logfs_super(sb);
  63. struct page *page;
  64. BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */
  65. BUG_ON(err);
  66. BUG_ON(bio->bi_vcnt == 0);
  67. do {
  68. page = bvec->bv_page;
  69. if (--bvec >= bio->bi_io_vec)
  70. prefetchw(&bvec->bv_page->flags);
  71. end_page_writeback(page);
  72. page_cache_release(page);
  73. } while (bvec >= bio->bi_io_vec);
  74. bio_put(bio);
  75. if (atomic_dec_and_test(&super->s_pending_writes))
  76. wake_up(&wq);
  77. }
  78. static int __bdev_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
  79. size_t nr_pages)
  80. {
  81. struct logfs_super *super = logfs_super(sb);
  82. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  83. struct bio *bio;
  84. struct page *page;
  85. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  86. unsigned int max_pages = queue_max_hw_sectors(q) >> (PAGE_SHIFT - 9);
  87. int i;
  88. if (max_pages > BIO_MAX_PAGES)
  89. max_pages = BIO_MAX_PAGES;
  90. bio = bio_alloc(GFP_NOFS, max_pages);
  91. BUG_ON(!bio);
  92. for (i = 0; i < nr_pages; i++) {
  93. if (i >= max_pages) {
  94. /* Block layer cannot split bios :( */
  95. bio->bi_vcnt = i;
  96. bio->bi_idx = 0;
  97. bio->bi_size = i * PAGE_SIZE;
  98. bio->bi_bdev = super->s_bdev;
  99. bio->bi_sector = ofs >> 9;
  100. bio->bi_private = sb;
  101. bio->bi_end_io = writeseg_end_io;
  102. atomic_inc(&super->s_pending_writes);
  103. submit_bio(WRITE, bio);
  104. ofs += i * PAGE_SIZE;
  105. index += i;
  106. nr_pages -= i;
  107. i = 0;
  108. bio = bio_alloc(GFP_NOFS, max_pages);
  109. BUG_ON(!bio);
  110. }
  111. page = find_lock_page(mapping, index + i);
  112. BUG_ON(!page);
  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;
  116. BUG_ON(PageWriteback(page));
  117. set_page_writeback(page);
  118. unlock_page(page);
  119. }
  120. bio->bi_vcnt = nr_pages;
  121. bio->bi_idx = 0;
  122. bio->bi_size = nr_pages * PAGE_SIZE;
  123. bio->bi_bdev = super->s_bdev;
  124. bio->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);
  129. return 0;
  130. }
  131. static void bdev_writeseg(struct super_block *sb, u64 ofs, size_t len)
  132. {
  133. struct logfs_super *super = logfs_super(sb);
  134. int head;
  135. BUG_ON(super->s_flags & LOGFS_SB_FLAG_RO);
  136. if (len == 0) {
  137. /* This can happen when the object fit perfectly into a
  138. * segment, the segment gets written per sync and subsequently
  139. * closed.
  140. */
  141. return;
  142. }
  143. head = ofs & (PAGE_SIZE - 1);
  144. if (head) {
  145. ofs -= head;
  146. len += head;
  147. }
  148. len = PAGE_ALIGN(len);
  149. __bdev_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
  150. }
  151. static void erase_end_io(struct bio *bio, int err)
  152. {
  153. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  154. struct super_block *sb = bio->bi_private;
  155. struct logfs_super *super = logfs_super(sb);
  156. BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */
  157. BUG_ON(err);
  158. BUG_ON(bio->bi_vcnt == 0);
  159. bio_put(bio);
  160. if (atomic_dec_and_test(&super->s_pending_writes))
  161. wake_up(&wq);
  162. }
  163. static int do_erase(struct super_block *sb, u64 ofs, pgoff_t index,
  164. size_t nr_pages)
  165. {
  166. struct logfs_super *super = logfs_super(sb);
  167. struct bio *bio;
  168. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  169. unsigned int max_pages = queue_max_hw_sectors(q) >> (PAGE_SHIFT - 9);
  170. int i;
  171. if (max_pages > BIO_MAX_PAGES)
  172. max_pages = BIO_MAX_PAGES;
  173. bio = bio_alloc(GFP_NOFS, max_pages);
  174. BUG_ON(!bio);
  175. for (i = 0; i < nr_pages; i++) {
  176. if (i >= max_pages) {
  177. /* Block layer cannot split bios :( */
  178. bio->bi_vcnt = i;
  179. bio->bi_idx = 0;
  180. bio->bi_size = i * PAGE_SIZE;
  181. bio->bi_bdev = super->s_bdev;
  182. bio->bi_sector = ofs >> 9;
  183. bio->bi_private = sb;
  184. bio->bi_end_io = erase_end_io;
  185. atomic_inc(&super->s_pending_writes);
  186. submit_bio(WRITE, bio);
  187. ofs += i * PAGE_SIZE;
  188. index += i;
  189. nr_pages -= i;
  190. i = 0;
  191. bio = bio_alloc(GFP_NOFS, max_pages);
  192. BUG_ON(!bio);
  193. }
  194. bio->bi_io_vec[i].bv_page = super->s_erase_page;
  195. bio->bi_io_vec[i].bv_len = PAGE_SIZE;
  196. bio->bi_io_vec[i].bv_offset = 0;
  197. }
  198. bio->bi_vcnt = nr_pages;
  199. bio->bi_idx = 0;
  200. bio->bi_size = nr_pages * PAGE_SIZE;
  201. bio->bi_bdev = super->s_bdev;
  202. bio->bi_sector = ofs >> 9;
  203. bio->bi_private = sb;
  204. bio->bi_end_io = erase_end_io;
  205. atomic_inc(&super->s_pending_writes);
  206. submit_bio(WRITE, bio);
  207. return 0;
  208. }
  209. static int bdev_erase(struct super_block *sb, loff_t to, size_t len,
  210. int ensure_write)
  211. {
  212. struct logfs_super *super = logfs_super(sb);
  213. BUG_ON(to & (PAGE_SIZE - 1));
  214. BUG_ON(len & (PAGE_SIZE - 1));
  215. if (super->s_flags & LOGFS_SB_FLAG_RO)
  216. return -EROFS;
  217. if (ensure_write) {
  218. /*
  219. * Object store doesn't care whether erases happen or not.
  220. * But for the journal they are required. Otherwise a scan
  221. * can find an old commit entry and assume it is the current
  222. * one, travelling back in time.
  223. */
  224. do_erase(sb, to, to >> PAGE_SHIFT, len >> PAGE_SHIFT);
  225. }
  226. return 0;
  227. }
  228. static void bdev_sync(struct super_block *sb)
  229. {
  230. struct logfs_super *super = logfs_super(sb);
  231. wait_event(wq, atomic_read(&super->s_pending_writes) == 0);
  232. }
  233. static struct page *bdev_find_first_sb(struct super_block *sb, u64 *ofs)
  234. {
  235. struct logfs_super *super = logfs_super(sb);
  236. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  237. filler_t *filler = bdev_readpage;
  238. *ofs = 0;
  239. return read_cache_page(mapping, 0, filler, sb);
  240. }
  241. static struct page *bdev_find_last_sb(struct super_block *sb, u64 *ofs)
  242. {
  243. struct logfs_super *super = logfs_super(sb);
  244. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  245. filler_t *filler = bdev_readpage;
  246. u64 pos = (super->s_bdev->bd_inode->i_size & ~0xfffULL) - 0x1000;
  247. pgoff_t index = pos >> PAGE_SHIFT;
  248. *ofs = pos;
  249. return read_cache_page(mapping, index, filler, sb);
  250. }
  251. static int bdev_write_sb(struct super_block *sb, struct page *page)
  252. {
  253. struct block_device *bdev = logfs_super(sb)->s_bdev;
  254. /* Nothing special to do for block devices. */
  255. return sync_request(page, bdev, WRITE);
  256. }
  257. static void bdev_put_device(struct logfs_super *s)
  258. {
  259. blkdev_put(s->s_bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  260. }
  261. static int bdev_can_write_buf(struct super_block *sb, u64 ofs)
  262. {
  263. return 0;
  264. }
  265. static const struct logfs_device_ops bd_devops = {
  266. .find_first_sb = bdev_find_first_sb,
  267. .find_last_sb = bdev_find_last_sb,
  268. .write_sb = bdev_write_sb,
  269. .readpage = bdev_readpage,
  270. .writeseg = bdev_writeseg,
  271. .erase = bdev_erase,
  272. .can_write_buf = bdev_can_write_buf,
  273. .sync = bdev_sync,
  274. .put_device = bdev_put_device,
  275. };
  276. int logfs_get_sb_bdev(struct logfs_super *p, struct file_system_type *type,
  277. const char *devname)
  278. {
  279. struct block_device *bdev;
  280. bdev = blkdev_get_by_path(devname, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
  281. type);
  282. if (IS_ERR(bdev))
  283. return PTR_ERR(bdev);
  284. if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
  285. int mtdnr = MINOR(bdev->bd_dev);
  286. blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  287. return logfs_get_sb_mtd(p, mtdnr);
  288. }
  289. p->s_bdev = bdev;
  290. p->s_mtd = NULL;
  291. p->s_devops = &bd_devops;
  292. return 0;
  293. }