1
2
3
4
5
6
7
8
9
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/major.h>
15#include <linux/blkdev.h>
16#include <linux/bio.h>
17#include <linux/highmem.h>
18#include <linux/mutex.h>
19#include <linux/radix-tree.h>
20#include <linux/fs.h>
21#include <linux/slab.h>
22#ifdef CONFIG_BLK_DEV_RAM_DAX
23#include <linux/pfn_t.h>
24#endif
25
26#include <asm/uaccess.h>
27
28#define SECTOR_SHIFT 9
29#define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
30#define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
31
32
33
34
35
36
37
38
39struct brd_device {
40 int brd_number;
41
42 struct request_queue *brd_queue;
43 struct gendisk *brd_disk;
44 struct list_head brd_list;
45
46
47
48
49
50 spinlock_t brd_lock;
51 struct radix_tree_root brd_pages;
52};
53
54
55
56
57static DEFINE_MUTEX(brd_mutex);
58static struct page *brd_lookup_page(struct brd_device *brd, sector_t sector)
59{
60 pgoff_t idx;
61 struct page *page;
62
63
64
65
66
67
68
69
70
71
72
73
74 rcu_read_lock();
75 idx = sector >> PAGE_SECTORS_SHIFT;
76 page = radix_tree_lookup(&brd->brd_pages, idx);
77 rcu_read_unlock();
78
79 BUG_ON(page && page->index != idx);
80
81 return page;
82}
83
84
85
86
87
88
89static struct page *brd_insert_page(struct brd_device *brd, sector_t sector)
90{
91 pgoff_t idx;
92 struct page *page;
93 gfp_t gfp_flags;
94
95 page = brd_lookup_page(brd, sector);
96 if (page)
97 return page;
98
99
100
101
102
103
104
105
106
107
108 gfp_flags = GFP_NOIO | __GFP_ZERO;
109#ifndef CONFIG_BLK_DEV_RAM_DAX
110 gfp_flags |= __GFP_HIGHMEM;
111#endif
112 page = alloc_page(gfp_flags);
113 if (!page)
114 return NULL;
115
116 if (radix_tree_preload(GFP_NOIO)) {
117 __free_page(page);
118 return NULL;
119 }
120
121 spin_lock(&brd->brd_lock);
122 idx = sector >> PAGE_SECTORS_SHIFT;
123 page->index = idx;
124 if (radix_tree_insert(&brd->brd_pages, idx, page)) {
125 __free_page(page);
126 page = radix_tree_lookup(&brd->brd_pages, idx);
127 BUG_ON(!page);
128 BUG_ON(page->index != idx);
129 }
130 spin_unlock(&brd->brd_lock);
131
132 radix_tree_preload_end();
133
134 return page;
135}
136
137static void brd_free_page(struct brd_device *brd, sector_t sector)
138{
139 struct page *page;
140 pgoff_t idx;
141
142 spin_lock(&brd->brd_lock);
143 idx = sector >> PAGE_SECTORS_SHIFT;
144 page = radix_tree_delete(&brd->brd_pages, idx);
145 spin_unlock(&brd->brd_lock);
146 if (page)
147 __free_page(page);
148}
149
150static void brd_zero_page(struct brd_device *brd, sector_t sector)
151{
152 struct page *page;
153
154 page = brd_lookup_page(brd, sector);
155 if (page)
156 clear_highpage(page);
157}
158
159
160
161
162
163#define FREE_BATCH 16
164static void brd_free_pages(struct brd_device *brd)
165{
166 unsigned long pos = 0;
167 struct page *pages[FREE_BATCH];
168 int nr_pages;
169
170 do {
171 int i;
172
173 nr_pages = radix_tree_gang_lookup(&brd->brd_pages,
174 (void **)pages, pos, FREE_BATCH);
175
176 for (i = 0; i < nr_pages; i++) {
177 void *ret;
178
179 BUG_ON(pages[i]->index < pos);
180 pos = pages[i]->index;
181 ret = radix_tree_delete(&brd->brd_pages, pos);
182 BUG_ON(!ret || ret != pages[i]);
183 __free_page(pages[i]);
184 }
185
186 pos++;
187
188
189
190
191
192
193 } while (nr_pages == FREE_BATCH);
194}
195
196
197
198
199static int copy_to_brd_setup(struct brd_device *brd, sector_t sector, size_t n)
200{
201 unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
202 size_t copy;
203
204 copy = min_t(size_t, n, PAGE_SIZE - offset);
205 if (!brd_insert_page(brd, sector))
206 return -ENOSPC;
207 if (copy < n) {
208 sector += copy >> SECTOR_SHIFT;
209 if (!brd_insert_page(brd, sector))
210 return -ENOSPC;
211 }
212 return 0;
213}
214
215static void discard_from_brd(struct brd_device *brd,
216 sector_t sector, size_t n)
217{
218 while (n >= PAGE_SIZE) {
219
220
221
222
223
224 if (0)
225 brd_free_page(brd, sector);
226 else
227 brd_zero_page(brd, sector);
228 sector += PAGE_SIZE >> SECTOR_SHIFT;
229 n -= PAGE_SIZE;
230 }
231}
232
233
234
235
236static void copy_to_brd(struct brd_device *brd, const void *src,
237 sector_t sector, size_t n)
238{
239 struct page *page;
240 void *dst;
241 unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
242 size_t copy;
243
244 copy = min_t(size_t, n, PAGE_SIZE - offset);
245 page = brd_lookup_page(brd, sector);
246 BUG_ON(!page);
247
248 dst = kmap_atomic(page);
249 memcpy(dst + offset, src, copy);
250 kunmap_atomic(dst);
251
252 if (copy < n) {
253 src += copy;
254 sector += copy >> SECTOR_SHIFT;
255 copy = n - copy;
256 page = brd_lookup_page(brd, sector);
257 BUG_ON(!page);
258
259 dst = kmap_atomic(page);
260 memcpy(dst, src, copy);
261 kunmap_atomic(dst);
262 }
263}
264
265
266
267
268static void copy_from_brd(void *dst, struct brd_device *brd,
269 sector_t sector, size_t n)
270{
271 struct page *page;
272 void *src;
273 unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
274 size_t copy;
275
276 copy = min_t(size_t, n, PAGE_SIZE - offset);
277 page = brd_lookup_page(brd, sector);
278 if (page) {
279 src = kmap_atomic(page);
280 memcpy(dst, src + offset, copy);
281 kunmap_atomic(src);
282 } else
283 memset(dst, 0, copy);
284
285 if (copy < n) {
286 dst += copy;
287 sector += copy >> SECTOR_SHIFT;
288 copy = n - copy;
289 page = brd_lookup_page(brd, sector);
290 if (page) {
291 src = kmap_atomic(page);
292 memcpy(dst, src, copy);
293 kunmap_atomic(src);
294 } else
295 memset(dst, 0, copy);
296 }
297}
298
299
300
301
302static int brd_do_bvec(struct brd_device *brd, struct page *page,
303 unsigned int len, unsigned int off, int rw,
304 sector_t sector)
305{
306 void *mem;
307 int err = 0;
308
309 if (rw != READ) {
310 err = copy_to_brd_setup(brd, sector, len);
311 if (err)
312 goto out;
313 }
314
315 mem = kmap_atomic(page);
316 if (rw == READ) {
317 copy_from_brd(mem + off, brd, sector, len);
318 flush_dcache_page(page);
319 } else {
320 flush_dcache_page(page);
321 copy_to_brd(brd, mem + off, sector, len);
322 }
323 kunmap_atomic(mem);
324
325out:
326 return err;
327}
328
329static void brd_make_request(struct request_queue *q, struct bio *bio)
330{
331 struct block_device *bdev = bio->bi_bdev;
332 struct brd_device *brd = bdev->bd_disk->private_data;
333 int rw;
334 struct bio_vec *bvec;
335 sector_t sector;
336 int i;
337 int err = -EIO;
338
339 sector = bio->bi_sector;
340 if (bio_end_sector(bio) > get_capacity(bdev->bd_disk))
341 goto out;
342
343 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
344 err = 0;
345 discard_from_brd(brd, sector, bio->bi_size);
346 goto out;
347 }
348
349 rw = bio_rw(bio);
350 if (rw == READA)
351 rw = READ;
352
353 bio_for_each_segment(bvec, bio, i) {
354 unsigned int len = bvec->bv_len;
355 err = brd_do_bvec(brd, bvec->bv_page, len,
356 bvec->bv_offset, rw, sector);
357 if (err)
358 break;
359 sector += len >> SECTOR_SHIFT;
360 }
361
362out:
363 bio_endio(bio, err);
364}
365
366static int brd_rw_page(struct block_device *bdev, sector_t sector,
367 struct page *page, int rw)
368{
369 struct brd_device *brd = bdev->bd_disk->private_data;
370 int err = brd_do_bvec(brd, page, PAGE_CACHE_SIZE, 0, rw, sector);
371 page_endio(page, rw & WRITE, err);
372 return err;
373}
374
375#ifdef CONFIG_BLK_DEV_RAM_DAX
376static long brd_direct_access(struct block_device *bdev, sector_t sector,
377 void **kaddr, pfn_t *pfn, long size)
378{
379 struct brd_device *brd = bdev->bd_disk->private_data;
380 struct page *page;
381
382 if (!brd)
383 return -ENODEV;
384 page = brd_insert_page(brd, sector);
385 if (!page)
386 return -ENOSPC;
387 *kaddr = page_address(page);
388 *pfn = page_to_pfn_t(page);
389
390 return PAGE_SIZE;
391}
392#else
393#define brd_direct_access NULL
394#endif
395
396static int brd_ioctl(struct block_device *bdev, fmode_t mode,
397 unsigned int cmd, unsigned long arg)
398{
399 int error;
400 struct brd_device *brd = bdev->bd_disk->private_data;
401
402 if (cmd != BLKFLSBUF)
403 return -ENOTTY;
404
405
406
407
408
409 mutex_lock(&brd_mutex);
410 mutex_lock(&bdev->bd_mutex);
411 error = -EBUSY;
412 if (bdev->bd_openers <= 1) {
413
414
415
416
417
418
419
420 kill_bdev(bdev);
421 brd_free_pages(brd);
422 error = 0;
423 }
424 mutex_unlock(&bdev->bd_mutex);
425 mutex_unlock(&brd_mutex);
426
427 return error;
428}
429
430static const struct block_device_operations brd_fops = {
431 .owner = THIS_MODULE,
432 .rw_page = brd_rw_page,
433 .ioctl = brd_ioctl,
434 .direct_access = brd_direct_access,
435};
436
437
438
439
440static int rd_nr;
441int rd_size = CONFIG_BLK_DEV_RAM_SIZE;
442static int max_part;
443static int part_shift;
444module_param(rd_nr, int, S_IRUGO);
445MODULE_PARM_DESC(rd_nr, "Maximum number of brd devices");
446module_param(rd_size, int, S_IRUGO);
447MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
448module_param(max_part, int, S_IRUGO);
449MODULE_PARM_DESC(max_part, "Maximum number of partitions per RAM disk");
450MODULE_LICENSE("GPL");
451MODULE_ALIAS_BLOCKDEV_MAJOR(RAMDISK_MAJOR);
452MODULE_ALIAS("rd");
453
454#ifndef MODULE
455
456static int __init ramdisk_size(char *str)
457{
458 rd_size = simple_strtol(str, NULL, 0);
459 return 1;
460}
461__setup("ramdisk_size=", ramdisk_size);
462#endif
463
464
465
466
467
468static LIST_HEAD(brd_devices);
469static DEFINE_MUTEX(brd_devices_mutex);
470
471static struct brd_device *brd_alloc(int i)
472{
473 struct brd_device *brd;
474 struct gendisk *disk;
475
476 brd = kzalloc(sizeof(*brd), GFP_KERNEL);
477 if (!brd)
478 goto out;
479 brd->brd_number = i;
480 spin_lock_init(&brd->brd_lock);
481 INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC);
482
483 brd->brd_queue = blk_alloc_queue(GFP_KERNEL);
484 if (!brd->brd_queue)
485 goto out_free_dev;
486 blk_queue_make_request(brd->brd_queue, brd_make_request);
487 blk_queue_max_hw_sectors(brd->brd_queue, 1024);
488 blk_queue_bounce_limit(brd->brd_queue, BLK_BOUNCE_ANY);
489
490 brd->brd_queue->limits.discard_granularity = PAGE_SIZE;
491 brd->brd_queue->limits.max_discard_sectors = UINT_MAX;
492 brd->brd_queue->limits.discard_zeroes_data = 1;
493 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, brd->brd_queue);
494#ifdef CONFIG_BLK_DEV_RAM_DAX
495 queue_flag_set_unlocked(QUEUE_FLAG_DAX, brd->brd_queue);
496#endif
497 disk = brd->brd_disk = alloc_disk(1 << part_shift);
498 if (!disk)
499 goto out_free_queue;
500 disk->major = RAMDISK_MAJOR;
501 disk->first_minor = i << part_shift;
502 disk->fops = &brd_fops;
503 disk->private_data = brd;
504 disk->queue = brd->brd_queue;
505 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
506 sprintf(disk->disk_name, "ram%d", i);
507 set_capacity(disk, rd_size * 2);
508
509 return brd;
510
511out_free_queue:
512 blk_cleanup_queue(brd->brd_queue);
513out_free_dev:
514 kfree(brd);
515out:
516 return NULL;
517}
518
519static void brd_free(struct brd_device *brd)
520{
521 put_disk(brd->brd_disk);
522 blk_cleanup_queue(brd->brd_queue);
523 brd_free_pages(brd);
524 kfree(brd);
525}
526
527static struct brd_device *brd_init_one(int i)
528{
529 struct brd_device *brd;
530
531 list_for_each_entry(brd, &brd_devices, brd_list) {
532 if (brd->brd_number == i)
533 goto out;
534 }
535
536 brd = brd_alloc(i);
537 if (brd) {
538 add_disk(brd->brd_disk);
539 list_add_tail(&brd->brd_list, &brd_devices);
540 }
541out:
542 return brd;
543}
544
545static void brd_del_one(struct brd_device *brd)
546{
547 list_del(&brd->brd_list);
548 del_gendisk(brd->brd_disk);
549 brd_free(brd);
550}
551
552static struct kobject *brd_probe(dev_t dev, int *part, void *data)
553{
554 struct brd_device *brd;
555 struct kobject *kobj;
556
557 mutex_lock(&brd_devices_mutex);
558 brd = brd_init_one(MINOR(dev) >> part_shift);
559 kobj = brd ? get_disk(brd->brd_disk) : ERR_PTR(-ENOMEM);
560 mutex_unlock(&brd_devices_mutex);
561
562 *part = 0;
563 return kobj;
564}
565
566static int __init brd_init(void)
567{
568 int i, nr;
569 unsigned long range;
570 struct brd_device *brd, *next;
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587 part_shift = 0;
588 if (max_part > 0) {
589 part_shift = fls(max_part);
590
591
592
593
594
595
596
597
598
599 max_part = (1UL << part_shift) - 1;
600 }
601
602 if ((1UL << part_shift) > DISK_MAX_PARTS)
603 return -EINVAL;
604
605 if (rd_nr > 1UL << (MINORBITS - part_shift))
606 return -EINVAL;
607
608 if (rd_nr) {
609 nr = rd_nr;
610 range = rd_nr << part_shift;
611 } else {
612 nr = CONFIG_BLK_DEV_RAM_COUNT;
613 range = 1UL << MINORBITS;
614 }
615
616 if (register_blkdev(RAMDISK_MAJOR, "ramdisk"))
617 return -EIO;
618
619 for (i = 0; i < nr; i++) {
620 brd = brd_alloc(i);
621 if (!brd)
622 goto out_free;
623 list_add_tail(&brd->brd_list, &brd_devices);
624 }
625
626
627
628 list_for_each_entry(brd, &brd_devices, brd_list)
629 add_disk(brd->brd_disk);
630
631 blk_register_region(MKDEV(RAMDISK_MAJOR, 0), range,
632 THIS_MODULE, brd_probe, NULL, NULL);
633
634 printk(KERN_INFO "brd: module loaded\n");
635 return 0;
636
637out_free:
638 list_for_each_entry_safe(brd, next, &brd_devices, brd_list) {
639 list_del(&brd->brd_list);
640 brd_free(brd);
641 }
642 unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
643
644 return -ENOMEM;
645}
646
647static void __exit brd_exit(void)
648{
649 unsigned long range;
650 struct brd_device *brd, *next;
651
652 range = rd_nr ? rd_nr << part_shift : 1UL << MINORBITS;
653
654 list_for_each_entry_safe(brd, next, &brd_devices, brd_list)
655 brd_del_one(brd);
656
657 blk_unregister_region(MKDEV(RAMDISK_MAJOR, 0), range);
658 unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
659}
660
661module_init(brd_init);
662module_exit(brd_exit);
663
664