dev_bdev.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 void request_complete(struct bio *bio, int err)
  16. {
  17. complete((struct completion *)bio->bi_private);
  18. }
  19. static int sync_request(struct page *page, struct block_device *bdev, int rw)
  20. {
  21. struct bio bio;
  22. struct bio_vec bio_vec;
  23. struct completion complete;
  24. bio_init(&bio);
  25. bio.bi_io_vec = &bio_vec;
  26. bio_vec.bv_page = page;
  27. bio_vec.bv_len = PAGE_SIZE;
  28. bio_vec.bv_offset = 0;
  29. bio.bi_vcnt = 1;
  30. bio.bi_idx = 0;
  31. bio.bi_size = PAGE_SIZE;
  32. bio.bi_bdev = bdev;
  33. bio.bi_sector = page->index * (PAGE_SIZE >> 9);
  34. init_completion(&complete);
  35. bio.bi_private = &complete;
  36. bio.bi_end_io = request_complete;
  37. submit_bio(rw, &bio);
  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. }
  152. static void erase_end_io(struct bio *bio, int err)
  153. {
  154. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  155. struct super_block *sb = bio->bi_private;
  156. struct logfs_super *super = logfs_super(sb);
  157. BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */
  158. BUG_ON(err);
  159. BUG_ON(bio->bi_vcnt == 0);
  160. bio_put(bio);
  161. if (atomic_dec_and_test(&super->s_pending_writes))
  162. wake_up(&wq);
  163. }
  164. static int do_erase(struct super_block *sb, u64 ofs, pgoff_t index,
  165. size_t nr_pages)
  166. {
  167. struct logfs_super *super = logfs_super(sb);
  168. struct bio *bio;
  169. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  170. unsigned int max_pages = queue_max_hw_sectors(q) >> (PAGE_SHIFT - 9);
  171. int i;
  172. if (max_pages > BIO_MAX_PAGES)
  173. max_pages = BIO_MAX_PAGES;
  174. bio = bio_alloc(GFP_NOFS, max_pages);
  175. BUG_ON(!bio);
  176. for (i = 0; i < nr_pages; i++) {
  177. if (i >= max_pages) {
  178. /* Block layer cannot split bios :( */
  179. bio->bi_vcnt = i;
  180. bio->bi_idx = 0;
  181. bio->bi_size = i * PAGE_SIZE;
  182. bio->bi_bdev = super->s_bdev;
  183. bio->bi_sector = ofs >> 9;
  184. bio->bi_private = sb;
  185. bio->bi_end_io = erase_end_io;
  186. atomic_inc(&super->s_pending_writes);
  187. submit_bio(WRITE, bio);
  188. ofs += i * PAGE_SIZE;
  189. index += i;
  190. nr_pages -= i;
  191. i = 0;
  192. bio = bio_alloc(GFP_NOFS, max_pages);
  193. BUG_ON(!bio);
  194. }
  195. bio->bi_io_vec[i].bv_page = super->s_erase_page;
  196. bio->bi_io_vec[i].bv_len = PAGE_SIZE;
  197. bio->bi_io_vec[i].bv_offset = 0;
  198. }
  199. bio->bi_vcnt = nr_pages;
  200. bio->bi_idx = 0;
  201. bio->bi_size = nr_pages * PAGE_SIZE;
  202. bio->bi_bdev = super->s_bdev;
  203. bio->bi_sector = ofs >> 9;
  204. bio->bi_private = sb;
  205. bio->bi_end_io = erase_end_io;
  206. atomic_inc(&super->s_pending_writes);
  207. submit_bio(WRITE, bio);
  208. return 0;
  209. }
  210. static int bdev_erase(struct super_block *sb, loff_t to, size_t len,
  211. int ensure_write)
  212. {
  213. struct logfs_super *super = logfs_super(sb);
  214. BUG_ON(to & (PAGE_SIZE - 1));
  215. BUG_ON(len & (PAGE_SIZE - 1));
  216. if (super->s_flags & LOGFS_SB_FLAG_RO)
  217. return -EROFS;
  218. if (ensure_write) {
  219. /*
  220. * Object store doesn't care whether erases happen or not.
  221. * But for the journal they are required. Otherwise a scan
  222. * can find an old commit entry and assume it is the current
  223. * one, travelling back in time.
  224. */
  225. do_erase(sb, to, to >> PAGE_SHIFT, len >> PAGE_SHIFT);
  226. }
  227. return 0;
  228. }
  229. static void bdev_sync(struct super_block *sb)
  230. {
  231. struct logfs_super *super = logfs_super(sb);
  232. wait_event(wq, atomic_read(&super->s_pending_writes) == 0);
  233. }
  234. static struct page *bdev_find_first_sb(struct super_block *sb, u64 *ofs)
  235. {
  236. struct logfs_super *super = logfs_super(sb);
  237. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  238. filler_t *filler = bdev_readpage;
  239. *ofs = 0;
  240. return read_cache_page(mapping, 0, filler, sb);
  241. }
  242. static struct page *bdev_find_last_sb(struct super_block *sb, u64 *ofs)
  243. {
  244. struct logfs_super *super = logfs_super(sb);
  245. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  246. filler_t *filler = bdev_readpage;
  247. u64 pos = (super->s_bdev->bd_inode->i_size & ~0xfffULL) - 0x1000;
  248. pgoff_t index = pos >> PAGE_SHIFT;
  249. *ofs = pos;
  250. return read_cache_page(mapping, index, filler, sb);
  251. }
  252. static int bdev_write_sb(struct super_block *sb, struct page *page)
  253. {
  254. struct block_device *bdev = logfs_super(sb)->s_bdev;
  255. /* Nothing special to do for block devices. */
  256. return sync_request(page, bdev, WRITE);
  257. }
  258. static void bdev_put_device(struct logfs_super *s)
  259. {
  260. blkdev_put(s->s_bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  261. }
  262. static int bdev_can_write_buf(struct super_block *sb, u64 ofs)
  263. {
  264. return 0;
  265. }
  266. static const struct logfs_device_ops bd_devops = {
  267. .find_first_sb = bdev_find_first_sb,
  268. .find_last_sb = bdev_find_last_sb,
  269. .write_sb = bdev_write_sb,
  270. .readpage = bdev_readpage,
  271. .writeseg = bdev_writeseg,
  272. .erase = bdev_erase,
  273. .can_write_buf = bdev_can_write_buf,
  274. .sync = bdev_sync,
  275. .put_device = bdev_put_device,
  276. };
  277. int logfs_get_sb_bdev(struct logfs_super *p, struct file_system_type *type,
  278. const char *devname)
  279. {
  280. struct block_device *bdev;
  281. bdev = blkdev_get_by_path(devname, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
  282. 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. blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  288. return logfs_get_sb_mtd(p, mtdnr);
  289. }
  290. p->s_bdev = bdev;
  291. p->s_mtd = NULL;
  292. p->s_devops = &bd_devops;
  293. return 0;
  294. }