brd.c 15.1 KB
Newer Older
N
Nick Piggin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Ram backed block device driver.
 *
 * Copyright (C) 2007 Nick Piggin
 * Copyright (C) 2007 Novell Inc.
 *
 * Parts derived from drivers/block/rd.c, and drivers/block/loop.c, copyright
 * of their respective owners.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/major.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/highmem.h>
18
#include <linux/mutex.h>
N
Nick Piggin 已提交
19
#include <linux/radix-tree.h>
A
Al Viro 已提交
20
#include <linux/fs.h>
21
#include <linux/slab.h>
D
Dan Williams 已提交
22 23 24
#ifdef CONFIG_BLK_DEV_RAM_DAX
#include <linux/pfn_t.h>
#endif
N
Nick Piggin 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

#include <asm/uaccess.h>

#define SECTOR_SHIFT		9
#define PAGE_SECTORS_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
#define PAGE_SECTORS		(1 << PAGE_SECTORS_SHIFT)

/*
 * Each block ramdisk device has a radix_tree brd_pages of pages that stores
 * the pages containing the block device's contents. A brd page's ->index is
 * its offset in PAGE_SIZE units. This is similar to, but in no way connected
 * with, the kernel's pagecache or buffer cache (which sit above our block
 * device).
 */
struct brd_device {
	int		brd_number;

	struct request_queue	*brd_queue;
	struct gendisk		*brd_disk;
	struct list_head	brd_list;

	/*
	 * Backing store of pages and lock to protect it. This is the contents
	 * of the block device.
	 */
	spinlock_t		brd_lock;
	struct radix_tree_root	brd_pages;
};

/*
 * Look up and return a brd's page for a given sector.
 */
57
static DEFINE_MUTEX(brd_mutex);
N
Nick Piggin 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static struct page *brd_lookup_page(struct brd_device *brd, sector_t sector)
{
	pgoff_t idx;
	struct page *page;

	/*
	 * The page lifetime is protected by the fact that we have opened the
	 * device node -- brd pages will never be deleted under us, so we
	 * don't need any further locking or refcounting.
	 *
	 * This is strictly true for the radix-tree nodes as well (ie. we
	 * don't actually need the rcu_read_lock()), however that is not a
	 * documented feature of the radix-tree API so it is better to be
	 * safe here (we don't have total exclusion from radix tree updates
	 * here, only deletes).
	 */
	rcu_read_lock();
	idx = sector >> PAGE_SECTORS_SHIFT; /* sector to page index */
	page = radix_tree_lookup(&brd->brd_pages, idx);
	rcu_read_unlock();

	BUG_ON(page && page->index != idx);

	return page;
}

/*
 * Look up and return a brd's page for a given sector.
 * If one does not exist, allocate an empty page, and insert that. Then
 * return it.
 */
static struct page *brd_insert_page(struct brd_device *brd, sector_t sector)
{
	pgoff_t idx;
	struct page *page;
N
Nick Piggin 已提交
93
	gfp_t gfp_flags;
N
Nick Piggin 已提交
94 95 96 97 98 99 100 101

	page = brd_lookup_page(brd, sector);
	if (page)
		return page;

	/*
	 * Must use NOIO because we don't want to recurse back into the
	 * block or filesystem layers from page reclaim.
N
Nick Piggin 已提交
102
	 *
M
Matthew Wilcox 已提交
103 104 105
	 * Cannot support DAX and highmem, because our ->direct_access
	 * routine for DAX must return memory that is always addressable.
	 * If DAX was reworked to use pfns and kmap throughout, this
N
Nick Piggin 已提交
106
	 * restriction might be able to be lifted.
N
Nick Piggin 已提交
107
	 */
N
Nick Piggin 已提交
108
	gfp_flags = GFP_NOIO | __GFP_ZERO;
M
Matthew Wilcox 已提交
109
#ifndef CONFIG_BLK_DEV_RAM_DAX
N
Nick Piggin 已提交
110 111
	gfp_flags |= __GFP_HIGHMEM;
#endif
P
Petr Tesarik 已提交
112
	page = alloc_page(gfp_flags);
N
Nick Piggin 已提交
113 114 115 116 117 118 119 120 121 122
	if (!page)
		return NULL;

