blob: 2d34dd2da4e9e9786e530d96107e855297ab2583 [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
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/module.h>
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01007#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/fs.h>
9#include <linux/genhd.h>
Andrew Mortonb446b602007-02-20 13:57:48 -080010#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040013#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/spinlock.h>
Alexey Dobriyanf5009752008-10-04 23:53:21 +040016#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/seq_file.h>
18#include <linux/slab.h>
19#include <linux/kmod.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080020#include <linux/mutex.h>
Tejun Heobcce3de2008-08-25 19:47:22 +090021#include <linux/idr.h>
Tejun Heo77ea8872010-12-08 20:57:37 +010022#include <linux/log2.h>
Ming Lei25e823c2013-02-22 16:34:13 -080023#include <linux/pm_runtime.h>
Vishal Verma99e66082016-01-09 08:36:51 -080024#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Adrian Bunkff889722008-03-04 11:23:45 +010026#include "blk.h"
27
Christoph Hellwig31eb6182020-03-25 16:48:35 +010028static struct kobject *block_depr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +010030DECLARE_RWSEM(bdev_lookup_sem);
Christoph Hellwig62b508f2020-10-29 15:58:25 +010031
Tejun Heobcce3de2008-08-25 19:47:22 +090032/* for extended dynamic devt allocation, currently only one major is used */
Tejun Heoce23bba2013-02-27 17:03:56 -080033#define NR_EXT_DEVT (1 << MINORBITS)
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +010034static DEFINE_IDA(ext_devt_ida);
Tejun Heobcce3de2008-08-25 19:47:22 +090035
Derek Basehore12c2bdb2012-12-18 12:27:20 -080036static void disk_check_events(struct disk_events *ev,
37 unsigned int *clearing_ptr);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +010038static void disk_alloc_events(struct gendisk *disk);
Tejun Heo77ea8872010-12-08 20:57:37 +010039static void disk_add_events(struct gendisk *disk);
40static void disk_del_events(struct gendisk *disk);
41static void disk_release_events(struct gendisk *disk);
42
Christoph Hellwiga7824832020-11-26 18:43:37 +010043void set_capacity(struct gendisk *disk, sector_t sectors)
44{
Christoph Hellwigcb8432d2020-11-26 18:47:17 +010045 struct block_device *bdev = disk->part0;
Christoph Hellwiga7824832020-11-26 18:43:37 +010046
47 spin_lock(&bdev->bd_size_lock);
48 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
49 spin_unlock(&bdev->bd_size_lock);
50}
51EXPORT_SYMBOL(set_capacity);
52
Balbir Singhe598a722020-03-13 05:30:05 +000053/*
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010054 * Set disk capacity and notify if the size is not currently zero and will not
55 * be set to zero. Returns true if a uevent was sent, otherwise false.
Balbir Singhe598a722020-03-13 05:30:05 +000056 */
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010057bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
Balbir Singhe598a722020-03-13 05:30:05 +000058{
59 sector_t capacity = get_capacity(disk);
Christoph Hellwiga7824832020-11-26 18:43:37 +010060 char *envp[] = { "RESIZE=1", NULL };
Balbir Singhe598a722020-03-13 05:30:05 +000061
62 set_capacity(disk, size);
Balbir Singhe598a722020-03-13 05:30:05 +000063
Christoph Hellwiga7824832020-11-26 18:43:37 +010064 /*
65 * Only print a message and send a uevent if the gendisk is user visible
66 * and alive. This avoids spamming the log and udev when setting the
67 * initial capacity during probing.
68 */
69 if (size == capacity ||
70 (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
71 return false;
Balbir Singhe598a722020-03-13 05:30:05 +000072
Christoph Hellwiga7824832020-11-26 18:43:37 +010073 pr_info("%s: detected capacity change from %lld to %lld\n",
74 disk->disk_name, size, capacity);
Christoph Hellwig7e890c32020-11-12 17:50:04 +010075
Christoph Hellwiga7824832020-11-26 18:43:37 +010076 /*
77 * Historically we did not send a uevent for changes to/from an empty
78 * device.
79 */
80 if (!capacity || !size)
81 return false;
82 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
83 return true;
Balbir Singhe598a722020-03-13 05:30:05 +000084}
Christoph Hellwig449f4ec2020-11-16 15:56:56 +010085EXPORT_SYMBOL_GPL(set_capacity_and_notify);
Balbir Singhe598a722020-03-13 05:30:05 +000086
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +010087/*
88 * Format the device name of the indicated disk into the supplied buffer and
89 * return a pointer to that same buffer for convenience.
90 */
91char *disk_name(struct gendisk *hd, int partno, char *buf)
92{
93 if (!partno)
94 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
95 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
96 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
97 else
98 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
99
100 return buf;
101}
102
103const char *bdevname(struct block_device *bdev, char *buf)
104{
Christoph Hellwig8a63a862020-09-03 07:41:03 +0200105 return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +0100106}
107EXPORT_SYMBOL(bdevname);
Balbir Singhe598a722020-03-13 05:30:05 +0000108
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300109static void part_stat_read_all(struct hd_struct *part, struct disk_stats *stat)
110{
111 int cpu;
112
113 memset(stat, 0, sizeof(struct disk_stats));
114 for_each_possible_cpu(cpu) {
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100115 struct disk_stats *ptr = per_cpu_ptr(part->bdev->bd_stats, cpu);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300116 int group;
117
118 for (group = 0; group < NR_STAT_GROUPS; group++) {
119 stat->nsecs[group] += ptr->nsecs[group];
120 stat->sectors[group] += ptr->sectors[group];
121 stat->ios[group] += ptr->ios[group];
122 stat->merges[group] += ptr->merges[group];
123 }
124
125 stat->io_ticks += ptr->io_ticks;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300126 }
127}
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300128
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100129static unsigned int part_in_flight(struct block_device *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600130{
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200131 unsigned int inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500132 int cpu;
133
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500134 for_each_possible_cpu(cpu) {
Mikulas Patockae016b782018-12-06 11:41:21 -0500135 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
136 part_stat_local_read_cpu(part, in_flight[1], cpu);
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500137 }
Mikulas Patockae016b782018-12-06 11:41:21 -0500138 if ((int)inflight < 0)
139 inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500140
Mikulas Patockae016b782018-12-06 11:41:21 -0500141 return inflight;
Jens Axboef299b7c2017-08-08 17:51:45 -0600142}
143
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100144static void part_in_flight_rw(struct block_device *part,
145 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700146{
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500147 int cpu;
148
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500149 inflight[0] = 0;
150 inflight[1] = 0;
151 for_each_possible_cpu(cpu) {
152 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
153 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
154 }
155 if ((int)inflight[0] < 0)
156 inflight[0] = 0;
157 if ((int)inflight[1] < 0)
158 inflight[1] = 0;
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700159}
160
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100161struct block_device *__disk_get_part(struct gendisk *disk, int partno)
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200162{
163 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
164
165 if (unlikely(partno < 0 || partno >= ptbl->len))
166 return NULL;
167 return rcu_dereference(ptbl->part[partno]);
168}
169
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200170/**
171 * disk_get_part - get partition
172 * @disk: disk to look partition from
173 * @partno: partition number
174 *
175 * Look for partition @partno from @disk. If found, increment
176 * reference count and return it.
177 *
178 * CONTEXT:
179 * Don't care.
180 *
181 * RETURNS:
182 * Pointer to the found partition on success, NULL if not found.
183 */
184struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
185{
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100186 struct block_device *bdev;
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200187 struct hd_struct *part;
Tejun Heo540eed52008-08-25 19:56:15 +0900188
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200189 rcu_read_lock();
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100190 bdev = __disk_get_part(disk, partno);
191 if (!bdev)
192 goto fail;
193 part = bdev->bd_part;
194 if (!kobject_get_unless_zero(&part_to_dev(part)->kobj))
195 goto fail;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200196 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200197 return part;
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100198fail:
199 rcu_read_unlock();
200 return NULL;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200201}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200202
203/**
204 * disk_part_iter_init - initialize partition iterator
205 * @piter: iterator to initialize
206 * @disk: disk to iterate over
207 * @flags: DISK_PITER_* flags
208 *
209 * Initialize @piter so that it iterates over partitions of @disk.
210 *
211 * CONTEXT:
212 * Don't care.
213 */
214void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
215 unsigned int flags)
216{
Tejun Heo540eed52008-08-25 19:56:15 +0900217 struct disk_part_tbl *ptbl;
218
219 rcu_read_lock();
220 ptbl = rcu_dereference(disk->part_tbl);
221
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200222 piter->disk = disk;
223 piter->part = NULL;
224
225 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +0900226 piter->idx = ptbl->len - 1;
Tejun Heo71982a42009-04-17 08:34:48 +0200227 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200228 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200229 else
230 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200231
232 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900233
234 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200235}
236EXPORT_SYMBOL_GPL(disk_part_iter_init);
237
238/**
239 * disk_part_iter_next - proceed iterator to the next partition and return it
240 * @piter: iterator of interest
241 *
242 * Proceed @piter to the next partition and return it.
243 *
244 * CONTEXT:
245 * Don't care.
246 */
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100247struct block_device *disk_part_iter_next(struct disk_part_iter *piter)
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200248{
Tejun Heo540eed52008-08-25 19:56:15 +0900249 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200250 int inc, end;
251
252 /* put the last partition */
Christoph Hellwige79319a2020-11-10 06:48:53 +0100253 disk_part_iter_exit(piter);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200254
Tejun Heo540eed52008-08-25 19:56:15 +0900255 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200256 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900257 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200258
259 /* determine iteration parameters */
260 if (piter->flags & DISK_PITER_REVERSE) {
261 inc = -1;
Tejun Heo71982a42009-04-17 08:34:48 +0200262 if (piter->flags & (DISK_PITER_INCL_PART0 |
263 DISK_PITER_INCL_EMPTY_PART0))
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200264 end = -1;
265 else
266 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200267 } else {
268 inc = 1;
Tejun Heo540eed52008-08-25 19:56:15 +0900269 end = ptbl->len;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200270 }
271
272 /* iterate to the next partition */
273 for (; piter->idx != end; piter->idx += inc) {
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100274 struct block_device *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200275
Tejun Heo540eed52008-08-25 19:56:15 +0900276 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200277 if (!part)
278 continue;
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100279 if (!bdev_nr_sectors(part) &&
Tejun Heo71982a42009-04-17 08:34:48 +0200280 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
281 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
282 piter->idx == 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200283 continue;
284
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100285 piter->part = bdgrab(part);
286 if (!piter->part)
287 continue;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200288 piter->idx += inc;
289 break;
290 }
291
292 rcu_read_unlock();
293
294 return piter->part;
295}
296EXPORT_SYMBOL_GPL(disk_part_iter_next);
297
298/**
299 * disk_part_iter_exit - finish up partition iteration
300 * @piter: iter of interest
301 *
302 * Called when iteration is over. Cleans up @piter.
303 *
304 * CONTEXT:
305 * Don't care.
306 */
307void disk_part_iter_exit(struct disk_part_iter *piter)
308{
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100309 if (piter->part)
310 bdput(piter->part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200311 piter->part = NULL;
312}
313EXPORT_SYMBOL_GPL(disk_part_iter_exit);
314
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100315static inline int sector_in_part(struct block_device *part, sector_t sector)
Jens Axboea6f23652008-10-24 12:52:42 +0200316{
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100317 return part->bd_start_sect <= sector &&
318 sector < part->bd_start_sect + bdev_nr_sectors(part);
Jens Axboea6f23652008-10-24 12:52:42 +0200319}
320
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200321/**
322 * disk_map_sector_rcu - map sector to partition
323 * @disk: gendisk of interest
324 * @sector: sector to map
325 *
326 * Find out which partition @sector maps to on @disk. This is
327 * primarily used for stats accounting.
328 *
329 * CONTEXT:
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100330 * RCU read locked.
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200331 *
332 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900333 * Found partition on success, part0 is returned if no partition matches
Ming Leib7d6c302020-05-08 16:17:55 +0800334 * or the matched partition is being deleted.
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200335 */
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100336struct block_device *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200337{
Tejun Heo540eed52008-08-25 19:56:15 +0900338 struct disk_part_tbl *ptbl;
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100339 struct block_device *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200340 int i;
341
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200342 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900343 ptbl = rcu_dereference(disk->part_tbl);
344
Jens Axboea6f23652008-10-24 12:52:42 +0200345 part = rcu_dereference(ptbl->last_lookup);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100346 if (part && sector_in_part(part, sector))
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200347 goto out_unlock;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200348
Jens Axboea6f23652008-10-24 12:52:42 +0200349 for (i = 1; i < ptbl->len; i++) {
350 part = rcu_dereference(ptbl->part[i]);
Jens Axboea6f23652008-10-24 12:52:42 +0200351 if (part && sector_in_part(part, sector)) {
352 rcu_assign_pointer(ptbl->last_lookup, part);
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200353 goto out_unlock;
Jens Axboea6f23652008-10-24 12:52:42 +0200354 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200355 }
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200356
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100357 part = disk->part0;
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200358out_unlock:
359 rcu_read_unlock();
360 return part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200361}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200362
Shin'ichiro Kawasakib53df2e2020-02-21 10:37:08 +0900363/**
364 * disk_has_partitions
365 * @disk: gendisk of interest
366 *
367 * Walk through the partition table and check if valid partition exists.
368 *
369 * CONTEXT:
370 * Don't care.
371 *
372 * RETURNS:
373 * True if the gendisk has at least one valid non-zero size partition.
374 * Otherwise false.
375 */
376bool disk_has_partitions(struct gendisk *disk)
377{
378 struct disk_part_tbl *ptbl;
379 int i;
380 bool ret = false;
381
382 rcu_read_lock();
383 ptbl = rcu_dereference(disk->part_tbl);
384
385 /* Iterate partitions skipping the whole device at index 0 */
386 for (i = 1; i < ptbl->len; i++) {
387 if (rcu_dereference(ptbl->part[i])) {
388 ret = true;
389 break;
390 }
391 }
392
393 rcu_read_unlock();
394
395 return ret;
396}
397EXPORT_SYMBOL_GPL(disk_has_partitions);
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/*
400 * Can be deleted altogether. Later.
401 *
402 */
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600403#define BLKDEV_MAJOR_HASH_SIZE 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404static struct blk_major_name {
405 struct blk_major_name *next;
406 int major;
407 char name[16];
Christoph Hellwiga160c612020-10-29 15:58:28 +0100408 void (*probe)(dev_t devt);
Joe Korty68eef3b2006-03-31 02:30:32 -0800409} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100410static DEFINE_MUTEX(major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100413static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Joe Korty68eef3b2006-03-31 02:30:32 -0800415 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Joe Korty68eef3b2006-03-31 02:30:32 -0800418#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200419void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800420{
Joe Korty68eef3b2006-03-31 02:30:32 -0800421 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800422
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100423 mutex_lock(&major_names_lock);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600424 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
425 if (dp->major == offset)
Tejun Heocf771cb2008-09-03 09:01:09 +0200426 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100427 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
Joe Korty68eef3b2006-03-31 02:30:32 -0800429#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100431/**
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100432 * __register_blkdev - register a new block device
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100433 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800434 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
435 * @major = 0, try to allocate any unused major number.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100436 * @name: the name of the new block device as a zero terminated string
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100437 * @probe: allback that is called on access to any minor number of @major
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100438 *
439 * The @name must be unique within the system.
440 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300441 * The return value depends on the @major input parameter:
442 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800443 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
444 * then the function returns zero on success, or a negative error code
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300445 * - if any unused major number was requested with @major = 0 parameter
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100446 * then the return value is the allocated major number in range
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800447 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
448 *
449 * See Documentation/admin-guide/devices.txt for the list of allocated
450 * major numbers.
Christoph Hellwige2b6b302020-11-14 18:08:21 +0100451 *
452 * Use register_blkdev instead for any new code.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100453 */
Christoph Hellwiga160c612020-10-29 15:58:28 +0100454int __register_blkdev(unsigned int major, const char *name,
455 void (*probe)(dev_t devt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct blk_major_name **n, *p;
458 int index, ret = 0;
459
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100460 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* temporary */
463 if (major == 0) {
464 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
465 if (major_names[index] == NULL)
466 break;
467 }
468
469 if (index == 0) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500470 printk("%s: failed to get major for %s\n",
471 __func__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 ret = -EBUSY;
473 goto out;
474 }
475 major = index;
476 ret = major;
477 }
478
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600479 if (major >= BLKDEV_MAJOR_MAX) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500480 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
481 __func__, major, BLKDEV_MAJOR_MAX-1, name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600482
483 ret = -EINVAL;
484 goto out;
485 }
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
488 if (p == NULL) {
489 ret = -ENOMEM;
490 goto out;
491 }
492
493 p->major = major;
Christoph Hellwiga160c612020-10-29 15:58:28 +0100494 p->probe = probe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 strlcpy(p->name, name, sizeof(p->name));
496 p->next = NULL;
497 index = major_to_index(major);
498
499 for (n = &major_names[index]; *n; n = &(*n)->next) {
500 if ((*n)->major == major)
501 break;
502 }
503 if (!*n)
504 *n = p;
505 else
506 ret = -EBUSY;
507
508 if (ret < 0) {
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800509 printk("register_blkdev: cannot get major %u for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 major, name);
511 kfree(p);
512 }
513out:
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100514 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return ret;
516}
Christoph Hellwiga160c612020-10-29 15:58:28 +0100517EXPORT_SYMBOL(__register_blkdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Akinobu Mitaf4480242007-07-17 04:03:47 -0700519void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct blk_major_name **n;
522 struct blk_major_name *p = NULL;
523 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100525 mutex_lock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 for (n = &major_names[index]; *n; n = &(*n)->next)
527 if ((*n)->major == major)
528 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700529 if (!*n || strcmp((*n)->name, name)) {
530 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700531 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 p = *n;
533 *n = p->next;
534 }
Christoph Hellwige49fbbb2020-10-29 15:58:26 +0100535 mutex_unlock(&major_names_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539EXPORT_SYMBOL(unregister_blkdev);
540
Tejun Heobcce3de2008-08-25 19:47:22 +0900541/**
Tejun Heo870d6652008-08-25 19:47:25 +0900542 * blk_mangle_minor - scatter minor numbers apart
543 * @minor: minor number to mangle
544 *
545 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
546 * is enabled. Mangling twice gives the original value.
547 *
548 * RETURNS:
549 * Mangled value.
550 *
551 * CONTEXT:
552 * Don't care.
553 */
554static int blk_mangle_minor(int minor)
555{
556#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
557 int i;
558
559 for (i = 0; i < MINORBITS / 2; i++) {
560 int low = minor & (1 << i);
561 int high = minor & (1 << (MINORBITS - 1 - i));
562 int distance = MINORBITS - 1 - 2 * i;
563
564 minor ^= low | high; /* clear both bits */
565 low <<= distance; /* swap the positions */
566 high >>= distance;
567 minor |= low | high; /* and set */
568 }
569#endif
570 return minor;
571}
572
573/**
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100574 * blk_alloc_devt - allocate a dev_t for a block device
575 * @bdev: block device to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900576 * @devt: out parameter for resulting dev_t
577 *
578 * Allocate a dev_t for block device.
579 *
580 * RETURNS:
581 * 0 on success, allocated dev_t is returned in *@devt. -errno on
582 * failure.
583 *
584 * CONTEXT:
585 * Might sleep.
586 */
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100587int blk_alloc_devt(struct block_device *bdev, dev_t *devt)
Tejun Heobcce3de2008-08-25 19:47:22 +0900588{
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100589 struct gendisk *disk = bdev->bd_disk;
Tejun Heobab998d2013-02-27 17:03:57 -0800590 int idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900591
592 /* in consecutive minor range? */
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100593 if (bdev->bd_partno < disk->minors) {
594 *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
Tejun Heobcce3de2008-08-25 19:47:22 +0900595 return 0;
596 }
597
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100598 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
Tejun Heobab998d2013-02-27 17:03:57 -0800599 if (idx < 0)
600 return idx == -ENOSPC ? -EBUSY : idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900601
Tejun Heo870d6652008-08-25 19:47:25 +0900602 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900603 return 0;
604}
605
606/**
607 * blk_free_devt - free a dev_t
608 * @devt: dev_t to free
609 *
610 * Free @devt which was allocated using blk_alloc_devt().
611 *
612 * CONTEXT:
613 * Might sleep.
614 */
615void blk_free_devt(dev_t devt)
616{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100617 if (MAJOR(devt) == BLOCK_EXT_MAJOR)
618 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800619}
620
Tejun Heo1f014292008-08-25 19:47:23 +0900621static char *bdevt_str(dev_t devt, char *buf)
622{
623 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
624 char tbuf[BDEVT_SIZE];
625 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
626 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
627 } else
628 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
629
630 return buf;
631}
632
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200633static void disk_scan_partitions(struct gendisk *disk)
634{
635 struct block_device *bdev;
636
637 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
638 return;
639
640 set_bit(GD_NEED_PART_SCAN, &disk->state);
641 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
642 if (!IS_ERR(bdev))
643 blkdev_put(bdev, FMODE_READ);
644}
645
Hannes Reineckefef912b2018-09-28 08:17:19 +0200646static void register_disk(struct device *parent, struct gendisk *disk,
647 const struct attribute_group **groups)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100648{
649 struct device *ddev = disk_to_dev(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100650 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100651 struct block_device *part;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100652 int err;
653
Dan Williamse63a46b2016-06-15 18:17:27 -0700654 ddev->parent = parent;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100655
Kees Cookffc8b302013-07-03 15:01:14 -0700656 dev_set_name(ddev, "%s", disk->disk_name);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100657
658 /* delay uevents, until we scanned partition table */
659 dev_set_uevent_suppress(ddev, 1);
660
Hannes Reineckefef912b2018-09-28 08:17:19 +0200661 if (groups) {
662 WARN_ON(ddev->groups);
663 ddev->groups = groups;
664 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100665 if (device_add(ddev))
666 return;
667 if (!sysfs_deprecated) {
668 err = sysfs_create_link(block_depr, &ddev->kobj,
669 kobject_name(&ddev->kobj));
670 if (err) {
671 device_del(ddev);
672 return;
673 }
674 }
Ming Lei25e823c2013-02-22 16:34:13 -0800675
676 /*
677 * avoid probable deadlock caused by allocating memory with
678 * GFP_KERNEL in runtime_resume callback of its all ancestor
679 * devices
680 */
681 pm_runtime_set_memalloc_noio(ddev, true);
682
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100683 disk->part0->bd_holder_dir =
684 kobject_create_and_add("holders", &ddev->kobj);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100685 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
686
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300687 if (disk->flags & GENHD_FL_HIDDEN) {
688 dev_set_uevent_suppress(ddev, 0);
689 return;
690 }
691
Christoph Hellwig9301fe72020-09-21 09:19:46 +0200692 disk_scan_partitions(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100693
Tejun Heod2bf1b62010-12-08 20:57:36 +0100694 /* announce disk after possible partitions are created */
695 dev_set_uevent_suppress(ddev, 0);
696 kobject_uevent(&ddev->kobj, KOBJ_ADD);
697
698 /* announce possible partitions */
699 disk_part_iter_init(&piter, disk, 0);
700 while ((part = disk_part_iter_next(&piter)))
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100701 kobject_uevent(bdev_kobj(part), KOBJ_ADD);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100702 disk_part_iter_exit(&piter);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300703
zhengbin4d7c1d32019-02-20 21:27:05 +0800704 if (disk->queue->backing_dev_info->dev) {
705 err = sysfs_create_link(&ddev->kobj,
706 &disk->queue->backing_dev_info->dev->kobj,
707 "bdi");
708 WARN_ON(err);
709 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100710}
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/**
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500713 * __device_add_disk - add disk information to kernel list
Dan Williamse63a46b2016-06-15 18:17:27 -0700714 * @parent: parent device for the disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 * @disk: per-device partitioning information
Hannes Reineckefef912b2018-09-28 08:17:19 +0200716 * @groups: Additional per-device sysfs groups
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500717 * @register_queue: register the queue if set to true
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 *
719 * This function registers the partitioning information in @disk
720 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900721 *
722 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500724static void __device_add_disk(struct device *parent, struct gendisk *disk,
Hannes Reineckefef912b2018-09-28 08:17:19 +0200725 const struct attribute_group **groups,
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500726 bool register_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900728 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400729 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700730
Damien Le Moal737eb782019-09-05 18:51:33 +0900731 /*
732 * The disk queue should now be all set with enough information about
733 * the device for the elevator code to pick an adequate default
734 * elevator if one is needed, that is, for devices requesting queue
735 * registration.
736 */
737 if (register_queue)
738 elevator_init_mq(disk->queue);
739
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900740 /* minors == 0 indicates to use ext devt from part0 and should
741 * be accompanied with EXT_DEVT flag. Make sure all
742 * parameters make sense.
743 */
744 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300745 WARN_ON(!disk->minors &&
746 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900749
Christoph Hellwig9fc995a2020-11-24 09:17:46 +0100750 retval = blk_alloc_devt(disk->part0, &devt);
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900751 if (retval) {
752 WARN_ON(1);
753 return;
754 }
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900755 disk->major = MAJOR(devt);
756 disk->first_minor = MINOR(devt);
757
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +0100758 disk_alloc_events(disk);
759
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300760 if (disk->flags & GENHD_FL_HIDDEN) {
761 /*
762 * Don't let hidden disks show up in /proc/partitions,
763 * and don't bother scanning for partitions either.
764 */
765 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
766 disk->flags |= GENHD_FL_NO_PART_SCAN;
767 } else {
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200768 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
769 struct device *dev = disk_to_dev(disk);
weiping zhang3a921682017-10-31 18:38:59 +0800770 int ret;
771
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300772 /* Register BDI before referencing it from bdev */
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200773 dev->devt = devt;
774 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
weiping zhang3a921682017-10-31 18:38:59 +0800775 WARN_ON(ret);
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200776 bdi_set_owner(bdi, dev);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100777 bdev_add(disk->part0, devt);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300778 }
Hannes Reineckefef912b2018-09-28 08:17:19 +0200779 register_disk(parent, disk, groups);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500780 if (register_queue)
781 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700782
Tejun Heo523e1d32011-10-19 14:31:07 +0200783 /*
784 * Take an extra ref on queue which will be put on disk_release()
785 * so that it sticks around as long as @disk is there.
786 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100787 WARN_ON_ONCE(!blk_get_queue(disk->queue));
Tejun Heo523e1d32011-10-19 14:31:07 +0200788
Tejun Heo77ea8872010-12-08 20:57:37 +0100789 disk_add_events(disk);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400790 blk_integrity_add(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500792
Hannes Reineckefef912b2018-09-28 08:17:19 +0200793void device_add_disk(struct device *parent, struct gendisk *disk,
794 const struct attribute_group **groups)
795
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500796{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200797 __device_add_disk(parent, disk, groups, true);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500798}
Dan Williamse63a46b2016-06-15 18:17:27 -0700799EXPORT_SYMBOL(device_add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500801void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
802{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200803 __device_add_disk(parent, disk, NULL, false);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500804}
805EXPORT_SYMBOL(device_add_disk_no_queue_reg);
806
Christoph Hellwig71773cf2020-11-24 09:20:37 +0100807static void invalidate_partition(struct block_device *bdev)
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200808{
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200809 fsync_bdev(bdev);
810 __invalidate_device(bdev, true);
Christoph Hellwig9bc5c392020-04-14 09:29:02 +0200811
812 /*
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100813 * Unhash the bdev inode for this device so that it can't be looked
814 * up any more even if openers still hold references to it.
Christoph Hellwig9bc5c392020-04-14 09:29:02 +0200815 */
816 remove_inode_hash(bdev->bd_inode);
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200817}
818
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000819/**
820 * del_gendisk - remove the gendisk
821 * @disk: the struct gendisk to remove
822 *
823 * Removes the gendisk and all its associated resources. This deletes the
824 * partitions associated with the gendisk, and unregisters the associated
825 * request_queue.
826 *
827 * This is the counter to the respective __device_add_disk() call.
828 *
829 * The final removal of the struct gendisk happens when its refcount reaches 0
830 * with put_disk(), which should be called after del_gendisk(), if
831 * __device_add_disk() was used.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000832 *
833 * Drivers exist which depend on the release of the gendisk to be synchronous,
834 * it should not be deferred.
835 *
836 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000837 */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100838void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Tejun Heod2bf1b62010-12-08 20:57:36 +0100840 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100841 struct block_device *part;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100842
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000843 might_sleep();
844
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100845 if (WARN_ON_ONCE(!disk->queue))
846 return;
847
Martin K. Petersen25520d52015-10-21 13:19:49 -0400848 blk_integrity_del(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +0100849 disk_del_events(disk);
850
Jan Kara56c09082018-02-26 13:01:41 +0100851 /*
852 * Block lookups of the disk until all bdevs are unhashed and the
853 * disk is marked as dead (GENHD_FL_UP cleared).
854 */
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100855 down_write(&bdev_lookup_sem);
856
Tejun Heod2bf1b62010-12-08 20:57:36 +0100857 /* invalidate stuff */
858 disk_part_iter_init(&piter, disk,
859 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
860 while ((part = disk_part_iter_next(&piter))) {
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100861 invalidate_partition(part);
862 delete_partition(part->bd_part);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100863 }
864 disk_part_iter_exit(&piter);
865
Christoph Hellwig71773cf2020-11-24 09:20:37 +0100866 invalidate_partition(disk->part0);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100867 set_capacity(disk, 0);
868 disk->flags &= ~GENHD_FL_UP;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100869 up_write(&bdev_lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100870
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100871 if (!(disk->flags & GENHD_FL_HIDDEN)) {
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300872 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100873
Jan Kara90f16fd2017-03-08 17:48:33 +0100874 /*
875 * Unregister bdi before releasing device numbers (as they can
876 * get reused and we'd get clashes in sysfs).
877 */
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100878 bdi_unregister(disk->queue->backing_dev_info);
Jan Kara90f16fd2017-03-08 17:48:33 +0100879 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100880
Christoph Hellwig6b3ba972020-10-29 15:58:24 +0100881 blk_unregister_queue(disk);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100882
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100883 kobject_put(disk->part0->bd_holder_dir);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100884 kobject_put(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100886 part_stat_set_all(disk->part0, 0);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100887 disk->part0->bd_stamp = 0;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100888 if (!sysfs_deprecated)
889 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
Ming Lei25e823c2013-02-22 16:34:13 -0800890 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100891 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100893EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Vishal Verma99e66082016-01-09 08:36:51 -0800895/* sysfs access to bad-blocks list. */
896static ssize_t disk_badblocks_show(struct device *dev,
897 struct device_attribute *attr,
898 char *page)
899{
900 struct gendisk *disk = dev_to_disk(dev);
901
902 if (!disk->bb)
903 return sprintf(page, "\n");
904
905 return badblocks_show(disk->bb, page, 0);
906}
907
908static ssize_t disk_badblocks_store(struct device *dev,
909 struct device_attribute *attr,
910 const char *page, size_t len)
911{
912 struct gendisk *disk = dev_to_disk(dev);
913
914 if (!disk->bb)
915 return -ENXIO;
916
917 return badblocks_store(disk->bb, page, len, 0);
918}
919
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100920void blk_request_module(dev_t devt)
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100921{
Christoph Hellwiga160c612020-10-29 15:58:28 +0100922 unsigned int major = MAJOR(devt);
923 struct blk_major_name **n;
924
925 mutex_lock(&major_names_lock);
926 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
927 if ((*n)->major == major && (*n)->probe) {
928 (*n)->probe(devt);
929 mutex_unlock(&major_names_lock);
930 return;
931 }
932 }
933 mutex_unlock(&major_names_lock);
934
Christoph Hellwigbd8eff32020-10-29 15:58:27 +0100935 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
936 /* Make old-style 2.4 aliases work */
937 request_module("block-major-%d", MAJOR(devt));
938}
939
Tejun Heof331c022008-09-03 09:01:48 +0200940/**
941 * bdget_disk - do bdget() by gendisk and partition number
942 * @disk: gendisk of interest
943 * @partno: partition number
944 *
945 * Find partition @partno from @disk, do bdget() on it.
946 *
947 * CONTEXT:
948 * Don't care.
949 *
950 * RETURNS:
951 * Resulting block_device on success, NULL on failure.
952 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200953struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200954{
Tejun Heo548b10e2008-08-29 09:01:47 +0200955 struct hd_struct *part;
956 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200957
Tejun Heo548b10e2008-08-29 09:01:47 +0200958 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +0200959 if (part)
Christoph Hellwig10ed1662020-09-25 18:06:18 +0200960 bdev = bdget_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +0200961 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200962
Tejun Heo548b10e2008-08-29 09:01:47 +0200963 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200964}
965EXPORT_SYMBOL(bdget_disk);
966
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700967/*
968 * print a full list of all partitions - intended for places where the root
969 * filesystem can't be mounted and thus to give the victim some idea of what
970 * went wrong
971 */
972void __init printk_all_partitions(void)
973{
Tejun Heodef4e382008-09-03 08:57:12 +0200974 struct class_dev_iter iter;
975 struct device *dev;
976
977 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
978 while ((dev = class_dev_iter_next(&iter))) {
979 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200980 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +0100981 struct block_device *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900982 char name_buf[BDEVNAME_SIZE];
983 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200984
985 /*
986 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300987 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +0200988 */
989 if (get_capacity(disk) == 0 ||
990 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
991 continue;
992
993 /*
994 * Note, unlike /proc/partitions, I am showing the
995 * numbers in hex - the same format as the root=
996 * option takes.
997 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900998 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
999 while ((part = disk_part_iter_next(&piter))) {
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001000 bool is_part0 = part == disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +02001001
Will Drewryb5af9212010-08-31 15:47:07 -05001002 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001003 bdevt_str(part->bd_dev, devt_buf),
1004 bdev_nr_sectors(part) >> 1,
1005 disk_name(disk, part->bd_partno, name_buf),
1006 part->bd_meta_info ?
1007 part->bd_meta_info->uuid : "");
Tejun Heo074a7ac2008-08-25 19:56:14 +09001008 if (is_part0) {
Dan Williams52c44d92016-06-15 19:43:07 -07001009 if (dev->parent && dev->parent->driver)
Tejun Heo074a7ac2008-08-25 19:56:14 +09001010 printk(" driver: %s\n",
Dan Williams52c44d92016-06-15 19:43:07 -07001011 dev->parent->driver->name);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001012 else
1013 printk(" (driver?)\n");
1014 } else
1015 printk("\n");
1016 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001017 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +02001018 }
1019 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -07001020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022#ifdef CONFIG_PROC_FS
1023/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +02001024static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001025{
Tejun Heodef4e382008-09-03 08:57:12 +02001026 loff_t skip = *pos;
1027 struct class_dev_iter *iter;
1028 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001029
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +02001030 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +02001031 if (!iter)
1032 return ERR_PTR(-ENOMEM);
1033
1034 seqf->private = iter;
1035 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
1036 do {
1037 dev = class_dev_iter_next(iter);
1038 if (!dev)
1039 return NULL;
1040 } while (skip--);
1041
1042 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001043}
1044
Tejun Heodef4e382008-09-03 08:57:12 +02001045static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001047 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -04001048
Tejun Heodef4e382008-09-03 08:57:12 +02001049 (*pos)++;
1050 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +02001051 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001052 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +02001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return NULL;
1055}
1056
Tejun Heodef4e382008-09-03 08:57:12 +02001057static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -04001058{
Tejun Heodef4e382008-09-03 08:57:12 +02001059 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -04001060
Tejun Heodef4e382008-09-03 08:57:12 +02001061 /* stop is called even after start failed :-( */
1062 if (iter) {
1063 class_dev_iter_exit(iter);
1064 kfree(iter);
Vegard Nossum77da1602016-07-29 10:40:31 +02001065 seqf->private = NULL;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
Tejun Heodef4e382008-09-03 08:57:12 +02001069static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Jianpeng Ma06768062012-08-03 10:42:00 +02001071 void *p;
Tejun Heodef4e382008-09-03 08:57:12 +02001072
1073 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +01001074 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +02001075 seq_puts(seqf, "major minor #blocks name\n\n");
1076 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Tejun Heocf771cb2008-09-03 09:01:09 +02001079static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001082 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001083 struct block_device *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 char buf[BDEVNAME_SIZE];
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +02001087 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +02001088 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return 0;
1090 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
1091 return 0;
1092
1093 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +09001094 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001095 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +09001096 seq_printf(seqf, "%4d %7d %10llu %s\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001097 MAJOR(part->bd_dev), MINOR(part->bd_dev),
1098 bdev_nr_sectors(part) >> 1,
1099 disk_name(sgp, part->bd_partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001100 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 return 0;
1103}
1104
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001105static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001106 .start = show_partition_start,
1107 .next = disk_seqf_next,
1108 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001109 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110};
1111#endif
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113static int __init genhd_device_init(void)
1114{
Dan Williamse105b8b2008-04-21 10:51:07 -07001115 int error;
1116
1117 block_class.dev_kobj = sysfs_dev_block_kobj;
1118 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -07001119 if (unlikely(error))
1120 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001122
Zhang, Yanmin561ec682008-11-14 08:26:30 +01001123 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1124
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001125 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +02001126 if (!sysfs_deprecated)
1127 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -08001128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131subsys_initcall(genhd_device_init);
1132
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001133static ssize_t disk_range_show(struct device *dev,
1134 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001136 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001138 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Tejun Heo1f014292008-08-25 19:47:23 +09001141static ssize_t disk_ext_range_show(struct device *dev,
1142 struct device_attribute *attr, char *buf)
1143{
1144 struct gendisk *disk = dev_to_disk(dev);
1145
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001146 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +09001147}
1148
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001149static ssize_t disk_removable_show(struct device *dev,
1150 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +02001151{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001152 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001153
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001154 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001156}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001158static ssize_t disk_hidden_show(struct device *dev,
1159 struct device_attribute *attr, char *buf)
1160{
1161 struct gendisk *disk = dev_to_disk(dev);
1162
1163 return sprintf(buf, "%d\n",
1164 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1165}
1166
Kay Sievers1c9ce522008-06-13 09:41:00 +02001167static ssize_t disk_ro_show(struct device *dev,
1168 struct device_attribute *attr, char *buf)
1169{
1170 struct gendisk *disk = dev_to_disk(dev);
1171
Tejun Heob7db9952008-08-25 19:56:10 +09001172 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +02001173}
1174
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001175ssize_t part_size_show(struct device *dev,
1176 struct device_attribute *attr, char *buf)
1177{
1178 struct hd_struct *p = dev_to_part(dev);
1179
Christoph Hellwiga7824832020-11-26 18:43:37 +01001180 return sprintf(buf, "%llu\n", bdev_nr_sectors(p->bdev));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001181}
1182
1183ssize_t part_stat_show(struct device *dev,
1184 struct device_attribute *attr, char *buf)
1185{
1186 struct hd_struct *p = dev_to_part(dev);
1187 struct request_queue *q = part_to_disk(p)->queue;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001188 struct disk_stats stat;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001189 unsigned int inflight;
1190
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001191 part_stat_read_all(p, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001192 if (queue_is_mq(q))
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001193 inflight = blk_mq_in_flight(q, p->bdev);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001194 else
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001195 inflight = part_in_flight(p->bdev);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001196
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001197 return sprintf(buf,
1198 "%8lu %8lu %8llu %8u "
1199 "%8lu %8lu %8llu %8u "
1200 "%8u %8u %8u "
1201 "%8lu %8lu %8llu %8u "
1202 "%8lu %8u"
1203 "\n",
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001204 stat.ios[STAT_READ],
1205 stat.merges[STAT_READ],
1206 (unsigned long long)stat.sectors[STAT_READ],
1207 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
1208 stat.ios[STAT_WRITE],
1209 stat.merges[STAT_WRITE],
1210 (unsigned long long)stat.sectors[STAT_WRITE],
1211 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001212 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001213 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001214 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1215 stat.nsecs[STAT_WRITE] +
1216 stat.nsecs[STAT_DISCARD] +
1217 stat.nsecs[STAT_FLUSH],
1218 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001219 stat.ios[STAT_DISCARD],
1220 stat.merges[STAT_DISCARD],
1221 (unsigned long long)stat.sectors[STAT_DISCARD],
1222 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
1223 stat.ios[STAT_FLUSH],
1224 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001225}
1226
1227ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
1228 char *buf)
1229{
1230 struct hd_struct *p = dev_to_part(dev);
1231 struct request_queue *q = part_to_disk(p)->queue;
1232 unsigned int inflight[2];
1233
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001234 if (queue_is_mq(q))
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001235 blk_mq_in_flight_rw(q, p->bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001236 else
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001237 part_in_flight_rw(p->bdev, inflight);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001238
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001239 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1240}
1241
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001242static ssize_t disk_capability_show(struct device *dev,
1243 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001244{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001245 struct gendisk *disk = dev_to_disk(dev);
1246
1247 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001248}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001249
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001250static ssize_t disk_alignment_offset_show(struct device *dev,
1251 struct device_attribute *attr,
1252 char *buf)
1253{
1254 struct gendisk *disk = dev_to_disk(dev);
1255
1256 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1257}
1258
Martin K. Petersen86b37282009-11-10 11:50:21 +01001259static ssize_t disk_discard_alignment_show(struct device *dev,
1260 struct device_attribute *attr,
1261 char *buf)
1262{
1263 struct gendisk *disk = dev_to_disk(dev);
1264
Martin K. Petersendd3d1452010-01-11 03:21:48 -05001265 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +01001266}
1267
Joe Perches5657a812018-05-24 13:38:59 -06001268static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1269static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1270static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1271static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1272static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1273static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1274static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1275static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1276static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1277static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1278static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1279static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001280
Akinobu Mitac17bb492006-12-08 02:39:46 -08001281#ifdef CONFIG_FAIL_MAKE_REQUEST
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001282ssize_t part_fail_show(struct device *dev,
1283 struct device_attribute *attr, char *buf)
1284{
1285 struct hd_struct *p = dev_to_part(dev);
1286
Christoph Hellwigb309e992020-11-23 16:28:47 +01001287 return sprintf(buf, "%d\n", p->bdev->bd_make_it_fail);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001288}
1289
1290ssize_t part_fail_store(struct device *dev,
1291 struct device_attribute *attr,
1292 const char *buf, size_t count)
1293{
1294 struct hd_struct *p = dev_to_part(dev);
1295 int i;
1296
1297 if (count > 0 && sscanf(buf, "%d", &i) > 0)
Christoph Hellwigb309e992020-11-23 16:28:47 +01001298 p->bdev->bd_make_it_fail = i;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001299
1300 return count;
1301}
1302
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001303static struct device_attribute dev_attr_fail =
Joe Perches5657a812018-05-24 13:38:59 -06001304 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001305#endif /* CONFIG_FAIL_MAKE_REQUEST */
1306
Jens Axboe581d4e22008-09-14 05:56:33 -07001307#ifdef CONFIG_FAIL_IO_TIMEOUT
1308static struct device_attribute dev_attr_fail_timeout =
Joe Perches5657a812018-05-24 13:38:59 -06001309 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
Jens Axboe581d4e22008-09-14 05:56:33 -07001310#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001311
1312static struct attribute *disk_attrs[] = {
1313 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +09001314 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001315 &dev_attr_removable.attr,
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001316 &dev_attr_hidden.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001317 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001318 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001319 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001320 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001321 &dev_attr_capability.attr,
1322 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001323 &dev_attr_inflight.attr,
Vishal Verma99e66082016-01-09 08:36:51 -08001324 &dev_attr_badblocks.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001325#ifdef CONFIG_FAIL_MAKE_REQUEST
1326 &dev_attr_fail.attr,
1327#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001328#ifdef CONFIG_FAIL_IO_TIMEOUT
1329 &dev_attr_fail_timeout.attr,
1330#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001331 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332};
1333
Dan Williams9438b3e02017-04-27 14:46:26 -07001334static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1335{
1336 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1337 struct gendisk *disk = dev_to_disk(dev);
1338
1339 if (a == &dev_attr_badblocks.attr && !disk->bb)
1340 return 0;
1341 return a->mode;
1342}
1343
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001344static struct attribute_group disk_attr_group = {
1345 .attrs = disk_attrs,
Dan Williams9438b3e02017-04-27 14:46:26 -07001346 .is_visible = disk_visible,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001347};
1348
David Brownella4dbd672009-06-24 10:06:31 -07001349static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001350 &disk_attr_group,
1351 NULL
1352};
1353
Tejun Heo540eed52008-08-25 19:56:15 +09001354/**
1355 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1356 * @disk: disk to replace part_tbl for
1357 * @new_ptbl: new part_tbl to install
1358 *
1359 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1360 * original ptbl is freed using RCU callback.
1361 *
1362 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001363 * Matching bd_mutex locked or the caller is the only user of @disk.
Tejun Heo540eed52008-08-25 19:56:15 +09001364 */
1365static void disk_replace_part_tbl(struct gendisk *disk,
1366 struct disk_part_tbl *new_ptbl)
1367{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001368 struct disk_part_tbl *old_ptbl =
1369 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001370
1371 rcu_assign_pointer(disk->part_tbl, new_ptbl);
Jens Axboea6f23652008-10-24 12:52:42 +02001372
1373 if (old_ptbl) {
1374 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
Lai Jiangshan57bdfbf2011-03-18 11:42:58 +08001375 kfree_rcu(old_ptbl, rcu_head);
Jens Axboea6f23652008-10-24 12:52:42 +02001376 }
Tejun Heo540eed52008-08-25 19:56:15 +09001377}
1378
1379/**
1380 * disk_expand_part_tbl - expand disk->part_tbl
1381 * @disk: disk to expand part_tbl for
1382 * @partno: expand such that this partno can fit in
1383 *
1384 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1385 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1386 *
1387 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001388 * Matching bd_mutex locked or the caller is the only user of @disk.
1389 * Might sleep.
Tejun Heo540eed52008-08-25 19:56:15 +09001390 *
1391 * RETURNS:
1392 * 0 on success, -errno on failure.
1393 */
1394int disk_expand_part_tbl(struct gendisk *disk, int partno)
1395{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001396 struct disk_part_tbl *old_ptbl =
1397 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001398 struct disk_part_tbl *new_ptbl;
1399 int len = old_ptbl ? old_ptbl->len : 0;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001400 int i, target;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001401
1402 /*
1403 * check for int overflow, since we can get here from blkpg_ioctl()
1404 * with a user passed 'partno'.
1405 */
1406 target = partno + 1;
1407 if (target < 0)
1408 return -EINVAL;
Tejun Heo540eed52008-08-25 19:56:15 +09001409
1410 /* disk_max_parts() is zero during initialization, ignore if so */
1411 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1412 return -EINVAL;
1413
1414 if (target <= len)
1415 return 0;
1416
Gustavo A. R. Silva78b90a22019-05-31 13:47:54 -05001417 new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
1418 disk->node_id);
Tejun Heo540eed52008-08-25 19:56:15 +09001419 if (!new_ptbl)
1420 return -ENOMEM;
1421
Tejun Heo540eed52008-08-25 19:56:15 +09001422 new_ptbl->len = target;
1423
1424 for (i = 0; i < len; i++)
1425 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1426
1427 disk_replace_part_tbl(disk, new_ptbl);
1428 return 0;
1429}
1430
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001431/**
1432 * disk_release - releases all allocated resources of the gendisk
1433 * @dev: the device representing this disk
1434 *
1435 * This function releases all allocated resources of the gendisk.
1436 *
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001437 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1438 * assigned. Since the request_queue sits on top of the gendisk for these
1439 * drivers we also call blk_put_queue() for them, and we expect the
1440 * request_queue refcount to reach 0 at this point, and so the request_queue
1441 * will also be freed prior to the disk.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001442 *
1443 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001444 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001445static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001447 struct gendisk *disk = dev_to_disk(dev);
1448
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001449 might_sleep();
1450
Keith Busch2da78092014-08-26 09:05:36 -06001451 blk_free_devt(dev->devt);
Tejun Heo77ea8872010-12-08 20:57:37 +01001452 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +09001454 disk_replace_part_tbl(disk, NULL);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001455 bdput(disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001456 if (disk->queue)
1457 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 kfree(disk);
1459}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001460struct class block_class = {
1461 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462};
1463
Kay Sievers3c2670e2013-04-06 09:56:00 -07001464static char *block_devnode(struct device *dev, umode_t *mode,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001465 kuid_t *uid, kgid_t *gid)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001466{
1467 struct gendisk *disk = dev_to_disk(dev);
1468
Christoph Hellwig348e1142020-03-27 09:07:17 +01001469 if (disk->fops->devnode)
1470 return disk->fops->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001471 return NULL;
1472}
1473
Boris Burkovef45fe42020-06-01 13:12:05 -07001474const struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001475 .name = "disk",
1476 .groups = disk_attr_groups,
1477 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001478 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479};
1480
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001481#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001482/*
1483 * aggregate disk stat collector. Uses the same stats that the sysfs
1484 * entries do, above, but makes them available through one seq_file.
1485 *
1486 * The output looks suspiciously like /proc/partitions with a bunch of
1487 * extra fields.
1488 */
1489static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001492 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001493 struct block_device *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 char buf[BDEVNAME_SIZE];
Mikulas Patockae016b782018-12-06 11:41:21 -05001495 unsigned int inflight;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001496 struct disk_stats stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001499 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001500 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 " rio rmerge rsect ruse wio wmerge "
1502 "wsect wuse running use aveq"
1503 "\n\n");
1504 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001505
Tejun Heo71982a42009-04-17 08:34:48 +02001506 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001507 while ((hd = disk_part_iter_next(&piter))) {
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001508 part_stat_read_all(hd->bd_part, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001509 if (queue_is_mq(gp->queue))
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001510 inflight = blk_mq_in_flight(gp->queue, hd);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001511 else
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001512 inflight = part_in_flight(hd);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001513
Michael Callahanbdca3c82018-07-18 04:47:40 -07001514 seq_printf(seqf, "%4d %7d %s "
1515 "%lu %lu %lu %u "
1516 "%lu %lu %lu %u "
1517 "%u %u %u "
Konstantin Khlebnikovb6866312019-11-21 13:40:26 +03001518 "%lu %lu %lu %u "
1519 "%lu %u"
1520 "\n",
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001521 MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
1522 disk_name(gp, hd->bd_partno, buf),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001523 stat.ios[STAT_READ],
1524 stat.merges[STAT_READ],
1525 stat.sectors[STAT_READ],
1526 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1527 NSEC_PER_MSEC),
1528 stat.ios[STAT_WRITE],
1529 stat.merges[STAT_WRITE],
1530 stat.sectors[STAT_WRITE],
1531 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1532 NSEC_PER_MSEC),
Mikulas Patockae016b782018-12-06 11:41:21 -05001533 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001534 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001535 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1536 stat.nsecs[STAT_WRITE] +
1537 stat.nsecs[STAT_DISCARD] +
1538 stat.nsecs[STAT_FLUSH],
1539 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001540 stat.ios[STAT_DISCARD],
1541 stat.merges[STAT_DISCARD],
1542 stat.sectors[STAT_DISCARD],
1543 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1544 NSEC_PER_MSEC),
1545 stat.ios[STAT_FLUSH],
1546 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1547 NSEC_PER_MSEC)
Jerome Marchand28f39d52008-02-08 11:04:56 +01001548 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001550 disk_part_iter_exit(&piter);
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return 0;
1553}
1554
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001555static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001556 .start = disk_seqf_start,
1557 .next = disk_seqf_next,
1558 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 .show = diskstats_show
1560};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001561
1562static int __init proc_genhd_init(void)
1563{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001564 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1565 proc_create_seq("partitions", 0, NULL, &partitions_op);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001566 return 0;
1567}
1568module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001569#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Tejun Heocf771cb2008-09-03 09:01:09 +02001571dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001572{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001573 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001574 struct class_dev_iter iter;
1575 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001576
Tejun Heodef4e382008-09-03 08:57:12 +02001577 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1578 while ((dev = class_dev_iter_next(&iter))) {
1579 struct gendisk *disk = dev_to_disk(dev);
Tejun Heo548b10e2008-08-29 09:01:47 +02001580 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001581
Kay Sievers3ada8b72009-01-06 10:44:43 -08001582 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001583 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001584
Neil Brown41b8c852009-02-18 10:33:59 +01001585 if (partno < disk->minors) {
1586 /* We need to return the right devno, even
1587 * if the partition doesn't exist yet.
1588 */
1589 devt = MKDEV(MAJOR(dev->devt),
1590 MINOR(dev->devt) + partno);
1591 break;
1592 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001593 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001594 if (part) {
Tejun Heof331c022008-09-03 09:01:48 +02001595 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001596 disk_put_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001597 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001598 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001599 disk_put_part(part);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001600 }
Tejun Heodef4e382008-09-03 08:57:12 +02001601 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001602 return devt;
1603}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001604
Byungchul Parke319e1f2017-10-25 17:56:05 +09001605struct gendisk *__alloc_disk_node(int minors, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001606{
1607 struct gendisk *disk;
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001608 struct disk_part_tbl *ptbl;
Christoph Lameter19460892005-06-23 00:08:19 -07001609
Christoph Hellwigde65b012017-08-23 19:10:29 +02001610 if (minors > DISK_MAX_PARTS) {
1611 printk(KERN_ERR
Randy Dunlap7fb52622017-11-18 17:43:38 -08001612 "block: can't allocate more than %d partitions\n",
Christoph Hellwigde65b012017-08-23 19:10:29 +02001613 DISK_MAX_PARTS);
1614 minors = DISK_MAX_PARTS;
1615 }
Christoph Lameter19460892005-06-23 00:08:19 -07001616
Joe Perchesc1b511e2013-08-29 15:21:42 -07001617 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001618 if (!disk)
1619 return NULL;
Jens Axboe6c23a962011-01-07 08:43:37 +01001620
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001621 disk->part0 = bdev_alloc(disk, 0);
1622 if (!disk->part0)
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001623 goto out_free_disk;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001624
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001625 disk->node_id = node_id;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001626 if (disk_expand_part_tbl(disk, 0))
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +01001627 goto out_bdput;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001628
1629 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001630 rcu_assign_pointer(ptbl->part[0], disk->part0);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001631
1632 disk->minors = minors;
1633 rand_initialize_disk(disk);
1634 disk_to_dev(disk)->class = &block_class;
1635 disk_to_dev(disk)->type = &disk_type;
1636 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return disk;
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001638
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001639out_bdput:
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001640 bdput(disk->part0);
Christoph Hellwigf93af2a2020-08-31 20:02:37 +02001641out_free_disk:
1642 kfree(disk);
1643 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
Byungchul Parke319e1f2017-10-25 17:56:05 +09001645EXPORT_SYMBOL(__alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001647/**
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001648 * put_disk - decrements the gendisk refcount
Randy Dunlap0d20dcc2020-07-30 18:42:30 -07001649 * @disk: the struct gendisk to decrement the refcount for
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001650 *
1651 * This decrements the refcount for the struct gendisk. When this reaches 0
1652 * we'll have disk_release() called.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001653 *
1654 * Context: Any context, but the last reference must not be dropped from
1655 * atomic context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001656 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657void put_disk(struct gendisk *disk)
1658{
1659 if (disk)
Christoph Hellwigefdc41c2020-11-10 07:25:37 +01001660 put_device(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662EXPORT_SYMBOL(put_disk);
1663
Hannes Reineckee3264a42009-07-28 09:13:13 +02001664static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1665{
1666 char event[] = "DISK_RO=1";
1667 char *envp[] = { event, NULL };
1668
1669 if (!ro)
1670 event[8] = '0';
1671 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1672}
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674void set_disk_ro(struct gendisk *disk, int flag)
1675{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001676 struct disk_part_iter piter;
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001677 struct block_device *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001678
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001679 if (disk->part0->bd_read_only != flag) {
Hannes Reineckee3264a42009-07-28 09:13:13 +02001680 set_disk_ro_uevent(disk, flag);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001681 disk->part0->bd_read_only = flag;
Hannes Reineckee3264a42009-07-28 09:13:13 +02001682 }
1683
1684 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001685 while ((part = disk_part_iter_next(&piter)))
Christoph Hellwigad1eaa52020-11-24 09:52:59 +01001686 part->bd_read_only = flag;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001687 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
1690EXPORT_SYMBOL(set_disk_ro);
1691
1692int bdev_read_only(struct block_device *bdev)
1693{
1694 if (!bdev)
1695 return 0;
Christoph Hellwig83950d32020-11-23 16:36:02 +01001696 return bdev->bd_read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699EXPORT_SYMBOL(bdev_read_only);
1700
Tejun Heo77ea8872010-12-08 20:57:37 +01001701/*
1702 * Disk events - monitor disk events like media change and eject request.
1703 */
1704struct disk_events {
1705 struct list_head node; /* all disk_event's */
1706 struct gendisk *disk; /* the associated disk */
1707 spinlock_t lock;
1708
Tejun Heofdd514e2011-06-09 20:43:59 +02001709 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001710 int block; /* event blocking depth */
1711 unsigned int pending; /* events already sent out */
1712 unsigned int clearing; /* events being cleared */
1713
1714 long poll_msecs; /* interval, -1 for default */
1715 struct delayed_work dwork;
1716};
1717
1718static const char *disk_events_strs[] = {
1719 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1720 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1721};
1722
1723static char *disk_uevents[] = {
1724 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1725 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1726};
1727
1728/* list of all disk_events */
1729static DEFINE_MUTEX(disk_events_mutex);
1730static LIST_HEAD(disk_events);
1731
1732/* disable in-kernel polling by default */
Wei Tang1fe8f342015-11-24 09:58:46 +08001733static unsigned long disk_events_dfl_poll_msecs;
Tejun Heo77ea8872010-12-08 20:57:37 +01001734
1735static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1736{
1737 struct disk_events *ev = disk->ev;
1738 long intv_msecs = 0;
1739
1740 /*
1741 * If device-specific poll interval is set, always use it. If
Martin Wilck673387a2019-03-27 14:51:01 +01001742 * the default is being used, poll if the POLL flag is set.
Tejun Heo77ea8872010-12-08 20:57:37 +01001743 */
1744 if (ev->poll_msecs >= 0)
1745 intv_msecs = ev->poll_msecs;
Martin Wilckc92e2f02019-03-27 14:51:02 +01001746 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
Tejun Heo77ea8872010-12-08 20:57:37 +01001747 intv_msecs = disk_events_dfl_poll_msecs;
1748
1749 return msecs_to_jiffies(intv_msecs);
1750}
1751
Tejun Heoc3af54a2011-06-09 20:43:55 +02001752/**
1753 * disk_block_events - block and flush disk event checking
1754 * @disk: disk to block events for
1755 *
1756 * On return from this function, it is guaranteed that event checking
1757 * isn't in progress and won't happen until unblocked by
1758 * disk_unblock_events(). Events blocking is counted and the actual
1759 * unblocking happens after the matching number of unblocks are done.
1760 *
1761 * Note that this intentionally does not block event checking from
1762 * disk_clear_events().
1763 *
1764 * CONTEXT:
1765 * Might sleep.
1766 */
1767void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001768{
1769 struct disk_events *ev = disk->ev;
1770 unsigned long flags;
1771 bool cancel;
1772
Tejun Heoc3af54a2011-06-09 20:43:55 +02001773 if (!ev)
1774 return;
1775
Tejun Heofdd514e2011-06-09 20:43:59 +02001776 /*
1777 * Outer mutex ensures that the first blocker completes canceling
1778 * the event work before further blockers are allowed to finish.
1779 */
1780 mutex_lock(&ev->block_mutex);
1781
Tejun Heo77ea8872010-12-08 20:57:37 +01001782 spin_lock_irqsave(&ev->lock, flags);
1783 cancel = !ev->block++;
1784 spin_unlock_irqrestore(&ev->lock, flags);
1785
Tejun Heoc3af54a2011-06-09 20:43:55 +02001786 if (cancel)
1787 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001788
1789 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001790}
1791
1792static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1793{
1794 struct disk_events *ev = disk->ev;
1795 unsigned long intv;
1796 unsigned long flags;
1797
1798 spin_lock_irqsave(&ev->lock, flags);
1799
1800 if (WARN_ON_ONCE(ev->block <= 0))
1801 goto out_unlock;
1802
1803 if (--ev->block)
1804 goto out_unlock;
1805
Tejun Heo77ea8872010-12-08 20:57:37 +01001806 intv = disk_events_poll_jiffies(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001807 if (check_now)
Viresh Kumar695588f2013-04-24 17:12:56 +05301808 queue_delayed_work(system_freezable_power_efficient_wq,
1809 &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001810 else if (intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301811 queue_delayed_work(system_freezable_power_efficient_wq,
1812 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001813out_unlock:
1814 spin_unlock_irqrestore(&ev->lock, flags);
1815}
1816
1817/**
Tejun Heo77ea8872010-12-08 20:57:37 +01001818 * disk_unblock_events - unblock disk event checking
1819 * @disk: disk to unblock events for
1820 *
1821 * Undo disk_block_events(). When the block count reaches zero, it
1822 * starts events polling if configured.
1823 *
1824 * CONTEXT:
1825 * Don't care. Safe to call from irq context.
1826 */
1827void disk_unblock_events(struct gendisk *disk)
1828{
1829 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01001830 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001831}
1832
1833/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02001834 * disk_flush_events - schedule immediate event checking and flushing
1835 * @disk: disk to check and flush events for
1836 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01001837 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02001838 * Schedule immediate event checking on @disk if not blocked. Events in
1839 * @mask are scheduled to be cleared from the driver. Note that this
1840 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01001841 *
1842 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02001843 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01001844 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001845void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001846{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001847 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001848
1849 if (!ev)
1850 return;
1851
Tejun Heo85ef06d2011-07-01 16:17:47 +02001852 spin_lock_irq(&ev->lock);
1853 ev->clearing |= mask;
Tejun Heo41f63c52012-08-03 10:30:47 -07001854 if (!ev->block)
Viresh Kumar695588f2013-04-24 17:12:56 +05301855 mod_delayed_work(system_freezable_power_efficient_wq,
1856 &ev->dwork, 0);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001857 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001858}
Tejun Heo77ea8872010-12-08 20:57:37 +01001859
1860/**
1861 * disk_clear_events - synchronously check, clear and return pending events
1862 * @disk: disk to fetch and clear events from
Masanari Iidada3dae52014-09-09 01:27:23 +09001863 * @mask: mask of events to be fetched and cleared
Tejun Heo77ea8872010-12-08 20:57:37 +01001864 *
1865 * Disk events are synchronously checked and pending events in @mask
1866 * are cleared and returned. This ignores the block count.
1867 *
1868 * CONTEXT:
1869 * Might sleep.
1870 */
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001871static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001872{
Tejun Heo77ea8872010-12-08 20:57:37 +01001873 struct disk_events *ev = disk->ev;
1874 unsigned int pending;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001875 unsigned int clearing = mask;
Tejun Heo77ea8872010-12-08 20:57:37 +01001876
Christoph Hellwiga564e232020-07-08 14:25:41 +02001877 if (!ev)
Tejun Heo77ea8872010-12-08 20:57:37 +01001878 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001879
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001880 disk_block_events(disk);
1881
1882 /*
1883 * store the union of mask and ev->clearing on the stack so that the
1884 * race with disk_flush_events does not cause ambiguity (ev->clearing
1885 * can still be modified even if events are blocked).
1886 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001887 spin_lock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001888 clearing |= ev->clearing;
1889 ev->clearing = 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001890 spin_unlock_irq(&ev->lock);
1891
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001892 disk_check_events(ev, &clearing);
Derek Basehoreaea24a82012-12-18 12:27:18 -08001893 /*
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001894 * if ev->clearing is not 0, the disk_flush_events got called in the
1895 * middle of this function, so we want to run the workfn without delay.
Derek Basehoreaea24a82012-12-18 12:27:18 -08001896 */
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001897 __disk_unblock_events(disk, ev->clearing ? true : false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001898
1899 /* then, fetch and clear pending events */
1900 spin_lock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001901 pending = ev->pending & mask;
1902 ev->pending &= ~mask;
1903 spin_unlock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001904 WARN_ON_ONCE(clearing & mask);
Tejun Heo77ea8872010-12-08 20:57:37 +01001905
1906 return pending;
1907}
1908
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001909/**
1910 * bdev_check_media_change - check if a removable media has been changed
1911 * @bdev: block device to check
1912 *
1913 * Check whether a removable media has been changed, and attempt to free all
1914 * dentries and inodes and invalidates all block device page cache entries in
1915 * that case.
1916 *
1917 * Returns %true if the block device changed, or %false if not.
1918 */
1919bool bdev_check_media_change(struct block_device *bdev)
1920{
1921 unsigned int events;
1922
1923 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1924 DISK_EVENT_EJECT_REQUEST);
1925 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1926 return false;
1927
1928 if (__invalidate_device(bdev, true))
1929 pr_warn("VFS: busy inodes on changed media %s\n",
1930 bdev->bd_disk->disk_name);
Christoph Hellwig38430f02020-09-21 09:19:45 +02001931 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Christoph Hellwig95f6f3a2020-09-08 16:53:29 +02001932 return true;
1933}
1934EXPORT_SYMBOL(bdev_check_media_change);
1935
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001936/*
1937 * Separate this part out so that a different pointer for clearing_ptr can be
1938 * passed in for disk_clear_events.
1939 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001940static void disk_events_workfn(struct work_struct *work)
1941{
1942 struct delayed_work *dwork = to_delayed_work(work);
1943 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001944
1945 disk_check_events(ev, &ev->clearing);
1946}
1947
1948static void disk_check_events(struct disk_events *ev,
1949 unsigned int *clearing_ptr)
1950{
Tejun Heo77ea8872010-12-08 20:57:37 +01001951 struct gendisk *disk = ev->disk;
1952 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001953 unsigned int clearing = *clearing_ptr;
Tejun Heo77ea8872010-12-08 20:57:37 +01001954 unsigned int events;
1955 unsigned long intv;
1956 int nr_events = 0, i;
1957
1958 /* check events */
1959 events = disk->fops->check_events(disk, clearing);
1960
1961 /* accumulate pending events and schedule next poll if necessary */
1962 spin_lock_irq(&ev->lock);
1963
1964 events &= ~ev->pending;
1965 ev->pending |= events;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001966 *clearing_ptr &= ~clearing;
Tejun Heo77ea8872010-12-08 20:57:37 +01001967
1968 intv = disk_events_poll_jiffies(disk);
1969 if (!ev->block && intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301970 queue_delayed_work(system_freezable_power_efficient_wq,
1971 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001972
1973 spin_unlock_irq(&ev->lock);
1974
Tejun Heo7c88a162011-04-21 19:43:58 +02001975 /*
1976 * Tell userland about new events. Only the events listed in
Martin Wilckc92e2f02019-03-27 14:51:02 +01001977 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1978 * is set. Otherwise, events are processed internally but never
1979 * get reported to userland.
Tejun Heo7c88a162011-04-21 19:43:58 +02001980 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001981 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Martin Wilckc92e2f02019-03-27 14:51:02 +01001982 if ((events & disk->events & (1 << i)) &&
1983 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
Tejun Heo77ea8872010-12-08 20:57:37 +01001984 envp[nr_events++] = disk_uevents[i];
1985
1986 if (nr_events)
1987 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1988}
1989
1990/*
1991 * A disk events enabled device has the following sysfs nodes under
1992 * its /sys/block/X/ directory.
1993 *
1994 * events : list of all supported events
1995 * events_async : list of events which can be detected w/o polling
Martin Wilck673387a2019-03-27 14:51:01 +01001996 * (always empty, only for backwards compatibility)
Tejun Heo77ea8872010-12-08 20:57:37 +01001997 * events_poll_msecs : polling interval, 0: disable, -1: system default
1998 */
1999static ssize_t __disk_events_show(unsigned int events, char *buf)
2000{
2001 const char *delim = "";
2002 ssize_t pos = 0;
2003 int i;
2004
2005 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
2006 if (events & (1 << i)) {
2007 pos += sprintf(buf + pos, "%s%s",
2008 delim, disk_events_strs[i]);
2009 delim = " ";
2010 }
2011 if (pos)
2012 pos += sprintf(buf + pos, "\n");
2013 return pos;
2014}
2015
2016static ssize_t disk_events_show(struct device *dev,
2017 struct device_attribute *attr, char *buf)
2018{
2019 struct gendisk *disk = dev_to_disk(dev);
2020
Martin Wilckc92e2f02019-03-27 14:51:02 +01002021 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
2022 return 0;
2023
Tejun Heo77ea8872010-12-08 20:57:37 +01002024 return __disk_events_show(disk->events, buf);
2025}
2026
2027static ssize_t disk_events_async_show(struct device *dev,
2028 struct device_attribute *attr, char *buf)
2029{
Martin Wilck673387a2019-03-27 14:51:01 +01002030 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01002031}
2032
2033static ssize_t disk_events_poll_msecs_show(struct device *dev,
2034 struct device_attribute *attr,
2035 char *buf)
2036{
2037 struct gendisk *disk = dev_to_disk(dev);
2038
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002039 if (!disk->ev)
2040 return sprintf(buf, "-1\n");
2041
Tejun Heo77ea8872010-12-08 20:57:37 +01002042 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
2043}
2044
2045static ssize_t disk_events_poll_msecs_store(struct device *dev,
2046 struct device_attribute *attr,
2047 const char *buf, size_t count)
2048{
2049 struct gendisk *disk = dev_to_disk(dev);
2050 long intv;
2051
2052 if (!count || !sscanf(buf, "%ld", &intv))
2053 return -EINVAL;
2054
2055 if (intv < 0 && intv != -1)
2056 return -EINVAL;
2057
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002058 if (!disk->ev)
2059 return -ENODEV;
2060
Tejun Heoc3af54a2011-06-09 20:43:55 +02002061 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01002062 disk->ev->poll_msecs = intv;
2063 __disk_unblock_events(disk, true);
2064
2065 return count;
2066}
2067
Joe Perches5657a812018-05-24 13:38:59 -06002068static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
2069static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
2070static const DEVICE_ATTR(events_poll_msecs, 0644,
Tejun Heo77ea8872010-12-08 20:57:37 +01002071 disk_events_poll_msecs_show,
2072 disk_events_poll_msecs_store);
2073
2074static const struct attribute *disk_events_attrs[] = {
2075 &dev_attr_events.attr,
2076 &dev_attr_events_async.attr,
2077 &dev_attr_events_poll_msecs.attr,
2078 NULL,
2079};
2080
2081/*
2082 * The default polling interval can be specified by the kernel
2083 * parameter block.events_dfl_poll_msecs which defaults to 0
2084 * (disable). This can also be modified runtime by writing to
Akinobu Mita1624b0b2019-07-16 21:59:35 +09002085 * /sys/module/block/parameters/events_dfl_poll_msecs.
Tejun Heo77ea8872010-12-08 20:57:37 +01002086 */
2087static int disk_events_set_dfl_poll_msecs(const char *val,
2088 const struct kernel_param *kp)
2089{
2090 struct disk_events *ev;
2091 int ret;
2092
2093 ret = param_set_ulong(val, kp);
2094 if (ret < 0)
2095 return ret;
2096
2097 mutex_lock(&disk_events_mutex);
2098
2099 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02002100 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01002101
2102 mutex_unlock(&disk_events_mutex);
2103
2104 return 0;
2105}
2106
2107static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
2108 .set = disk_events_set_dfl_poll_msecs,
2109 .get = param_get_ulong,
2110};
2111
2112#undef MODULE_PARAM_PREFIX
2113#define MODULE_PARAM_PREFIX "block."
2114
2115module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
2116 &disk_events_dfl_poll_msecs, 0644);
2117
2118/*
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002119 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
Tejun Heo77ea8872010-12-08 20:57:37 +01002120 */
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002121static void disk_alloc_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01002122{
2123 struct disk_events *ev;
2124
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002125 if (!disk->fops->check_events || !disk->events)
Tejun Heo77ea8872010-12-08 20:57:37 +01002126 return;
2127
2128 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
2129 if (!ev) {
2130 pr_warn("%s: failed to initialize events\n", disk->disk_name);
2131 return;
2132 }
2133
Tejun Heo77ea8872010-12-08 20:57:37 +01002134 INIT_LIST_HEAD(&ev->node);
2135 ev->disk = disk;
2136 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02002137 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01002138 ev->block = 1;
2139 ev->poll_msecs = -1;
2140 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2141
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002142 disk->ev = ev;
2143}
2144
2145static void disk_add_events(struct gendisk *disk)
2146{
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002147 /* FIXME: error handling */
2148 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2149 pr_warn("%s: failed to create sysfs files for events\n",
2150 disk->disk_name);
2151
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002152 if (!disk->ev)
2153 return;
2154
Tejun Heo77ea8872010-12-08 20:57:37 +01002155 mutex_lock(&disk_events_mutex);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002156 list_add_tail(&disk->ev->node, &disk_events);
Tejun Heo77ea8872010-12-08 20:57:37 +01002157 mutex_unlock(&disk_events_mutex);
2158
2159 /*
2160 * Block count is initialized to 1 and the following initial
2161 * unblock kicks it into action.
2162 */
2163 __disk_unblock_events(disk, true);
2164}
2165
2166static void disk_del_events(struct gendisk *disk)
2167{
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002168 if (disk->ev) {
2169 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01002170
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002171 mutex_lock(&disk_events_mutex);
2172 list_del_init(&disk->ev->node);
2173 mutex_unlock(&disk_events_mutex);
2174 }
Tejun Heo77ea8872010-12-08 20:57:37 +01002175
2176 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2177}
2178
2179static void disk_release_events(struct gendisk *disk)
2180{
2181 /* the block count should be 1 from disk_del_events() */
2182 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
2183 kfree(disk->ev);
2184}