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