blob: c55e8f0fced1d830eb27c39dabf54357925e7d74 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * gendisk handling
Christoph Hellwig7b51e702020-12-10 08:55:44 +01004 *
5 * Portions Copyright (C) 2020 Christoph Hellwig
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01009#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fs.h>
11#include <linux/genhd.h>
Andrew Mortonb446b602007-02-20 13:57:48 -080012#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040015#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include <linux/spinlock.h>
Alexey Dobriyanf5009752008-10-04 23:53:21 +040018#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/seq_file.h>
20#include <linux/slab.h>
21#include <linux/kmod.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080022#include <linux/mutex.h>
Tejun Heobcce3de2008-08-25 19:47:22 +090023#include <linux/idr.h>
Tejun Heo77ea8872010-12-08 20:57:37 +010024#include <linux/log2.h>
Ming Lei25e823c2013-02-22 16:34:13 -080025#include <linux/pm_runtime.h>
Vishal Verma99e66082016-01-09 08:36:51 -080026#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Adrian Bunkff889722008-03-04 11:23:45 +010028#include "blk.h"
29
Christoph Hellwig31eb6182020-03-25 16:48:35 +010030static struct kobject *block_depr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +010032DECLARE_RWSEM(bdev_lookup_sem);
Christoph Hellwig62b508f2020-10-29 15:58:25 +010033
Tejun Heobcce3de2008-08-25 19:47:22 +090034/* for extended dynamic devt allocation, currently only one major is used */
Tejun Heoce23bba2013-02-27 17:03:56 -080035#define NR_EXT_DEVT (1 << MINORBITS)
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +010036static DEFINE_IDA(ext_devt_ida);
Tejun Heobcce3de2008-08-25 19:47:22 +090037
Derek Basehore12c2bdb2012-12-18 12:27:20 -080038static void disk_check_events(struct disk_events *ev,
39 unsigned int *clearing_ptr);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +010040static void disk_alloc_events(struct gendisk *disk);
Tejun Heo77ea8872010-12-08 20:57:37 +010041static void disk_add_events(struct gendisk *disk);
42static void disk_del_events(struct gendisk *disk);
43static void disk_release_events(struct gendisk *disk);
44
Christoph Hellwiga7824832020-11-26 18:43:37 +010045void set_capacity(struct gendisk *disk, sector_t sectors)
46{
Christoph Hellwigcb8432d2020-11-26 18:47:17 +010047 struct block_device *bdev = disk->part0;
Christoph Hellwiga7824832020-11-26 18:43:37 +010048
Damien Le Moal0f472272021-03-01 12:04:02 +090049 spin_lock(&bdev->bd_size_lock);
Christoph Hellwiga7824832020-11-26 18:43:37 +010050 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
Damien Le Moal0f472272021-03-01 12:04:02 +090051 spin_unlock(&bdev->bd_size_lock);
Christoph Hellwiga7824832020-11-26 18:43:37 +010052}
53EXPORT_SYMBOL(set_capacity);
54
Balbir Singhe598a722020-03-13 05:30:05 +000055/*
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010056 * Set disk capacity and notify if the size is not currently zero and will not
57 * be set to zero. Returns true if a uevent was sent, otherwise false.
Balbir Singhe598a722020-03-13 05:30:05 +000058 */
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010059bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
Balbir Singhe598a722020-03-13 05:30:05 +000060{
61 sector_t capacity = get_capacity(disk);
Christoph Hellwiga7824832020-11-26 18:43:37 +010062 char *envp[] = { "RESIZE=1", NULL };
Balbir Singhe598a722020-03-13 05:30:05 +000063
64 set_capacity(disk, size);
Balbir Singhe598a722020-03-13 05:30:05 +000065
Christoph Hellwiga7824832020-11-26 18:43:37 +010066 /*
67 * Only print a message and send a uevent if the gendisk is user visible
68 * and alive. This avoids spamming the log and udev when setting the
69 * initial capacity during probing.
70 */
71 if (size == capacity ||
72 (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
73 return false;
Balbir Singhe598a722020-03-13 05:30:05 +000074
Christoph Hellwiga7824832020-11-26 18:43:37 +010075 pr_info("%s: detected capacity change from %lld to %lld\n",
Ming Lei452c0bf2021-02-23 16:50:15 +080076 disk->disk_name, capacity, size);
Christoph Hellwig7e890c32020-11-12 17:50:04 +010077
Christoph Hellwiga7824832020-11-26 18:43:37 +010078 /*
79 * Historically we did not send a uevent for changes to/from an empty
80 * device.
81 */
82 if (!capacity || !size)
83 return false;
84 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
85 return true;
Balbir Singhe598a722020-03-13 05:30:05 +000086}
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010087EXPORT_SYMBOL_GPL(set_capacity_and_notify);
Balbir Singhe598a722020-03-13 05:30:05 +000088
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +010089/*
90 * Format the device name of the indicated disk into the supplied buffer and
91 * return a pointer to that same buffer for convenience.
92 */
93char *disk_name(struct gendisk *hd, int partno, char *buf)
94{
95 if (!partno)
96 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
97 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
98 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
99 else
100 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
101
102 return buf;
103}
104
105const char *bdevname(struct block_device *bdev, char *buf)
106{
Christoph Hellwig8a63a862020-09-03 07:41:03 +0200107 return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +0100108}
109EXPORT_SYMBOL(bdevname);
Balbir Singhe598a722020-03-13 05:30:05 +0000110
Christoph Hellwig0d021292020-11-27 16:43:51 +0100111static void part_stat_read_all(struct block_device *part,
112 struct disk_stats *stat)
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300113{
114 int cpu;
115
116 memset(stat, 0, sizeof(struct disk_stats));
117 for_each_possible_cpu(cpu) {
Christoph Hellwig0d021292020-11-27 16:43:51 +0100118 struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300119 int group;
120
121 for (group = 0; group < NR_STAT_GROUPS; group++) {
122 stat->nsecs[group] += ptr->nsecs[group];
123 stat->sectors[group] += ptr->sectors[group];
124 stat->ios[group] += ptr->ios[group];
125 stat->merges[group] += ptr->merges[group];
126 }
127
128 stat->io_ticks += ptr->io_ticks;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300129 }
130}
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300131
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100132static unsigned int part_in_flight(struct block_device *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600133{
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200134 unsigned int inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500135 int cpu;
136
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500137 for_each_possible_cpu(cpu) {
Mikulas Patockae016b782018-12-06 11:41:21 -0500138 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
139 part_stat_local_read_cpu(part, in_flight[1], cpu);
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500140 }
Mikulas Patockae016b782018-12-06 11:41:21 -0500141 if ((int)inflight < 0)
142 inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500143
Mikulas Patockae016b782018-12-06 11:41:21 -0500144 return inflight;
Jens Axboef299b7c2017-08-08 17:51:45 -0600145}
146
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100147static void part_in_flight_rw(struct block_device *part,
148 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700149{
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500150 int cpu;
151
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500152 inflight[0] = 0;
153 inflight[1] = 0;
154 for_each_possible_cpu(cpu) {
155 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
156 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
157 }
158 if ((int)inflight[0] < 0)
159 inflight[0] = 0;
160 if ((int)inflight[1] < 0)
161 inflight[1] = 0;
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700162}
163
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200164/**
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200165 * disk_part_iter_init - initialize partition iterator
166 * @piter: iterator to initialize
167 * @disk: disk to iterate over
168 * @flags: DISK_PITER_* flags
169 *
170 * Initialize @piter so that it iterates over partitions of @disk.
171 *
172 * CONTEXT:
173 * Don't care.
174 */
175void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
176 unsigned int flags)
177{
178 piter->disk = disk;
179 piter->part = NULL;
Christoph Hellwig0470dd92021-01-24 11:02:40 +0100180 if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200181 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200182 else
183 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200184 piter->flags = flags;
185}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200186
187/**
188 * disk_part_iter_next - proceed iterator to the next partition and return it
189 * @piter: iterator of interest
190 *
191 * Proceed @piter to the next partition and return it.
192 *
193 * CONTEXT:
194 * Don't care.
195 */
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100196struct block_device *disk_part_iter_next(struct disk_part_iter *piter)
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200197{
Christoph Hellwiga33df752021-01-24 11:02:41 +0100198 struct block_device *part;
199 unsigned long idx;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200200
201 /* put the last partition */
Christoph Hellwige79319a2020-11-10 06:48:53 +0100202 disk_part_iter_exit(piter);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200203
204 rcu_read_lock();
Christoph Hellwiga33df752021-01-24 11:02:41 +0100205 xa_for_each_start(&piter->disk->part_tbl, idx, part, piter->idx) {
Ming Leiaebf5db2020-12-21 12:33:35 +0800206 if (!bdev_nr_sectors(part) &&
207 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
208 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
Christoph Hellwiga33df752021-01-24 11:02:41 +0100209 piter->idx == 0))
Ming Leiaebf5db2020-12-21 12:33:35 +0800210 continue;
Ming Leiaebf5db2020-12-21 12:33:35 +0800211
Christoph Hellwig0470dd92021-01-24 11:02:40 +0100212 piter->part = bdgrab(part);
213 if (!piter->part)
214 continue;
Christoph Hellwiga33df752021-01-24 11:02:41 +0100215 piter->idx = idx + 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200216 break;
217 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200218 rcu_read_unlock();
219
220 return piter->part;
221}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200222
223/**
224 * disk_part_iter_exit - finish up partition iteration
225 * @piter: iter of interest
226 *
227 * Called when iteration is over. Cleans up @piter.
228 *
229 * CONTEXT:
230 * Don't care.
231 */
232void disk_part_iter_exit(struct disk_part_iter *piter)
233{
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100234 if (piter->part)
235 bdput(piter->part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200236 piter->part = NULL;
237}
Shin'ichiro Kawasakib53df2e2020-02-21 10:37:08 +0900238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * Can be deleted altogether. Later.
241 *
242 */
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600243#define BLKDEV_MAJOR_HASH_SIZE 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static struct blk_major_name {
245 struct blk_major_name *next;
246 int major;
247 char name[16];
Christoph Hellwiga160c612020-10-29 15:58:28 +0100248 void (*probe)(dev_t devt);
Joe Korty68eef3b2006-03-31 02:30:32 -0800249} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100250static DEFINE_MUTEX(major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100253static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Joe Korty68eef3b2006-03-31 02:30:32 -0800255 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Joe Korty68eef3b2006-03-31 02:30:32 -0800258#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200259void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800260{
Joe Korty68eef3b2006-03-31 02:30:32 -0800261 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800262
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100263 mutex_lock(&major_names_lock);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600264 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
265 if (dp->major == offset)
Tejun Heocf771cb2008-09-03 09:01:09 +0200266 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100267 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
Joe Korty68eef3b2006-03-31 02:30:32 -0800269#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100271/**
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100272 * __register_blkdev - register a new block device
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100273 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800274 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
275 * @major = 0, try to allocate any unused major number.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100276 * @name: the name of the new block device as a zero terminated string
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100277 * @probe: allback that is called on access to any minor number of @major
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100278 *
279 * The @name must be unique within the system.
280 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300281 * The return value depends on the @major input parameter:
282 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800283 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
284 * then the function returns zero on success, or a negative error code
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300285 * - if any unused major number was requested with @major = 0 parameter
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100286 * then the return value is the allocated major number in range
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800287 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
288 *
289 * See Documentation/admin-guide/devices.txt for the list of allocated
290 * major numbers.
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100291 *
292 * Use register_blkdev instead for any new code.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100293 */
Christoph Hellwiga160c612020-10-29 15:58:28 +0100294int __register_blkdev(unsigned int major, const char *name,
295 void (*probe)(dev_t devt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct blk_major_name **n, *p;
298 int index, ret = 0;
299
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100300 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 /* temporary */
303 if (major == 0) {
304 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
305 if (major_names[index] == NULL)
306 break;
307 }
308
309 if (index == 0) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500310 printk("%s: failed to get major for %s\n",
311 __func__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ret = -EBUSY;
313 goto out;
314 }
315 major = index;
316 ret = major;
317 }
318
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600319 if (major >= BLKDEV_MAJOR_MAX) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500320 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
321 __func__, major, BLKDEV_MAJOR_MAX-1, name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600322
323 ret = -EINVAL;
324 goto out;
325 }
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
328 if (p == NULL) {
329 ret = -ENOMEM;
330 goto out;
331 }
332
333 p->major = major;
Christoph Hellwiga160c612020-10-29 15:58:28 +0100334 p->probe = probe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 strlcpy(p->name, name, sizeof(p->name));
336 p->next = NULL;
337 index = major_to_index(major);
338
339 for (n = &major_names[index]; *n; n = &(*n)->next) {
340 if ((*n)->major == major)
341 break;
342 }
343 if (!*n)
344 *n = p;
345 else
346 ret = -EBUSY;
347
348 if (ret < 0) {
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800349 printk("register_blkdev: cannot get major %u for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 major, name);
351 kfree(p);
352 }
353out:
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100354 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return ret;
356}
Christoph Hellwiga160c612020-10-29 15:58:28 +0100357EXPORT_SYMBOL(__register_blkdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Akinobu Mitaf4480242007-07-17 04:03:47 -0700359void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct blk_major_name **n;
362 struct blk_major_name *p = NULL;
363 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100365 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 for (n = &major_names[index]; *n; n = &(*n)->next)
367 if ((*n)->major == major)
368 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700369 if (!*n || strcmp((*n)->name, name)) {
370 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700371 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 p = *n;
373 *n = p->next;
374 }
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100375 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379EXPORT_SYMBOL(unregister_blkdev);
380
Tejun Heobcce3de2008-08-25 19:47:22 +0900381/**
Tejun Heo870d6652008-08-25 19:47:25 +0900382 * blk_mangle_minor - scatter minor numbers apart
383 * @minor: minor number to mangle
384 *
385 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
386 * is enabled. Mangling twice gives the original value.
387 *
388 * RETURNS:
389 * Mangled value.
390 *
391 * CONTEXT:
392 * Don't care.
393 */
394static int blk_mangle_minor(int minor)
395{
396#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
397 int i;
398
399 for (i = 0; i < MINORBITS / 2; i++) {
400 int low = minor & (1 << i);
401 int high = minor & (1 << (MINORBITS - 1 - i));
402 int distance = MINORBITS - 1 - 2 * i;
403
404 minor ^= low | high; /* clear both bits */
405 low <<= distance; /* swap the positions */
406 high >>= distance;
407 minor |= low | high; /* and set */
408 }
409#endif
410 return minor;
411}
412
413/**
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100414 * blk_alloc_devt - allocate a dev_t for a block device
415 * @bdev: block device to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900416 * @devt: out parameter for resulting dev_t
417 *
418 * Allocate a dev_t for block device.
419 *
420 * RETURNS:
421 * 0 on success, allocated dev_t is returned in *@devt. -errno on
422 * failure.
423 *
424 * CONTEXT:
425 * Might sleep.
426 */
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100427int blk_alloc_devt(struct block_device *bdev, dev_t *devt)
Tejun Heobcce3de2008-08-25 19:47:22 +0900428{
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100429 struct gendisk *disk = bdev->bd_disk;
Tejun Heobab998d2013-02-27 17:03:57 -0800430 int idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900431
432 /* in consecutive minor range? */
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100433 if (bdev->bd_partno < disk->minors) {
434 *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
Tejun Heobcce3de2008-08-25 19:47:22 +0900435 return 0;
436 }
437
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100438 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
Tejun Heobab998d2013-02-27 17:03:57 -0800439 if (idx < 0)
440 return idx == -ENOSPC ? -EBUSY : idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900441
Tejun Heo870d6652008-08-25 19:47:25 +0900442 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900443 return 0;
444}
445
446/**
447 * blk_free_devt - free a dev_t
448 * @devt: dev_t to free
449 *
450 * Free @devt which was allocated using blk_alloc_devt().
451 *
452 * CONTEXT:
453 * Might sleep.
454 */
455void blk_free_devt(dev_t devt)
456{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100457 if (MAJOR(devt) == BLOCK_EXT_MAJOR)
458 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800459}
460
Tejun Heo1f014292008-08-25 19:47:23 +0900461static char *bdevt_str(dev_t devt, char *buf)
462{
463 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
464 char tbuf[BDEVT_SIZE];
465 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
466 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
467 } else
468 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
469
470 return buf;
471}
472
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100473void disk_uevent(struct gendisk *disk, enum kobject_action action)
474{
475 struct disk_part_iter piter;
476 struct block_device *part;
477
Christoph Hellwig75ab6af2021-02-23 15:41:25 -0700478 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY_PART0);
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100479 while ((part = disk_part_iter_next(&piter)))
480 kobject_uevent(bdev_kobj(part), action);
481 disk_part_iter_exit(&piter);
482}
483EXPORT_SYMBOL_GPL(disk_uevent);
484
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200485static void disk_scan_partitions(struct gendisk *disk)
486{
487 struct block_device *bdev;
488
489 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
490 return;
491
492 set_bit(GD_NEED_PART_SCAN, &disk->state);
493 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
494 if (!IS_ERR(bdev))
495 blkdev_put(bdev, FMODE_READ);
496}
497
Hannes Reineckefef912b2018-09-28 08:17:19 +0200498static void register_disk(struct device *parent, struct gendisk *disk,
499 const struct attribute_group **groups)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100500{
501 struct device *ddev = disk_to_dev(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100502 int err;
503
Dan Williamse63a46b2016-06-15 18:17:27 -0700504 ddev->parent = parent;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100505
Kees Cookffc8b302013-07-03 15:01:14 -0700506 dev_set_name(ddev, "%s", disk->disk_name);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100507
508 /* delay uevents, until we scanned partition table */
509 dev_set_uevent_suppress(ddev, 1);
510
Hannes Reineckefef912b2018-09-28 08:17:19 +0200511 if (groups) {
512 WARN_ON(ddev->groups);
513 ddev->groups = groups;
514 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100515 if (device_add(ddev))
516 return;
517 if (!sysfs_deprecated) {
518 err = sysfs_create_link(block_depr, &ddev->kobj,
519 kobject_name(&ddev->kobj));
520 if (err) {
521 device_del(ddev);
522 return;
523 }
524 }
Ming Lei25e823c2013-02-22 16:34:13 -0800525
526 /*
527 * avoid probable deadlock caused by allocating memory with
528 * GFP_KERNEL in runtime_resume callback of its all ancestor
529 * devices
530 */
531 pm_runtime_set_memalloc_noio(ddev, true);
532
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100533 disk->part0->bd_holder_dir =
534 kobject_create_and_add("holders", &ddev->kobj);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100535 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
536
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300537 if (disk->flags & GENHD_FL_HIDDEN) {
538 dev_set_uevent_suppress(ddev, 0);
539 return;
540 }
541
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200542 disk_scan_partitions(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100543
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100544 /* announce the disk and partitions after all partitions are created */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100545 dev_set_uevent_suppress(ddev, 0);
Christoph Hellwigbc359d02021-01-24 11:02:39 +0100546 disk_uevent(disk, KOBJ_ADD);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300547
zhengbin4d7c1d32019-02-20 21:27:05 +0800548 if (disk->queue->backing_dev_info->dev) {
549 err = sysfs_create_link(&ddev->kobj,
550 &disk->queue->backing_dev_info->dev->kobj,
551 "bdi");
552 WARN_ON(err);
553 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/**
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500557 * __device_add_disk - add disk information to kernel list
Dan Williamse63a46b2016-06-15 18:17:27 -0700558 * @parent: parent device for the disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * @disk: per-device partitioning information
Hannes Reineckefef912b2018-09-28 08:17:19 +0200560 * @groups: Additional per-device sysfs groups
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500561 * @register_queue: register the queue if set to true
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 *
563 * This function registers the partitioning information in @disk
564 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900565 *
566 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500568static void __device_add_disk(struct device *parent, struct gendisk *disk,
Hannes Reineckefef912b2018-09-28 08:17:19 +0200569 const struct attribute_group **groups,
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500570 bool register_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900572 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400573 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700574
Damien Le Moal737eb782019-09-05 18:51:33 +0900575 /*
576 * The disk queue should now be all set with enough information about
577 * the device for the elevator code to pick an adequate default
578 * elevator if one is needed, that is, for devices requesting queue
579 * registration.
580 */
581 if (register_queue)
582 elevator_init_mq(disk->queue);
583
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900584 /* minors == 0 indicates to use ext devt from part0 and should
585 * be accompanied with EXT_DEVT flag. Make sure all
586 * parameters make sense.
587 */
588 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300589 WARN_ON(!disk->minors &&
590 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900593
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100594 retval = blk_alloc_devt(disk->part0, &devt);
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900595 if (retval) {
596 WARN_ON(1);
597 return;
598 }
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900599 disk->major = MAJOR(devt);
600 disk->first_minor = MINOR(devt);
601
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +0100602 disk_alloc_events(disk);
603
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300604 if (disk->flags & GENHD_FL_HIDDEN) {
605 /*
606 * Don't let hidden disks show up in /proc/partitions,
607 * and don't bother scanning for partitions either.
608 */
609 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
610 disk->flags |= GENHD_FL_NO_PART_SCAN;
611 } else {
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200612 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
613 struct device *dev = disk_to_dev(disk);
weiping zhang3a921682017-10-31 18:38:59 +0800614 int ret;
615
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300616 /* Register BDI before referencing it from bdev */
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200617 dev->devt = devt;
618 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
weiping zhang3a921682017-10-31 18:38:59 +0800619 WARN_ON(ret);
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200620 bdi_set_owner(bdi, dev);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100621 bdev_add(disk->part0, devt);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300622 }
Hannes Reineckefef912b2018-09-28 08:17:19 +0200623 register_disk(parent, disk, groups);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500624 if (register_queue)
625 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700626
Tejun Heo523e1d32011-10-19 14:31:07 +0200627 /*
628 * Take an extra ref on queue which will be put on disk_release()
629 * so that it sticks around as long as @disk is there.
630 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100631 WARN_ON_ONCE(!blk_get_queue(disk->queue));
Tejun Heo523e1d32011-10-19 14:31:07 +0200632
Tejun Heo77ea8872010-12-08 20:57:37 +0100633 disk_add_events(disk);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400634 blk_integrity_add(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500636
Hannes Reineckefef912b2018-09-28 08:17:19 +0200637void device_add_disk(struct device *parent, struct gendisk *disk,
638 const struct attribute_group **groups)
639
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500640{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200641 __device_add_disk(parent, disk, groups, true);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500642}
Dan Williamse63a46b2016-06-15 18:17:27 -0700643EXPORT_SYMBOL(device_add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500645void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
646{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200647 __device_add_disk(parent, disk, NULL, false);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500648}
649EXPORT_SYMBOL(device_add_disk_no_queue_reg);
650
Christoph Hellwig71773cf2020-11-24 09:20:37 +0100651static void invalidate_partition(struct block_device *bdev)
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200652{
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200653 fsync_bdev(bdev);
654 __invalidate_device(bdev, true);
Christoph Hellwig9bc5c392020-04-14 09:29:02 +0200655
656 /*
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100657 * Unhash the bdev inode for this device so that it can't be looked
658 * up any more even if openers still hold references to it.
Christoph Hellwig9bc5c392020-04-14 09:29:02 +0200659 */
660 remove_inode_hash(bdev->bd_inode);
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200661}
662
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000663/**
664 * del_gendisk - remove the gendisk
665 * @disk: the struct gendisk to remove
666 *
667 * Removes the gendisk and all its associated resources. This deletes the
668 * partitions associated with the gendisk, and unregisters the associated
669 * request_queue.
670 *
671 * This is the counter to the respective __device_add_disk() call.
672 *
673 * The final removal of the struct gendisk happens when its refcount reaches 0
674 * with put_disk(), which should be called after del_gendisk(), if
675 * __device_add_disk() was used.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000676 *
677 * Drivers exist which depend on the release of the gendisk to be synchronous,
678 * it should not be deferred.
679 *
680 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000681 */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100682void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Tejun Heod2bf1b62010-12-08 20:57:36 +0100684 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100685 struct block_device *part;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100686
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000687 might_sleep();
688
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100689 if (WARN_ON_ONCE(!disk->queue))
690 return;
691
Martin K. Petersen25520d52015-10-21 13:19:49 -0400692 blk_integrity_del(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +0100693 disk_del_events(disk);
694
Jan Kara56c09082018-02-26 13:01:41 +0100695 /*
696 * Block lookups of the disk until all bdevs are unhashed and the
697 * disk is marked as dead (GENHD_FL_UP cleared).
698 */
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100699 down_write(&bdev_lookup_sem);
700
Tejun Heod2bf1b62010-12-08 20:57:36 +0100701 /* invalidate stuff */
Christoph Hellwig0470dd92021-01-24 11:02:40 +0100702 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100703 while ((part = disk_part_iter_next(&piter))) {
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100704 invalidate_partition(part);
Christoph Hellwig0d021292020-11-27 16:43:51 +0100705 delete_partition(part);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100706 }
707 disk_part_iter_exit(&piter);
708
Christoph Hellwig71773cf2020-11-24 09:20:37 +0100709 invalidate_partition(disk->part0);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100710 set_capacity(disk, 0);
711 disk->flags &= ~GENHD_FL_UP;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100712 up_write(&bdev_lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100713
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100714 if (!(disk->flags & GENHD_FL_HIDDEN)) {
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300715 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100716
Jan Kara90f16fd2017-03-08 17:48:33 +0100717 /*
718 * Unregister bdi before releasing device numbers (as they can
719 * get reused and we'd get clashes in sysfs).
720 */
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100721 bdi_unregister(disk->queue->backing_dev_info);
Jan Kara90f16fd2017-03-08 17:48:33 +0100722 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100723
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100724 blk_unregister_queue(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100725
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100726 kobject_put(disk->part0->bd_holder_dir);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100727 kobject_put(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100729 part_stat_set_all(disk->part0, 0);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100730 disk->part0->bd_stamp = 0;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100731 if (!sysfs_deprecated)
732 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
Ming Lei25e823c2013-02-22 16:34:13 -0800733 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100734 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100736EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Vishal Verma99e66082016-01-09 08:36:51 -0800738/* sysfs access to bad-blocks list. */
739static ssize_t disk_badblocks_show(struct device *dev,
740 struct device_attribute *attr,
741 char *page)
742{
743 struct gendisk *disk = dev_to_disk(dev);
744
745 if (!disk->bb)
746 return sprintf(page, "\n");
747
748 return badblocks_show(disk->bb, page, 0);
749}
750
751static ssize_t disk_badblocks_store(struct device *dev,
752 struct device_attribute *attr,
753 const char *page, size_t len)
754{
755 struct gendisk *disk = dev_to_disk(dev);
756
757 if (!disk->bb)
758 return -ENXIO;
759
760 return badblocks_store(disk->bb, page, len, 0);
761}
762
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100763void blk_request_module(dev_t devt)
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100764{
Christoph Hellwiga160c612020-10-29 15:58:28 +0100765 unsigned int major = MAJOR(devt);
766 struct blk_major_name **n;
767
768 mutex_lock(&major_names_lock);
769 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
770 if ((*n)->major == major && (*n)->probe) {
771 (*n)->probe(devt);
772 mutex_unlock(&major_names_lock);
773 return;
774 }
775 }
776 mutex_unlock(&major_names_lock);
777
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100778 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
779 /* Make old-style 2.4 aliases work */
780 request_module("block-major-%d", MAJOR(devt));
781}
782
Tejun Heof331c022008-09-03 09:01:48 +0200783/**
784 * bdget_disk - do bdget() by gendisk and partition number
785 * @disk: gendisk of interest
786 * @partno: partition number
787 *
788 * Find partition @partno from @disk, do bdget() on it.
789 *
790 * CONTEXT:
791 * Don't care.
792 *
793 * RETURNS:
794 * Resulting block_device on success, NULL on failure.
795 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200796struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200797{
Tejun Heo548b10e2008-08-29 09:01:47 +0200798 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200799
Christoph Hellwig0d021292020-11-27 16:43:51 +0100800 rcu_read_lock();
Christoph Hellwiga33df752021-01-24 11:02:41 +0100801 bdev = xa_load(&disk->part_tbl, partno);
Christoph Hellwig0d021292020-11-27 16:43:51 +0100802 if (bdev && !bdgrab(bdev))
803 bdev = NULL;
804 rcu_read_unlock();
Tejun Heof331c022008-09-03 09:01:48 +0200805
Tejun Heo548b10e2008-08-29 09:01:47 +0200806 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200807}
Tejun Heof331c022008-09-03 09:01:48 +0200808
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700809/*
810 * print a full list of all partitions - intended for places where the root
811 * filesystem can't be mounted and thus to give the victim some idea of what
812 * went wrong
813 */
814void __init printk_all_partitions(void)
815{
Tejun Heodef4e382008-09-03 08:57:12 +0200816 struct class_dev_iter iter;
817 struct device *dev;
818
819 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
820 while ((dev = class_dev_iter_next(&iter))) {
821 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200822 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100823 struct block_device *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900824 char name_buf[BDEVNAME_SIZE];
825 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200826
827 /*
828 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300829 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +0200830 */
831 if (get_capacity(disk) == 0 ||
832 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
833 continue;
834
835 /*
836 * Note, unlike /proc/partitions, I am showing the
837 * numbers in hex - the same format as the root=
838 * option takes.
839 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900840 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
841 while ((part = disk_part_iter_next(&piter))) {
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100842 bool is_part0 = part == disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +0200843
Will Drewryb5af9212010-08-31 15:47:07 -0500844 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100845 bdevt_str(part->bd_dev, devt_buf),
846 bdev_nr_sectors(part) >> 1,
847 disk_name(disk, part->bd_partno, name_buf),
848 part->bd_meta_info ?
849 part->bd_meta_info->uuid : "");
Tejun Heo074a7ac2008-08-25 19:56:14 +0900850 if (is_part0) {
Dan Williams52c44d92016-06-15 19:43:07 -0700851 if (dev->parent && dev->parent->driver)
Tejun Heo074a7ac2008-08-25 19:56:14 +0900852 printk(" driver: %s\n",
Dan Williams52c44d92016-06-15 19:43:07 -0700853 dev->parent->driver->name);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900854 else
855 printk(" (driver?)\n");
856 } else
857 printk("\n");
858 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200859 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200860 }
861 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864#ifdef CONFIG_PROC_FS
865/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200866static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400867{
Tejun Heodef4e382008-09-03 08:57:12 +0200868 loff_t skip = *pos;
869 struct class_dev_iter *iter;
870 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400871
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200872 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +0200873 if (!iter)
874 return ERR_PTR(-ENOMEM);
875
876 seqf->private = iter;
877 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
878 do {
879 dev = class_dev_iter_next(iter);
880 if (!dev)
881 return NULL;
882 } while (skip--);
883
884 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400885}
886
Tejun Heodef4e382008-09-03 08:57:12 +0200887static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200889 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400890
Tejun Heodef4e382008-09-03 08:57:12 +0200891 (*pos)++;
892 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200893 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400894 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return NULL;
897}
898
Tejun Heodef4e382008-09-03 08:57:12 +0200899static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400900{
Tejun Heodef4e382008-09-03 08:57:12 +0200901 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400902
Tejun Heodef4e382008-09-03 08:57:12 +0200903 /* stop is called even after start failed :-( */
904 if (iter) {
905 class_dev_iter_exit(iter);
906 kfree(iter);
Vegard Nossum77da1602016-07-29 10:40:31 +0200907 seqf->private = NULL;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Tejun Heodef4e382008-09-03 08:57:12 +0200911static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Jianpeng Ma06768062012-08-03 10:42:00 +0200913 void *p;
Tejun Heodef4e382008-09-03 08:57:12 +0200914
915 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +0100916 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +0200917 seq_puts(seqf, "major minor #blocks name\n\n");
918 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Tejun Heocf771cb2008-09-03 09:01:09 +0200921static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200924 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100925 struct block_device *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 char buf[BDEVNAME_SIZE];
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +0200929 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200930 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
932 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
933 return 0;
934
935 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900936 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200937 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900938 seq_printf(seqf, "%4d %7d %10llu %s\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100939 MAJOR(part->bd_dev), MINOR(part->bd_dev),
940 bdev_nr_sectors(part) >> 1,
941 disk_name(sgp, part->bd_partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200942 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 return 0;
945}
946
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400947static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200948 .start = show_partition_start,
949 .next = disk_seqf_next,
950 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200951 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952};
953#endif
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955static int __init genhd_device_init(void)
956{
Dan Williamse105b8b2008-04-21 10:51:07 -0700957 int error;
958
959 block_class.dev_kobj = sysfs_dev_block_kobj;
960 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700961 if (unlikely(error))
962 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200964
Zhang, Yanmin561ec682008-11-14 08:26:30 +0100965 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
966
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200967 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +0200968 if (!sysfs_deprecated)
969 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973subsys_initcall(genhd_device_init);
974
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200975static ssize_t disk_range_show(struct device *dev,
976 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200978 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200980 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Tejun Heo1f014292008-08-25 19:47:23 +0900983static ssize_t disk_ext_range_show(struct device *dev,
984 struct device_attribute *attr, char *buf)
985{
986 struct gendisk *disk = dev_to_disk(dev);
987
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200988 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +0900989}
990
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200991static ssize_t disk_removable_show(struct device *dev,
992 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200993{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200994 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200995
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200996 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200998}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001000static ssize_t disk_hidden_show(struct device *dev,
1001 struct device_attribute *attr, char *buf)
1002{
1003 struct gendisk *disk = dev_to_disk(dev);
1004
1005 return sprintf(buf, "%d\n",
1006 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1007}
1008
Kay Sievers1c9ce522008-06-13 09:41:00 +02001009static ssize_t disk_ro_show(struct device *dev,
1010 struct device_attribute *attr, char *buf)
1011{
1012 struct gendisk *disk = dev_to_disk(dev);
1013
Tejun Heob7db9952008-08-25 19:56:10 +09001014 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +02001015}
1016
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001017ssize_t part_size_show(struct device *dev,
1018 struct device_attribute *attr, char *buf)
1019{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001020 return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001021}
1022
1023ssize_t part_stat_show(struct device *dev,
1024 struct device_attribute *attr, char *buf)
1025{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001026 struct block_device *bdev = dev_to_bdev(dev);
1027 struct request_queue *q = bdev->bd_disk->queue;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001028 struct disk_stats stat;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001029 unsigned int inflight;
1030
Christoph Hellwig0d021292020-11-27 16:43:51 +01001031 part_stat_read_all(bdev, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001032 if (queue_is_mq(q))
Christoph Hellwig0d021292020-11-27 16:43:51 +01001033 inflight = blk_mq_in_flight(q, bdev);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001034 else
Christoph Hellwig0d021292020-11-27 16:43:51 +01001035 inflight = part_in_flight(bdev);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001036
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001037 return sprintf(buf,
1038 "%8lu %8lu %8llu %8u "
1039 "%8lu %8lu %8llu %8u "
1040 "%8u %8u %8u "
1041 "%8lu %8lu %8llu %8u "
1042 "%8lu %8u"
1043 "\n",
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001044 stat.ios[STAT_READ],
1045 stat.merges[STAT_READ],
1046 (unsigned long long)stat.sectors[STAT_READ],
1047 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
1048 stat.ios[STAT_WRITE],
1049 stat.merges[STAT_WRITE],
1050 (unsigned long long)stat.sectors[STAT_WRITE],
1051 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001052 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001053 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001054 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1055 stat.nsecs[STAT_WRITE] +
1056 stat.nsecs[STAT_DISCARD] +
1057 stat.nsecs[STAT_FLUSH],
1058 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001059 stat.ios[STAT_DISCARD],
1060 stat.merges[STAT_DISCARD],
1061 (unsigned long long)stat.sectors[STAT_DISCARD],
1062 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
1063 stat.ios[STAT_FLUSH],
1064 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001065}
1066
1067ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
1068 char *buf)
1069{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001070 struct block_device *bdev = dev_to_bdev(dev);
1071 struct request_queue *q = bdev->bd_disk->queue;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001072 unsigned int inflight[2];
1073
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001074 if (queue_is_mq(q))
Christoph Hellwig0d021292020-11-27 16:43:51 +01001075 blk_mq_in_flight_rw(q, bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001076 else
Christoph Hellwig0d021292020-11-27 16:43:51 +01001077 part_in_flight_rw(bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001078
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001079 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1080}
1081
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001082static ssize_t disk_capability_show(struct device *dev,
1083 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001084{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001085 struct gendisk *disk = dev_to_disk(dev);
1086
1087 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001088}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001089
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001090static ssize_t disk_alignment_offset_show(struct device *dev,
1091 struct device_attribute *attr,
1092 char *buf)
1093{
1094 struct gendisk *disk = dev_to_disk(dev);
1095
1096 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1097}
1098
Martin K. Petersen86b37282009-11-10 11:50:21 +01001099static ssize_t disk_discard_alignment_show(struct device *dev,
1100 struct device_attribute *attr,
1101 char *buf)
1102{
1103 struct gendisk *disk = dev_to_disk(dev);
1104
Martin K. Petersendd3d1452010-01-11 03:21:48 -05001105 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +01001106}
1107
Joe Perches5657a812018-05-24 13:38:59 -06001108static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1109static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1110static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1111static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1112static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1113static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1114static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1115static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1116static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1117static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1118static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1119static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001120
Akinobu Mitac17bb492006-12-08 02:39:46 -08001121#ifdef CONFIG_FAIL_MAKE_REQUEST
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001122ssize_t part_fail_show(struct device *dev,
1123 struct device_attribute *attr, char *buf)
1124{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001125 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001126}
1127
1128ssize_t part_fail_store(struct device *dev,
1129 struct device_attribute *attr,
1130 const char *buf, size_t count)
1131{
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001132 int i;
1133
1134 if (count > 0 && sscanf(buf, "%d", &i) > 0)
Christoph Hellwig0d021292020-11-27 16:43:51 +01001135 dev_to_bdev(dev)->bd_make_it_fail = i;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001136
1137 return count;
1138}
1139
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001140static struct device_attribute dev_attr_fail =
Joe Perches5657a812018-05-24 13:38:59 -06001141 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001142#endif /* CONFIG_FAIL_MAKE_REQUEST */
1143
Jens Axboe581d4e22008-09-14 05:56:33 -07001144#ifdef CONFIG_FAIL_IO_TIMEOUT
1145static struct device_attribute dev_attr_fail_timeout =
Joe Perches5657a812018-05-24 13:38:59 -06001146 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
Jens Axboe581d4e22008-09-14 05:56:33 -07001147#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001148
1149static struct attribute *disk_attrs[] = {
1150 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +09001151 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001152 &dev_attr_removable.attr,
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001153 &dev_attr_hidden.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001154 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001155 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001156 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001157 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001158 &dev_attr_capability.attr,
1159 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001160 &dev_attr_inflight.attr,
Vishal Verma99e66082016-01-09 08:36:51 -08001161 &dev_attr_badblocks.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001162#ifdef CONFIG_FAIL_MAKE_REQUEST
1163 &dev_attr_fail.attr,
1164#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001165#ifdef CONFIG_FAIL_IO_TIMEOUT
1166 &dev_attr_fail_timeout.attr,
1167#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001168 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169};
1170
Dan Williams9438b3e02017-04-27 14:46:26 -07001171static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1172{
1173 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1174 struct gendisk *disk = dev_to_disk(dev);
1175
1176 if (a == &dev_attr_badblocks.attr && !disk->bb)
1177 return 0;
1178 return a->mode;
1179}
1180
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001181static struct attribute_group disk_attr_group = {
1182 .attrs = disk_attrs,
Dan Williams9438b3e02017-04-27 14:46:26 -07001183 .is_visible = disk_visible,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001184};
1185
David Brownella4dbd672009-06-24 10:06:31 -07001186static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001187 &disk_attr_group,
1188 NULL
1189};
1190
Tejun Heo540eed52008-08-25 19:56:15 +09001191/**
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001192 * disk_release - releases all allocated resources of the gendisk
1193 * @dev: the device representing this disk
1194 *
1195 * This function releases all allocated resources of the gendisk.
1196 *
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001197 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1198 * assigned. Since the request_queue sits on top of the gendisk for these
1199 * drivers we also call blk_put_queue() for them, and we expect the
1200 * request_queue refcount to reach 0 at this point, and so the request_queue
1201 * will also be freed prior to the disk.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001202 *
1203 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001204 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001205static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001207 struct gendisk *disk = dev_to_disk(dev);
1208
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001209 might_sleep();
1210
Keith Busch2da78092014-08-26 09:05:36 -06001211 blk_free_devt(dev->devt);
Tejun Heo77ea8872010-12-08 20:57:37 +01001212 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 kfree(disk->random);
Christoph Hellwiga33df752021-01-24 11:02:41 +01001214 xa_destroy(&disk->part_tbl);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001215 bdput(disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001216 if (disk->queue)
1217 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 kfree(disk);
1219}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001220struct class block_class = {
1221 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222};
1223
Kay Sievers3c2670e2013-04-06 09:56:00 -07001224static char *block_devnode(struct device *dev, umode_t *mode,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001225 kuid_t *uid, kgid_t *gid)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001226{
1227 struct gendisk *disk = dev_to_disk(dev);
1228
Christoph Hellwig348e1142020-03-27 09:07:17 +01001229 if (disk->fops->devnode)
1230 return disk->fops->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001231 return NULL;
1232}
1233
Boris Burkovef45fe42020-06-01 13:12:05 -07001234const struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001235 .name = "disk",
1236 .groups = disk_attr_groups,
1237 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001238 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239};
1240
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001241#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001242/*
1243 * aggregate disk stat collector. Uses the same stats that the sysfs
1244 * entries do, above, but makes them available through one seq_file.
1245 *
1246 * The output looks suspiciously like /proc/partitions with a bunch of
1247 * extra fields.
1248 */
1249static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001252 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001253 struct block_device *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 char buf[BDEVNAME_SIZE];
Mikulas Patockae016b782018-12-06 11:41:21 -05001255 unsigned int inflight;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001256 struct disk_stats stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001259 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001260 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 " rio rmerge rsect ruse wio wmerge "
1262 "wsect wuse running use aveq"
1263 "\n\n");
1264 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001265
Tejun Heo71982a42009-04-17 08:34:48 +02001266 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001267 while ((hd = disk_part_iter_next(&piter))) {
Christoph Hellwig0d021292020-11-27 16:43:51 +01001268 part_stat_read_all(hd, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001269 if (queue_is_mq(gp->queue))
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001270 inflight = blk_mq_in_flight(gp->queue, hd);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001271 else
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001272 inflight = part_in_flight(hd);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001273
Michael Callahanbdca3c82018-07-18 04:47:40 -07001274 seq_printf(seqf, "%4d %7d %s "
1275 "%lu %lu %lu %u "
1276 "%lu %lu %lu %u "
1277 "%u %u %u "
Konstantin Khlebnikovb6866312019-11-21 13:40:26 +03001278 "%lu %lu %lu %u "
1279 "%lu %u"
1280 "\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001281 MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
1282 disk_name(gp, hd->bd_partno, buf),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001283 stat.ios[STAT_READ],
1284 stat.merges[STAT_READ],
1285 stat.sectors[STAT_READ],
1286 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1287 NSEC_PER_MSEC),
1288 stat.ios[STAT_WRITE],
1289 stat.merges[STAT_WRITE],
1290 stat.sectors[STAT_WRITE],
1291 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1292 NSEC_PER_MSEC),
Mikulas Patockae016b782018-12-06 11:41:21 -05001293 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001294 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001295 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1296 stat.nsecs[STAT_WRITE] +
1297 stat.nsecs[STAT_DISCARD] +
1298 stat.nsecs[STAT_FLUSH],
1299 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001300 stat.ios[STAT_DISCARD],
1301 stat.merges[STAT_DISCARD],
1302 stat.sectors[STAT_DISCARD],
1303 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1304 NSEC_PER_MSEC),
1305 stat.ios[STAT_FLUSH],
1306 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1307 NSEC_PER_MSEC)
Jerome Marchand28f39d52008-02-08 11:04:56 +01001308 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001310 disk_part_iter_exit(&piter);
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return 0;
1313}
1314
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001315static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001316 .start = disk_seqf_start,
1317 .next = disk_seqf_next,
1318 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 .show = diskstats_show
1320};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001321
1322static int __init proc_genhd_init(void)
1323{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001324 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1325 proc_create_seq("partitions", 0, NULL, &partitions_op);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001326 return 0;
1327}
1328module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001329#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Tejun Heocf771cb2008-09-03 09:01:09 +02001331dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001332{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001333 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001334 struct class_dev_iter iter;
1335 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001336
Tejun Heodef4e382008-09-03 08:57:12 +02001337 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1338 while ((dev = class_dev_iter_next(&iter))) {
1339 struct gendisk *disk = dev_to_disk(dev);
Christoph Hellwig0d021292020-11-27 16:43:51 +01001340 struct block_device *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001341
Kay Sievers3ada8b72009-01-06 10:44:43 -08001342 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001343 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001344
Neil Brown41b8c852009-02-18 10:33:59 +01001345 if (partno < disk->minors) {
1346 /* We need to return the right devno, even
1347 * if the partition doesn't exist yet.
1348 */
1349 devt = MKDEV(MAJOR(dev->devt),
1350 MINOR(dev->devt) + partno);
1351 break;
1352 }
Christoph Hellwig0d021292020-11-27 16:43:51 +01001353 part = bdget_disk(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001354 if (part) {
Christoph Hellwig0d021292020-11-27 16:43:51 +01001355 devt = part->bd_dev;
1356 bdput(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001357 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001358 }
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001359 }
Tejun Heodef4e382008-09-03 08:57:12 +02001360 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001361 return devt;
1362}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001363
Byungchul Parke319e1f2017-10-25 17:56:05 +09001364struct gendisk *__alloc_disk_node(int minors, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001365{
1366 struct gendisk *disk;
1367
Christoph Hellwigde65b012017-08-23 19:10:29 +02001368 if (minors > DISK_MAX_PARTS) {
1369 printk(KERN_ERR
Randy Dunlap7fb52622017-11-18 17:43:38 -08001370 "block: can't allocate more than %d partitions\n",
Christoph Hellwigde65b012017-08-23 19:10:29 +02001371 DISK_MAX_PARTS);
1372 minors = DISK_MAX_PARTS;
1373 }
Christoph Lameter19460892005-06-23 00:08:19 -07001374
Joe Perchesc1b511e2013-08-29 15:21:42 -07001375 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001376 if (!disk)
1377 return NULL;
Jens Axboe6c23a962011-01-07 08:43:37 +01001378
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001379 disk->part0 = bdev_alloc(disk, 0);
1380 if (!disk->part0)
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001381 goto out_free_disk;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001382
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001383 disk->node_id = node_id;
Christoph Hellwiga33df752021-01-24 11:02:41 +01001384 xa_init(&disk->part_tbl);
1385 if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL))
1386 goto out_destroy_part_tbl;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001387
1388 disk->minors = minors;
1389 rand_initialize_disk(disk);
1390 disk_to_dev(disk)->class = &block_class;
1391 disk_to_dev(disk)->type = &disk_type;
1392 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return disk;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001394
Christoph Hellwiga33df752021-01-24 11:02:41 +01001395out_destroy_part_tbl:
1396 xa_destroy(&disk->part_tbl);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001397 bdput(disk->part0);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001398out_free_disk:
1399 kfree(disk);
1400 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
Byungchul Parke319e1f2017-10-25 17:56:05 +09001402EXPORT_SYMBOL(__alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001404/**
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001405 * put_disk - decrements the gendisk refcount
Randy Dunlap0d20dcc2020-07-30 18:42:30 -07001406 * @disk: the struct gendisk to decrement the refcount for
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001407 *
1408 * This decrements the refcount for the struct gendisk. When this reaches 0
1409 * we'll have disk_release() called.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001410 *
1411 * Context: Any context, but the last reference must not be dropped from
1412 * atomic context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414void put_disk(struct gendisk *disk)
1415{
1416 if (disk)
Christoph Hellwigefdc41c2020-11-10 07:25:37 +01001417 put_device(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419EXPORT_SYMBOL(put_disk);
1420
Hannes Reineckee3264a42009-07-28 09:13:13 +02001421static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1422{
1423 char event[] = "DISK_RO=1";
1424 char *envp[] = { event, NULL };
1425
1426 if (!ro)
1427 event[8] = '0';
1428 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1429}
1430
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001431/**
1432 * set_disk_ro - set a gendisk read-only
1433 * @disk: gendisk to operate on
Lukas Bulwahn7f31bee2021-01-29 05:55:05 +01001434 * @read_only: %true to set the disk read-only, %false set the disk read/write
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001435 *
1436 * This function is used to indicate whether a given disk device should have its
1437 * read-only flag set. set_disk_ro() is typically used by device drivers to
1438 * indicate whether the underlying physical device is write-protected.
1439 */
1440void set_disk_ro(struct gendisk *disk, bool read_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001442 if (read_only) {
1443 if (test_and_set_bit(GD_READ_ONLY, &disk->state))
1444 return;
1445 } else {
1446 if (!test_and_clear_bit(GD_READ_ONLY, &disk->state))
1447 return;
Hannes Reineckee3264a42009-07-28 09:13:13 +02001448 }
Christoph Hellwig52f019d2021-01-09 11:42:51 +01001449 set_disk_ro_uevent(disk, read_only);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451EXPORT_SYMBOL(set_disk_ro);
1452
1453int bdev_read_only(struct block_device *bdev)
1454{
Christoph Hellwig947139b2021-01-09 11:42:52 +01001455 return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457EXPORT_SYMBOL(bdev_read_only);
1458
Tejun Heo77ea8872010-12-08 20:57:37 +01001459/*
1460 * Disk events - monitor disk events like media change and eject request.
1461 */
1462struct disk_events {
1463 struct list_head node; /* all disk_event's */
1464 struct gendisk *disk; /* the associated disk */
1465 spinlock_t lock;
1466
Tejun Heofdd514e2011-06-09 20:43:59 +02001467 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001468 int block; /* event blocking depth */
1469 unsigned int pending; /* events already sent out */
1470 unsigned int clearing; /* events being cleared */
1471
1472 long poll_msecs; /* interval, -1 for default */
1473 struct delayed_work dwork;
1474};
1475
1476static const char *disk_events_strs[] = {
1477 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1478 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1479};
1480
1481static char *disk_uevents[] = {
1482 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1483 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1484};
1485
1486/* list of all disk_events */
1487static DEFINE_MUTEX(disk_events_mutex);
1488static LIST_HEAD(disk_events);
1489
1490/* disable in-kernel polling by default */
Wei Tang1fe8f342015-11-24 09:58:46 +08001491static unsigned long disk_events_dfl_poll_msecs;
Tejun Heo77ea8872010-12-08 20:57:37 +01001492
1493static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1494{
1495 struct disk_events *ev = disk->ev;
1496 long intv_msecs = 0;
1497
1498 /*
1499 * If device-specific poll interval is set, always use it. If
Martin Wilck673387a2019-03-27 14:51:01 +01001500 * the default is being used, poll if the POLL flag is set.
Tejun Heo77ea8872010-12-08 20:57:37 +01001501 */
1502 if (ev->poll_msecs >= 0)
1503 intv_msecs = ev->poll_msecs;
Martin Wilckc92e2f02019-03-27 14:51:02 +01001504 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
Tejun Heo77ea8872010-12-08 20:57:37 +01001505 intv_msecs = disk_events_dfl_poll_msecs;
1506
1507 return msecs_to_jiffies(intv_msecs);
1508}
1509
Tejun Heoc3af54a2011-06-09 20:43:55 +02001510/**
1511 * disk_block_events - block and flush disk event checking
1512 * @disk: disk to block events for
1513 *
1514 * On return from this function, it is guaranteed that event checking
1515 * isn't in progress and won't happen until unblocked by
1516 * disk_unblock_events(). Events blocking is counted and the actual
1517 * unblocking happens after the matching number of unblocks are done.
1518 *
1519 * Note that this intentionally does not block event checking from
1520 * disk_clear_events().
1521 *
1522 * CONTEXT:
1523 * Might sleep.
1524 */
1525void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001526{
1527 struct disk_events *ev = disk->ev;
1528 unsigned long flags;
1529 bool cancel;
1530
Tejun Heoc3af54a2011-06-09 20:43:55 +02001531 if (!ev)
1532 return;
1533
Tejun Heofdd514e2011-06-09 20:43:59 +02001534 /*
1535 * Outer mutex ensures that the first blocker completes canceling
1536 * the event work before further blockers are allowed to finish.
1537 */
1538 mutex_lock(&ev->block_mutex);
1539
Tejun Heo77ea8872010-12-08 20:57:37 +01001540 spin_lock_irqsave(&ev->lock, flags);
1541 cancel = !ev->block++;
1542 spin_unlock_irqrestore(&ev->lock, flags);
1543
Tejun Heoc3af54a2011-06-09 20:43:55 +02001544 if (cancel)
1545 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001546
1547 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001548}
1549
1550static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1551{
1552 struct disk_events *ev = disk->ev;
1553 unsigned long intv;
1554 unsigned long flags;
1555
1556 spin_lock_irqsave(&ev->lock, flags);
1557
1558 if (WARN_ON_ONCE(ev->block <= 0))
1559 goto out_unlock;
1560
1561 if (--ev->block)
1562 goto out_unlock;
1563
Tejun Heo77ea8872010-12-08 20:57:37 +01001564 intv = disk_events_poll_jiffies(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001565 if (check_now)
Viresh Kumar695588f2013-04-24 17:12:56 +05301566 queue_delayed_work(system_freezable_power_efficient_wq,
1567 &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001568 else if (intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301569 queue_delayed_work(system_freezable_power_efficient_wq,
1570 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001571out_unlock:
1572 spin_unlock_irqrestore(&ev->lock, flags);
1573}
1574
1575/**
Tejun Heo77ea8872010-12-08 20:57:37 +01001576 * disk_unblock_events - unblock disk event checking
1577 * @disk: disk to unblock events for
1578 *
1579 * Undo disk_block_events(). When the block count reaches zero, it
1580 * starts events polling if configured.
1581 *
1582 * CONTEXT:
1583 * Don't care. Safe to call from irq context.
1584 */
1585void disk_unblock_events(struct gendisk *disk)
1586{
1587 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01001588 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001589}
1590
1591/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02001592 * disk_flush_events - schedule immediate event checking and flushing
1593 * @disk: disk to check and flush events for
1594 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01001595 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02001596 * Schedule immediate event checking on @disk if not blocked. Events in
1597 * @mask are scheduled to be cleared from the driver. Note that this
1598 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01001599 *
1600 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02001601 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01001602 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001603void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001604{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001605 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001606
1607 if (!ev)
1608 return;
1609
Tejun Heo85ef06d2011-07-01 16:17:47 +02001610 spin_lock_irq(&ev->lock);
1611 ev->clearing |= mask;
Tejun Heo41f63c52012-08-03 10:30:47 -07001612 if (!ev->block)
Viresh Kumar695588f2013-04-24 17:12:56 +05301613 mod_delayed_work(system_freezable_power_efficient_wq,
1614 &ev->dwork, 0);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001615 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001616}
Tejun Heo77ea8872010-12-08 20:57:37 +01001617
1618/**
1619 * disk_clear_events - synchronously check, clear and return pending events
1620 * @disk: disk to fetch and clear events from
Masanari Iidada3dae52014-09-09 01:27:23 +09001621 * @mask: mask of events to be fetched and cleared
Tejun Heo77ea8872010-12-08 20:57:37 +01001622 *
1623 * Disk events are synchronously checked and pending events in @mask
1624 * are cleared and returned. This ignores the block count.
1625 *
1626 * CONTEXT:
1627 * Might sleep.
1628 */
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001629static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001630{
Tejun Heo77ea8872010-12-08 20:57:37 +01001631 struct disk_events *ev = disk->ev;
1632 unsigned int pending;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001633 unsigned int clearing = mask;
Tejun Heo77ea8872010-12-08 20:57:37 +01001634
Christoph Hellwiga564e232020-07-08 14:25:41 +02001635 if (!ev)
Tejun Heo77ea8872010-12-08 20:57:37 +01001636 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001637
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001638 disk_block_events(disk);
1639
1640 /*
1641 * store the union of mask and ev->clearing on the stack so that the
1642 * race with disk_flush_events does not cause ambiguity (ev->clearing
1643 * can still be modified even if events are blocked).
1644 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001645 spin_lock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001646 clearing |= ev->clearing;
1647 ev->clearing = 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001648 spin_unlock_irq(&ev->lock);
1649
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001650 disk_check_events(ev, &clearing);
Derek Basehoreaea24a82012-12-18 12:27:18 -08001651 /*
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001652 * if ev->clearing is not 0, the disk_flush_events got called in the
1653 * middle of this function, so we want to run the workfn without delay.
Derek Basehoreaea24a82012-12-18 12:27:18 -08001654 */
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001655 __disk_unblock_events(disk, ev->clearing ? true : false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001656
1657 /* then, fetch and clear pending events */
1658 spin_lock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001659 pending = ev->pending & mask;
1660 ev->pending &= ~mask;
1661 spin_unlock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001662 WARN_ON_ONCE(clearing & mask);
Tejun Heo77ea8872010-12-08 20:57:37 +01001663
1664 return pending;
1665}
1666
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001667/**
1668 * bdev_check_media_change - check if a removable media has been changed
1669 * @bdev: block device to check
1670 *
1671 * Check whether a removable media has been changed, and attempt to free all
1672 * dentries and inodes and invalidates all block device page cache entries in
1673 * that case.
1674 *
1675 * Returns %true if the block device changed, or %false if not.
1676 */
1677bool bdev_check_media_change(struct block_device *bdev)
1678{
1679 unsigned int events;
1680
1681 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1682 DISK_EVENT_EJECT_REQUEST);
1683 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1684 return false;
1685
1686 if (__invalidate_device(bdev, true))
1687 pr_warn("VFS: busy inodes on changed media %s\n",
1688 bdev->bd_disk->disk_name);
Christoph Hellwig38430f02020-09-21 09:19:45 +02001689 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001690 return true;
1691}
1692EXPORT_SYMBOL(bdev_check_media_change);
1693
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001694/*
1695 * Separate this part out so that a different pointer for clearing_ptr can be
1696 * passed in for disk_clear_events.
1697 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001698static void disk_events_workfn(struct work_struct *work)
1699{
1700 struct delayed_work *dwork = to_delayed_work(work);
1701 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001702
1703 disk_check_events(ev, &ev->clearing);
1704}
1705
1706static void disk_check_events(struct disk_events *ev,
1707 unsigned int *clearing_ptr)
1708{
Tejun Heo77ea8872010-12-08 20:57:37 +01001709 struct gendisk *disk = ev->disk;
1710 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001711 unsigned int clearing = *clearing_ptr;
Tejun Heo77ea8872010-12-08 20:57:37 +01001712 unsigned int events;
1713 unsigned long intv;
1714 int nr_events = 0, i;
1715
1716 /* check events */
1717 events = disk->fops->check_events(disk, clearing);
1718
1719 /* accumulate pending events and schedule next poll if necessary */
1720 spin_lock_irq(&ev->lock);
1721
1722 events &= ~ev->pending;
1723 ev->pending |= events;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001724 *clearing_ptr &= ~clearing;
Tejun Heo77ea8872010-12-08 20:57:37 +01001725
1726 intv = disk_events_poll_jiffies(disk);
1727 if (!ev->block && intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301728 queue_delayed_work(system_freezable_power_efficient_wq,
1729 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001730
1731 spin_unlock_irq(&ev->lock);
1732
Tejun Heo7c88a162011-04-21 19:43:58 +02001733 /*
1734 * Tell userland about new events. Only the events listed in
Martin Wilckc92e2f02019-03-27 14:51:02 +01001735 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1736 * is set. Otherwise, events are processed internally but never
1737 * get reported to userland.
Tejun Heo7c88a162011-04-21 19:43:58 +02001738 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001739 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Martin Wilckc92e2f02019-03-27 14:51:02 +01001740 if ((events & disk->events & (1 << i)) &&
1741 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
Tejun Heo77ea8872010-12-08 20:57:37 +01001742 envp[nr_events++] = disk_uevents[i];
1743
1744 if (nr_events)
1745 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1746}
1747
1748/*
1749 * A disk events enabled device has the following sysfs nodes under
1750 * its /sys/block/X/ directory.
1751 *
1752 * events : list of all supported events
1753 * events_async : list of events which can be detected w/o polling
Martin Wilck673387a2019-03-27 14:51:01 +01001754 * (always empty, only for backwards compatibility)
Tejun Heo77ea8872010-12-08 20:57:37 +01001755 * events_poll_msecs : polling interval, 0: disable, -1: system default
1756 */
1757static ssize_t __disk_events_show(unsigned int events, char *buf)
1758{
1759 const char *delim = "";
1760 ssize_t pos = 0;
1761 int i;
1762
1763 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1764 if (events & (1 << i)) {
1765 pos += sprintf(buf + pos, "%s%s",
1766 delim, disk_events_strs[i]);
1767 delim = " ";
1768 }
1769 if (pos)
1770 pos += sprintf(buf + pos, "\n");
1771 return pos;
1772}
1773
1774static ssize_t disk_events_show(struct device *dev,
1775 struct device_attribute *attr, char *buf)
1776{
1777 struct gendisk *disk = dev_to_disk(dev);
1778
Martin Wilckc92e2f02019-03-27 14:51:02 +01001779 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1780 return 0;
1781
Tejun Heo77ea8872010-12-08 20:57:37 +01001782 return __disk_events_show(disk->events, buf);
1783}
1784
1785static ssize_t disk_events_async_show(struct device *dev,
1786 struct device_attribute *attr, char *buf)
1787{
Martin Wilck673387a2019-03-27 14:51:01 +01001788 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001789}
1790
1791static ssize_t disk_events_poll_msecs_show(struct device *dev,
1792 struct device_attribute *attr,
1793 char *buf)
1794{
1795 struct gendisk *disk = dev_to_disk(dev);
1796
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001797 if (!disk->ev)
1798 return sprintf(buf, "-1\n");
1799
Tejun Heo77ea8872010-12-08 20:57:37 +01001800 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1801}
1802
1803static ssize_t disk_events_poll_msecs_store(struct device *dev,
1804 struct device_attribute *attr,
1805 const char *buf, size_t count)
1806{
1807 struct gendisk *disk = dev_to_disk(dev);
1808 long intv;
1809
1810 if (!count || !sscanf(buf, "%ld", &intv))
1811 return -EINVAL;
1812
1813 if (intv < 0 && intv != -1)
1814 return -EINVAL;
1815
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001816 if (!disk->ev)
1817 return -ENODEV;
1818
Tejun Heoc3af54a2011-06-09 20:43:55 +02001819 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001820 disk->ev->poll_msecs = intv;
1821 __disk_unblock_events(disk, true);
1822
1823 return count;
1824}
1825
Joe Perches5657a812018-05-24 13:38:59 -06001826static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
1827static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
1828static const DEVICE_ATTR(events_poll_msecs, 0644,
Tejun Heo77ea8872010-12-08 20:57:37 +01001829 disk_events_poll_msecs_show,
1830 disk_events_poll_msecs_store);
1831
1832static const struct attribute *disk_events_attrs[] = {
1833 &dev_attr_events.attr,
1834 &dev_attr_events_async.attr,
1835 &dev_attr_events_poll_msecs.attr,
1836 NULL,
1837};
1838
1839/*
1840 * The default polling interval can be specified by the kernel
1841 * parameter block.events_dfl_poll_msecs which defaults to 0
1842 * (disable). This can also be modified runtime by writing to
Akinobu Mita1624b0b2019-07-16 21:59:35 +09001843 * /sys/module/block/parameters/events_dfl_poll_msecs.
Tejun Heo77ea8872010-12-08 20:57:37 +01001844 */
1845static int disk_events_set_dfl_poll_msecs(const char *val,
1846 const struct kernel_param *kp)
1847{
1848 struct disk_events *ev;
1849 int ret;
1850
1851 ret = param_set_ulong(val, kp);
1852 if (ret < 0)
1853 return ret;
1854
1855 mutex_lock(&disk_events_mutex);
1856
1857 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02001858 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001859
1860 mutex_unlock(&disk_events_mutex);
1861
1862 return 0;
1863}
1864
1865static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1866 .set = disk_events_set_dfl_poll_msecs,
1867 .get = param_get_ulong,
1868};
1869
1870#undef MODULE_PARAM_PREFIX
1871#define MODULE_PARAM_PREFIX "block."
1872
1873module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1874 &disk_events_dfl_poll_msecs, 0644);
1875
1876/*
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001877 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
Tejun Heo77ea8872010-12-08 20:57:37 +01001878 */
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001879static void disk_alloc_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001880{
1881 struct disk_events *ev;
1882
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001883 if (!disk->fops->check_events || !disk->events)
Tejun Heo77ea8872010-12-08 20:57:37 +01001884 return;
1885
1886 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1887 if (!ev) {
1888 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1889 return;
1890 }
1891
Tejun Heo77ea8872010-12-08 20:57:37 +01001892 INIT_LIST_HEAD(&ev->node);
1893 ev->disk = disk;
1894 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02001895 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001896 ev->block = 1;
1897 ev->poll_msecs = -1;
1898 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
1899
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001900 disk->ev = ev;
1901}
1902
1903static void disk_add_events(struct gendisk *disk)
1904{
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001905 /* FIXME: error handling */
1906 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
1907 pr_warn("%s: failed to create sysfs files for events\n",
1908 disk->disk_name);
1909
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001910 if (!disk->ev)
1911 return;
1912
Tejun Heo77ea8872010-12-08 20:57:37 +01001913 mutex_lock(&disk_events_mutex);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001914 list_add_tail(&disk->ev->node, &disk_events);
Tejun Heo77ea8872010-12-08 20:57:37 +01001915 mutex_unlock(&disk_events_mutex);
1916
1917 /*
1918 * Block count is initialized to 1 and the following initial
1919 * unblock kicks it into action.
1920 */
1921 __disk_unblock_events(disk, true);
1922}
1923
1924static void disk_del_events(struct gendisk *disk)
1925{
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001926 if (disk->ev) {
1927 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001928
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001929 mutex_lock(&disk_events_mutex);
1930 list_del_init(&disk->ev->node);
1931 mutex_unlock(&disk_events_mutex);
1932 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001933
1934 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
1935}
1936
1937static void disk_release_events(struct gendisk *disk)
1938{
1939 /* the block count should be 1 from disk_del_events() */
1940 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
1941 kfree(disk->ev);
1942}