	if (radix_tree_preload(GFP_NOIO)) {
		__free_page(page);
		return NULL;
	}

	spin_lock(&brd->brd_lock);
	idx = sector >> PAGE_SECTORS_SHIFT;
123
	page->index = idx;
N
Nick Piggin 已提交
124 125 126 127 128
	if (radix_tree_insert(&brd->brd_pages, idx, page)) {
		__free_page(page);
		page = radix_tree_lookup(&brd->brd_pages, idx);
		BUG_ON(!page);
		BUG_ON(page->index != idx);
129
	}
N
Nick Piggin 已提交
130 131 132 133 134 135 136
	spin_unlock(&brd->brd_lock);

	radix_tree_preload_end();

	return page;
}

N
Nick Piggin 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
static void brd_free_page(struct brd_device *brd, sector_t sector)
{
	struct page *page;
	pgoff_t idx;

	spin_lock(&brd->brd_lock);
	idx = sector >> PAGE_SECTORS_SHIFT;
	page = radix_tree_delete(&brd->brd_pages, idx);
	spin_unlock(&brd->brd_lock);
	if (page)
		__free_page(page);
}

static void brd_zero_page(struct brd_device *brd, sector_t sector)
{
	struct page *page;

	page = brd_lookup_page(brd, sector);
	if (page)
		clear_highpage(page);
}

N
Nick Piggin 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
/*
 * Free all backing store pages and radix tree. This must only be called when
 * there are no other users of the device.
 */
#define FREE_BATCH 16
static void brd_free_pages(struct brd_device *brd)
{
	unsigned long pos = 0;
	struct page *pages[FREE_BATCH];
	int nr_pages;

	do {
		int i;

		nr_pages = radix_tree_gang_lookup(&brd->brd_pages,
				(void **)pages, pos, FREE_BATCH);

		for (i = 0; i < nr_pages; i++) {
			void *ret;

			BUG_ON(pages[i]->index < pos);
			pos = pages[i]->index;
			ret = radix_tree_delete(&brd->brd_pages, pos);
			BUG_ON(!ret || ret != pages[i]);
			__free_page(pages[i]);
		}

		pos++;

		/*
		 * This assumes radix_tree_gang_lookup always returns as
		 * many pages as possible. If the radix-tree code changes,
		 * so will this have to.
		 */
	} while (nr_pages == FREE_BATCH);
}

/*
 * copy_to_brd_setup must be called before copy_to_brd. It may sleep.
 */
static int copy_to_brd_setup(struct brd_device *brd, sector_t sector, size_t n)
{
	unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
	size_t copy;

	copy = min_t(size_t, n, PAGE_SIZE - offset);
	if (!brd_insert_page(brd, sector))
206
		return -ENOSPC;
N
Nick Piggin 已提交
207 208 209
	if (copy < n) {
		sector += copy >> SECTOR_SHIFT;
		if (!brd_insert_page(brd, sector))
210
			return -ENOSPC;
N
Nick Piggin 已提交
211 212 213 214
	}
	return 0;
}

N
Nick Piggin 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
static void discard_from_brd(struct brd_device *brd,
			sector_t sector, size_t n)
{
	while (n >= PAGE_SIZE) {
		/*
		 * Don't want to actually discard pages here because
		 * re-allocating the pages can result in writeback
		 * deadlocks under heavy load.
		 */
		if (0)
			brd_free_page(brd, sector);
		else
			brd_zero_page(brd, sector);
		sector += PAGE_SIZE >> SECTOR_SHIFT;
		n -= PAGE_SIZE;
	}
}

N
Nick Piggin 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
/*
 * Copy n bytes from src to the brd starting at sector. Does not sleep.
 */
static void copy_to_brd(struct brd_device *brd, const void *src,
			sector_t sector, size_t n)
{
	struct page *page;
	void *dst;
	unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
	size_t copy;

	copy = min_t(size_t, n, PAGE_SIZE - offset);
	page = brd_lookup_page(brd, sector);
	BUG_ON(!page);

248
	dst = kmap_atomic(page);
N
Nick Piggin 已提交
249
	memcpy(dst + offset, src, copy);
250
	kunmap_atomic(dst);
N
Nick Piggin 已提交
251 252 253 254 255 256 257 258

	if (copy < n) {
		src += copy;
		sector += copy >> SECTOR_SHIFT;
		copy = n - copy;
		page = brd_lookup_page(brd, sector);
		BUG_ON(!page);

259
		dst = kmap_atomic(page);
N
Nick Piggin 已提交
260
		memcpy(dst, src, copy);
261
		kunmap_atomic(dst);
N
Nick Piggin 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	}
}

