brd.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Ram backed block device driver.
  4. *
  5. * Copyright (C) 2007 Nick Piggin
  6. * Copyright (C) 2007 Novell Inc.
  7. *
  8. * Parts derived from drivers/block/rd.c, and drivers/block/loop.c, copyright
  9. * of their respective owners.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/initrd.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/major.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/bio.h>
  18. #include <linux/highmem.h>
  19. #include <linux/mutex.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/xarray.h>
  22. #include <linux/fs.h>
  23. #include <linux/slab.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/uaccess.h>
  27. /*
  28. * Each block ramdisk device has a xarray brd_pages of pages that stores
  29. * the pages containing the block device's contents.
  30. */
  31. struct brd_device {
  32. int brd_number;
  33. struct gendisk *brd_disk;
  34. struct list_head brd_list;
  35. /*
  36. * Backing store of pages. This is the contents of the block device.
  37. */
  38. struct xarray brd_pages;
  39. u64 brd_nr_pages;
  40. };
  41. /*
  42. * Look up and return a brd's page with reference grabbed for a given sector.
  43. */
  44. static struct page *brd_lookup_page(struct brd_device *brd, sector_t sector)
  45. {
  46. struct page *page;
  47. XA_STATE(xas, &brd->brd_pages, sector >> PAGE_SECTORS_SHIFT);
  48. rcu_read_lock();
  49. repeat:
  50. page = xas_load(&xas);
  51. if (xas_retry(&xas, page)) {
  52. xas_reset(&xas);
  53. goto repeat;
  54. }
  55. if (!page)
  56. goto out;
  57. if (!get_page_unless_zero(page)) {
  58. xas_reset(&xas);
  59. goto repeat;
  60. }
  61. if (unlikely(page != xas_reload(&xas))) {
  62. put_page(page);
  63. xas_reset(&xas);
  64. goto repeat;
  65. }
  66. out:
  67. rcu_read_unlock();
  68. return page;
  69. }
  70. /*
  71. * Insert a new page for a given sector, if one does not already exist.
  72. * The returned page will grab reference.
  73. */
  74. static struct page *brd_insert_page(struct brd_device *brd, sector_t sector,
  75. blk_opf_t opf)
  76. {
  77. gfp_t gfp = (opf & REQ_NOWAIT) ? GFP_NOWAIT : GFP_NOIO;
  78. struct page *page, *ret;
  79. page = alloc_page(gfp | __GFP_ZERO | __GFP_HIGHMEM);
  80. if (!page)
  81. return ERR_PTR(-ENOMEM);
  82. xa_lock(&brd->brd_pages);
  83. ret = __xa_cmpxchg(&brd->brd_pages, sector >> PAGE_SECTORS_SHIFT, NULL,
  84. page, gfp);
  85. if (!ret) {
  86. brd->brd_nr_pages++;
  87. get_page(page);
  88. xa_unlock(&brd->brd_pages);
  89. return page;
  90. }
  91. if (!xa_is_err(ret)) {
  92. get_page(ret);
  93. xa_unlock(&brd->brd_pages);
  94. put_page(page);
  95. return ret;
  96. }
  97. xa_unlock(&brd->brd_pages);
  98. put_page(page);
  99. return ERR_PTR(xa_err(ret));
  100. }
  101. /*
  102. * Free all backing store pages and xarray. This must only be called when
  103. * there are no other users of the device.
  104. */
  105. static void brd_free_pages(struct brd_device *brd)
  106. {
  107. struct page *page;
  108. pgoff_t idx;
  109. xa_for_each(&brd->brd_pages, idx, page) {
  110. put_page(page);
  111. cond_resched();
  112. }
  113. xa_destroy(&brd->brd_pages);
  114. }
  115. /*
  116. * Process a single segment. The segment is capped to not cross page boundaries
  117. * in both the bio and the brd backing memory.
  118. */
  119. static bool brd_rw_bvec(struct brd_device *brd, struct bio *bio)
  120. {
  121. struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
  122. sector_t sector = bio->bi_iter.bi_sector;
  123. u32 offset = (sector & (PAGE_SECTORS - 1)) << SECTOR_SHIFT;
  124. blk_opf_t opf = bio->bi_opf;
  125. struct page *page;
  126. void *kaddr;
  127. bv.bv_len = min_t(u32, bv.bv_len, PAGE_SIZE - offset);
  128. page = brd_lookup_page(brd, sector);
  129. if (!page && op_is_write(opf)) {
  130. page = brd_insert_page(brd, sector, opf);
  131. if (IS_ERR(page))
  132. goto out_error;
  133. }
  134. kaddr = bvec_kmap_local(&bv);
  135. if (op_is_write(opf)) {
  136. memcpy_to_page(page, offset, kaddr, bv.bv_len);
  137. } else {
  138. if (page)
  139. memcpy_from_page(kaddr, page, offset, bv.bv_len);
  140. else
  141. memset(kaddr, 0, bv.bv_len);
  142. }
  143. kunmap_local(kaddr);
  144. bio_advance_iter_single(bio, &bio->bi_iter, bv.bv_len);
  145. if (page)
  146. put_page(page);
  147. return true;
  148. out_error:
  149. if (PTR_ERR(page) == -ENOMEM && (opf & REQ_NOWAIT))
  150. bio_wouldblock_error(bio);
  151. else
  152. bio_io_error(bio);
  153. return false;
  154. }
  155. static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size)
  156. {
  157. sector_t aligned_sector = round_up(sector, PAGE_SECTORS);
  158. sector_t aligned_end = round_down(
  159. sector + (size >> SECTOR_SHIFT), PAGE_SECTORS);
  160. struct page *page;
  161. if (aligned_end <= aligned_sector)
  162. return;
  163. xa_lock(&brd->brd_pages);
  164. while (aligned_sector < aligned_end && aligned_sector < rd_size * 2) {
  165. page = __xa_erase(&brd->brd_pages, aligned_sector >> PAGE_SECTORS_SHIFT);
  166. if (page) {
  167. put_page(page);
  168. brd->brd_nr_pages--;
  169. }
  170. aligned_sector += PAGE_SECTORS;
  171. }
  172. xa_unlock(&brd->brd_pages);
  173. }
  174. static void brd_submit_bio(struct bio *bio)
  175. {
  176. struct brd_device *brd = bio->bi_bdev->bd_disk->private_data;
  177. if (unlikely(op_is_discard(bio->bi_opf))) {
  178. brd_do_discard(brd, bio->bi_iter.bi_sector,
  179. bio->bi_iter.bi_size);
  180. bio_endio(bio);
  181. return;
  182. }
  183. do {
  184. if (!brd_rw_bvec(brd, bio))
  185. return;
  186. } while (bio->bi_iter.bi_size);
  187. bio_endio(bio);
  188. }
  189. static const struct block_device_operations brd_fops = {
  190. .owner = THIS_MODULE,
  191. .submit_bio = brd_submit_bio,
  192. };
  193. /*
  194. * And now the modules code and kernel interface.
  195. */
  196. static int rd_nr = CONFIG_BLK_DEV_RAM_COUNT;
  197. module_param(rd_nr, int, 0444);
  198. MODULE_PARM_DESC(rd_nr, "Maximum number of brd devices");
  199. unsigned long rd_size = CONFIG_BLK_DEV_RAM_SIZE;
  200. module_param(rd_size, ulong, 0444);
  201. MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
  202. static int max_part = 1;
  203. module_param(max_part, int, 0444);
  204. MODULE_PARM_DESC(max_part, "Num Minors to reserve between devices");
  205. MODULE_DESCRIPTION("Ram backed block device driver");
  206. MODULE_LICENSE("GPL");
  207. MODULE_ALIAS_BLOCKDEV_MAJOR(RAMDISK_MAJOR);
  208. MODULE_ALIAS("rd");
  209. #ifndef MODULE
  210. /* Legacy boot options - nonmodular */
  211. static int __init ramdisk_size(char *str)
  212. {
  213. return kstrtoul(str, 0, &rd_size) == 0;
  214. }
  215. __setup("ramdisk_size=", ramdisk_size);
  216. #endif
  217. /*
  218. * The device scheme is derived from loop.c. Keep them in synch where possible
  219. * (should share code eventually).
  220. */
  221. static LIST_HEAD(brd_devices);
  222. static DEFINE_MUTEX(brd_devices_mutex);
  223. static struct dentry *brd_debugfs_dir;
  224. static struct brd_device *brd_find_or_alloc_device(int i)
  225. {
  226. struct brd_device *brd;
  227. mutex_lock(&brd_devices_mutex);
  228. list_for_each_entry(brd, &brd_devices, brd_list) {
  229. if (brd->brd_number == i) {
  230. mutex_unlock(&brd_devices_mutex);
  231. return ERR_PTR(-EEXIST);
  232. }
  233. }
  234. brd = kzalloc_obj(*brd);
  235. if (!brd) {
  236. mutex_unlock(&brd_devices_mutex);
  237. return ERR_PTR(-ENOMEM);
  238. }
  239. brd->brd_number = i;
  240. list_add_tail(&brd->brd_list, &brd_devices);
  241. mutex_unlock(&brd_devices_mutex);
  242. return brd;
  243. }
  244. static void brd_free_device(struct brd_device *brd)
  245. {
  246. mutex_lock(&brd_devices_mutex);
  247. list_del(&brd->brd_list);
  248. mutex_unlock(&brd_devices_mutex);
  249. kfree(brd);
  250. }
  251. static int brd_alloc(int i)
  252. {
  253. struct brd_device *brd;
  254. struct gendisk *disk;
  255. char buf[DISK_NAME_LEN];
  256. int err = -ENOMEM;
  257. struct queue_limits lim = {
  258. /*
  259. * This is so fdisk will align partitions on 4k, because of
  260. * direct_access API needing 4k alignment, returning a PFN
  261. * (This is only a problem on very small devices <= 4M,
  262. * otherwise fdisk will align on 1M. Regardless this call
  263. * is harmless)
  264. */
  265. .physical_block_size = PAGE_SIZE,
  266. .max_hw_discard_sectors = UINT_MAX,
  267. .max_discard_segments = 1,
  268. .discard_granularity = PAGE_SIZE,
  269. .features = BLK_FEAT_SYNCHRONOUS |
  270. BLK_FEAT_NOWAIT,
  271. };
  272. brd = brd_find_or_alloc_device(i);
  273. if (IS_ERR(brd))
  274. return PTR_ERR(brd);
  275. xa_init(&brd->brd_pages);
  276. snprintf(buf, DISK_NAME_LEN, "ram%d", i);
  277. if (!IS_ERR_OR_NULL(brd_debugfs_dir))
  278. debugfs_create_u64(buf, 0444, brd_debugfs_dir,
  279. &brd->brd_nr_pages);
  280. disk = brd->brd_disk = blk_alloc_disk(&lim, NUMA_NO_NODE);
  281. if (IS_ERR(disk)) {
  282. err = PTR_ERR(disk);
  283. goto out_free_dev;
  284. }
  285. disk->major = RAMDISK_MAJOR;
  286. disk->first_minor = i * max_part;
  287. disk->minors = max_part;
  288. disk->fops = &brd_fops;
  289. disk->private_data = brd;
  290. strscpy(disk->disk_name, buf, DISK_NAME_LEN);
  291. set_capacity(disk, rd_size * 2);
  292. err = add_disk(disk);
  293. if (err)
  294. goto out_cleanup_disk;
  295. return 0;
  296. out_cleanup_disk:
  297. put_disk(disk);
  298. out_free_dev:
  299. brd_free_device(brd);
  300. return err;
  301. }
  302. static void brd_probe(dev_t dev)
  303. {
  304. brd_alloc(MINOR(dev) / max_part);
  305. }
  306. static void brd_cleanup(void)
  307. {
  308. struct brd_device *brd, *next;
  309. debugfs_remove_recursive(brd_debugfs_dir);
  310. list_for_each_entry_safe(brd, next, &brd_devices, brd_list) {
  311. del_gendisk(brd->brd_disk);
  312. put_disk(brd->brd_disk);
  313. brd_free_pages(brd);
  314. brd_free_device(brd);
  315. }
  316. }
  317. static inline void brd_check_and_reset_par(void)
  318. {
  319. if (unlikely(!max_part))
  320. max_part = 1;
  321. /*
  322. * make sure 'max_part' can be divided exactly by (1U << MINORBITS),
  323. * otherwise, it is possiable to get same dev_t when adding partitions.
  324. */
  325. if ((1U << MINORBITS) % max_part != 0)
  326. max_part = 1UL << fls(max_part);
  327. if (max_part > DISK_MAX_PARTS) {
  328. pr_info("brd: max_part can't be larger than %d, reset max_part = %d.\n",
  329. DISK_MAX_PARTS, DISK_MAX_PARTS);
  330. max_part = DISK_MAX_PARTS;
  331. }
  332. }
  333. static int __init brd_init(void)
  334. {
  335. int err, i;
  336. /*
  337. * brd module now has a feature to instantiate underlying device
  338. * structure on-demand, provided that there is an access dev node.
  339. *
  340. * (1) if rd_nr is specified, create that many upfront. else
  341. * it defaults to CONFIG_BLK_DEV_RAM_COUNT
  342. * (2) User can further extend brd devices by create dev node themselves
  343. * and have kernel automatically instantiate actual device
  344. * on-demand. Example:
  345. * mknod /path/devnod_name b 1 X # 1 is the rd major
  346. * fdisk -l /path/devnod_name
  347. * If (X / max_part) was not already created it will be created
  348. * dynamically.
  349. */
  350. brd_check_and_reset_par();
  351. brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
  352. if (__register_blkdev(RAMDISK_MAJOR, "ramdisk", brd_probe)) {
  353. err = -EIO;
  354. goto out_free;
  355. }
  356. for (i = 0; i < rd_nr; i++)
  357. brd_alloc(i);
  358. pr_info("brd: module loaded\n");
  359. return 0;
  360. out_free:
  361. brd_cleanup();
  362. pr_info("brd: module NOT loaded !!!\n");
  363. return err;
  364. }
  365. static void __exit brd_exit(void)
  366. {
  367. unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
  368. brd_cleanup();
  369. pr_info("brd: module unloaded\n");
  370. }
  371. module_init(brd_init);
  372. module_exit(brd_exit);