Skip to content

Commit 7f9b348

Browse files
Christoph Hellwigaxboe
Christoph Hellwig
authored andcommitted
brd: convert to blk_alloc_disk/blk_cleanup_disk
Convert the brd driver to use the blk_alloc_disk and blk_cleanup_disk helpers to simplify gendisk and request_queue allocation. This also allows to remove the request_queue pointer in struct request_queue, and to simplify the initialization as blk_cleanup_disk can be called on any disk returned from blk_alloc_disk. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Hannes Reinecke <[email protected]> Reviewed-by: Ulf Hansson <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent f525464 commit 7f9b348

File tree

1 file changed

+33
-61
lines changed

1 file changed

+33
-61
lines changed

drivers/block/brd.c

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
* device).
3939
*/
4040
struct brd_device {
41-
int brd_number;
42-
43-
struct request_queue *brd_queue;
41+
int brd_number;
4442
struct gendisk *brd_disk;
4543
struct list_head brd_list;
4644

@@ -372,86 +370,71 @@ static LIST_HEAD(brd_devices);
372370
static DEFINE_MUTEX(brd_devices_mutex);
373371
static struct dentry *brd_debugfs_dir;
374372

375-
static struct brd_device *brd_alloc(int i)
373+
static int brd_alloc(int i)
376374
{
377375
struct brd_device *brd;
378376
struct gendisk *disk;
379377
char buf[DISK_NAME_LEN];
380378

381379
brd = kzalloc(sizeof(*brd), GFP_KERNEL);
382380
if (!brd)
383-
goto out;
381+
return -ENOMEM;
384382
brd->brd_number = i;
385383
spin_lock_init(&brd->brd_lock);
386384
INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC);
387385

388-
brd->brd_queue = blk_alloc_queue(NUMA_NO_NODE);
389-
if (!brd->brd_queue)
390-
goto out_free_dev;
391-
392386
snprintf(buf, DISK_NAME_LEN, "ram%d", i);
393387
if (!IS_ERR_OR_NULL(brd_debugfs_dir))
394388
debugfs_create_u64(buf, 0444, brd_debugfs_dir,
395389
&brd->brd_nr_pages);
396390

397-
/* This is so fdisk will align partitions on 4k, because of
398-
* direct_access API needing 4k alignment, returning a PFN
399-
* (This is only a problem on very small devices <= 4M,
400-
* otherwise fdisk will align on 1M. Regardless this call
401-
* is harmless)
402-
*/
403-
blk_queue_physical_block_size(brd->brd_queue, PAGE_SIZE);
404-
disk = brd->brd_disk = alloc_disk(max_part);
391+
disk = brd->brd_disk = blk_alloc_disk(NUMA_NO_NODE);
405392
if (!disk)
406-
goto out_free_queue;
393+
goto out_free_dev;
394+
407395
disk->major = RAMDISK_MAJOR;
408396
disk->first_minor = i * max_part;
397+
disk->minors = max_part;
409398
disk->fops = &brd_fops;
410399
disk->private_data = brd;
411400
disk->flags = GENHD_FL_EXT_DEVT;
412401
strlcpy(disk->disk_name, buf, DISK_NAME_LEN);
413402
set_capacity(disk, rd_size * 2);
403+
404+
/*
405+
* This is so fdisk will align partitions on 4k, because of
406+
* direct_access API needing 4k alignment, returning a PFN
407+
* (This is only a problem on very small devices <= 4M,
408+
* otherwise fdisk will align on 1M. Regardless this call
409+
* is harmless)
410+
*/
411+
blk_queue_physical_block_size(disk->queue, PAGE_SIZE);
414412

415413
/* Tell the block layer that this is not a rotational device */
416-
blk_queue_flag_set(QUEUE_FLAG_NONROT, brd->brd_queue);
417-
blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, brd->brd_queue);
414+
blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
415+
blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
416+
add_disk(disk);
417+
list_add_tail(&brd->brd_list, &brd_devices);
418418

419-
return brd;
419+
return 0;
420420

421-
out_free_queue:
422-
blk_cleanup_queue(brd->brd_queue);
423421
out_free_dev:
424422
kfree(brd);
425-
out:
426-
return NULL;
427-
}
428-
429-
static void brd_free(struct brd_device *brd)
430-
{
431-
put_disk(brd->brd_disk);
432-
blk_cleanup_queue(brd->brd_queue);
433-
brd_free_pages(brd);
434-
kfree(brd);
423+
return -ENOMEM;
435424
}
436425

437426
static void brd_probe(dev_t dev)
438427
{
439-
struct brd_device *brd;
440428
int i = MINOR(dev) / max_part;
429+
struct brd_device *brd;
441430

442431
mutex_lock(&brd_devices_mutex);
443432
list_for_each_entry(brd, &brd_devices, brd_list) {
444433
if (brd->brd_number == i)
445434
goto out_unlock;
446435
}
447436

448-
brd = brd_alloc(i);
449-
if (brd) {
450-
brd->brd_disk->queue = brd->brd_queue;
451-
add_disk(brd->brd_disk);
452-
list_add_tail(&brd->brd_list, &brd_devices);
453-
}
454-
437+
brd_alloc(i);
455438
out_unlock:
456439
mutex_unlock(&brd_devices_mutex);
457440
}
@@ -460,7 +443,9 @@ static void brd_del_one(struct brd_device *brd)
460443
{
461444
list_del(&brd->brd_list);
462445
del_gendisk(brd->brd_disk);
463-
brd_free(brd);
446+
blk_cleanup_disk(brd->brd_disk);
447+
brd_free_pages(brd);
448+
kfree(brd);
464449
}
465450

466451
static inline void brd_check_and_reset_par(void)
@@ -485,7 +470,7 @@ static inline void brd_check_and_reset_par(void)
485470
static int __init brd_init(void)
486471
{
487472
struct brd_device *brd, *next;
488-
int i;
473+
int err, i;
489474

490475
/*
491476
* brd module now has a feature to instantiate underlying device
@@ -511,22 +496,11 @@ static int __init brd_init(void)
511496

512497
mutex_lock(&brd_devices_mutex);
513498
for (i = 0; i < rd_nr; i++) {
514-
brd = brd_alloc(i);
515-
if (!brd)
499+
err = brd_alloc(i);
500+
if (err)
516501
goto out_free;
517-
list_add_tail(&brd->brd_list, &brd_devices);
518502
}
519503

520-
/* point of no return */
521-
522-
list_for_each_entry(brd, &brd_devices, brd_list) {
523-
/*
524-
* associate with queue just before adding disk for
525-
* avoiding to mess up failure path
526-
*/
527-
brd->brd_disk->queue = brd->brd_queue;
528-
add_disk(brd->brd_disk);
529-
}
530504
mutex_unlock(&brd_devices_mutex);
531505

532506
pr_info("brd: module loaded\n");
@@ -535,15 +509,13 @@ static int __init brd_init(void)
535509
out_free:
536510
debugfs_remove_recursive(brd_debugfs_dir);
537511

538-
list_for_each_entry_safe(brd, next, &brd_devices, brd_list) {
539-
list_del(&brd->brd_list);
540-
brd_free(brd);
541-
}
512+
list_for_each_entry_safe(brd, next, &brd_devices, brd_list)
513+
brd_del_one(brd);
542514
mutex_unlock(&brd_devices_mutex);
543515
unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
544516

545517
pr_info("brd: module NOT loaded !!!\n");
546-
return -ENOMEM;
518+
return err;
547519
}
548520

549521
static void __exit brd_exit(void)

0 commit comments

Comments
 (0)