/*
 * Copy n bytes to dst from the brd starting at sector. Does not sleep.
 */
static void copy_from_brd(void *dst, struct brd_device *brd,
			sector_t sector, size_t n)
{
	struct page *page;
	void *src;
	unsigned int offset = (sector & (PAGE_SECTORS-1)) << SECTOR_SHIFT;
	size_t copy;

	copy = min_t(size_t, n, PAGE_SIZE - offset);
	page = brd_lookup_page(brd, sector);
	if (page) {
279
		src = kmap_atomic(page);
N
Nick Piggin 已提交
280
		memcpy(dst, src + offset, copy);
281
		kunmap_atomic(src);
N
Nick Piggin 已提交
282 283 284 285 286 287 288 289 290
	} else
		memset(dst, 0, copy);

	if (copy < n) {
		dst += copy;
		sector += copy >> SECTOR_SHIFT;
		copy = n - copy;
		page = brd_lookup_page(brd, sector);
		if (page) {
291
			src = kmap_atomic(page);
N
Nick Piggin 已提交
292
			memcpy(dst, src, copy);
293
			kunmap_atomic(src);
N
Nick Piggin 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
		} else
			memset(dst, 0, copy);
	}
}

/*
 * Process a single bvec of a bio.
 */
static int brd_do_bvec(struct brd_device *brd, struct page *page,
			unsigned int len, unsigned int off, int rw,
			sector_t sector)
{
	void *mem;
	int err = 0;

	if (rw != READ) {
		err = copy_to_brd_setup(brd, sector, len);
		if (err)
			goto out;
	}

315
	mem = kmap_atomic(page);
N
Nick Piggin 已提交
316 317 318
	if (rw == READ) {
		copy_from_brd(mem + off, brd, sector, len);
		flush_dcache_page(page);
N
Nick Piggin 已提交
319 320
	} else {
		flush_dcache_page(page);
N
Nick Piggin 已提交
321
		copy_to_brd(brd, mem + off, sector, len);
N
Nick Piggin 已提交
322
	}
323
	kunmap_atomic(mem);
N
Nick Piggin 已提交
324 325 326 327 328

out:
	return err;
}

329
static blk_qc_t brd_make_request(struct request_queue *q, struct bio *bio)
N
Nick Piggin 已提交
330 331 332 333
{
	struct block_device *bdev = bio->bi_bdev;
	struct brd_device *brd = bdev->bd_disk->private_data;
	int rw;
334
	struct bio_vec bvec;
N
Nick Piggin 已提交
335
	sector_t sector;
336
	struct bvec_iter iter;
N
Nick Piggin 已提交
337

338
	sector = bio->bi_iter.bi_sector;
K
Kent Overstreet 已提交
339
	if (bio_end_sector(bio) > get_capacity(bdev->bd_disk))
340
		goto io_error;
N
Nick Piggin 已提交
341

342
	if (unlikely(bio->bi_rw & REQ_DISCARD)) {
343 344 345
		if (sector & ((PAGE_SIZE >> SECTOR_SHIFT) - 1) ||
		    bio->bi_iter.bi_size & PAGE_MASK)
			goto io_error;
346
		discard_from_brd(brd, sector, bio->bi_iter.bi_size);
N
Nick Piggin 已提交
347 348 349
		goto out;
	}

N
Nick Piggin 已提交
350 351 352 353
	rw = bio_rw(bio);
	if (rw == READA)
		rw = READ;

354 355
	bio_for_each_segment(bvec, bio, iter) {
		unsigned int len = bvec.bv_len;
356 357
		int err;

358 359
		err = brd_do_bvec(brd, bvec.bv_page, len,
					bvec.bv_offset, rw, sector);
N
Nick Piggin 已提交
360
		if (err)
361
			goto io_error;
N
Nick Piggin 已提交
362 363 364 365
		sector += len >> SECTOR_SHIFT;
	}

out:
366
	bio_endio(bio);
367
	return BLK_QC_T_NONE;
368 369
io_error:
	bio_io_error(bio);
370
	return BLK_QC_T_NONE;
N
Nick Piggin 已提交
371 372
}

