blob: 60ae4e1b4d3877b0869f77063cb215f3b6c0b737 [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>
20#include <linux/kobj_map.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080021#include <linux/mutex.h>
Tejun Heobcce3de2008-08-25 19:47:22 +090022#include <linux/idr.h>
Tejun Heo77ea8872010-12-08 20:57:37 +010023#include <linux/log2.h>
Ming Lei25e823c2013-02-22 16:34:13 -080024#include <linux/pm_runtime.h>
Vishal Verma99e66082016-01-09 08:36:51 -080025#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Adrian Bunkff889722008-03-04 11:23:45 +010027#include "blk.h"
28
Kay Sieversedfaa7c2007-05-21 22:08:01 +020029static DEFINE_MUTEX(block_class_lock);
Christoph Hellwig31eb6182020-03-25 16:48:35 +010030static struct kobject *block_depr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Tejun Heobcce3de2008-08-25 19:47:22 +090032/* for extended dynamic devt allocation, currently only one major is used */
Tejun Heoce23bba2013-02-27 17:03:56 -080033#define NR_EXT_DEVT (1 << MINORBITS)
Tejun Heobcce3de2008-08-25 19:47:22 +090034
Keith Busch2da78092014-08-26 09:05:36 -060035/* For extended devt allocation. ext_devt_lock prevents look up
Tejun Heobcce3de2008-08-25 19:47:22 +090036 * results from going away underneath its user.
37 */
Keith Busch2da78092014-08-26 09:05:36 -060038static DEFINE_SPINLOCK(ext_devt_lock);
Tejun Heobcce3de2008-08-25 19:47:22 +090039static DEFINE_IDR(ext_devt_idr);
40
Bart Van Asscheedf8ff52017-06-20 11:15:48 -070041static const struct device_type disk_type;
Adrian Bunk1826ead2008-03-04 11:23:46 +010042
Derek Basehore12c2bdb2012-12-18 12:27:20 -080043static void disk_check_events(struct disk_events *ev,
44 unsigned int *clearing_ptr);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +010045static void disk_alloc_events(struct gendisk *disk);
Tejun Heo77ea8872010-12-08 20:57:37 +010046static void disk_add_events(struct gendisk *disk);
47static void disk_del_events(struct gendisk *disk);
48static void disk_release_events(struct gendisk *disk);
49
Balbir Singhe598a722020-03-13 05:30:05 +000050/*
51 * Set disk capacity and notify if the size is not currently
52 * zero and will not be set to zero
53 */
54void set_capacity_revalidate_and_notify(struct gendisk *disk, sector_t size,
55 bool revalidate)
56{
57 sector_t capacity = get_capacity(disk);
58
59 set_capacity(disk, size);
60
61 if (revalidate)
62 revalidate_disk(disk);
63
64 if (capacity != size && capacity != 0 && size != 0) {
65 char *envp[] = { "RESIZE=1", NULL };
66
67 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
68 }
69}
70
71EXPORT_SYMBOL_GPL(set_capacity_revalidate_and_notify);
72
Christoph Hellwig5cbd28e2020-03-24 08:25:12 +010073/*
74 * Format the device name of the indicated disk into the supplied buffer and
75 * return a pointer to that same buffer for convenience.
76 */
77char *disk_name(struct gendisk *hd, int partno, char *buf)
78{
79 if (!partno)
80 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
81 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
82 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
83 else
84 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
85
86 return buf;
87}
88
89const char *bdevname(struct block_device *bdev, char *buf)
90{
91 return disk_name(bdev->bd_disk, bdev->bd_part->partno, buf);
92}
93EXPORT_SYMBOL(bdevname);
Balbir Singhe598a722020-03-13 05:30:05 +000094
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +030095static void part_stat_read_all(struct hd_struct *part, struct disk_stats *stat)
96{
97 int cpu;
98
99 memset(stat, 0, sizeof(struct disk_stats));
100 for_each_possible_cpu(cpu) {
101 struct disk_stats *ptr = per_cpu_ptr(part->dkstats, cpu);
102 int group;
103
104 for (group = 0; group < NR_STAT_GROUPS; group++) {
105 stat->nsecs[group] += ptr->nsecs[group];
106 stat->sectors[group] += ptr->sectors[group];
107 stat->ios[group] += ptr->ios[group];
108 stat->merges[group] += ptr->merges[group];
109 }
110
111 stat->io_ticks += ptr->io_ticks;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300112 }
113}
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300114
Christoph Hellwig60057712020-03-25 16:48:36 +0100115static unsigned int part_in_flight(struct request_queue *q,
116 struct hd_struct *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600117{
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200118 unsigned int inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500119 int cpu;
120
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500121 for_each_possible_cpu(cpu) {
Mikulas Patockae016b782018-12-06 11:41:21 -0500122 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
123 part_stat_local_read_cpu(part, in_flight[1], cpu);
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500124 }
Mikulas Patockae016b782018-12-06 11:41:21 -0500125 if ((int)inflight < 0)
126 inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500127
Mikulas Patockae016b782018-12-06 11:41:21 -0500128 return inflight;
Jens Axboef299b7c2017-08-08 17:51:45 -0600129}
130
Christoph Hellwig60057712020-03-25 16:48:36 +0100131static void part_in_flight_rw(struct request_queue *q, struct hd_struct *part,
132 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700133{
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500134 int cpu;
135
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500136 inflight[0] = 0;
137 inflight[1] = 0;
138 for_each_possible_cpu(cpu) {
139 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
140 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
141 }
142 if ((int)inflight[0] < 0)
143 inflight[0] = 0;
144 if ((int)inflight[1] < 0)
145 inflight[1] = 0;
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700146}
147
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200148struct hd_struct *__disk_get_part(struct gendisk *disk, int partno)
149{
150 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
151
152 if (unlikely(partno < 0 || partno >= ptbl->len))
153 return NULL;
154 return rcu_dereference(ptbl->part[partno]);
155}
156
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200157/**
158 * disk_get_part - get partition
159 * @disk: disk to look partition from
160 * @partno: partition number
161 *
162 * Look for partition @partno from @disk. If found, increment
163 * reference count and return it.
164 *
165 * CONTEXT:
166 * Don't care.
167 *
168 * RETURNS:
169 * Pointer to the found partition on success, NULL if not found.
170 */
171struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
172{
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200173 struct hd_struct *part;
Tejun Heo540eed52008-08-25 19:56:15 +0900174
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200175 rcu_read_lock();
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200176 part = __disk_get_part(disk, partno);
177 if (part)
178 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200179 rcu_read_unlock();
180
181 return part;
182}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200183
184/**
185 * disk_part_iter_init - initialize partition iterator
186 * @piter: iterator to initialize
187 * @disk: disk to iterate over
188 * @flags: DISK_PITER_* flags
189 *
190 * Initialize @piter so that it iterates over partitions of @disk.
191 *
192 * CONTEXT:
193 * Don't care.
194 */
195void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
196 unsigned int flags)
197{
Tejun Heo540eed52008-08-25 19:56:15 +0900198 struct disk_part_tbl *ptbl;
199
200 rcu_read_lock();
201 ptbl = rcu_dereference(disk->part_tbl);
202
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200203 piter->disk = disk;
204 piter->part = NULL;
205
206 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +0900207 piter->idx = ptbl->len - 1;
Tejun Heo71982a42009-04-17 08:34:48 +0200208 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200209 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200210 else
211 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200212
213 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900214
215 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200216}
217EXPORT_SYMBOL_GPL(disk_part_iter_init);
218
219/**
220 * disk_part_iter_next - proceed iterator to the next partition and return it
221 * @piter: iterator of interest
222 *
223 * Proceed @piter to the next partition and return it.
224 *
225 * CONTEXT:
226 * Don't care.
227 */
228struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
229{
Tejun Heo540eed52008-08-25 19:56:15 +0900230 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200231 int inc, end;
232
233 /* put the last partition */
234 disk_put_part(piter->part);
235 piter->part = NULL;
236
Tejun Heo540eed52008-08-25 19:56:15 +0900237 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200238 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900239 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200240
241 /* determine iteration parameters */
242 if (piter->flags & DISK_PITER_REVERSE) {
243 inc = -1;
Tejun Heo71982a42009-04-17 08:34:48 +0200244 if (piter->flags & (DISK_PITER_INCL_PART0 |
245 DISK_PITER_INCL_EMPTY_PART0))
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200246 end = -1;
247 else
248 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200249 } else {
250 inc = 1;
Tejun Heo540eed52008-08-25 19:56:15 +0900251 end = ptbl->len;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200252 }
253
254 /* iterate to the next partition */
255 for (; piter->idx != end; piter->idx += inc) {
256 struct hd_struct *part;
257
Tejun Heo540eed52008-08-25 19:56:15 +0900258 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200259 if (!part)
260 continue;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200261 if (!part_nr_sects_read(part) &&
Tejun Heo71982a42009-04-17 08:34:48 +0200262 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
263 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
264 piter->idx == 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200265 continue;
266
Tejun Heoed9e1982008-08-25 19:56:05 +0900267 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200268 piter->part = part;
269 piter->idx += inc;
270 break;
271 }
272
273 rcu_read_unlock();
274
275 return piter->part;
276}
277EXPORT_SYMBOL_GPL(disk_part_iter_next);
278
279/**
280 * disk_part_iter_exit - finish up partition iteration
281 * @piter: iter of interest
282 *
283 * Called when iteration is over. Cleans up @piter.
284 *
285 * CONTEXT:
286 * Don't care.
287 */
288void disk_part_iter_exit(struct disk_part_iter *piter)
289{
290 disk_put_part(piter->part);
291 piter->part = NULL;
292}
293EXPORT_SYMBOL_GPL(disk_part_iter_exit);
294
Jens Axboea6f23652008-10-24 12:52:42 +0200295static inline int sector_in_part(struct hd_struct *part, sector_t sector)
296{
297 return part->start_sect <= sector &&
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200298 sector < part->start_sect + part_nr_sects_read(part);
Jens Axboea6f23652008-10-24 12:52:42 +0200299}
300
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200301/**
302 * disk_map_sector_rcu - map sector to partition
303 * @disk: gendisk of interest
304 * @sector: sector to map
305 *
306 * Find out which partition @sector maps to on @disk. This is
307 * primarily used for stats accounting.
308 *
309 * CONTEXT:
Ming Leib7d6c302020-05-08 16:17:55 +0800310 * RCU read locked. The returned partition pointer is always valid
Ming Lei27eb3af2020-05-08 16:17:58 +0800311 * because its refcount is grabbed except for part0, which lifetime
312 * is same with the disk.
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200313 *
314 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900315 * Found partition on success, part0 is returned if no partition matches
Ming Leib7d6c302020-05-08 16:17:55 +0800316 * or the matched partition is being deleted.
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200317 */
318struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
319{
Tejun Heo540eed52008-08-25 19:56:15 +0900320 struct disk_part_tbl *ptbl;
Jens Axboea6f23652008-10-24 12:52:42 +0200321 struct hd_struct *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200322 int i;
323
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200324 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900325 ptbl = rcu_dereference(disk->part_tbl);
326
Jens Axboea6f23652008-10-24 12:52:42 +0200327 part = rcu_dereference(ptbl->last_lookup);
Ming Leib7d6c302020-05-08 16:17:55 +0800328 if (part && sector_in_part(part, sector) && hd_struct_try_get(part))
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200329 goto out_unlock;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200330
Jens Axboea6f23652008-10-24 12:52:42 +0200331 for (i = 1; i < ptbl->len; i++) {
332 part = rcu_dereference(ptbl->part[i]);
333
334 if (part && sector_in_part(part, sector)) {
Ming Leib7d6c302020-05-08 16:17:55 +0800335 /*
336 * only live partition can be cached for lookup,
337 * so use-after-free on cached & deleting partition
338 * can be avoided
339 */
340 if (!hd_struct_try_get(part))
341 break;
Jens Axboea6f23652008-10-24 12:52:42 +0200342 rcu_assign_pointer(ptbl->last_lookup, part);
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200343 goto out_unlock;
Jens Axboea6f23652008-10-24 12:52:42 +0200344 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200345 }
Konstantin Khlebnikov8ab1d402020-05-27 07:24:17 +0200346
347 part = &disk->part0;
348out_unlock:
349 rcu_read_unlock();
350 return part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200351}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200352
Shin'ichiro Kawasakib53df2e2020-02-21 10:37:08 +0900353/**
354 * disk_has_partitions
355 * @disk: gendisk of interest
356 *
357 * Walk through the partition table and check if valid partition exists.
358 *
359 * CONTEXT:
360 * Don't care.
361 *
362 * RETURNS:
363 * True if the gendisk has at least one valid non-zero size partition.
364 * Otherwise false.
365 */
366bool disk_has_partitions(struct gendisk *disk)
367{
368 struct disk_part_tbl *ptbl;
369 int i;
370 bool ret = false;
371
372 rcu_read_lock();
373 ptbl = rcu_dereference(disk->part_tbl);
374
375 /* Iterate partitions skipping the whole device at index 0 */
376 for (i = 1; i < ptbl->len; i++) {
377 if (rcu_dereference(ptbl->part[i])) {
378 ret = true;
379 break;
380 }
381 }
382
383 rcu_read_unlock();
384
385 return ret;
386}
387EXPORT_SYMBOL_GPL(disk_has_partitions);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
390 * Can be deleted altogether. Later.
391 *
392 */
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600393#define BLKDEV_MAJOR_HASH_SIZE 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static struct blk_major_name {
395 struct blk_major_name *next;
396 int major;
397 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800398} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100401static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Joe Korty68eef3b2006-03-31 02:30:32 -0800403 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Joe Korty68eef3b2006-03-31 02:30:32 -0800406#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200407void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800408{
Joe Korty68eef3b2006-03-31 02:30:32 -0800409 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800410
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600411 mutex_lock(&block_class_lock);
412 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
413 if (dp->major == offset)
Tejun Heocf771cb2008-09-03 09:01:09 +0200414 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600415 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
Joe Korty68eef3b2006-03-31 02:30:32 -0800417#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100419/**
420 * register_blkdev - register a new block device
421 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800422 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
423 * @major = 0, try to allocate any unused major number.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100424 * @name: the name of the new block device as a zero terminated string
425 *
426 * The @name must be unique within the system.
427 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300428 * The return value depends on the @major input parameter:
429 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800430 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
431 * then the function returns zero on success, or a negative error code
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300432 * - if any unused major number was requested with @major = 0 parameter
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100433 * then the return value is the allocated major number in range
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800434 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
435 *
436 * See Documentation/admin-guide/devices.txt for the list of allocated
437 * major numbers.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439int register_blkdev(unsigned int major, const char *name)
440{
441 struct blk_major_name **n, *p;
442 int index, ret = 0;
443
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200444 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* temporary */
447 if (major == 0) {
448 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
449 if (major_names[index] == NULL)
450 break;
451 }
452
453 if (index == 0) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500454 printk("%s: failed to get major for %s\n",
455 __func__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 ret = -EBUSY;
457 goto out;
458 }
459 major = index;
460 ret = major;
461 }
462
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600463 if (major >= BLKDEV_MAJOR_MAX) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500464 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
465 __func__, major, BLKDEV_MAJOR_MAX-1, name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600466
467 ret = -EINVAL;
468 goto out;
469 }
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
472 if (p == NULL) {
473 ret = -ENOMEM;
474 goto out;
475 }
476
477 p->major = major;
478 strlcpy(p->name, name, sizeof(p->name));
479 p->next = NULL;
480 index = major_to_index(major);
481
482 for (n = &major_names[index]; *n; n = &(*n)->next) {
483 if ((*n)->major == major)
484 break;
485 }
486 if (!*n)
487 *n = p;
488 else
489 ret = -EBUSY;
490
491 if (ret < 0) {
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800492 printk("register_blkdev: cannot get major %u for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 major, name);
494 kfree(p);
495 }
496out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200497 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return ret;
499}
500
501EXPORT_SYMBOL(register_blkdev);
502
Akinobu Mitaf4480242007-07-17 04:03:47 -0700503void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct blk_major_name **n;
506 struct blk_major_name *p = NULL;
507 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200509 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 for (n = &major_names[index]; *n; n = &(*n)->next)
511 if ((*n)->major == major)
512 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700513 if (!*n || strcmp((*n)->name, name)) {
514 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700515 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 p = *n;
517 *n = p->next;
518 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200519 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523EXPORT_SYMBOL(unregister_blkdev);
524
525static struct kobj_map *bdev_map;
526
Tejun Heobcce3de2008-08-25 19:47:22 +0900527/**
Tejun Heo870d6652008-08-25 19:47:25 +0900528 * blk_mangle_minor - scatter minor numbers apart
529 * @minor: minor number to mangle
530 *
531 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
532 * is enabled. Mangling twice gives the original value.
533 *
534 * RETURNS:
535 * Mangled value.
536 *
537 * CONTEXT:
538 * Don't care.
539 */
540static int blk_mangle_minor(int minor)
541{
542#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
543 int i;
544
545 for (i = 0; i < MINORBITS / 2; i++) {
546 int low = minor & (1 << i);
547 int high = minor & (1 << (MINORBITS - 1 - i));
548 int distance = MINORBITS - 1 - 2 * i;
549
550 minor ^= low | high; /* clear both bits */
551 low <<= distance; /* swap the positions */
552 high >>= distance;
553 minor |= low | high; /* and set */
554 }
555#endif
556 return minor;
557}
558
559/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900560 * blk_alloc_devt - allocate a dev_t for a partition
561 * @part: partition to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900562 * @devt: out parameter for resulting dev_t
563 *
564 * Allocate a dev_t for block device.
565 *
566 * RETURNS:
567 * 0 on success, allocated dev_t is returned in *@devt. -errno on
568 * failure.
569 *
570 * CONTEXT:
571 * Might sleep.
572 */
573int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
574{
575 struct gendisk *disk = part_to_disk(part);
Tejun Heobab998d2013-02-27 17:03:57 -0800576 int idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900577
578 /* in consecutive minor range? */
579 if (part->partno < disk->minors) {
580 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
581 return 0;
582 }
583
584 /* allocate ext devt */
Keith Busch2da78092014-08-26 09:05:36 -0600585 idr_preload(GFP_KERNEL);
586
Dan Williams4d66e5e2015-06-10 23:47:14 -0400587 spin_lock_bh(&ext_devt_lock);
Keith Busch2da78092014-08-26 09:05:36 -0600588 idx = idr_alloc(&ext_devt_idr, part, 0, NR_EXT_DEVT, GFP_NOWAIT);
Dan Williams4d66e5e2015-06-10 23:47:14 -0400589 spin_unlock_bh(&ext_devt_lock);
Keith Busch2da78092014-08-26 09:05:36 -0600590
591 idr_preload_end();
Tejun Heobab998d2013-02-27 17:03:57 -0800592 if (idx < 0)
593 return idx == -ENOSPC ? -EBUSY : idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900594
Tejun Heo870d6652008-08-25 19:47:25 +0900595 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900596 return 0;
597}
598
599/**
600 * blk_free_devt - free a dev_t
601 * @devt: dev_t to free
602 *
603 * Free @devt which was allocated using blk_alloc_devt().
604 *
605 * CONTEXT:
606 * Might sleep.
607 */
608void blk_free_devt(dev_t devt)
609{
Tejun Heobcce3de2008-08-25 19:47:22 +0900610 if (devt == MKDEV(0, 0))
611 return;
612
613 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
Dan Williams4d66e5e2015-06-10 23:47:14 -0400614 spin_lock_bh(&ext_devt_lock);
Tejun Heo870d6652008-08-25 19:47:25 +0900615 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Dan Williams4d66e5e2015-06-10 23:47:14 -0400616 spin_unlock_bh(&ext_devt_lock);
Tejun Heobcce3de2008-08-25 19:47:22 +0900617 }
618}
619
Bart Van Assche33c826e2019-05-30 17:00:47 -0700620/*
621 * We invalidate devt by assigning NULL pointer for devt in idr.
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800622 */
623void blk_invalidate_devt(dev_t devt)
624{
625 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
626 spin_lock_bh(&ext_devt_lock);
627 idr_replace(&ext_devt_idr, NULL, blk_mangle_minor(MINOR(devt)));
628 spin_unlock_bh(&ext_devt_lock);
629 }
630}
631
Tejun Heo1f014292008-08-25 19:47:23 +0900632static char *bdevt_str(dev_t devt, char *buf)
633{
634 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
635 char tbuf[BDEVT_SIZE];
636 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
637 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
638 } else
639 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
640
641 return buf;
642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/*
645 * Register device numbers dev..(dev+range-1)
646 * range must be nonzero
647 * The hash chain is sorted on range, so that subranges can override.
648 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200649void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct kobject *(*probe)(dev_t, int *, void *),
651 int (*lock)(dev_t, void *), void *data)
652{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200653 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656EXPORT_SYMBOL(blk_register_region);
657
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200658void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200660 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663EXPORT_SYMBOL(blk_unregister_region);
664
Tejun Heocf771cb2008-09-03 09:01:09 +0200665static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200668
Tejun Heoed9e1982008-08-25 19:56:05 +0900669 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200672static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct gendisk *p = data;
675
Jan Kara3079c222018-02-26 13:01:38 +0100676 if (!get_disk_and_module(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -1;
678 return 0;
679}
680
Hannes Reineckefef912b2018-09-28 08:17:19 +0200681static void register_disk(struct device *parent, struct gendisk *disk,
682 const struct attribute_group **groups)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100683{
684 struct device *ddev = disk_to_dev(disk);
685 struct block_device *bdev;
686 struct disk_part_iter piter;
687 struct hd_struct *part;
688 int err;
689
Dan Williamse63a46b2016-06-15 18:17:27 -0700690 ddev->parent = parent;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100691
Kees Cookffc8b302013-07-03 15:01:14 -0700692 dev_set_name(ddev, "%s", disk->disk_name);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100693
694 /* delay uevents, until we scanned partition table */
695 dev_set_uevent_suppress(ddev, 1);
696
Hannes Reineckefef912b2018-09-28 08:17:19 +0200697 if (groups) {
698 WARN_ON(ddev->groups);
699 ddev->groups = groups;
700 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100701 if (device_add(ddev))
702 return;
703 if (!sysfs_deprecated) {
704 err = sysfs_create_link(block_depr, &ddev->kobj,
705 kobject_name(&ddev->kobj));
706 if (err) {
707 device_del(ddev);
708 return;
709 }
710 }
Ming Lei25e823c2013-02-22 16:34:13 -0800711
712 /*
713 * avoid probable deadlock caused by allocating memory with
714 * GFP_KERNEL in runtime_resume callback of its all ancestor
715 * devices
716 */
717 pm_runtime_set_memalloc_noio(ddev, true);
718
Tejun Heod2bf1b62010-12-08 20:57:36 +0100719 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
720 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
721
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300722 if (disk->flags & GENHD_FL_HIDDEN) {
723 dev_set_uevent_suppress(ddev, 0);
724 return;
725 }
726
Tejun Heod2bf1b62010-12-08 20:57:36 +0100727 /* No minors to use for partitions */
Tejun Heod27769e2011-08-23 20:01:04 +0200728 if (!disk_part_scan_enabled(disk))
Tejun Heod2bf1b62010-12-08 20:57:36 +0100729 goto exit;
730
731 /* No such device (e.g., media were just removed) */
732 if (!get_capacity(disk))
733 goto exit;
734
735 bdev = bdget_disk(disk, 0);
736 if (!bdev)
737 goto exit;
738
739 bdev->bd_invalidated = 1;
740 err = blkdev_get(bdev, FMODE_READ, NULL);
741 if (err < 0)
742 goto exit;
743 blkdev_put(bdev, FMODE_READ);
744
745exit:
746 /* announce disk after possible partitions are created */
747 dev_set_uevent_suppress(ddev, 0);
748 kobject_uevent(&ddev->kobj, KOBJ_ADD);
749
750 /* announce possible partitions */
751 disk_part_iter_init(&piter, disk, 0);
752 while ((part = disk_part_iter_next(&piter)))
753 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
754 disk_part_iter_exit(&piter);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300755
zhengbin4d7c1d32019-02-20 21:27:05 +0800756 if (disk->queue->backing_dev_info->dev) {
757 err = sysfs_create_link(&ddev->kobj,
758 &disk->queue->backing_dev_info->dev->kobj,
759 "bdi");
760 WARN_ON(err);
761 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/**
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500765 * __device_add_disk - add disk information to kernel list
Dan Williamse63a46b2016-06-15 18:17:27 -0700766 * @parent: parent device for the disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 * @disk: per-device partitioning information
Hannes Reineckefef912b2018-09-28 08:17:19 +0200768 * @groups: Additional per-device sysfs groups
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500769 * @register_queue: register the queue if set to true
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 *
771 * This function registers the partitioning information in @disk
772 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900773 *
774 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500776static void __device_add_disk(struct device *parent, struct gendisk *disk,
Hannes Reineckefef912b2018-09-28 08:17:19 +0200777 const struct attribute_group **groups,
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500778 bool register_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900780 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400781 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700782
Damien Le Moal737eb782019-09-05 18:51:33 +0900783 /*
784 * The disk queue should now be all set with enough information about
785 * the device for the elevator code to pick an adequate default
786 * elevator if one is needed, that is, for devices requesting queue
787 * registration.
788 */
789 if (register_queue)
790 elevator_init_mq(disk->queue);
791
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900792 /* minors == 0 indicates to use ext devt from part0 and should
793 * be accompanied with EXT_DEVT flag. Make sure all
794 * parameters make sense.
795 */
796 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300797 WARN_ON(!disk->minors &&
798 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900801
802 retval = blk_alloc_devt(&disk->part0, &devt);
803 if (retval) {
804 WARN_ON(1);
805 return;
806 }
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900807 disk->major = MAJOR(devt);
808 disk->first_minor = MINOR(devt);
809
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +0100810 disk_alloc_events(disk);
811
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300812 if (disk->flags & GENHD_FL_HIDDEN) {
813 /*
814 * Don't let hidden disks show up in /proc/partitions,
815 * and don't bother scanning for partitions either.
816 */
817 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
818 disk->flags |= GENHD_FL_NO_PART_SCAN;
819 } else {
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200820 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
821 struct device *dev = disk_to_dev(disk);
weiping zhang3a921682017-10-31 18:38:59 +0800822 int ret;
823
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300824 /* Register BDI before referencing it from bdev */
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200825 dev->devt = devt;
826 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
weiping zhang3a921682017-10-31 18:38:59 +0800827 WARN_ON(ret);
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200828 bdi_set_owner(bdi, dev);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300829 blk_register_region(disk_devt(disk), disk->minors, NULL,
830 exact_match, exact_lock, disk);
831 }
Hannes Reineckefef912b2018-09-28 08:17:19 +0200832 register_disk(parent, disk, groups);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500833 if (register_queue)
834 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700835
Tejun Heo523e1d32011-10-19 14:31:07 +0200836 /*
837 * Take an extra ref on queue which will be put on disk_release()
838 * so that it sticks around as long as @disk is there.
839 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100840 WARN_ON_ONCE(!blk_get_queue(disk->queue));
Tejun Heo523e1d32011-10-19 14:31:07 +0200841
Tejun Heo77ea8872010-12-08 20:57:37 +0100842 disk_add_events(disk);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400843 blk_integrity_add(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500845
Hannes Reineckefef912b2018-09-28 08:17:19 +0200846void device_add_disk(struct device *parent, struct gendisk *disk,
847 const struct attribute_group **groups)
848
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500849{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200850 __device_add_disk(parent, disk, groups, true);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500851}
Dan Williamse63a46b2016-06-15 18:17:27 -0700852EXPORT_SYMBOL(device_add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500854void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
855{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200856 __device_add_disk(parent, disk, NULL, false);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500857}
858EXPORT_SYMBOL(device_add_disk_no_queue_reg);
859
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200860static void invalidate_partition(struct gendisk *disk, int partno)
861{
862 struct block_device *bdev;
863
864 bdev = bdget_disk(disk, partno);
865 if (!bdev)
866 return;
867
868 fsync_bdev(bdev);
869 __invalidate_device(bdev, true);
Christoph Hellwig9bc5c392020-04-14 09:29:02 +0200870
871 /*
872 * Unhash the bdev inode for this device so that it gets evicted as soon
873 * as last inode reference is dropped.
874 */
875 remove_inode_hash(bdev->bd_inode);
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200876 bdput(bdev);
877}
878
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000879/**
880 * del_gendisk - remove the gendisk
881 * @disk: the struct gendisk to remove
882 *
883 * Removes the gendisk and all its associated resources. This deletes the
884 * partitions associated with the gendisk, and unregisters the associated
885 * request_queue.
886 *
887 * This is the counter to the respective __device_add_disk() call.
888 *
889 * The final removal of the struct gendisk happens when its refcount reaches 0
890 * with put_disk(), which should be called after del_gendisk(), if
891 * __device_add_disk() was used.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000892 *
893 * Drivers exist which depend on the release of the gendisk to be synchronous,
894 * it should not be deferred.
895 *
896 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000897 */
Tejun Heod2bf1b62010-12-08 20:57:36 +0100898void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Tejun Heod2bf1b62010-12-08 20:57:36 +0100900 struct disk_part_iter piter;
901 struct hd_struct *part;
902
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000903 might_sleep();
904
Martin K. Petersen25520d52015-10-21 13:19:49 -0400905 blk_integrity_del(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +0100906 disk_del_events(disk);
907
Jan Kara56c09082018-02-26 13:01:41 +0100908 /*
909 * Block lookups of the disk until all bdevs are unhashed and the
910 * disk is marked as dead (GENHD_FL_UP cleared).
911 */
912 down_write(&disk->lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100913 /* invalidate stuff */
914 disk_part_iter_init(&piter, disk,
915 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
916 while ((part = disk_part_iter_next(&piter))) {
917 invalidate_partition(disk, part->partno);
Christoph Hellwigcddae802020-04-14 09:28:54 +0200918 delete_partition(disk, part);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100919 }
920 disk_part_iter_exit(&piter);
921
922 invalidate_partition(disk, 0);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100923 set_capacity(disk, 0);
924 disk->flags &= ~GENHD_FL_UP;
Jan Kara56c09082018-02-26 13:01:41 +0100925 up_write(&disk->lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100926
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300927 if (!(disk->flags & GENHD_FL_HIDDEN))
928 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Jan Kara90f16fd2017-03-08 17:48:33 +0100929 if (disk->queue) {
930 /*
931 * Unregister bdi before releasing device numbers (as they can
932 * get reused and we'd get clashes in sysfs).
933 */
Mike Snitzerbc8d0622018-01-09 20:46:49 -0500934 if (!(disk->flags & GENHD_FL_HIDDEN))
935 bdi_unregister(disk->queue->backing_dev_info);
Jan Kara90f16fd2017-03-08 17:48:33 +0100936 blk_unregister_queue(disk);
937 } else {
938 WARN_ON(1);
939 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100940
Hannes Reinecke17eac092017-11-09 17:57:06 +0100941 if (!(disk->flags & GENHD_FL_HIDDEN))
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300942 blk_unregister_region(disk_devt(disk), disk->minors);
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800943 /*
944 * Remove gendisk pointer from idr so that it cannot be looked up
945 * while RCU period before freeing gendisk is running to prevent
946 * use-after-free issues. Note that the device number stays
947 * "in-use" until we really free the gendisk.
948 */
949 blk_invalidate_devt(disk_devt(disk));
Tejun Heod2bf1b62010-12-08 20:57:36 +0100950
951 kobject_put(disk->part0.holder_dir);
952 kobject_put(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 part_stat_set_all(&disk->part0, 0);
Tejun Heoed9e1982008-08-25 19:56:05 +0900955 disk->part0.stamp = 0;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100956 if (!sysfs_deprecated)
957 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
Ming Lei25e823c2013-02-22 16:34:13 -0800958 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100959 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100961EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Vishal Verma99e66082016-01-09 08:36:51 -0800963/* sysfs access to bad-blocks list. */
964static ssize_t disk_badblocks_show(struct device *dev,
965 struct device_attribute *attr,
966 char *page)
967{
968 struct gendisk *disk = dev_to_disk(dev);
969
970 if (!disk->bb)
971 return sprintf(page, "\n");
972
973 return badblocks_show(disk->bb, page, 0);
974}
975
976static ssize_t disk_badblocks_store(struct device *dev,
977 struct device_attribute *attr,
978 const char *page, size_t len)
979{
980 struct gendisk *disk = dev_to_disk(dev);
981
982 if (!disk->bb)
983 return -ENXIO;
984
985 return badblocks_store(disk->bb, page, len, 0);
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/**
989 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200990 * @devt: device to get partitioning information for
Randy Dunlap496aa8a2008-10-16 07:46:23 +0200991 * @partno: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 *
993 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200994 * information for the given device @devt.
Luis Chamberlain763b5892020-06-19 20:47:24 +0000995 *
996 * Context: can sleep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200998struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Tejun Heobcce3de2008-08-25 19:47:22 +09001000 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001001
Luis Chamberlain763b5892020-06-19 20:47:24 +00001002 might_sleep();
1003
Tejun Heobcce3de2008-08-25 19:47:22 +09001004 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
1005 struct kobject *kobj;
1006
1007 kobj = kobj_lookup(bdev_map, devt, partno);
1008 if (kobj)
1009 disk = dev_to_disk(kobj_to_dev(kobj));
1010 } else {
1011 struct hd_struct *part;
1012
Dan Williams4d66e5e2015-06-10 23:47:14 -04001013 spin_lock_bh(&ext_devt_lock);
Tejun Heo870d6652008-08-25 19:47:25 +09001014 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Jan Kara3079c222018-02-26 13:01:38 +01001015 if (part && get_disk_and_module(part_to_disk(part))) {
Tejun Heobcce3de2008-08-25 19:47:22 +09001016 *partno = part->partno;
1017 disk = part_to_disk(part);
1018 }
Dan Williams4d66e5e2015-06-10 23:47:14 -04001019 spin_unlock_bh(&ext_devt_lock);
Tejun Heobcce3de2008-08-25 19:47:22 +09001020 }
1021
Jan Kara56c09082018-02-26 13:01:41 +01001022 if (!disk)
1023 return NULL;
1024
1025 /*
1026 * Synchronize with del_gendisk() to not return disk that is being
1027 * destroyed.
1028 */
1029 down_read(&disk->lookup_sem);
1030 if (unlikely((disk->flags & GENHD_FL_HIDDEN) ||
1031 !(disk->flags & GENHD_FL_UP))) {
1032 up_read(&disk->lookup_sem);
Jan Kara9df6c292018-02-26 13:01:39 +01001033 put_disk_and_module(disk);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001034 disk = NULL;
Jan Kara56c09082018-02-26 13:01:41 +01001035 } else {
1036 up_read(&disk->lookup_sem);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001037 }
Tejun Heobcce3de2008-08-25 19:47:22 +09001038 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Tejun Heof331c022008-09-03 09:01:48 +02001041/**
1042 * bdget_disk - do bdget() by gendisk and partition number
1043 * @disk: gendisk of interest
1044 * @partno: partition number
1045 *
1046 * Find partition @partno from @disk, do bdget() on it.
1047 *
1048 * CONTEXT:
1049 * Don't care.
1050 *
1051 * RETURNS:
1052 * Resulting block_device on success, NULL on failure.
1053 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +02001054struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +02001055{
Tejun Heo548b10e2008-08-29 09:01:47 +02001056 struct hd_struct *part;
1057 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +02001058
Tejun Heo548b10e2008-08-29 09:01:47 +02001059 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001060 if (part)
Tejun Heo548b10e2008-08-29 09:01:47 +02001061 bdev = bdget(part_devt(part));
1062 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +02001063
Tejun Heo548b10e2008-08-29 09:01:47 +02001064 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +02001065}
1066EXPORT_SYMBOL(bdget_disk);
1067
Dave Gilbertdd2a3452007-05-09 02:33:24 -07001068/*
1069 * print a full list of all partitions - intended for places where the root
1070 * filesystem can't be mounted and thus to give the victim some idea of what
1071 * went wrong
1072 */
1073void __init printk_all_partitions(void)
1074{
Tejun Heodef4e382008-09-03 08:57:12 +02001075 struct class_dev_iter iter;
1076 struct device *dev;
1077
1078 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1079 while ((dev = class_dev_iter_next(&iter))) {
1080 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001081 struct disk_part_iter piter;
1082 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +09001083 char name_buf[BDEVNAME_SIZE];
1084 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +02001085
1086 /*
1087 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001088 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +02001089 */
1090 if (get_capacity(disk) == 0 ||
1091 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
1092 continue;
1093
1094 /*
1095 * Note, unlike /proc/partitions, I am showing the
1096 * numbers in hex - the same format as the root=
1097 * option takes.
1098 */
Tejun Heo074a7ac2008-08-25 19:56:14 +09001099 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
1100 while ((part = disk_part_iter_next(&piter))) {
1101 bool is_part0 = part == &disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +02001102
Will Drewryb5af9212010-08-31 15:47:07 -05001103 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Tejun Heo1f014292008-08-25 19:47:23 +09001104 bdevt_str(part_devt(part), devt_buf),
Vivek Goyalc83f6bf2012-08-01 12:24:18 +02001105 (unsigned long long)part_nr_sects_read(part) >> 1
1106 , disk_name(disk, part->partno, name_buf),
Stephen Warren1ad7e892012-11-08 16:12:25 -08001107 part->info ? part->info->uuid : "");
Tejun Heo074a7ac2008-08-25 19:56:14 +09001108 if (is_part0) {
Dan Williams52c44d92016-06-15 19:43:07 -07001109 if (dev->parent && dev->parent->driver)
Tejun Heo074a7ac2008-08-25 19:56:14 +09001110 printk(" driver: %s\n",
Dan Williams52c44d92016-06-15 19:43:07 -07001111 dev->parent->driver->name);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001112 else
1113 printk(" (driver?)\n");
1114 } else
1115 printk("\n");
1116 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001117 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +02001118 }
1119 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -07001120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122#ifdef CONFIG_PROC_FS
1123/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +02001124static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001125{
Tejun Heodef4e382008-09-03 08:57:12 +02001126 loff_t skip = *pos;
1127 struct class_dev_iter *iter;
1128 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001129
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +02001130 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +02001131 if (!iter)
1132 return ERR_PTR(-ENOMEM);
1133
1134 seqf->private = iter;
1135 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
1136 do {
1137 dev = class_dev_iter_next(iter);
1138 if (!dev)
1139 return NULL;
1140 } while (skip--);
1141
1142 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001143}
1144
Tejun Heodef4e382008-09-03 08:57:12 +02001145static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001147 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -04001148
Tejun Heodef4e382008-09-03 08:57:12 +02001149 (*pos)++;
1150 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +02001151 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001152 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +02001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return NULL;
1155}
1156
Tejun Heodef4e382008-09-03 08:57:12 +02001157static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -04001158{
Tejun Heodef4e382008-09-03 08:57:12 +02001159 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -04001160
Tejun Heodef4e382008-09-03 08:57:12 +02001161 /* stop is called even after start failed :-( */
1162 if (iter) {
1163 class_dev_iter_exit(iter);
1164 kfree(iter);
Vegard Nossum77da1602016-07-29 10:40:31 +02001165 seqf->private = NULL;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
Tejun Heodef4e382008-09-03 08:57:12 +02001169static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Jianpeng Ma06768062012-08-03 10:42:00 +02001171 void *p;
Tejun Heodef4e382008-09-03 08:57:12 +02001172
1173 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +01001174 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +02001175 seq_puts(seqf, "major minor #blocks name\n\n");
1176 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Tejun Heocf771cb2008-09-03 09:01:09 +02001179static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001182 struct disk_part_iter piter;
1183 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 char buf[BDEVNAME_SIZE];
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +02001187 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +02001188 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return 0;
1190 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
1191 return 0;
1192
1193 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +09001194 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001195 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +09001196 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +02001197 MAJOR(part_devt(part)), MINOR(part_devt(part)),
Vivek Goyalc83f6bf2012-08-01 12:24:18 +02001198 (unsigned long long)part_nr_sects_read(part) >> 1,
Tejun Heof331c022008-09-03 09:01:48 +02001199 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001200 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 return 0;
1203}
1204
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001205static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001206 .start = show_partition_start,
1207 .next = disk_seqf_next,
1208 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001209 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210};
1211#endif
1212
1213
Tejun Heocf771cb2008-09-03 09:01:09 +02001214static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001216 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001218 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return NULL;
1220}
1221
1222static int __init genhd_device_init(void)
1223{
Dan Williamse105b8b2008-04-21 10:51:07 -07001224 int error;
1225
1226 block_class.dev_kobj = sysfs_dev_block_kobj;
1227 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -07001228 if (unlikely(error))
1229 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001230 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001232
Zhang, Yanmin561ec682008-11-14 08:26:30 +01001233 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1234
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001235 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +02001236 if (!sysfs_deprecated)
1237 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -08001238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
1241subsys_initcall(genhd_device_init);
1242
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001243static ssize_t disk_range_show(struct device *dev,
1244 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001246 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001248 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Tejun Heo1f014292008-08-25 19:47:23 +09001251static ssize_t disk_ext_range_show(struct device *dev,
1252 struct device_attribute *attr, char *buf)
1253{
1254 struct gendisk *disk = dev_to_disk(dev);
1255
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001256 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +09001257}
1258
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001259static ssize_t disk_removable_show(struct device *dev,
1260 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +02001261{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001262 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001263
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001264 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001266}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001268static ssize_t disk_hidden_show(struct device *dev,
1269 struct device_attribute *attr, char *buf)
1270{
1271 struct gendisk *disk = dev_to_disk(dev);
1272
1273 return sprintf(buf, "%d\n",
1274 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1275}
1276
Kay Sievers1c9ce522008-06-13 09:41:00 +02001277static ssize_t disk_ro_show(struct device *dev,
1278 struct device_attribute *attr, char *buf)
1279{
1280 struct gendisk *disk = dev_to_disk(dev);
1281
Tejun Heob7db9952008-08-25 19:56:10 +09001282 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +02001283}
1284
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001285ssize_t part_size_show(struct device *dev,
1286 struct device_attribute *attr, char *buf)
1287{
1288 struct hd_struct *p = dev_to_part(dev);
1289
1290 return sprintf(buf, "%llu\n",
1291 (unsigned long long)part_nr_sects_read(p));
1292}
1293
1294ssize_t part_stat_show(struct device *dev,
1295 struct device_attribute *attr, char *buf)
1296{
1297 struct hd_struct *p = dev_to_part(dev);
1298 struct request_queue *q = part_to_disk(p)->queue;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001299 struct disk_stats stat;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001300 unsigned int inflight;
1301
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001302 part_stat_read_all(p, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001303 if (queue_is_mq(q))
1304 inflight = blk_mq_in_flight(q, p);
1305 else
1306 inflight = part_in_flight(q, p);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001307
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001308 return sprintf(buf,
1309 "%8lu %8lu %8llu %8u "
1310 "%8lu %8lu %8llu %8u "
1311 "%8u %8u %8u "
1312 "%8lu %8lu %8llu %8u "
1313 "%8lu %8u"
1314 "\n",
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001315 stat.ios[STAT_READ],
1316 stat.merges[STAT_READ],
1317 (unsigned long long)stat.sectors[STAT_READ],
1318 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
1319 stat.ios[STAT_WRITE],
1320 stat.merges[STAT_WRITE],
1321 (unsigned long long)stat.sectors[STAT_WRITE],
1322 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001323 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001324 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001325 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1326 stat.nsecs[STAT_WRITE] +
1327 stat.nsecs[STAT_DISCARD] +
1328 stat.nsecs[STAT_FLUSH],
1329 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001330 stat.ios[STAT_DISCARD],
1331 stat.merges[STAT_DISCARD],
1332 (unsigned long long)stat.sectors[STAT_DISCARD],
1333 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
1334 stat.ios[STAT_FLUSH],
1335 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001336}
1337
1338ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
1339 char *buf)
1340{
1341 struct hd_struct *p = dev_to_part(dev);
1342 struct request_queue *q = part_to_disk(p)->queue;
1343 unsigned int inflight[2];
1344
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001345 if (queue_is_mq(q))
1346 blk_mq_in_flight_rw(q, p, inflight);
1347 else
1348 part_in_flight_rw(q, p, inflight);
1349
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001350 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1351}
1352
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001353static ssize_t disk_capability_show(struct device *dev,
1354 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001355{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001356 struct gendisk *disk = dev_to_disk(dev);
1357
1358 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001359}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001360
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001361static ssize_t disk_alignment_offset_show(struct device *dev,
1362 struct device_attribute *attr,
1363 char *buf)
1364{
1365 struct gendisk *disk = dev_to_disk(dev);
1366
1367 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1368}
1369
Martin K. Petersen86b37282009-11-10 11:50:21 +01001370static ssize_t disk_discard_alignment_show(struct device *dev,
1371 struct device_attribute *attr,
1372 char *buf)
1373{
1374 struct gendisk *disk = dev_to_disk(dev);
1375
Martin K. Petersendd3d1452010-01-11 03:21:48 -05001376 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +01001377}
1378
Joe Perches5657a812018-05-24 13:38:59 -06001379static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1380static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1381static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1382static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1383static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1384static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1385static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1386static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1387static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1388static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1389static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1390static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001391
Akinobu Mitac17bb492006-12-08 02:39:46 -08001392#ifdef CONFIG_FAIL_MAKE_REQUEST
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001393ssize_t part_fail_show(struct device *dev,
1394 struct device_attribute *attr, char *buf)
1395{
1396 struct hd_struct *p = dev_to_part(dev);
1397
1398 return sprintf(buf, "%d\n", p->make_it_fail);
1399}
1400
1401ssize_t part_fail_store(struct device *dev,
1402 struct device_attribute *attr,
1403 const char *buf, size_t count)
1404{
1405 struct hd_struct *p = dev_to_part(dev);
1406 int i;
1407
1408 if (count > 0 && sscanf(buf, "%d", &i) > 0)
1409 p->make_it_fail = (i == 0) ? 0 : 1;
1410
1411 return count;
1412}
1413
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001414static struct device_attribute dev_attr_fail =
Joe Perches5657a812018-05-24 13:38:59 -06001415 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001416#endif /* CONFIG_FAIL_MAKE_REQUEST */
1417
Jens Axboe581d4e22008-09-14 05:56:33 -07001418#ifdef CONFIG_FAIL_IO_TIMEOUT
1419static struct device_attribute dev_attr_fail_timeout =
Joe Perches5657a812018-05-24 13:38:59 -06001420 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
Jens Axboe581d4e22008-09-14 05:56:33 -07001421#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001422
1423static struct attribute *disk_attrs[] = {
1424 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +09001425 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001426 &dev_attr_removable.attr,
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001427 &dev_attr_hidden.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001428 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001429 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001430 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001431 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001432 &dev_attr_capability.attr,
1433 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001434 &dev_attr_inflight.attr,
Vishal Verma99e66082016-01-09 08:36:51 -08001435 &dev_attr_badblocks.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001436#ifdef CONFIG_FAIL_MAKE_REQUEST
1437 &dev_attr_fail.attr,
1438#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001439#ifdef CONFIG_FAIL_IO_TIMEOUT
1440 &dev_attr_fail_timeout.attr,
1441#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001442 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443};
1444
Dan Williams9438b3e02017-04-27 14:46:26 -07001445static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1446{
1447 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1448 struct gendisk *disk = dev_to_disk(dev);
1449
1450 if (a == &dev_attr_badblocks.attr && !disk->bb)
1451 return 0;
1452 return a->mode;
1453}
1454
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001455static struct attribute_group disk_attr_group = {
1456 .attrs = disk_attrs,
Dan Williams9438b3e02017-04-27 14:46:26 -07001457 .is_visible = disk_visible,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001458};
1459
David Brownella4dbd672009-06-24 10:06:31 -07001460static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001461 &disk_attr_group,
1462 NULL
1463};
1464
Tejun Heo540eed52008-08-25 19:56:15 +09001465/**
1466 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1467 * @disk: disk to replace part_tbl for
1468 * @new_ptbl: new part_tbl to install
1469 *
1470 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1471 * original ptbl is freed using RCU callback.
1472 *
1473 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001474 * Matching bd_mutex locked or the caller is the only user of @disk.
Tejun Heo540eed52008-08-25 19:56:15 +09001475 */
1476static void disk_replace_part_tbl(struct gendisk *disk,
1477 struct disk_part_tbl *new_ptbl)
1478{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001479 struct disk_part_tbl *old_ptbl =
1480 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001481
1482 rcu_assign_pointer(disk->part_tbl, new_ptbl);
Jens Axboea6f23652008-10-24 12:52:42 +02001483
1484 if (old_ptbl) {
1485 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
Lai Jiangshan57bdfbf2011-03-18 11:42:58 +08001486 kfree_rcu(old_ptbl, rcu_head);
Jens Axboea6f23652008-10-24 12:52:42 +02001487 }
Tejun Heo540eed52008-08-25 19:56:15 +09001488}
1489
1490/**
1491 * disk_expand_part_tbl - expand disk->part_tbl
1492 * @disk: disk to expand part_tbl for
1493 * @partno: expand such that this partno can fit in
1494 *
1495 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1496 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1497 *
1498 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001499 * Matching bd_mutex locked or the caller is the only user of @disk.
1500 * Might sleep.
Tejun Heo540eed52008-08-25 19:56:15 +09001501 *
1502 * RETURNS:
1503 * 0 on success, -errno on failure.
1504 */
1505int disk_expand_part_tbl(struct gendisk *disk, int partno)
1506{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001507 struct disk_part_tbl *old_ptbl =
1508 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001509 struct disk_part_tbl *new_ptbl;
1510 int len = old_ptbl ? old_ptbl->len : 0;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001511 int i, target;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001512
1513 /*
1514 * check for int overflow, since we can get here from blkpg_ioctl()
1515 * with a user passed 'partno'.
1516 */
1517 target = partno + 1;
1518 if (target < 0)
1519 return -EINVAL;
Tejun Heo540eed52008-08-25 19:56:15 +09001520
1521 /* disk_max_parts() is zero during initialization, ignore if so */
1522 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1523 return -EINVAL;
1524
1525 if (target <= len)
1526 return 0;
1527
Gustavo A. R. Silva78b90a22019-05-31 13:47:54 -05001528 new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
1529 disk->node_id);
Tejun Heo540eed52008-08-25 19:56:15 +09001530 if (!new_ptbl)
1531 return -ENOMEM;
1532
Tejun Heo540eed52008-08-25 19:56:15 +09001533 new_ptbl->len = target;
1534
1535 for (i = 0; i < len; i++)
1536 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1537
1538 disk_replace_part_tbl(disk, new_ptbl);
1539 return 0;
1540}
1541
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001542/**
1543 * disk_release - releases all allocated resources of the gendisk
1544 * @dev: the device representing this disk
1545 *
1546 * This function releases all allocated resources of the gendisk.
1547 *
1548 * The struct gendisk refcount is incremented with get_gendisk() or
1549 * get_disk_and_module(), and its refcount is decremented with
1550 * put_disk_and_module() or put_disk(). Once the refcount reaches 0 this
1551 * function is called.
1552 *
1553 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1554 * assigned. Since the request_queue sits on top of the gendisk for these
1555 * drivers we also call blk_put_queue() for them, and we expect the
1556 * request_queue refcount to reach 0 at this point, and so the request_queue
1557 * will also be freed prior to the disk.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001558 *
1559 * Context: can sleep
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001560 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001561static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001563 struct gendisk *disk = dev_to_disk(dev);
1564
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001565 might_sleep();
1566
Keith Busch2da78092014-08-26 09:05:36 -06001567 blk_free_devt(dev->devt);
Tejun Heo77ea8872010-12-08 20:57:37 +01001568 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +09001570 disk_replace_part_tbl(disk, NULL);
Ming Leib54e5ed2015-07-16 11:16:44 +08001571 hd_free_part(&disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001572 if (disk->queue)
1573 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 kfree(disk);
1575}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001576struct class block_class = {
1577 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578};
1579
Kay Sievers3c2670e2013-04-06 09:56:00 -07001580static char *block_devnode(struct device *dev, umode_t *mode,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001581 kuid_t *uid, kgid_t *gid)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001582{
1583 struct gendisk *disk = dev_to_disk(dev);
1584
Christoph Hellwig348e1142020-03-27 09:07:17 +01001585 if (disk->fops->devnode)
1586 return disk->fops->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001587 return NULL;
1588}
1589
Bart Van Asscheedf8ff52017-06-20 11:15:48 -07001590static const struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001591 .name = "disk",
1592 .groups = disk_attr_groups,
1593 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001594 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595};
1596
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001597#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001598/*
1599 * aggregate disk stat collector. Uses the same stats that the sysfs
1600 * entries do, above, but makes them available through one seq_file.
1601 *
1602 * The output looks suspiciously like /proc/partitions with a bunch of
1603 * extra fields.
1604 */
1605static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001608 struct disk_part_iter piter;
1609 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 char buf[BDEVNAME_SIZE];
Mikulas Patockae016b782018-12-06 11:41:21 -05001611 unsigned int inflight;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001612 struct disk_stats stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001615 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001616 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 " rio rmerge rsect ruse wio wmerge "
1618 "wsect wuse running use aveq"
1619 "\n\n");
1620 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001621
Tejun Heo71982a42009-04-17 08:34:48 +02001622 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001623 while ((hd = disk_part_iter_next(&piter))) {
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001624 part_stat_read_all(hd, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001625 if (queue_is_mq(gp->queue))
1626 inflight = blk_mq_in_flight(gp->queue, hd);
1627 else
1628 inflight = part_in_flight(gp->queue, hd);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001629
Michael Callahanbdca3c82018-07-18 04:47:40 -07001630 seq_printf(seqf, "%4d %7d %s "
1631 "%lu %lu %lu %u "
1632 "%lu %lu %lu %u "
1633 "%u %u %u "
Konstantin Khlebnikovb6866312019-11-21 13:40:26 +03001634 "%lu %lu %lu %u "
1635 "%lu %u"
1636 "\n",
Tejun Heof331c022008-09-03 09:01:48 +02001637 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1638 disk_name(gp, hd->partno, buf),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001639 stat.ios[STAT_READ],
1640 stat.merges[STAT_READ],
1641 stat.sectors[STAT_READ],
1642 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1643 NSEC_PER_MSEC),
1644 stat.ios[STAT_WRITE],
1645 stat.merges[STAT_WRITE],
1646 stat.sectors[STAT_WRITE],
1647 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1648 NSEC_PER_MSEC),
Mikulas Patockae016b782018-12-06 11:41:21 -05001649 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001650 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001651 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1652 stat.nsecs[STAT_WRITE] +
1653 stat.nsecs[STAT_DISCARD] +
1654 stat.nsecs[STAT_FLUSH],
1655 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001656 stat.ios[STAT_DISCARD],
1657 stat.merges[STAT_DISCARD],
1658 stat.sectors[STAT_DISCARD],
1659 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1660 NSEC_PER_MSEC),
1661 stat.ios[STAT_FLUSH],
1662 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1663 NSEC_PER_MSEC)
Jerome Marchand28f39d52008-02-08 11:04:56 +01001664 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001666 disk_part_iter_exit(&piter);
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return 0;
1669}
1670
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001671static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001672 .start = disk_seqf_start,
1673 .next = disk_seqf_next,
1674 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 .show = diskstats_show
1676};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001677
1678static int __init proc_genhd_init(void)
1679{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001680 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1681 proc_create_seq("partitions", 0, NULL, &partitions_op);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001682 return 0;
1683}
1684module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001685#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Tejun Heocf771cb2008-09-03 09:01:09 +02001687dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001688{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001689 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001690 struct class_dev_iter iter;
1691 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001692
Tejun Heodef4e382008-09-03 08:57:12 +02001693 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1694 while ((dev = class_dev_iter_next(&iter))) {
1695 struct gendisk *disk = dev_to_disk(dev);
Tejun Heo548b10e2008-08-29 09:01:47 +02001696 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001697
Kay Sievers3ada8b72009-01-06 10:44:43 -08001698 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001699 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001700
Neil Brown41b8c852009-02-18 10:33:59 +01001701 if (partno < disk->minors) {
1702 /* We need to return the right devno, even
1703 * if the partition doesn't exist yet.
1704 */
1705 devt = MKDEV(MAJOR(dev->devt),
1706 MINOR(dev->devt) + partno);
1707 break;
1708 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001709 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001710 if (part) {
Tejun Heof331c022008-09-03 09:01:48 +02001711 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001712 disk_put_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001713 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001714 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001715 disk_put_part(part);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001716 }
Tejun Heodef4e382008-09-03 08:57:12 +02001717 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001718 return devt;
1719}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001720
Byungchul Parke319e1f2017-10-25 17:56:05 +09001721struct gendisk *__alloc_disk_node(int minors, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001722{
1723 struct gendisk *disk;
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001724 struct disk_part_tbl *ptbl;
Christoph Lameter19460892005-06-23 00:08:19 -07001725
Christoph Hellwigde65b012017-08-23 19:10:29 +02001726 if (minors > DISK_MAX_PARTS) {
1727 printk(KERN_ERR
Randy Dunlap7fb52622017-11-18 17:43:38 -08001728 "block: can't allocate more than %d partitions\n",
Christoph Hellwigde65b012017-08-23 19:10:29 +02001729 DISK_MAX_PARTS);
1730 minors = DISK_MAX_PARTS;
1731 }
Christoph Lameter19460892005-06-23 00:08:19 -07001732
Joe Perchesc1b511e2013-08-29 15:21:42 -07001733 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (disk) {
Christoph Hellwig58d4f142020-05-27 07:24:14 +02001735 disk->part0.dkstats = alloc_percpu(struct disk_stats);
1736 if (!disk->part0.dkstats) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 kfree(disk);
1738 return NULL;
1739 }
Jan Kara56c09082018-02-26 13:01:41 +01001740 init_rwsem(&disk->lookup_sem);
Cheng Renquanbf91db12008-11-20 08:37:37 +01001741 disk->node_id = node_id;
Tejun Heo540eed52008-08-25 19:56:15 +09001742 if (disk_expand_part_tbl(disk, 0)) {
Christoph Hellwig58d4f142020-05-27 07:24:14 +02001743 free_percpu(disk->part0.dkstats);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001744 kfree(disk);
1745 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001747 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
1748 rcu_assign_pointer(ptbl->part[0], &disk->part0);
Jens Axboe6c23a962011-01-07 08:43:37 +01001749
Vivek Goyalc83f6bf2012-08-01 12:24:18 +02001750 /*
1751 * set_capacity() and get_capacity() currently don't use
1752 * seqcounter to read/update the part0->nr_sects. Still init
1753 * the counter as we can read the sectors in IO submission
1754 * patch using seqence counters.
1755 *
1756 * TODO: Ideally set_capacity() and get_capacity() should be
1757 * converted to make use of bd_mutex and sequence counters.
1758 */
Ming Lei07c4e1e2020-05-08 16:17:56 +08001759 hd_sects_seq_init(&disk->part0);
Ming Lei6c710132015-07-16 11:16:45 +08001760 if (hd_ref_init(&disk->part0)) {
1761 hd_free_part(&disk->part0);
1762 kfree(disk);
1763 return NULL;
1764 }
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 disk->minors = minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001768 disk_to_dev(disk)->class = &block_class;
1769 disk_to_dev(disk)->type = &disk_type;
1770 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772 return disk;
1773}
Byungchul Parke319e1f2017-10-25 17:56:05 +09001774EXPORT_SYMBOL(__alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001776/**
1777 * get_disk_and_module - increments the gendisk and gendisk fops module refcount
1778 * @disk: the struct gendisk to to increment the refcount for
1779 *
1780 * This increments the refcount for the struct gendisk, and the gendisk's
1781 * fops module owner.
Luis Chamberlain763b5892020-06-19 20:47:24 +00001782 *
1783 * Context: Any context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001784 */
Jan Kara3079c222018-02-26 13:01:38 +01001785struct kobject *get_disk_and_module(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 struct module *owner;
1788 struct kobject *kobj;
1789
1790 if (!disk->fops)
1791 return NULL;
1792 owner = disk->fops->owner;
1793 if (owner && !try_module_get(owner))
1794 return NULL;
Jan Karad01b2dc2017-03-23 01:37:02 +01001795 kobj = kobject_get_unless_zero(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (kobj == NULL) {
1797 module_put(owner);
1798 return NULL;
1799 }
1800 return kobj;
1801
1802}
Jan Kara3079c222018-02-26 13:01:38 +01001803EXPORT_SYMBOL(get_disk_and_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001805/**
1806 * put_disk - decrements the gendisk refcount
1807 * @disk: the struct gendisk to to decrement the refcount for
1808 *
1809 * This decrements the refcount for the struct gendisk. When this reaches 0
1810 * we'll have disk_release() called.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001811 *
1812 * Context: Any context, but the last reference must not be dropped from
1813 * atomic context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815void put_disk(struct gendisk *disk)
1816{
1817 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001818 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820EXPORT_SYMBOL(put_disk);
1821
Luis Chamberlainb5bd3572020-06-19 20:47:23 +00001822/**
1823 * put_disk_and_module - decrements the module and gendisk refcount
1824 * @disk: the struct gendisk to to decrement the refcount for
1825 *
Jan Kara9df6c292018-02-26 13:01:39 +01001826 * This is a counterpart of get_disk_and_module() and thus also of
1827 * get_gendisk().
Luis Chamberlaine8c7d142020-06-19 20:47:25 +00001828 *
1829 * Context: Any context, but the last reference must not be dropped from
1830 * atomic context.
Jan Kara9df6c292018-02-26 13:01:39 +01001831 */
1832void put_disk_and_module(struct gendisk *disk)
1833{
1834 if (disk) {
1835 struct module *owner = disk->fops->owner;
1836
1837 put_disk(disk);
1838 module_put(owner);
1839 }
1840}
1841EXPORT_SYMBOL(put_disk_and_module);
1842
Hannes Reineckee3264a42009-07-28 09:13:13 +02001843static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1844{
1845 char event[] = "DISK_RO=1";
1846 char *envp[] = { event, NULL };
1847
1848 if (!ro)
1849 event[8] = '0';
1850 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1851}
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853void set_device_ro(struct block_device *bdev, int flag)
1854{
Tejun Heob7db9952008-08-25 19:56:10 +09001855 bdev->bd_part->policy = flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
1858EXPORT_SYMBOL(set_device_ro);
1859
1860void set_disk_ro(struct gendisk *disk, int flag)
1861{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001862 struct disk_part_iter piter;
1863 struct hd_struct *part;
1864
Hannes Reineckee3264a42009-07-28 09:13:13 +02001865 if (disk->part0.policy != flag) {
1866 set_disk_ro_uevent(disk, flag);
1867 disk->part0.policy = flag;
1868 }
1869
1870 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001871 while ((part = disk_part_iter_next(&piter)))
1872 part->policy = flag;
1873 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
1876EXPORT_SYMBOL(set_disk_ro);
1877
1878int bdev_read_only(struct block_device *bdev)
1879{
1880 if (!bdev)
1881 return 0;
Tejun Heob7db9952008-08-25 19:56:10 +09001882 return bdev->bd_part->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
1885EXPORT_SYMBOL(bdev_read_only);
1886
Tejun Heo77ea8872010-12-08 20:57:37 +01001887/*
1888 * Disk events - monitor disk events like media change and eject request.
1889 */
1890struct disk_events {
1891 struct list_head node; /* all disk_event's */
1892 struct gendisk *disk; /* the associated disk */
1893 spinlock_t lock;
1894
Tejun Heofdd514e2011-06-09 20:43:59 +02001895 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001896 int block; /* event blocking depth */
1897 unsigned int pending; /* events already sent out */
1898 unsigned int clearing; /* events being cleared */
1899
1900 long poll_msecs; /* interval, -1 for default */
1901 struct delayed_work dwork;
1902};
1903
1904static const char *disk_events_strs[] = {
1905 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1906 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1907};
1908
1909static char *disk_uevents[] = {
1910 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1911 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1912};
1913
1914/* list of all disk_events */
1915static DEFINE_MUTEX(disk_events_mutex);
1916static LIST_HEAD(disk_events);
1917
1918/* disable in-kernel polling by default */
Wei Tang1fe8f342015-11-24 09:58:46 +08001919static unsigned long disk_events_dfl_poll_msecs;
Tejun Heo77ea8872010-12-08 20:57:37 +01001920
1921static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1922{
1923 struct disk_events *ev = disk->ev;
1924 long intv_msecs = 0;
1925
1926 /*
1927 * If device-specific poll interval is set, always use it. If
Martin Wilck673387a2019-03-27 14:51:01 +01001928 * the default is being used, poll if the POLL flag is set.
Tejun Heo77ea8872010-12-08 20:57:37 +01001929 */
1930 if (ev->poll_msecs >= 0)
1931 intv_msecs = ev->poll_msecs;
Martin Wilckc92e2f02019-03-27 14:51:02 +01001932 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
Tejun Heo77ea8872010-12-08 20:57:37 +01001933 intv_msecs = disk_events_dfl_poll_msecs;
1934
1935 return msecs_to_jiffies(intv_msecs);
1936}
1937
Tejun Heoc3af54a2011-06-09 20:43:55 +02001938/**
1939 * disk_block_events - block and flush disk event checking
1940 * @disk: disk to block events for
1941 *
1942 * On return from this function, it is guaranteed that event checking
1943 * isn't in progress and won't happen until unblocked by
1944 * disk_unblock_events(). Events blocking is counted and the actual
1945 * unblocking happens after the matching number of unblocks are done.
1946 *
1947 * Note that this intentionally does not block event checking from
1948 * disk_clear_events().
1949 *
1950 * CONTEXT:
1951 * Might sleep.
1952 */
1953void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001954{
1955 struct disk_events *ev = disk->ev;
1956 unsigned long flags;
1957 bool cancel;
1958
Tejun Heoc3af54a2011-06-09 20:43:55 +02001959 if (!ev)
1960 return;
1961
Tejun Heofdd514e2011-06-09 20:43:59 +02001962 /*
1963 * Outer mutex ensures that the first blocker completes canceling
1964 * the event work before further blockers are allowed to finish.
1965 */
1966 mutex_lock(&ev->block_mutex);
1967
Tejun Heo77ea8872010-12-08 20:57:37 +01001968 spin_lock_irqsave(&ev->lock, flags);
1969 cancel = !ev->block++;
1970 spin_unlock_irqrestore(&ev->lock, flags);
1971
Tejun Heoc3af54a2011-06-09 20:43:55 +02001972 if (cancel)
1973 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001974
1975 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001976}
1977
1978static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1979{
1980 struct disk_events *ev = disk->ev;
1981 unsigned long intv;
1982 unsigned long flags;
1983
1984 spin_lock_irqsave(&ev->lock, flags);
1985
1986 if (WARN_ON_ONCE(ev->block <= 0))
1987 goto out_unlock;
1988
1989 if (--ev->block)
1990 goto out_unlock;
1991
Tejun Heo77ea8872010-12-08 20:57:37 +01001992 intv = disk_events_poll_jiffies(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001993 if (check_now)
Viresh Kumar695588f2013-04-24 17:12:56 +05301994 queue_delayed_work(system_freezable_power_efficient_wq,
1995 &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001996 else if (intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301997 queue_delayed_work(system_freezable_power_efficient_wq,
1998 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001999out_unlock:
2000 spin_unlock_irqrestore(&ev->lock, flags);
2001}
2002
2003/**
Tejun Heo77ea8872010-12-08 20:57:37 +01002004 * disk_unblock_events - unblock disk event checking
2005 * @disk: disk to unblock events for
2006 *
2007 * Undo disk_block_events(). When the block count reaches zero, it
2008 * starts events polling if configured.
2009 *
2010 * CONTEXT:
2011 * Don't care. Safe to call from irq context.
2012 */
2013void disk_unblock_events(struct gendisk *disk)
2014{
2015 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01002016 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01002017}
2018
2019/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02002020 * disk_flush_events - schedule immediate event checking and flushing
2021 * @disk: disk to check and flush events for
2022 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01002023 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02002024 * Schedule immediate event checking on @disk if not blocked. Events in
2025 * @mask are scheduled to be cleared from the driver. Note that this
2026 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01002027 *
2028 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02002029 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01002030 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02002031void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01002032{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02002033 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02002034
2035 if (!ev)
2036 return;
2037
Tejun Heo85ef06d2011-07-01 16:17:47 +02002038 spin_lock_irq(&ev->lock);
2039 ev->clearing |= mask;
Tejun Heo41f63c52012-08-03 10:30:47 -07002040 if (!ev->block)
Viresh Kumar695588f2013-04-24 17:12:56 +05302041 mod_delayed_work(system_freezable_power_efficient_wq,
2042 &ev->dwork, 0);
Tejun Heo85ef06d2011-07-01 16:17:47 +02002043 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01002044}
Tejun Heo77ea8872010-12-08 20:57:37 +01002045
2046/**
2047 * disk_clear_events - synchronously check, clear and return pending events
2048 * @disk: disk to fetch and clear events from
Masanari Iidada3dae52014-09-09 01:27:23 +09002049 * @mask: mask of events to be fetched and cleared
Tejun Heo77ea8872010-12-08 20:57:37 +01002050 *
2051 * Disk events are synchronously checked and pending events in @mask
2052 * are cleared and returned. This ignores the block count.
2053 *
2054 * CONTEXT:
2055 * Might sleep.
2056 */
2057unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
2058{
2059 const struct block_device_operations *bdops = disk->fops;
2060 struct disk_events *ev = disk->ev;
2061 unsigned int pending;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002062 unsigned int clearing = mask;
Tejun Heo77ea8872010-12-08 20:57:37 +01002063
2064 if (!ev) {
2065 /* for drivers still using the old ->media_changed method */
2066 if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
2067 bdops->media_changed && bdops->media_changed(disk))
2068 return DISK_EVENT_MEDIA_CHANGE;
2069 return 0;
2070 }
2071
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002072 disk_block_events(disk);
2073
2074 /*
2075 * store the union of mask and ev->clearing on the stack so that the
2076 * race with disk_flush_events does not cause ambiguity (ev->clearing
2077 * can still be modified even if events are blocked).
2078 */
Tejun Heo77ea8872010-12-08 20:57:37 +01002079 spin_lock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002080 clearing |= ev->clearing;
2081 ev->clearing = 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01002082 spin_unlock_irq(&ev->lock);
2083
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002084 disk_check_events(ev, &clearing);
Derek Basehoreaea24a82012-12-18 12:27:18 -08002085 /*
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002086 * if ev->clearing is not 0, the disk_flush_events got called in the
2087 * middle of this function, so we want to run the workfn without delay.
Derek Basehoreaea24a82012-12-18 12:27:18 -08002088 */
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002089 __disk_unblock_events(disk, ev->clearing ? true : false);
Tejun Heo77ea8872010-12-08 20:57:37 +01002090
2091 /* then, fetch and clear pending events */
2092 spin_lock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01002093 pending = ev->pending & mask;
2094 ev->pending &= ~mask;
2095 spin_unlock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002096 WARN_ON_ONCE(clearing & mask);
Tejun Heo77ea8872010-12-08 20:57:37 +01002097
2098 return pending;
2099}
2100
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002101/*
2102 * Separate this part out so that a different pointer for clearing_ptr can be
2103 * passed in for disk_clear_events.
2104 */
Tejun Heo77ea8872010-12-08 20:57:37 +01002105static void disk_events_workfn(struct work_struct *work)
2106{
2107 struct delayed_work *dwork = to_delayed_work(work);
2108 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002109
2110 disk_check_events(ev, &ev->clearing);
2111}
2112
2113static void disk_check_events(struct disk_events *ev,
2114 unsigned int *clearing_ptr)
2115{
Tejun Heo77ea8872010-12-08 20:57:37 +01002116 struct gendisk *disk = ev->disk;
2117 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002118 unsigned int clearing = *clearing_ptr;
Tejun Heo77ea8872010-12-08 20:57:37 +01002119 unsigned int events;
2120 unsigned long intv;
2121 int nr_events = 0, i;
2122
2123 /* check events */
2124 events = disk->fops->check_events(disk, clearing);
2125
2126 /* accumulate pending events and schedule next poll if necessary */
2127 spin_lock_irq(&ev->lock);
2128
2129 events &= ~ev->pending;
2130 ev->pending |= events;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002131 *clearing_ptr &= ~clearing;
Tejun Heo77ea8872010-12-08 20:57:37 +01002132
2133 intv = disk_events_poll_jiffies(disk);
2134 if (!ev->block && intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05302135 queue_delayed_work(system_freezable_power_efficient_wq,
2136 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01002137
2138 spin_unlock_irq(&ev->lock);
2139
Tejun Heo7c88a162011-04-21 19:43:58 +02002140 /*
2141 * Tell userland about new events. Only the events listed in
Martin Wilckc92e2f02019-03-27 14:51:02 +01002142 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
2143 * is set. Otherwise, events are processed internally but never
2144 * get reported to userland.
Tejun Heo7c88a162011-04-21 19:43:58 +02002145 */
Tejun Heo77ea8872010-12-08 20:57:37 +01002146 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Martin Wilckc92e2f02019-03-27 14:51:02 +01002147 if ((events & disk->events & (1 << i)) &&
2148 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
Tejun Heo77ea8872010-12-08 20:57:37 +01002149 envp[nr_events++] = disk_uevents[i];
2150
2151 if (nr_events)
2152 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
2153}
2154
2155/*
2156 * A disk events enabled device has the following sysfs nodes under
2157 * its /sys/block/X/ directory.
2158 *
2159 * events : list of all supported events
2160 * events_async : list of events which can be detected w/o polling
Martin Wilck673387a2019-03-27 14:51:01 +01002161 * (always empty, only for backwards compatibility)
Tejun Heo77ea8872010-12-08 20:57:37 +01002162 * events_poll_msecs : polling interval, 0: disable, -1: system default
2163 */
2164static ssize_t __disk_events_show(unsigned int events, char *buf)
2165{
2166 const char *delim = "";
2167 ssize_t pos = 0;
2168 int i;
2169
2170 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
2171 if (events & (1 << i)) {
2172 pos += sprintf(buf + pos, "%s%s",
2173 delim, disk_events_strs[i]);
2174 delim = " ";
2175 }
2176 if (pos)
2177 pos += sprintf(buf + pos, "\n");
2178 return pos;
2179}
2180
2181static ssize_t disk_events_show(struct device *dev,
2182 struct device_attribute *attr, char *buf)
2183{
2184 struct gendisk *disk = dev_to_disk(dev);
2185
Martin Wilckc92e2f02019-03-27 14:51:02 +01002186 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
2187 return 0;
2188
Tejun Heo77ea8872010-12-08 20:57:37 +01002189 return __disk_events_show(disk->events, buf);
2190}
2191
2192static ssize_t disk_events_async_show(struct device *dev,
2193 struct device_attribute *attr, char *buf)
2194{
Martin Wilck673387a2019-03-27 14:51:01 +01002195 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01002196}
2197
2198static ssize_t disk_events_poll_msecs_show(struct device *dev,
2199 struct device_attribute *attr,
2200 char *buf)
2201{
2202 struct gendisk *disk = dev_to_disk(dev);
2203
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002204 if (!disk->ev)
2205 return sprintf(buf, "-1\n");
2206
Tejun Heo77ea8872010-12-08 20:57:37 +01002207 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
2208}
2209
2210static ssize_t disk_events_poll_msecs_store(struct device *dev,
2211 struct device_attribute *attr,
2212 const char *buf, size_t count)
2213{
2214 struct gendisk *disk = dev_to_disk(dev);
2215 long intv;
2216
2217 if (!count || !sscanf(buf, "%ld", &intv))
2218 return -EINVAL;
2219
2220 if (intv < 0 && intv != -1)
2221 return -EINVAL;
2222
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002223 if (!disk->ev)
2224 return -ENODEV;
2225
Tejun Heoc3af54a2011-06-09 20:43:55 +02002226 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01002227 disk->ev->poll_msecs = intv;
2228 __disk_unblock_events(disk, true);
2229
2230 return count;
2231}
2232
Joe Perches5657a812018-05-24 13:38:59 -06002233static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
2234static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
2235static const DEVICE_ATTR(events_poll_msecs, 0644,
Tejun Heo77ea8872010-12-08 20:57:37 +01002236 disk_events_poll_msecs_show,
2237 disk_events_poll_msecs_store);
2238
2239static const struct attribute *disk_events_attrs[] = {
2240 &dev_attr_events.attr,
2241 &dev_attr_events_async.attr,
2242 &dev_attr_events_poll_msecs.attr,
2243 NULL,
2244};
2245
2246/*
2247 * The default polling interval can be specified by the kernel
2248 * parameter block.events_dfl_poll_msecs which defaults to 0
2249 * (disable). This can also be modified runtime by writing to
Akinobu Mita1624b0b2019-07-16 21:59:35 +09002250 * /sys/module/block/parameters/events_dfl_poll_msecs.
Tejun Heo77ea8872010-12-08 20:57:37 +01002251 */
2252static int disk_events_set_dfl_poll_msecs(const char *val,
2253 const struct kernel_param *kp)
2254{
2255 struct disk_events *ev;
2256 int ret;
2257
2258 ret = param_set_ulong(val, kp);
2259 if (ret < 0)
2260 return ret;
2261
2262 mutex_lock(&disk_events_mutex);
2263
2264 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02002265 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01002266
2267 mutex_unlock(&disk_events_mutex);
2268
2269 return 0;
2270}
2271
2272static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
2273 .set = disk_events_set_dfl_poll_msecs,
2274 .get = param_get_ulong,
2275};
2276
2277#undef MODULE_PARAM_PREFIX
2278#define MODULE_PARAM_PREFIX "block."
2279
2280module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
2281 &disk_events_dfl_poll_msecs, 0644);
2282
2283/*
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002284 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
Tejun Heo77ea8872010-12-08 20:57:37 +01002285 */
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002286static void disk_alloc_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01002287{
2288 struct disk_events *ev;
2289
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002290 if (!disk->fops->check_events || !disk->events)
Tejun Heo77ea8872010-12-08 20:57:37 +01002291 return;
2292
2293 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
2294 if (!ev) {
2295 pr_warn("%s: failed to initialize events\n", disk->disk_name);
2296 return;
2297 }
2298
Tejun Heo77ea8872010-12-08 20:57:37 +01002299 INIT_LIST_HEAD(&ev->node);
2300 ev->disk = disk;
2301 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02002302 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01002303 ev->block = 1;
2304 ev->poll_msecs = -1;
2305 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2306
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002307 disk->ev = ev;
2308}
2309
2310static void disk_add_events(struct gendisk *disk)
2311{
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002312 /* FIXME: error handling */
2313 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2314 pr_warn("%s: failed to create sysfs files for events\n",
2315 disk->disk_name);
2316
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002317 if (!disk->ev)
2318 return;
2319
Tejun Heo77ea8872010-12-08 20:57:37 +01002320 mutex_lock(&disk_events_mutex);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002321 list_add_tail(&disk->ev->node, &disk_events);
Tejun Heo77ea8872010-12-08 20:57:37 +01002322 mutex_unlock(&disk_events_mutex);
2323
2324 /*
2325 * Block count is initialized to 1 and the following initial
2326 * unblock kicks it into action.
2327 */
2328 __disk_unblock_events(disk, true);
2329}
2330
2331static void disk_del_events(struct gendisk *disk)
2332{
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002333 if (disk->ev) {
2334 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01002335
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002336 mutex_lock(&disk_events_mutex);
2337 list_del_init(&disk->ev->node);
2338 mutex_unlock(&disk_events_mutex);
2339 }
Tejun Heo77ea8872010-12-08 20:57:37 +01002340
2341 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2342}
2343
2344static void disk_release_events(struct gendisk *disk)
2345{
2346 /* the block count should be 1 from disk_del_events() */
2347 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
2348 kfree(disk->ev);
2349}