dev_bdev.c 8.5 KB

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