1856 lines · 46.2 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* *  gendisk handling * * Portions Copyright (C) 2020 Christoph Hellwig */ #include <linux/module.h>#include <linux/ctype.h>#include <linux/fs.h>#include <linux/genhd.h>#include <linux/kdev_t.h>#include <linux/kernel.h>#include <linux/blkdev.h>#include <linux/backing-dev.h>#include <linux/init.h>#include <linux/spinlock.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <linux/slab.h>#include <linux/kmod.h>#include <linux/mutex.h>#include <linux/idr.h>#include <linux/log2.h>#include <linux/pm_runtime.h>#include <linux/badblocks.h> #include "blk.h" static struct kobject *block_depr; /* for extended dynamic devt allocation, currently only one major is used */#define NR_EXT_DEVT		(1 << MINORBITS)static DEFINE_IDA(ext_devt_ida); static void disk_check_events(struct disk_events *ev,			      unsigned int *clearing_ptr);static void disk_alloc_events(struct gendisk *disk);static void disk_add_events(struct gendisk *disk);static void disk_del_events(struct gendisk *disk);static void disk_release_events(struct gendisk *disk); void set_capacity(struct gendisk *disk, sector_t sectors){	struct block_device *bdev = disk->part0; 	spin_lock(&bdev->bd_size_lock);	i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);	spin_unlock(&bdev->bd_size_lock);}EXPORT_SYMBOL(set_capacity); /* * Set disk capacity and notify if the size is not currently zero and will not * be set to zero.  Returns true if a uevent was sent, otherwise false. */bool set_capacity_and_notify(struct gendisk *disk, sector_t size){	sector_t capacity = get_capacity(disk);	char *envp[] = { "RESIZE=1", NULL }; 	set_capacity(disk, size); 	/*	 * Only print a message and send a uevent if the gendisk is user visible	 * and alive.  This avoids spamming the log and udev when setting the	 * initial capacity during probing.	 */	if (size == capacity ||	    (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)		return false; 	pr_info("%s: detected capacity change from %lld to %lld\n",		disk->disk_name, capacity, size); 	/*	 * Historically we did not send a uevent for changes to/from an empty	 * device.	 */	if (!capacity || !size)		return false;	kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);	return true;}EXPORT_SYMBOL_GPL(set_capacity_and_notify); /* * Format the device name of the indicated disk into the supplied buffer and * return a pointer to that same buffer for convenience. */char *disk_name(struct gendisk *hd, int partno, char *buf){	if (!partno)		snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);	else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))		snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);	else		snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno); 	return buf;} const char *bdevname(struct block_device *bdev, char *buf){	return disk_name(bdev->bd_disk, bdev->bd_partno, buf);}EXPORT_SYMBOL(bdevname); static void part_stat_read_all(struct block_device *part,		struct disk_stats *stat){	int cpu; 	memset(stat, 0, sizeof(struct disk_stats));	for_each_possible_cpu(cpu) {		struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);		int group; 		for (group = 0; group < NR_STAT_GROUPS; group++) {			stat->nsecs[group] += ptr->nsecs[group];			stat->sectors[group] += ptr->sectors[group];			stat->ios[group] += ptr->ios[group];			stat->merges[group] += ptr->merges[group];		} 		stat->io_ticks += ptr->io_ticks;	}} static unsigned int part_in_flight(struct block_device *part){	unsigned int inflight = 0;	int cpu; 	for_each_possible_cpu(cpu) {		inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +			    part_stat_local_read_cpu(part, in_flight[1], cpu);	}	if ((int)inflight < 0)		inflight = 0; 	return inflight;} static void part_in_flight_rw(struct block_device *part,		unsigned int inflight[2]){	int cpu; 	inflight[0] = 0;	inflight[1] = 0;	for_each_possible_cpu(cpu) {		inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);		inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);	}	if ((int)inflight[0] < 0)		inflight[0] = 0;	if ((int)inflight[1] < 0)		inflight[1] = 0;} /* * Can be deleted altogether. Later. * */#define BLKDEV_MAJOR_HASH_SIZE 255static struct blk_major_name {	struct blk_major_name *next;	int major;	char name[16];	void (*probe)(dev_t devt);} *major_names[BLKDEV_MAJOR_HASH_SIZE];static DEFINE_MUTEX(major_names_lock); /* index in the above - for now: assume no multimajor ranges */static inline int major_to_index(unsigned major){	return major % BLKDEV_MAJOR_HASH_SIZE;} #ifdef CONFIG_PROC_FSvoid blkdev_show(struct seq_file *seqf, off_t offset){	struct blk_major_name *dp; 	mutex_lock(&major_names_lock);	for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)		if (dp->major == offset)			seq_printf(seqf, "%3d %s\n", dp->major, dp->name);	mutex_unlock(&major_names_lock);}#endif /* CONFIG_PROC_FS */ /** * __register_blkdev - register a new block device * * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If *         @major = 0, try to allocate any unused major number. * @name: the name of the new block device as a zero terminated string * @probe: allback that is called on access to any minor number of @major * * The @name must be unique within the system. * * The return value depends on the @major input parameter: * *  - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1] *    then the function returns zero on success, or a negative error code *  - if any unused major number was requested with @major = 0 parameter *    then the return value is the allocated major number in range *    [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise * * See Documentation/admin-guide/devices.txt for the list of allocated * major numbers. * * Use register_blkdev instead for any new code. */int __register_blkdev(unsigned int major, const char *name,		void (*probe)(dev_t devt)){	struct blk_major_name **n, *p;	int index, ret = 0; 	mutex_lock(&major_names_lock); 	/* temporary */	if (major == 0) {		for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {			if (major_names[index] == NULL)				break;		} 		if (index == 0) {			printk("%s: failed to get major for %s\n",			       __func__, name);			ret = -EBUSY;			goto out;		}		major = index;		ret = major;	} 	if (major >= BLKDEV_MAJOR_MAX) {		pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",		       __func__, major, BLKDEV_MAJOR_MAX-1, name); 		ret = -EINVAL;		goto out;	} 	p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);	if (p == NULL) {		ret = -ENOMEM;		goto out;	} 	p->major = major;	p->probe = probe;	strlcpy(p->name, name, sizeof(p->name));	p->next = NULL;	index = major_to_index(major); 	for (n = &major_names[index]; *n; n = &(*n)->next) {		if ((*n)->major == major)			break;	}	if (!*n)		*n = p;	else		ret = -EBUSY; 	if (ret < 0) {		printk("register_blkdev: cannot get major %u for %s\n",		       major, name);		kfree(p);	}out:	mutex_unlock(&major_names_lock);	return ret;}EXPORT_SYMBOL(__register_blkdev); void unregister_blkdev(unsigned int major, const char *name){	struct blk_major_name **n;	struct blk_major_name *p = NULL;	int index = major_to_index(major); 	mutex_lock(&major_names_lock);	for (n = &major_names[index]; *n; n = &(*n)->next)		if ((*n)->major == major)			break;	if (!*n || strcmp((*n)->name, name)) {		WARN_ON(1);	} else {		p = *n;		*n = p->next;	}	mutex_unlock(&major_names_lock);	kfree(p);} EXPORT_SYMBOL(unregister_blkdev); /** * blk_mangle_minor - scatter minor numbers apart * @minor: minor number to mangle * * Scatter consecutively allocated @minor number apart if MANGLE_DEVT * is enabled.  Mangling twice gives the original value. * * RETURNS: * Mangled value. * * CONTEXT: * Don't care. */static int blk_mangle_minor(int minor){#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT	int i; 	for (i = 0; i < MINORBITS / 2; i++) {		int low = minor & (1 << i);		int high = minor & (1 << (MINORBITS - 1 - i));		int distance = MINORBITS - 1 - 2 * i; 		minor ^= low | high;	/* clear both bits */		low <<= distance;	/* swap the positions */		high >>= distance;		minor |= low | high;	/* and set */	}#endif	return minor;} /** * blk_alloc_devt - allocate a dev_t for a block device * @bdev: block device to allocate dev_t for * @devt: out parameter for resulting dev_t * * Allocate a dev_t for block device. * * RETURNS: * 0 on success, allocated dev_t is returned in *@devt.  -errno on * failure. * * CONTEXT: * Might sleep. */int blk_alloc_devt(struct block_device *bdev, dev_t *devt){	struct gendisk *disk = bdev->bd_disk;	int idx; 	/* in consecutive minor range? */	if (bdev->bd_partno < disk->minors) {		*devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);		return 0;	} 	idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);	if (idx < 0)		return idx == -ENOSPC ? -EBUSY : idx; 	*devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));	return 0;} /** * blk_free_devt - free a dev_t * @devt: dev_t to free * * Free @devt which was allocated using blk_alloc_devt(). * * CONTEXT: * Might sleep. */void blk_free_devt(dev_t devt){	if (MAJOR(devt) == BLOCK_EXT_MAJOR)		ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));} static char *bdevt_str(dev_t devt, char *buf){	if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {		char tbuf[BDEVT_SIZE];		snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));		snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);	} else		snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt)); 	return buf;} void disk_uevent(struct gendisk *disk, enum kobject_action action){	struct block_device *part;	unsigned long idx; 	rcu_read_lock();	xa_for_each(&disk->part_tbl, idx, part) {		if (bdev_is_partition(part) && !bdev_nr_sectors(part))			continue;		if (!bdgrab(part))			continue; 		rcu_read_unlock();		kobject_uevent(bdev_kobj(part), action);		bdput(part);		rcu_read_lock();	}	rcu_read_unlock();}EXPORT_SYMBOL_GPL(disk_uevent); static void disk_scan_partitions(struct gendisk *disk){	struct block_device *bdev; 	if (!get_capacity(disk) || !disk_part_scan_enabled(disk))		return; 	set_bit(GD_NEED_PART_SCAN, &disk->state);	bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);	if (!IS_ERR(bdev))		blkdev_put(bdev, FMODE_READ);} static void register_disk(struct device *parent, struct gendisk *disk,			  const struct attribute_group **groups){	struct device *ddev = disk_to_dev(disk);	int err; 	ddev->parent = parent; 	dev_set_name(ddev, "%s", disk->disk_name); 	/* delay uevents, until we scanned partition table */	dev_set_uevent_suppress(ddev, 1); 	if (groups) {		WARN_ON(ddev->groups);		ddev->groups = groups;	}	if (device_add(ddev))		return;	if (!sysfs_deprecated) {		err = sysfs_create_link(block_depr, &ddev->kobj,					kobject_name(&ddev->kobj));		if (err) {			device_del(ddev);			return;		}	} 	/*	 * avoid probable deadlock caused by allocating memory with	 * GFP_KERNEL in runtime_resume callback of its all ancestor	 * devices	 */	pm_runtime_set_memalloc_noio(ddev, true); 	disk->part0->bd_holder_dir =		kobject_create_and_add("holders", &ddev->kobj);	disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj); 	if (disk->flags & GENHD_FL_HIDDEN)		return; 	disk_scan_partitions(disk); 	/* announce the disk and partitions after all partitions are created */	dev_set_uevent_suppress(ddev, 0);	disk_uevent(disk, KOBJ_ADD); 	if (disk->queue->backing_dev_info->dev) {		err = sysfs_create_link(&ddev->kobj,			  &disk->queue->backing_dev_info->dev->kobj,			  "bdi");		WARN_ON(err);	}} /** * __device_add_disk - add disk information to kernel list * @parent: parent device for the disk * @disk: per-device partitioning information * @groups: Additional per-device sysfs groups * @register_queue: register the queue if set to true * * This function registers the partitioning information in @disk * with the kernel. * * FIXME: error handling */static void __device_add_disk(struct device *parent, struct gendisk *disk,			      const struct attribute_group **groups,			      bool register_queue)

Lines 1–500 of 1856.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t