12 #include <linux/module.h>
21 #include <linux/slab.h>
23 #include <asm/uaccess.h>
25 #define SECTOR_SHIFT 9
26 #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
27 #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
92 page = brd_lookup_page(brd, sector);
106 #ifndef CONFIG_BLK_DEV_XIP
129 radix_tree_preload_end();
151 page = brd_lookup_page(brd, sector);
153 clear_highpage(page);
160 #define FREE_BATCH 16
161 static void brd_free_pages(
struct brd_device *brd)
163 unsigned long pos = 0;
173 for (i = 0; i < nr_pages; i++) {
179 BUG_ON(!ret || ret != pages[i]);
202 if (!brd_insert_page(brd, sector))
206 if (!brd_insert_page(brd, sector))
212 static void discard_from_brd(
struct brd_device *brd,
222 brd_free_page(brd, sector);
224 brd_zero_page(brd, sector);
233 static void copy_to_brd(
struct brd_device *brd,
const void *
src,
242 page = brd_lookup_page(brd, sector);
246 memcpy(dst + offset, src, copy);
253 page = brd_lookup_page(brd, sector);
265 static void copy_from_brd(
void *dst,
struct brd_device *brd,
274 page = brd_lookup_page(brd, sector);
277 memcpy(dst, src + offset, copy);
286 page = brd_lookup_page(brd, sector);
299 static int brd_do_bvec(
struct brd_device *brd,
struct page *page,
300 unsigned int len,
unsigned int off,
int rw,
307 err = copy_to_brd_setup(brd, sector, len);
314 copy_from_brd(mem + off, brd, sector, len);
318 copy_to_brd(brd, mem + off, sector, len);
326 static void brd_make_request(
struct request_queue *
q,
struct bio *bio)
331 struct bio_vec *bvec;
336 sector = bio->bi_sector;
337 if (sector + (bio->bi_size >> SECTOR_SHIFT) >
343 discard_from_brd(brd, sector, bio->bi_size);
351 bio_for_each_segment(bvec, bio, i) {
352 unsigned int len = bvec->bv_len;
353 err = brd_do_bvec(brd, bvec->bv_page, len,
354 bvec->bv_offset, rw, sector);
364 #ifdef CONFIG_BLK_DEV_XIP
366 void **kaddr,
unsigned long *pfn)
377 page = brd_insert_page(brd, sector);
388 unsigned int cmd,
unsigned long arg)
421 static const struct block_device_operations brd_fops = {
424 #ifdef CONFIG_BLK_DEV_XIP
425 .direct_access = brd_direct_access,
435 static int part_shift;
466 struct gendisk *disk;
484 brd->
brd_queue->limits.discard_zeroes_data = 1;
485 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, brd->
brd_queue);
491 disk->first_minor = i << part_shift;
492 disk->fops = &brd_fops;
493 disk->private_data = brd;
495 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
496 sprintf(disk->disk_name,
"ram%d", i);
497 set_capacity(disk, rd_size * 2);
535 static void brd_del_one(
struct brd_device *brd)
548 brd = brd_init_one(
MINOR(dev) >> part_shift);
556 static int __init brd_init(
void)
579 part_shift = fls(max_part);
589 max_part = (1
UL << part_shift) - 1;
592 if ((1
UL << part_shift) > DISK_MAX_PARTS)
600 range = rd_nr << part_shift;
602 nr = CONFIG_BLK_DEV_RAM_COUNT;
609 for (i = 0; i <
nr; i++) {
637 static void __exit brd_exit(
void)
642 range = rd_nr ? rd_nr << part_shift : 1
UL <<
MINORBITS;