Skip to content

Commit f525464

Browse files
Christoph Hellwigaxboe
Christoph Hellwig
authored andcommitted
block: add blk_alloc_disk and blk_cleanup_disk APIs
Add two new APIs to allocate and free a gendisk including the request_queue for use with BIO based drivers. This is to avoid boilerplate code in drivers. 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 958229a commit f525464

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

block/genhd.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,25 @@ struct gendisk *__alloc_disk_node(int minors, int node_id)
12931293
}
12941294
EXPORT_SYMBOL(__alloc_disk_node);
12951295

1296+
struct gendisk *__blk_alloc_disk(int node)
1297+
{
1298+
struct request_queue *q;
1299+
struct gendisk *disk;
1300+
1301+
q = blk_alloc_queue(node);
1302+
if (!q)
1303+
return NULL;
1304+
1305+
disk = __alloc_disk_node(0, node);
1306+
if (!disk) {
1307+
blk_cleanup_queue(q);
1308+
return NULL;
1309+
}
1310+
disk->queue = q;
1311+
return disk;
1312+
}
1313+
EXPORT_SYMBOL(__blk_alloc_disk);
1314+
12961315
/**
12971316
* put_disk - decrements the gendisk refcount
12981317
* @disk: the struct gendisk to decrement the refcount for
@@ -1310,6 +1329,22 @@ void put_disk(struct gendisk *disk)
13101329
}
13111330
EXPORT_SYMBOL(put_disk);
13121331

1332+
/**
1333+
* blk_cleanup_disk - shutdown a gendisk allocated by blk_alloc_disk
1334+
* @disk: gendisk to shutdown
1335+
*
1336+
* Mark the queue hanging off @disk DYING, drain all pending requests, then mark
1337+
* the queue DEAD, destroy and put it and the gendisk structure.
1338+
*
1339+
* Context: can sleep
1340+
*/
1341+
void blk_cleanup_disk(struct gendisk *disk)
1342+
{
1343+
blk_cleanup_queue(disk->queue);
1344+
put_disk(disk);
1345+
}
1346+
EXPORT_SYMBOL(blk_cleanup_disk);
1347+
13131348
static void set_disk_ro_uevent(struct gendisk *gd, int ro)
13141349
{
13151350
char event[] = "DISK_RO=1";

include/linux/genhd.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,28 @@ extern void put_disk(struct gendisk *disk);
278278

279279
#define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)
280280

281+
/**
282+
* blk_alloc_disk - allocate a gendisk structure
283+
* @node_id: numa node to allocate on
284+
*
285+
* Allocate and pre-initialize a gendisk structure for use with BIO based
286+
* drivers.
287+
*
288+
* Context: can sleep
289+
*/
290+
#define blk_alloc_disk(node_id) \
291+
({ \
292+
struct gendisk *__disk = __blk_alloc_disk(node_id); \
293+
static struct lock_class_key __key; \
294+
\
295+
if (__disk) \
296+
lockdep_init_map(&__disk->lockdep_map, \
297+
"(bio completion)", &__key, 0); \
298+
__disk; \
299+
})
300+
struct gendisk *__blk_alloc_disk(int node);
301+
void blk_cleanup_disk(struct gendisk *disk);
302+
281303
int __register_blkdev(unsigned int major, const char *name,
282304
void (*probe)(dev_t devt));
283305
#define register_blkdev(major, name) \

0 commit comments

Comments
 (0)