M
Matthew Wilcox 已提交
373 374 375 376 377 378 379 380 381
static int brd_rw_page(struct block_device *bdev, sector_t sector,
		       struct page *page, int rw)
{
	struct brd_device *brd = bdev->bd_disk->private_data;
	int err = brd_do_bvec(brd, page, PAGE_CACHE_SIZE, 0, rw, sector);
	page_endio(page, rw & WRITE, err);
	return err;
}

M
Matthew Wilcox 已提交
382
#ifdef CONFIG_BLK_DEV_RAM_DAX
383
static long brd_direct_access(struct block_device *bdev, sector_t sector,
D
Dan Williams 已提交
384
			void __pmem **kaddr, pfn_t *pfn)
N
Nick Piggin 已提交
385 386 387 388 389 390 391 392
{
	struct brd_device *brd = bdev->bd_disk->private_data;
	struct page *page;

	if (!brd)
		return -ENODEV;
	page = brd_insert_page(brd, sector);
	if (!page)
393
		return -ENOSPC;
394
	*kaddr = (void __pmem *)page_address(page);
D
Dan Williams 已提交
395
	*pfn = page_to_pfn_t(page);
N
Nick Piggin 已提交
396

397
	return PAGE_SIZE;
N
Nick Piggin 已提交
398
}
M
Matthew Wilcox 已提交
399 400
#else
#define brd_direct_access NULL
N
Nick Piggin 已提交
401 402
#endif

A
Al Viro 已提交
403
static int brd_ioctl(struct block_device *bdev, fmode_t mode,
N
Nick Piggin 已提交
404 405 406 407 408 409 410 411 412 413 414 415
			unsigned int cmd, unsigned long arg)
{
	int error;
	struct brd_device *brd = bdev->bd_disk->private_data;

	if (cmd != BLKFLSBUF)
		return -ENOTTY;

	/*
	 * ram device BLKFLSBUF has special semantics, we want to actually
	 * release and destroy the ramdisk data.
	 */
416
	mutex_lock(&brd_mutex);
N
Nick Piggin 已提交
417 418 419 420
	mutex_lock(&bdev->bd_mutex);
	error = -EBUSY;
	if (bdev->bd_openers <= 1) {
		/*
A
Al Viro 已提交
421 422
		 * Kill the cache first, so it isn't written back to the
		 * device.
N
Nick Piggin 已提交
423 424 425 426
		 *
		 * Another thread might instantiate more buffercache here,
		 * but there is not much we can do to close that race.
		 */
A
Al Viro 已提交
427
		kill_bdev(bdev);
N
Nick Piggin 已提交
428 429 430 431
		brd_free_pages(brd);
		error = 0;
	}
	mutex_unlock(&bdev->bd_mutex);
432
	mutex_unlock(&brd_mutex);
N
Nick Piggin 已提交
433 434 435 436

	return error;
}

437
static const struct block_device_operations brd_fops = {
N
Nick Piggin 已提交
438
	.owner =		THIS_MODULE,
M
Matthew Wilcox 已提交
439
	.rw_page =		brd_rw_page,
440
	.ioctl =		brd_ioctl,
N
Nick Piggin 已提交
441
	.direct_access =	brd_direct_access,
N
Nick Piggin 已提交
442 443 444 445 446
};

/*
 * And now the modules code and kernel interface.
 */
B
Boaz Harrosh 已提交
447
static int rd_nr = CONFIG_BLK_DEV_RAM_COUNT;
N
Namhyung Kim 已提交
448
module_param(rd_nr, int, S_IRUGO);
N
Nick Piggin 已提交
449
MODULE_PARM_DESC(rd_nr, "Maximum number of brd devices");
B
Boaz Harrosh 已提交
450 451

int rd_size = CONFIG_BLK_DEV_RAM_SIZE;
N
Namhyung Kim 已提交
452
module_param(rd_size, int, S_IRUGO);
N
Nick Piggin 已提交
453
MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
B
Boaz Harrosh 已提交
454 455

