Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Ram backed block device driver. |
| 3 | * |
| 4 | * Copyright (C) 2007 Nick Piggin |
| 5 | * Copyright (C) 2007 Novell Inc. |
| 6 | * |
| 7 | * Parts derived from drivers/block/rd.c, and drivers/block/loop.c, copyright |
| 8 | * of their respective owners. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
Bart Van Assche | 287f3ca | 2017-07-10 15:51:10 -0700 | [diff] [blame] | 12 | #include <linux/initrd.h> |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/major.h> |
| 16 | #include <linux/blkdev.h> |
| 17 | #include <linux/bio.h> |
| 18 | #include <linux/highmem.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 19 | #include <linux/mutex.h> |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 20 | #include <linux/radix-tree.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 21 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 23 | #ifdef CONFIG_BLK_DEV_RAM_DAX |
| 24 | #include <linux/pfn_t.h> |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 25 | #include <linux/dax.h> |
Dan Williams | 5d61e43 | 2017-06-27 13:06:22 -0700 | [diff] [blame] | 26 | #include <linux/uio.h> |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 27 | #endif |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 28 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 29 | #include <linux/uaccess.h> |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 30 | |
| 31 | #define SECTOR_SHIFT 9 |
| 32 | #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) |
| 33 | #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT) |
| 34 | |
| 35 | /* |
| 36 | * Each block ramdisk device has a radix_tree brd_pages of pages that stores |
| 37 | * the pages containing the block device's contents. A brd page's ->index is |
| 38 | * its offset in PAGE_SIZE units. This is similar to, but in no way connected |
| 39 | * with, the kernel's pagecache or buffer cache (which sit above our block |
| 40 | * device). |
| 41 | */ |
| 42 | struct brd_device { |
| 43 | int brd_number; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 44 | |
| 45 | struct request_queue *brd_queue; |
| 46 | struct gendisk *brd_disk; |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 47 | #ifdef CONFIG_BLK_DEV_RAM_DAX |
| 48 | struct dax_device *dax_dev; |
| 49 | #endif |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 50 | struct list_head brd_list; |
| 51 | |
| 52 | /* |
| 53 | * Backing store of pages and lock to protect it. This is the contents |
| 54 | * of the block device. |
| 55 | */ |
| 56 | spinlock_t brd_lock; |
| 57 | struct radix_tree_root brd_pages; |
| 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * Look up and return a brd's page for a given sector. |
| 62 | */ |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 63 | static DEFINE_MUTEX(brd_mutex); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 64 | static struct page *brd_lookup_page(struct brd_device *brd, sector_t sector) |
| 65 | { |
| 66 | pgoff_t idx; |
| 67 | struct page *page; |
| 68 | |
| 69 | /* |
| 70 | * The page lifetime is protected by the fact that we have opened the |
| 71 | * device node -- brd pages will never be deleted under us, so we |
| 72 | * don't need any further locking or refcounting. |
| 73 | * |
| 74 | * This is strictly true for the radix-tree nodes as well (ie. we |
| 75 | * don't actually need the rcu_read_lock()), however that is not a |
| 76 | * documented feature of the radix-tree API so it is better to be |
| 77 | * safe here (we don't have total exclusion from radix tree updates |
| 78 | * here, only deletes). |
| 79 | */ |
| 80 | rcu_read_lock(); |
| 81 | idx = sector >> PAGE_SECTORS_SHIFT; /* sector to page index */ |
| 82 | page = radix_tree_lookup(&brd->brd_pages, idx); |
| 83 | rcu_read_unlock(); |
| 84 | |
| 85 | BUG_ON(page && page->index != idx); |
| 86 | |
| 87 | return page; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Look up and return a brd's page for a given sector. |
| 92 | * If one does not exist, allocate an empty page, and insert that. Then |
| 93 | * return it. |
| 94 | */ |
| 95 | static struct page *brd_insert_page(struct brd_device *brd, sector_t sector) |
| 96 | { |
| 97 | pgoff_t idx; |
| 98 | struct page *page; |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 99 | gfp_t gfp_flags; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 100 | |
| 101 | page = brd_lookup_page(brd, sector); |
| 102 | if (page) |
| 103 | return page; |
| 104 | |
| 105 | /* |
| 106 | * Must use NOIO because we don't want to recurse back into the |
| 107 | * block or filesystem layers from page reclaim. |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 108 | * |
Matthew Wilcox | a7a97fc | 2015-02-16 15:59:41 -0800 | [diff] [blame] | 109 | * Cannot support DAX and highmem, because our ->direct_access |
| 110 | * routine for DAX must return memory that is always addressable. |
| 111 | * If DAX was reworked to use pfns and kmap throughout, this |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 112 | * restriction might be able to be lifted. |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 113 | */ |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 114 | gfp_flags = GFP_NOIO | __GFP_ZERO; |
Matthew Wilcox | a7a97fc | 2015-02-16 15:59:41 -0800 | [diff] [blame] | 115 | #ifndef CONFIG_BLK_DEV_RAM_DAX |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 116 | gfp_flags |= __GFP_HIGHMEM; |
| 117 | #endif |
Petr Tesarik | 26defe3 | 2008-04-22 05:36:52 +0200 | [diff] [blame] | 118 | page = alloc_page(gfp_flags); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 119 | if (!page) |
| 120 | return NULL; |
| 121 | |
| 122 | if (radix_tree_preload(GFP_NOIO)) { |
| 123 | __free_page(page); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | spin_lock(&brd->brd_lock); |
| 128 | idx = sector >> PAGE_SECTORS_SHIFT; |
Brian Behlendorf | dfd20b2 | 2013-05-24 15:55:28 -0700 | [diff] [blame] | 129 | page->index = idx; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 130 | if (radix_tree_insert(&brd->brd_pages, idx, page)) { |
| 131 | __free_page(page); |
| 132 | page = radix_tree_lookup(&brd->brd_pages, idx); |
| 133 | BUG_ON(!page); |
| 134 | BUG_ON(page->index != idx); |
Brian Behlendorf | dfd20b2 | 2013-05-24 15:55:28 -0700 | [diff] [blame] | 135 | } |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 136 | spin_unlock(&brd->brd_lock); |
| 137 | |
| 138 | radix_tree_preload_end(); |
| 139 | |
| 140 | return page; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Free all backing store pages and radix tree. This must only be called when |
| 145 | * there are no other users of the device. |
| 146 | */ |
| 147 | #define FREE_BATCH 16 |
| 148 | static void brd_free_pages(struct brd_device *brd) |
| 149 | { |
| 150 | unsigned long pos = 0; |
| 151 | struct page *pages[FREE_BATCH]; |
| 152 | int nr_pages; |
| 153 | |
| 154 | do { |
| 155 | int i; |
| 156 | |
| 157 | nr_pages = radix_tree_gang_lookup(&brd->brd_pages, |
| 158 | (void **)pages, pos, FREE_BATCH); |
| 159 | |
| 160 | for (i = 0; i < nr_pages; i++) { |
| 161 | void *ret; |
| 162 | |
| 163 | BUG_ON(pages[i]->index < pos); |
| 164 | pos = pages[i]->index; |
| 165 | ret = radix_tree_delete(&brd->brd_pages, pos); |
| 166 | BUG_ON(!ret || ret != pages[i]); |
| 167 | __free_page(pages[i]); |
| 168 | } |
| 169 | |
| 170 | pos++; |
| 171 | |
| 172 | /* |
| 173 | * This assumes radix_tree_gang_lookup always returns as |
| 174 | * many pages as possible. If the radix-tree code changes, |
| 175 | * so will this have to. |
| 176 | */ |
| 177 | } while (nr_pages == FREE_BATCH); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * copy_to_brd_setup must be called before copy_to_brd. It may sleep. |
| 182 | */ |
| 183 | static int copy_to_brd_setup(struct brd_device *brd, sector_t sector, size_t n) |
| 184 | { |
| 185 | unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT; |
| 186 | size_t copy; |
| 187 | |
| 188 | copy = min_t(size_t, n, PAGE_SIZE - offset); |
| 189 | if (!brd_insert_page(brd, sector)) |
Matthew Wilcox | 96f8d8e | 2014-06-04 16:07:50 -0700 | [diff] [blame] | 190 | return -ENOSPC; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 191 | if (copy < n) { |
| 192 | sector += copy >> SECTOR_SHIFT; |
| 193 | if (!brd_insert_page(brd, sector)) |
Matthew Wilcox | 96f8d8e | 2014-06-04 16:07:50 -0700 | [diff] [blame] | 194 | return -ENOSPC; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Copy n bytes from src to the brd starting at sector. Does not sleep. |
| 201 | */ |
| 202 | static void copy_to_brd(struct brd_device *brd, const void *src, |
| 203 | sector_t sector, size_t n) |
| 204 | { |
| 205 | struct page *page; |
| 206 | void *dst; |
| 207 | unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT; |
| 208 | size_t copy; |
| 209 | |
| 210 | copy = min_t(size_t, n, PAGE_SIZE - offset); |
| 211 | page = brd_lookup_page(brd, sector); |
| 212 | BUG_ON(!page); |
| 213 | |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 214 | dst = kmap_atomic(page); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 215 | memcpy(dst + offset, src, copy); |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 216 | kunmap_atomic(dst); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 217 | |
| 218 | if (copy < n) { |
| 219 | src += copy; |
| 220 | sector += copy >> SECTOR_SHIFT; |
| 221 | copy = n - copy; |
| 222 | page = brd_lookup_page(brd, sector); |
| 223 | BUG_ON(!page); |
| 224 | |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 225 | dst = kmap_atomic(page); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 226 | memcpy(dst, src, copy); |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 227 | kunmap_atomic(dst); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Copy n bytes to dst from the brd starting at sector. Does not sleep. |
| 233 | */ |
| 234 | static void copy_from_brd(void *dst, struct brd_device *brd, |
| 235 | sector_t sector, size_t n) |
| 236 | { |
| 237 | struct page *page; |
| 238 | void *src; |
| 239 | unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT; |
| 240 | size_t copy; |
| 241 | |
| 242 | copy = min_t(size_t, n, PAGE_SIZE - offset); |
| 243 | page = brd_lookup_page(brd, sector); |
| 244 | if (page) { |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 245 | src = kmap_atomic(page); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 246 | memcpy(dst, src + offset, copy); |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 247 | kunmap_atomic(src); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 248 | } else |
| 249 | memset(dst, 0, copy); |
| 250 | |
| 251 | if (copy < n) { |
| 252 | dst += copy; |
| 253 | sector += copy >> SECTOR_SHIFT; |
| 254 | copy = n - copy; |
| 255 | page = brd_lookup_page(brd, sector); |
| 256 | if (page) { |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 257 | src = kmap_atomic(page); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 258 | memcpy(dst, src, copy); |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 259 | kunmap_atomic(src); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 260 | } else |
| 261 | memset(dst, 0, copy); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Process a single bvec of a bio. |
| 267 | */ |
| 268 | static int brd_do_bvec(struct brd_device *brd, struct page *page, |
Jens Axboe | c11f0c0 | 2016-08-05 08:11:04 -0600 | [diff] [blame] | 269 | unsigned int len, unsigned int off, bool is_write, |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 270 | sector_t sector) |
| 271 | { |
| 272 | void *mem; |
| 273 | int err = 0; |
| 274 | |
Jens Axboe | c11f0c0 | 2016-08-05 08:11:04 -0600 | [diff] [blame] | 275 | if (is_write) { |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 276 | err = copy_to_brd_setup(brd, sector, len); |
| 277 | if (err) |
| 278 | goto out; |
| 279 | } |
| 280 | |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 281 | mem = kmap_atomic(page); |
Jens Axboe | c11f0c0 | 2016-08-05 08:11:04 -0600 | [diff] [blame] | 282 | if (!is_write) { |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 283 | copy_from_brd(mem + off, brd, sector, len); |
| 284 | flush_dcache_page(page); |
Nick Piggin | c2572f2 | 2009-04-15 10:32:07 +0200 | [diff] [blame] | 285 | } else { |
| 286 | flush_dcache_page(page); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 287 | copy_to_brd(brd, mem + off, sector, len); |
Nick Piggin | c2572f2 | 2009-04-15 10:32:07 +0200 | [diff] [blame] | 288 | } |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 289 | kunmap_atomic(mem); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 290 | |
| 291 | out: |
| 292 | return err; |
| 293 | } |
| 294 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 295 | static blk_qc_t brd_make_request(struct request_queue *q, struct bio *bio) |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 296 | { |
| 297 | struct block_device *bdev = bio->bi_bdev; |
| 298 | struct brd_device *brd = bdev->bd_disk->private_data; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 299 | struct bio_vec bvec; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 300 | sector_t sector; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 301 | struct bvec_iter iter; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 302 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 303 | sector = bio->bi_iter.bi_sector; |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 304 | if (bio_end_sector(bio) > get_capacity(bdev->bd_disk)) |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 305 | goto io_error; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 306 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 307 | bio_for_each_segment(bvec, bio, iter) { |
| 308 | unsigned int len = bvec.bv_len; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 309 | int err; |
| 310 | |
Jens Axboe | c11f0c0 | 2016-08-05 08:11:04 -0600 | [diff] [blame] | 311 | err = brd_do_bvec(brd, bvec.bv_page, len, bvec.bv_offset, |
| 312 | op_is_write(bio_op(bio)), sector); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 313 | if (err) |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 314 | goto io_error; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 315 | sector += len >> SECTOR_SHIFT; |
| 316 | } |
| 317 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 318 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 319 | return BLK_QC_T_NONE; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 320 | io_error: |
| 321 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 322 | return BLK_QC_T_NONE; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Matthew Wilcox | a72132c | 2014-06-04 16:07:49 -0700 | [diff] [blame] | 325 | static int brd_rw_page(struct block_device *bdev, sector_t sector, |
Jens Axboe | c11f0c0 | 2016-08-05 08:11:04 -0600 | [diff] [blame] | 326 | struct page *page, bool is_write) |
Matthew Wilcox | a72132c | 2014-06-04 16:07:49 -0700 | [diff] [blame] | 327 | { |
| 328 | struct brd_device *brd = bdev->bd_disk->private_data; |
Huang Ying | 98cc093 | 2017-09-06 16:22:27 -0700 | [diff] [blame^] | 329 | int err; |
| 330 | |
| 331 | if (PageTransHuge(page)) |
| 332 | return -ENOTSUPP; |
| 333 | err = brd_do_bvec(brd, page, PAGE_SIZE, 0, is_write, sector); |
Jens Axboe | c11f0c0 | 2016-08-05 08:11:04 -0600 | [diff] [blame] | 334 | page_endio(page, is_write, err); |
Matthew Wilcox | a72132c | 2014-06-04 16:07:49 -0700 | [diff] [blame] | 335 | return err; |
| 336 | } |
| 337 | |
Matthew Wilcox | a7a97fc | 2015-02-16 15:59:41 -0800 | [diff] [blame] | 338 | #ifdef CONFIG_BLK_DEV_RAM_DAX |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 339 | static long __brd_direct_access(struct brd_device *brd, pgoff_t pgoff, |
| 340 | long nr_pages, void **kaddr, pfn_t *pfn) |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 341 | { |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 342 | struct page *page; |
| 343 | |
| 344 | if (!brd) |
| 345 | return -ENODEV; |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 346 | page = brd_insert_page(brd, PFN_PHYS(pgoff) / 512); |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 347 | if (!page) |
Matthew Wilcox | 96f8d8e | 2014-06-04 16:07:50 -0700 | [diff] [blame] | 348 | return -ENOSPC; |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 349 | *kaddr = page_address(page); |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 350 | *pfn = page_to_pfn_t(page); |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 351 | |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 352 | return 1; |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 353 | } |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 354 | |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 355 | static long brd_dax_direct_access(struct dax_device *dax_dev, |
| 356 | pgoff_t pgoff, long nr_pages, void **kaddr, pfn_t *pfn) |
| 357 | { |
| 358 | struct brd_device *brd = dax_get_private(dax_dev); |
| 359 | |
| 360 | return __brd_direct_access(brd, pgoff, nr_pages, kaddr, pfn); |
| 361 | } |
| 362 | |
Dan Williams | 5d61e43 | 2017-06-27 13:06:22 -0700 | [diff] [blame] | 363 | static size_t brd_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, |
| 364 | void *addr, size_t bytes, struct iov_iter *i) |
| 365 | { |
| 366 | return copy_from_iter(addr, bytes, i); |
| 367 | } |
| 368 | |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 369 | static const struct dax_operations brd_dax_ops = { |
| 370 | .direct_access = brd_dax_direct_access, |
Dan Williams | 5d61e43 | 2017-06-27 13:06:22 -0700 | [diff] [blame] | 371 | .copy_from_iter = brd_dax_copy_from_iter, |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 372 | }; |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 373 | #endif |
| 374 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 375 | static const struct block_device_operations brd_fops = { |
Nick Piggin | 75acb9c | 2008-02-08 04:19:50 -0800 | [diff] [blame] | 376 | .owner = THIS_MODULE, |
Matthew Wilcox | a72132c | 2014-06-04 16:07:49 -0700 | [diff] [blame] | 377 | .rw_page = brd_rw_page, |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | /* |
| 381 | * And now the modules code and kernel interface. |
| 382 | */ |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 383 | static int rd_nr = CONFIG_BLK_DEV_RAM_COUNT; |
Namhyung Kim | 8892cba | 2011-05-26 21:06:50 +0200 | [diff] [blame] | 384 | module_param(rd_nr, int, S_IRUGO); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 385 | MODULE_PARM_DESC(rd_nr, "Maximum number of brd devices"); |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 386 | |
Jan Kara | 366f4ae | 2016-10-25 13:53:27 +0200 | [diff] [blame] | 387 | unsigned long rd_size = CONFIG_BLK_DEV_RAM_SIZE; |
| 388 | module_param(rd_size, ulong, S_IRUGO); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 389 | MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes."); |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 390 | |
| 391 | static int max_part = 1; |
Namhyung Kim | 8892cba | 2011-05-26 21:06:50 +0200 | [diff] [blame] | 392 | module_param(max_part, int, S_IRUGO); |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 393 | MODULE_PARM_DESC(max_part, "Num Minors to reserve between devices"); |
| 394 | |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 395 | MODULE_LICENSE("GPL"); |
| 396 | MODULE_ALIAS_BLOCKDEV_MAJOR(RAMDISK_MAJOR); |
Nick Piggin | efedf51 | 2008-06-04 17:18:42 +0200 | [diff] [blame] | 397 | MODULE_ALIAS("rd"); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 398 | |
| 399 | #ifndef MODULE |
| 400 | /* Legacy boot options - nonmodular */ |
| 401 | static int __init ramdisk_size(char *str) |
| 402 | { |
| 403 | rd_size = simple_strtol(str, NULL, 0); |
| 404 | return 1; |
| 405 | } |
Robert P. J. Day | 1adbee5 | 2009-06-10 12:57:08 -0700 | [diff] [blame] | 406 | __setup("ramdisk_size=", ramdisk_size); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 407 | #endif |
| 408 | |
| 409 | /* |
| 410 | * The device scheme is derived from loop.c. Keep them in synch where possible |
| 411 | * (should share code eventually). |
| 412 | */ |
| 413 | static LIST_HEAD(brd_devices); |
| 414 | static DEFINE_MUTEX(brd_devices_mutex); |
| 415 | |
| 416 | static struct brd_device *brd_alloc(int i) |
| 417 | { |
| 418 | struct brd_device *brd; |
| 419 | struct gendisk *disk; |
| 420 | |
| 421 | brd = kzalloc(sizeof(*brd), GFP_KERNEL); |
| 422 | if (!brd) |
| 423 | goto out; |
| 424 | brd->brd_number = i; |
| 425 | spin_lock_init(&brd->brd_lock); |
| 426 | INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC); |
| 427 | |
| 428 | brd->brd_queue = blk_alloc_queue(GFP_KERNEL); |
| 429 | if (!brd->brd_queue) |
| 430 | goto out_free_dev; |
Boaz Harrosh | c8fa317 | 2015-01-07 18:09:38 +0200 | [diff] [blame] | 431 | |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 432 | blk_queue_make_request(brd->brd_queue, brd_make_request); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 433 | blk_queue_max_hw_sectors(brd->brd_queue, 1024); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 434 | |
Boaz Harrosh | c8fa317 | 2015-01-07 18:09:38 +0200 | [diff] [blame] | 435 | /* This is so fdisk will align partitions on 4k, because of |
| 436 | * direct_access API needing 4k alignment, returning a PFN |
| 437 | * (This is only a problem on very small devices <= 4M, |
| 438 | * otherwise fdisk will align on 1M. Regardless this call |
| 439 | * is harmless) |
| 440 | */ |
| 441 | blk_queue_physical_block_size(brd->brd_queue, PAGE_SIZE); |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 442 | disk = brd->brd_disk = alloc_disk(max_part); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 443 | if (!disk) |
| 444 | goto out_free_queue; |
| 445 | disk->major = RAMDISK_MAJOR; |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 446 | disk->first_minor = i * max_part; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 447 | disk->fops = &brd_fops; |
| 448 | disk->private_data = brd; |
| 449 | disk->queue = brd->brd_queue; |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 450 | disk->flags = GENHD_FL_EXT_DEVT; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 451 | sprintf(disk->disk_name, "ram%d", i); |
| 452 | set_capacity(disk, rd_size * 2); |
| 453 | |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 454 | #ifdef CONFIG_BLK_DEV_RAM_DAX |
| 455 | queue_flag_set_unlocked(QUEUE_FLAG_DAX, brd->brd_queue); |
Gerald Schaefer | 1ef97fe | 2017-05-03 14:56:02 +0200 | [diff] [blame] | 456 | brd->dax_dev = alloc_dax(brd, disk->disk_name, &brd_dax_ops); |
| 457 | if (!brd->dax_dev) |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 458 | goto out_free_inode; |
| 459 | #endif |
| 460 | |
| 461 | |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 462 | return brd; |
| 463 | |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 464 | #ifdef CONFIG_BLK_DEV_RAM_DAX |
| 465 | out_free_inode: |
Gerald Schaefer | 1ef97fe | 2017-05-03 14:56:02 +0200 | [diff] [blame] | 466 | kill_dax(brd->dax_dev); |
| 467 | put_dax(brd->dax_dev); |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 468 | #endif |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 469 | out_free_queue: |
| 470 | blk_cleanup_queue(brd->brd_queue); |
| 471 | out_free_dev: |
| 472 | kfree(brd); |
| 473 | out: |
| 474 | return NULL; |
| 475 | } |
| 476 | |
| 477 | static void brd_free(struct brd_device *brd) |
| 478 | { |
| 479 | put_disk(brd->brd_disk); |
| 480 | blk_cleanup_queue(brd->brd_queue); |
| 481 | brd_free_pages(brd); |
| 482 | kfree(brd); |
| 483 | } |
| 484 | |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 485 | static struct brd_device *brd_init_one(int i, bool *new) |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 486 | { |
| 487 | struct brd_device *brd; |
| 488 | |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 489 | *new = false; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 490 | list_for_each_entry(brd, &brd_devices, brd_list) { |
| 491 | if (brd->brd_number == i) |
| 492 | goto out; |
| 493 | } |
| 494 | |
| 495 | brd = brd_alloc(i); |
| 496 | if (brd) { |
| 497 | add_disk(brd->brd_disk); |
| 498 | list_add_tail(&brd->brd_list, &brd_devices); |
| 499 | } |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 500 | *new = true; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 501 | out: |
| 502 | return brd; |
| 503 | } |
| 504 | |
| 505 | static void brd_del_one(struct brd_device *brd) |
| 506 | { |
| 507 | list_del(&brd->brd_list); |
Dan Williams | 1647b9b | 2017-01-25 16:54:45 -0800 | [diff] [blame] | 508 | #ifdef CONFIG_BLK_DEV_RAM_DAX |
| 509 | kill_dax(brd->dax_dev); |
| 510 | put_dax(brd->dax_dev); |
| 511 | #endif |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 512 | del_gendisk(brd->brd_disk); |
| 513 | brd_free(brd); |
| 514 | } |
| 515 | |
| 516 | static struct kobject *brd_probe(dev_t dev, int *part, void *data) |
| 517 | { |
| 518 | struct brd_device *brd; |
| 519 | struct kobject *kobj; |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 520 | bool new; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 521 | |
| 522 | mutex_lock(&brd_devices_mutex); |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 523 | brd = brd_init_one(MINOR(dev) / max_part, &new); |
Mikulas Patocka | a207f59 | 2013-10-14 12:13:24 -0400 | [diff] [blame] | 524 | kobj = brd ? get_disk(brd->brd_disk) : NULL; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 525 | mutex_unlock(&brd_devices_mutex); |
| 526 | |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 527 | if (new) |
| 528 | *part = 0; |
| 529 | |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 530 | return kobj; |
| 531 | } |
| 532 | |
| 533 | static int __init brd_init(void) |
| 534 | { |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 535 | struct brd_device *brd, *next; |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 536 | int i; |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | * brd module now has a feature to instantiate underlying device |
| 540 | * structure on-demand, provided that there is an access dev node. |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 541 | * |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 542 | * (1) if rd_nr is specified, create that many upfront. else |
| 543 | * it defaults to CONFIG_BLK_DEV_RAM_COUNT |
| 544 | * (2) User can further extend brd devices by create dev node themselves |
| 545 | * and have kernel automatically instantiate actual device |
| 546 | * on-demand. Example: |
| 547 | * mknod /path/devnod_name b 1 X # 1 is the rd major |
| 548 | * fdisk -l /path/devnod_name |
| 549 | * If (X / max_part) was not already created it will be created |
| 550 | * dynamically. |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 551 | */ |
Laurent Vivier | d7853d1 | 2008-04-30 00:55:06 -0700 | [diff] [blame] | 552 | |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 553 | if (register_blkdev(RAMDISK_MAJOR, "ramdisk")) |
| 554 | return -EIO; |
| 555 | |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 556 | if (unlikely(!max_part)) |
| 557 | max_part = 1; |
| 558 | |
| 559 | for (i = 0; i < rd_nr; i++) { |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 560 | brd = brd_alloc(i); |
| 561 | if (!brd) |
| 562 | goto out_free; |
| 563 | list_add_tail(&brd->brd_list, &brd_devices); |
| 564 | } |
| 565 | |
| 566 | /* point of no return */ |
| 567 | |
| 568 | list_for_each_entry(brd, &brd_devices, brd_list) |
| 569 | add_disk(brd->brd_disk); |
| 570 | |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 571 | blk_register_region(MKDEV(RAMDISK_MAJOR, 0), 1UL << MINORBITS, |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 572 | THIS_MODULE, brd_probe, NULL, NULL); |
| 573 | |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 574 | pr_info("brd: module loaded\n"); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 575 | return 0; |
| 576 | |
| 577 | out_free: |
| 578 | list_for_each_entry_safe(brd, next, &brd_devices, brd_list) { |
| 579 | list_del(&brd->brd_list); |
| 580 | brd_free(brd); |
| 581 | } |
Akinobu Mita | c82f296 | 2008-08-20 14:09:09 -0700 | [diff] [blame] | 582 | unregister_blkdev(RAMDISK_MAJOR, "ramdisk"); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 583 | |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 584 | pr_info("brd: module NOT loaded !!!\n"); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 585 | return -ENOMEM; |
| 586 | } |
| 587 | |
| 588 | static void __exit brd_exit(void) |
| 589 | { |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 590 | struct brd_device *brd, *next; |
| 591 | |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 592 | list_for_each_entry_safe(brd, next, &brd_devices, brd_list) |
| 593 | brd_del_one(brd); |
| 594 | |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 595 | blk_unregister_region(MKDEV(RAMDISK_MAJOR, 0), 1UL << MINORBITS); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 596 | unregister_blkdev(RAMDISK_MAJOR, "ramdisk"); |
Boaz Harrosh | 937af5e | 2015-01-07 18:07:56 +0200 | [diff] [blame] | 597 | |
| 598 | pr_info("brd: module unloaded\n"); |
Nick Piggin | 9db5579 | 2008-02-08 04:19:49 -0800 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | module_init(brd_init); |
| 602 | module_exit(brd_exit); |
| 603 | |