dev_bdev.c 7.6 KB

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