dev_bdev.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
  13. static void request_complete(struct bio *bio, int err)
  14. {
  15. complete((struct completion *)bio->bi_private);
  16. }
  17. static int sync_request(struct page *page, struct block_device *bdev, int rw)
  18. {
  19. struct bio bio;
  20. struct bio_vec bio_vec;
  21. struct completion complete;
  22. bio_init(&bio);
  23. bio.bi_io_vec = &bio_vec;
  24. bio_vec.bv_page = page;
  25. bio_vec.bv_len = PAGE_SIZE;
  26. bio_vec.bv_offset = 0;
  27. bio.bi_vcnt = 1;
  28. bio.bi_idx = 0;
  29. bio.bi_size = PAGE_SIZE;
  30. bio.bi_bdev = bdev;
  31. bio.bi_sector = page->index * (PAGE_SIZE >> 9);
  32. init_completion(&complete);
  33. bio.bi_private = &complete;
  34. bio.bi_end_io = request_complete;
  35. submit_bio(rw, &bio);
  36. generic_unplug_device(bdev_get_queue(bdev));
  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. } while (bvec >= bio->bi_io_vec);
  73. bio_put(bio);
  74. if (atomic_dec_and_test(&super->s_pending_writes))
  75. wake_up(&wq);
  76. }
  77. static int __bdev_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
  78. size_t nr_pages)
  79. {
  80. struct logfs_super *super = logfs_super(sb);
  81. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  82. struct bio *bio;
  83. struct page *page;
  84. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  85. unsigned int max_pages = queue_max_hw_sectors(q) >> (PAGE_SHIFT - 9);
  86. int i;
  87. if (max_pages > BIO_MAX_PAGES)
  88. max_pages = BIO_MAX_PAGES;
  89. bio = bio_alloc(GFP_NOFS, max_pages);
  90. BUG_ON(!bio);
  91. for (i = 0; i < nr_pages; i++) {
  92. if (i >= max_pages) {
  93. /* Block layer cannot split bios :( */
  94. bio->bi_vcnt = i;
  95. bio->bi_idx = 0;
  96. bio->bi_size = i * PAGE_SIZE;
  97. bio->bi_bdev = super->s_bdev;
  98. bio->bi_sector = ofs >> 9;
  99. bio->bi_private = sb;
  100. bio->bi_end_io = writeseg_end_io;
  101. atomic_inc(&super->s_pending_writes);
  102. submit_bio(WRITE, bio);
  103. ofs += i * PAGE_SIZE;
  104. index += i;
  105. nr_pages -= i;
  106. i = 0;
  107. bio = bio_alloc(GFP_NOFS, max_pages);
  108. BUG_ON(!bio);
  109. }
  110. page = find_lock_page(mapping, index + i);
  111. BUG_ON(!page);
  112. bio->bi_io_vec[i].bv_page = page;
  113. bio->bi_io_vec[i].bv_len = PAGE_SIZE;
  114. bio->bi_io_vec[i].bv_offset = 0;
  115. BUG_ON(PageWriteback(page));
  116. set_page_writeback(page);
  117. unlock_page(page);
  118. }
  119. bio->bi_vcnt = nr_pages;
  120. bio->bi_idx = 0;
  121. bio->bi_size = nr_pages * PAGE_SIZE;
  122. bio->bi_bdev = super->s_bdev;
  123. bio->bi_sector = ofs >> 9;
  124. bio->bi_private = sb;
  125. bio->bi_end_io = writeseg_end_io;
  126. atomic_inc(&super->s_pending_writes);
  127. submit_bio(WRITE, bio);
  128. return 0;
  129. }
  130. static void bdev_writeseg(struct super_block *sb, u64 ofs, size_t len)
  131. {
  132. struct logfs_super *super = logfs_super(sb);
  133. int head;
  134. BUG_ON(super->s_flags & LOGFS_SB_FLAG_RO);
  135. if (len == 0) {
  136. /* This can happen when the object fit perfectly into a
  137. * segment, the segment gets written per sync and subsequently
  138. * closed.
  139. */
  140. return;
  141. }
  142. head = ofs & (PAGE_SIZE - 1);
  143. if (head) {
  144. ofs -= head;
  145. len += head;
  146. }
  147. len = PAGE_ALIGN(len);
  148. __bdev_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
  149. generic_unplug_device(bdev_get_queue(logfs_super(sb)->s_bdev));
  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 super_block *sb)
  258. {
  259. close_bdev_exclusive(logfs_super(sb)->s_bdev, FMODE_READ|FMODE_WRITE);
  260. }
  261. static const struct logfs_device_ops bd_devops = {
  262. .find_first_sb = bdev_find_first_sb,
  263. .find_last_sb = bdev_find_last_sb,
  264. .write_sb = bdev_write_sb,
  265. .readpage = bdev_readpage,
  266. .writeseg = bdev_writeseg,
  267. .erase = bdev_erase,
  268. .sync = bdev_sync,
  269. .put_device = bdev_put_device,
  270. };
  271. int logfs_get_sb_bdev(struct file_system_type *type, int flags,
  272. const char *devname, struct vfsmount *mnt)
  273. {
  274. struct block_device *bdev;
  275. bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, type);
  276. if (IS_ERR(bdev))
  277. return PTR_ERR(bdev);
  278. if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
  279. int mtdnr = MINOR(bdev->bd_dev);
  280. close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
  281. return logfs_get_sb_mtd(type, flags, mtdnr, mnt);
  282. }
  283. return logfs_get_sb_device(type, flags, NULL, bdev, &bd_devops, mnt);
  284. }