static int max_part = 1;
N
Namhyung Kim 已提交
456
module_param(max_part, int, S_IRUGO);
B
Boaz Harrosh 已提交
457 458
MODULE_PARM_DESC(max_part, "Num Minors to reserve between devices");

N
Nick Piggin 已提交
459 460
MODULE_LICENSE("GPL");
MODULE_ALIAS_BLOCKDEV_MAJOR(RAMDISK_MAJOR);
461
MODULE_ALIAS("rd");
N
Nick Piggin 已提交
462 463 464 465 466 467 468 469

#ifndef MODULE
/* Legacy boot options - nonmodular */
static int __init ramdisk_size(char *str)
{
	rd_size = simple_strtol(str, NULL, 0);
	return 1;
}
470
__setup("ramdisk_size=", ramdisk_size);
N
Nick Piggin 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
#endif

/*
 * The device scheme is derived from loop.c. Keep them in synch where possible
 * (should share code eventually).
 */
static LIST_HEAD(brd_devices);
static DEFINE_MUTEX(brd_devices_mutex);

static struct brd_device *brd_alloc(int i)
{
	struct brd_device *brd;
	struct gendisk *disk;

	brd = kzalloc(sizeof(*brd), GFP_KERNEL);
	if (!brd)
		goto out;
	brd->brd_number		= i;
	spin_lock_init(&brd->brd_lock);
	INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC);

	brd->brd_queue = blk_alloc_queue(GFP_KERNEL);
	if (!brd->brd_queue)
		goto out_free_dev;
495

N
Nick Piggin 已提交
496
	blk_queue_make_request(brd->brd_queue, brd_make_request);
497
	blk_queue_max_hw_sectors(brd->brd_queue, 1024);
N
Nick Piggin 已提交
498 499
	blk_queue_bounce_limit(brd->brd_queue, BLK_BOUNCE_ANY);

500 501 502 503 504 505 506 507
	/* This is so fdisk will align partitions on 4k, because of
	 * direct_access API needing 4k alignment, returning a PFN
	 * (This is only a problem on very small devices <= 4M,
	 *  otherwise fdisk will align on 1M. Regardless this call
	 *  is harmless)
	 */
	blk_queue_physical_block_size(brd->brd_queue, PAGE_SIZE);

N
Nick Piggin 已提交
508
	brd->brd_queue->limits.discard_granularity = PAGE_SIZE;
509
	blk_queue_max_discard_sectors(brd->brd_queue, UINT_MAX);
N
Nick Piggin 已提交
510 511 512
	brd->brd_queue->limits.discard_zeroes_data = 1;
	queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, brd->brd_queue);

B
Boaz Harrosh 已提交
513
	disk = brd->brd_disk = alloc_disk(max_part);
N
Nick Piggin 已提交
514 515 516
	if (!disk)
		goto out_free_queue;
	disk->major		= RAMDISK_MAJOR;
B
Boaz Harrosh 已提交
517
	disk->first_minor	= i * max_part;
N
Nick Piggin 已提交
518 519 520
	disk->fops		= &brd_fops;
	disk->private_data	= brd;
	disk->queue		= brd->brd_queue;
B
Boaz Harrosh 已提交
521
	disk->flags		= GENHD_FL_EXT_DEVT;
N
Nick Piggin 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	sprintf(disk->disk_name, "ram%d", i);
	set_capacity(disk, rd_size * 2);

	return brd;

out_free_queue:
	blk_cleanup_queue(brd->brd_queue);
out_free_dev:
	kfree(brd);
out:
	return NULL;
}

static void brd_free(struct brd_device *brd)
{
	put_disk(brd->brd_disk);
	blk_cleanup_queue(brd->brd_queue);
	brd_free_pages(brd);
	kfree(brd);
}

B
Boaz Harrosh 已提交
543
static struct brd_device *brd_init_one(int i, bool *new)
N
Nick Piggin 已提交
544 545 546
{
	struct brd_device *brd;

B
Boaz Harrosh 已提交
547
	*new = false;
N
Nick Piggin 已提交
548 549 550 551 552 553 554 555 556 557
	list_for_each_entry(brd, &brd_devices, brd_list) {
		if (brd->brd_number == i)
			goto out;
	}

	brd = brd_alloc(i);
	if (brd) {
		add_disk(brd->brd_disk);
		list_add_tail(&brd->brd_list, &brd_devices);
	}
B
Boaz Harrosh 已提交
558
	*new = true;
N
Nick Piggin 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
out:
	return brd;
}

