blob: 9e741a4f351bed030e9a4804a0f2e0ff8493dd47 [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;
Damien Le Moal0fe37722021-01-28 15:36:19 +090048 unsigned long flags;
Christoph Hellwiga7824832020-11-26 18:43:37 +010049
Damien Le Moal0fe37722021-01-28 15:36:19 +090050 spin_lock_irqsave(&bdev->bd_size_lock, flags);
Christoph Hellwiga7824832020-11-26 18:43:37 +010051 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
Damien Le Moal0fe37722021-01-28 15:36:19 +090052 spin_unlock_irqrestore(&bdev->bd_size_lock, flags);
Christoph Hellwiga7824832020-11-26 18:43:37 +010053}
54EXPORT_SYMBOL(set_capacity);
55
Balbir Singhe598a722020-03-13 05:30:05 +000056/*
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010057 * Set disk capacity and notify if the size is not currently zero and will not
58 * be set to zero. Returns true if a uevent was sent, otherwise false.
Balbir Singhe598a722020-03-13 05:30:05 +000059 */
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010060bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
Balbir Singhe598a722020-03-13 05:30:05 +000061{
62 sector_t capacity = get_capacity(disk);
Christoph Hellwiga7824832020-11-26 18:43:37 +010063 char *envp[] = { "RESIZE=1", NULL };
Balbir Singhe598a722020-03-13 05:30:05 +000064
65 set_capacity(disk, size);
Balbir Singhe598a722020-03-13 05:30:05 +000066
Christoph Hellwiga7824832020-11-26 18:43:37 +010067 /*
68 * Only print a message and send a uevent if the gendisk is user visible
69 * and alive. This avoids spamming the log and udev when setting the
70 * initial capacity during probing.
71 */
72 if (size == capacity ||
73 (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
74 return false;
Balbir Singhe598a722020-03-13 05:30:05 +000075
Christoph Hellwiga7824832020-11-26 18:43:37 +010076 pr_info("%s: detected capacity change from %lld to %lld\n",
77 disk->disk_name, size, capacity);
Christoph Hellwig7e890c32020-11-12 17:50:04 +010078
Christoph Hellwiga7824832020-11-26 18:43:37 +010079 /*
80 * Historically we did not send a uevent for changes to/from an empty
81 * device.
82 */
83 if (!capacity || !size)
84 return false;
85 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
86 return true;
Balbir Singhe598a722020-03-13 05:30:05 +000087}
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010088EXPORT_SYMBOL_GPL(set_capacity_and_notify);
Balbir Singhe598a722020-03-13 05:30:05 +000089
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +010090/*
91 * Format the device name of the indicated disk into the supplied buffer and
92 * return a pointer to that same buffer for convenience.
93 */
94char *disk_name(struct gendisk *hd, int partno, char *buf)
95{
96 if (!partno)
97 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
98 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
99 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
100 else
101 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
102
103 return buf;
104}
105
106const char *bdevname(struct block_device *bdev, char *buf)
107{
Christoph Hellwig8a63a862020-09-03 07:41:03 +0200108 return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +0100109}
110EXPORT_SYMBOL(bdevname);
Balbir Singhe598a722020-03-13 05:30:05 +0000111
Christoph Hellwig0d021292020-11-27 16:43:51 +0100112static void part_stat_read_all(struct block_device *part,
113 struct disk_stats *stat)
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300114{
115 int cpu;
116
117 memset(stat, 0, sizeof(struct disk_stats));
118 for_each_possible_cpu(cpu) {
Christoph Hellwig0d021292020-11-27 16:43:51 +0100119 struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300120 int group;
121
122 for (group = 0; group < NR_STAT_GROUPS; group++) {
123 stat->nsecs[group] += ptr->nsecs[group];
124 stat->sectors[group] += ptr->sectors[group];
125 stat->ios[group] += ptr->ios[group];
126 stat->merges[group] += ptr->merges[group];
127 }
128
129 stat->io_ticks += ptr->io_ticks;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300130 }
131}
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300132
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100133static unsigned int part_in_flight(struct block_device *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600134{
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200135 unsigned int inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500136 int cpu;
137
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500138 for_each_possible_cpu(cpu) {
Mikulas Patockae016b782018-12-06 11:41:21 -0500139 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
140 part_stat_local_read_cpu(part, in_flight[1], cpu);
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500141 }
Mikulas Patockae016b782018-12-06 11:41:21 -0500142 if ((int)inflight < 0)
143 inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500144
Mikulas Patockae016b782018-12-06 11:41:21 -0500145 return inflight;
Jens Axboef299b7c2017-08-08 17:51:45 -0600146}
147
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100148static void part_in_flight_rw(struct block_device *part,
149 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700150{
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500151 int cpu;
152
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500153 inflight[0] = 0;
154 inflight[1] = 0;
155 for_each_possible_cpu(cpu) {
156 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
157 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
158 }
159 if ((int)inflight[0] < 0)
160 inflight[0] = 0;
161 if ((int)inflight[1] < 0)
162 inflight[1] = 0;
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700163}
164
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100165struct block_device *__disk_get_part(struct gendisk *disk, int partno)
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200166{
167 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
168
169 if (unlikely(partno < 0 || partno >= ptbl->len))
170 return NULL;
171 return rcu_dereference(ptbl->part[partno]);
172}
173
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200174/**
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200175 * disk_part_iter_init - initialize partition iterator
176 * @piter: iterator to initialize
177 * @disk: disk to iterate over
178 * @flags: DISK_PITER_* flags
179 *
180 * Initialize @piter so that it iterates over partitions of @disk.
181 *
182 * CONTEXT:
183 * Don't care.
184 */
185void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
186 unsigned int flags)
187{
Tejun Heo540eed52008-08-25 19:56:15 +0900188 struct disk_part_tbl *ptbl;
189
190 rcu_read_lock();
191 ptbl = rcu_dereference(disk->part_tbl);
192
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200193 piter->disk = disk;
194 piter->part = NULL;
195
196 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +0900197 piter->idx = ptbl->len - 1;
Tejun Heo71982a42009-04-17 08:34:48 +0200198 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200199 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200200 else
201 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200202
203 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900204
205 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200206}
207EXPORT_SYMBOL_GPL(disk_part_iter_init);
208
209/**
210 * disk_part_iter_next - proceed iterator to the next partition and return it
211 * @piter: iterator of interest
212 *
213 * Proceed @piter to the next partition and return it.
214 *
215 * CONTEXT:
216 * Don't care.
217 */
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100218struct block_device *disk_part_iter_next(struct disk_part_iter *piter)
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200219{
Tejun Heo540eed52008-08-25 19:56:15 +0900220 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200221 int inc, end;
222
223 /* put the last partition */
Christoph Hellwige79319a2020-11-10 06:48:53 +0100224 disk_part_iter_exit(piter);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200225
Tejun Heo540eed52008-08-25 19:56:15 +0900226 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200227 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900228 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200229
230 /* determine iteration parameters */
231 if (piter->flags & DISK_PITER_REVERSE) {
232 inc = -1;
Tejun Heo71982a42009-04-17 08:34:48 +0200233 if (piter->flags & (DISK_PITER_INCL_PART0 |
234 DISK_PITER_INCL_EMPTY_PART0))
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200235 end = -1;
236 else
237 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200238 } else {
239 inc = 1;
Tejun Heo540eed52008-08-25 19:56:15 +0900240 end = ptbl->len;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200241 }
242
243 /* iterate to the next partition */
244 for (; piter->idx != end; piter->idx += inc) {
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100245 struct block_device *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200246
Tejun Heo540eed52008-08-25 19:56:15 +0900247 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200248 if (!part)
249 continue;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100250 piter->part = bdgrab(part);
251 if (!piter->part)
252 continue;
Ming Leiaebf5db2020-12-21 12:33:35 +0800253 if (!bdev_nr_sectors(part) &&
254 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
255 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
256 piter->idx == 0)) {
257 bdput(piter->part);
258 piter->part = NULL;
259 continue;
260 }
261
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200262 piter->idx += inc;
263 break;
264 }
265
266 rcu_read_unlock();
267
268 return piter->part;
269}
270EXPORT_SYMBOL_GPL(disk_part_iter_next);
271
272/**
273 * disk_part_iter_exit - finish up partition iteration
274 * @piter: iter of interest
275 *
276 * Called when iteration is over. Cleans up @piter.
277 *
278 * CONTEXT:
279 * Don't care.
280 */
281void disk_part_iter_exit(struct disk_part_iter *piter)
282{
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100283 if (piter->part)
284 bdput(piter->part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200285 piter->part = NULL;
286}
287EXPORT_SYMBOL_GPL(disk_part_iter_exit);
288
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100289static inline int sector_in_part(struct block_device *part, sector_t sector)
Jens Axboea6f23652008-10-24 12:52:42 +0200290{
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100291 return part->bd_start_sect <= sector &&
292 sector < part->bd_start_sect + bdev_nr_sectors(part);
Jens Axboea6f23652008-10-24 12:52:42 +0200293}
294
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200295/**
296 * disk_map_sector_rcu - map sector to partition
297 * @disk: gendisk of interest
298 * @sector: sector to map
299 *
300 * Find out which partition @sector maps to on @disk. This is
301 * primarily used for stats accounting.
302 *
303 * CONTEXT:
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100304 * RCU read locked.
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200305 *
306 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900307 * Found partition on success, part0 is returned if no partition matches
Ming Leib7d6c302020-05-08 16:17:55 +0800308 * or the matched partition is being deleted.
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200309 */
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100310struct block_device *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200311{
Tejun Heo540eed52008-08-25 19:56:15 +0900312 struct disk_part_tbl *ptbl;
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100313 struct block_device *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200314 int i;
315
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200316 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900317 ptbl = rcu_dereference(disk->part_tbl);
318
Jens Axboea6f23652008-10-24 12:52:42 +0200319 part = rcu_dereference(ptbl->last_lookup);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100320 if (part && sector_in_part(part, sector))
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200321 goto out_unlock;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200322
Jens Axboea6f23652008-10-24 12:52:42 +0200323 for (i = 1; i < ptbl->len; i++) {
324 part = rcu_dereference(ptbl->part[i]);
Jens Axboea6f23652008-10-24 12:52:42 +0200325 if (part && sector_in_part(part, sector)) {
326 rcu_assign_pointer(ptbl->last_lookup, part);
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200327 goto out_unlock;
Jens Axboea6f23652008-10-24 12:52:42 +0200328 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200329 }
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200330
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100331 part = disk->part0;
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200332out_unlock:
333 rcu_read_unlock();
334 return part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200335}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200336
Shin'ichiro Kawasakib53df2e2020-02-21 10:37:08 +0900337/**
338 * disk_has_partitions
339 * @disk: gendisk of interest
340 *
341 * Walk through the partition table and check if valid partition exists.
342 *
343 * CONTEXT:
344 * Don't care.
345 *
346 * RETURNS:
347 * True if the gendisk has at least one valid non-zero size partition.
348 * Otherwise false.
349 */
350bool disk_has_partitions(struct gendisk *disk)
351{
352 struct disk_part_tbl *ptbl;
353 int i;
354 bool ret = false;
355
356 rcu_read_lock();
357 ptbl = rcu_dereference(disk->part_tbl);
358
359 /* Iterate partitions skipping the whole device at index 0 */
360 for (i = 1; i < ptbl->len; i++) {
361 if (rcu_dereference(ptbl->part[i])) {
362 ret = true;
363 break;
364 }
365 }
366
367 rcu_read_unlock();
368
369 return ret;
370}
371EXPORT_SYMBOL_GPL(disk_has_partitions);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/*
374 * Can be deleted altogether. Later.
375 *
376 */
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600377#define BLKDEV_MAJOR_HASH_SIZE 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static struct blk_major_name {
379 struct blk_major_name *next;
380 int major;
381 char name[16];
Christoph Hellwiga160c612020-10-29 15:58:28 +0100382 void (*probe)(dev_t devt);
Joe Korty68eef3b2006-03-31 02:30:32 -0800383} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100384static DEFINE_MUTEX(major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100387static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Joe Korty68eef3b2006-03-31 02:30:32 -0800389 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Joe Korty68eef3b2006-03-31 02:30:32 -0800392#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200393void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800394{
Joe Korty68eef3b2006-03-31 02:30:32 -0800395 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800396
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100397 mutex_lock(&major_names_lock);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600398 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
399 if (dp->major == offset)
Tejun Heocf771cb2008-09-03 09:01:09 +0200400 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100401 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Joe Korty68eef3b2006-03-31 02:30:32 -0800403#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100405/**
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100406 * __register_blkdev - register a new block device
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100407 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800408 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
409 * @major = 0, try to allocate any unused major number.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100410 * @name: the name of the new block device as a zero terminated string
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100411 * @probe: allback that is called on access to any minor number of @major
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100412 *
413 * The @name must be unique within the system.
414 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300415 * The return value depends on the @major input parameter:
416 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800417 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
418 * then the function returns zero on success, or a negative error code
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300419 * - if any unused major number was requested with @major = 0 parameter
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100420 * then the return value is the allocated major number in range
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800421 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
422 *
423 * See Documentation/admin-guide/devices.txt for the list of allocated
424 * major numbers.
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100425 *
426 * Use register_blkdev instead for any new code.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100427 */
Christoph Hellwiga160c612020-10-29 15:58:28 +0100428int __register_blkdev(unsigned int major, const char *name,
429 void (*probe)(dev_t devt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct blk_major_name **n, *p;
432 int index, ret = 0;
433
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100434 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* temporary */
437 if (major == 0) {
438 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
439 if (major_names[index] == NULL)
440 break;
441 }
442
443 if (index == 0) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500444 printk("%s: failed to get major for %s\n",
445 __func__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ret = -EBUSY;
447 goto out;
448 }
449 major = index;
450 ret = major;
451 }
452
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600453 if (major >= BLKDEV_MAJOR_MAX) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500454 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
455 __func__, major, BLKDEV_MAJOR_MAX-1, name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600456
457 ret = -EINVAL;
458 goto out;
459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
462 if (p == NULL) {
463 ret = -ENOMEM;
464 goto out;
465 }
466
467 p->major = major;
Christoph Hellwiga160c612020-10-29 15:58:28 +0100468 p->probe = probe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 strlcpy(p->name, name, sizeof(p->name));
470 p->next = NULL;
471 index = major_to_index(major);
472
473 for (n = &major_names[index]; *n; n = &(*n)->next) {
474 if ((*n)->major == major)
475 break;
476 }
477 if (!*n)
478 *n = p;
479 else
480 ret = -EBUSY;
481
482 if (ret < 0) {
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800483 printk("register_blkdev: cannot get major %u for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 major, name);
485 kfree(p);
486 }
487out:
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100488 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return ret;
490}
Christoph Hellwiga160c612020-10-29 15:58:28 +0100491EXPORT_SYMBOL(__register_blkdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Akinobu Mitaf4480242007-07-17 04:03:47 -0700493void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct blk_major_name **n;
496 struct blk_major_name *p = NULL;
497 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100499 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 for (n = &major_names[index]; *n; n = &(*n)->next)
501 if ((*n)->major == major)
502 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700503 if (!*n || strcmp((*n)->name, name)) {
504 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700505 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 p = *n;
507 *n = p->next;
508 }
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100509 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513EXPORT_SYMBOL(unregister_blkdev);
514
Tejun Heobcce3de2008-08-25 19:47:22 +0900515/**
Tejun Heo870d6652008-08-25 19:47:25 +0900516 * blk_mangle_minor - scatter minor numbers apart
517 * @minor: minor number to mangle
518 *
519 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
520 * is enabled. Mangling twice gives the original value.
521 *
522 * RETURNS:
523 * Mangled value.
524 *
525 * CONTEXT:
526 * Don't care.
527 */
528static int blk_mangle_minor(int minor)
529{
530#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
531 int i;
532
533 for (i = 0; i < MINORBITS / 2; i++) {
534 int low = minor & (1 << i);
535 int high = minor & (1 << (MINORBITS - 1 - i));
536 int distance = MINORBITS - 1 - 2 * i;
537
538 minor ^= low | high; /* clear both bits */
539 low <<= distance; /* swap the positions */
540 high >>= distance;
541 minor |= low | high; /* and set */
542 }
543#endif
544 return minor;
545}
546
547/**
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100548 * blk_alloc_devt - allocate a dev_t for a block device
549 * @bdev: block device to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900550 * @devt: out parameter for resulting dev_t
551 *
552 * Allocate a dev_t for block device.
553 *
554 * RETURNS:
555 * 0 on success, allocated dev_t is returned in *@devt. -errno on
556 * failure.
557 *
558 * CONTEXT:
559 * Might sleep.
560 */
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100561int blk_alloc_devt(struct block_device *bdev, dev_t *devt)
Tejun Heobcce3de2008-08-25 19:47:22 +0900562{
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100563 struct gendisk *disk = bdev->bd_disk;
Tejun Heobab998d2013-02-27 17:03:57 -0800564 int idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900565
566 /* in consecutive minor range? */
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100567 if (bdev->bd_partno < disk->minors) {
568 *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
Tejun Heobcce3de2008-08-25 19:47:22 +0900569 return 0;
570 }
571
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100572 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
Tejun Heobab998d2013-02-27 17:03:57 -0800573 if (idx < 0)
574 return idx == -ENOSPC ? -EBUSY : idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900575
Tejun Heo870d6652008-08-25 19:47:25 +0900576 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900577 return 0;
578}
579
580/**
581 * blk_free_devt - free a dev_t
582 * @devt: dev_t to free
583 *
584 * Free @devt which was allocated using blk_alloc_devt().
585 *
586 * CONTEXT:
587 * Might sleep.
588 */
589void blk_free_devt(dev_t devt)
590{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100591 if (MAJOR(devt) == BLOCK_EXT_MAJOR)
592 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800593}
594
Tejun Heo1f014292008-08-25 19:47:23 +0900595static char *bdevt_str(dev_t devt, char *buf)
596{
597 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
598 char tbuf[BDEVT_SIZE];
599 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
600 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
601 } else
602 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
603
604 return buf;
605}
606
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200607static void disk_scan_partitions(struct gendisk *disk)
608{
609 struct block_device *bdev;
610
611 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
612 return;
613
614 set_bit(GD_NEED_PART_SCAN, &disk->state);
615 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
616 if (!IS_ERR(bdev))
617 blkdev_put(bdev, FMODE_READ);
618}
619
Hannes Reineckefef912b2018-09-28 08:17:19 +0200620static void register_disk(struct device *parent, struct gendisk *disk,
621 const struct attribute_group **groups)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100622{
623 struct device *ddev = disk_to_dev(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100624 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100625 struct block_device *part;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100626 int err;
627
Dan Williamse63a46b2016-06-15 18:17:27 -0700628 ddev->parent = parent;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100629
Kees Cookffc8b302013-07-03 15:01:14 -0700630 dev_set_name(ddev, "%s", disk->disk_name);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100631
632 /* delay uevents, until we scanned partition table */
633 dev_set_uevent_suppress(ddev, 1);
634
Hannes Reineckefef912b2018-09-28 08:17:19 +0200635 if (groups) {
636 WARN_ON(ddev->groups);
637 ddev->groups = groups;
638 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100639 if (device_add(ddev))
640 return;
641 if (!sysfs_deprecated) {
642 err = sysfs_create_link(block_depr, &ddev->kobj,
643 kobject_name(&ddev->kobj));
644 if (err) {
645 device_del(ddev);
646 return;
647 }
648 }
Ming Lei25e823c2013-02-22 16:34:13 -0800649
650 /*
651 * avoid probable deadlock caused by allocating memory with
652 * GFP_KERNEL in runtime_resume callback of its all ancestor
653 * devices
654 */
655 pm_runtime_set_memalloc_noio(ddev, true);
656
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100657 disk->part0->bd_holder_dir =
658 kobject_create_and_add("holders", &ddev->kobj);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100659 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
660
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300661 if (disk->flags & GENHD_FL_HIDDEN) {
662 dev_set_uevent_suppress(ddev, 0);
663 return;
664 }
665
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200666 disk_scan_partitions(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100667
Tejun Heod2bf1b62010-12-08 20:57:36 +0100668 /* announce disk after possible partitions are created */
669 dev_set_uevent_suppress(ddev, 0);
670 kobject_uevent(&ddev->kobj, KOBJ_ADD);
671
672 /* announce possible partitions */
673 disk_part_iter_init(&piter, disk, 0);
674 while ((part = disk_part_iter_next(&piter)))
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100675 kobject_uevent(bdev_kobj(part), KOBJ_ADD);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100676 disk_part_iter_exit(&piter);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300677
zhengbin4d7c1d32019-02-20 21:27:05 +0800678 if (disk->queue->backing_dev_info->dev) {
679 err = sysfs_create_link(&ddev->kobj,
680 &disk->queue->backing_dev_info->dev->kobj,
681 "bdi");
682 WARN_ON(err);
683 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/**
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500687 * __device_add_disk - add disk information to kernel list
Dan Williamse63a46b2016-06-15 18:17:27 -0700688 * @parent: parent device for the disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * @disk: per-device partitioning information
Hannes Reineckefef912b2018-09-28 08:17:19 +0200690 * @groups: Additional per-device sysfs groups
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500691 * @register_queue: register the queue if set to true
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 *
693 * This function registers the partitioning information in @disk
694 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900695 *
696 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500698static void __device_add_disk(struct device *parent, struct gendisk *disk,
Hannes Reineckefef912b2018-09-28 08:17:19 +0200699 const struct attribute_group **groups,
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500700 bool register_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900702 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400703 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700704
Damien Le Moal737eb782019-09-05 18:51:33 +0900705 /*
706 * The disk queue should now be all set with enough information about
707 * the device for the elevator code to pick an adequate default
708 * elevator if one is needed, that is, for devices requesting queue
709 * registration.
710 */
711 if (register_queue)
712 elevator_init_mq(disk->queue);
713
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900714 /* minors == 0 indicates to use ext devt from part0 and should
715 * be accompanied with EXT_DEVT flag. Make sure all
716 * parameters make sense.
717 */
718 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300719 WARN_ON(!disk->minors &&
720 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900723
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100724 retval = blk_alloc_devt(disk->part0, &devt);
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900725 if (retval) {
726 WARN_ON(1);
727 return;
728 }
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900729 disk->major = MAJOR(devt);
730 disk->first_minor = MINOR(devt);
731
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +0100732 disk_alloc_events(disk);
733
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300734 if (disk->flags & GENHD_FL_HIDDEN) {
735 /*
736 * Don't let hidden disks show up in /proc/partitions,
737 * and don't bother scanning for partitions either.
738 */
739 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
740 disk->flags |= GENHD_FL_NO_PART_SCAN;
741 } else {
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200742 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
743 struct device *dev = disk_to_dev(disk);
weiping zhang3a921682017-10-31 18:38:59 +0800744 int ret;
745
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300746 /* Register BDI before referencing it from bdev */
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200747 dev->devt = devt;
748 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
weiping zhang3a921682017-10-31 18:38:59 +0800749 WARN_ON(ret);
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200750 bdi_set_owner(bdi, dev);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100751 bdev_add(disk->part0, devt);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300752 }
Hannes Reineckefef912b2018-09-28 08:17:19 +0200753 register_disk(parent, disk, groups);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500754 if (register_queue)
755 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700756
Tejun Heo523e1d32011-10-19 14:31:07 +0200757 /*
758 * Take an extra ref on queue which will be put on disk_release()
759 * so that it sticks around as long as @disk is there.
760 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100761 WARN_ON_ONCE(!blk_get_queue(disk->queue));
Tejun Heo523e1d32011-10-19 14:31:07 +0200762
Tejun Heo77ea8872010-12-08 20:57:37 +0100763 disk_add_events(disk);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400764 blk_integrity_add(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500766
Hannes Reineckefef912b2018-09-28 08:17:19 +0200767void device_add_disk(struct device *parent, struct gendisk *disk,
768 const struct attribute_group **groups)
769
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500770{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200771 __device_add_disk(parent, disk, groups, true);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500772}
Dan Williamse63a46b2016-06-15 18:17:27 -0700773EXPORT_SYMBOL(device_add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500775void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
776{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200777 __device_add_disk(parent, disk, NULL, false);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500778}
779EXPORT_SYMBOL(device_add_disk_no_queue_reg);
780
Christoph Hellwig71773cf2020-11-24 09:20:37 +0100781static void invalidate_partition(struct block_device *bdev)
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200782{
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200783 fsync_bdev(bdev);
784 __invalidate_device(bdev, true);
Christoph Hellwig9bc5c392020-04-14 09:29:02 +0200785
786 /*
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100787 * Unhash the bdev inode for this device so that it can't be looked
788 * up any more even if openers still hold references to it.
Christoph Hellwig9bc5c392020-04-14 09:29:02 +0200789 */
790 remove_inode_hash(bdev->bd_inode);
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200791}
792
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000793/**
794 * del_gendisk - remove the gendisk
795 * @disk: the struct gendisk to remove
796 *
797 * Removes the gendisk and all its associated resources. This deletes the
798 * partitions associated with the gendisk, and unregisters the associated
799 * request_queue.
800 *
801 * This is the counter to the respective __device_add_disk() call.
802 *
803 * The final removal of the struct gendisk happens when its refcount reaches 0
804 * with put_disk(), which should be called after del_gendisk(), if
805 * __device_add_disk() was used.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000806 *
807 * Drivers exist which depend on the release of the gendisk to be synchronous,
808 * it should not be deferred.
809 *
810 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000811 */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100812void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Tejun Heod2bf1b62010-12-08 20:57:36 +0100814 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100815 struct block_device *part;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100816
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000817 might_sleep();
818
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100819 if (WARN_ON_ONCE(!disk->queue))
820 return;
821
Martin K. Petersen25520d52015-10-21 13:19:49 -0400822 blk_integrity_del(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +0100823 disk_del_events(disk);
824
Jan Kara56c09082018-02-26 13:01:41 +0100825 /*
826 * Block lookups of the disk until all bdevs are unhashed and the
827 * disk is marked as dead (GENHD_FL_UP cleared).
828 */
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100829 down_write(&bdev_lookup_sem);
830
Tejun Heod2bf1b62010-12-08 20:57:36 +0100831 /* invalidate stuff */
832 disk_part_iter_init(&piter, disk,
833 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
834 while ((part = disk_part_iter_next(&piter))) {
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100835 invalidate_partition(part);
Christoph Hellwig0d021292020-11-27 16:43:51 +0100836 delete_partition(part);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100837 }
838 disk_part_iter_exit(&piter);
839
Christoph Hellwig71773cf2020-11-24 09:20:37 +0100840 invalidate_partition(disk->part0);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100841 set_capacity(disk, 0);
842 disk->flags &= ~GENHD_FL_UP;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100843 up_write(&bdev_lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100844
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100845 if (!(disk->flags & GENHD_FL_HIDDEN)) {
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300846 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100847
Jan Kara90f16fd2017-03-08 17:48:33 +0100848 /*
849 * Unregister bdi before releasing device numbers (as they can
850 * get reused and we'd get clashes in sysfs).
851 */
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100852 bdi_unregister(disk->queue->backing_dev_info);
Jan Kara90f16fd2017-03-08 17:48:33 +0100853 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100854
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100855 blk_unregister_queue(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100856
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100857 kobject_put(disk->part0->bd_holder_dir);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100858 kobject_put(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100860 part_stat_set_all(disk->part0, 0);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100861 disk->part0->bd_stamp = 0;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100862 if (!sysfs_deprecated)
863 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
Ming Lei25e823c2013-02-22 16:34:13 -0800864 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100865 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100867EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Vishal Verma99e66082016-01-09 08:36:51 -0800869/* sysfs access to bad-blocks list. */
870static ssize_t disk_badblocks_show(struct device *dev,
871 struct device_attribute *attr,
872 char *page)
873{
874 struct gendisk *disk = dev_to_disk(dev);
875
876 if (!disk->bb)
877 return sprintf(page, "\n");
878
879 return badblocks_show(disk->bb, page, 0);
880}
881
882static ssize_t disk_badblocks_store(struct device *dev,
883 struct device_attribute *attr,
884 const char *page, size_t len)
885{
886 struct gendisk *disk = dev_to_disk(dev);
887
888 if (!disk->bb)
889 return -ENXIO;
890
891 return badblocks_store(disk->bb, page, len, 0);
892}
893
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100894void blk_request_module(dev_t devt)
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100895{
Christoph Hellwiga160c612020-10-29 15:58:28 +0100896 unsigned int major = MAJOR(devt);
897 struct blk_major_name **n;
898
899 mutex_lock(&major_names_lock);
900 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
901 if ((*n)->major == major && (*n)->probe) {
902 (*n)->probe(devt);
903 mutex_unlock(&major_names_lock);
904 return;
905 }
906 }
907 mutex_unlock(&major_names_lock);
908
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100909 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
910 /* Make old-style 2.4 aliases work */
911 request_module("block-major-%d", MAJOR(devt));
912}
913
Tejun Heof331c022008-09-03 09:01:48 +0200914/**
915 * bdget_disk - do bdget() by gendisk and partition number
916 * @disk: gendisk of interest
917 * @partno: partition number
918 *
919 * Find partition @partno from @disk, do bdget() on it.
920 *
921 * CONTEXT:
922 * Don't care.
923 *
924 * RETURNS:
925 * Resulting block_device on success, NULL on failure.
926 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200927struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200928{
Tejun Heo548b10e2008-08-29 09:01:47 +0200929 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200930
Christoph Hellwig0d021292020-11-27 16:43:51 +0100931 rcu_read_lock();
932 bdev = __disk_get_part(disk, partno);
933 if (bdev && !bdgrab(bdev))
934 bdev = NULL;
935 rcu_read_unlock();
Tejun Heof331c022008-09-03 09:01:48 +0200936
Tejun Heo548b10e2008-08-29 09:01:47 +0200937 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200938}
Tejun Heof331c022008-09-03 09:01:48 +0200939
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700940/*
941 * print a full list of all partitions - intended for places where the root
942 * filesystem can't be mounted and thus to give the victim some idea of what
943 * went wrong
944 */
945void __init printk_all_partitions(void)
946{
Tejun Heodef4e382008-09-03 08:57:12 +0200947 struct class_dev_iter iter;
948 struct device *dev;
949
950 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
951 while ((dev = class_dev_iter_next(&iter))) {
952 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200953 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100954 struct block_device *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900955 char name_buf[BDEVNAME_SIZE];
956 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200957
958 /*
959 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300960 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +0200961 */
962 if (get_capacity(disk) == 0 ||
963 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
964 continue;
965
966 /*
967 * Note, unlike /proc/partitions, I am showing the
968 * numbers in hex - the same format as the root=
969 * option takes.
970 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900971 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
972 while ((part = disk_part_iter_next(&piter))) {
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100973 bool is_part0 = part == disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +0200974
Will Drewryb5af9212010-08-31 15:47:07 -0500975 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100976 bdevt_str(part->bd_dev, devt_buf),
977 bdev_nr_sectors(part) >> 1,
978 disk_name(disk, part->bd_partno, name_buf),
979 part->bd_meta_info ?
980 part->bd_meta_info->uuid : "");
Tejun Heo074a7ac2008-08-25 19:56:14 +0900981 if (is_part0) {
Dan Williams52c44d92016-06-15 19:43:07 -0700982 if (dev->parent && dev->parent->driver)
Tejun Heo074a7ac2008-08-25 19:56:14 +0900983 printk(" driver: %s\n",
Dan Williams52c44d92016-06-15 19:43:07 -0700984 dev->parent->driver->name);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900985 else
986 printk(" (driver?)\n");
987 } else
988 printk("\n");
989 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200990 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200991 }
992 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995#ifdef CONFIG_PROC_FS
996/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200997static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400998{
Tejun Heodef4e382008-09-03 08:57:12 +0200999 loff_t skip = *pos;
1000 struct class_dev_iter *iter;
1001 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001002
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +02001003 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +02001004 if (!iter)
1005 return ERR_PTR(-ENOMEM);
1006
1007 seqf->private = iter;
1008 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
1009 do {
1010 dev = class_dev_iter_next(iter);
1011 if (!dev)
1012 return NULL;
1013 } while (skip--);
1014
1015 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001016}
1017
Tejun Heodef4e382008-09-03 08:57:12 +02001018static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001020 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -04001021
Tejun Heodef4e382008-09-03 08:57:12 +02001022 (*pos)++;
1023 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +02001024 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001025 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +02001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return NULL;
1028}
1029
Tejun Heodef4e382008-09-03 08:57:12 +02001030static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -04001031{
Tejun Heodef4e382008-09-03 08:57:12 +02001032 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -04001033
Tejun Heodef4e382008-09-03 08:57:12 +02001034 /* stop is called even after start failed :-( */
1035 if (iter) {
1036 class_dev_iter_exit(iter);
1037 kfree(iter);
Vegard Nossum77da1602016-07-29 10:40:31 +02001038 seqf->private = NULL;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Tejun Heodef4e382008-09-03 08:57:12 +02001042static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Jianpeng Ma06768062012-08-03 10:42:00 +02001044 void *p;
Tejun Heodef4e382008-09-03 08:57:12 +02001045
1046 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +01001047 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +02001048 seq_puts(seqf, "major minor #blocks name\n\n");
1049 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Tejun Heocf771cb2008-09-03 09:01:09 +02001052static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001055 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001056 struct block_device *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 char buf[BDEVNAME_SIZE];
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +02001060 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +02001061 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
1064 return 0;
1065
1066 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +09001067 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001068 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +09001069 seq_printf(seqf, "%4d %7d %10llu %s\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001070 MAJOR(part->bd_dev), MINOR(part->bd_dev),
1071 bdev_nr_sectors(part) >> 1,
1072 disk_name(sgp, part->bd_partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001073 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 return 0;
1076}
1077
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001078static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001079 .start = show_partition_start,
1080 .next = disk_seqf_next,
1081 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001082 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083};
1084#endif
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086static int __init genhd_device_init(void)
1087{
Dan Williamse105b8b2008-04-21 10:51:07 -07001088 int error;
1089
1090 block_class.dev_kobj = sysfs_dev_block_kobj;
1091 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -07001092 if (unlikely(error))
1093 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001095
Zhang, Yanmin561ec682008-11-14 08:26:30 +01001096 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1097
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001098 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +02001099 if (!sysfs_deprecated)
1100 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -08001101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104subsys_initcall(genhd_device_init);
1105
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001106static ssize_t disk_range_show(struct device *dev,
1107 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001109 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001111 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Tejun Heo1f014292008-08-25 19:47:23 +09001114static ssize_t disk_ext_range_show(struct device *dev,
1115 struct device_attribute *attr, char *buf)
1116{
1117 struct gendisk *disk = dev_to_disk(dev);
1118
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001119 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +09001120}
1121
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001122static ssize_t disk_removable_show(struct device *dev,
1123 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +02001124{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001125 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001126
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001127 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001131static ssize_t disk_hidden_show(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1133{
1134 struct gendisk *disk = dev_to_disk(dev);
1135
1136 return sprintf(buf, "%d\n",
1137 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1138}
1139
Kay Sievers1c9ce522008-06-13 09:41:00 +02001140static ssize_t disk_ro_show(struct device *dev,
1141 struct device_attribute *attr, char *buf)
1142{
1143 struct gendisk *disk = dev_to_disk(dev);
1144
Tejun Heob7db9952008-08-25 19:56:10 +09001145 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +02001146}
1147
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001148ssize_t part_size_show(struct device *dev,
1149 struct device_attribute *attr, char *buf)
1150{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001151 return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001152}
1153
1154ssize_t part_stat_show(struct device *dev,
1155 struct device_attribute *attr, char *buf)
1156{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001157 struct block_device *bdev = dev_to_bdev(dev);
1158 struct request_queue *q = bdev->bd_disk->queue;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001159 struct disk_stats stat;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001160 unsigned int inflight;
1161
Christoph Hellwig0d021292020-11-27 16:43:51 +01001162 part_stat_read_all(bdev, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001163 if (queue_is_mq(q))
Christoph Hellwig0d021292020-11-27 16:43:51 +01001164 inflight = blk_mq_in_flight(q, bdev);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001165 else
Christoph Hellwig0d021292020-11-27 16:43:51 +01001166 inflight = part_in_flight(bdev);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001167
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001168 return sprintf(buf,
1169 "%8lu %8lu %8llu %8u "
1170 "%8lu %8lu %8llu %8u "
1171 "%8u %8u %8u "
1172 "%8lu %8lu %8llu %8u "
1173 "%8lu %8u"
1174 "\n",
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001175 stat.ios[STAT_READ],
1176 stat.merges[STAT_READ],
1177 (unsigned long long)stat.sectors[STAT_READ],
1178 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
1179 stat.ios[STAT_WRITE],
1180 stat.merges[STAT_WRITE],
1181 (unsigned long long)stat.sectors[STAT_WRITE],
1182 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001183 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001184 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001185 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1186 stat.nsecs[STAT_WRITE] +
1187 stat.nsecs[STAT_DISCARD] +
1188 stat.nsecs[STAT_FLUSH],
1189 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001190 stat.ios[STAT_DISCARD],
1191 stat.merges[STAT_DISCARD],
1192 (unsigned long long)stat.sectors[STAT_DISCARD],
1193 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
1194 stat.ios[STAT_FLUSH],
1195 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001196}
1197
1198ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
1199 char *buf)
1200{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001201 struct block_device *bdev = dev_to_bdev(dev);
1202 struct request_queue *q = bdev->bd_disk->queue;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001203 unsigned int inflight[2];
1204
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001205 if (queue_is_mq(q))
Christoph Hellwig0d021292020-11-27 16:43:51 +01001206 blk_mq_in_flight_rw(q, bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001207 else
Christoph Hellwig0d021292020-11-27 16:43:51 +01001208 part_in_flight_rw(bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001209
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001210 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1211}
1212
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001213static ssize_t disk_capability_show(struct device *dev,
1214 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001215{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001216 struct gendisk *disk = dev_to_disk(dev);
1217
1218 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001219}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001220
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001221static ssize_t disk_alignment_offset_show(struct device *dev,
1222 struct device_attribute *attr,
1223 char *buf)
1224{
1225 struct gendisk *disk = dev_to_disk(dev);
1226
1227 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1228}
1229
Martin K. Petersen86b37282009-11-10 11:50:21 +01001230static ssize_t disk_discard_alignment_show(struct device *dev,
1231 struct device_attribute *attr,
1232 char *buf)
1233{
1234 struct gendisk *disk = dev_to_disk(dev);
1235
Martin K. Petersendd3d1452010-01-11 03:21:48 -05001236 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +01001237}
1238
Joe Perches5657a812018-05-24 13:38:59 -06001239static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1240static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1241static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1242static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1243static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1244static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1245static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1246static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1247static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1248static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1249static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1250static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001251
Akinobu Mitac17bb492006-12-08 02:39:46 -08001252#ifdef CONFIG_FAIL_MAKE_REQUEST
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001253ssize_t part_fail_show(struct device *dev,
1254 struct device_attribute *attr, char *buf)
1255{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001256 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001257}
1258
1259ssize_t part_fail_store(struct device *dev,
1260 struct device_attribute *attr,
1261 const char *buf, size_t count)
1262{
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001263 int i;
1264
1265 if (count > 0 && sscanf(buf, "%d", &i) > 0)
Christoph Hellwig0d021292020-11-27 16:43:51 +01001266 dev_to_bdev(dev)->bd_make_it_fail = i;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001267
1268 return count;
1269}
1270
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001271static struct device_attribute dev_attr_fail =
Joe Perches5657a812018-05-24 13:38:59 -06001272 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001273#endif /* CONFIG_FAIL_MAKE_REQUEST */
1274
Jens Axboe581d4e22008-09-14 05:56:33 -07001275#ifdef CONFIG_FAIL_IO_TIMEOUT
1276static struct device_attribute dev_attr_fail_timeout =
Joe Perches5657a812018-05-24 13:38:59 -06001277 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
Jens Axboe581d4e22008-09-14 05:56:33 -07001278#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001279
1280static struct attribute *disk_attrs[] = {
1281 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +09001282 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001283 &dev_attr_removable.attr,
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001284 &dev_attr_hidden.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001285 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001286 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001287 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001288 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001289 &dev_attr_capability.attr,
1290 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001291 &dev_attr_inflight.attr,
Vishal Verma99e66082016-01-09 08:36:51 -08001292 &dev_attr_badblocks.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001293#ifdef CONFIG_FAIL_MAKE_REQUEST
1294 &dev_attr_fail.attr,
1295#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001296#ifdef CONFIG_FAIL_IO_TIMEOUT
1297 &dev_attr_fail_timeout.attr,
1298#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001299 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300};
1301
Dan Williams9438b3e02017-04-27 14:46:26 -07001302static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1303{
1304 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1305 struct gendisk *disk = dev_to_disk(dev);
1306
1307 if (a == &dev_attr_badblocks.attr && !disk->bb)
1308 return 0;
1309 return a->mode;
1310}
1311
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001312static struct attribute_group disk_attr_group = {
1313 .attrs = disk_attrs,
Dan Williams9438b3e02017-04-27 14:46:26 -07001314 .is_visible = disk_visible,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001315};
1316
David Brownella4dbd672009-06-24 10:06:31 -07001317static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001318 &disk_attr_group,
1319 NULL
1320};
1321
Tejun Heo540eed52008-08-25 19:56:15 +09001322/**
1323 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1324 * @disk: disk to replace part_tbl for
1325 * @new_ptbl: new part_tbl to install
1326 *
1327 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1328 * original ptbl is freed using RCU callback.
1329 *
1330 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001331 * Matching bd_mutex locked or the caller is the only user of @disk.
Tejun Heo540eed52008-08-25 19:56:15 +09001332 */
1333static void disk_replace_part_tbl(struct gendisk *disk,
1334 struct disk_part_tbl *new_ptbl)
1335{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001336 struct disk_part_tbl *old_ptbl =
1337 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001338
1339 rcu_assign_pointer(disk->part_tbl, new_ptbl);
Jens Axboea6f23652008-10-24 12:52:42 +02001340
1341 if (old_ptbl) {
1342 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
Lai Jiangshan57bdfbf2011-03-18 11:42:58 +08001343 kfree_rcu(old_ptbl, rcu_head);
Jens Axboea6f23652008-10-24 12:52:42 +02001344 }
Tejun Heo540eed52008-08-25 19:56:15 +09001345}
1346
1347/**
1348 * disk_expand_part_tbl - expand disk->part_tbl
1349 * @disk: disk to expand part_tbl for
1350 * @partno: expand such that this partno can fit in
1351 *
1352 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1353 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1354 *
1355 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001356 * Matching bd_mutex locked or the caller is the only user of @disk.
1357 * Might sleep.
Tejun Heo540eed52008-08-25 19:56:15 +09001358 *
1359 * RETURNS:
1360 * 0 on success, -errno on failure.
1361 */
1362int disk_expand_part_tbl(struct gendisk *disk, int partno)
1363{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001364 struct disk_part_tbl *old_ptbl =
1365 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001366 struct disk_part_tbl *new_ptbl;
1367 int len = old_ptbl ? old_ptbl->len : 0;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001368 int i, target;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001369
1370 /*
1371 * check for int overflow, since we can get here from blkpg_ioctl()
1372 * with a user passed 'partno'.
1373 */
1374 target = partno + 1;
1375 if (target < 0)
1376 return -EINVAL;
Tejun Heo540eed52008-08-25 19:56:15 +09001377
1378 /* disk_max_parts() is zero during initialization, ignore if so */
1379 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1380 return -EINVAL;
1381
1382 if (target <= len)
1383 return 0;
1384
Gustavo A. R. Silva78b90a22019-05-31 13:47:54 -05001385 new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
1386 disk->node_id);
Tejun Heo540eed52008-08-25 19:56:15 +09001387 if (!new_ptbl)
1388 return -ENOMEM;
1389
Tejun Heo540eed52008-08-25 19:56:15 +09001390 new_ptbl->len = target;
1391
1392 for (i = 0; i < len; i++)
1393 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1394
1395 disk_replace_part_tbl(disk, new_ptbl);
1396 return 0;
1397}
1398
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001399/**
1400 * disk_release - releases all allocated resources of the gendisk
1401 * @dev: the device representing this disk
1402 *
1403 * This function releases all allocated resources of the gendisk.
1404 *
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001405 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1406 * assigned. Since the request_queue sits on top of the gendisk for these
1407 * drivers we also call blk_put_queue() for them, and we expect the
1408 * request_queue refcount to reach 0 at this point, and so the request_queue
1409 * will also be freed prior to the disk.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001410 *
1411 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001412 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001413static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001415 struct gendisk *disk = dev_to_disk(dev);
1416
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001417 might_sleep();
1418
Keith Busch2da78092014-08-26 09:05:36 -06001419 blk_free_devt(dev->devt);
Tejun Heo77ea8872010-12-08 20:57:37 +01001420 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +09001422 disk_replace_part_tbl(disk, NULL);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001423 bdput(disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001424 if (disk->queue)
1425 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 kfree(disk);
1427}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001428struct class block_class = {
1429 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430};
1431
Kay Sievers3c2670e2013-04-06 09:56:00 -07001432static char *block_devnode(struct device *dev, umode_t *mode,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001433 kuid_t *uid, kgid_t *gid)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001434{
1435 struct gendisk *disk = dev_to_disk(dev);
1436
Christoph Hellwig348e1142020-03-27 09:07:17 +01001437 if (disk->fops->devnode)
1438 return disk->fops->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001439 return NULL;
1440}
1441
Boris Burkovef45fe42020-06-01 13:12:05 -07001442const struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001443 .name = "disk",
1444 .groups = disk_attr_groups,
1445 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001446 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447};
1448
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001449#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001450/*
1451 * aggregate disk stat collector. Uses the same stats that the sysfs
1452 * entries do, above, but makes them available through one seq_file.
1453 *
1454 * The output looks suspiciously like /proc/partitions with a bunch of
1455 * extra fields.
1456 */
1457static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001460 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001461 struct block_device *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 char buf[BDEVNAME_SIZE];
Mikulas Patockae016b782018-12-06 11:41:21 -05001463 unsigned int inflight;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001464 struct disk_stats stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001467 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001468 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 " rio rmerge rsect ruse wio wmerge "
1470 "wsect wuse running use aveq"
1471 "\n\n");
1472 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001473
Tejun Heo71982a42009-04-17 08:34:48 +02001474 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001475 while ((hd = disk_part_iter_next(&piter))) {
Christoph Hellwig0d021292020-11-27 16:43:51 +01001476 part_stat_read_all(hd, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001477 if (queue_is_mq(gp->queue))
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001478 inflight = blk_mq_in_flight(gp->queue, hd);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001479 else
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001480 inflight = part_in_flight(hd);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001481
Michael Callahanbdca3c82018-07-18 04:47:40 -07001482 seq_printf(seqf, "%4d %7d %s "
1483 "%lu %lu %lu %u "
1484 "%lu %lu %lu %u "
1485 "%u %u %u "
Konstantin Khlebnikovb6866312019-11-21 13:40:26 +03001486 "%lu %lu %lu %u "
1487 "%lu %u"
1488 "\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001489 MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
1490 disk_name(gp, hd->bd_partno, buf),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001491 stat.ios[STAT_READ],
1492 stat.merges[STAT_READ],
1493 stat.sectors[STAT_READ],
1494 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1495 NSEC_PER_MSEC),
1496 stat.ios[STAT_WRITE],
1497 stat.merges[STAT_WRITE],
1498 stat.sectors[STAT_WRITE],
1499 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1500 NSEC_PER_MSEC),
Mikulas Patockae016b782018-12-06 11:41:21 -05001501 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001502 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001503 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1504 stat.nsecs[STAT_WRITE] +
1505 stat.nsecs[STAT_DISCARD] +
1506 stat.nsecs[STAT_FLUSH],
1507 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001508 stat.ios[STAT_DISCARD],
1509 stat.merges[STAT_DISCARD],
1510 stat.sectors[STAT_DISCARD],
1511 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1512 NSEC_PER_MSEC),
1513 stat.ios[STAT_FLUSH],
1514 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1515 NSEC_PER_MSEC)
Jerome Marchand28f39d52008-02-08 11:04:56 +01001516 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001518 disk_part_iter_exit(&piter);
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return 0;
1521}
1522
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001523static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001524 .start = disk_seqf_start,
1525 .next = disk_seqf_next,
1526 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 .show = diskstats_show
1528};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001529
1530static int __init proc_genhd_init(void)
1531{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001532 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1533 proc_create_seq("partitions", 0, NULL, &partitions_op);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001534 return 0;
1535}
1536module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001537#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Tejun Heocf771cb2008-09-03 09:01:09 +02001539dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001540{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001541 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001542 struct class_dev_iter iter;
1543 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001544
Tejun Heodef4e382008-09-03 08:57:12 +02001545 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1546 while ((dev = class_dev_iter_next(&iter))) {
1547 struct gendisk *disk = dev_to_disk(dev);
Christoph Hellwig0d021292020-11-27 16:43:51 +01001548 struct block_device *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001549
Kay Sievers3ada8b72009-01-06 10:44:43 -08001550 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001551 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001552
Neil Brown41b8c852009-02-18 10:33:59 +01001553 if (partno < disk->minors) {
1554 /* We need to return the right devno, even
1555 * if the partition doesn't exist yet.
1556 */
1557 devt = MKDEV(MAJOR(dev->devt),
1558 MINOR(dev->devt) + partno);
1559 break;
1560 }
Christoph Hellwig0d021292020-11-27 16:43:51 +01001561 part = bdget_disk(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001562 if (part) {
Christoph Hellwig0d021292020-11-27 16:43:51 +01001563 devt = part->bd_dev;
1564 bdput(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001565 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001566 }
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001567 }
Tejun Heodef4e382008-09-03 08:57:12 +02001568 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001569 return devt;
1570}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001571
Byungchul Parke319e1f2017-10-25 17:56:05 +09001572struct gendisk *__alloc_disk_node(int minors, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001573{
1574 struct gendisk *disk;
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001575 struct disk_part_tbl *ptbl;
Christoph Lameter19460892005-06-23 00:08:19 -07001576
Christoph Hellwigde65b012017-08-23 19:10:29 +02001577 if (minors > DISK_MAX_PARTS) {
1578 printk(KERN_ERR
Randy Dunlap7fb52622017-11-18 17:43:38 -08001579 "block: can't allocate more than %d partitions\n",
Christoph Hellwigde65b012017-08-23 19:10:29 +02001580 DISK_MAX_PARTS);
1581 minors = DISK_MAX_PARTS;
1582 }
Christoph Lameter19460892005-06-23 00:08:19 -07001583
Joe Perchesc1b511e2013-08-29 15:21:42 -07001584 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001585 if (!disk)
1586 return NULL;
Jens Axboe6c23a962011-01-07 08:43:37 +01001587
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001588 disk->part0 = bdev_alloc(disk, 0);
1589 if (!disk->part0)
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001590 goto out_free_disk;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001591
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001592 disk->node_id = node_id;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001593 if (disk_expand_part_tbl(disk, 0))
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +01001594 goto out_bdput;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001595
1596 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001597 rcu_assign_pointer(ptbl->part[0], disk->part0);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001598
1599 disk->minors = minors;
1600 rand_initialize_disk(disk);
1601 disk_to_dev(disk)->class = &block_class;
1602 disk_to_dev(disk)->type = &disk_type;
1603 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return disk;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001605
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001606out_bdput:
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001607 bdput(disk->part0);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001608out_free_disk:
1609 kfree(disk);
1610 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
Byungchul Parke319e1f2017-10-25 17:56:05 +09001612EXPORT_SYMBOL(__alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001614/**
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001615 * put_disk - decrements the gendisk refcount
Randy Dunlap0d20dcc2020-07-30 18:42:30 -07001616 * @disk: the struct gendisk to decrement the refcount for
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001617 *
1618 * This decrements the refcount for the struct gendisk. When this reaches 0
1619 * we'll have disk_release() called.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001620 *
1621 * Context: Any context, but the last reference must not be dropped from
1622 * atomic context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624void put_disk(struct gendisk *disk)
1625{
1626 if (disk)
Christoph Hellwigefdc41c2020-11-10 07:25:37 +01001627 put_device(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629EXPORT_SYMBOL(put_disk);
1630
Hannes Reineckee3264a42009-07-28 09:13:13 +02001631static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1632{
1633 char event[] = "DISK_RO=1";
1634 char *envp[] = { event, NULL };
1635
1636 if (!ro)
1637 event[8] = '0';
1638 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1639}
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641void set_disk_ro(struct gendisk *disk, int flag)
1642{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001643 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001644 struct block_device *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001645
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001646 if (disk->part0->bd_read_only != flag) {
Hannes Reineckee3264a42009-07-28 09:13:13 +02001647 set_disk_ro_uevent(disk, flag);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001648 disk->part0->bd_read_only = flag;
Hannes Reineckee3264a42009-07-28 09:13:13 +02001649 }
1650
1651 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001652 while ((part = disk_part_iter_next(&piter)))
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001653 part->bd_read_only = flag;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001654 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
1657EXPORT_SYMBOL(set_disk_ro);
1658
1659int bdev_read_only(struct block_device *bdev)
1660{
1661 if (!bdev)
1662 return 0;
Christoph Hellwig83950d32020-11-23 16:36:02 +01001663 return bdev->bd_read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
1666EXPORT_SYMBOL(bdev_read_only);
1667
Tejun Heo77ea8872010-12-08 20:57:37 +01001668/*
1669 * Disk events - monitor disk events like media change and eject request.
1670 */
1671struct disk_events {
1672 struct list_head node; /* all disk_event's */
1673 struct gendisk *disk; /* the associated disk */
1674 spinlock_t lock;
1675
Tejun Heofdd514e2011-06-09 20:43:59 +02001676 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001677 int block; /* event blocking depth */
1678 unsigned int pending; /* events already sent out */
1679 unsigned int clearing; /* events being cleared */
1680
1681 long poll_msecs; /* interval, -1 for default */
1682 struct delayed_work dwork;
1683};
1684
1685static const char *disk_events_strs[] = {
1686 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1687 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1688};
1689
1690static char *disk_uevents[] = {
1691 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1692 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1693};
1694
1695/* list of all disk_events */
1696static DEFINE_MUTEX(disk_events_mutex);
1697static LIST_HEAD(disk_events);
1698
1699/* disable in-kernel polling by default */
Wei Tang1fe8f342015-11-24 09:58:46 +08001700static unsigned long disk_events_dfl_poll_msecs;
Tejun Heo77ea8872010-12-08 20:57:37 +01001701
1702static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1703{
1704 struct disk_events *ev = disk->ev;
1705 long intv_msecs = 0;
1706
1707 /*
1708 * If device-specific poll interval is set, always use it. If
Martin Wilck673387a2019-03-27 14:51:01 +01001709 * the default is being used, poll if the POLL flag is set.
Tejun Heo77ea8872010-12-08 20:57:37 +01001710 */
1711 if (ev->poll_msecs >= 0)
1712 intv_msecs = ev->poll_msecs;
Martin Wilckc92e2f02019-03-27 14:51:02 +01001713 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
Tejun Heo77ea8872010-12-08 20:57:37 +01001714 intv_msecs = disk_events_dfl_poll_msecs;
1715
1716 return msecs_to_jiffies(intv_msecs);
1717}
1718
Tejun Heoc3af54a2011-06-09 20:43:55 +02001719/**
1720 * disk_block_events - block and flush disk event checking
1721 * @disk: disk to block events for
1722 *
1723 * On return from this function, it is guaranteed that event checking
1724 * isn't in progress and won't happen until unblocked by
1725 * disk_unblock_events(). Events blocking is counted and the actual
1726 * unblocking happens after the matching number of unblocks are done.
1727 *
1728 * Note that this intentionally does not block event checking from
1729 * disk_clear_events().
1730 *
1731 * CONTEXT:
1732 * Might sleep.
1733 */
1734void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001735{
1736 struct disk_events *ev = disk->ev;
1737 unsigned long flags;
1738 bool cancel;
1739
Tejun Heoc3af54a2011-06-09 20:43:55 +02001740 if (!ev)
1741 return;
1742
Tejun Heofdd514e2011-06-09 20:43:59 +02001743 /*
1744 * Outer mutex ensures that the first blocker completes canceling
1745 * the event work before further blockers are allowed to finish.
1746 */
1747 mutex_lock(&ev->block_mutex);
1748
Tejun Heo77ea8872010-12-08 20:57:37 +01001749 spin_lock_irqsave(&ev->lock, flags);
1750 cancel = !ev->block++;
1751 spin_unlock_irqrestore(&ev->lock, flags);
1752
Tejun Heoc3af54a2011-06-09 20:43:55 +02001753 if (cancel)
1754 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001755
1756 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001757}
1758
1759static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1760{
1761 struct disk_events *ev = disk->ev;
1762 unsigned long intv;
1763 unsigned long flags;
1764
1765 spin_lock_irqsave(&ev->lock, flags);
1766
1767 if (WARN_ON_ONCE(ev->block <= 0))
1768 goto out_unlock;
1769
1770 if (--ev->block)
1771 goto out_unlock;
1772
Tejun Heo77ea8872010-12-08 20:57:37 +01001773 intv = disk_events_poll_jiffies(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001774 if (check_now)
Viresh Kumar695588f2013-04-24 17:12:56 +05301775 queue_delayed_work(system_freezable_power_efficient_wq,
1776 &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001777 else if (intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301778 queue_delayed_work(system_freezable_power_efficient_wq,
1779 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001780out_unlock:
1781 spin_unlock_irqrestore(&ev->lock, flags);
1782}
1783
1784/**
Tejun Heo77ea8872010-12-08 20:57:37 +01001785 * disk_unblock_events - unblock disk event checking
1786 * @disk: disk to unblock events for
1787 *
1788 * Undo disk_block_events(). When the block count reaches zero, it
1789 * starts events polling if configured.
1790 *
1791 * CONTEXT:
1792 * Don't care. Safe to call from irq context.
1793 */
1794void disk_unblock_events(struct gendisk *disk)
1795{
1796 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01001797 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001798}
1799
1800/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02001801 * disk_flush_events - schedule immediate event checking and flushing
1802 * @disk: disk to check and flush events for
1803 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01001804 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02001805 * Schedule immediate event checking on @disk if not blocked. Events in
1806 * @mask are scheduled to be cleared from the driver. Note that this
1807 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01001808 *
1809 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02001810 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01001811 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001812void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001813{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001814 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001815
1816 if (!ev)
1817 return;
1818
Tejun Heo85ef06d2011-07-01 16:17:47 +02001819 spin_lock_irq(&ev->lock);
1820 ev->clearing |= mask;
Tejun Heo41f63c52012-08-03 10:30:47 -07001821 if (!ev->block)
Viresh Kumar695588f2013-04-24 17:12:56 +05301822 mod_delayed_work(system_freezable_power_efficient_wq,
1823 &ev->dwork, 0);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001824 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001825}
Tejun Heo77ea8872010-12-08 20:57:37 +01001826
1827/**
1828 * disk_clear_events - synchronously check, clear and return pending events
1829 * @disk: disk to fetch and clear events from
Masanari Iidada3dae52014-09-09 01:27:23 +09001830 * @mask: mask of events to be fetched and cleared
Tejun Heo77ea8872010-12-08 20:57:37 +01001831 *
1832 * Disk events are synchronously checked and pending events in @mask
1833 * are cleared and returned. This ignores the block count.
1834 *
1835 * CONTEXT:
1836 * Might sleep.
1837 */
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001838static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001839{
Tejun Heo77ea8872010-12-08 20:57:37 +01001840 struct disk_events *ev = disk->ev;
1841 unsigned int pending;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001842 unsigned int clearing = mask;
Tejun Heo77ea8872010-12-08 20:57:37 +01001843
Christoph Hellwiga564e232020-07-08 14:25:41 +02001844 if (!ev)
Tejun Heo77ea8872010-12-08 20:57:37 +01001845 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001846
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001847 disk_block_events(disk);
1848
1849 /*
1850 * store the union of mask and ev->clearing on the stack so that the
1851 * race with disk_flush_events does not cause ambiguity (ev->clearing
1852 * can still be modified even if events are blocked).
1853 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001854 spin_lock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001855 clearing |= ev->clearing;
1856 ev->clearing = 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001857 spin_unlock_irq(&ev->lock);
1858
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001859 disk_check_events(ev, &clearing);
Derek Basehoreaea24a82012-12-18 12:27:18 -08001860 /*
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001861 * if ev->clearing is not 0, the disk_flush_events got called in the
1862 * middle of this function, so we want to run the workfn without delay.
Derek Basehoreaea24a82012-12-18 12:27:18 -08001863 */
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001864 __disk_unblock_events(disk, ev->clearing ? true : false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001865
1866 /* then, fetch and clear pending events */
1867 spin_lock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001868 pending = ev->pending & mask;
1869 ev->pending &= ~mask;
1870 spin_unlock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001871 WARN_ON_ONCE(clearing & mask);
Tejun Heo77ea8872010-12-08 20:57:37 +01001872
1873 return pending;
1874}
1875
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001876/**
1877 * bdev_check_media_change - check if a removable media has been changed
1878 * @bdev: block device to check
1879 *
1880 * Check whether a removable media has been changed, and attempt to free all
1881 * dentries and inodes and invalidates all block device page cache entries in
1882 * that case.
1883 *
1884 * Returns %true if the block device changed, or %false if not.
1885 */
1886bool bdev_check_media_change(struct block_device *bdev)
1887{
1888 unsigned int events;
1889
1890 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1891 DISK_EVENT_EJECT_REQUEST);
1892 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1893 return false;
1894
1895 if (__invalidate_device(bdev, true))
1896 pr_warn("VFS: busy inodes on changed media %s\n",
1897 bdev->bd_disk->disk_name);
Christoph Hellwig38430f02020-09-21 09:19:45 +02001898 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001899 return true;
1900}
1901EXPORT_SYMBOL(bdev_check_media_change);
1902
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001903/*
1904 * Separate this part out so that a different pointer for clearing_ptr can be
1905 * passed in for disk_clear_events.
1906 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001907static void disk_events_workfn(struct work_struct *work)
1908{
1909 struct delayed_work *dwork = to_delayed_work(work);
1910 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001911
1912 disk_check_events(ev, &ev->clearing);
1913}
1914
1915static void disk_check_events(struct disk_events *ev,
1916 unsigned int *clearing_ptr)
1917{
Tejun Heo77ea8872010-12-08 20:57:37 +01001918 struct gendisk *disk = ev->disk;
1919 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001920 unsigned int clearing = *clearing_ptr;
Tejun Heo77ea8872010-12-08 20:57:37 +01001921 unsigned int events;
1922 unsigned long intv;
1923 int nr_events = 0, i;
1924
1925 /* check events */
1926 events = disk->fops->check_events(disk, clearing);
1927
1928 /* accumulate pending events and schedule next poll if necessary */
1929 spin_lock_irq(&ev->lock);
1930
1931 events &= ~ev->pending;
1932 ev->pending |= events;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001933 *clearing_ptr &= ~clearing;
Tejun Heo77ea8872010-12-08 20:57:37 +01001934
1935 intv = disk_events_poll_jiffies(disk);
1936 if (!ev->block && intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301937 queue_delayed_work(system_freezable_power_efficient_wq,
1938 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001939
1940 spin_unlock_irq(&ev->lock);
1941
Tejun Heo7c88a162011-04-21 19:43:58 +02001942 /*
1943 * Tell userland about new events. Only the events listed in
Martin Wilckc92e2f02019-03-27 14:51:02 +01001944 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1945 * is set. Otherwise, events are processed internally but never
1946 * get reported to userland.
Tejun Heo7c88a162011-04-21 19:43:58 +02001947 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001948 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Martin Wilckc92e2f02019-03-27 14:51:02 +01001949 if ((events & disk->events & (1 << i)) &&
1950 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
Tejun Heo77ea8872010-12-08 20:57:37 +01001951 envp[nr_events++] = disk_uevents[i];
1952
1953 if (nr_events)
1954 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1955}
1956
1957/*
1958 * A disk events enabled device has the following sysfs nodes under
1959 * its /sys/block/X/ directory.
1960 *
1961 * events : list of all supported events
1962 * events_async : list of events which can be detected w/o polling
Martin Wilck673387a2019-03-27 14:51:01 +01001963 * (always empty, only for backwards compatibility)
Tejun Heo77ea8872010-12-08 20:57:37 +01001964 * events_poll_msecs : polling interval, 0: disable, -1: system default
1965 */
1966static ssize_t __disk_events_show(unsigned int events, char *buf)
1967{
1968 const char *delim = "";
1969 ssize_t pos = 0;
1970 int i;
1971
1972 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1973 if (events & (1 << i)) {
1974 pos += sprintf(buf + pos, "%s%s",
1975 delim, disk_events_strs[i]);
1976 delim = " ";
1977 }
1978 if (pos)
1979 pos += sprintf(buf + pos, "\n");
1980 return pos;
1981}
1982
1983static ssize_t disk_events_show(struct device *dev,
1984 struct device_attribute *attr, char *buf)
1985{
1986 struct gendisk *disk = dev_to_disk(dev);
1987
Martin Wilckc92e2f02019-03-27 14:51:02 +01001988 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1989 return 0;
1990
Tejun Heo77ea8872010-12-08 20:57:37 +01001991 return __disk_events_show(disk->events, buf);
1992}
1993
1994static ssize_t disk_events_async_show(struct device *dev,
1995 struct device_attribute *attr, char *buf)
1996{
Martin Wilck673387a2019-03-27 14:51:01 +01001997 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001998}
1999
2000static ssize_t disk_events_poll_msecs_show(struct device *dev,
2001 struct device_attribute *attr,
2002 char *buf)
2003{
2004 struct gendisk *disk = dev_to_disk(dev);
2005
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002006 if (!disk->ev)
2007 return sprintf(buf, "-1\n");
2008
Tejun Heo77ea8872010-12-08 20:57:37 +01002009 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
2010}
2011
2012static ssize_t disk_events_poll_msecs_store(struct device *dev,
2013 struct device_attribute *attr,
2014 const char *buf, size_t count)
2015{
2016 struct gendisk *disk = dev_to_disk(dev);
2017 long intv;
2018
2019 if (!count || !sscanf(buf, "%ld", &intv))
2020 return -EINVAL;
2021
2022 if (intv < 0 && intv != -1)
2023 return -EINVAL;
2024
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002025 if (!disk->ev)
2026 return -ENODEV;
2027
Tejun Heoc3af54a2011-06-09 20:43:55 +02002028 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01002029 disk->ev->poll_msecs = intv;
2030 __disk_unblock_events(disk, true);
2031
2032 return count;
2033}
2034
Joe Perches5657a812018-05-24 13:38:59 -06002035static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
2036static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
2037static const DEVICE_ATTR(events_poll_msecs, 0644,
Tejun Heo77ea8872010-12-08 20:57:37 +01002038 disk_events_poll_msecs_show,
2039 disk_events_poll_msecs_store);
2040
2041static const struct attribute *disk_events_attrs[] = {
2042 &dev_attr_events.attr,
2043 &dev_attr_events_async.attr,
2044 &dev_attr_events_poll_msecs.attr,
2045 NULL,
2046};
2047
2048/*
2049 * The default polling interval can be specified by the kernel
2050 * parameter block.events_dfl_poll_msecs which defaults to 0
2051 * (disable). This can also be modified runtime by writing to
Akinobu Mita1624b0b2019-07-16 21:59:35 +09002052 * /sys/module/block/parameters/events_dfl_poll_msecs.
Tejun Heo77ea8872010-12-08 20:57:37 +01002053 */
2054static int disk_events_set_dfl_poll_msecs(const char *val,
2055 const struct kernel_param *kp)
2056{
2057 struct disk_events *ev;
2058 int ret;
2059
2060 ret = param_set_ulong(val, kp);
2061 if (ret < 0)
2062 return ret;
2063
2064 mutex_lock(&disk_events_mutex);
2065
2066 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02002067 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01002068
2069 mutex_unlock(&disk_events_mutex);
2070
2071 return 0;
2072}
2073
2074static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
2075 .set = disk_events_set_dfl_poll_msecs,
2076 .get = param_get_ulong,
2077};
2078
2079#undef MODULE_PARAM_PREFIX
2080#define MODULE_PARAM_PREFIX "block."
2081
2082module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
2083 &disk_events_dfl_poll_msecs, 0644);
2084
2085/*
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002086 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
Tejun Heo77ea8872010-12-08 20:57:37 +01002087 */
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002088static void disk_alloc_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01002089{
2090 struct disk_events *ev;
2091
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002092 if (!disk->fops->check_events || !disk->events)
Tejun Heo77ea8872010-12-08 20:57:37 +01002093 return;
2094
2095 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
2096 if (!ev) {
2097 pr_warn("%s: failed to initialize events\n", disk->disk_name);
2098 return;
2099 }
2100
Tejun Heo77ea8872010-12-08 20:57:37 +01002101 INIT_LIST_HEAD(&ev->node);
2102 ev->disk = disk;
2103 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02002104 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01002105 ev->block = 1;
2106 ev->poll_msecs = -1;
2107 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2108
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002109 disk->ev = ev;
2110}
2111
2112static void disk_add_events(struct gendisk *disk)
2113{
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002114 /* FIXME: error handling */
2115 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2116 pr_warn("%s: failed to create sysfs files for events\n",
2117 disk->disk_name);
2118
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002119 if (!disk->ev)
2120 return;
2121
Tejun Heo77ea8872010-12-08 20:57:37 +01002122 mutex_lock(&disk_events_mutex);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002123 list_add_tail(&disk->ev->node, &disk_events);
Tejun Heo77ea8872010-12-08 20:57:37 +01002124 mutex_unlock(&disk_events_mutex);
2125
2126 /*
2127 * Block count is initialized to 1 and the following initial
2128 * unblock kicks it into action.
2129 */
2130 __disk_unblock_events(disk, true);
2131}
2132
2133static void disk_del_events(struct gendisk *disk)
2134{
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002135 if (disk->ev) {
2136 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01002137
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002138 mutex_lock(&disk_events_mutex);
2139 list_del_init(&disk->ev->node);
2140 mutex_unlock(&disk_events_mutex);
2141 }
Tejun Heo77ea8872010-12-08 20:57:37 +01002142
2143 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2144}
2145
2146static void disk_release_events(struct gendisk *disk)
2147{
2148 /* the block count should be 1 from disk_del_events() */
2149 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
2150 kfree(disk->ev);
2151}