blob: 3daab80201df500f3810e3947c4a137db2aaa8a0 [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
Tejun Heobcce3de2008-08-25 19:47:22 +090032/* for extended dynamic devt allocation, currently only one major is used */
Tejun Heoce23bba2013-02-27 17:03:56 -080033#define NR_EXT_DEVT (1 << MINORBITS)
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +010034static DEFINE_IDA(ext_devt_ida);
Tejun Heobcce3de2008-08-25 19:47:22 +090035
Derek Basehore12c2bdb2012-12-18 12:27:20 -080036static void disk_check_events(struct disk_events *ev,
37 unsigned int *clearing_ptr);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +010038static void disk_alloc_events(struct gendisk *disk);
Tejun Heo77ea8872010-12-08 20:57:37 +010039static void disk_add_events(struct gendisk *disk);
40static void disk_del_events(struct gendisk *disk);
41static void disk_release_events(struct gendisk *disk);
42
Christoph Hellwiga7824832020-11-26 18:43:37 +010043void set_capacity(struct gendisk *disk, sector_t sectors)
44{
Christoph Hellwigcb8432d2020-11-26 18:47:17 +010045 struct block_device *bdev = disk->part0;
Christoph Hellwiga7824832020-11-26 18:43:37 +010046
Damien Le Moal0f472272021-03-01 12:04:02 +090047 spin_lock(&bdev->bd_size_lock);
Christoph Hellwiga7824832020-11-26 18:43:37 +010048 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
Damien Le Moal0f472272021-03-01 12:04:02 +090049 spin_unlock(&bdev->bd_size_lock);
Christoph Hellwiga7824832020-11-26 18:43:37 +010050}
51EXPORT_SYMBOL(set_capacity);
52
Balbir Singhe598a722020-03-13 05:30:05 +000053/*
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010054 * Set disk capacity and notify if the size is not currently zero and will not
55 * be set to zero. Returns true if a uevent was sent, otherwise false.
Balbir Singhe598a722020-03-13 05:30:05 +000056 */
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010057bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
Balbir Singhe598a722020-03-13 05:30:05 +000058{
59 sector_t capacity = get_capacity(disk);
Christoph Hellwiga7824832020-11-26 18:43:37 +010060 char *envp[] = { "RESIZE=1", NULL };
Balbir Singhe598a722020-03-13 05:30:05 +000061
62 set_capacity(disk, size);
Balbir Singhe598a722020-03-13 05:30:05 +000063
Christoph Hellwiga7824832020-11-26 18:43:37 +010064 /*
65 * Only print a message and send a uevent if the gendisk is user visible
66 * and alive. This avoids spamming the log and udev when setting the
67 * initial capacity during probing.
68 */
69 if (size == capacity ||
70 (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
71 return false;
Balbir Singhe598a722020-03-13 05:30:05 +000072
Christoph Hellwiga7824832020-11-26 18:43:37 +010073 pr_info("%s: detected capacity change from %lld to %lld\n",
Ming Lei452c0bf2021-02-23 16:50:15 +080074 disk->disk_name, capacity, size);
Christoph Hellwig7e890c32020-11-12 17:50:04 +010075
Christoph Hellwiga7824832020-11-26 18:43:37 +010076 /*
77 * Historically we did not send a uevent for changes to/from an empty
78 * device.
79 */
80 if (!capacity || !size)
81 return false;
82 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
83 return true;
Balbir Singhe598a722020-03-13 05:30:05 +000084}
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010085EXPORT_SYMBOL_GPL(set_capacity_and_notify);
Balbir Singhe598a722020-03-13 05:30:05 +000086
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +010087/*
88 * Format the device name of the indicated disk into the supplied buffer and
89 * return a pointer to that same buffer for convenience.
90 */
91char *disk_name(struct gendisk *hd, int partno, char *buf)
92{
93 if (!partno)
94 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
95 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
96 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
97 else
98 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
99
100 return buf;
101}
102
103const char *bdevname(struct block_device *bdev, char *buf)
104{
Christoph Hellwig8a63a862020-09-03 07:41:03 +0200105 return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +0100106}
107EXPORT_SYMBOL(bdevname);
Balbir Singhe598a722020-03-13 05:30:05 +0000108
Christoph Hellwig0d021292020-11-27 16:43:51 +0100109static void part_stat_read_all(struct block_device *part,
110 struct disk_stats *stat)
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300111{
112 int cpu;
113
114 memset(stat, 0, sizeof(struct disk_stats));
115 for_each_possible_cpu(cpu) {
Christoph Hellwig0d021292020-11-27 16:43:51 +0100116 struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300117 int group;
118
119 for (group = 0; group < NR_STAT_GROUPS; group++) {
120 stat->nsecs[group] += ptr->nsecs[group];
121 stat->sectors[group] += ptr->sectors[group];
122 stat->ios[group] += ptr->ios[group];
123 stat->merges[group] += ptr->merges[group];
124 }
125
126 stat->io_ticks += ptr->io_ticks;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300127 }
128}
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300129
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100130static unsigned int part_in_flight(struct block_device *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600131{
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200132 unsigned int inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500133 int cpu;
134
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500135 for_each_possible_cpu(cpu) {
Mikulas Patockae016b782018-12-06 11:41:21 -0500136 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
137 part_stat_local_read_cpu(part, in_flight[1], cpu);
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500138 }
Mikulas Patockae016b782018-12-06 11:41:21 -0500139 if ((int)inflight < 0)
140 inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500141
Mikulas Patockae016b782018-12-06 11:41:21 -0500142 return inflight;
Jens Axboef299b7c2017-08-08 17:51:45 -0600143}
144
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100145static void part_in_flight_rw(struct block_device *part,
146 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700147{
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500148 int cpu;
149
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500150 inflight[0] = 0;
151 inflight[1] = 0;
152 for_each_possible_cpu(cpu) {
153 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
154 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
155 }
156 if ((int)inflight[0] < 0)
157 inflight[0] = 0;
158 if ((int)inflight[1] < 0)
159 inflight[1] = 0;
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/*
163 * Can be deleted altogether. Later.
164 *
165 */
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600166#define BLKDEV_MAJOR_HASH_SIZE 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static struct blk_major_name {
168 struct blk_major_name *next;
169 int major;
170 char name[16];
Christoph Hellwiga160c612020-10-29 15:58:28 +0100171 void (*probe)(dev_t devt);
Joe Korty68eef3b2006-03-31 02:30:32 -0800172} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100173static DEFINE_MUTEX(major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100176static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Joe Korty68eef3b2006-03-31 02:30:32 -0800178 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Joe Korty68eef3b2006-03-31 02:30:32 -0800181#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200182void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800183{
Joe Korty68eef3b2006-03-31 02:30:32 -0800184 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800185
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100186 mutex_lock(&major_names_lock);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600187 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
188 if (dp->major == offset)
Tejun Heocf771cb2008-09-03 09:01:09 +0200189 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100190 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
Joe Korty68eef3b2006-03-31 02:30:32 -0800192#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100194/**
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100195 * __register_blkdev - register a new block device
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100196 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800197 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
198 * @major = 0, try to allocate any unused major number.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100199 * @name: the name of the new block device as a zero terminated string
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100200 * @probe: allback that is called on access to any minor number of @major
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100201 *
202 * The @name must be unique within the system.
203 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300204 * The return value depends on the @major input parameter:
205 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800206 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
207 * then the function returns zero on success, or a negative error code
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300208 * - if any unused major number was requested with @major = 0 parameter
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100209 * then the return value is the allocated major number in range
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800210 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
211 *
212 * See Documentation/admin-guide/devices.txt for the list of allocated
213 * major numbers.
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100214 *
215 * Use register_blkdev instead for any new code.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100216 */
Christoph Hellwiga160c612020-10-29 15:58:28 +0100217int __register_blkdev(unsigned int major, const char *name,
218 void (*probe)(dev_t devt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct blk_major_name **n, *p;
221 int index, ret = 0;
222
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100223 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* temporary */
226 if (major == 0) {
227 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
228 if (major_names[index] == NULL)
229 break;
230 }
231
232 if (index == 0) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500233 printk("%s: failed to get major for %s\n",
234 __func__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ret = -EBUSY;
236 goto out;
237 }
238 major = index;
239 ret = major;
240 }
241
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600242 if (major >= BLKDEV_MAJOR_MAX) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500243 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
244 __func__, major, BLKDEV_MAJOR_MAX-1, name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600245
246 ret = -EINVAL;
247 goto out;
248 }
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
251 if (p == NULL) {
252 ret = -ENOMEM;
253 goto out;
254 }
255
256 p->major = major;
Christoph Hellwiga160c612020-10-29 15:58:28 +0100257 p->probe = probe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 strlcpy(p->name, name, sizeof(p->name));
259 p->next = NULL;
260 index = major_to_index(major);
261
262 for (n = &major_names[index]; *n; n = &(*n)->next) {
263 if ((*n)->major == major)
264 break;
265 }
266 if (!*n)
267 *n = p;
268 else
269 ret = -EBUSY;
270
271 if (ret < 0) {
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800272 printk("register_blkdev: cannot get major %u for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 major, name);
274 kfree(p);
275 }
276out:
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100277 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return ret;
279}
Christoph Hellwiga160c612020-10-29 15:58:28 +0100280EXPORT_SYMBOL(__register_blkdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Akinobu Mitaf4480242007-07-17 04:03:47 -0700282void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct blk_major_name **n;
285 struct blk_major_name *p = NULL;
286 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100288 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 for (n = &major_names[index]; *n; n = &(*n)->next)
290 if ((*n)->major == major)
291 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700292 if (!*n || strcmp((*n)->name, name)) {
293 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700294 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 p = *n;
296 *n = p->next;
297 }
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100298 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302EXPORT_SYMBOL(unregister_blkdev);
303
Tejun Heobcce3de2008-08-25 19:47:22 +0900304/**
Tejun Heo870d6652008-08-25 19:47:25 +0900305 * blk_mangle_minor - scatter minor numbers apart
306 * @minor: minor number to mangle
307 *
308 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
309 * is enabled. Mangling twice gives the original value.
310 *
311 * RETURNS:
312 * Mangled value.
313 *
314 * CONTEXT:
315 * Don't care.
316 */
317static int blk_mangle_minor(int minor)
318{
319#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
320 int i;
321
322 for (i = 0; i < MINORBITS / 2; i++) {
323 int low = minor & (1 << i);
324 int high = minor & (1 << (MINORBITS - 1 - i));
325 int distance = MINORBITS - 1 - 2 * i;
326
327 minor ^= low | high; /* clear both bits */
328 low <<= distance; /* swap the positions */
329 high >>= distance;
330 minor |= low | high; /* and set */
331 }
332#endif
333 return minor;
334}
335
Christoph Hellwig7c3f8282021-05-21 07:50:51 +0200336int blk_alloc_ext_minor(void)
Tejun Heobcce3de2008-08-25 19:47:22 +0900337{
Tejun Heobab998d2013-02-27 17:03:57 -0800338 int idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900339
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100340 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
Christoph Hellwig7c3f8282021-05-21 07:50:51 +0200341 if (idx < 0) {
342 if (idx == -ENOSPC)
343 return -EBUSY;
344 return idx;
345 }
346 return blk_mangle_minor(idx);
Tejun Heobcce3de2008-08-25 19:47:22 +0900347}
348
Christoph Hellwig7c3f8282021-05-21 07:50:51 +0200349void blk_free_ext_minor(unsigned int minor)
Tejun Heobcce3de2008-08-25 19:47:22 +0900350{
Christoph Hellwig7c3f8282021-05-21 07:50:51 +0200351 ida_free(&ext_devt_ida, blk_mangle_minor(minor));
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800352}
353
Tejun Heo1f014292008-08-25 19:47:23 +0900354static char *bdevt_str(dev_t devt, char *buf)
355{
356 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
357 char tbuf[BDEVT_SIZE];
358 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
359 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
360 } else
361 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
362
363 return buf;
364}
365
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100366void disk_uevent(struct gendisk *disk, enum kobject_action action)
367{
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100368 struct block_device *part;
Christoph Hellwig32121352021-04-06 08:23:02 +0200369 unsigned long idx;
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100370
Christoph Hellwig32121352021-04-06 08:23:02 +0200371 rcu_read_lock();
372 xa_for_each(&disk->part_tbl, idx, part) {
373 if (bdev_is_partition(part) && !bdev_nr_sectors(part))
374 continue;
375 if (!bdgrab(part))
376 continue;
377
378 rcu_read_unlock();
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100379 kobject_uevent(bdev_kobj(part), action);
Christoph Hellwig32121352021-04-06 08:23:02 +0200380 bdput(part);
381 rcu_read_lock();
382 }
383 rcu_read_unlock();
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100384}
385EXPORT_SYMBOL_GPL(disk_uevent);
386
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200387static void disk_scan_partitions(struct gendisk *disk)
388{
389 struct block_device *bdev;
390
391 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
392 return;
393
394 set_bit(GD_NEED_PART_SCAN, &disk->state);
395 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
396 if (!IS_ERR(bdev))
397 blkdev_put(bdev, FMODE_READ);
398}
399
Hannes Reineckefef912b2018-09-28 08:17:19 +0200400static void register_disk(struct device *parent, struct gendisk *disk,
401 const struct attribute_group **groups)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100402{
403 struct device *ddev = disk_to_dev(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100404 int err;
405
Dan Williamse63a46b2016-06-15 18:17:27 -0700406 ddev->parent = parent;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100407
Kees Cookffc8b302013-07-03 15:01:14 -0700408 dev_set_name(ddev, "%s", disk->disk_name);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100409
410 /* delay uevents, until we scanned partition table */
411 dev_set_uevent_suppress(ddev, 1);
412
Hannes Reineckefef912b2018-09-28 08:17:19 +0200413 if (groups) {
414 WARN_ON(ddev->groups);
415 ddev->groups = groups;
416 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100417 if (device_add(ddev))
418 return;
419 if (!sysfs_deprecated) {
420 err = sysfs_create_link(block_depr, &ddev->kobj,
421 kobject_name(&ddev->kobj));
422 if (err) {
423 device_del(ddev);
424 return;
425 }
426 }
Ming Lei25e823c2013-02-22 16:34:13 -0800427
428 /*
429 * avoid probable deadlock caused by allocating memory with
430 * GFP_KERNEL in runtime_resume callback of its all ancestor
431 * devices
432 */
433 pm_runtime_set_memalloc_noio(ddev, true);
434
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100435 disk->part0->bd_holder_dir =
436 kobject_create_and_add("holders", &ddev->kobj);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100437 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
438
Daniel Wagner9ec49142021-03-11 16:19:17 +0100439 if (disk->flags & GENHD_FL_HIDDEN)
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300440 return;
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300441
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200442 disk_scan_partitions(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100443
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100444 /* announce the disk and partitions after all partitions are created */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100445 dev_set_uevent_suppress(ddev, 0);
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100446 disk_uevent(disk, KOBJ_ADD);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300447
zhengbin4d7c1d32019-02-20 21:27:05 +0800448 if (disk->queue->backing_dev_info->dev) {
449 err = sysfs_create_link(&ddev->kobj,
450 &disk->queue->backing_dev_info->dev->kobj,
451 "bdi");
452 WARN_ON(err);
453 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/**
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500457 * __device_add_disk - add disk information to kernel list
Dan Williamse63a46b2016-06-15 18:17:27 -0700458 * @parent: parent device for the disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * @disk: per-device partitioning information
Hannes Reineckefef912b2018-09-28 08:17:19 +0200460 * @groups: Additional per-device sysfs groups
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500461 * @register_queue: register the queue if set to true
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *
463 * This function registers the partitioning information in @disk
464 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900465 *
466 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500468static void __device_add_disk(struct device *parent, struct gendisk *disk,
Hannes Reineckefef912b2018-09-28 08:17:19 +0200469 const struct attribute_group **groups,
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500470 bool register_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Christoph Hellwig7c3f8282021-05-21 07:50:51 +0200472 int ret;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700473
Damien Le Moal737eb782019-09-05 18:51:33 +0900474 /*
475 * The disk queue should now be all set with enough information about
476 * the device for the elevator code to pick an adequate default
477 * elevator if one is needed, that is, for devices requesting queue
478 * registration.
479 */
480 if (register_queue)
481 elevator_init_mq(disk->queue);
482
Christoph Hellwig7c3f8282021-05-21 07:50:51 +0200483 /*
484 * If the driver provides an explicit major number it also must provide
485 * the number of minors numbers supported, and those will be used to
486 * setup the gendisk.
487 * Otherwise just allocate the device numbers for both the whole device
488 * and all partitions from the extended dev_t space.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900489 */
Christoph Hellwig7c3f8282021-05-21 07:50:51 +0200490 if (disk->major) {
491 WARN_ON(!disk->minors);
492 } else {
493 WARN_ON(disk->minors);
494 WARN_ON(!(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
495
496 ret = blk_alloc_ext_minor();
497 if (ret < 0) {
498 WARN_ON(1);
499 return;
500 }
501 disk->major = BLOCK_EXT_MAJOR;
502 disk->first_minor = MINOR(ret);
503 }
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900506
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +0100507 disk_alloc_events(disk);
508
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300509 if (disk->flags & GENHD_FL_HIDDEN) {
510 /*
511 * Don't let hidden disks show up in /proc/partitions,
512 * and don't bother scanning for partitions either.
513 */
514 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
515 disk->flags |= GENHD_FL_NO_PART_SCAN;
516 } else {
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200517 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
518 struct device *dev = disk_to_dev(disk);
weiping zhang3a921682017-10-31 18:38:59 +0800519
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300520 /* Register BDI before referencing it from bdev */
Christoph Hellwig7c3f8282021-05-21 07:50:51 +0200521 dev->devt = MKDEV(disk->major, disk->first_minor);
522 ret = bdi_register(bdi, "%u:%u",
523 disk->major, disk->first_minor);
weiping zhang3a921682017-10-31 18:38:59 +0800524 WARN_ON(ret);
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200525 bdi_set_owner(bdi, dev);
Christoph Hellwig7c3f8282021-05-21 07:50:51 +0200526 bdev_add(disk->part0, dev->devt);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300527 }
Hannes Reineckefef912b2018-09-28 08:17:19 +0200528 register_disk(parent, disk, groups);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500529 if (register_queue)
530 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700531
Tejun Heo523e1d32011-10-19 14:31:07 +0200532 /*
533 * Take an extra ref on queue which will be put on disk_release()
534 * so that it sticks around as long as @disk is there.
535 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100536 WARN_ON_ONCE(!blk_get_queue(disk->queue));
Tejun Heo523e1d32011-10-19 14:31:07 +0200537
Tejun Heo77ea8872010-12-08 20:57:37 +0100538 disk_add_events(disk);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400539 blk_integrity_add(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500541
Hannes Reineckefef912b2018-09-28 08:17:19 +0200542void device_add_disk(struct device *parent, struct gendisk *disk,
543 const struct attribute_group **groups)
544
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500545{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200546 __device_add_disk(parent, disk, groups, true);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500547}
Dan Williamse63a46b2016-06-15 18:17:27 -0700548EXPORT_SYMBOL(device_add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500550void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
551{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200552 __device_add_disk(parent, disk, NULL, false);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500553}
554EXPORT_SYMBOL(device_add_disk_no_queue_reg);
555
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000556/**
557 * del_gendisk - remove the gendisk
558 * @disk: the struct gendisk to remove
559 *
560 * Removes the gendisk and all its associated resources. This deletes the
561 * partitions associated with the gendisk, and unregisters the associated
562 * request_queue.
563 *
564 * This is the counter to the respective __device_add_disk() call.
565 *
566 * The final removal of the struct gendisk happens when its refcount reaches 0
567 * with put_disk(), which should be called after del_gendisk(), if
568 * __device_add_disk() was used.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000569 *
570 * Drivers exist which depend on the release of the gendisk to be synchronous,
571 * it should not be deferred.
572 *
573 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000574 */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100575void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000577 might_sleep();
578
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100579 if (WARN_ON_ONCE(!disk->queue))
580 return;
581
Martin K. Petersen25520d52015-10-21 13:19:49 -0400582 blk_integrity_del(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +0100583 disk_del_events(disk);
584
Christoph Hellwigc76f48e2021-04-06 08:22:56 +0200585 mutex_lock(&disk->part0->bd_mutex);
Christoph Hellwig6c60ff02021-05-14 15:18:41 +0200586 disk->flags &= ~GENHD_FL_UP;
Christoph Hellwigd3c4a432021-04-06 08:22:55 +0200587 blk_drop_partitions(disk);
Christoph Hellwigc76f48e2021-04-06 08:22:56 +0200588 mutex_unlock(&disk->part0->bd_mutex);
589
Christoph Hellwig45611832021-04-06 08:22:53 +0200590 fsync_bdev(disk->part0);
591 __invalidate_device(disk->part0, true);
592
593 /*
594 * Unhash the bdev inode for this device so that it can't be looked
595 * up any more even if openers still hold references to it.
596 */
597 remove_inode_hash(disk->part0->bd_inode);
598
Tejun Heod2bf1b62010-12-08 20:57:36 +0100599 set_capacity(disk, 0);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100600
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100601 if (!(disk->flags & GENHD_FL_HIDDEN)) {
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300602 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100603
Jan Kara90f16fd2017-03-08 17:48:33 +0100604 /*
605 * Unregister bdi before releasing device numbers (as they can
606 * get reused and we'd get clashes in sysfs).
607 */
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100608 bdi_unregister(disk->queue->backing_dev_info);
Jan Kara90f16fd2017-03-08 17:48:33 +0100609 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100610
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100611 blk_unregister_queue(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100612
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100613 kobject_put(disk->part0->bd_holder_dir);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100614 kobject_put(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100616 part_stat_set_all(disk->part0, 0);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100617 disk->part0->bd_stamp = 0;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100618 if (!sysfs_deprecated)
619 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
Ming Lei25e823c2013-02-22 16:34:13 -0800620 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100621 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100623EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Vishal Verma99e66082016-01-09 08:36:51 -0800625/* sysfs access to bad-blocks list. */
626static ssize_t disk_badblocks_show(struct device *dev,
627 struct device_attribute *attr,
628 char *page)
629{
630 struct gendisk *disk = dev_to_disk(dev);
631
632 if (!disk->bb)
633 return sprintf(page, "\n");
634
635 return badblocks_show(disk->bb, page, 0);
636}
637
638static ssize_t disk_badblocks_store(struct device *dev,
639 struct device_attribute *attr,
640 const char *page, size_t len)
641{
642 struct gendisk *disk = dev_to_disk(dev);
643
644 if (!disk->bb)
645 return -ENXIO;
646
647 return badblocks_store(disk->bb, page, len, 0);
648}
649
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100650void blk_request_module(dev_t devt)
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100651{
Christoph Hellwiga160c612020-10-29 15:58:28 +0100652 unsigned int major = MAJOR(devt);
653 struct blk_major_name **n;
654
655 mutex_lock(&major_names_lock);
656 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
657 if ((*n)->major == major && (*n)->probe) {
658 (*n)->probe(devt);
659 mutex_unlock(&major_names_lock);
660 return;
661 }
662 }
663 mutex_unlock(&major_names_lock);
664
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100665 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
666 /* Make old-style 2.4 aliases work */
667 request_module("block-major-%d", MAJOR(devt));
668}
669
Tejun Heof331c022008-09-03 09:01:48 +0200670/**
671 * bdget_disk - do bdget() by gendisk and partition number
672 * @disk: gendisk of interest
673 * @partno: partition number
674 *
675 * Find partition @partno from @disk, do bdget() on it.
676 *
677 * CONTEXT:
678 * Don't care.
679 *
680 * RETURNS:
681 * Resulting block_device on success, NULL on failure.
682 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200683struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200684{
Tejun Heo548b10e2008-08-29 09:01:47 +0200685 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200686
Christoph Hellwig0d021292020-11-27 16:43:51 +0100687 rcu_read_lock();
Christoph Hellwiga33df752021-01-24 11:02:41 +0100688 bdev = xa_load(&disk->part_tbl, partno);
Christoph Hellwig0d021292020-11-27 16:43:51 +0100689 if (bdev && !bdgrab(bdev))
690 bdev = NULL;
691 rcu_read_unlock();
Tejun Heof331c022008-09-03 09:01:48 +0200692
Tejun Heo548b10e2008-08-29 09:01:47 +0200693 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200694}
Tejun Heof331c022008-09-03 09:01:48 +0200695
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700696/*
697 * print a full list of all partitions - intended for places where the root
698 * filesystem can't be mounted and thus to give the victim some idea of what
699 * went wrong
700 */
701void __init printk_all_partitions(void)
702{
Tejun Heodef4e382008-09-03 08:57:12 +0200703 struct class_dev_iter iter;
704 struct device *dev;
705
706 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
707 while ((dev = class_dev_iter_next(&iter))) {
708 struct gendisk *disk = dev_to_disk(dev);
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100709 struct block_device *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900710 char name_buf[BDEVNAME_SIZE];
711 char devt_buf[BDEVT_SIZE];
Christoph Hellwige559f582021-04-06 08:22:59 +0200712 unsigned long idx;
Tejun Heodef4e382008-09-03 08:57:12 +0200713
714 /*
715 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300716 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +0200717 */
718 if (get_capacity(disk) == 0 ||
719 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
720 continue;
721
722 /*
Christoph Hellwige559f582021-04-06 08:22:59 +0200723 * Note, unlike /proc/partitions, I am showing the numbers in
724 * hex - the same format as the root= option takes.
Tejun Heodef4e382008-09-03 08:57:12 +0200725 */
Christoph Hellwige559f582021-04-06 08:22:59 +0200726 rcu_read_lock();
727 xa_for_each(&disk->part_tbl, idx, part) {
728 if (!bdev_nr_sectors(part))
729 continue;
730 printk("%s%s %10llu %s %s",
731 bdev_is_partition(part) ? " " : "",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100732 bdevt_str(part->bd_dev, devt_buf),
733 bdev_nr_sectors(part) >> 1,
734 disk_name(disk, part->bd_partno, name_buf),
735 part->bd_meta_info ?
736 part->bd_meta_info->uuid : "");
Christoph Hellwige559f582021-04-06 08:22:59 +0200737 if (bdev_is_partition(part))
Tejun Heo074a7ac2008-08-25 19:56:14 +0900738 printk("\n");
Christoph Hellwige559f582021-04-06 08:22:59 +0200739 else if (dev->parent && dev->parent->driver)
740 printk(" driver: %s\n",
741 dev->parent->driver->name);
742 else
743 printk(" (driver?)\n");
Tejun Heo074a7ac2008-08-25 19:56:14 +0900744 }
Christoph Hellwige559f582021-04-06 08:22:59 +0200745 rcu_read_unlock();
Tejun Heodef4e382008-09-03 08:57:12 +0200746 }
747 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750#ifdef CONFIG_PROC_FS
751/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200752static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400753{
Tejun Heodef4e382008-09-03 08:57:12 +0200754 loff_t skip = *pos;
755 struct class_dev_iter *iter;
756 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400757
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200758 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +0200759 if (!iter)
760 return ERR_PTR(-ENOMEM);
761
762 seqf->private = iter;
763 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
764 do {
765 dev = class_dev_iter_next(iter);
766 if (!dev)
767 return NULL;
768 } while (skip--);
769
770 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400771}
772
Tejun Heodef4e382008-09-03 08:57:12 +0200773static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200775 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400776
Tejun Heodef4e382008-09-03 08:57:12 +0200777 (*pos)++;
778 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200779 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400780 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return NULL;
783}
784
Tejun Heodef4e382008-09-03 08:57:12 +0200785static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400786{
Tejun Heodef4e382008-09-03 08:57:12 +0200787 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400788
Tejun Heodef4e382008-09-03 08:57:12 +0200789 /* stop is called even after start failed :-( */
790 if (iter) {
791 class_dev_iter_exit(iter);
792 kfree(iter);
Vegard Nossum77da1602016-07-29 10:40:31 +0200793 seqf->private = NULL;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Tejun Heodef4e382008-09-03 08:57:12 +0200797static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Jianpeng Ma06768062012-08-03 10:42:00 +0200799 void *p;
Tejun Heodef4e382008-09-03 08:57:12 +0200800
801 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +0100802 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +0200803 seq_puts(seqf, "major minor #blocks name\n\n");
804 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Tejun Heocf771cb2008-09-03 09:01:09 +0200807static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct gendisk *sgp = v;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100810 struct block_device *part;
Christoph Hellwigecc75a92021-04-06 08:23:00 +0200811 unsigned long idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 char buf[BDEVNAME_SIZE];
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +0200815 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200816 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return 0;
818 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
819 return 0;
820
Christoph Hellwigecc75a92021-04-06 08:23:00 +0200821 rcu_read_lock();
822 xa_for_each(&sgp->part_tbl, idx, part) {
823 if (!bdev_nr_sectors(part))
824 continue;
Tejun Heo1f014292008-08-25 19:47:23 +0900825 seq_printf(seqf, "%4d %7d %10llu %s\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100826 MAJOR(part->bd_dev), MINOR(part->bd_dev),
827 bdev_nr_sectors(part) >> 1,
828 disk_name(sgp, part->bd_partno, buf));
Christoph Hellwigecc75a92021-04-06 08:23:00 +0200829 }
830 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 0;
832}
833
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400834static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200835 .start = show_partition_start,
836 .next = disk_seqf_next,
837 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200838 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839};
840#endif
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842static int __init genhd_device_init(void)
843{
Dan Williamse105b8b2008-04-21 10:51:07 -0700844 int error;
845
846 block_class.dev_kobj = sysfs_dev_block_kobj;
847 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700848 if (unlikely(error))
849 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200851
Zhang, Yanmin561ec682008-11-14 08:26:30 +0100852 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
853
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200854 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +0200855 if (!sysfs_deprecated)
856 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860subsys_initcall(genhd_device_init);
861
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200862static ssize_t disk_range_show(struct device *dev,
863 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200865 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200867 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Tejun Heo1f014292008-08-25 19:47:23 +0900870static ssize_t disk_ext_range_show(struct device *dev,
871 struct device_attribute *attr, char *buf)
872{
873 struct gendisk *disk = dev_to_disk(dev);
874
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200875 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +0900876}
877
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200878static ssize_t disk_removable_show(struct device *dev,
879 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200880{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200881 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200882
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200883 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200885}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300887static ssize_t disk_hidden_show(struct device *dev,
888 struct device_attribute *attr, char *buf)
889{
890 struct gendisk *disk = dev_to_disk(dev);
891
892 return sprintf(buf, "%d\n",
893 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
894}
895
Kay Sievers1c9ce522008-06-13 09:41:00 +0200896static ssize_t disk_ro_show(struct device *dev,
897 struct device_attribute *attr, char *buf)
898{
899 struct gendisk *disk = dev_to_disk(dev);
900
Tejun Heob7db9952008-08-25 19:56:10 +0900901 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200902}
903
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100904ssize_t part_size_show(struct device *dev,
905 struct device_attribute *attr, char *buf)
906{
Christoph Hellwig0d021292020-11-27 16:43:51 +0100907 return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100908}
909
910ssize_t part_stat_show(struct device *dev,
911 struct device_attribute *attr, char *buf)
912{
Christoph Hellwig0d021292020-11-27 16:43:51 +0100913 struct block_device *bdev = dev_to_bdev(dev);
914 struct request_queue *q = bdev->bd_disk->queue;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300915 struct disk_stats stat;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100916 unsigned int inflight;
917
Christoph Hellwig0d021292020-11-27 16:43:51 +0100918 part_stat_read_all(bdev, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200919 if (queue_is_mq(q))
Christoph Hellwig0d021292020-11-27 16:43:51 +0100920 inflight = blk_mq_in_flight(q, bdev);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200921 else
Christoph Hellwig0d021292020-11-27 16:43:51 +0100922 inflight = part_in_flight(bdev);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300923
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100924 return sprintf(buf,
925 "%8lu %8lu %8llu %8u "
926 "%8lu %8lu %8llu %8u "
927 "%8u %8u %8u "
928 "%8lu %8lu %8llu %8u "
929 "%8lu %8u"
930 "\n",
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300931 stat.ios[STAT_READ],
932 stat.merges[STAT_READ],
933 (unsigned long long)stat.sectors[STAT_READ],
934 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
935 stat.ios[STAT_WRITE],
936 stat.merges[STAT_WRITE],
937 (unsigned long long)stat.sectors[STAT_WRITE],
938 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100939 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300940 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +0300941 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
942 stat.nsecs[STAT_WRITE] +
943 stat.nsecs[STAT_DISCARD] +
944 stat.nsecs[STAT_FLUSH],
945 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300946 stat.ios[STAT_DISCARD],
947 stat.merges[STAT_DISCARD],
948 (unsigned long long)stat.sectors[STAT_DISCARD],
949 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
950 stat.ios[STAT_FLUSH],
951 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100952}
953
954ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
955 char *buf)
956{
Christoph Hellwig0d021292020-11-27 16:43:51 +0100957 struct block_device *bdev = dev_to_bdev(dev);
958 struct request_queue *q = bdev->bd_disk->queue;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100959 unsigned int inflight[2];
960
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200961 if (queue_is_mq(q))
Christoph Hellwig0d021292020-11-27 16:43:51 +0100962 blk_mq_in_flight_rw(q, bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200963 else
Christoph Hellwig0d021292020-11-27 16:43:51 +0100964 part_in_flight_rw(bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200965
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +0100966 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
967}
968
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200969static ssize_t disk_capability_show(struct device *dev,
970 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700971{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200972 struct gendisk *disk = dev_to_disk(dev);
973
974 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700975}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200976
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400977static ssize_t disk_alignment_offset_show(struct device *dev,
978 struct device_attribute *attr,
979 char *buf)
980{
981 struct gendisk *disk = dev_to_disk(dev);
982
983 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
984}
985
Martin K. Petersen86b37282009-11-10 11:50:21 +0100986static ssize_t disk_discard_alignment_show(struct device *dev,
987 struct device_attribute *attr,
988 char *buf)
989{
990 struct gendisk *disk = dev_to_disk(dev);
991
Martin K. Petersendd3d1452010-01-11 03:21:48 -0500992 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +0100993}
994
Joe Perches5657a812018-05-24 13:38:59 -0600995static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
996static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
997static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
998static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
999static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1000static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1001static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1002static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1003static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1004static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1005static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1006static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001007
Akinobu Mitac17bb492006-12-08 02:39:46 -08001008#ifdef CONFIG_FAIL_MAKE_REQUEST
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001009ssize_t part_fail_show(struct device *dev,
1010 struct device_attribute *attr, char *buf)
1011{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001012 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001013}
1014
1015ssize_t part_fail_store(struct device *dev,
1016 struct device_attribute *attr,
1017 const char *buf, size_t count)
1018{
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001019 int i;
1020
1021 if (count > 0 && sscanf(buf, "%d", &i) > 0)
Christoph Hellwig0d021292020-11-27 16:43:51 +01001022 dev_to_bdev(dev)->bd_make_it_fail = i;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001023
1024 return count;
1025}
1026
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001027static struct device_attribute dev_attr_fail =
Joe Perches5657a812018-05-24 13:38:59 -06001028 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001029#endif /* CONFIG_FAIL_MAKE_REQUEST */
1030
Jens Axboe581d4e22008-09-14 05:56:33 -07001031#ifdef CONFIG_FAIL_IO_TIMEOUT
1032static struct device_attribute dev_attr_fail_timeout =
Joe Perches5657a812018-05-24 13:38:59 -06001033 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
Jens Axboe581d4e22008-09-14 05:56:33 -07001034#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001035
1036static struct attribute *disk_attrs[] = {
1037 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +09001038 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001039 &dev_attr_removable.attr,
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001040 &dev_attr_hidden.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001041 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001042 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001043 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001044 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001045 &dev_attr_capability.attr,
1046 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001047 &dev_attr_inflight.attr,
Vishal Verma99e66082016-01-09 08:36:51 -08001048 &dev_attr_badblocks.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001049#ifdef CONFIG_FAIL_MAKE_REQUEST
1050 &dev_attr_fail.attr,
1051#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001052#ifdef CONFIG_FAIL_IO_TIMEOUT
1053 &dev_attr_fail_timeout.attr,
1054#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001055 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056};
1057
Dan Williams9438b3e02017-04-27 14:46:26 -07001058static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1059{
1060 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1061 struct gendisk *disk = dev_to_disk(dev);
1062
1063 if (a == &dev_attr_badblocks.attr && !disk->bb)
1064 return 0;
1065 return a->mode;
1066}
1067
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001068static struct attribute_group disk_attr_group = {
1069 .attrs = disk_attrs,
Dan Williams9438b3e02017-04-27 14:46:26 -07001070 .is_visible = disk_visible,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001071};
1072
David Brownella4dbd672009-06-24 10:06:31 -07001073static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001074 &disk_attr_group,
1075 NULL
1076};
1077
Tejun Heo540eed52008-08-25 19:56:15 +09001078/**
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001079 * disk_release - releases all allocated resources of the gendisk
1080 * @dev: the device representing this disk
1081 *
1082 * This function releases all allocated resources of the gendisk.
1083 *
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001084 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1085 * assigned. Since the request_queue sits on top of the gendisk for these
1086 * drivers we also call blk_put_queue() for them, and we expect the
1087 * request_queue refcount to reach 0 at this point, and so the request_queue
1088 * will also be freed prior to the disk.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001089 *
1090 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001091 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001092static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001094 struct gendisk *disk = dev_to_disk(dev);
1095
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001096 might_sleep();
1097
Christoph Hellwig7c3f8282021-05-21 07:50:51 +02001098 if (MAJOR(dev->devt) == BLOCK_EXT_MAJOR)
1099 blk_free_ext_minor(MINOR(dev->devt));
Tejun Heo77ea8872010-12-08 20:57:37 +01001100 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 kfree(disk->random);
Christoph Hellwiga33df752021-01-24 11:02:41 +01001102 xa_destroy(&disk->part_tbl);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001103 bdput(disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001104 if (disk->queue)
1105 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 kfree(disk);
1107}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001108struct class block_class = {
1109 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110};
1111
Kay Sievers3c2670e2013-04-06 09:56:00 -07001112static char *block_devnode(struct device *dev, umode_t *mode,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001113 kuid_t *uid, kgid_t *gid)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001114{
1115 struct gendisk *disk = dev_to_disk(dev);
1116
Christoph Hellwig348e1142020-03-27 09:07:17 +01001117 if (disk->fops->devnode)
1118 return disk->fops->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001119 return NULL;
1120}
1121
Boris Burkovef45fe42020-06-01 13:12:05 -07001122const struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001123 .name = "disk",
1124 .groups = disk_attr_groups,
1125 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001126 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127};
1128
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001129#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001130/*
1131 * aggregate disk stat collector. Uses the same stats that the sysfs
1132 * entries do, above, but makes them available through one seq_file.
1133 *
1134 * The output looks suspiciously like /proc/partitions with a bunch of
1135 * extra fields.
1136 */
1137static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 struct gendisk *gp = v;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001140 struct block_device *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 char buf[BDEVNAME_SIZE];
Mikulas Patockae016b782018-12-06 11:41:21 -05001142 unsigned int inflight;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001143 struct disk_stats stat;
Christoph Hellwig7fae67c2021-04-06 08:23:01 +02001144 unsigned long idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001147 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001148 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 " rio rmerge rsect ruse wio wmerge "
1150 "wsect wuse running use aveq"
1151 "\n\n");
1152 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001153
Christoph Hellwig7fae67c2021-04-06 08:23:01 +02001154 rcu_read_lock();
1155 xa_for_each(&gp->part_tbl, idx, hd) {
1156 if (bdev_is_partition(hd) && !bdev_nr_sectors(hd))
1157 continue;
Christoph Hellwig0d021292020-11-27 16:43:51 +01001158 part_stat_read_all(hd, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001159 if (queue_is_mq(gp->queue))
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001160 inflight = blk_mq_in_flight(gp->queue, hd);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001161 else
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001162 inflight = part_in_flight(hd);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001163
Michael Callahanbdca3c82018-07-18 04:47:40 -07001164 seq_printf(seqf, "%4d %7d %s "
1165 "%lu %lu %lu %u "
1166 "%lu %lu %lu %u "
1167 "%u %u %u "
Konstantin Khlebnikovb6866312019-11-21 13:40:26 +03001168 "%lu %lu %lu %u "
1169 "%lu %u"
1170 "\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001171 MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
1172 disk_name(gp, hd->bd_partno, buf),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001173 stat.ios[STAT_READ],
1174 stat.merges[STAT_READ],
1175 stat.sectors[STAT_READ],
1176 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1177 NSEC_PER_MSEC),
1178 stat.ios[STAT_WRITE],
1179 stat.merges[STAT_WRITE],
1180 stat.sectors[STAT_WRITE],
1181 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1182 NSEC_PER_MSEC),
Mikulas Patockae016b782018-12-06 11:41:21 -05001183 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001184 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001185 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1186 stat.nsecs[STAT_WRITE] +
1187 stat.nsecs[STAT_DISCARD] +
1188 stat.nsecs[STAT_FLUSH],
1189 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001190 stat.ios[STAT_DISCARD],
1191 stat.merges[STAT_DISCARD],
1192 stat.sectors[STAT_DISCARD],
1193 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1194 NSEC_PER_MSEC),
1195 stat.ios[STAT_FLUSH],
1196 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1197 NSEC_PER_MSEC)
Jerome Marchand28f39d52008-02-08 11:04:56 +01001198 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
Christoph Hellwig7fae67c2021-04-06 08:23:01 +02001200 rcu_read_unlock();
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203}
1204
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001205static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001206 .start = disk_seqf_start,
1207 .next = disk_seqf_next,
1208 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 .show = diskstats_show
1210};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001211
1212static int __init proc_genhd_init(void)
1213{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001214 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1215 proc_create_seq("partitions", 0, NULL, &partitions_op);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001216 return 0;
1217}
1218module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001219#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Tejun Heocf771cb2008-09-03 09:01:09 +02001221dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001222{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001223 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001224 struct class_dev_iter iter;
1225 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001226
Tejun Heodef4e382008-09-03 08:57:12 +02001227 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1228 while ((dev = class_dev_iter_next(&iter))) {
1229 struct gendisk *disk = dev_to_disk(dev);
Christoph Hellwig0d021292020-11-27 16:43:51 +01001230 struct block_device *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001231
Kay Sievers3ada8b72009-01-06 10:44:43 -08001232 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001233 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001234
Neil Brown41b8c852009-02-18 10:33:59 +01001235 if (partno < disk->minors) {
1236 /* We need to return the right devno, even
1237 * if the partition doesn't exist yet.
1238 */
1239 devt = MKDEV(MAJOR(dev->devt),
1240 MINOR(dev->devt) + partno);
1241 break;
1242 }
Christoph Hellwig0d021292020-11-27 16:43:51 +01001243 part = bdget_disk(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001244 if (part) {
Christoph Hellwig0d021292020-11-27 16:43:51 +01001245 devt = part->bd_dev;
1246 bdput(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001247 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001248 }
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001249 }
Tejun Heodef4e382008-09-03 08:57:12 +02001250 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001251 return devt;
1252}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001253
Byungchul Parke319e1f2017-10-25 17:56:05 +09001254struct gendisk *__alloc_disk_node(int minors, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001255{
1256 struct gendisk *disk;
1257
Christoph Hellwigde65b012017-08-23 19:10:29 +02001258 if (minors > DISK_MAX_PARTS) {
1259 printk(KERN_ERR
Randy Dunlap7fb52622017-11-18 17:43:38 -08001260 "block: can't allocate more than %d partitions\n",
Christoph Hellwigde65b012017-08-23 19:10:29 +02001261 DISK_MAX_PARTS);
1262 minors = DISK_MAX_PARTS;
1263 }
Christoph Lameter19460892005-06-23 00:08:19 -07001264
Joe Perchesc1b511e2013-08-29 15:21:42 -07001265 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001266 if (!disk)
1267 return NULL;
Jens Axboe6c23a962011-01-07 08:43:37 +01001268
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001269 disk->part0 = bdev_alloc(disk, 0);
1270 if (!disk->part0)
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001271 goto out_free_disk;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001272
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001273 disk->node_id = node_id;
Christoph Hellwiga33df752021-01-24 11:02:41 +01001274 xa_init(&disk->part_tbl);
1275 if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL))
1276 goto out_destroy_part_tbl;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001277
1278 disk->minors = minors;
1279 rand_initialize_disk(disk);
1280 disk_to_dev(disk)->class = &block_class;
1281 disk_to_dev(disk)->type = &disk_type;
1282 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return disk;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001284
Christoph Hellwiga33df752021-01-24 11:02:41 +01001285out_destroy_part_tbl:
1286 xa_destroy(&disk->part_tbl);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001287 bdput(disk->part0);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001288out_free_disk:
1289 kfree(disk);
1290 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
Byungchul Parke319e1f2017-10-25 17:56:05 +09001292EXPORT_SYMBOL(__alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001294/**
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001295 * put_disk - decrements the gendisk refcount
Randy Dunlap0d20dcc2020-07-30 18:42:30 -07001296 * @disk: the struct gendisk to decrement the refcount for
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001297 *
1298 * This decrements the refcount for the struct gendisk. When this reaches 0
1299 * we'll have disk_release() called.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001300 *
1301 * Context: Any context, but the last reference must not be dropped from
1302 * atomic context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304void put_disk(struct gendisk *disk)
1305{
1306 if (disk)
Christoph Hellwigefdc41c2020-11-10 07:25:37 +01001307 put_device(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309EXPORT_SYMBOL(put_disk);
1310
Hannes Reineckee3264a42009-07-28 09:13:13 +02001311static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1312{
1313 char event[] = "DISK_RO=1";
1314 char *envp[] = { event, NULL };
1315
1316 if (!ro)
1317 event[8] = '0';
1318 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1319}
1320
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001321/**
1322 * set_disk_ro - set a gendisk read-only
1323 * @disk: gendisk to operate on
Lukas Bulwahn7f31bee2021-01-29 05:55:05 +01001324 * @read_only: %true to set the disk read-only, %false set the disk read/write
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001325 *
1326 * This function is used to indicate whether a given disk device should have its
1327 * read-only flag set. set_disk_ro() is typically used by device drivers to
1328 * indicate whether the underlying physical device is write-protected.
1329 */
1330void set_disk_ro(struct gendisk *disk, bool read_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001332 if (read_only) {
1333 if (test_and_set_bit(GD_READ_ONLY, &disk->state))
1334 return;
1335 } else {
1336 if (!test_and_clear_bit(GD_READ_ONLY, &disk->state))
1337 return;
Hannes Reineckee3264a42009-07-28 09:13:13 +02001338 }
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001339 set_disk_ro_uevent(disk, read_only);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341EXPORT_SYMBOL(set_disk_ro);
1342
1343int bdev_read_only(struct block_device *bdev)
1344{
Christoph Hellwig947139b2021-01-09 11:42:52 +01001345 return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347EXPORT_SYMBOL(bdev_read_only);
1348
Tejun Heo77ea8872010-12-08 20:57:37 +01001349/*
1350 * Disk events - monitor disk events like media change and eject request.
1351 */
1352struct disk_events {
1353 struct list_head node; /* all disk_event's */
1354 struct gendisk *disk; /* the associated disk */
1355 spinlock_t lock;
1356
Tejun Heofdd514e2011-06-09 20:43:59 +02001357 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001358 int block; /* event blocking depth */
1359 unsigned int pending; /* events already sent out */
1360 unsigned int clearing; /* events being cleared */
1361
1362 long poll_msecs; /* interval, -1 for default */
1363 struct delayed_work dwork;
1364};
1365
1366static const char *disk_events_strs[] = {
1367 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1368 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1369};
1370
1371static char *disk_uevents[] = {
1372 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1373 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1374};
1375
1376/* list of all disk_events */
1377static DEFINE_MUTEX(disk_events_mutex);
1378static LIST_HEAD(disk_events);
1379
1380/* disable in-kernel polling by default */
Wei Tang1fe8f342015-11-24 09:58:46 +08001381static unsigned long disk_events_dfl_poll_msecs;
Tejun Heo77ea8872010-12-08 20:57:37 +01001382
1383static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1384{
1385 struct disk_events *ev = disk->ev;
1386 long intv_msecs = 0;
1387
1388 /*
1389 * If device-specific poll interval is set, always use it. If
Martin Wilck673387a2019-03-27 14:51:01 +01001390 * the default is being used, poll if the POLL flag is set.
Tejun Heo77ea8872010-12-08 20:57:37 +01001391 */
1392 if (ev->poll_msecs >= 0)
1393 intv_msecs = ev->poll_msecs;
Martin Wilckc92e2f02019-03-27 14:51:02 +01001394 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
Tejun Heo77ea8872010-12-08 20:57:37 +01001395 intv_msecs = disk_events_dfl_poll_msecs;
1396
1397 return msecs_to_jiffies(intv_msecs);
1398}
1399
Tejun Heoc3af54a2011-06-09 20:43:55 +02001400/**
1401 * disk_block_events - block and flush disk event checking
1402 * @disk: disk to block events for
1403 *
1404 * On return from this function, it is guaranteed that event checking
1405 * isn't in progress and won't happen until unblocked by
1406 * disk_unblock_events(). Events blocking is counted and the actual
1407 * unblocking happens after the matching number of unblocks are done.
1408 *
1409 * Note that this intentionally does not block event checking from
1410 * disk_clear_events().
1411 *
1412 * CONTEXT:
1413 * Might sleep.
1414 */
1415void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001416{
1417 struct disk_events *ev = disk->ev;
1418 unsigned long flags;
1419 bool cancel;
1420
Tejun Heoc3af54a2011-06-09 20:43:55 +02001421 if (!ev)
1422 return;
1423
Tejun Heofdd514e2011-06-09 20:43:59 +02001424 /*
1425 * Outer mutex ensures that the first blocker completes canceling
1426 * the event work before further blockers are allowed to finish.
1427 */
1428 mutex_lock(&ev->block_mutex);
1429
Tejun Heo77ea8872010-12-08 20:57:37 +01001430 spin_lock_irqsave(&ev->lock, flags);
1431 cancel = !ev->block++;
1432 spin_unlock_irqrestore(&ev->lock, flags);
1433
Tejun Heoc3af54a2011-06-09 20:43:55 +02001434 if (cancel)
1435 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001436
1437 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001438}
1439
1440static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1441{
1442 struct disk_events *ev = disk->ev;
1443 unsigned long intv;
1444 unsigned long flags;
1445
1446 spin_lock_irqsave(&ev->lock, flags);
1447
1448 if (WARN_ON_ONCE(ev->block <= 0))
1449 goto out_unlock;
1450
1451 if (--ev->block)
1452 goto out_unlock;
1453
Tejun Heo77ea8872010-12-08 20:57:37 +01001454 intv = disk_events_poll_jiffies(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001455 if (check_now)
Viresh Kumar695588f2013-04-24 17:12:56 +05301456 queue_delayed_work(system_freezable_power_efficient_wq,
1457 &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001458 else if (intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301459 queue_delayed_work(system_freezable_power_efficient_wq,
1460 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001461out_unlock:
1462 spin_unlock_irqrestore(&ev->lock, flags);
1463}
1464
1465/**
Tejun Heo77ea8872010-12-08 20:57:37 +01001466 * disk_unblock_events - unblock disk event checking
1467 * @disk: disk to unblock events for
1468 *
1469 * Undo disk_block_events(). When the block count reaches zero, it
1470 * starts events polling if configured.
1471 *
1472 * CONTEXT:
1473 * Don't care. Safe to call from irq context.
1474 */
1475void disk_unblock_events(struct gendisk *disk)
1476{
1477 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01001478 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001479}
1480
1481/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02001482 * disk_flush_events - schedule immediate event checking and flushing
1483 * @disk: disk to check and flush events for
1484 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01001485 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02001486 * Schedule immediate event checking on @disk if not blocked. Events in
1487 * @mask are scheduled to be cleared from the driver. Note that this
1488 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01001489 *
1490 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02001491 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01001492 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001493void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001494{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001495 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001496
1497 if (!ev)
1498 return;
1499
Tejun Heo85ef06d2011-07-01 16:17:47 +02001500 spin_lock_irq(&ev->lock);
1501 ev->clearing |= mask;
Tejun Heo41f63c52012-08-03 10:30:47 -07001502 if (!ev->block)
Viresh Kumar695588f2013-04-24 17:12:56 +05301503 mod_delayed_work(system_freezable_power_efficient_wq,
1504 &ev->dwork, 0);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001505 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001506}
Tejun Heo77ea8872010-12-08 20:57:37 +01001507
1508/**
1509 * disk_clear_events - synchronously check, clear and return pending events
1510 * @disk: disk to fetch and clear events from
Masanari Iidada3dae52014-09-09 01:27:23 +09001511 * @mask: mask of events to be fetched and cleared
Tejun Heo77ea8872010-12-08 20:57:37 +01001512 *
1513 * Disk events are synchronously checked and pending events in @mask
1514 * are cleared and returned. This ignores the block count.
1515 *
1516 * CONTEXT:
1517 * Might sleep.
1518 */
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001519static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001520{
Tejun Heo77ea8872010-12-08 20:57:37 +01001521 struct disk_events *ev = disk->ev;
1522 unsigned int pending;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001523 unsigned int clearing = mask;
Tejun Heo77ea8872010-12-08 20:57:37 +01001524
Christoph Hellwiga564e232020-07-08 14:25:41 +02001525 if (!ev)
Tejun Heo77ea8872010-12-08 20:57:37 +01001526 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001527
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001528 disk_block_events(disk);
1529
1530 /*
1531 * store the union of mask and ev->clearing on the stack so that the
1532 * race with disk_flush_events does not cause ambiguity (ev->clearing
1533 * can still be modified even if events are blocked).
1534 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001535 spin_lock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001536 clearing |= ev->clearing;
1537 ev->clearing = 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001538 spin_unlock_irq(&ev->lock);
1539
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001540 disk_check_events(ev, &clearing);
Derek Basehoreaea24a82012-12-18 12:27:18 -08001541 /*
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001542 * if ev->clearing is not 0, the disk_flush_events got called in the
1543 * middle of this function, so we want to run the workfn without delay.
Derek Basehoreaea24a82012-12-18 12:27:18 -08001544 */
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001545 __disk_unblock_events(disk, ev->clearing ? true : false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001546
1547 /* then, fetch and clear pending events */
1548 spin_lock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001549 pending = ev->pending & mask;
1550 ev->pending &= ~mask;
1551 spin_unlock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001552 WARN_ON_ONCE(clearing & mask);
Tejun Heo77ea8872010-12-08 20:57:37 +01001553
1554 return pending;
1555}
1556
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001557/**
1558 * bdev_check_media_change - check if a removable media has been changed
1559 * @bdev: block device to check
1560 *
1561 * Check whether a removable media has been changed, and attempt to free all
1562 * dentries and inodes and invalidates all block device page cache entries in
1563 * that case.
1564 *
1565 * Returns %true if the block device changed, or %false if not.
1566 */
1567bool bdev_check_media_change(struct block_device *bdev)
1568{
1569 unsigned int events;
1570
1571 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1572 DISK_EVENT_EJECT_REQUEST);
1573 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1574 return false;
1575
1576 if (__invalidate_device(bdev, true))
1577 pr_warn("VFS: busy inodes on changed media %s\n",
1578 bdev->bd_disk->disk_name);
Christoph Hellwig38430f02020-09-21 09:19:45 +02001579 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001580 return true;
1581}
1582EXPORT_SYMBOL(bdev_check_media_change);
1583
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001584/*
1585 * Separate this part out so that a different pointer for clearing_ptr can be
1586 * passed in for disk_clear_events.
1587 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001588static void disk_events_workfn(struct work_struct *work)
1589{
1590 struct delayed_work *dwork = to_delayed_work(work);
1591 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001592
1593 disk_check_events(ev, &ev->clearing);
1594}
1595
1596static void disk_check_events(struct disk_events *ev,
1597 unsigned int *clearing_ptr)
1598{
Tejun Heo77ea8872010-12-08 20:57:37 +01001599 struct gendisk *disk = ev->disk;
1600 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001601 unsigned int clearing = *clearing_ptr;
Tejun Heo77ea8872010-12-08 20:57:37 +01001602 unsigned int events;
1603 unsigned long intv;
1604 int nr_events = 0, i;
1605
1606 /* check events */
1607 events = disk->fops->check_events(disk, clearing);
1608
1609 /* accumulate pending events and schedule next poll if necessary */
1610 spin_lock_irq(&ev->lock);
1611
1612 events &= ~ev->pending;
1613 ev->pending |= events;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001614 *clearing_ptr &= ~clearing;
Tejun Heo77ea8872010-12-08 20:57:37 +01001615
1616 intv = disk_events_poll_jiffies(disk);
1617 if (!ev->block && intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301618 queue_delayed_work(system_freezable_power_efficient_wq,
1619 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001620
1621 spin_unlock_irq(&ev->lock);
1622
Tejun Heo7c88a162011-04-21 19:43:58 +02001623 /*
1624 * Tell userland about new events. Only the events listed in
Martin Wilckc92e2f02019-03-27 14:51:02 +01001625 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1626 * is set. Otherwise, events are processed internally but never
1627 * get reported to userland.
Tejun Heo7c88a162011-04-21 19:43:58 +02001628 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001629 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Martin Wilckc92e2f02019-03-27 14:51:02 +01001630 if ((events & disk->events & (1 << i)) &&
1631 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
Tejun Heo77ea8872010-12-08 20:57:37 +01001632 envp[nr_events++] = disk_uevents[i];
1633
1634 if (nr_events)
1635 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1636}
1637
1638/*
1639 * A disk events enabled device has the following sysfs nodes under
1640 * its /sys/block/X/ directory.
1641 *
1642 * events : list of all supported events
1643 * events_async : list of events which can be detected w/o polling
Martin Wilck673387a2019-03-27 14:51:01 +01001644 * (always empty, only for backwards compatibility)
Tejun Heo77ea8872010-12-08 20:57:37 +01001645 * events_poll_msecs : polling interval, 0: disable, -1: system default
1646 */
1647static ssize_t __disk_events_show(unsigned int events, char *buf)
1648{
1649 const char *delim = "";
1650 ssize_t pos = 0;
1651 int i;
1652
1653 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1654 if (events & (1 << i)) {
1655 pos += sprintf(buf + pos, "%s%s",
1656 delim, disk_events_strs[i]);
1657 delim = " ";
1658 }
1659 if (pos)
1660 pos += sprintf(buf + pos, "\n");
1661 return pos;
1662}
1663
1664static ssize_t disk_events_show(struct device *dev,
1665 struct device_attribute *attr, char *buf)
1666{
1667 struct gendisk *disk = dev_to_disk(dev);
1668
Martin Wilckc92e2f02019-03-27 14:51:02 +01001669 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1670 return 0;
1671
Tejun Heo77ea8872010-12-08 20:57:37 +01001672 return __disk_events_show(disk->events, buf);
1673}
1674
1675static ssize_t disk_events_async_show(struct device *dev,
1676 struct device_attribute *attr, char *buf)
1677{
Martin Wilck673387a2019-03-27 14:51:01 +01001678 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001679}
1680
1681static ssize_t disk_events_poll_msecs_show(struct device *dev,
1682 struct device_attribute *attr,
1683 char *buf)
1684{
1685 struct gendisk *disk = dev_to_disk(dev);
1686
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001687 if (!disk->ev)
1688 return sprintf(buf, "-1\n");
1689
Tejun Heo77ea8872010-12-08 20:57:37 +01001690 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1691}
1692
1693static ssize_t disk_events_poll_msecs_store(struct device *dev,
1694 struct device_attribute *attr,
1695 const char *buf, size_t count)
1696{
1697 struct gendisk *disk = dev_to_disk(dev);
1698 long intv;
1699
1700 if (!count || !sscanf(buf, "%ld", &intv))
1701 return -EINVAL;
1702
1703 if (intv < 0 && intv != -1)
1704 return -EINVAL;
1705
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001706 if (!disk->ev)
1707 return -ENODEV;
1708
Tejun Heoc3af54a2011-06-09 20:43:55 +02001709 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001710 disk->ev->poll_msecs = intv;
1711 __disk_unblock_events(disk, true);
1712
1713 return count;
1714}
1715
Joe Perches5657a812018-05-24 13:38:59 -06001716static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
1717static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
1718static const DEVICE_ATTR(events_poll_msecs, 0644,
Tejun Heo77ea8872010-12-08 20:57:37 +01001719 disk_events_poll_msecs_show,
1720 disk_events_poll_msecs_store);
1721
1722static const struct attribute *disk_events_attrs[] = {
1723 &dev_attr_events.attr,
1724 &dev_attr_events_async.attr,
1725 &dev_attr_events_poll_msecs.attr,
1726 NULL,
1727};
1728
1729/*
1730 * The default polling interval can be specified by the kernel
1731 * parameter block.events_dfl_poll_msecs which defaults to 0
1732 * (disable). This can also be modified runtime by writing to
Akinobu Mita1624b0b2019-07-16 21:59:35 +09001733 * /sys/module/block/parameters/events_dfl_poll_msecs.
Tejun Heo77ea8872010-12-08 20:57:37 +01001734 */
1735static int disk_events_set_dfl_poll_msecs(const char *val,
1736 const struct kernel_param *kp)
1737{
1738 struct disk_events *ev;
1739 int ret;
1740
1741 ret = param_set_ulong(val, kp);
1742 if (ret < 0)
1743 return ret;
1744
1745 mutex_lock(&disk_events_mutex);
1746
1747 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02001748 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001749
1750 mutex_unlock(&disk_events_mutex);
1751
1752 return 0;
1753}
1754
1755static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1756 .set = disk_events_set_dfl_poll_msecs,
1757 .get = param_get_ulong,
1758};
1759
1760#undef MODULE_PARAM_PREFIX
1761#define MODULE_PARAM_PREFIX "block."
1762
1763module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1764 &disk_events_dfl_poll_msecs, 0644);
1765
1766/*
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001767 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
Tejun Heo77ea8872010-12-08 20:57:37 +01001768 */
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001769static void disk_alloc_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001770{
1771 struct disk_events *ev;
1772
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001773 if (!disk->fops->check_events || !disk->events)
Tejun Heo77ea8872010-12-08 20:57:37 +01001774 return;
1775
1776 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1777 if (!ev) {
1778 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1779 return;
1780 }
1781
Tejun Heo77ea8872010-12-08 20:57:37 +01001782 INIT_LIST_HEAD(&ev->node);
1783 ev->disk = disk;
1784 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02001785 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001786 ev->block = 1;
1787 ev->poll_msecs = -1;
1788 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
1789
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001790 disk->ev = ev;
1791}
1792
1793static void disk_add_events(struct gendisk *disk)
1794{
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001795 /* FIXME: error handling */
1796 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
1797 pr_warn("%s: failed to create sysfs files for events\n",
1798 disk->disk_name);
1799
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001800 if (!disk->ev)
1801 return;
1802
Tejun Heo77ea8872010-12-08 20:57:37 +01001803 mutex_lock(&disk_events_mutex);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001804 list_add_tail(&disk->ev->node, &disk_events);
Tejun Heo77ea8872010-12-08 20:57:37 +01001805 mutex_unlock(&disk_events_mutex);
1806
1807 /*
1808 * Block count is initialized to 1 and the following initial
1809 * unblock kicks it into action.
1810 */
1811 __disk_unblock_events(disk, true);
1812}
1813
1814static void disk_del_events(struct gendisk *disk)
1815{
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001816 if (disk->ev) {
1817 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001818
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001819 mutex_lock(&disk_events_mutex);
1820 list_del_init(&disk->ev->node);
1821 mutex_unlock(&disk_events_mutex);
1822 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001823
1824 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
1825}
1826
1827static void disk_release_events(struct gendisk *disk)
1828{
1829 /* the block count should be 1 from disk_del_events() */
1830 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
1831 kfree(disk->ev);
1832}