Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
9db5579b NP |
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> | |
287f3ca5 | 13 | #include <linux/initrd.h> |
9db5579b NP |
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> | |
2a48fc0a | 20 | #include <linux/mutex.h> |
4ee60ec1 | 21 | #include <linux/pagemap.h> |
786bb024 | 22 | #include <linux/xarray.h> |
ff01bb48 | 23 | #include <linux/fs.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
23c47d2a | 25 | #include <linux/backing-dev.h> |
f4be591f | 26 | #include <linux/debugfs.h> |
9db5579b | 27 | |
7c0f6ba6 | 28 | #include <linux/uaccess.h> |
9db5579b | 29 | |
9db5579b | 30 | /* |
786bb024 | 31 | * Each block ramdisk device has a xarray brd_pages of pages that stores |
7d8d3579 | 32 | * the pages containing the block device's contents. |
9db5579b NP |
33 | */ |
34 | struct brd_device { | |
7f9b348c | 35 | int brd_number; |
9db5579b NP |
36 | struct gendisk *brd_disk; |
37 | struct list_head brd_list; | |
38 | ||
39 | /* | |
786bb024 | 40 | * Backing store of pages. This is the contents of the block device. |
9db5579b | 41 | */ |
786bb024 | 42 | struct xarray brd_pages; |
f4be591f | 43 | u64 brd_nr_pages; |
9db5579b NP |
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 | { | |
7d8d3579 | 51 | return xa_load(&brd->brd_pages, sector >> PAGE_SECTORS_SHIFT); |
9db5579b NP |
52 | } |
53 | ||
54 | /* | |
db0ccc44 | 55 | * Insert a new page for a given sector, if one does not already exist. |
9db5579b | 56 | */ |
bbcacab2 CH |
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) | |
9db5579b | 61 | { |
bbcacab2 CH |
62 | gfp_t gfp = (opf & REQ_NOWAIT) ? GFP_NOWAIT : GFP_NOIO; |
63 | struct page *page, *ret; | |
9db5579b | 64 | |
bbcacab2 | 65 | rcu_read_unlock(); |
6ded703c | 66 | page = alloc_page(gfp | __GFP_ZERO | __GFP_HIGHMEM); |
bbcacab2 | 67 | rcu_read_lock(); |
9db5579b | 68 | if (!page) |
bbcacab2 | 69 | return ERR_PTR(-ENOMEM); |
9db5579b | 70 | |
786bb024 | 71 | xa_lock(&brd->brd_pages); |
bbcacab2 CH |
72 | ret = __xa_cmpxchg(&brd->brd_pages, sector >> PAGE_SECTORS_SHIFT, NULL, |
73 | page, gfp); | |
74 | if (ret) { | |
75 | xa_unlock(&brd->brd_pages); | |
7d8d3579 | 76 | __free_page(page); |
bbcacab2 CH |
77 | if (xa_is_err(ret)) |
78 | return ERR_PTR(xa_err(ret)); | |
79 | return ret; | |
7d8d3579 | 80 | } |
bbcacab2 CH |
81 | brd->brd_nr_pages++; |
82 | xa_unlock(&brd->brd_pages); | |
83 | return page; | |
9db5579b NP |
84 | } |
85 | ||
86 | /* | |
786bb024 | 87 | * Free all backing store pages and xarray. This must only be called when |
9db5579b NP |
88 | * there are no other users of the device. |
89 | */ | |
9db5579b NP |
90 | static void brd_free_pages(struct brd_device *brd) |
91 | { | |
786bb024 PR |
92 | struct page *page; |
93 | pgoff_t idx; | |
9db5579b | 94 | |
786bb024 PR |
95 | xa_for_each(&brd->brd_pages, idx, page) { |
96 | __free_page(page); | |
6dd4423f | 97 | cond_resched(); |
786bb024 | 98 | } |
936b33f7 | 99 | |
786bb024 | 100 | xa_destroy(&brd->brd_pages); |
9db5579b NP |
101 | } |
102 | ||
9db5579b | 103 | /* |
3185444f CH |
104 | * Process a single segment. The segment is capped to not cross page boundaries |
105 | * in both the bio and the brd backing memory. | |
9db5579b | 106 | */ |
3185444f | 107 | static bool brd_rw_bvec(struct brd_device *brd, struct bio *bio) |
9db5579b | 108 | { |
3185444f CH |
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; | |
53ec1abc CH |
113 | struct page *page; |
114 | void *kaddr; | |
9db5579b | 115 | |
3185444f CH |
116 | bv.bv_len = min_t(u32, bv.bv_len, PAGE_SIZE - offset); |
117 | ||
0e8acffc | 118 | rcu_read_lock(); |
53ec1abc | 119 | page = brd_lookup_page(brd, sector); |
bbcacab2 CH |
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 | } | |
53ec1abc CH |
125 | |
126 | kaddr = bvec_kmap_local(&bv); | |
127 | if (op_is_write(opf)) { | |
bbcacab2 | 128 | memcpy_to_page(page, offset, kaddr, bv.bv_len); |
c2572f2b | 129 | } else { |
53ec1abc CH |
130 | if (page) |
131 | memcpy_from_page(kaddr, page, offset, bv.bv_len); | |
132 | else | |
133 | memset(kaddr, 0, bv.bv_len); | |
c2572f2b | 134 | } |
53ec1abc | 135 | kunmap_local(kaddr); |
0e8acffc | 136 | rcu_read_unlock(); |
3185444f CH |
137 | |
138 | bio_advance_iter_single(bio, &bio->bi_iter, bv.bv_len); | |
139 | return true; | |
bbcacab2 CH |
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; | |
9db5579b NP |
148 | } |
149 | ||
0e8acffc YK |
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 | ||
9ead7efc KB |
157 | static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size) |
158 | { | |
d4099f88 | 159 | sector_t aligned_sector = round_up(sector, PAGE_SECTORS); |
a26a339a YK |
160 | sector_t aligned_end = round_down( |
161 | sector + (size >> SECTOR_SHIFT), PAGE_SECTORS); | |
9ead7efc KB |
162 | struct page *page; |
163 | ||
a26a339a YK |
164 | if (aligned_end <= aligned_sector) |
165 | return; | |
166 | ||
9ead7efc | 167 | xa_lock(&brd->brd_pages); |
a26a339a | 168 | while (aligned_sector < aligned_end && aligned_sector < rd_size * 2) { |
9ead7efc | 169 | page = __xa_erase(&brd->brd_pages, aligned_sector >> PAGE_SECTORS_SHIFT); |
82734209 | 170 | if (page) { |
0e8acffc | 171 | call_rcu(&page->rcu_head, brd_free_one_page); |
82734209 ZX |
172 | brd->brd_nr_pages--; |
173 | } | |
9ead7efc | 174 | aligned_sector += PAGE_SECTORS; |
9ead7efc KB |
175 | } |
176 | xa_unlock(&brd->brd_pages); | |
177 | } | |
178 | ||
3e08773c | 179 | static void brd_submit_bio(struct bio *bio) |
9db5579b | 180 | { |
309dca30 | 181 | struct brd_device *brd = bio->bi_bdev->bd_disk->private_data; |
9db5579b | 182 | |
9ead7efc | 183 | if (unlikely(op_is_discard(bio->bi_opf))) { |
857aba38 CH |
184 | brd_do_discard(brd, bio->bi_iter.bi_sector, |
185 | bio->bi_iter.bi_size); | |
9ead7efc KB |
186 | bio_endio(bio); |
187 | return; | |
188 | } | |
189 | ||
3185444f CH |
190 | do { |
191 | if (!brd_rw_bvec(brd, bio)) | |
3e08773c | 192 | return; |
3185444f | 193 | } while (bio->bi_iter.bi_size); |
9db5579b | 194 | |
4246a0b6 | 195 | bio_endio(bio); |
9db5579b NP |
196 | } |
197 | ||
83d5cde4 | 198 | static const struct block_device_operations brd_fops = { |
75acb9cd | 199 | .owner = THIS_MODULE, |
c62b37d9 | 200 | .submit_bio = brd_submit_bio, |
9db5579b NP |
201 | }; |
202 | ||
203 | /* | |
204 | * And now the modules code and kernel interface. | |
205 | */ | |
937af5ec | 206 | static int rd_nr = CONFIG_BLK_DEV_RAM_COUNT; |
5657a819 | 207 | module_param(rd_nr, int, 0444); |
9db5579b | 208 | MODULE_PARM_DESC(rd_nr, "Maximum number of brd devices"); |
937af5ec | 209 | |
366f4aea | 210 | unsigned long rd_size = CONFIG_BLK_DEV_RAM_SIZE; |
5657a819 | 211 | module_param(rd_size, ulong, 0444); |
9db5579b | 212 | MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes."); |
937af5ec BH |
213 | |
214 | static int max_part = 1; | |
5657a819 | 215 | module_param(max_part, int, 0444); |
937af5ec BH |
216 | MODULE_PARM_DESC(max_part, "Num Minors to reserve between devices"); |
217 | ||
876835b1 | 218 | MODULE_DESCRIPTION("Ram backed block device driver"); |
9db5579b NP |
219 | MODULE_LICENSE("GPL"); |
220 | MODULE_ALIAS_BLOCKDEV_MAJOR(RAMDISK_MAJOR); | |
efedf51c | 221 | MODULE_ALIAS("rd"); |
9db5579b NP |
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 | } | |
1adbee50 | 230 | __setup("ramdisk_size=", ramdisk_size); |
9db5579b NP |
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); | |
826cc42a | 238 | static DEFINE_MUTEX(brd_devices_mutex); |
f4be591f | 239 | static struct dentry *brd_debugfs_dir; |
9db5579b | 240 | |
826cc42a YE |
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 | ||
7f9b348c | 272 | static int brd_alloc(int i) |
9db5579b NP |
273 | { |
274 | struct brd_device *brd; | |
275 | struct gendisk *disk; | |
f4be591f | 276 | char buf[DISK_NAME_LEN]; |
e1528830 | 277 | int err = -ENOMEM; |
b5baaba4 CH |
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, | |
9ead7efc KB |
287 | .max_hw_discard_sectors = UINT_MAX, |
288 | .max_discard_segments = 1, | |
289 | .discard_granularity = PAGE_SIZE, | |
f76af42f CH |
290 | .features = BLK_FEAT_SYNCHRONOUS | |
291 | BLK_FEAT_NOWAIT, | |
b5baaba4 | 292 | }; |
9db5579b | 293 | |
826cc42a YE |
294 | brd = brd_find_or_alloc_device(i); |
295 | if (IS_ERR(brd)) | |
296 | return PTR_ERR(brd); | |
f7bf3586 | 297 | |
786bb024 | 298 | xa_init(&brd->brd_pages); |
9db5579b | 299 | |
f4be591f CO |
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 | ||
b5baaba4 | 305 | disk = brd->brd_disk = blk_alloc_disk(&lim, NUMA_NO_NODE); |
74fa8f9c CH |
306 | if (IS_ERR(disk)) { |
307 | err = PTR_ERR(disk); | |
7f9b348c | 308 | goto out_free_dev; |
74fa8f9c | 309 | } |
9db5579b | 310 | disk->major = RAMDISK_MAJOR; |
937af5ec | 311 | disk->first_minor = i * max_part; |
7f9b348c | 312 | disk->minors = max_part; |
9db5579b NP |
313 | disk->fops = &brd_fops; |
314 | disk->private_data = brd; | |
e55e1b48 | 315 | strscpy(disk->disk_name, buf, DISK_NAME_LEN); |
9db5579b | 316 | set_capacity(disk, rd_size * 2); |
7f9b348c | 317 | |
e1528830 LC |
318 | err = add_disk(disk); |
319 | if (err) | |
320 | goto out_cleanup_disk; | |
316ba573 | 321 | |
7f9b348c | 322 | return 0; |
9db5579b | 323 | |
e1528830 | 324 | out_cleanup_disk: |
8b9ab626 | 325 | put_disk(disk); |
9db5579b | 326 | out_free_dev: |
826cc42a | 327 | brd_free_device(brd); |
e1528830 | 328 | return err; |
9db5579b NP |
329 | } |
330 | ||
7cc178a6 | 331 | static void brd_probe(dev_t dev) |
9db5579b | 332 | { |
f7bf3586 | 333 | brd_alloc(MINOR(dev) / max_part); |
9db5579b NP |
334 | } |
335 | ||
00358933 | 336 | static void brd_cleanup(void) |
9db5579b | 337 | { |
00358933 TH |
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); | |
8b9ab626 | 344 | put_disk(brd->brd_disk); |
00358933 | 345 | brd_free_pages(brd); |
826cc42a | 346 | brd_free_device(brd); |
00358933 | 347 | } |
9db5579b NP |
348 | } |
349 | ||
c8ab4225 ZL |
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 | ||
9db5579b NP |
369 | static int __init brd_init(void) |
370 | { | |
7f9b348c | 371 | int err, i; |
9db5579b NP |
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. | |
9db5579b | 376 | * |
937af5ec BH |
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. | |
9db5579b | 386 | */ |
d7853d1f | 387 | |
826cc42a YE |
388 | brd_check_and_reset_par(); |
389 | ||
390 | brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL); | |
391 | ||
00358933 TH |
392 | if (__register_blkdev(RAMDISK_MAJOR, "ramdisk", brd_probe)) { |
393 | err = -EIO; | |
394 | goto out_free; | |
9db5579b NP |
395 | } |
396 | ||
826cc42a YE |
397 | for (i = 0; i < rd_nr; i++) |
398 | brd_alloc(i); | |
399 | ||
937af5ec | 400 | pr_info("brd: module loaded\n"); |
9db5579b NP |
401 | return 0; |
402 | ||
403 | out_free: | |
00358933 | 404 | brd_cleanup(); |
9db5579b | 405 | |
937af5ec | 406 | pr_info("brd: module NOT loaded !!!\n"); |
7f9b348c | 407 | return err; |
9db5579b NP |
408 | } |
409 | ||
410 | static void __exit brd_exit(void) | |
411 | { | |
9db5579b | 412 | |
f7bf3586 | 413 | unregister_blkdev(RAMDISK_MAJOR, "ramdisk"); |
00358933 | 414 | brd_cleanup(); |
9db5579b | 415 | |
937af5ec | 416 | pr_info("brd: module unloaded\n"); |
9db5579b NP |
417 | } |
418 | ||
419 | module_init(brd_init); | |
420 | module_exit(brd_exit); | |
421 |