brd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * Ram backed block device driver.
  3. *
  4. * Copyright (C) 2007 Nick Piggin
  5. * Copyright (C) 2007 Novell Inc.
  6. *
  7. * Parts derived from drivers/block/rd.c, and drivers/block/loop.c, copyright
  8. * of their respective owners.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/major.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/bio.h>
  16. #include <linux/highmem.h>
  17. #include <linux/mutex.h>
  18. #include <linux/radix-tree.h>
  19. #include <linux/fs.h>
  20. #include <linux/slab.h>
  21. #ifdef CONFIG_BLK_DEV_RAM_DAX
  22. #include <linux/pfn_t.h>
  23. #endif
  24. #include <asm/uaccess.h>
  25. #define SECTOR_SHIFT 9
  26. #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
  27. #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
  28. /*
  29. * Each block ramdisk device has a radix_tree brd_pages of pages that stores
  30. * the pages containing the block device's contents. A brd page's ->index is
  31. * its offset in PAGE_SIZE units. This is similar to, but in no way connected
  32. * with, the kernel's pagecache or buffer cache (which sit above our block
  33. * device).
  34. */
  35. struct brd_device {
  36. int brd_number;
  37. struct request_queue *brd_queue;
  38. struct gendisk *brd_disk;
  39. struct list_head brd_list;
  40. /*
  41. * Backing store of pages and lock to protect it. This is the contents
  42. * of the block device.
  43. */
  44. spinlock_t brd_lock;
  45. struct radix_tree_root brd_pages;
  46. };
  47. /*
  48. * Look up and return a brd's page for a given sector.
  49. */
  50. static DEFINE_MUTEX(brd_mutex);
  51. static struct page *brd_lookup_page(struct brd_device *brd, sector_t sector)
  52. {
  53. pgoff_t idx;
  54. struct page *page;
  55. /*
  56. * The page lifetime is protected by the fact that we have opened the
  57. * device node -- brd pages will never be deleted under us, so we
  58. * don't need any further locking or refcounting.
  59. *
  60. * This is strictly true for the radix-tree nodes as well (ie. we
  61. * don't actually need the rcu_read_lock()), however that is not a
  62. * documented feature of the radix-tree API so it is better to be
  63. * safe here (we don't have total exclusion from radix tree updates
  64. * here, only deletes).
  65. */
  66. rcu_read_lock();
  67. idx = sector >> PAGE_SECTORS_SHIFT; /* sector to page index */
  68. page = radix_tree_lookup(&brd->brd_pages, idx);
  69. rcu_read_unlock();
  70. BUG_ON(page && page->index != idx);
  71. return page;
  72. }
  73. /*
  74. * Look up and return a brd's page for a given sector.
  75. * If one does not exist, allocate an empty page, and insert that. Then
  76. * return it.
  77. */
  78. static struct page *brd_insert_page(struct brd_device *brd, sector_t sector)
  79. {
  80. pgoff_t idx;
  81. struct page *page;
  82. gfp_t gfp_flags;
  83. page = brd_lookup_page(brd, sector);
  84. if (page)
  85. return page;
  86. /*
  87. * Must use NOIO because we don't want to recurse back into the
  88. * block or filesystem layers from page reclaim.
  89. *
  90. * Cannot support DAX and highmem, because our ->direct_access
  91. * routine for DAX must return memory that is always addressable.
  92. * If DAX was reworked to use pfns and kmap throughout, this
  93. * restriction might be able to be lifted.
  94. */
  95. gfp_flags = GFP_NOIO | __GFP_ZERO;
  96. #ifndef CONFIG_BLK_DEV_RAM_DAX
  97. gfp_flags |= __GFP_HIGHMEM;
  98. #endif
  99. page = alloc_page(gfp_flags);
  100. if (!page)
  101. return NULL;
  102. if (radix_tree_preload(GFP_NOIO)) {
  103. __free_page(page);
  104. return NULL;
  105. }
  106. spin_lock(&brd->brd_lock);
  107. idx = sector >> PAGE_SECTORS_SHIFT;
  108. page->index = idx;
  109. if (radix_tree_insert(&brd->brd_pages, idx, page)) {
  110. __free_page(page);
  111. page = radix_tree_lookup(&brd->brd_pages, idx);
  112. BUG_ON(!page);
  113. BUG_ON(page->index != idx);
  114. }
  115. spin_unlock(&brd->brd_lock);
  116. radix_tree_preload_end();
  117. return page;
  118. }
  119. static void brd_free_page(struct brd_device *brd, sector_t sector)
  120. {
  121. struct page *page;
  122. pgoff_t idx;
  123. spin_lock(&brd->brd_lock);
  124. idx = sector >> PAGE_SECTORS_SHIFT;
  125. page = radix_tree_delete(&brd->brd_pages, idx);
  126. spin_unlock(&brd->brd_lock);
  127. if (page)
  128. __free_page(page);
  129. }
  130. static void brd_zero_page(struct brd_device *brd, sector_t sector)
  131. {
  132. struct page *page;
  133. page = brd_lookup_page(brd, sector);
  134. if (page)
  135. clear_highpage(page);
  136. }
  137. /*
  138. * Free all backing store pages and radix tree. This must only be called when
  139. * there are no other users of the device.
  140. */
  141. #define FREE_BATCH 16
  142. static void brd_free_pages(struct brd_device *brd)
  143. {
  144. unsigned long pos = 0;
  145. struct page *pages[FREE_BATCH];
  146. int nr_pages;
  147. do {
  148. int i;
  149. nr_pages = radix_tree_gang_lookup(&brd->brd_pages,
  150. (void **)pages, pos, FREE_BATCH);
  151. for (i = 0; i < nr_pages; i++) {
  152. void *ret;
  153. BUG_ON(pages[i]->index < pos);
  154. pos = pages[i]->index;
  155. ret = radix_tree_delete(&brd->brd_pages, pos);
  156. BUG_ON(!ret || ret != pages[i]);
  157. __free_page(pages[i]);
  158. }
  159. pos++;
  160. /*
  161. * This assumes radix_tree_gang_lookup always returns as
  162. * many pages as possible. If the radix-tree code changes,
  163. * so will this have to.
  164. */
  165. } while (nr_pages == FREE_BATCH);
  166. }
  167. /*
  168. * copy_to_brd_setup must be called before copy_to_brd. It may sleep.
  169. */
  170. static int copy_to_brd_setup(struct brd_device *brd, sector_t sector, size_t n)
  171. {
  172. unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
  173. size_t copy;
  174. copy = min_t(size_t, n, PAGE_SIZE - offset);
  175. if (!brd_insert_page(brd, sector))
  176. return -ENOSPC;
  177. if (copy < n) {
  178. sector += copy >> SECTOR_SHIFT;
  179. if (!brd_insert_page(brd, sector))
  180. return -ENOSPC;
  181. }
  182. return 0;
  183. }
  184. static void discard_from_brd(struct brd_device *brd,
  185. sector_t sector, size_t n)
  186. {
  187. while (n >= PAGE_SIZE) {
  188. /*
  189. * Don't want to actually discard pages here because
  190. * re-allocating the pages can result in writeback
  191. * deadlocks under heavy load.
  192. */
  193. if (0)
  194. brd_free_page(brd, sector);
  195. else
  196. brd_zero_page(brd, sector);
  197. sector += PAGE_SIZE >> SECTOR_SHIFT;
  198. n -= PAGE_SIZE;
  199. }
  200. }
  201. /*
  202. * Copy n bytes from src to the brd starting at sector. Does not sleep.
  203. */
  204. static void copy_to_brd(struct brd_device *brd, const void *src,
  205. sector_t sector, size_t n)
  206. {
  207. struct page *page;
  208. void *dst;
  209. unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
  210. size_t copy;
  211. copy = min_t(size_t, n, PAGE_SIZE - offset);
  212. page = brd_lookup_page(brd, sector);
  213. BUG_ON(!page);
  214. dst = kmap_atomic(page);
  215. memcpy(dst + offset, src, copy);
  216. kunmap_atomic(dst);
  217. if (copy < n) {
  218. src += copy;
  219. sector += copy >> SECTOR_SHIFT;
  220. copy = n - copy;
  221. page = brd_lookup_page(brd, sector);
  222. BUG_ON(!page);
  223. dst = kmap_atomic(page);
  224. memcpy(dst, src, copy);
  225. kunmap_atomic(dst);
  226. }
  227. }
  228. /*
  229. * Copy n bytes to dst from the brd starting at sector. Does not sleep.
  230. */
  231. static void copy_from_brd(void *dst, struct brd_device *brd,
  232. sector_t sector, size_t n)
  233. {
  234. struct page *page;
  235. void *src;
  236. unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
  237. size_t copy;
  238. copy = min_t(size_t, n, PAGE_SIZE - offset);
  239. page = brd_lookup_page(brd, sector);
  240. if (page) {
  241. src = kmap_atomic(page);
  242. memcpy(dst, src + offset, copy);
  243. kunmap_atomic(src);
  244. } else
  245. memset(dst, 0, copy);
  246. if (copy < n) {
  247. dst += copy;
  248. sector += copy >> SECTOR_SHIFT;
  249. copy = n - copy;
  250. page = brd_lookup_page(brd, sector);
  251. if (page) {
  252. src = kmap_atomic(page);
  253. memcpy(dst, src, copy);
  254. kunmap_atomic(src);
  255. } else
  256. memset(dst, 0, copy);
  257. }
  258. }
  259. /*
  260. * Process a single bvec of a bio.
  261. */
  262. static int brd_do_bvec(struct brd_device *brd, struct page *page,
  263. unsigned int len, unsigned int off, bool is_write,
  264. sector_t sector)
  265. {
  266. void *mem;
  267. int err = 0;
  268. if (is_write) {
  269. err = copy_to_brd_setup(brd, sector, len);
  270. if (err)
  271. goto out;
  272. }
  273. mem = kmap_atomic(page);
  274. if (!is_write) {
  275. copy_from_brd(mem + off, brd, sector, len);
  276. flush_dcache_page(page);
  277. } else {
  278. flush_dcache_page(page);
  279. copy_to_brd(brd, mem + off, sector, len);
  280. }
  281. kunmap_atomic(mem);
  282. out:
  283. return err;
  284. }
  285. static blk_qc_t brd_make_request(struct request_queue *q, struct bio *bio)
  286. {
  287. struct block_device *bdev = bio->bi_bdev;
  288. struct brd_device *brd = bdev->bd_disk->private_data;
  289. struct bio_vec bvec;
  290. sector_t sector;
  291. struct bvec_iter iter;
  292. sector = bio->bi_iter.bi_sector;
  293. if (bio_end_sector(bio) > get_capacity(bdev->bd_disk))
  294. goto io_error;
  295. if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
  296. if (sector & ((PAGE_SIZE >> SECTOR_SHIFT) - 1) ||
  297. bio->bi_iter.bi_size & ~PAGE_MASK)
  298. goto io_error;
  299. discard_from_brd(brd, sector, bio->bi_iter.bi_size);
  300. goto out;
  301. }
  302. bio_for_each_segment(bvec, bio, iter) {
  303. unsigned int len = bvec.bv_len;
  304. int err;
  305. err = brd_do_bvec(brd, bvec.bv_page, len, bvec.bv_offset,
  306. op_is_write(bio_op(bio)), sector);
  307. if (err)
  308. goto io_error;
  309. sector += len >> SECTOR_SHIFT;
  310. }
  311. out:
  312. bio_endio(bio);
  313. return BLK_QC_T_NONE;
  314. io_error:
  315. bio_io_error(bio);
  316. return BLK_QC_T_NONE;
  317. }
  318. static int brd_rw_page(struct block_device *bdev, sector_t sector,
  319. struct page *page, bool is_write)
  320. {
  321. struct brd_device *brd = bdev->bd_disk->private_data;
  322. int err = brd_do_bvec(brd, page, PAGE_SIZE, 0, is_write, sector);
  323. page_endio(page, is_write, err);
  324. return err;
  325. }
  326. #ifdef CONFIG_BLK_DEV_RAM_DAX
  327. static long brd_direct_access(struct block_device *bdev, sector_t sector,
  328. void **kaddr, pfn_t *pfn, long size)
  329. {
  330. struct brd_device *brd = bdev->bd_disk->private_data;
  331. struct page *page;
  332. if (!brd)
  333. return -ENODEV;
  334. page = brd_insert_page(brd, sector);
  335. if (!page)
  336. return -ENOSPC;
  337. *kaddr = page_address(page);
  338. *pfn = page_to_pfn_t(page);
  339. return PAGE_SIZE;
  340. }
  341. #else
  342. #define brd_direct_access NULL
  343. #endif
  344. static int brd_ioctl(struct block_device *bdev, fmode_t mode,
  345. unsigned int cmd, unsigned long arg)
  346. {
  347. int error;
  348. struct brd_device *brd = bdev->bd_disk->private_data;
  349. if (cmd != BLKFLSBUF)
  350. return -ENOTTY;
  351. /*
  352. * ram device BLKFLSBUF has special semantics, we want to actually
  353. * release and destroy the ramdisk data.
  354. */
  355. mutex_lock(&brd_mutex);
  356. mutex_lock(&bdev->bd_mutex);
  357. error = -EBUSY;
  358. if (bdev->bd_openers <= 1) {
  359. /*
  360. * Kill the cache first, so it isn't written back to the
  361. * device.
  362. *
  363. * Another thread might instantiate more buffercache here,
  364. * but there is not much we can do to close that race.
  365. */
  366. kill_bdev(bdev);
  367. brd_free_pages(brd);
  368. error = 0;
  369. }
  370. mutex_unlock(&bdev->bd_mutex);
  371. mutex_unlock(&brd_mutex);
  372. return error;
  373. }
  374. static const struct block_device_operations brd_fops = {
  375. .owner = THIS_MODULE,
  376. .rw_page = brd_rw_page,
  377. .ioctl = brd_ioctl,
  378. .direct_access = brd_direct_access,
  379. };
  380. /*
  381. * And now the modules code and kernel interface.
  382. */
  383. static int rd_nr = CONFIG_BLK_DEV_RAM_COUNT;
  384. module_param(rd_nr, int, S_IRUGO);
  385. MODULE_PARM_DESC(rd_nr, "Maximum number of brd devices");
  386. int rd_size = CONFIG_BLK_DEV_RAM_SIZE;
  387. module_param(rd_size, int, S_IRUGO);
  388. MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
  389. static int max_part = 1;
  390. module_param(max_part, int, S_IRUGO);
  391. MODULE_PARM_DESC(max_part, "Num Minors to reserve between devices");
  392. MODULE_LICENSE("GPL");
  393. MODULE_ALIAS_BLOCKDEV_MAJOR(RAMDISK_MAJOR);
  394. MODULE_ALIAS("rd");
  395. #ifndef MODULE
  396. /* Legacy boot options - nonmodular */
  397. static int __init ramdisk_size(char *str)
  398. {
  399. rd_size = simple_strtol(str, NULL, 0);
  400. return 1;
  401. }
  402. __setup("ramdisk_size=", ramdisk_size);
  403. #endif
  404. /*
  405. * The device scheme is derived from loop.c. Keep them in synch where possible
  406. * (should share code eventually).
  407. */
  408. static LIST_HEAD(brd_devices);
  409. static DEFINE_MUTEX(brd_devices_mutex);
  410. static struct brd_device *brd_alloc(int i)
  411. {
  412. struct brd_device *brd;
  413. struct gendisk *disk;
  414. brd = kzalloc(sizeof(*brd), GFP_KERNEL);
  415. if (!brd)
  416. goto out;
  417. brd->brd_number = i;
  418. spin_lock_init(&brd->brd_lock);
  419. INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC);
  420. brd->brd_queue = blk_alloc_queue(GFP_KERNEL);
  421. if (!brd->brd_queue)
  422. goto out_free_dev;
  423. blk_queue_make_request(brd->brd_queue, brd_make_request);
  424. blk_queue_max_hw_sectors(brd->brd_queue, 1024);
  425. blk_queue_bounce_limit(brd->brd_queue, BLK_BOUNCE_ANY);
  426. /* This is so fdisk will align partitions on 4k, because of
  427. * direct_access API needing 4k alignment, returning a PFN
  428. * (This is only a problem on very small devices <= 4M,
  429. * otherwise fdisk will align on 1M. Regardless this call
  430. * is harmless)
  431. */
  432. blk_queue_physical_block_size(brd->brd_queue, PAGE_SIZE);
  433. brd->brd_queue->limits.discard_granularity = PAGE_SIZE;
  434. blk_queue_max_discard_sectors(brd->brd_queue, UINT_MAX);
  435. brd->brd_queue->limits.discard_zeroes_data = 1;
  436. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, brd->brd_queue);
  437. #ifdef CONFIG_BLK_DEV_RAM_DAX
  438. queue_flag_set_unlocked(QUEUE_FLAG_DAX, brd->brd_queue);
  439. #endif
  440. disk = brd->brd_disk = alloc_disk(max_part);
  441. if (!disk)
  442. goto out_free_queue;
  443. disk->major = RAMDISK_MAJOR;
  444. disk->first_minor = i * max_part;
  445. disk->fops = &brd_fops;
  446. disk->private_data = brd;
  447. disk->queue = brd->brd_queue;
  448. disk->flags = GENHD_FL_EXT_DEVT;
  449. sprintf(disk->disk_name, "ram%d", i);
  450. set_capacity(disk, rd_size * 2);
  451. return brd;
  452. out_free_queue:
  453. blk_cleanup_queue(brd->brd_queue);
  454. out_free_dev:
  455. kfree(brd);
  456. out:
  457. return NULL;
  458. }
  459. static void brd_free(struct brd_device *brd)
  460. {
  461. put_disk(brd->brd_disk);
  462. blk_cleanup_queue(brd->brd_queue);
  463. brd_free_pages(brd);
  464. kfree(brd);
  465. }
  466. static struct brd_device *brd_init_one(int i, bool *new)
  467. {
  468. struct brd_device *brd;
  469. *new = false;
  470. list_for_each_entry(brd, &brd_devices, brd_list) {
  471. if (brd->brd_number == i)
  472. goto out;
  473. }
  474. brd = brd_alloc(i);
  475. if (brd) {
  476. add_disk(brd->brd_disk);
  477. list_add_tail(&brd->brd_list, &brd_devices);
  478. }
  479. *new = true;
  480. out:
  481. return brd;
  482. }
  483. static void brd_del_one(struct brd_device *brd)
  484. {
  485. list_del(&brd->brd_list);
  486. del_gendisk(brd->brd_disk);
  487. brd_free(brd);
  488. }
  489. static struct kobject *brd_probe(dev_t dev, int *part, void *data)
  490. {
  491. struct brd_device *brd;
  492. struct kobject *kobj;
  493. bool new;
  494. mutex_lock(&brd_devices_mutex);
  495. brd = brd_init_one(MINOR(dev) / max_part, &new);
  496. kobj = brd ? get_disk(brd->brd_disk) : NULL;
  497. mutex_unlock(&brd_devices_mutex);
  498. if (new)
  499. *part = 0;
  500. return kobj;
  501. }
  502. static int __init brd_init(void)
  503. {
  504. struct brd_device *brd, *next;
  505. int i;
  506. /*
  507. * brd module now has a feature to instantiate underlying device
  508. * structure on-demand, provided that there is an access dev node.
  509. *
  510. * (1) if rd_nr is specified, create that many upfront. else
  511. * it defaults to CONFIG_BLK_DEV_RAM_COUNT
  512. * (2) User can further extend brd devices by create dev node themselves
  513. * and have kernel automatically instantiate actual device
  514. * on-demand. Example:
  515. * mknod /path/devnod_name b 1 X # 1 is the rd major
  516. * fdisk -l /path/devnod_name
  517. * If (X / max_part) was not already created it will be created
  518. * dynamically.
  519. */
  520. if (register_blkdev(RAMDISK_MAJOR, "ramdisk"))
  521. return -EIO;
  522. if (unlikely(!max_part))
  523. max_part = 1;
  524. for (i = 0; i < rd_nr; i++) {
  525. brd = brd_alloc(i);
  526. if (!brd)
  527. goto out_free;
  528. list_add_tail(&brd->brd_list, &brd_devices);
  529. }
  530. /* point of no return */
  531. list_for_each_entry(brd, &brd_devices, brd_list)
  532. add_disk(brd->brd_disk);
  533. blk_register_region(MKDEV(RAMDISK_MAJOR, 0), 1UL << MINORBITS,
  534. THIS_MODULE, brd_probe, NULL, NULL);
  535. pr_info("brd: module loaded\n");
  536. return 0;
  537. out_free:
  538. list_for_each_entry_safe(brd, next, &brd_devices, brd_list) {
  539. list_del(&brd->brd_list);
  540. brd_free(brd);
  541. }
  542. unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
  543. pr_info("brd: module NOT loaded !!!\n");
  544. return -ENOMEM;
  545. }
  546. static void __exit brd_exit(void)
  547. {
  548. struct brd_device *brd, *next;
  549. list_for_each_entry_safe(brd, next, &brd_devices, brd_list)
  550. brd_del_one(brd);
  551. blk_unregister_region(MKDEV(RAMDISK_MAJOR, 0), 1UL << MINORBITS);
  552. unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
  553. pr_info("brd: module unloaded\n");
  554. }
  555. module_init(brd_init);
  556. module_exit(brd_exit);