brd.c 15 KB

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