1 // SPDX-License-Identifier: GPL-2.0-only
3 * Ram backed block device driver.
5 * Copyright (C) 2007 Nick Piggin
6 * Copyright (C) 2007 Novell Inc.
8 * Parts derived from drivers/block/rd.c, and drivers/block/loop.c, copyright
9 * of their respective owners.
12 #include <linux/init.h>
13 #include <linux/initrd.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/major.h>
17 #include <linux/blkdev.h>
18 #include <linux/bio.h>
19 #include <linux/highmem.h>
20 #include <linux/mutex.h>
21 #include <linux/pagemap.h>
22 #include <linux/xarray.h>
24 #include <linux/slab.h>
25 #include <linux/backing-dev.h>
26 #include <linux/debugfs.h>
28 #include <linux/uaccess.h>
31 * Each block ramdisk device has a xarray brd_pages of pages that stores
32 * the pages containing the block device's contents. A brd page's ->index is
33 * its offset in PAGE_SIZE units. This is similar to, but in no way connected
34 * with, the kernel's pagecache or buffer cache (which sit above our block
39 struct gendisk *brd_disk;
40 struct list_head brd_list;
43 * Backing store of pages. This is the contents of the block device.
45 struct xarray brd_pages;
50 * Look up and return a brd's page for a given sector.
52 static struct page *brd_lookup_page(struct brd_device *brd, sector_t sector)
57 idx = sector >> PAGE_SECTORS_SHIFT; /* sector to page index */
58 page = xa_load(&brd->brd_pages, idx);
60 BUG_ON(page && page->index != idx);
66 * Insert a new page for a given sector, if one does not already exist.
68 static int brd_insert_page(struct brd_device *brd, sector_t sector, gfp_t gfp)
71 struct page *page, *cur;
74 page = brd_lookup_page(brd, sector);
78 page = alloc_page(gfp | __GFP_ZERO | __GFP_HIGHMEM);
82 xa_lock(&brd->brd_pages);
84 idx = sector >> PAGE_SECTORS_SHIFT;
87 cur = __xa_cmpxchg(&brd->brd_pages, idx, NULL, page, gfp);
92 if (!ret && (cur->index != idx))
98 xa_unlock(&brd->brd_pages);
104 * Free all backing store pages and xarray. This must only be called when
105 * there are no other users of the device.
107 static void brd_free_pages(struct brd_device *brd)
112 xa_for_each(&brd->brd_pages, idx, page) {
117 xa_destroy(&brd->brd_pages);
121 * copy_to_brd_setup must be called before copy_to_brd. It may sleep.
123 static int copy_to_brd_setup(struct brd_device *brd, sector_t sector, size_t n,
126 unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
130 copy = min_t(size_t, n, PAGE_SIZE - offset);
131 ret = brd_insert_page(brd, sector, gfp);
135 sector += copy >> SECTOR_SHIFT;
136 ret = brd_insert_page(brd, sector, gfp);
142 * Copy n bytes from src to the brd starting at sector. Does not sleep.
144 static void copy_to_brd(struct brd_device *brd, const void *src,
145 sector_t sector, size_t n)
149 unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
152 copy = min_t(size_t, n, PAGE_SIZE - offset);
153 page = brd_lookup_page(brd, sector);
156 dst = kmap_atomic(page);
157 memcpy(dst + offset, src, copy);
162 sector += copy >> SECTOR_SHIFT;
164 page = brd_lookup_page(brd, sector);
167 dst = kmap_atomic(page);
168 memcpy(dst, src, copy);
174 * Copy n bytes to dst from the brd starting at sector. Does not sleep.
176 static void copy_from_brd(void *dst, struct brd_device *brd,
177 sector_t sector, size_t n)
181 unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
184 copy = min_t(size_t, n, PAGE_SIZE - offset);
185 page = brd_lookup_page(brd, sector);
187 src = kmap_atomic(page);
188 memcpy(dst, src + offset, copy);
191 memset(dst, 0, copy);
195 sector += copy >> SECTOR_SHIFT;
197 page = brd_lookup_page(brd, sector);
199 src = kmap_atomic(page);
200 memcpy(dst, src, copy);
203 memset(dst, 0, copy);
208 * Process a single bvec of a bio.
210 static int brd_do_bvec(struct brd_device *brd, struct page *page,
211 unsigned int len, unsigned int off, blk_opf_t opf,
217 if (op_is_write(opf)) {
219 * Must use NOIO because we don't want to recurse back into the
220 * block or filesystem layers from page reclaim.
222 gfp_t gfp = opf & REQ_NOWAIT ? GFP_NOWAIT : GFP_NOIO;
224 err = copy_to_brd_setup(brd, sector, len, gfp);
229 mem = kmap_atomic(page);
230 if (!op_is_write(opf)) {
231 copy_from_brd(mem + off, brd, sector, len);
232 flush_dcache_page(page);
234 flush_dcache_page(page);
235 copy_to_brd(brd, mem + off, sector, len);
243 static void brd_submit_bio(struct bio *bio)
245 struct brd_device *brd = bio->bi_bdev->bd_disk->private_data;
246 sector_t sector = bio->bi_iter.bi_sector;
248 struct bvec_iter iter;
250 bio_for_each_segment(bvec, bio, iter) {
251 unsigned int len = bvec.bv_len;
254 /* Don't support un-aligned buffer */
255 WARN_ON_ONCE((bvec.bv_offset & (SECTOR_SIZE - 1)) ||
256 (len & (SECTOR_SIZE - 1)));
258 err = brd_do_bvec(brd, bvec.bv_page, len, bvec.bv_offset,
259 bio->bi_opf, sector);
261 if (err == -ENOMEM && bio->bi_opf & REQ_NOWAIT) {
262 bio_wouldblock_error(bio);
268 sector += len >> SECTOR_SHIFT;
274 static const struct block_device_operations brd_fops = {
275 .owner = THIS_MODULE,
276 .submit_bio = brd_submit_bio,
280 * And now the modules code and kernel interface.
282 static int rd_nr = CONFIG_BLK_DEV_RAM_COUNT;
283 module_param(rd_nr, int, 0444);
284 MODULE_PARM_DESC(rd_nr, "Maximum number of brd devices");
286 unsigned long rd_size = CONFIG_BLK_DEV_RAM_SIZE;
287 module_param(rd_size, ulong, 0444);
288 MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
290 static int max_part = 1;
291 module_param(max_part, int, 0444);
292 MODULE_PARM_DESC(max_part, "Num Minors to reserve between devices");
294 MODULE_LICENSE("GPL");
295 MODULE_ALIAS_BLOCKDEV_MAJOR(RAMDISK_MAJOR);
299 /* Legacy boot options - nonmodular */
300 static int __init ramdisk_size(char *str)
302 rd_size = simple_strtol(str, NULL, 0);
305 __setup("ramdisk_size=", ramdisk_size);
309 * The device scheme is derived from loop.c. Keep them in synch where possible
310 * (should share code eventually).
312 static LIST_HEAD(brd_devices);
313 static struct dentry *brd_debugfs_dir;
315 static int brd_alloc(int i)
317 struct brd_device *brd;
318 struct gendisk *disk;
319 char buf[DISK_NAME_LEN];
322 list_for_each_entry(brd, &brd_devices, brd_list)
323 if (brd->brd_number == i)
325 brd = kzalloc(sizeof(*brd), GFP_KERNEL);
329 list_add_tail(&brd->brd_list, &brd_devices);
331 xa_init(&brd->brd_pages);
333 snprintf(buf, DISK_NAME_LEN, "ram%d", i);
334 if (!IS_ERR_OR_NULL(brd_debugfs_dir))
335 debugfs_create_u64(buf, 0444, brd_debugfs_dir,
338 disk = brd->brd_disk = blk_alloc_disk(NUMA_NO_NODE);
342 disk->major = RAMDISK_MAJOR;
343 disk->first_minor = i * max_part;
344 disk->minors = max_part;
345 disk->fops = &brd_fops;
346 disk->private_data = brd;
347 strscpy(disk->disk_name, buf, DISK_NAME_LEN);
348 set_capacity(disk, rd_size * 2);
351 * This is so fdisk will align partitions on 4k, because of
352 * direct_access API needing 4k alignment, returning a PFN
353 * (This is only a problem on very small devices <= 4M,
354 * otherwise fdisk will align on 1M. Regardless this call
357 blk_queue_physical_block_size(disk->queue, PAGE_SIZE);
359 /* Tell the block layer that this is not a rotational device */
360 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
361 blk_queue_flag_set(QUEUE_FLAG_SYNCHRONOUS, disk->queue);
362 blk_queue_flag_set(QUEUE_FLAG_NOWAIT, disk->queue);
363 err = add_disk(disk);
365 goto out_cleanup_disk;
372 list_del(&brd->brd_list);
377 static void brd_probe(dev_t dev)
379 brd_alloc(MINOR(dev) / max_part);
382 static void brd_cleanup(void)
384 struct brd_device *brd, *next;
386 debugfs_remove_recursive(brd_debugfs_dir);
388 list_for_each_entry_safe(brd, next, &brd_devices, brd_list) {
389 del_gendisk(brd->brd_disk);
390 put_disk(brd->brd_disk);
392 list_del(&brd->brd_list);
397 static inline void brd_check_and_reset_par(void)
399 if (unlikely(!max_part))
403 * make sure 'max_part' can be divided exactly by (1U << MINORBITS),
404 * otherwise, it is possiable to get same dev_t when adding partitions.
406 if ((1U << MINORBITS) % max_part != 0)
407 max_part = 1UL << fls(max_part);
409 if (max_part > DISK_MAX_PARTS) {
410 pr_info("brd: max_part can't be larger than %d, reset max_part = %d.\n",
411 DISK_MAX_PARTS, DISK_MAX_PARTS);
412 max_part = DISK_MAX_PARTS;
416 static int __init brd_init(void)
420 brd_check_and_reset_par();
422 brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
424 for (i = 0; i < rd_nr; i++) {
431 * brd module now has a feature to instantiate underlying device
432 * structure on-demand, provided that there is an access dev node.
434 * (1) if rd_nr is specified, create that many upfront. else
435 * it defaults to CONFIG_BLK_DEV_RAM_COUNT
436 * (2) User can further extend brd devices by create dev node themselves
437 * and have kernel automatically instantiate actual device
438 * on-demand. Example:
439 * mknod /path/devnod_name b 1 X # 1 is the rd major
440 * fdisk -l /path/devnod_name
441 * If (X / max_part) was not already created it will be created
445 if (__register_blkdev(RAMDISK_MAJOR, "ramdisk", brd_probe)) {
450 pr_info("brd: module loaded\n");
456 pr_info("brd: module NOT loaded !!!\n");
460 static void __exit brd_exit(void)
463 unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
466 pr_info("brd: module unloaded\n");
469 module_init(brd_init);
470 module_exit(brd_exit);