static void brd_del_one(struct brd_device *brd)
{
	list_del(&brd->brd_list);
	del_gendisk(brd->brd_disk);
	brd_free(brd);
}

static struct kobject *brd_probe(dev_t dev, int *part, void *data)
{
	struct brd_device *brd;
	struct kobject *kobj;
B
Boaz Harrosh 已提交
574
	bool new;
N
Nick Piggin 已提交
575 576

	mutex_lock(&brd_devices_mutex);
B
Boaz Harrosh 已提交
577
	brd = brd_init_one(MINOR(dev) / max_part, &new);
578
	kobj = brd ? get_disk(brd->brd_disk) : NULL;
N
Nick Piggin 已提交
579 580
	mutex_unlock(&brd_devices_mutex);

B
Boaz Harrosh 已提交
581 582 583
	if (new)
		*part = 0;

N
Nick Piggin 已提交
584 585 586 587 588 589
	return kobj;
}

static int __init brd_init(void)
{
	struct brd_device *brd, *next;
B
Boaz Harrosh 已提交
590
	int i;
N
Nick Piggin 已提交
591 592 593 594 595

	/*
	 * brd module now has a feature to instantiate underlying device
	 * structure on-demand, provided that there is an access dev node.
	 *
B
Boaz Harrosh 已提交
596 597 598 599 600 601 602 603 604
	 * (1) if rd_nr is specified, create that many upfront. else
	 *     it defaults to CONFIG_BLK_DEV_RAM_COUNT
	 * (2) User can further extend brd devices by create dev node themselves
	 *     and have kernel automatically instantiate actual device
	 *     on-demand. Example:
	 *		mknod /path/devnod_name b 1 X	# 1 is the rd major
	 *		fdisk -l /path/devnod_name
	 *	If (X / max_part) was not already created it will be created
	 *	dynamically.
N
Nick Piggin 已提交
605
	 */
606

N
Nick Piggin 已提交
607 608 609
	if (register_blkdev(RAMDISK_MAJOR, "ramdisk"))
		return -EIO;

B
Boaz Harrosh 已提交
610 611 612 613
	if (unlikely(!max_part))
		max_part = 1;

	for (i = 0; i < rd_nr; i++) {
N
Nick Piggin 已提交
614 615 616 617 618 619 620 621 622 623 624
		brd = brd_alloc(i);
		if (!brd)
			goto out_free;
		list_add_tail(&brd->brd_list, &brd_devices);
	}

	/* point of no return */

	list_for_each_entry(brd, &brd_devices, brd_list)
		add_disk(brd->brd_disk);

B
Boaz Harrosh 已提交
625
	blk_register_region(MKDEV(RAMDISK_MAJOR, 0), 1UL << MINORBITS,
N
Nick Piggin 已提交
626 627
				  THIS_MODULE, brd_probe, NULL, NULL);

B
Boaz Harrosh 已提交
628
	pr_info("brd: module loaded\n");
N
Nick Piggin 已提交
629 630 631 632 633 634 635
	return 0;

out_free:
	list_for_each_entry_safe(brd, next, &brd_devices, brd_list) {
		list_del(&brd->brd_list);
		brd_free(brd);
	}
636
	unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
N
Nick Piggin 已提交
637

B
Boaz Harrosh 已提交
638
	pr_info("brd: module NOT loaded !!!\n");
N
Nick Piggin 已提交
639 640 641 642 643 644 645 646 647 648
	return -ENOMEM;
}

static void __exit brd_exit(void)
{
	struct brd_device *brd, *next;

	list_for_each_entry_safe(brd, next, &brd_devices, brd_list)
		brd_del_one(brd);

B
Boaz Harrosh 已提交
649
	blk_unregister_region(MKDEV(RAMDISK_MAJOR, 0), 1UL << MINORBITS);
N
Nick Piggin 已提交
650
	unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
B
Boaz Harrosh 已提交
651 652

	pr_info("brd: module unloaded\n");
N
Nick Piggin 已提交
653 654 655 656 657
}

module_init(brd_init);
module_exit(brd_exit);