dev_bdev.c 7.8 KB

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