blob: 39ca97b0edc612ad2828952e7424d0a232bbf640 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * gendisk handling
Christoph Hellwig7b51e702020-12-10 08:55:44 +01004 *
5 * Portions Copyright (C) 2020 Christoph Hellwig
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01009#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fs.h>
11#include <linux/genhd.h>
Andrew Mortonb446b602007-02-20 13:57:48 -080012#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040015#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include <linux/spinlock.h>
Alexey Dobriyanf5009752008-10-04 23:53:21 +040018#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/seq_file.h>
20#include <linux/slab.h>
21#include <linux/kmod.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080022#include <linux/mutex.h>
Tejun Heobcce3de2008-08-25 19:47:22 +090023#include <linux/idr.h>
Tejun Heo77ea8872010-12-08 20:57:37 +010024#include <linux/log2.h>
Ming Lei25e823c2013-02-22 16:34:13 -080025#include <linux/pm_runtime.h>
Vishal Verma99e66082016-01-09 08:36:51 -080026#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Adrian Bunkff889722008-03-04 11:23:45 +010028#include "blk.h"
29
Christoph Hellwig31eb6182020-03-25 16:48:35 +010030static struct kobject *block_depr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +010032DECLARE_RWSEM(bdev_lookup_sem);
Christoph Hellwig62b508f2020-10-29 15:58:25 +010033
Tejun Heobcce3de2008-08-25 19:47:22 +090034/* for extended dynamic devt allocation, currently only one major is used */
Tejun Heoce23bba2013-02-27 17:03:56 -080035#define NR_EXT_DEVT (1 << MINORBITS)
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +010036static DEFINE_IDA(ext_devt_ida);
Tejun Heobcce3de2008-08-25 19:47:22 +090037
Derek Basehore12c2bdb2012-12-18 12:27:20 -080038static void disk_check_events(struct disk_events *ev,
39 unsigned int *clearing_ptr);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +010040static void disk_alloc_events(struct gendisk *disk);
Tejun Heo77ea8872010-12-08 20:57:37 +010041static void disk_add_events(struct gendisk *disk);
42static void disk_del_events(struct gendisk *disk);
43static void disk_release_events(struct gendisk *disk);
44
Christoph Hellwiga7824832020-11-26 18:43:37 +010045void set_capacity(struct gendisk *disk, sector_t sectors)
46{
Christoph Hellwigcb8432d2020-11-26 18:47:17 +010047 struct block_device *bdev = disk->part0;
Christoph Hellwiga7824832020-11-26 18:43:37 +010048
Damien Le Moal0f472272021-03-01 12:04:02 +090049 spin_lock(&bdev->bd_size_lock);
Christoph Hellwiga7824832020-11-26 18:43:37 +010050 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
Damien Le Moal0f472272021-03-01 12:04:02 +090051 spin_unlock(&bdev->bd_size_lock);
Christoph Hellwiga7824832020-11-26 18:43:37 +010052}
53EXPORT_SYMBOL(set_capacity);
54
Balbir Singhe598a722020-03-13 05:30:05 +000055/*
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010056 * Set disk capacity and notify if the size is not currently zero and will not
57 * be set to zero. Returns true if a uevent was sent, otherwise false.
Balbir Singhe598a722020-03-13 05:30:05 +000058 */
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010059bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
Balbir Singhe598a722020-03-13 05:30:05 +000060{
61 sector_t capacity = get_capacity(disk);
Christoph Hellwiga7824832020-11-26 18:43:37 +010062 char *envp[] = { "RESIZE=1", NULL };
Balbir Singhe598a722020-03-13 05:30:05 +000063
64 set_capacity(disk, size);
Balbir Singhe598a722020-03-13 05:30:05 +000065
Christoph Hellwiga7824832020-11-26 18:43:37 +010066 /*
67 * Only print a message and send a uevent if the gendisk is user visible
68 * and alive. This avoids spamming the log and udev when setting the
69 * initial capacity during probing.
70 */
71 if (size == capacity ||
72 (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
73 return false;
Balbir Singhe598a722020-03-13 05:30:05 +000074
Christoph Hellwiga7824832020-11-26 18:43:37 +010075 pr_info("%s: detected capacity change from %lld to %lld\n",
Ming Lei452c0bf2021-02-23 16:50:15 +080076 disk->disk_name, capacity, size);
Christoph Hellwig7e890c32020-11-12 17:50:04 +010077
Christoph Hellwiga7824832020-11-26 18:43:37 +010078 /*
79 * Historically we did not send a uevent for changes to/from an empty
80 * device.
81 */
82 if (!capacity || !size)
83 return false;
84 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
85 return true;
Balbir Singhe598a722020-03-13 05:30:05 +000086}
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010087EXPORT_SYMBOL_GPL(set_capacity_and_notify);
Balbir Singhe598a722020-03-13 05:30:05 +000088
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +010089/*
90 * Format the device name of the indicated disk into the supplied buffer and
91 * return a pointer to that same buffer for convenience.
92 */
93char *disk_name(struct gendisk *hd, int partno, char *buf)
94{
95 if (!partno)
96 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
97 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
98 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
99 else
100 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
101
102 return buf;
103}
104
105const char *bdevname(struct block_device *bdev, char *buf)
106{
Christoph Hellwig8a63a862020-09-03 07:41:03 +0200107 return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +0100108}
109EXPORT_SYMBOL(bdevname);
Balbir Singhe598a722020-03-13 05:30:05 +0000110
Christoph Hellwig0d021292020-11-27 16:43:51 +0100111static void part_stat_read_all(struct block_device *part,
112 struct disk_stats *stat)
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300113{
114 int cpu;
115
116 memset(stat, 0, sizeof(struct disk_stats));
117 for_each_possible_cpu(cpu) {
Christoph Hellwig0d021292020-11-27 16:43:51 +0100118 struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300119 int group;
120
121 for (group = 0; group < NR_STAT_GROUPS; group++) {
122 stat->nsecs[group] += ptr->nsecs[group];
123 stat->sectors[group] += ptr->sectors[group];
124 stat->ios[group] += ptr->ios[group];
125 stat->merges[group] += ptr->merges[group];
126 }
127
128 stat->io_ticks += ptr->io_ticks;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300129 }
130}
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300131
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100132static unsigned int part_in_flight(struct block_device *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600133{
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200134 unsigned int inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500135 int cpu;
136
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500137 for_each_possible_cpu(cpu) {
Mikulas Patockae016b782018-12-06 11:41:21 -0500138 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
139 part_stat_local_read_cpu(part, in_flight[1], cpu);
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500140 }
Mikulas Patockae016b782018-12-06 11:41:21 -0500141 if ((int)inflight < 0)
142 inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500143
Mikulas Patockae016b782018-12-06 11:41:21 -0500144 return inflight;
Jens Axboef299b7c2017-08-08 17:51:45 -0600145}
146
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100147static void part_in_flight_rw(struct block_device *part,
148 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700149{
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500150 int cpu;
151
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500152 inflight[0] = 0;
153 inflight[1] = 0;
154 for_each_possible_cpu(cpu) {
155 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
156 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
157 }
158 if ((int)inflight[0] < 0)
159 inflight[0] = 0;
160 if ((int)inflight[1] < 0)
161 inflight[1] = 0;
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
165 * Can be deleted altogether. Later.
166 *
167 */
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600168#define BLKDEV_MAJOR_HASH_SIZE 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static struct blk_major_name {
170 struct blk_major_name *next;
171 int major;
172 char name[16];
Christoph Hellwiga160c612020-10-29 15:58:28 +0100173 void (*probe)(dev_t devt);
Joe Korty68eef3b2006-03-31 02:30:32 -0800174} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100175static DEFINE_MUTEX(major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100178static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Joe Korty68eef3b2006-03-31 02:30:32 -0800180 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Joe Korty68eef3b2006-03-31 02:30:32 -0800183#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200184void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800185{
Joe Korty68eef3b2006-03-31 02:30:32 -0800186 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800187
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100188 mutex_lock(&major_names_lock);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600189 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
190 if (dp->major == offset)
Tejun Heocf771cb2008-09-03 09:01:09 +0200191 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100192 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
Joe Korty68eef3b2006-03-31 02:30:32 -0800194#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100196/**
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100197 * __register_blkdev - register a new block device
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100198 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800199 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
200 * @major = 0, try to allocate any unused major number.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100201 * @name: the name of the new block device as a zero terminated string
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100202 * @probe: allback that is called on access to any minor number of @major
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100203 *
204 * The @name must be unique within the system.
205 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300206 * The return value depends on the @major input parameter:
207 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800208 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
209 * then the function returns zero on success, or a negative error code
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300210 * - if any unused major number was requested with @major = 0 parameter
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100211 * then the return value is the allocated major number in range
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800212 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
213 *
214 * See Documentation/admin-guide/devices.txt for the list of allocated
215 * major numbers.
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100216 *
217 * Use register_blkdev instead for any new code.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100218 */
Christoph Hellwiga160c612020-10-29 15:58:28 +0100219int __register_blkdev(unsigned int major, const char *name,
220 void (*probe)(dev_t devt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct blk_major_name **n, *p;
223 int index, ret = 0;
224
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100225 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* temporary */
228 if (major == 0) {
229 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
230 if (major_names[index] == NULL)
231 break;
232 }
233
234 if (index == 0) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500235 printk("%s: failed to get major for %s\n",
236 __func__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 ret = -EBUSY;
238 goto out;
239 }
240 major = index;
241 ret = major;
242 }
243
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600244 if (major >= BLKDEV_MAJOR_MAX) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500245 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
246 __func__, major, BLKDEV_MAJOR_MAX-1, name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600247
248 ret = -EINVAL;
249 goto out;
250 }
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
253 if (p == NULL) {
254 ret = -ENOMEM;
255 goto out;
256 }
257
258 p->major = major;
Christoph Hellwiga160c612020-10-29 15:58:28 +0100259 p->probe = probe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 strlcpy(p->name, name, sizeof(p->name));
261 p->next = NULL;
262 index = major_to_index(major);
263
264 for (n = &major_names[index]; *n; n = &(*n)->next) {
265 if ((*n)->major == major)
266 break;
267 }
268 if (!*n)
269 *n = p;
270 else
271 ret = -EBUSY;
272
273 if (ret < 0) {
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800274 printk("register_blkdev: cannot get major %u for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 major, name);
276 kfree(p);
277 }
278out:
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100279 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return ret;
281}
Christoph Hellwiga160c612020-10-29 15:58:28 +0100282EXPORT_SYMBOL(__register_blkdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Akinobu Mitaf4480242007-07-17 04:03:47 -0700284void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct blk_major_name **n;
287 struct blk_major_name *p = NULL;
288 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100290 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 for (n = &major_names[index]; *n; n = &(*n)->next)
292 if ((*n)->major == major)
293 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700294 if (!*n || strcmp((*n)->name, name)) {
295 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700296 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 p = *n;
298 *n = p->next;
299 }
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100300 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304EXPORT_SYMBOL(unregister_blkdev);
305
Tejun Heobcce3de2008-08-25 19:47:22 +0900306/**
Tejun Heo870d6652008-08-25 19:47:25 +0900307 * blk_mangle_minor - scatter minor numbers apart
308 * @minor: minor number to mangle
309 *
310 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
311 * is enabled. Mangling twice gives the original value.
312 *
313 * RETURNS:
314 * Mangled value.
315 *
316 * CONTEXT:
317 * Don't care.
318 */
319static int blk_mangle_minor(int minor)
320{
321#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
322 int i;
323
324 for (i = 0; i < MINORBITS / 2; i++) {
325 int low = minor & (1 << i);
326 int high = minor & (1 << (MINORBITS - 1 - i));
327 int distance = MINORBITS - 1 - 2 * i;
328
329 minor ^= low | high; /* clear both bits */
330 low <<= distance; /* swap the positions */
331 high >>= distance;
332 minor |= low | high; /* and set */
333 }
334#endif
335 return minor;
336}
337
338/**
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100339 * blk_alloc_devt - allocate a dev_t for a block device
340 * @bdev: block device to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900341 * @devt: out parameter for resulting dev_t
342 *
343 * Allocate a dev_t for block device.
344 *
345 * RETURNS:
346 * 0 on success, allocated dev_t is returned in *@devt. -errno on
347 * failure.
348 *
349 * CONTEXT:
350 * Might sleep.
351 */
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100352int blk_alloc_devt(struct block_device *bdev, dev_t *devt)
Tejun Heobcce3de2008-08-25 19:47:22 +0900353{
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100354 struct gendisk *disk = bdev->bd_disk;
Tejun Heobab998d2013-02-27 17:03:57 -0800355 int idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900356
357 /* in consecutive minor range? */
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100358 if (bdev->bd_partno < disk->minors) {
359 *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
Tejun Heobcce3de2008-08-25 19:47:22 +0900360 return 0;
361 }
362
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100363 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
Tejun Heobab998d2013-02-27 17:03:57 -0800364 if (idx < 0)
365 return idx == -ENOSPC ? -EBUSY : idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900366
Tejun Heo870d6652008-08-25 19:47:25 +0900367 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900368 return 0;
369}
370
371/**
372 * blk_free_devt - free a dev_t
373 * @devt: dev_t to free
374 *
375 * Free @devt which was allocated using blk_alloc_devt().
376 *
377 * CONTEXT:
378 * Might sleep.
379 */
380void blk_free_devt(dev_t devt)
381{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100382 if (MAJOR(devt) == BLOCK_EXT_MAJOR)
383 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800384}
385
Tejun Heo1f014292008-08-25 19:47:23 +0900386static char *bdevt_str(dev_t devt, char *buf)
387{
388 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
389 char tbuf[BDEVT_SIZE];
390 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
391 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
392 } else
393 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
394
395 return buf;
396}
397
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100398void disk_uevent(struct gendisk *disk, enum kobject_action action)
399{
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100400 struct block_device *part;
Christoph Hellwig32121352021-04-06 08:23:02 +0200401 unsigned long idx;
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100402
Christoph Hellwig32121352021-04-06 08:23:02 +0200403 rcu_read_lock();
404 xa_for_each(&disk->part_tbl, idx, part) {
405 if (bdev_is_partition(part) && !bdev_nr_sectors(part))
406 continue;
407 if (!bdgrab(part))
408 continue;
409
410 rcu_read_unlock();
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100411 kobject_uevent(bdev_kobj(part), action);
Christoph Hellwig32121352021-04-06 08:23:02 +0200412 bdput(part);
413 rcu_read_lock();
414 }
415 rcu_read_unlock();
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100416}
417EXPORT_SYMBOL_GPL(disk_uevent);
418
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200419static void disk_scan_partitions(struct gendisk *disk)
420{
421 struct block_device *bdev;
422
423 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
424 return;
425
426 set_bit(GD_NEED_PART_SCAN, &disk->state);
427 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
428 if (!IS_ERR(bdev))
429 blkdev_put(bdev, FMODE_READ);
430}
431
Hannes Reineckefef912b2018-09-28 08:17:19 +0200432static void register_disk(struct device *parent, struct gendisk *disk,
433 const struct attribute_group **groups)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100434{
435 struct device *ddev = disk_to_dev(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100436 int err;
437
Dan Williamse63a46b2016-06-15 18:17:27 -0700438 ddev->parent = parent;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100439
Kees Cookffc8b302013-07-03 15:01:14 -0700440 dev_set_name(ddev, "%s", disk->disk_name);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100441
442 /* delay uevents, until we scanned partition table */
443 dev_set_uevent_suppress(ddev, 1);
444
Hannes Reineckefef912b2018-09-28 08:17:19 +0200445 if (groups) {
446 WARN_ON(ddev->groups);
447 ddev->groups = groups;
448 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100449 if (device_add(ddev))
450 return;
451 if (!sysfs_deprecated) {
452 err = sysfs_create_link(block_depr, &ddev->kobj,
453 kobject_name(&ddev->kobj));
454 if (err) {
455 device_del(ddev);
456 return;
457 }
458 }
Ming Lei25e823c2013-02-22 16:34:13 -0800459
460 /*
461 * avoid probable deadlock caused by allocating memory with
462 * GFP_KERNEL in runtime_resume callback of its all ancestor
463 * devices
464 */
465 pm_runtime_set_memalloc_noio(ddev, true);
466
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100467 disk->part0->bd_holder_dir =
468 kobject_create_and_add("holders", &ddev->kobj);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100469 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
470
Daniel Wagner9ec49142021-03-11 16:19:17 +0100471 if (disk->flags & GENHD_FL_HIDDEN)
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300472 return;
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300473
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200474 disk_scan_partitions(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100475
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100476 /* announce the disk and partitions after all partitions are created */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100477 dev_set_uevent_suppress(ddev, 0);
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100478 disk_uevent(disk, KOBJ_ADD);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300479
zhengbin4d7c1d32019-02-20 21:27:05 +0800480 if (disk->queue->backing_dev_info->dev) {
481 err = sysfs_create_link(&ddev->kobj,
482 &disk->queue->backing_dev_info->dev->kobj,
483 "bdi");
484 WARN_ON(err);
485 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/**
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500489 * __device_add_disk - add disk information to kernel list
Dan Williamse63a46b2016-06-15 18:17:27 -0700490 * @parent: parent device for the disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * @disk: per-device partitioning information
Hannes Reineckefef912b2018-09-28 08:17:19 +0200492 * @groups: Additional per-device sysfs groups
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500493 * @register_queue: register the queue if set to true
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 *
495 * This function registers the partitioning information in @disk
496 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900497 *
498 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500500static void __device_add_disk(struct device *parent, struct gendisk *disk,
Hannes Reineckefef912b2018-09-28 08:17:19 +0200501 const struct attribute_group **groups,
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500502 bool register_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900504 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400505 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700506
Damien Le Moal737eb782019-09-05 18:51:33 +0900507 /*
508 * The disk queue should now be all set with enough information about
509 * the device for the elevator code to pick an adequate default
510 * elevator if one is needed, that is, for devices requesting queue
511 * registration.
512 */
513 if (register_queue)
514 elevator_init_mq(disk->queue);
515
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900516 /* minors == 0 indicates to use ext devt from part0 and should
517 * be accompanied with EXT_DEVT flag. Make sure all
518 * parameters make sense.
519 */
520 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300521 WARN_ON(!disk->minors &&
522 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900525
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100526 retval = blk_alloc_devt(disk->part0, &devt);
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900527 if (retval) {
528 WARN_ON(1);
529 return;
530 }
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900531 disk->major = MAJOR(devt);
532 disk->first_minor = MINOR(devt);
533
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +0100534 disk_alloc_events(disk);
535
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300536 if (disk->flags & GENHD_FL_HIDDEN) {
537 /*
538 * Don't let hidden disks show up in /proc/partitions,
539 * and don't bother scanning for partitions either.
540 */
541 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
542 disk->flags |= GENHD_FL_NO_PART_SCAN;
543 } else {
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200544 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
545 struct device *dev = disk_to_dev(disk);
weiping zhang3a921682017-10-31 18:38:59 +0800546 int ret;
547
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300548 /* Register BDI before referencing it from bdev */
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200549 dev->devt = devt;
550 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
weiping zhang3a921682017-10-31 18:38:59 +0800551 WARN_ON(ret);
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200552 bdi_set_owner(bdi, dev);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100553 bdev_add(disk->part0, devt);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300554 }
Hannes Reineckefef912b2018-09-28 08:17:19 +0200555 register_disk(parent, disk, groups);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500556 if (register_queue)
557 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700558
Tejun Heo523e1d32011-10-19 14:31:07 +0200559 /*
560 * Take an extra ref on queue which will be put on disk_release()
561 * so that it sticks around as long as @disk is there.
562 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100563 WARN_ON_ONCE(!blk_get_queue(disk->queue));
Tejun Heo523e1d32011-10-19 14:31:07 +0200564
Tejun Heo77ea8872010-12-08 20:57:37 +0100565 disk_add_events(disk);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400566 blk_integrity_add(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500568
Hannes Reineckefef912b2018-09-28 08:17:19 +0200569void device_add_disk(struct device *parent, struct gendisk *disk,
570 const struct attribute_group **groups)
571
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500572{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200573 __device_add_disk(parent, disk, groups, true);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500574}
Dan Williamse63a46b2016-06-15 18:17:27 -0700575EXPORT_SYMBOL(device_add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500577void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
578{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200579 __device_add_disk(parent, disk, NULL, false);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500580}
581EXPORT_SYMBOL(device_add_disk_no_queue_reg);
582
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000583/**
584 * del_gendisk - remove the gendisk
585 * @disk: the struct gendisk to remove
586 *
587 * Removes the gendisk and all its associated resources. This deletes the
588 * partitions associated with the gendisk, and unregisters the associated
589 * request_queue.
590 *
591 * This is the counter to the respective __device_add_disk() call.
592 *
593 * The final removal of the struct gendisk happens when its refcount reaches 0
594 * with put_disk(), which should be called after del_gendisk(), if
595 * __device_add_disk() was used.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000596 *
597 * Drivers exist which depend on the release of the gendisk to be synchronous,
598 * it should not be deferred.
599 *
600 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000601 */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100602void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000604 might_sleep();
605
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100606 if (WARN_ON_ONCE(!disk->queue))
607 return;
608
Martin K. Petersen25520d52015-10-21 13:19:49 -0400609 blk_integrity_del(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +0100610 disk_del_events(disk);
611
Jan Kara56c09082018-02-26 13:01:41 +0100612 /*
613 * Block lookups of the disk until all bdevs are unhashed and the
614 * disk is marked as dead (GENHD_FL_UP cleared).
615 */
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100616 down_write(&bdev_lookup_sem);
Christoph Hellwigc76f48e2021-04-06 08:22:56 +0200617
618 mutex_lock(&disk->part0->bd_mutex);
Christoph Hellwigd3c4a432021-04-06 08:22:55 +0200619 blk_drop_partitions(disk);
Christoph Hellwigc76f48e2021-04-06 08:22:56 +0200620 mutex_unlock(&disk->part0->bd_mutex);
621
Christoph Hellwig45611832021-04-06 08:22:53 +0200622 fsync_bdev(disk->part0);
623 __invalidate_device(disk->part0, true);
624
625 /*
626 * Unhash the bdev inode for this device so that it can't be looked
627 * up any more even if openers still hold references to it.
628 */
629 remove_inode_hash(disk->part0->bd_inode);
630
Tejun Heod2bf1b62010-12-08 20:57:36 +0100631 set_capacity(disk, 0);
632 disk->flags &= ~GENHD_FL_UP;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100633 up_write(&bdev_lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100634
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100635 if (!(disk->flags & GENHD_FL_HIDDEN)) {
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300636 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100637
Jan Kara90f16fd2017-03-08 17:48:33 +0100638 /*
639 * Unregister bdi before releasing device numbers (as they can
640 * get reused and we'd get clashes in sysfs).
641 */
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100642 bdi_unregister(disk->queue->backing_dev_info);
Jan Kara90f16fd2017-03-08 17:48:33 +0100643 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100644
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100645 blk_unregister_queue(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100646
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100647 kobject_put(disk->part0->bd_holder_dir);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100648 kobject_put(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100650 part_stat_set_all(disk->part0, 0);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100651 disk->part0->bd_stamp = 0;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100652 if (!sysfs_deprecated)
653 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
Ming Lei25e823c2013-02-22 16:34:13 -0800654 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100655 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100657EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Vishal Verma99e66082016-01-09 08:36:51 -0800659/* sysfs access to bad-blocks list. */
660static ssize_t disk_badblocks_show(struct device *dev,
661 struct device_attribute *attr,
662 char *page)
663{
664 struct gendisk *disk = dev_to_disk(dev);
665
666 if (!disk->bb)
667 return sprintf(page, "\n");
668
669 return badblocks_show(disk->bb, page, 0);
670}
671
672static ssize_t disk_badblocks_store(struct device *dev,
673 struct device_attribute *attr,
674 const char *page, size_t len)
675{
676 struct gendisk *disk = dev_to_disk(dev);
677
678 if (!disk->bb)
679 return -ENXIO;
680
681 return badblocks_store(disk->bb, page, len, 0);
682}
683
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100684void blk_request_module(dev_t devt)
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100685{
Christoph Hellwiga160c612020-10-29 15:58:28 +0100686 unsigned int major = MAJOR(devt);
687 struct blk_major_name **n;
688
689 mutex_lock(&major_names_lock);
690 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
691 if ((*n)->major == major && (*n)->probe) {
692 (*n)->probe(devt);
693 mutex_unlock(&major_names_lock);
694 return;
695 }
696 }
697 mutex_unlock(&major_names_lock);
698
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100699 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
700 /* Make old-style 2.4 aliases work */
701 request_module("block-major-%d", MAJOR(devt));
702}
703
Tejun Heof331c022008-09-03 09:01:48 +0200704/**
705 * bdget_disk - do bdget() by gendisk and partition number
706 * @disk: gendisk of interest
707 * @partno: partition number
708 *
709 * Find partition @partno from @disk, do bdget() on it.
710 *
711 * CONTEXT:
712 * Don't care.
713 *
714 * RETURNS:
715 * Resulting block_device on success, NULL on failure.
716 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200717struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200718{
Tejun Heo548b10e2008-08-29 09:01:47 +0200719 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200720
Christoph Hellwig0d021292020-11-27 16:43:51 +0100721 rcu_read_lock();
Christoph Hellwiga33df752021-01-24 11:02:41 +0100722 bdev = xa_load(&disk->part_tbl, partno);
Christoph Hellwig0d021292020-11-27 16:43:51 +0100723 if (bdev && !bdgrab(bdev))
724 bdev = NULL;
725 rcu_read_unlock();
Tejun Heof331c022008-09-03 09:01:48 +0200726
Tejun Heo548b10e2008-08-29 09:01:47 +0200727 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200728}
Tejun Heof331c022008-09-03 09:01:48 +0200729
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700730/*
731 * print a full list of all partitions - intended for places where the root
732 * filesystem can't be mounted and thus to give the victim some idea of what
733 * went wrong
734 */
735void __init printk_all_partitions(void)
736{
Tejun Heodef4e382008-09-03 08:57:12 +0200737 struct class_dev_iter iter;
738 struct device *dev;
739
740 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
741 while ((dev = class_dev_iter_next(&iter))) {
742 struct gendisk *disk = dev_to_disk(dev);
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100743 struct block_device *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900744 char name_buf[BDEVNAME_SIZE];
745 char devt_buf[BDEVT_SIZE];
Christoph Hellwige559f582021-04-06 08:22:59 +0200746 unsigned long idx;
Tejun Heodef4e382008-09-03 08:57:12 +0200747
748 /*
749 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300750 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +0200751 */
752 if (get_capacity(disk) == 0 ||
753 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
754 continue;
755
756 /*
Christoph Hellwige559f582021-04-06 08:22:59 +0200757 * Note, unlike /proc/partitions, I am showing the numbers in
758 * hex - the same format as the root= option takes.
Tejun Heodef4e382008-09-03 08:57:12 +0200759 */
Christoph Hellwige559f582021-04-06 08:22:59 +0200760 rcu_read_lock();
761 xa_for_each(&disk->part_tbl, idx, part) {
762 if (!bdev_nr_sectors(part))
763 continue;
764 printk("%s%s %10llu %s %s",
765 bdev_is_partition(part) ? " " : "",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100766 bdevt_str(part->bd_dev, devt_buf),
767 bdev_nr_sectors(part) >> 1,
768 disk_name(disk, part->bd_partno, name_buf),
769 part->bd_meta_info ?
770 part->bd_meta_info->uuid : "");
Christoph Hellwige559f582021-04-06 08:22:59 +0200771 if (bdev_is_partition(part))
Tejun Heo074a7ac2008-08-25 19:56:14 +0900772 printk("\n");
Christoph Hellwige559f582021-04-06 08:22:59 +0200773 else if (dev->parent && dev->parent->driver)
774 printk(" driver: %s\n",
775 dev->parent->driver->name);
776 else
777 printk(" (driver?)\n");
Tejun Heo074a7ac2008-08-25 19:56:14 +0900778 }
Christoph Hellwige559f582021-04-06 08:22:59 +0200779 rcu_read_unlock();
Tejun Heodef4e382008-09-03 08:57:12 +0200780 }
781 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784#ifdef CONFIG_PROC_FS
785/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200786static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400787{
Tejun Heodef4e382008-09-03 08:57:12 +0200788 loff_t skip = *pos;
789 struct class_dev_iter *iter;
790 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400791
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200792 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +0200793 if (!iter)
794 return ERR_PTR(-ENOMEM);
795
796 seqf->private = iter;
797 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
798 do {
799 dev = class_dev_iter_next(iter);
800 if (!dev)
801 return NULL;
802 } while (skip--);
803
804 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400805}
806
Tejun Heodef4e382008-09-03 08:57:12 +0200807static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200809 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400810
Tejun Heodef4e382008-09-03 08:57:12 +0200811 (*pos)++;
812 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200813 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400814 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return NULL;
817}
818
Tejun Heodef4e382008-09-03 08:57:12 +0200819static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400820{
Tejun Heodef4e382008-09-03 08:57:12 +0200821 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400822
Tejun Heodef4e382008-09-03 08:57:12 +0200823 /* stop is called even after start failed :-( */
824 if (iter) {
825 class_dev_iter_exit(iter);
826 kfree(iter);
Vegard Nossum77da1602016-07-29 10:40:31 +0200827 seqf->private = NULL;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Tejun Heodef4e382008-09-03 08:57:12 +0200831static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Jianpeng Ma06768062012-08-03 10:42:00 +0200833 void *p;
Tejun Heodef4e382008-09-03 08:57:12 +0200834
835 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +0100836 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +0200837 seq_puts(seqf, "major minor #blocks name\n\n");
838 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Tejun Heocf771cb2008-09-03 09:01:09 +0200841static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct gendisk *sgp = v;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100844 struct block_device *part;
Christoph Hellwigecc75a92021-04-06 08:23:00 +0200845 unsigned long idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 char buf[BDEVNAME_SIZE];
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +0200849 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200850 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 0;
852 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
853 return 0;
854
Christoph Hellwigecc75a92021-04-06 08:23:00 +0200855 rcu_read_lock();
856 xa_for_each(&sgp->part_tbl, idx, part) {
857 if (!bdev_nr_sectors(part))
858 continue;
Tejun Heo1f014292008-08-25 19:47:23 +0900859 seq_printf(seqf, "%4d %7d %10llu %s\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100860 MAJOR(part->bd_dev), MINOR(part->bd_dev),
861 bdev_nr_sectors(part) >> 1,
862 disk_name(sgp, part->bd_partno, buf));
Christoph Hellwigecc75a92021-04-06 08:23:00 +0200863 }
864 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866}
867
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400868static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200869 .start = show_partition_start,
870 .next = disk_seqf_next,
871 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200872 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873};
874#endif
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876static int __init genhd_device_init(void)
877{
Dan Williamse105b8b2008-04-21 10:51:07 -0700878 int error;
879
880 block_class.dev_kobj = sysfs_dev_block_kobj;
881 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700882 if (unlikely(error))
883 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200885
Zhang, Yanmin561ec682008-11-14 08:26:30 +0100886 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
887
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200888 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +0200889 if (!sysfs_deprecated)
890 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894subsys_initcall(genhd_device_init);
895
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200896static ssize_t disk_range_show(struct device *dev,
897 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200899 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200901 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Tejun Heo1f014292008-08-25 19:47:23 +0900904static ssize_t disk_ext_range_show(struct device *dev,
905 struct device_attribute *attr, char *buf)
906{
907 struct gendisk *disk = dev_to_disk(dev);
908
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200909 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +0900910}
911
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200912static ssize_t disk_removable_show(struct device *dev,
913 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200914{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200915 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200916
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200917 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200919}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300921static ssize_t disk_hidden_show(struct device *dev,
922 struct device_attribute *attr, char *buf)
923{
924 struct gendisk *disk = dev_to_disk(dev);
925
926 return sprintf(buf, "%d\n",
927 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
928}
929
Kay Sievers1c9ce522008-06-13 09:41:00 +0200930static ssize_t disk_ro_show(struct device *dev,
931 struct device_attribute *attr, char *buf)
932{
933 struct gendisk *disk = dev_to_disk(dev);
934
Tejun Heob7db9952008-08-25 19:56:10 +0900935 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200936}
937
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100938ssize_t part_size_show(struct device *dev,
939 struct device_attribute *attr, char *buf)
940{
Christoph Hellwig0d021292020-11-27 16:43:51 +0100941 return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100942}
943
944ssize_t part_stat_show(struct device *dev,
945 struct device_attribute *attr, char *buf)
946{
Christoph Hellwig0d021292020-11-27 16:43:51 +0100947 struct block_device *bdev = dev_to_bdev(dev);
948 struct request_queue *q = bdev->bd_disk->queue;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300949 struct disk_stats stat;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100950 unsigned int inflight;
951
Christoph Hellwig0d021292020-11-27 16:43:51 +0100952 part_stat_read_all(bdev, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200953 if (queue_is_mq(q))
Christoph Hellwig0d021292020-11-27 16:43:51 +0100954 inflight = blk_mq_in_flight(q, bdev);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200955 else
Christoph Hellwig0d021292020-11-27 16:43:51 +0100956 inflight = part_in_flight(bdev);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300957
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100958 return sprintf(buf,
959 "%8lu %8lu %8llu %8u "
960 "%8lu %8lu %8llu %8u "
961 "%8u %8u %8u "
962 "%8lu %8lu %8llu %8u "
963 "%8lu %8u"
964 "\n",
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300965 stat.ios[STAT_READ],
966 stat.merges[STAT_READ],
967 (unsigned long long)stat.sectors[STAT_READ],
968 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
969 stat.ios[STAT_WRITE],
970 stat.merges[STAT_WRITE],
971 (unsigned long long)stat.sectors[STAT_WRITE],
972 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100973 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300974 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +0300975 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
976 stat.nsecs[STAT_WRITE] +
977 stat.nsecs[STAT_DISCARD] +
978 stat.nsecs[STAT_FLUSH],
979 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300980 stat.ios[STAT_DISCARD],
981 stat.merges[STAT_DISCARD],
982 (unsigned long long)stat.sectors[STAT_DISCARD],
983 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
984 stat.ios[STAT_FLUSH],
985 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100986}
987
988ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
989 char *buf)
990{
Christoph Hellwig0d021292020-11-27 16:43:51 +0100991 struct block_device *bdev = dev_to_bdev(dev);
992 struct request_queue *q = bdev->bd_disk->queue;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100993 unsigned int inflight[2];
994
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200995 if (queue_is_mq(q))
Christoph Hellwig0d021292020-11-27 16:43:51 +0100996 blk_mq_in_flight_rw(q, bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200997 else
Christoph Hellwig0d021292020-11-27 16:43:51 +0100998 part_in_flight_rw(bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200999
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001000 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1001}
1002
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001003static ssize_t disk_capability_show(struct device *dev,
1004 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001005{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001006 struct gendisk *disk = dev_to_disk(dev);
1007
1008 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001009}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001010
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001011static ssize_t disk_alignment_offset_show(struct device *dev,
1012 struct device_attribute *attr,
1013 char *buf)
1014{
1015 struct gendisk *disk = dev_to_disk(dev);
1016
1017 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1018}
1019
Martin K. Petersen86b37282009-11-10 11:50:21 +01001020static ssize_t disk_discard_alignment_show(struct device *dev,
1021 struct device_attribute *attr,
1022 char *buf)
1023{
1024 struct gendisk *disk = dev_to_disk(dev);
1025
Martin K. Petersendd3d1452010-01-11 03:21:48 -05001026 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +01001027}
1028
Joe Perches5657a812018-05-24 13:38:59 -06001029static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1030static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1031static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1032static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1033static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1034static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1035static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1036static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1037static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1038static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1039static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1040static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001041
Akinobu Mitac17bb492006-12-08 02:39:46 -08001042#ifdef CONFIG_FAIL_MAKE_REQUEST
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001043ssize_t part_fail_show(struct device *dev,
1044 struct device_attribute *attr, char *buf)
1045{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001046 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001047}
1048
1049ssize_t part_fail_store(struct device *dev,
1050 struct device_attribute *attr,
1051 const char *buf, size_t count)
1052{
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001053 int i;
1054
1055 if (count > 0 && sscanf(buf, "%d", &i) > 0)
Christoph Hellwig0d021292020-11-27 16:43:51 +01001056 dev_to_bdev(dev)->bd_make_it_fail = i;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001057
1058 return count;
1059}
1060
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001061static struct device_attribute dev_attr_fail =
Joe Perches5657a812018-05-24 13:38:59 -06001062 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001063#endif /* CONFIG_FAIL_MAKE_REQUEST */
1064
Jens Axboe581d4e22008-09-14 05:56:33 -07001065#ifdef CONFIG_FAIL_IO_TIMEOUT
1066static struct device_attribute dev_attr_fail_timeout =
Joe Perches5657a812018-05-24 13:38:59 -06001067 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
Jens Axboe581d4e22008-09-14 05:56:33 -07001068#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001069
1070static struct attribute *disk_attrs[] = {
1071 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +09001072 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001073 &dev_attr_removable.attr,
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001074 &dev_attr_hidden.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001075 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001076 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001077 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001078 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001079 &dev_attr_capability.attr,
1080 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001081 &dev_attr_inflight.attr,
Vishal Verma99e66082016-01-09 08:36:51 -08001082 &dev_attr_badblocks.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001083#ifdef CONFIG_FAIL_MAKE_REQUEST
1084 &dev_attr_fail.attr,
1085#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001086#ifdef CONFIG_FAIL_IO_TIMEOUT
1087 &dev_attr_fail_timeout.attr,
1088#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001089 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090};
1091
Dan Williams9438b3e02017-04-27 14:46:26 -07001092static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1093{
1094 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1095 struct gendisk *disk = dev_to_disk(dev);
1096
1097 if (a == &dev_attr_badblocks.attr && !disk->bb)
1098 return 0;
1099 return a->mode;
1100}
1101
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001102static struct attribute_group disk_attr_group = {
1103 .attrs = disk_attrs,
Dan Williams9438b3e02017-04-27 14:46:26 -07001104 .is_visible = disk_visible,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001105};
1106
David Brownella4dbd672009-06-24 10:06:31 -07001107static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001108 &disk_attr_group,
1109 NULL
1110};
1111
Tejun Heo540eed52008-08-25 19:56:15 +09001112/**
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001113 * disk_release - releases all allocated resources of the gendisk
1114 * @dev: the device representing this disk
1115 *
1116 * This function releases all allocated resources of the gendisk.
1117 *
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001118 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1119 * assigned. Since the request_queue sits on top of the gendisk for these
1120 * drivers we also call blk_put_queue() for them, and we expect the
1121 * request_queue refcount to reach 0 at this point, and so the request_queue
1122 * will also be freed prior to the disk.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001123 *
1124 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001125 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001126static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001128 struct gendisk *disk = dev_to_disk(dev);
1129
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001130 might_sleep();
1131
Keith Busch2da78092014-08-26 09:05:36 -06001132 blk_free_devt(dev->devt);
Tejun Heo77ea8872010-12-08 20:57:37 +01001133 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 kfree(disk->random);
Christoph Hellwiga33df752021-01-24 11:02:41 +01001135 xa_destroy(&disk->part_tbl);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001136 bdput(disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001137 if (disk->queue)
1138 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 kfree(disk);
1140}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001141struct class block_class = {
1142 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143};
1144
Kay Sievers3c2670e2013-04-06 09:56:00 -07001145static char *block_devnode(struct device *dev, umode_t *mode,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001146 kuid_t *uid, kgid_t *gid)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001147{
1148 struct gendisk *disk = dev_to_disk(dev);
1149
Christoph Hellwig348e1142020-03-27 09:07:17 +01001150 if (disk->fops->devnode)
1151 return disk->fops->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001152 return NULL;
1153}
1154
Boris Burkovef45fe42020-06-01 13:12:05 -07001155const struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001156 .name = "disk",
1157 .groups = disk_attr_groups,
1158 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001159 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160};
1161
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001162#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001163/*
1164 * aggregate disk stat collector. Uses the same stats that the sysfs
1165 * entries do, above, but makes them available through one seq_file.
1166 *
1167 * The output looks suspiciously like /proc/partitions with a bunch of
1168 * extra fields.
1169 */
1170static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 struct gendisk *gp = v;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001173 struct block_device *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 char buf[BDEVNAME_SIZE];
Mikulas Patockae016b782018-12-06 11:41:21 -05001175 unsigned int inflight;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001176 struct disk_stats stat;
Christoph Hellwig7fae67c2021-04-06 08:23:01 +02001177 unsigned long idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001180 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001181 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 " rio rmerge rsect ruse wio wmerge "
1183 "wsect wuse running use aveq"
1184 "\n\n");
1185 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001186
Christoph Hellwig7fae67c2021-04-06 08:23:01 +02001187 rcu_read_lock();
1188 xa_for_each(&gp->part_tbl, idx, hd) {
1189 if (bdev_is_partition(hd) && !bdev_nr_sectors(hd))
1190 continue;
Christoph Hellwig0d021292020-11-27 16:43:51 +01001191 part_stat_read_all(hd, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001192 if (queue_is_mq(gp->queue))
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001193 inflight = blk_mq_in_flight(gp->queue, hd);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001194 else
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001195 inflight = part_in_flight(hd);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001196
Michael Callahanbdca3c82018-07-18 04:47:40 -07001197 seq_printf(seqf, "%4d %7d %s "
1198 "%lu %lu %lu %u "
1199 "%lu %lu %lu %u "
1200 "%u %u %u "
Konstantin Khlebnikovb6866312019-11-21 13:40:26 +03001201 "%lu %lu %lu %u "
1202 "%lu %u"
1203 "\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001204 MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
1205 disk_name(gp, hd->bd_partno, buf),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001206 stat.ios[STAT_READ],
1207 stat.merges[STAT_READ],
1208 stat.sectors[STAT_READ],
1209 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1210 NSEC_PER_MSEC),
1211 stat.ios[STAT_WRITE],
1212 stat.merges[STAT_WRITE],
1213 stat.sectors[STAT_WRITE],
1214 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1215 NSEC_PER_MSEC),
Mikulas Patockae016b782018-12-06 11:41:21 -05001216 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001217 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001218 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1219 stat.nsecs[STAT_WRITE] +
1220 stat.nsecs[STAT_DISCARD] +
1221 stat.nsecs[STAT_FLUSH],
1222 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001223 stat.ios[STAT_DISCARD],
1224 stat.merges[STAT_DISCARD],
1225 stat.sectors[STAT_DISCARD],
1226 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1227 NSEC_PER_MSEC),
1228 stat.ios[STAT_FLUSH],
1229 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1230 NSEC_PER_MSEC)
Jerome Marchand28f39d52008-02-08 11:04:56 +01001231 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Christoph Hellwig7fae67c2021-04-06 08:23:01 +02001233 rcu_read_unlock();
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return 0;
1236}
1237
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001238static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001239 .start = disk_seqf_start,
1240 .next = disk_seqf_next,
1241 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 .show = diskstats_show
1243};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001244
1245static int __init proc_genhd_init(void)
1246{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001247 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1248 proc_create_seq("partitions", 0, NULL, &partitions_op);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001249 return 0;
1250}
1251module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001252#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Tejun Heocf771cb2008-09-03 09:01:09 +02001254dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001255{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001256 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001257 struct class_dev_iter iter;
1258 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001259
Tejun Heodef4e382008-09-03 08:57:12 +02001260 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1261 while ((dev = class_dev_iter_next(&iter))) {
1262 struct gendisk *disk = dev_to_disk(dev);
Christoph Hellwig0d021292020-11-27 16:43:51 +01001263 struct block_device *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001264
Kay Sievers3ada8b72009-01-06 10:44:43 -08001265 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001266 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001267
Neil Brown41b8c852009-02-18 10:33:59 +01001268 if (partno < disk->minors) {
1269 /* We need to return the right devno, even
1270 * if the partition doesn't exist yet.
1271 */
1272 devt = MKDEV(MAJOR(dev->devt),
1273 MINOR(dev->devt) + partno);
1274 break;
1275 }
Christoph Hellwig0d021292020-11-27 16:43:51 +01001276 part = bdget_disk(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001277 if (part) {
Christoph Hellwig0d021292020-11-27 16:43:51 +01001278 devt = part->bd_dev;
1279 bdput(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001280 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001281 }
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001282 }
Tejun Heodef4e382008-09-03 08:57:12 +02001283 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001284 return devt;
1285}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001286
Byungchul Parke319e1f2017-10-25 17:56:05 +09001287struct gendisk *__alloc_disk_node(int minors, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001288{
1289 struct gendisk *disk;
1290
Christoph Hellwigde65b012017-08-23 19:10:29 +02001291 if (minors > DISK_MAX_PARTS) {
1292 printk(KERN_ERR
Randy Dunlap7fb52622017-11-18 17:43:38 -08001293 "block: can't allocate more than %d partitions\n",
Christoph Hellwigde65b012017-08-23 19:10:29 +02001294 DISK_MAX_PARTS);
1295 minors = DISK_MAX_PARTS;
1296 }
Christoph Lameter19460892005-06-23 00:08:19 -07001297
Joe Perchesc1b511e2013-08-29 15:21:42 -07001298 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001299 if (!disk)
1300 return NULL;
Jens Axboe6c23a962011-01-07 08:43:37 +01001301
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001302 disk->part0 = bdev_alloc(disk, 0);
1303 if (!disk->part0)
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001304 goto out_free_disk;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001305
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001306 disk->node_id = node_id;
Christoph Hellwiga33df752021-01-24 11:02:41 +01001307 xa_init(&disk->part_tbl);
1308 if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL))
1309 goto out_destroy_part_tbl;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001310
1311 disk->minors = minors;
1312 rand_initialize_disk(disk);
1313 disk_to_dev(disk)->class = &block_class;
1314 disk_to_dev(disk)->type = &disk_type;
1315 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return disk;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001317
Christoph Hellwiga33df752021-01-24 11:02:41 +01001318out_destroy_part_tbl:
1319 xa_destroy(&disk->part_tbl);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001320 bdput(disk->part0);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001321out_free_disk:
1322 kfree(disk);
1323 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
Byungchul Parke319e1f2017-10-25 17:56:05 +09001325EXPORT_SYMBOL(__alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001327/**
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001328 * put_disk - decrements the gendisk refcount
Randy Dunlap0d20dcc2020-07-30 18:42:30 -07001329 * @disk: the struct gendisk to decrement the refcount for
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001330 *
1331 * This decrements the refcount for the struct gendisk. When this reaches 0
1332 * we'll have disk_release() called.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001333 *
1334 * Context: Any context, but the last reference must not be dropped from
1335 * atomic context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337void put_disk(struct gendisk *disk)
1338{
1339 if (disk)
Christoph Hellwigefdc41c2020-11-10 07:25:37 +01001340 put_device(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342EXPORT_SYMBOL(put_disk);
1343
Hannes Reineckee3264a42009-07-28 09:13:13 +02001344static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1345{
1346 char event[] = "DISK_RO=1";
1347 char *envp[] = { event, NULL };
1348
1349 if (!ro)
1350 event[8] = '0';
1351 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1352}
1353
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001354/**
1355 * set_disk_ro - set a gendisk read-only
1356 * @disk: gendisk to operate on
Lukas Bulwahn7f31bee2021-01-29 05:55:05 +01001357 * @read_only: %true to set the disk read-only, %false set the disk read/write
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001358 *
1359 * This function is used to indicate whether a given disk device should have its
1360 * read-only flag set. set_disk_ro() is typically used by device drivers to
1361 * indicate whether the underlying physical device is write-protected.
1362 */
1363void set_disk_ro(struct gendisk *disk, bool read_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001365 if (read_only) {
1366 if (test_and_set_bit(GD_READ_ONLY, &disk->state))
1367 return;
1368 } else {
1369 if (!test_and_clear_bit(GD_READ_ONLY, &disk->state))
1370 return;
Hannes Reineckee3264a42009-07-28 09:13:13 +02001371 }
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001372 set_disk_ro_uevent(disk, read_only);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374EXPORT_SYMBOL(set_disk_ro);
1375
1376int bdev_read_only(struct block_device *bdev)
1377{
Christoph Hellwig947139b2021-01-09 11:42:52 +01001378 return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380EXPORT_SYMBOL(bdev_read_only);
1381
Tejun Heo77ea8872010-12-08 20:57:37 +01001382/*
1383 * Disk events - monitor disk events like media change and eject request.
1384 */
1385struct disk_events {
1386 struct list_head node; /* all disk_event's */
1387 struct gendisk *disk; /* the associated disk */
1388 spinlock_t lock;
1389
Tejun Heofdd514e2011-06-09 20:43:59 +02001390 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001391 int block; /* event blocking depth */
1392 unsigned int pending; /* events already sent out */
1393 unsigned int clearing; /* events being cleared */
1394
1395 long poll_msecs; /* interval, -1 for default */
1396 struct delayed_work dwork;
1397};
1398
1399static const char *disk_events_strs[] = {
1400 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1401 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1402};
1403
1404static char *disk_uevents[] = {
1405 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1406 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1407};
1408
1409/* list of all disk_events */
1410static DEFINE_MUTEX(disk_events_mutex);
1411static LIST_HEAD(disk_events);
1412
1413/* disable in-kernel polling by default */
Wei Tang1fe8f342015-11-24 09:58:46 +08001414static unsigned long disk_events_dfl_poll_msecs;
Tejun Heo77ea8872010-12-08 20:57:37 +01001415
1416static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1417{
1418 struct disk_events *ev = disk->ev;
1419 long intv_msecs = 0;
1420
1421 /*
1422 * If device-specific poll interval is set, always use it. If
Martin Wilck673387a2019-03-27 14:51:01 +01001423 * the default is being used, poll if the POLL flag is set.
Tejun Heo77ea8872010-12-08 20:57:37 +01001424 */
1425 if (ev->poll_msecs >= 0)
1426 intv_msecs = ev->poll_msecs;
Martin Wilckc92e2f02019-03-27 14:51:02 +01001427 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
Tejun Heo77ea8872010-12-08 20:57:37 +01001428 intv_msecs = disk_events_dfl_poll_msecs;
1429
1430 return msecs_to_jiffies(intv_msecs);
1431}
1432
Tejun Heoc3af54a2011-06-09 20:43:55 +02001433/**
1434 * disk_block_events - block and flush disk event checking
1435 * @disk: disk to block events for
1436 *
1437 * On return from this function, it is guaranteed that event checking
1438 * isn't in progress and won't happen until unblocked by
1439 * disk_unblock_events(). Events blocking is counted and the actual
1440 * unblocking happens after the matching number of unblocks are done.
1441 *
1442 * Note that this intentionally does not block event checking from
1443 * disk_clear_events().
1444 *
1445 * CONTEXT:
1446 * Might sleep.
1447 */
1448void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001449{
1450 struct disk_events *ev = disk->ev;
1451 unsigned long flags;
1452 bool cancel;
1453
Tejun Heoc3af54a2011-06-09 20:43:55 +02001454 if (!ev)
1455 return;
1456
Tejun Heofdd514e2011-06-09 20:43:59 +02001457 /*
1458 * Outer mutex ensures that the first blocker completes canceling
1459 * the event work before further blockers are allowed to finish.
1460 */
1461 mutex_lock(&ev->block_mutex);
1462
Tejun Heo77ea8872010-12-08 20:57:37 +01001463 spin_lock_irqsave(&ev->lock, flags);
1464 cancel = !ev->block++;
1465 spin_unlock_irqrestore(&ev->lock, flags);
1466
Tejun Heoc3af54a2011-06-09 20:43:55 +02001467 if (cancel)
1468 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001469
1470 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001471}
1472
1473static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1474{
1475 struct disk_events *ev = disk->ev;
1476 unsigned long intv;
1477 unsigned long flags;
1478
1479 spin_lock_irqsave(&ev->lock, flags);
1480
1481 if (WARN_ON_ONCE(ev->block <= 0))
1482 goto out_unlock;
1483
1484 if (--ev->block)
1485 goto out_unlock;
1486
Tejun Heo77ea8872010-12-08 20:57:37 +01001487 intv = disk_events_poll_jiffies(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001488 if (check_now)
Viresh Kumar695588f2013-04-24 17:12:56 +05301489 queue_delayed_work(system_freezable_power_efficient_wq,
1490 &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001491 else if (intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301492 queue_delayed_work(system_freezable_power_efficient_wq,
1493 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001494out_unlock:
1495 spin_unlock_irqrestore(&ev->lock, flags);
1496}
1497
1498/**
Tejun Heo77ea8872010-12-08 20:57:37 +01001499 * disk_unblock_events - unblock disk event checking
1500 * @disk: disk to unblock events for
1501 *
1502 * Undo disk_block_events(). When the block count reaches zero, it
1503 * starts events polling if configured.
1504 *
1505 * CONTEXT:
1506 * Don't care. Safe to call from irq context.
1507 */
1508void disk_unblock_events(struct gendisk *disk)
1509{
1510 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01001511 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001512}
1513
1514/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02001515 * disk_flush_events - schedule immediate event checking and flushing
1516 * @disk: disk to check and flush events for
1517 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01001518 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02001519 * Schedule immediate event checking on @disk if not blocked. Events in
1520 * @mask are scheduled to be cleared from the driver. Note that this
1521 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01001522 *
1523 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02001524 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01001525 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001526void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001527{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001528 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001529
1530 if (!ev)
1531 return;
1532
Tejun Heo85ef06d2011-07-01 16:17:47 +02001533 spin_lock_irq(&ev->lock);
1534 ev->clearing |= mask;
Tejun Heo41f63c52012-08-03 10:30:47 -07001535 if (!ev->block)
Viresh Kumar695588f2013-04-24 17:12:56 +05301536 mod_delayed_work(system_freezable_power_efficient_wq,
1537 &ev->dwork, 0);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001538 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001539}
Tejun Heo77ea8872010-12-08 20:57:37 +01001540
1541/**
1542 * disk_clear_events - synchronously check, clear and return pending events
1543 * @disk: disk to fetch and clear events from
Masanari Iidada3dae52014-09-09 01:27:23 +09001544 * @mask: mask of events to be fetched and cleared
Tejun Heo77ea8872010-12-08 20:57:37 +01001545 *
1546 * Disk events are synchronously checked and pending events in @mask
1547 * are cleared and returned. This ignores the block count.
1548 *
1549 * CONTEXT:
1550 * Might sleep.
1551 */
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001552static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001553{
Tejun Heo77ea8872010-12-08 20:57:37 +01001554 struct disk_events *ev = disk->ev;
1555 unsigned int pending;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001556 unsigned int clearing = mask;
Tejun Heo77ea8872010-12-08 20:57:37 +01001557
Christoph Hellwiga564e232020-07-08 14:25:41 +02001558 if (!ev)
Tejun Heo77ea8872010-12-08 20:57:37 +01001559 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001560
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001561 disk_block_events(disk);
1562
1563 /*
1564 * store the union of mask and ev->clearing on the stack so that the
1565 * race with disk_flush_events does not cause ambiguity (ev->clearing
1566 * can still be modified even if events are blocked).
1567 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001568 spin_lock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001569 clearing |= ev->clearing;
1570 ev->clearing = 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001571 spin_unlock_irq(&ev->lock);
1572
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001573 disk_check_events(ev, &clearing);
Derek Basehoreaea24a82012-12-18 12:27:18 -08001574 /*
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001575 * if ev->clearing is not 0, the disk_flush_events got called in the
1576 * middle of this function, so we want to run the workfn without delay.
Derek Basehoreaea24a82012-12-18 12:27:18 -08001577 */
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001578 __disk_unblock_events(disk, ev->clearing ? true : false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001579
1580 /* then, fetch and clear pending events */
1581 spin_lock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001582 pending = ev->pending & mask;
1583 ev->pending &= ~mask;
1584 spin_unlock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001585 WARN_ON_ONCE(clearing & mask);
Tejun Heo77ea8872010-12-08 20:57:37 +01001586
1587 return pending;
1588}
1589
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001590/**
1591 * bdev_check_media_change - check if a removable media has been changed
1592 * @bdev: block device to check
1593 *
1594 * Check whether a removable media has been changed, and attempt to free all
1595 * dentries and inodes and invalidates all block device page cache entries in
1596 * that case.
1597 *
1598 * Returns %true if the block device changed, or %false if not.
1599 */
1600bool bdev_check_media_change(struct block_device *bdev)
1601{
1602 unsigned int events;
1603
1604 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1605 DISK_EVENT_EJECT_REQUEST);
1606 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1607 return false;
1608
1609 if (__invalidate_device(bdev, true))
1610 pr_warn("VFS: busy inodes on changed media %s\n",
1611 bdev->bd_disk->disk_name);
Christoph Hellwig38430f02020-09-21 09:19:45 +02001612 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001613 return true;
1614}
1615EXPORT_SYMBOL(bdev_check_media_change);
1616
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001617/*
1618 * Separate this part out so that a different pointer for clearing_ptr can be
1619 * passed in for disk_clear_events.
1620 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001621static void disk_events_workfn(struct work_struct *work)
1622{
1623 struct delayed_work *dwork = to_delayed_work(work);
1624 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001625
1626 disk_check_events(ev, &ev->clearing);
1627}
1628
1629static void disk_check_events(struct disk_events *ev,
1630 unsigned int *clearing_ptr)
1631{
Tejun Heo77ea8872010-12-08 20:57:37 +01001632 struct gendisk *disk = ev->disk;
1633 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001634 unsigned int clearing = *clearing_ptr;
Tejun Heo77ea8872010-12-08 20:57:37 +01001635 unsigned int events;
1636 unsigned long intv;
1637 int nr_events = 0, i;
1638
1639 /* check events */
1640 events = disk->fops->check_events(disk, clearing);
1641
1642 /* accumulate pending events and schedule next poll if necessary */
1643 spin_lock_irq(&ev->lock);
1644
1645 events &= ~ev->pending;
1646 ev->pending |= events;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001647 *clearing_ptr &= ~clearing;
Tejun Heo77ea8872010-12-08 20:57:37 +01001648
1649 intv = disk_events_poll_jiffies(disk);
1650 if (!ev->block && intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301651 queue_delayed_work(system_freezable_power_efficient_wq,
1652 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001653
1654 spin_unlock_irq(&ev->lock);
1655
Tejun Heo7c88a162011-04-21 19:43:58 +02001656 /*
1657 * Tell userland about new events. Only the events listed in
Martin Wilckc92e2f02019-03-27 14:51:02 +01001658 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1659 * is set. Otherwise, events are processed internally but never
1660 * get reported to userland.
Tejun Heo7c88a162011-04-21 19:43:58 +02001661 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001662 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Martin Wilckc92e2f02019-03-27 14:51:02 +01001663 if ((events & disk->events & (1 << i)) &&
1664 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
Tejun Heo77ea8872010-12-08 20:57:37 +01001665 envp[nr_events++] = disk_uevents[i];
1666
1667 if (nr_events)
1668 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1669}
1670
1671/*
1672 * A disk events enabled device has the following sysfs nodes under
1673 * its /sys/block/X/ directory.
1674 *
1675 * events : list of all supported events
1676 * events_async : list of events which can be detected w/o polling
Martin Wilck673387a2019-03-27 14:51:01 +01001677 * (always empty, only for backwards compatibility)
Tejun Heo77ea8872010-12-08 20:57:37 +01001678 * events_poll_msecs : polling interval, 0: disable, -1: system default
1679 */
1680static ssize_t __disk_events_show(unsigned int events, char *buf)
1681{
1682 const char *delim = "";
1683 ssize_t pos = 0;
1684 int i;
1685
1686 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1687 if (events & (1 << i)) {
1688 pos += sprintf(buf + pos, "%s%s",
1689 delim, disk_events_strs[i]);
1690 delim = " ";
1691 }
1692 if (pos)
1693 pos += sprintf(buf + pos, "\n");
1694 return pos;
1695}
1696
1697static ssize_t disk_events_show(struct device *dev,
1698 struct device_attribute *attr, char *buf)
1699{
1700 struct gendisk *disk = dev_to_disk(dev);
1701
Martin Wilckc92e2f02019-03-27 14:51:02 +01001702 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1703 return 0;
1704
Tejun Heo77ea8872010-12-08 20:57:37 +01001705 return __disk_events_show(disk->events, buf);
1706}
1707
1708static ssize_t disk_events_async_show(struct device *dev,
1709 struct device_attribute *attr, char *buf)
1710{
Martin Wilck673387a2019-03-27 14:51:01 +01001711 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001712}
1713
1714static ssize_t disk_events_poll_msecs_show(struct device *dev,
1715 struct device_attribute *attr,
1716 char *buf)
1717{
1718 struct gendisk *disk = dev_to_disk(dev);
1719
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001720 if (!disk->ev)
1721 return sprintf(buf, "-1\n");
1722
Tejun Heo77ea8872010-12-08 20:57:37 +01001723 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1724}
1725
1726static ssize_t disk_events_poll_msecs_store(struct device *dev,
1727 struct device_attribute *attr,
1728 const char *buf, size_t count)
1729{
1730 struct gendisk *disk = dev_to_disk(dev);
1731 long intv;
1732
1733 if (!count || !sscanf(buf, "%ld", &intv))
1734 return -EINVAL;
1735
1736 if (intv < 0 && intv != -1)
1737 return -EINVAL;
1738
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001739 if (!disk->ev)
1740 return -ENODEV;
1741
Tejun Heoc3af54a2011-06-09 20:43:55 +02001742 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001743 disk->ev->poll_msecs = intv;
1744 __disk_unblock_events(disk, true);
1745
1746 return count;
1747}
1748
Joe Perches5657a812018-05-24 13:38:59 -06001749static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
1750static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
1751static const DEVICE_ATTR(events_poll_msecs, 0644,
Tejun Heo77ea8872010-12-08 20:57:37 +01001752 disk_events_poll_msecs_show,
1753 disk_events_poll_msecs_store);
1754
1755static const struct attribute *disk_events_attrs[] = {
1756 &dev_attr_events.attr,
1757 &dev_attr_events_async.attr,
1758 &dev_attr_events_poll_msecs.attr,
1759 NULL,
1760};
1761
1762/*
1763 * The default polling interval can be specified by the kernel
1764 * parameter block.events_dfl_poll_msecs which defaults to 0
1765 * (disable). This can also be modified runtime by writing to
Akinobu Mita1624b0b2019-07-16 21:59:35 +09001766 * /sys/module/block/parameters/events_dfl_poll_msecs.
Tejun Heo77ea8872010-12-08 20:57:37 +01001767 */
1768static int disk_events_set_dfl_poll_msecs(const char *val,
1769 const struct kernel_param *kp)
1770{
1771 struct disk_events *ev;
1772 int ret;
1773
1774 ret = param_set_ulong(val, kp);
1775 if (ret < 0)
1776 return ret;
1777
1778 mutex_lock(&disk_events_mutex);
1779
1780 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02001781 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001782
1783 mutex_unlock(&disk_events_mutex);
1784
1785 return 0;
1786}
1787
1788static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1789 .set = disk_events_set_dfl_poll_msecs,
1790 .get = param_get_ulong,
1791};
1792
1793#undef MODULE_PARAM_PREFIX
1794#define MODULE_PARAM_PREFIX "block."
1795
1796module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1797 &disk_events_dfl_poll_msecs, 0644);
1798
1799/*
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001800 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
Tejun Heo77ea8872010-12-08 20:57:37 +01001801 */
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001802static void disk_alloc_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001803{
1804 struct disk_events *ev;
1805
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001806 if (!disk->fops->check_events || !disk->events)
Tejun Heo77ea8872010-12-08 20:57:37 +01001807 return;
1808
1809 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1810 if (!ev) {
1811 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1812 return;
1813 }
1814
Tejun Heo77ea8872010-12-08 20:57:37 +01001815 INIT_LIST_HEAD(&ev->node);
1816 ev->disk = disk;
1817 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02001818 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001819 ev->block = 1;
1820 ev->poll_msecs = -1;
1821 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
1822
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001823 disk->ev = ev;
1824}
1825
1826static void disk_add_events(struct gendisk *disk)
1827{
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001828 /* FIXME: error handling */
1829 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
1830 pr_warn("%s: failed to create sysfs files for events\n",
1831 disk->disk_name);
1832
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001833 if (!disk->ev)
1834 return;
1835
Tejun Heo77ea8872010-12-08 20:57:37 +01001836 mutex_lock(&disk_events_mutex);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001837 list_add_tail(&disk->ev->node, &disk_events);
Tejun Heo77ea8872010-12-08 20:57:37 +01001838 mutex_unlock(&disk_events_mutex);
1839
1840 /*
1841 * Block count is initialized to 1 and the following initial
1842 * unblock kicks it into action.
1843 */
1844 __disk_unblock_events(disk, true);
1845}
1846
1847static void disk_del_events(struct gendisk *disk)
1848{
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001849 if (disk->ev) {
1850 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001851
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001852 mutex_lock(&disk_events_mutex);
1853 list_del_init(&disk->ev->node);
1854 mutex_unlock(&disk_events_mutex);
1855 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001856
1857 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
1858}
1859
1860static void disk_release_events(struct gendisk *disk)
1861{
1862 /* the block count should be 1 from disk_del_events() */
1863 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
1864 kfree(disk->ev);
1865}