block/blk-lib.c
Source file repositories/reference/linux-study-clean/block/blk-lib.c
File Facts
- System
- Linux kernel
- Corpus path
block/blk-lib.c- Extension
.c- Size
- 9783 bytes
- Lines
- 358
- Domain
- Representative Device Path
- Bucket
- PCIe NVMe Storage Path
- Inferred role
- Representative Device Path: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Part of the selected hardware vertical slice: PCI discovery, driver binding, NVMe queues, block requests, DMA, interrupts, and completion.
- Part of the selected hardware vertical slice: PCI discovery, driver binding, NVMe queues, block requests, DMA, interrupts, and completion.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/module.hlinux/bio.hlinux/blkdev.hlinux/scatterlist.hblk.h
Detected Declarations
function bio_discard_limitfunction __blkdev_issue_discardfunction blkdev_issue_discardfunction bio_write_zeroes_limitfunction __blkdev_issue_write_zeroesfunction blkdev_issue_write_zeroesfunction __blkdev_sectors_to_bio_pagesfunction __blkdev_issue_zero_pagesfunction blkdev_issue_zero_pagesfunction __blkdev_issue_zerooutfunction blkdev_issue_zerooutfunction blkdev_issue_secure_eraseexport __blkdev_issue_discardexport blkdev_issue_discardexport __blkdev_issue_zerooutexport blkdev_issue_zerooutexport blkdev_issue_secure_erase
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* Functions related to generic helpers functions
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/bio.h>
#include <linux/blkdev.h>
#include <linux/scatterlist.h>
#include "blk.h"
static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector)
{
unsigned int discard_granularity = bdev_discard_granularity(bdev);
sector_t granularity_aligned_sector;
if (bdev_is_partition(bdev))
sector += bdev->bd_start_sect;
granularity_aligned_sector =
round_up(sector, discard_granularity >> SECTOR_SHIFT);
/*
* Make sure subsequent bios start aligned to the discard granularity if
* it needs to be split.
*/
if (granularity_aligned_sector != sector)
return granularity_aligned_sector - sector;
/*
* Align the bio size to the discard granularity to make splitting the bio
* at discard granularity boundaries easier in the driver if needed.
*/
return round_down(BIO_MAX_SIZE, discard_granularity) >> SECTOR_SHIFT;
}
struct bio *blk_alloc_discard_bio(struct block_device *bdev,
sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask)
{
sector_t bio_sects = min(*nr_sects, bio_discard_limit(bdev, *sector));
struct bio *bio;
if (!bio_sects)
return NULL;
bio = bio_alloc(bdev, 0, REQ_OP_DISCARD, gfp_mask);
if (!bio)
return NULL;
bio->bi_iter.bi_sector = *sector;
bio->bi_iter.bi_size = bio_sects << SECTOR_SHIFT;
*sector += bio_sects;
*nr_sects -= bio_sects;
/*
* We can loop for a long time in here if someone does full device
* discards (like mkfs). Be nice and allow us to schedule out to avoid
* softlocking if preempt is disabled.
*/
cond_resched();
return bio;
}
void __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
sector_t nr_sects, gfp_t gfp_mask, struct bio **biop)
{
struct bio *bio;
while ((bio = blk_alloc_discard_bio(bdev, §or, &nr_sects,
gfp_mask)))
*biop = bio_chain_and_submit(*biop, bio);
}
EXPORT_SYMBOL(__blkdev_issue_discard);
/**
* blkdev_issue_discard - queue a discard
* @bdev: blockdev to issue discard for
* @sector: start sector
* @nr_sects: number of sectors to discard
* @gfp_mask: memory allocation flags (for bio_alloc)
*
* Description:
* Issue a discard request for the sectors in question.
*/
int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
sector_t nr_sects, gfp_t gfp_mask)
{
struct bio *bio = NULL;
struct blk_plug plug;
int ret = 0;
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/module.h`, `linux/bio.h`, `linux/blkdev.h`, `linux/scatterlist.h`, `blk.h`.
- Detected declarations: `function bio_discard_limit`, `function __blkdev_issue_discard`, `function blkdev_issue_discard`, `function bio_write_zeroes_limit`, `function __blkdev_issue_write_zeroes`, `function blkdev_issue_write_zeroes`, `function __blkdev_sectors_to_bio_pages`, `function __blkdev_issue_zero_pages`, `function blkdev_issue_zero_pages`, `function __blkdev_issue_zeroout`.
- Atlas domain: Representative Device Path / PCIe NVMe Storage Path.
- Implementation status: integration implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.