blob: 1832add5c73849f65208393221fc185aa50e9ada [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
49 spin_lock(&bdev->bd_size_lock);
50 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
51 spin_unlock(&bdev->bd_size_lock);
52}
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",
76 disk->disk_name, size, capacity);
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
Christoph Hellwig309dca302021-01-24 11:02:34 +0100164static struct block_device *__disk_get_part(struct gendisk *disk, int partno)
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200165{
166 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
167
168 if (unlikely(partno < 0 || partno >= ptbl->len))
169 return NULL;
170 return rcu_dereference(ptbl->part[partno]);
171}
172
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200173/**
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200174 * disk_part_iter_init - initialize partition iterator
175 * @piter: iterator to initialize
176 * @disk: disk to iterate over
177 * @flags: DISK_PITER_* flags
178 *
179 * Initialize @piter so that it iterates over partitions of @disk.
180 *
181 * CONTEXT:
182 * Don't care.
183 */
184void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
185 unsigned int flags)
186{
187 piter->disk = disk;
188 piter->part = NULL;
Christoph Hellwig0470dd92021-01-24 11:02:40 +0100189 if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200190 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200191 else
192 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200193 piter->flags = flags;
194}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200195
196/**
197 * disk_part_iter_next - proceed iterator to the next partition and return it
198 * @piter: iterator of interest
199 *
200 * Proceed @piter to the next partition and return it.
201 *
202 * CONTEXT:
203 * Don't care.
204 */
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100205struct block_device *disk_part_iter_next(struct disk_part_iter *piter)
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200206{
Tejun Heo540eed52008-08-25 19:56:15 +0900207 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200208
209 /* put the last partition */
Christoph Hellwige79319a2020-11-10 06:48:53 +0100210 disk_part_iter_exit(piter);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200211
Tejun Heo540eed52008-08-25 19:56:15 +0900212 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200213 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900214 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200215
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200216 /* iterate to the next partition */
Christoph Hellwig0470dd92021-01-24 11:02:40 +0100217 for (; piter->idx != ptbl->len; piter->idx += 1) {
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100218 struct block_device *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200219
Tejun Heo540eed52008-08-25 19:56:15 +0900220 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200221 if (!part)
222 continue;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100223 piter->part = bdgrab(part);
224 if (!piter->part)
225 continue;
Ming Leiaebf5db2020-12-21 12:33:35 +0800226 if (!bdev_nr_sectors(part) &&
227 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
228 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
229 piter->idx == 0)) {
230 bdput(piter->part);
231 piter->part = NULL;
232 continue;
233 }
234
Christoph Hellwig0470dd92021-01-24 11:02:40 +0100235 piter->part = bdgrab(part);
236 if (!piter->part)
237 continue;
238 piter->idx += 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200239 break;
240 }
241
242 rcu_read_unlock();
243
244 return piter->part;
245}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200246
247/**
248 * disk_part_iter_exit - finish up partition iteration
249 * @piter: iter of interest
250 *
251 * Called when iteration is over. Cleans up @piter.
252 *
253 * CONTEXT:
254 * Don't care.
255 */
256void disk_part_iter_exit(struct disk_part_iter *piter)
257{
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100258 if (piter->part)
259 bdput(piter->part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200260 piter->part = NULL;
261}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200262
Shin'ichiro Kawasakib53df2e2020-02-21 10:37:08 +0900263/**
264 * disk_has_partitions
265 * @disk: gendisk of interest
266 *
267 * Walk through the partition table and check if valid partition exists.
268 *
269 * CONTEXT:
270 * Don't care.
271 *
272 * RETURNS:
273 * True if the gendisk has at least one valid non-zero size partition.
274 * Otherwise false.
275 */
276bool disk_has_partitions(struct gendisk *disk)
277{
278 struct disk_part_tbl *ptbl;
279 int i;
280 bool ret = false;
281
282 rcu_read_lock();
283 ptbl = rcu_dereference(disk->part_tbl);
284
285 /* Iterate partitions skipping the whole device at index 0 */
286 for (i = 1; i < ptbl->len; i++) {
287 if (rcu_dereference(ptbl->part[i])) {
288 ret = true;
289 break;
290 }
291 }
292
293 rcu_read_unlock();
294
295 return ret;
296}
297EXPORT_SYMBOL_GPL(disk_has_partitions);
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
300 * Can be deleted altogether. Later.
301 *
302 */
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600303#define BLKDEV_MAJOR_HASH_SIZE 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static struct blk_major_name {
305 struct blk_major_name *next;
306 int major;
307 char name[16];
Christoph Hellwiga160c612020-10-29 15:58:28 +0100308 void (*probe)(dev_t devt);
Joe Korty68eef3b2006-03-31 02:30:32 -0800309} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100310static DEFINE_MUTEX(major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100313static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Joe Korty68eef3b2006-03-31 02:30:32 -0800315 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Joe Korty68eef3b2006-03-31 02:30:32 -0800318#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200319void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800320{
Joe Korty68eef3b2006-03-31 02:30:32 -0800321 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800322
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100323 mutex_lock(&major_names_lock);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600324 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
325 if (dp->major == offset)
Tejun Heocf771cb2008-09-03 09:01:09 +0200326 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100327 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
Joe Korty68eef3b2006-03-31 02:30:32 -0800329#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100331/**
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100332 * __register_blkdev - register a new block device
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100333 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800334 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
335 * @major = 0, try to allocate any unused major number.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100336 * @name: the name of the new block device as a zero terminated string
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100337 * @probe: allback that is called on access to any minor number of @major
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100338 *
339 * The @name must be unique within the system.
340 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300341 * The return value depends on the @major input parameter:
342 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800343 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
344 * then the function returns zero on success, or a negative error code
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300345 * - if any unused major number was requested with @major = 0 parameter
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100346 * then the return value is the allocated major number in range
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800347 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
348 *
349 * See Documentation/admin-guide/devices.txt for the list of allocated
350 * major numbers.
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100351 *
352 * Use register_blkdev instead for any new code.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100353 */
Christoph Hellwiga160c612020-10-29 15:58:28 +0100354int __register_blkdev(unsigned int major, const char *name,
355 void (*probe)(dev_t devt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct blk_major_name **n, *p;
358 int index, ret = 0;
359
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100360 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* temporary */
363 if (major == 0) {
364 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
365 if (major_names[index] == NULL)
366 break;
367 }
368
369 if (index == 0) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500370 printk("%s: failed to get major for %s\n",
371 __func__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ret = -EBUSY;
373 goto out;
374 }
375 major = index;
376 ret = major;
377 }
378
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600379 if (major >= BLKDEV_MAJOR_MAX) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500380 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
381 __func__, major, BLKDEV_MAJOR_MAX-1, name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600382
383 ret = -EINVAL;
384 goto out;
385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
388 if (p == NULL) {
389 ret = -ENOMEM;
390 goto out;
391 }
392
393 p->major = major;
Christoph Hellwiga160c612020-10-29 15:58:28 +0100394 p->probe = probe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 strlcpy(p->name, name, sizeof(p->name));
396 p->next = NULL;
397 index = major_to_index(major);
398
399 for (n = &major_names[index]; *n; n = &(*n)->next) {
400 if ((*n)->major == major)
401 break;
402 }
403 if (!*n)
404 *n = p;
405 else
406 ret = -EBUSY;
407
408 if (ret < 0) {
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800409 printk("register_blkdev: cannot get major %u for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 major, name);
411 kfree(p);
412 }
413out:
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100414 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return ret;
416}
Christoph Hellwiga160c612020-10-29 15:58:28 +0100417EXPORT_SYMBOL(__register_blkdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Akinobu Mitaf4480242007-07-17 04:03:47 -0700419void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct blk_major_name **n;
422 struct blk_major_name *p = NULL;
423 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100425 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 for (n = &major_names[index]; *n; n = &(*n)->next)
427 if ((*n)->major == major)
428 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700429 if (!*n || strcmp((*n)->name, name)) {
430 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700431 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 p = *n;
433 *n = p->next;
434 }
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100435 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439EXPORT_SYMBOL(unregister_blkdev);
440
Tejun Heobcce3de2008-08-25 19:47:22 +0900441/**
Tejun Heo870d6652008-08-25 19:47:25 +0900442 * blk_mangle_minor - scatter minor numbers apart
443 * @minor: minor number to mangle
444 *
445 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
446 * is enabled. Mangling twice gives the original value.
447 *
448 * RETURNS:
449 * Mangled value.
450 *
451 * CONTEXT:
452 * Don't care.
453 */
454static int blk_mangle_minor(int minor)
455{
456#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
457 int i;
458
459 for (i = 0; i < MINORBITS / 2; i++) {
460 int low = minor & (1 << i);
461 int high = minor & (1 << (MINORBITS - 1 - i));
462 int distance = MINORBITS - 1 - 2 * i;
463
464 minor ^= low | high; /* clear both bits */
465 low <<= distance; /* swap the positions */
466 high >>= distance;
467 minor |= low | high; /* and set */
468 }
469#endif
470 return minor;
471}
472
473/**
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100474 * blk_alloc_devt - allocate a dev_t for a block device
475 * @bdev: block device to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900476 * @devt: out parameter for resulting dev_t
477 *
478 * Allocate a dev_t for block device.
479 *
480 * RETURNS:
481 * 0 on success, allocated dev_t is returned in *@devt. -errno on
482 * failure.
483 *
484 * CONTEXT:
485 * Might sleep.
486 */
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100487int blk_alloc_devt(struct block_device *bdev, dev_t *devt)
Tejun Heobcce3de2008-08-25 19:47:22 +0900488{
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100489 struct gendisk *disk = bdev->bd_disk;
Tejun Heobab998d2013-02-27 17:03:57 -0800490 int idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900491
492 /* in consecutive minor range? */
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100493 if (bdev->bd_partno < disk->minors) {
494 *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
Tejun Heobcce3de2008-08-25 19:47:22 +0900495 return 0;
496 }
497
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100498 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
Tejun Heobab998d2013-02-27 17:03:57 -0800499 if (idx < 0)
500 return idx == -ENOSPC ? -EBUSY : idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900501
Tejun Heo870d6652008-08-25 19:47:25 +0900502 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900503 return 0;
504}
505
506/**
507 * blk_free_devt - free a dev_t
508 * @devt: dev_t to free
509 *
510 * Free @devt which was allocated using blk_alloc_devt().
511 *
512 * CONTEXT:
513 * Might sleep.
514 */
515void blk_free_devt(dev_t devt)
516{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100517 if (MAJOR(devt) == BLOCK_EXT_MAJOR)
518 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800519}
520
Tejun Heo1f014292008-08-25 19:47:23 +0900521static char *bdevt_str(dev_t devt, char *buf)
522{
523 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
524 char tbuf[BDEVT_SIZE];
525 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
526 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
527 } else
528 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
529
530 return buf;
531}
532
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100533void disk_uevent(struct gendisk *disk, enum kobject_action action)
534{
535 struct disk_part_iter piter;
536 struct block_device *part;
537
538 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
539 while ((part = disk_part_iter_next(&piter)))
540 kobject_uevent(bdev_kobj(part), action);
541 disk_part_iter_exit(&piter);
542}
543EXPORT_SYMBOL_GPL(disk_uevent);
544
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200545static void disk_scan_partitions(struct gendisk *disk)
546{
547 struct block_device *bdev;
548
549 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
550 return;
551
552 set_bit(GD_NEED_PART_SCAN, &disk->state);
553 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
554 if (!IS_ERR(bdev))
555 blkdev_put(bdev, FMODE_READ);
556}
557
Hannes Reineckefef912b2018-09-28 08:17:19 +0200558static void register_disk(struct device *parent, struct gendisk *disk,
559 const struct attribute_group **groups)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100560{
561 struct device *ddev = disk_to_dev(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100562 int err;
563
Dan Williamse63a46b2016-06-15 18:17:27 -0700564 ddev->parent = parent;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100565
Kees Cookffc8b302013-07-03 15:01:14 -0700566 dev_set_name(ddev, "%s", disk->disk_name);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100567
568 /* delay uevents, until we scanned partition table */
569 dev_set_uevent_suppress(ddev, 1);
570
Hannes Reineckefef912b2018-09-28 08:17:19 +0200571 if (groups) {
572 WARN_ON(ddev->groups);
573 ddev->groups = groups;
574 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100575 if (device_add(ddev))
576 return;
577 if (!sysfs_deprecated) {
578 err = sysfs_create_link(block_depr, &ddev->kobj,
579 kobject_name(&ddev->kobj));
580 if (err) {
581 device_del(ddev);
582 return;
583 }
584 }
Ming Lei25e823c2013-02-22 16:34:13 -0800585
586 /*
587 * avoid probable deadlock caused by allocating memory with
588 * GFP_KERNEL in runtime_resume callback of its all ancestor
589 * devices
590 */
591 pm_runtime_set_memalloc_noio(ddev, true);
592
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100593 disk->part0->bd_holder_dir =
594 kobject_create_and_add("holders", &ddev->kobj);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100595 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
596
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300597 if (disk->flags & GENHD_FL_HIDDEN) {
598 dev_set_uevent_suppress(ddev, 0);
599 return;
600 }
601
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200602 disk_scan_partitions(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100603
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100604 /* announce the disk and partitions after all partitions are created */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100605 dev_set_uevent_suppress(ddev, 0);
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100606 disk_uevent(disk, KOBJ_ADD);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300607
zhengbin4d7c1d32019-02-20 21:27:05 +0800608 if (disk->queue->backing_dev_info->dev) {
609 err = sysfs_create_link(&ddev->kobj,
610 &disk->queue->backing_dev_info->dev->kobj,
611 "bdi");
612 WARN_ON(err);
613 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/**
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500617 * __device_add_disk - add disk information to kernel list
Dan Williamse63a46b2016-06-15 18:17:27 -0700618 * @parent: parent device for the disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * @disk: per-device partitioning information
Hannes Reineckefef912b2018-09-28 08:17:19 +0200620 * @groups: Additional per-device sysfs groups
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500621 * @register_queue: register the queue if set to true
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 *
623 * This function registers the partitioning information in @disk
624 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900625 *
626 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500628static void __device_add_disk(struct device *parent, struct gendisk *disk,
Hannes Reineckefef912b2018-09-28 08:17:19 +0200629 const struct attribute_group **groups,
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500630 bool register_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900632 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400633 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700634
Damien Le Moal737eb782019-09-05 18:51:33 +0900635 /*
636 * The disk queue should now be all set with enough information about
637 * the device for the elevator code to pick an adequate default
638 * elevator if one is needed, that is, for devices requesting queue
639 * registration.
640 */
641 if (register_queue)
642 elevator_init_mq(disk->queue);
643
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900644 /* minors == 0 indicates to use ext devt from part0 and should
645 * be accompanied with EXT_DEVT flag. Make sure all
646 * parameters make sense.
647 */
648 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300649 WARN_ON(!disk->minors &&
650 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900653
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100654 retval = blk_alloc_devt(disk->part0, &devt);
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900655 if (retval) {
656 WARN_ON(1);
657 return;
658 }
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900659 disk->major = MAJOR(devt);
660 disk->first_minor = MINOR(devt);
661
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +0100662 disk_alloc_events(disk);
663
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300664 if (disk->flags & GENHD_FL_HIDDEN) {
665 /*
666 * Don't let hidden disks show up in /proc/partitions,
667 * and don't bother scanning for partitions either.
668 */
669 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
670 disk->flags |= GENHD_FL_NO_PART_SCAN;
671 } else {
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200672 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
673 struct device *dev = disk_to_dev(disk);
weiping zhang3a921682017-10-31 18:38:59 +0800674 int ret;
675
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300676 /* Register BDI before referencing it from bdev */
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200677 dev->devt = devt;
678 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
weiping zhang3a921682017-10-31 18:38:59 +0800679 WARN_ON(ret);
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200680 bdi_set_owner(bdi, dev);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100681 bdev_add(disk->part0, devt);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300682 }
Hannes Reineckefef912b2018-09-28 08:17:19 +0200683 register_disk(parent, disk, groups);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500684 if (register_queue)
685 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700686
Tejun Heo523e1d32011-10-19 14:31:07 +0200687 /*
688 * Take an extra ref on queue which will be put on disk_release()
689 * so that it sticks around as long as @disk is there.
690 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100691 WARN_ON_ONCE(!blk_get_queue(disk->queue));
Tejun Heo523e1d32011-10-19 14:31:07 +0200692
Tejun Heo77ea8872010-12-08 20:57:37 +0100693 disk_add_events(disk);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400694 blk_integrity_add(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500696
Hannes Reineckefef912b2018-09-28 08:17:19 +0200697void device_add_disk(struct device *parent, struct gendisk *disk,
698 const struct attribute_group **groups)
699
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500700{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200701 __device_add_disk(parent, disk, groups, true);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500702}
Dan Williamse63a46b2016-06-15 18:17:27 -0700703EXPORT_SYMBOL(device_add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500705void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
706{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200707 __device_add_disk(parent, disk, NULL, false);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500708}
709EXPORT_SYMBOL(device_add_disk_no_queue_reg);
710
Christoph Hellwig71773cf2020-11-24 09:20:37 +0100711static void invalidate_partition(struct block_device *bdev)
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200712{
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200713 fsync_bdev(bdev);
714 __invalidate_device(bdev, true);
Christoph Hellwig9bc5c392020-04-14 09:29:02 +0200715
716 /*
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100717 * Unhash the bdev inode for this device so that it can't be looked
718 * up any more even if openers still hold references to it.
Christoph Hellwig9bc5c392020-04-14 09:29:02 +0200719 */
720 remove_inode_hash(bdev->bd_inode);
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200721}
722
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000723/**
724 * del_gendisk - remove the gendisk
725 * @disk: the struct gendisk to remove
726 *
727 * Removes the gendisk and all its associated resources. This deletes the
728 * partitions associated with the gendisk, and unregisters the associated
729 * request_queue.
730 *
731 * This is the counter to the respective __device_add_disk() call.
732 *
733 * The final removal of the struct gendisk happens when its refcount reaches 0
734 * with put_disk(), which should be called after del_gendisk(), if
735 * __device_add_disk() was used.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000736 *
737 * Drivers exist which depend on the release of the gendisk to be synchronous,
738 * it should not be deferred.
739 *
740 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000741 */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100742void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Tejun Heod2bf1b62010-12-08 20:57:36 +0100744 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100745 struct block_device *part;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100746
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000747 might_sleep();
748
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100749 if (WARN_ON_ONCE(!disk->queue))
750 return;
751
Martin K. Petersen25520d52015-10-21 13:19:49 -0400752 blk_integrity_del(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +0100753 disk_del_events(disk);
754
Jan Kara56c09082018-02-26 13:01:41 +0100755 /*
756 * Block lookups of the disk until all bdevs are unhashed and the
757 * disk is marked as dead (GENHD_FL_UP cleared).
758 */
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100759 down_write(&bdev_lookup_sem);
760
Tejun Heod2bf1b62010-12-08 20:57:36 +0100761 /* invalidate stuff */
Christoph Hellwig0470dd92021-01-24 11:02:40 +0100762 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100763 while ((part = disk_part_iter_next(&piter))) {
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100764 invalidate_partition(part);
Christoph Hellwig0d021292020-11-27 16:43:51 +0100765 delete_partition(part);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100766 }
767 disk_part_iter_exit(&piter);
768
Christoph Hellwig71773cf2020-11-24 09:20:37 +0100769 invalidate_partition(disk->part0);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100770 set_capacity(disk, 0);
771 disk->flags &= ~GENHD_FL_UP;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100772 up_write(&bdev_lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100773
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100774 if (!(disk->flags & GENHD_FL_HIDDEN)) {
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300775 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100776
Jan Kara90f16fd2017-03-08 17:48:33 +0100777 /*
778 * Unregister bdi before releasing device numbers (as they can
779 * get reused and we'd get clashes in sysfs).
780 */
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100781 bdi_unregister(disk->queue->backing_dev_info);
Jan Kara90f16fd2017-03-08 17:48:33 +0100782 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100783
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100784 blk_unregister_queue(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100785
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100786 kobject_put(disk->part0->bd_holder_dir);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100787 kobject_put(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100789 part_stat_set_all(disk->part0, 0);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100790 disk->part0->bd_stamp = 0;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100791 if (!sysfs_deprecated)
792 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
Ming Lei25e823c2013-02-22 16:34:13 -0800793 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100794 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100796EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Vishal Verma99e66082016-01-09 08:36:51 -0800798/* sysfs access to bad-blocks list. */
799static ssize_t disk_badblocks_show(struct device *dev,
800 struct device_attribute *attr,
801 char *page)
802{
803 struct gendisk *disk = dev_to_disk(dev);
804
805 if (!disk->bb)
806 return sprintf(page, "\n");
807
808 return badblocks_show(disk->bb, page, 0);
809}
810
811static ssize_t disk_badblocks_store(struct device *dev,
812 struct device_attribute *attr,
813 const char *page, size_t len)
814{
815 struct gendisk *disk = dev_to_disk(dev);
816
817 if (!disk->bb)
818 return -ENXIO;
819
820 return badblocks_store(disk->bb, page, len, 0);
821}
822
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100823void blk_request_module(dev_t devt)
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100824{
Christoph Hellwiga160c612020-10-29 15:58:28 +0100825 unsigned int major = MAJOR(devt);
826 struct blk_major_name **n;
827
828 mutex_lock(&major_names_lock);
829 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
830 if ((*n)->major == major && (*n)->probe) {
831 (*n)->probe(devt);
832 mutex_unlock(&major_names_lock);
833 return;
834 }
835 }
836 mutex_unlock(&major_names_lock);
837
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100838 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
839 /* Make old-style 2.4 aliases work */
840 request_module("block-major-%d", MAJOR(devt));
841}
842
Tejun Heof331c022008-09-03 09:01:48 +0200843/**
844 * bdget_disk - do bdget() by gendisk and partition number
845 * @disk: gendisk of interest
846 * @partno: partition number
847 *
848 * Find partition @partno from @disk, do bdget() on it.
849 *
850 * CONTEXT:
851 * Don't care.
852 *
853 * RETURNS:
854 * Resulting block_device on success, NULL on failure.
855 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200856struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200857{
Tejun Heo548b10e2008-08-29 09:01:47 +0200858 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200859
Christoph Hellwig0d021292020-11-27 16:43:51 +0100860 rcu_read_lock();
861 bdev = __disk_get_part(disk, partno);
862 if (bdev && !bdgrab(bdev))
863 bdev = NULL;
864 rcu_read_unlock();
Tejun Heof331c022008-09-03 09:01:48 +0200865
Tejun Heo548b10e2008-08-29 09:01:47 +0200866 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200867}
Tejun Heof331c022008-09-03 09:01:48 +0200868
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700869/*
870 * print a full list of all partitions - intended for places where the root
871 * filesystem can't be mounted and thus to give the victim some idea of what
872 * went wrong
873 */
874void __init printk_all_partitions(void)
875{
Tejun Heodef4e382008-09-03 08:57:12 +0200876 struct class_dev_iter iter;
877 struct device *dev;
878
879 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
880 while ((dev = class_dev_iter_next(&iter))) {
881 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200882 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100883 struct block_device *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900884 char name_buf[BDEVNAME_SIZE];
885 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200886
887 /*
888 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300889 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +0200890 */
891 if (get_capacity(disk) == 0 ||
892 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
893 continue;
894
895 /*
896 * Note, unlike /proc/partitions, I am showing the
897 * numbers in hex - the same format as the root=
898 * option takes.
899 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900900 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
901 while ((part = disk_part_iter_next(&piter))) {
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100902 bool is_part0 = part == disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +0200903
Will Drewryb5af9212010-08-31 15:47:07 -0500904 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100905 bdevt_str(part->bd_dev, devt_buf),
906 bdev_nr_sectors(part) >> 1,
907 disk_name(disk, part->bd_partno, name_buf),
908 part->bd_meta_info ?
909 part->bd_meta_info->uuid : "");
Tejun Heo074a7ac2008-08-25 19:56:14 +0900910 if (is_part0) {
Dan Williams52c44d92016-06-15 19:43:07 -0700911 if (dev->parent && dev->parent->driver)
Tejun Heo074a7ac2008-08-25 19:56:14 +0900912 printk(" driver: %s\n",
Dan Williams52c44d92016-06-15 19:43:07 -0700913 dev->parent->driver->name);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900914 else
915 printk(" (driver?)\n");
916 } else
917 printk("\n");
918 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200919 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200920 }
921 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924#ifdef CONFIG_PROC_FS
925/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200926static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400927{
Tejun Heodef4e382008-09-03 08:57:12 +0200928 loff_t skip = *pos;
929 struct class_dev_iter *iter;
930 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400931
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200932 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +0200933 if (!iter)
934 return ERR_PTR(-ENOMEM);
935
936 seqf->private = iter;
937 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
938 do {
939 dev = class_dev_iter_next(iter);
940 if (!dev)
941 return NULL;
942 } while (skip--);
943
944 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400945}
946
Tejun Heodef4e382008-09-03 08:57:12 +0200947static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200949 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400950
Tejun Heodef4e382008-09-03 08:57:12 +0200951 (*pos)++;
952 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200953 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400954 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return NULL;
957}
958
Tejun Heodef4e382008-09-03 08:57:12 +0200959static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400960{
Tejun Heodef4e382008-09-03 08:57:12 +0200961 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400962
Tejun Heodef4e382008-09-03 08:57:12 +0200963 /* stop is called even after start failed :-( */
964 if (iter) {
965 class_dev_iter_exit(iter);
966 kfree(iter);
Vegard Nossum77da1602016-07-29 10:40:31 +0200967 seqf->private = NULL;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Tejun Heodef4e382008-09-03 08:57:12 +0200971static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Jianpeng Ma06768062012-08-03 10:42:00 +0200973 void *p;
Tejun Heodef4e382008-09-03 08:57:12 +0200974
975 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +0100976 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +0200977 seq_puts(seqf, "major minor #blocks name\n\n");
978 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Tejun Heocf771cb2008-09-03 09:01:09 +0200981static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200984 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100985 struct block_device *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 char buf[BDEVNAME_SIZE];
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +0200989 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200990 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return 0;
992 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
993 return 0;
994
995 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900996 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200997 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900998 seq_printf(seqf, "%4d %7d %10llu %s\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100999 MAJOR(part->bd_dev), MINOR(part->bd_dev),
1000 bdev_nr_sectors(part) >> 1,
1001 disk_name(sgp, part->bd_partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001002 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 return 0;
1005}
1006
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001007static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001008 .start = show_partition_start,
1009 .next = disk_seqf_next,
1010 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001011 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012};
1013#endif
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015static int __init genhd_device_init(void)
1016{
Dan Williamse105b8b2008-04-21 10:51:07 -07001017 int error;
1018
1019 block_class.dev_kobj = sysfs_dev_block_kobj;
1020 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -07001021 if (unlikely(error))
1022 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001024
Zhang, Yanmin561ec682008-11-14 08:26:30 +01001025 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1026
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001027 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +02001028 if (!sysfs_deprecated)
1029 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -08001030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033subsys_initcall(genhd_device_init);
1034
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001035static ssize_t disk_range_show(struct device *dev,
1036 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001038 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001040 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Tejun Heo1f014292008-08-25 19:47:23 +09001043static ssize_t disk_ext_range_show(struct device *dev,
1044 struct device_attribute *attr, char *buf)
1045{
1046 struct gendisk *disk = dev_to_disk(dev);
1047
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001048 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +09001049}
1050
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001051static ssize_t disk_removable_show(struct device *dev,
1052 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +02001053{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001054 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001055
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001056 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001060static ssize_t disk_hidden_show(struct device *dev,
1061 struct device_attribute *attr, char *buf)
1062{
1063 struct gendisk *disk = dev_to_disk(dev);
1064
1065 return sprintf(buf, "%d\n",
1066 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1067}
1068
Kay Sievers1c9ce522008-06-13 09:41:00 +02001069static ssize_t disk_ro_show(struct device *dev,
1070 struct device_attribute *attr, char *buf)
1071{
1072 struct gendisk *disk = dev_to_disk(dev);
1073
Tejun Heob7db9952008-08-25 19:56:10 +09001074 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +02001075}
1076
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001077ssize_t part_size_show(struct device *dev,
1078 struct device_attribute *attr, char *buf)
1079{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001080 return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001081}
1082
1083ssize_t part_stat_show(struct device *dev,
1084 struct device_attribute *attr, char *buf)
1085{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001086 struct block_device *bdev = dev_to_bdev(dev);
1087 struct request_queue *q = bdev->bd_disk->queue;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001088 struct disk_stats stat;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001089 unsigned int inflight;
1090
Christoph Hellwig0d021292020-11-27 16:43:51 +01001091 part_stat_read_all(bdev, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001092 if (queue_is_mq(q))
Christoph Hellwig0d021292020-11-27 16:43:51 +01001093 inflight = blk_mq_in_flight(q, bdev);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001094 else
Christoph Hellwig0d021292020-11-27 16:43:51 +01001095 inflight = part_in_flight(bdev);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001096
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001097 return sprintf(buf,
1098 "%8lu %8lu %8llu %8u "
1099 "%8lu %8lu %8llu %8u "
1100 "%8u %8u %8u "
1101 "%8lu %8lu %8llu %8u "
1102 "%8lu %8u"
1103 "\n",
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001104 stat.ios[STAT_READ],
1105 stat.merges[STAT_READ],
1106 (unsigned long long)stat.sectors[STAT_READ],
1107 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
1108 stat.ios[STAT_WRITE],
1109 stat.merges[STAT_WRITE],
1110 (unsigned long long)stat.sectors[STAT_WRITE],
1111 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001112 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001113 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001114 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1115 stat.nsecs[STAT_WRITE] +
1116 stat.nsecs[STAT_DISCARD] +
1117 stat.nsecs[STAT_FLUSH],
1118 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001119 stat.ios[STAT_DISCARD],
1120 stat.merges[STAT_DISCARD],
1121 (unsigned long long)stat.sectors[STAT_DISCARD],
1122 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
1123 stat.ios[STAT_FLUSH],
1124 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001125}
1126
1127ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
1128 char *buf)
1129{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001130 struct block_device *bdev = dev_to_bdev(dev);
1131 struct request_queue *q = bdev->bd_disk->queue;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001132 unsigned int inflight[2];
1133
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001134 if (queue_is_mq(q))
Christoph Hellwig0d021292020-11-27 16:43:51 +01001135 blk_mq_in_flight_rw(q, bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001136 else
Christoph Hellwig0d021292020-11-27 16:43:51 +01001137 part_in_flight_rw(bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001138
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001139 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1140}
1141
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001142static ssize_t disk_capability_show(struct device *dev,
1143 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001144{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001145 struct gendisk *disk = dev_to_disk(dev);
1146
1147 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001148}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001149
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001150static ssize_t disk_alignment_offset_show(struct device *dev,
1151 struct device_attribute *attr,
1152 char *buf)
1153{
1154 struct gendisk *disk = dev_to_disk(dev);
1155
1156 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1157}
1158
Martin K. Petersen86b37282009-11-10 11:50:21 +01001159static ssize_t disk_discard_alignment_show(struct device *dev,
1160 struct device_attribute *attr,
1161 char *buf)
1162{
1163 struct gendisk *disk = dev_to_disk(dev);
1164
Martin K. Petersendd3d1452010-01-11 03:21:48 -05001165 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +01001166}
1167
Joe Perches5657a812018-05-24 13:38:59 -06001168static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1169static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1170static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1171static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1172static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1173static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1174static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1175static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1176static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1177static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1178static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1179static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001180
Akinobu Mitac17bb492006-12-08 02:39:46 -08001181#ifdef CONFIG_FAIL_MAKE_REQUEST
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001182ssize_t part_fail_show(struct device *dev,
1183 struct device_attribute *attr, char *buf)
1184{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001185 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001186}
1187
1188ssize_t part_fail_store(struct device *dev,
1189 struct device_attribute *attr,
1190 const char *buf, size_t count)
1191{
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001192 int i;
1193
1194 if (count > 0 && sscanf(buf, "%d", &i) > 0)
Christoph Hellwig0d021292020-11-27 16:43:51 +01001195 dev_to_bdev(dev)->bd_make_it_fail = i;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001196
1197 return count;
1198}
1199
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001200static struct device_attribute dev_attr_fail =
Joe Perches5657a812018-05-24 13:38:59 -06001201 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001202#endif /* CONFIG_FAIL_MAKE_REQUEST */
1203
Jens Axboe581d4e22008-09-14 05:56:33 -07001204#ifdef CONFIG_FAIL_IO_TIMEOUT
1205static struct device_attribute dev_attr_fail_timeout =
Joe Perches5657a812018-05-24 13:38:59 -06001206 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
Jens Axboe581d4e22008-09-14 05:56:33 -07001207#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001208
1209static struct attribute *disk_attrs[] = {
1210 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +09001211 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001212 &dev_attr_removable.attr,
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001213 &dev_attr_hidden.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001214 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001215 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001216 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001217 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001218 &dev_attr_capability.attr,
1219 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001220 &dev_attr_inflight.attr,
Vishal Verma99e66082016-01-09 08:36:51 -08001221 &dev_attr_badblocks.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001222#ifdef CONFIG_FAIL_MAKE_REQUEST
1223 &dev_attr_fail.attr,
1224#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001225#ifdef CONFIG_FAIL_IO_TIMEOUT
1226 &dev_attr_fail_timeout.attr,
1227#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001228 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229};
1230
Dan Williams9438b3e02017-04-27 14:46:26 -07001231static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1232{
1233 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1234 struct gendisk *disk = dev_to_disk(dev);
1235
1236 if (a == &dev_attr_badblocks.attr && !disk->bb)
1237 return 0;
1238 return a->mode;
1239}
1240
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001241static struct attribute_group disk_attr_group = {
1242 .attrs = disk_attrs,
Dan Williams9438b3e02017-04-27 14:46:26 -07001243 .is_visible = disk_visible,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001244};
1245
David Brownella4dbd672009-06-24 10:06:31 -07001246static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001247 &disk_attr_group,
1248 NULL
1249};
1250
Tejun Heo540eed52008-08-25 19:56:15 +09001251/**
1252 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1253 * @disk: disk to replace part_tbl for
1254 * @new_ptbl: new part_tbl to install
1255 *
1256 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1257 * original ptbl is freed using RCU callback.
1258 *
1259 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001260 * Matching bd_mutex locked or the caller is the only user of @disk.
Tejun Heo540eed52008-08-25 19:56:15 +09001261 */
1262static void disk_replace_part_tbl(struct gendisk *disk,
1263 struct disk_part_tbl *new_ptbl)
1264{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001265 struct disk_part_tbl *old_ptbl =
1266 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001267
1268 rcu_assign_pointer(disk->part_tbl, new_ptbl);
Jens Axboea6f23652008-10-24 12:52:42 +02001269
1270 if (old_ptbl) {
1271 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
Lai Jiangshan57bdfbf2011-03-18 11:42:58 +08001272 kfree_rcu(old_ptbl, rcu_head);
Jens Axboea6f23652008-10-24 12:52:42 +02001273 }
Tejun Heo540eed52008-08-25 19:56:15 +09001274}
1275
1276/**
1277 * disk_expand_part_tbl - expand disk->part_tbl
1278 * @disk: disk to expand part_tbl for
1279 * @partno: expand such that this partno can fit in
1280 *
1281 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1282 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1283 *
1284 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001285 * Matching bd_mutex locked or the caller is the only user of @disk.
1286 * Might sleep.
Tejun Heo540eed52008-08-25 19:56:15 +09001287 *
1288 * RETURNS:
1289 * 0 on success, -errno on failure.
1290 */
1291int disk_expand_part_tbl(struct gendisk *disk, int partno)
1292{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001293 struct disk_part_tbl *old_ptbl =
1294 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001295 struct disk_part_tbl *new_ptbl;
1296 int len = old_ptbl ? old_ptbl->len : 0;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001297 int i, target;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001298
1299 /*
1300 * check for int overflow, since we can get here from blkpg_ioctl()
1301 * with a user passed 'partno'.
1302 */
1303 target = partno + 1;
1304 if (target < 0)
1305 return -EINVAL;
Tejun Heo540eed52008-08-25 19:56:15 +09001306
1307 /* disk_max_parts() is zero during initialization, ignore if so */
1308 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1309 return -EINVAL;
1310
1311 if (target <= len)
1312 return 0;
1313
Gustavo A. R. Silva78b90a22019-05-31 13:47:54 -05001314 new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
1315 disk->node_id);
Tejun Heo540eed52008-08-25 19:56:15 +09001316 if (!new_ptbl)
1317 return -ENOMEM;
1318
Tejun Heo540eed52008-08-25 19:56:15 +09001319 new_ptbl->len = target;
1320
1321 for (i = 0; i < len; i++)
1322 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1323
1324 disk_replace_part_tbl(disk, new_ptbl);
1325 return 0;
1326}
1327
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001328/**
1329 * disk_release - releases all allocated resources of the gendisk
1330 * @dev: the device representing this disk
1331 *
1332 * This function releases all allocated resources of the gendisk.
1333 *
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001334 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1335 * assigned. Since the request_queue sits on top of the gendisk for these
1336 * drivers we also call blk_put_queue() for them, and we expect the
1337 * request_queue refcount to reach 0 at this point, and so the request_queue
1338 * will also be freed prior to the disk.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001339 *
1340 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001341 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001342static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001344 struct gendisk *disk = dev_to_disk(dev);
1345
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001346 might_sleep();
1347
Keith Busch2da78092014-08-26 09:05:36 -06001348 blk_free_devt(dev->devt);
Tejun Heo77ea8872010-12-08 20:57:37 +01001349 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +09001351 disk_replace_part_tbl(disk, NULL);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001352 bdput(disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001353 if (disk->queue)
1354 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 kfree(disk);
1356}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001357struct class block_class = {
1358 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359};
1360
Kay Sievers3c2670e2013-04-06 09:56:00 -07001361static char *block_devnode(struct device *dev, umode_t *mode,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001362 kuid_t *uid, kgid_t *gid)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001363{
1364 struct gendisk *disk = dev_to_disk(dev);
1365
Christoph Hellwig348e1142020-03-27 09:07:17 +01001366 if (disk->fops->devnode)
1367 return disk->fops->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001368 return NULL;
1369}
1370
Boris Burkovef45fe42020-06-01 13:12:05 -07001371const struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001372 .name = "disk",
1373 .groups = disk_attr_groups,
1374 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001375 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376};
1377
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001378#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001379/*
1380 * aggregate disk stat collector. Uses the same stats that the sysfs
1381 * entries do, above, but makes them available through one seq_file.
1382 *
1383 * The output looks suspiciously like /proc/partitions with a bunch of
1384 * extra fields.
1385 */
1386static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001389 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001390 struct block_device *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 char buf[BDEVNAME_SIZE];
Mikulas Patockae016b782018-12-06 11:41:21 -05001392 unsigned int inflight;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001393 struct disk_stats stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001396 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001397 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 " rio rmerge rsect ruse wio wmerge "
1399 "wsect wuse running use aveq"
1400 "\n\n");
1401 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001402
Tejun Heo71982a42009-04-17 08:34:48 +02001403 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001404 while ((hd = disk_part_iter_next(&piter))) {
Christoph Hellwig0d021292020-11-27 16:43:51 +01001405 part_stat_read_all(hd, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001406 if (queue_is_mq(gp->queue))
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001407 inflight = blk_mq_in_flight(gp->queue, hd);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001408 else
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001409 inflight = part_in_flight(hd);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001410
Michael Callahanbdca3c82018-07-18 04:47:40 -07001411 seq_printf(seqf, "%4d %7d %s "
1412 "%lu %lu %lu %u "
1413 "%lu %lu %lu %u "
1414 "%u %u %u "
Konstantin Khlebnikovb6866312019-11-21 13:40:26 +03001415 "%lu %lu %lu %u "
1416 "%lu %u"
1417 "\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001418 MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
1419 disk_name(gp, hd->bd_partno, buf),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001420 stat.ios[STAT_READ],
1421 stat.merges[STAT_READ],
1422 stat.sectors[STAT_READ],
1423 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1424 NSEC_PER_MSEC),
1425 stat.ios[STAT_WRITE],
1426 stat.merges[STAT_WRITE],
1427 stat.sectors[STAT_WRITE],
1428 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1429 NSEC_PER_MSEC),
Mikulas Patockae016b782018-12-06 11:41:21 -05001430 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001431 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001432 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1433 stat.nsecs[STAT_WRITE] +
1434 stat.nsecs[STAT_DISCARD] +
1435 stat.nsecs[STAT_FLUSH],
1436 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001437 stat.ios[STAT_DISCARD],
1438 stat.merges[STAT_DISCARD],
1439 stat.sectors[STAT_DISCARD],
1440 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1441 NSEC_PER_MSEC),
1442 stat.ios[STAT_FLUSH],
1443 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1444 NSEC_PER_MSEC)
Jerome Marchand28f39d52008-02-08 11:04:56 +01001445 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001447 disk_part_iter_exit(&piter);
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return 0;
1450}
1451
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001452static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001453 .start = disk_seqf_start,
1454 .next = disk_seqf_next,
1455 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 .show = diskstats_show
1457};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001458
1459static int __init proc_genhd_init(void)
1460{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001461 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1462 proc_create_seq("partitions", 0, NULL, &partitions_op);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001463 return 0;
1464}
1465module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001466#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Tejun Heocf771cb2008-09-03 09:01:09 +02001468dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001469{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001470 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001471 struct class_dev_iter iter;
1472 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001473
Tejun Heodef4e382008-09-03 08:57:12 +02001474 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1475 while ((dev = class_dev_iter_next(&iter))) {
1476 struct gendisk *disk = dev_to_disk(dev);
Christoph Hellwig0d021292020-11-27 16:43:51 +01001477 struct block_device *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001478
Kay Sievers3ada8b72009-01-06 10:44:43 -08001479 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001480 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001481
Neil Brown41b8c852009-02-18 10:33:59 +01001482 if (partno < disk->minors) {
1483 /* We need to return the right devno, even
1484 * if the partition doesn't exist yet.
1485 */
1486 devt = MKDEV(MAJOR(dev->devt),
1487 MINOR(dev->devt) + partno);
1488 break;
1489 }
Christoph Hellwig0d021292020-11-27 16:43:51 +01001490 part = bdget_disk(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001491 if (part) {
Christoph Hellwig0d021292020-11-27 16:43:51 +01001492 devt = part->bd_dev;
1493 bdput(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001494 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001495 }
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001496 }
Tejun Heodef4e382008-09-03 08:57:12 +02001497 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001498 return devt;
1499}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001500
Byungchul Parke319e1f2017-10-25 17:56:05 +09001501struct gendisk *__alloc_disk_node(int minors, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001502{
1503 struct gendisk *disk;
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001504 struct disk_part_tbl *ptbl;
Christoph Lameter19460892005-06-23 00:08:19 -07001505
Christoph Hellwigde65b012017-08-23 19:10:29 +02001506 if (minors > DISK_MAX_PARTS) {
1507 printk(KERN_ERR
Randy Dunlap7fb52622017-11-18 17:43:38 -08001508 "block: can't allocate more than %d partitions\n",
Christoph Hellwigde65b012017-08-23 19:10:29 +02001509 DISK_MAX_PARTS);
1510 minors = DISK_MAX_PARTS;
1511 }
Christoph Lameter19460892005-06-23 00:08:19 -07001512
Joe Perchesc1b511e2013-08-29 15:21:42 -07001513 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001514 if (!disk)
1515 return NULL;
Jens Axboe6c23a962011-01-07 08:43:37 +01001516
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001517 disk->part0 = bdev_alloc(disk, 0);
1518 if (!disk->part0)
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001519 goto out_free_disk;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001520
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001521 disk->node_id = node_id;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001522 if (disk_expand_part_tbl(disk, 0))
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +01001523 goto out_bdput;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001524
1525 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001526 rcu_assign_pointer(ptbl->part[0], disk->part0);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001527
1528 disk->minors = minors;
1529 rand_initialize_disk(disk);
1530 disk_to_dev(disk)->class = &block_class;
1531 disk_to_dev(disk)->type = &disk_type;
1532 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return disk;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001534
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001535out_bdput:
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001536 bdput(disk->part0);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001537out_free_disk:
1538 kfree(disk);
1539 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
Byungchul Parke319e1f2017-10-25 17:56:05 +09001541EXPORT_SYMBOL(__alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001543/**
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001544 * put_disk - decrements the gendisk refcount
Randy Dunlap0d20dcc2020-07-30 18:42:30 -07001545 * @disk: the struct gendisk to decrement the refcount for
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001546 *
1547 * This decrements the refcount for the struct gendisk. When this reaches 0
1548 * we'll have disk_release() called.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001549 *
1550 * Context: Any context, but the last reference must not be dropped from
1551 * atomic context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001552 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553void put_disk(struct gendisk *disk)
1554{
1555 if (disk)
Christoph Hellwigefdc41c2020-11-10 07:25:37 +01001556 put_device(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558EXPORT_SYMBOL(put_disk);
1559
Hannes Reineckee3264a42009-07-28 09:13:13 +02001560static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1561{
1562 char event[] = "DISK_RO=1";
1563 char *envp[] = { event, NULL };
1564
1565 if (!ro)
1566 event[8] = '0';
1567 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1568}
1569
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001570/**
1571 * set_disk_ro - set a gendisk read-only
1572 * @disk: gendisk to operate on
1573 * @ready_only: %true to set the disk read-only, %false set the disk read/write
1574 *
1575 * This function is used to indicate whether a given disk device should have its
1576 * read-only flag set. set_disk_ro() is typically used by device drivers to
1577 * indicate whether the underlying physical device is write-protected.
1578 */
1579void set_disk_ro(struct gendisk *disk, bool read_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001581 if (read_only) {
1582 if (test_and_set_bit(GD_READ_ONLY, &disk->state))
1583 return;
1584 } else {
1585 if (!test_and_clear_bit(GD_READ_ONLY, &disk->state))
1586 return;
Hannes Reineckee3264a42009-07-28 09:13:13 +02001587 }
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001588 set_disk_ro_uevent(disk, read_only);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590EXPORT_SYMBOL(set_disk_ro);
1591
1592int bdev_read_only(struct block_device *bdev)
1593{
Christoph Hellwig947139b2021-01-09 11:42:52 +01001594 return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596EXPORT_SYMBOL(bdev_read_only);
1597
Tejun Heo77ea8872010-12-08 20:57:37 +01001598/*
1599 * Disk events - monitor disk events like media change and eject request.
1600 */
1601struct disk_events {
1602 struct list_head node; /* all disk_event's */
1603 struct gendisk *disk; /* the associated disk */
1604 spinlock_t lock;
1605
Tejun Heofdd514e2011-06-09 20:43:59 +02001606 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001607 int block; /* event blocking depth */
1608 unsigned int pending; /* events already sent out */
1609 unsigned int clearing; /* events being cleared */
1610
1611 long poll_msecs; /* interval, -1 for default */
1612 struct delayed_work dwork;
1613};
1614
1615static const char *disk_events_strs[] = {
1616 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1617 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1618};
1619
1620static char *disk_uevents[] = {
1621 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1622 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1623};
1624
1625/* list of all disk_events */
1626static DEFINE_MUTEX(disk_events_mutex);
1627static LIST_HEAD(disk_events);
1628
1629/* disable in-kernel polling by default */
Wei Tang1fe8f342015-11-24 09:58:46 +08001630static unsigned long disk_events_dfl_poll_msecs;
Tejun Heo77ea8872010-12-08 20:57:37 +01001631
1632static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1633{
1634 struct disk_events *ev = disk->ev;
1635 long intv_msecs = 0;
1636
1637 /*
1638 * If device-specific poll interval is set, always use it. If
Martin Wilck673387a2019-03-27 14:51:01 +01001639 * the default is being used, poll if the POLL flag is set.
Tejun Heo77ea8872010-12-08 20:57:37 +01001640 */
1641 if (ev->poll_msecs >= 0)
1642 intv_msecs = ev->poll_msecs;
Martin Wilckc92e2f02019-03-27 14:51:02 +01001643 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
Tejun Heo77ea8872010-12-08 20:57:37 +01001644 intv_msecs = disk_events_dfl_poll_msecs;
1645
1646 return msecs_to_jiffies(intv_msecs);
1647}
1648
Tejun Heoc3af54a2011-06-09 20:43:55 +02001649/**
1650 * disk_block_events - block and flush disk event checking
1651 * @disk: disk to block events for
1652 *
1653 * On return from this function, it is guaranteed that event checking
1654 * isn't in progress and won't happen until unblocked by
1655 * disk_unblock_events(). Events blocking is counted and the actual
1656 * unblocking happens after the matching number of unblocks are done.
1657 *
1658 * Note that this intentionally does not block event checking from
1659 * disk_clear_events().
1660 *
1661 * CONTEXT:
1662 * Might sleep.
1663 */
1664void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001665{
1666 struct disk_events *ev = disk->ev;
1667 unsigned long flags;
1668 bool cancel;
1669
Tejun Heoc3af54a2011-06-09 20:43:55 +02001670 if (!ev)
1671 return;
1672
Tejun Heofdd514e2011-06-09 20:43:59 +02001673 /*
1674 * Outer mutex ensures that the first blocker completes canceling
1675 * the event work before further blockers are allowed to finish.
1676 */
1677 mutex_lock(&ev->block_mutex);
1678
Tejun Heo77ea8872010-12-08 20:57:37 +01001679 spin_lock_irqsave(&ev->lock, flags);
1680 cancel = !ev->block++;
1681 spin_unlock_irqrestore(&ev->lock, flags);
1682
Tejun Heoc3af54a2011-06-09 20:43:55 +02001683 if (cancel)
1684 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001685
1686 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001687}
1688
1689static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1690{
1691 struct disk_events *ev = disk->ev;
1692 unsigned long intv;
1693 unsigned long flags;
1694
1695 spin_lock_irqsave(&ev->lock, flags);
1696
1697 if (WARN_ON_ONCE(ev->block <= 0))
1698 goto out_unlock;
1699
1700 if (--ev->block)
1701 goto out_unlock;
1702
Tejun Heo77ea8872010-12-08 20:57:37 +01001703 intv = disk_events_poll_jiffies(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001704 if (check_now)
Viresh Kumar695588f2013-04-24 17:12:56 +05301705 queue_delayed_work(system_freezable_power_efficient_wq,
1706 &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001707 else if (intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301708 queue_delayed_work(system_freezable_power_efficient_wq,
1709 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001710out_unlock:
1711 spin_unlock_irqrestore(&ev->lock, flags);
1712}
1713
1714/**
Tejun Heo77ea8872010-12-08 20:57:37 +01001715 * disk_unblock_events - unblock disk event checking
1716 * @disk: disk to unblock events for
1717 *
1718 * Undo disk_block_events(). When the block count reaches zero, it
1719 * starts events polling if configured.
1720 *
1721 * CONTEXT:
1722 * Don't care. Safe to call from irq context.
1723 */
1724void disk_unblock_events(struct gendisk *disk)
1725{
1726 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01001727 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001728}
1729
1730/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02001731 * disk_flush_events - schedule immediate event checking and flushing
1732 * @disk: disk to check and flush events for
1733 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01001734 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02001735 * Schedule immediate event checking on @disk if not blocked. Events in
1736 * @mask are scheduled to be cleared from the driver. Note that this
1737 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01001738 *
1739 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02001740 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01001741 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001742void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001743{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001744 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001745
1746 if (!ev)
1747 return;
1748
Tejun Heo85ef06d2011-07-01 16:17:47 +02001749 spin_lock_irq(&ev->lock);
1750 ev->clearing |= mask;
Tejun Heo41f63c52012-08-03 10:30:47 -07001751 if (!ev->block)
Viresh Kumar695588f2013-04-24 17:12:56 +05301752 mod_delayed_work(system_freezable_power_efficient_wq,
1753 &ev->dwork, 0);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001754 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001755}
Tejun Heo77ea8872010-12-08 20:57:37 +01001756
1757/**
1758 * disk_clear_events - synchronously check, clear and return pending events
1759 * @disk: disk to fetch and clear events from
Masanari Iidada3dae52014-09-09 01:27:23 +09001760 * @mask: mask of events to be fetched and cleared
Tejun Heo77ea8872010-12-08 20:57:37 +01001761 *
1762 * Disk events are synchronously checked and pending events in @mask
1763 * are cleared and returned. This ignores the block count.
1764 *
1765 * CONTEXT:
1766 * Might sleep.
1767 */
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001768static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001769{
Tejun Heo77ea8872010-12-08 20:57:37 +01001770 struct disk_events *ev = disk->ev;
1771 unsigned int pending;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001772 unsigned int clearing = mask;
Tejun Heo77ea8872010-12-08 20:57:37 +01001773
Christoph Hellwiga564e232020-07-08 14:25:41 +02001774 if (!ev)
Tejun Heo77ea8872010-12-08 20:57:37 +01001775 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001776
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001777 disk_block_events(disk);
1778
1779 /*
1780 * store the union of mask and ev->clearing on the stack so that the
1781 * race with disk_flush_events does not cause ambiguity (ev->clearing
1782 * can still be modified even if events are blocked).
1783 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001784 spin_lock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001785 clearing |= ev->clearing;
1786 ev->clearing = 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001787 spin_unlock_irq(&ev->lock);
1788
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001789 disk_check_events(ev, &clearing);
Derek Basehoreaea24a82012-12-18 12:27:18 -08001790 /*
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001791 * if ev->clearing is not 0, the disk_flush_events got called in the
1792 * middle of this function, so we want to run the workfn without delay.
Derek Basehoreaea24a82012-12-18 12:27:18 -08001793 */
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001794 __disk_unblock_events(disk, ev->clearing ? true : false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001795
1796 /* then, fetch and clear pending events */
1797 spin_lock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001798 pending = ev->pending & mask;
1799 ev->pending &= ~mask;
1800 spin_unlock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001801 WARN_ON_ONCE(clearing & mask);
Tejun Heo77ea8872010-12-08 20:57:37 +01001802
1803 return pending;
1804}
1805
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001806/**
1807 * bdev_check_media_change - check if a removable media has been changed
1808 * @bdev: block device to check
1809 *
1810 * Check whether a removable media has been changed, and attempt to free all
1811 * dentries and inodes and invalidates all block device page cache entries in
1812 * that case.
1813 *
1814 * Returns %true if the block device changed, or %false if not.
1815 */
1816bool bdev_check_media_change(struct block_device *bdev)
1817{
1818 unsigned int events;
1819
1820 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1821 DISK_EVENT_EJECT_REQUEST);
1822 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1823 return false;
1824
1825 if (__invalidate_device(bdev, true))
1826 pr_warn("VFS: busy inodes on changed media %s\n",
1827 bdev->bd_disk->disk_name);
Christoph Hellwig38430f02020-09-21 09:19:45 +02001828 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001829 return true;
1830}
1831EXPORT_SYMBOL(bdev_check_media_change);
1832
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001833/*
1834 * Separate this part out so that a different pointer for clearing_ptr can be
1835 * passed in for disk_clear_events.
1836 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001837static void disk_events_workfn(struct work_struct *work)
1838{
1839 struct delayed_work *dwork = to_delayed_work(work);
1840 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001841
1842 disk_check_events(ev, &ev->clearing);
1843}
1844
1845static void disk_check_events(struct disk_events *ev,
1846 unsigned int *clearing_ptr)
1847{
Tejun Heo77ea8872010-12-08 20:57:37 +01001848 struct gendisk *disk = ev->disk;
1849 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001850 unsigned int clearing = *clearing_ptr;
Tejun Heo77ea8872010-12-08 20:57:37 +01001851 unsigned int events;
1852 unsigned long intv;
1853 int nr_events = 0, i;
1854
1855 /* check events */
1856 events = disk->fops->check_events(disk, clearing);
1857
1858 /* accumulate pending events and schedule next poll if necessary */
1859 spin_lock_irq(&ev->lock);
1860
1861 events &= ~ev->pending;
1862 ev->pending |= events;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001863 *clearing_ptr &= ~clearing;
Tejun Heo77ea8872010-12-08 20:57:37 +01001864
1865 intv = disk_events_poll_jiffies(disk);
1866 if (!ev->block && intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301867 queue_delayed_work(system_freezable_power_efficient_wq,
1868 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001869
1870 spin_unlock_irq(&ev->lock);
1871
Tejun Heo7c88a162011-04-21 19:43:58 +02001872 /*
1873 * Tell userland about new events. Only the events listed in
Martin Wilckc92e2f02019-03-27 14:51:02 +01001874 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1875 * is set. Otherwise, events are processed internally but never
1876 * get reported to userland.
Tejun Heo7c88a162011-04-21 19:43:58 +02001877 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001878 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Martin Wilckc92e2f02019-03-27 14:51:02 +01001879 if ((events & disk->events & (1 << i)) &&
1880 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
Tejun Heo77ea8872010-12-08 20:57:37 +01001881 envp[nr_events++] = disk_uevents[i];
1882
1883 if (nr_events)
1884 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1885}
1886
1887/*
1888 * A disk events enabled device has the following sysfs nodes under
1889 * its /sys/block/X/ directory.
1890 *
1891 * events : list of all supported events
1892 * events_async : list of events which can be detected w/o polling
Martin Wilck673387a2019-03-27 14:51:01 +01001893 * (always empty, only for backwards compatibility)
Tejun Heo77ea8872010-12-08 20:57:37 +01001894 * events_poll_msecs : polling interval, 0: disable, -1: system default
1895 */
1896static ssize_t __disk_events_show(unsigned int events, char *buf)
1897{
1898 const char *delim = "";
1899 ssize_t pos = 0;
1900 int i;
1901
1902 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1903 if (events & (1 << i)) {
1904 pos += sprintf(buf + pos, "%s%s",
1905 delim, disk_events_strs[i]);
1906 delim = " ";
1907 }
1908 if (pos)
1909 pos += sprintf(buf + pos, "\n");
1910 return pos;
1911}
1912
1913static ssize_t disk_events_show(struct device *dev,
1914 struct device_attribute *attr, char *buf)
1915{
1916 struct gendisk *disk = dev_to_disk(dev);
1917
Martin Wilckc92e2f02019-03-27 14:51:02 +01001918 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1919 return 0;
1920
Tejun Heo77ea8872010-12-08 20:57:37 +01001921 return __disk_events_show(disk->events, buf);
1922}
1923
1924static ssize_t disk_events_async_show(struct device *dev,
1925 struct device_attribute *attr, char *buf)
1926{
Martin Wilck673387a2019-03-27 14:51:01 +01001927 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001928}
1929
1930static ssize_t disk_events_poll_msecs_show(struct device *dev,
1931 struct device_attribute *attr,
1932 char *buf)
1933{
1934 struct gendisk *disk = dev_to_disk(dev);
1935
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001936 if (!disk->ev)
1937 return sprintf(buf, "-1\n");
1938
Tejun Heo77ea8872010-12-08 20:57:37 +01001939 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1940}
1941
1942static ssize_t disk_events_poll_msecs_store(struct device *dev,
1943 struct device_attribute *attr,
1944 const char *buf, size_t count)
1945{
1946 struct gendisk *disk = dev_to_disk(dev);
1947 long intv;
1948
1949 if (!count || !sscanf(buf, "%ld", &intv))
1950 return -EINVAL;
1951
1952 if (intv < 0 && intv != -1)
1953 return -EINVAL;
1954
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001955 if (!disk->ev)
1956 return -ENODEV;
1957
Tejun Heoc3af54a2011-06-09 20:43:55 +02001958 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001959 disk->ev->poll_msecs = intv;
1960 __disk_unblock_events(disk, true);
1961
1962 return count;
1963}
1964
Joe Perches5657a812018-05-24 13:38:59 -06001965static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
1966static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
1967static const DEVICE_ATTR(events_poll_msecs, 0644,
Tejun Heo77ea8872010-12-08 20:57:37 +01001968 disk_events_poll_msecs_show,
1969 disk_events_poll_msecs_store);
1970
1971static const struct attribute *disk_events_attrs[] = {
1972 &dev_attr_events.attr,
1973 &dev_attr_events_async.attr,
1974 &dev_attr_events_poll_msecs.attr,
1975 NULL,
1976};
1977
1978/*
1979 * The default polling interval can be specified by the kernel
1980 * parameter block.events_dfl_poll_msecs which defaults to 0
1981 * (disable). This can also be modified runtime by writing to
Akinobu Mita1624b0b2019-07-16 21:59:35 +09001982 * /sys/module/block/parameters/events_dfl_poll_msecs.
Tejun Heo77ea8872010-12-08 20:57:37 +01001983 */
1984static int disk_events_set_dfl_poll_msecs(const char *val,
1985 const struct kernel_param *kp)
1986{
1987 struct disk_events *ev;
1988 int ret;
1989
1990 ret = param_set_ulong(val, kp);
1991 if (ret < 0)
1992 return ret;
1993
1994 mutex_lock(&disk_events_mutex);
1995
1996 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02001997 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001998
1999 mutex_unlock(&disk_events_mutex);
2000
2001 return 0;
2002}
2003
2004static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
2005 .set = disk_events_set_dfl_poll_msecs,
2006 .get = param_get_ulong,
2007};
2008
2009#undef MODULE_PARAM_PREFIX
2010#define MODULE_PARAM_PREFIX "block."
2011
2012module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
2013 &disk_events_dfl_poll_msecs, 0644);
2014
2015/*
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002016 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
Tejun Heo77ea8872010-12-08 20:57:37 +01002017 */
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002018static void disk_alloc_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01002019{
2020 struct disk_events *ev;
2021
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002022 if (!disk->fops->check_events || !disk->events)
Tejun Heo77ea8872010-12-08 20:57:37 +01002023 return;
2024
2025 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
2026 if (!ev) {
2027 pr_warn("%s: failed to initialize events\n", disk->disk_name);
2028 return;
2029 }
2030
Tejun Heo77ea8872010-12-08 20:57:37 +01002031 INIT_LIST_HEAD(&ev->node);
2032 ev->disk = disk;
2033 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02002034 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01002035 ev->block = 1;
2036 ev->poll_msecs = -1;
2037 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2038
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002039 disk->ev = ev;
2040}
2041
2042static void disk_add_events(struct gendisk *disk)
2043{
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002044 /* FIXME: error handling */
2045 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2046 pr_warn("%s: failed to create sysfs files for events\n",
2047 disk->disk_name);
2048
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002049 if (!disk->ev)
2050 return;
2051
Tejun Heo77ea8872010-12-08 20:57:37 +01002052 mutex_lock(&disk_events_mutex);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002053 list_add_tail(&disk->ev->node, &disk_events);
Tejun Heo77ea8872010-12-08 20:57:37 +01002054 mutex_unlock(&disk_events_mutex);
2055
2056 /*
2057 * Block count is initialized to 1 and the following initial
2058 * unblock kicks it into action.
2059 */
2060 __disk_unblock_events(disk, true);
2061}
2062
2063static void disk_del_events(struct gendisk *disk)
2064{
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002065 if (disk->ev) {
2066 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01002067
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002068 mutex_lock(&disk_events_mutex);
2069 list_del_init(&disk->ev->node);
2070 mutex_unlock(&disk_events_mutex);
2071 }
Tejun Heo77ea8872010-12-08 20:57:37 +01002072
2073 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2074}
2075
2076static void disk_release_events(struct gendisk *disk)
2077{
2078 /* the block count should be 1 from disk_del_events() */
2079 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
2080 kfree(disk->ev);
2081}