blob: 56e0560738c49505fc4114e1c031cd16f1cf2cd6 [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 +030095#ifdef CONFIG_SMP
96static void part_stat_read_all(struct hd_struct *part, struct disk_stats *stat)
97{
98 int cpu;
99
100 memset(stat, 0, sizeof(struct disk_stats));
101 for_each_possible_cpu(cpu) {
102 struct disk_stats *ptr = per_cpu_ptr(part->dkstats, cpu);
103 int group;
104
105 for (group = 0; group < NR_STAT_GROUPS; group++) {
106 stat->nsecs[group] += ptr->nsecs[group];
107 stat->sectors[group] += ptr->sectors[group];
108 stat->ios[group] += ptr->ios[group];
109 stat->merges[group] += ptr->merges[group];
110 }
111
112 stat->io_ticks += ptr->io_ticks;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +0300113 }
114}
115#else /* CONFIG_SMP */
116static void part_stat_read_all(struct hd_struct *part, struct disk_stats *stat)
117{
118 memcpy(stat, &part->dkstats, sizeof(struct disk_stats));
119}
120#endif /* CONFIG_SMP */
121
Jens Axboef299b7c2017-08-08 17:51:45 -0600122void part_inc_in_flight(struct request_queue *q, struct hd_struct *part, int rw)
123{
Jens Axboe344e9ff2018-11-15 12:22:51 -0700124 if (queue_is_mq(q))
Jens Axboef299b7c2017-08-08 17:51:45 -0600125 return;
126
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500127 part_stat_local_inc(part, in_flight[rw]);
Jens Axboef299b7c2017-08-08 17:51:45 -0600128 if (part->partno)
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500129 part_stat_local_inc(&part_to_disk(part)->part0, in_flight[rw]);
Jens Axboef299b7c2017-08-08 17:51:45 -0600130}
131
132void part_dec_in_flight(struct request_queue *q, struct hd_struct *part, int rw)
133{
Jens Axboe344e9ff2018-11-15 12:22:51 -0700134 if (queue_is_mq(q))
Jens Axboef299b7c2017-08-08 17:51:45 -0600135 return;
136
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500137 part_stat_local_dec(part, in_flight[rw]);
Jens Axboef299b7c2017-08-08 17:51:45 -0600138 if (part->partno)
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500139 part_stat_local_dec(&part_to_disk(part)->part0, in_flight[rw]);
Jens Axboef299b7c2017-08-08 17:51:45 -0600140}
141
Christoph Hellwig60057712020-03-25 16:48:36 +0100142static unsigned int part_in_flight(struct request_queue *q,
143 struct hd_struct *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600144{
Christoph Hellwigb2f609e2020-05-13 12:49:33 +0200145 unsigned int inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500146 int cpu;
147
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500148 for_each_possible_cpu(cpu) {
Mikulas Patockae016b782018-12-06 11:41:21 -0500149 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
150 part_stat_local_read_cpu(part, in_flight[1], cpu);
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500151 }
Mikulas Patockae016b782018-12-06 11:41:21 -0500152 if ((int)inflight < 0)
153 inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500154
Mikulas Patockae016b782018-12-06 11:41:21 -0500155 return inflight;
Jens Axboef299b7c2017-08-08 17:51:45 -0600156}
157
Christoph Hellwig60057712020-03-25 16:48:36 +0100158static void part_in_flight_rw(struct request_queue *q, struct hd_struct *part,
159 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700160{
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500161 int cpu;
162
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500163 inflight[0] = 0;
164 inflight[1] = 0;
165 for_each_possible_cpu(cpu) {
166 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
167 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
168 }
169 if ((int)inflight[0] < 0)
170 inflight[0] = 0;
171 if ((int)inflight[1] < 0)
172 inflight[1] = 0;
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700173}
174
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200175struct hd_struct *__disk_get_part(struct gendisk *disk, int partno)
176{
177 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
178
179 if (unlikely(partno < 0 || partno >= ptbl->len))
180 return NULL;
181 return rcu_dereference(ptbl->part[partno]);
182}
183
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200184/**
185 * disk_get_part - get partition
186 * @disk: disk to look partition from
187 * @partno: partition number
188 *
189 * Look for partition @partno from @disk. If found, increment
190 * reference count and return it.
191 *
192 * CONTEXT:
193 * Don't care.
194 *
195 * RETURNS:
196 * Pointer to the found partition on success, NULL if not found.
197 */
198struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
199{
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200200 struct hd_struct *part;
Tejun Heo540eed52008-08-25 19:56:15 +0900201
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200202 rcu_read_lock();
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200203 part = __disk_get_part(disk, partno);
204 if (part)
205 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200206 rcu_read_unlock();
207
208 return part;
209}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200210
211/**
212 * disk_part_iter_init - initialize partition iterator
213 * @piter: iterator to initialize
214 * @disk: disk to iterate over
215 * @flags: DISK_PITER_* flags
216 *
217 * Initialize @piter so that it iterates over partitions of @disk.
218 *
219 * CONTEXT:
220 * Don't care.
221 */
222void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
223 unsigned int flags)
224{
Tejun Heo540eed52008-08-25 19:56:15 +0900225 struct disk_part_tbl *ptbl;
226
227 rcu_read_lock();
228 ptbl = rcu_dereference(disk->part_tbl);
229
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200230 piter->disk = disk;
231 piter->part = NULL;
232
233 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +0900234 piter->idx = ptbl->len - 1;
Tejun Heo71982a42009-04-17 08:34:48 +0200235 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200236 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200237 else
238 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200239
240 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900241
242 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200243}
244EXPORT_SYMBOL_GPL(disk_part_iter_init);
245
246/**
247 * disk_part_iter_next - proceed iterator to the next partition and return it
248 * @piter: iterator of interest
249 *
250 * Proceed @piter to the next partition and return it.
251 *
252 * CONTEXT:
253 * Don't care.
254 */
255struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
256{
Tejun Heo540eed52008-08-25 19:56:15 +0900257 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200258 int inc, end;
259
260 /* put the last partition */
261 disk_put_part(piter->part);
262 piter->part = NULL;
263
Tejun Heo540eed52008-08-25 19:56:15 +0900264 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200265 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900266 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200267
268 /* determine iteration parameters */
269 if (piter->flags & DISK_PITER_REVERSE) {
270 inc = -1;
Tejun Heo71982a42009-04-17 08:34:48 +0200271 if (piter->flags & (DISK_PITER_INCL_PART0 |
272 DISK_PITER_INCL_EMPTY_PART0))
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200273 end = -1;
274 else
275 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200276 } else {
277 inc = 1;
Tejun Heo540eed52008-08-25 19:56:15 +0900278 end = ptbl->len;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200279 }
280
281 /* iterate to the next partition */
282 for (; piter->idx != end; piter->idx += inc) {
283 struct hd_struct *part;
284
Tejun Heo540eed52008-08-25 19:56:15 +0900285 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200286 if (!part)
287 continue;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200288 if (!part_nr_sects_read(part) &&
Tejun Heo71982a42009-04-17 08:34:48 +0200289 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
290 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
291 piter->idx == 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200292 continue;
293
Tejun Heoed9e1982008-08-25 19:56:05 +0900294 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200295 piter->part = part;
296 piter->idx += inc;
297 break;
298 }
299
300 rcu_read_unlock();
301
302 return piter->part;
303}
304EXPORT_SYMBOL_GPL(disk_part_iter_next);
305
306/**
307 * disk_part_iter_exit - finish up partition iteration
308 * @piter: iter of interest
309 *
310 * Called when iteration is over. Cleans up @piter.
311 *
312 * CONTEXT:
313 * Don't care.
314 */
315void disk_part_iter_exit(struct disk_part_iter *piter)
316{
317 disk_put_part(piter->part);
318 piter->part = NULL;
319}
320EXPORT_SYMBOL_GPL(disk_part_iter_exit);
321
Jens Axboea6f23652008-10-24 12:52:42 +0200322static inline int sector_in_part(struct hd_struct *part, sector_t sector)
323{
324 return part->start_sect <= sector &&
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200325 sector < part->start_sect + part_nr_sects_read(part);
Jens Axboea6f23652008-10-24 12:52:42 +0200326}
327
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200328/**
329 * disk_map_sector_rcu - map sector to partition
330 * @disk: gendisk of interest
331 * @sector: sector to map
332 *
333 * Find out which partition @sector maps to on @disk. This is
334 * primarily used for stats accounting.
335 *
336 * CONTEXT:
Ming Leib7d6c302020-05-08 16:17:55 +0800337 * RCU read locked. The returned partition pointer is always valid
Ming Lei27eb3af2020-05-08 16:17:58 +0800338 * because its refcount is grabbed except for part0, which lifetime
339 * is same with the disk.
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200340 *
341 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900342 * Found partition on success, part0 is returned if no partition matches
Ming Leib7d6c302020-05-08 16:17:55 +0800343 * or the matched partition is being deleted.
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200344 */
345struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
346{
Tejun Heo540eed52008-08-25 19:56:15 +0900347 struct disk_part_tbl *ptbl;
Jens Axboea6f23652008-10-24 12:52:42 +0200348 struct hd_struct *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200349 int i;
350
Tejun Heo540eed52008-08-25 19:56:15 +0900351 ptbl = rcu_dereference(disk->part_tbl);
352
Jens Axboea6f23652008-10-24 12:52:42 +0200353 part = rcu_dereference(ptbl->last_lookup);
Ming Leib7d6c302020-05-08 16:17:55 +0800354 if (part && sector_in_part(part, sector) && hd_struct_try_get(part))
Jens Axboea6f23652008-10-24 12:52:42 +0200355 return part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200356
Jens Axboea6f23652008-10-24 12:52:42 +0200357 for (i = 1; i < ptbl->len; i++) {
358 part = rcu_dereference(ptbl->part[i]);
359
360 if (part && sector_in_part(part, sector)) {
Ming Leib7d6c302020-05-08 16:17:55 +0800361 /*
362 * only live partition can be cached for lookup,
363 * so use-after-free on cached & deleting partition
364 * can be avoided
365 */
366 if (!hd_struct_try_get(part))
367 break;
Jens Axboea6f23652008-10-24 12:52:42 +0200368 rcu_assign_pointer(ptbl->last_lookup, part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200369 return part;
Jens Axboea6f23652008-10-24 12:52:42 +0200370 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200371 }
Tejun Heo074a7ac2008-08-25 19:56:14 +0900372 return &disk->part0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200373}
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200374
Shin'ichiro Kawasakib53df2e2020-02-21 10:37:08 +0900375/**
376 * disk_has_partitions
377 * @disk: gendisk of interest
378 *
379 * Walk through the partition table and check if valid partition exists.
380 *
381 * CONTEXT:
382 * Don't care.
383 *
384 * RETURNS:
385 * True if the gendisk has at least one valid non-zero size partition.
386 * Otherwise false.
387 */
388bool disk_has_partitions(struct gendisk *disk)
389{
390 struct disk_part_tbl *ptbl;
391 int i;
392 bool ret = false;
393
394 rcu_read_lock();
395 ptbl = rcu_dereference(disk->part_tbl);
396
397 /* Iterate partitions skipping the whole device at index 0 */
398 for (i = 1; i < ptbl->len; i++) {
399 if (rcu_dereference(ptbl->part[i])) {
400 ret = true;
401 break;
402 }
403 }
404
405 rcu_read_unlock();
406
407 return ret;
408}
409EXPORT_SYMBOL_GPL(disk_has_partitions);
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/*
412 * Can be deleted altogether. Later.
413 *
414 */
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600415#define BLKDEV_MAJOR_HASH_SIZE 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static struct blk_major_name {
417 struct blk_major_name *next;
418 int major;
419 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800420} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100423static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Joe Korty68eef3b2006-03-31 02:30:32 -0800425 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Joe Korty68eef3b2006-03-31 02:30:32 -0800428#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200429void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800430{
Joe Korty68eef3b2006-03-31 02:30:32 -0800431 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800432
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600433 mutex_lock(&block_class_lock);
434 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
435 if (dp->major == offset)
Tejun Heocf771cb2008-09-03 09:01:09 +0200436 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600437 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
Joe Korty68eef3b2006-03-31 02:30:32 -0800439#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100441/**
442 * register_blkdev - register a new block device
443 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800444 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
445 * @major = 0, try to allocate any unused major number.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100446 * @name: the name of the new block device as a zero terminated string
447 *
448 * The @name must be unique within the system.
449 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300450 * The return value depends on the @major input parameter:
451 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800452 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
453 * then the function returns zero on success, or a negative error code
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300454 * - if any unused major number was requested with @major = 0 parameter
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100455 * then the return value is the allocated major number in range
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800456 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
457 *
458 * See Documentation/admin-guide/devices.txt for the list of allocated
459 * major numbers.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100460 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461int register_blkdev(unsigned int major, const char *name)
462{
463 struct blk_major_name **n, *p;
464 int index, ret = 0;
465
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200466 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* temporary */
469 if (major == 0) {
470 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
471 if (major_names[index] == NULL)
472 break;
473 }
474
475 if (index == 0) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500476 printk("%s: failed to get major for %s\n",
477 __func__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 ret = -EBUSY;
479 goto out;
480 }
481 major = index;
482 ret = major;
483 }
484
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600485 if (major >= BLKDEV_MAJOR_MAX) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500486 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
487 __func__, major, BLKDEV_MAJOR_MAX-1, name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600488
489 ret = -EINVAL;
490 goto out;
491 }
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
494 if (p == NULL) {
495 ret = -ENOMEM;
496 goto out;
497 }
498
499 p->major = major;
500 strlcpy(p->name, name, sizeof(p->name));
501 p->next = NULL;
502 index = major_to_index(major);
503
504 for (n = &major_names[index]; *n; n = &(*n)->next) {
505 if ((*n)->major == major)
506 break;
507 }
508 if (!*n)
509 *n = p;
510 else
511 ret = -EBUSY;
512
513 if (ret < 0) {
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800514 printk("register_blkdev: cannot get major %u for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 major, name);
516 kfree(p);
517 }
518out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200519 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return ret;
521}
522
523EXPORT_SYMBOL(register_blkdev);
524
Akinobu Mitaf4480242007-07-17 04:03:47 -0700525void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 struct blk_major_name **n;
528 struct blk_major_name *p = NULL;
529 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200531 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 for (n = &major_names[index]; *n; n = &(*n)->next)
533 if ((*n)->major == major)
534 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700535 if (!*n || strcmp((*n)->name, name)) {
536 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700537 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 p = *n;
539 *n = p->next;
540 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200541 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545EXPORT_SYMBOL(unregister_blkdev);
546
547static struct kobj_map *bdev_map;
548
Tejun Heobcce3de2008-08-25 19:47:22 +0900549/**
Tejun Heo870d6652008-08-25 19:47:25 +0900550 * blk_mangle_minor - scatter minor numbers apart
551 * @minor: minor number to mangle
552 *
553 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
554 * is enabled. Mangling twice gives the original value.
555 *
556 * RETURNS:
557 * Mangled value.
558 *
559 * CONTEXT:
560 * Don't care.
561 */
562static int blk_mangle_minor(int minor)
563{
564#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
565 int i;
566
567 for (i = 0; i < MINORBITS / 2; i++) {
568 int low = minor & (1 << i);
569 int high = minor & (1 << (MINORBITS - 1 - i));
570 int distance = MINORBITS - 1 - 2 * i;
571
572 minor ^= low | high; /* clear both bits */
573 low <<= distance; /* swap the positions */
574 high >>= distance;
575 minor |= low | high; /* and set */
576 }
577#endif
578 return minor;
579}
580
581/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900582 * blk_alloc_devt - allocate a dev_t for a partition
583 * @part: partition to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900584 * @devt: out parameter for resulting dev_t
585 *
586 * Allocate a dev_t for block device.
587 *
588 * RETURNS:
589 * 0 on success, allocated dev_t is returned in *@devt. -errno on
590 * failure.
591 *
592 * CONTEXT:
593 * Might sleep.
594 */
595int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
596{
597 struct gendisk *disk = part_to_disk(part);
Tejun Heobab998d2013-02-27 17:03:57 -0800598 int idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900599
600 /* in consecutive minor range? */
601 if (part->partno < disk->minors) {
602 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
603 return 0;
604 }
605
606 /* allocate ext devt */
Keith Busch2da78092014-08-26 09:05:36 -0600607 idr_preload(GFP_KERNEL);
608
Dan Williams4d66e5e2015-06-10 23:47:14 -0400609 spin_lock_bh(&ext_devt_lock);
Keith Busch2da78092014-08-26 09:05:36 -0600610 idx = idr_alloc(&ext_devt_idr, part, 0, NR_EXT_DEVT, GFP_NOWAIT);
Dan Williams4d66e5e2015-06-10 23:47:14 -0400611 spin_unlock_bh(&ext_devt_lock);
Keith Busch2da78092014-08-26 09:05:36 -0600612
613 idr_preload_end();
Tejun Heobab998d2013-02-27 17:03:57 -0800614 if (idx < 0)
615 return idx == -ENOSPC ? -EBUSY : idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900616
Tejun Heo870d6652008-08-25 19:47:25 +0900617 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900618 return 0;
619}
620
621/**
622 * blk_free_devt - free a dev_t
623 * @devt: dev_t to free
624 *
625 * Free @devt which was allocated using blk_alloc_devt().
626 *
627 * CONTEXT:
628 * Might sleep.
629 */
630void blk_free_devt(dev_t devt)
631{
Tejun Heobcce3de2008-08-25 19:47:22 +0900632 if (devt == MKDEV(0, 0))
633 return;
634
635 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
Dan Williams4d66e5e2015-06-10 23:47:14 -0400636 spin_lock_bh(&ext_devt_lock);
Tejun Heo870d6652008-08-25 19:47:25 +0900637 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Dan Williams4d66e5e2015-06-10 23:47:14 -0400638 spin_unlock_bh(&ext_devt_lock);
Tejun Heobcce3de2008-08-25 19:47:22 +0900639 }
640}
641
Bart Van Assche33c826e2019-05-30 17:00:47 -0700642/*
643 * We invalidate devt by assigning NULL pointer for devt in idr.
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800644 */
645void blk_invalidate_devt(dev_t devt)
646{
647 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
648 spin_lock_bh(&ext_devt_lock);
649 idr_replace(&ext_devt_idr, NULL, blk_mangle_minor(MINOR(devt)));
650 spin_unlock_bh(&ext_devt_lock);
651 }
652}
653
Tejun Heo1f014292008-08-25 19:47:23 +0900654static char *bdevt_str(dev_t devt, char *buf)
655{
656 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
657 char tbuf[BDEVT_SIZE];
658 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
659 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
660 } else
661 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
662
663 return buf;
664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/*
667 * Register device numbers dev..(dev+range-1)
668 * range must be nonzero
669 * The hash chain is sorted on range, so that subranges can override.
670 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200671void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct kobject *(*probe)(dev_t, int *, void *),
673 int (*lock)(dev_t, void *), void *data)
674{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200675 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678EXPORT_SYMBOL(blk_register_region);
679
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200680void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200682 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685EXPORT_SYMBOL(blk_unregister_region);
686
Tejun Heocf771cb2008-09-03 09:01:09 +0200687static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200690
Tejun Heoed9e1982008-08-25 19:56:05 +0900691 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200694static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct gendisk *p = data;
697
Jan Kara3079c222018-02-26 13:01:38 +0100698 if (!get_disk_and_module(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -1;
700 return 0;
701}
702
Hannes Reineckefef912b2018-09-28 08:17:19 +0200703static void register_disk(struct device *parent, struct gendisk *disk,
704 const struct attribute_group **groups)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100705{
706 struct device *ddev = disk_to_dev(disk);
707 struct block_device *bdev;
708 struct disk_part_iter piter;
709 struct hd_struct *part;
710 int err;
711
Dan Williamse63a46b2016-06-15 18:17:27 -0700712 ddev->parent = parent;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100713
Kees Cookffc8b302013-07-03 15:01:14 -0700714 dev_set_name(ddev, "%s", disk->disk_name);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100715
716 /* delay uevents, until we scanned partition table */
717 dev_set_uevent_suppress(ddev, 1);
718
Hannes Reineckefef912b2018-09-28 08:17:19 +0200719 if (groups) {
720 WARN_ON(ddev->groups);
721 ddev->groups = groups;
722 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100723 if (device_add(ddev))
724 return;
725 if (!sysfs_deprecated) {
726 err = sysfs_create_link(block_depr, &ddev->kobj,
727 kobject_name(&ddev->kobj));
728 if (err) {
729 device_del(ddev);
730 return;
731 }
732 }
Ming Lei25e823c2013-02-22 16:34:13 -0800733
734 /*
735 * avoid probable deadlock caused by allocating memory with
736 * GFP_KERNEL in runtime_resume callback of its all ancestor
737 * devices
738 */
739 pm_runtime_set_memalloc_noio(ddev, true);
740
Tejun Heod2bf1b62010-12-08 20:57:36 +0100741 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
742 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
743
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300744 if (disk->flags & GENHD_FL_HIDDEN) {
745 dev_set_uevent_suppress(ddev, 0);
746 return;
747 }
748
Tejun Heod2bf1b62010-12-08 20:57:36 +0100749 /* No minors to use for partitions */
Tejun Heod27769e2011-08-23 20:01:04 +0200750 if (!disk_part_scan_enabled(disk))
Tejun Heod2bf1b62010-12-08 20:57:36 +0100751 goto exit;
752
753 /* No such device (e.g., media were just removed) */
754 if (!get_capacity(disk))
755 goto exit;
756
757 bdev = bdget_disk(disk, 0);
758 if (!bdev)
759 goto exit;
760
761 bdev->bd_invalidated = 1;
762 err = blkdev_get(bdev, FMODE_READ, NULL);
763 if (err < 0)
764 goto exit;
765 blkdev_put(bdev, FMODE_READ);
766
767exit:
768 /* announce disk after possible partitions are created */
769 dev_set_uevent_suppress(ddev, 0);
770 kobject_uevent(&ddev->kobj, KOBJ_ADD);
771
772 /* announce possible partitions */
773 disk_part_iter_init(&piter, disk, 0);
774 while ((part = disk_part_iter_next(&piter)))
775 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
776 disk_part_iter_exit(&piter);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300777
zhengbin4d7c1d32019-02-20 21:27:05 +0800778 if (disk->queue->backing_dev_info->dev) {
779 err = sysfs_create_link(&ddev->kobj,
780 &disk->queue->backing_dev_info->dev->kobj,
781 "bdi");
782 WARN_ON(err);
783 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786/**
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500787 * __device_add_disk - add disk information to kernel list
Dan Williamse63a46b2016-06-15 18:17:27 -0700788 * @parent: parent device for the disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * @disk: per-device partitioning information
Hannes Reineckefef912b2018-09-28 08:17:19 +0200790 * @groups: Additional per-device sysfs groups
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500791 * @register_queue: register the queue if set to true
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 *
793 * This function registers the partitioning information in @disk
794 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900795 *
796 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500798static void __device_add_disk(struct device *parent, struct gendisk *disk,
Hannes Reineckefef912b2018-09-28 08:17:19 +0200799 const struct attribute_group **groups,
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500800 bool register_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900802 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400803 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700804
Damien Le Moal737eb782019-09-05 18:51:33 +0900805 /*
806 * The disk queue should now be all set with enough information about
807 * the device for the elevator code to pick an adequate default
808 * elevator if one is needed, that is, for devices requesting queue
809 * registration.
810 */
811 if (register_queue)
812 elevator_init_mq(disk->queue);
813
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900814 /* minors == 0 indicates to use ext devt from part0 and should
815 * be accompanied with EXT_DEVT flag. Make sure all
816 * parameters make sense.
817 */
818 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300819 WARN_ON(!disk->minors &&
820 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900823
824 retval = blk_alloc_devt(&disk->part0, &devt);
825 if (retval) {
826 WARN_ON(1);
827 return;
828 }
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900829 disk->major = MAJOR(devt);
830 disk->first_minor = MINOR(devt);
831
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +0100832 disk_alloc_events(disk);
833
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300834 if (disk->flags & GENHD_FL_HIDDEN) {
835 /*
836 * Don't let hidden disks show up in /proc/partitions,
837 * and don't bother scanning for partitions either.
838 */
839 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
840 disk->flags |= GENHD_FL_NO_PART_SCAN;
841 } else {
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200842 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
843 struct device *dev = disk_to_dev(disk);
weiping zhang3a921682017-10-31 18:38:59 +0800844 int ret;
845
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300846 /* Register BDI before referencing it from bdev */
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200847 dev->devt = devt;
848 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
weiping zhang3a921682017-10-31 18:38:59 +0800849 WARN_ON(ret);
Christoph Hellwig3c5d2022020-05-04 14:47:59 +0200850 bdi_set_owner(bdi, dev);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300851 blk_register_region(disk_devt(disk), disk->minors, NULL,
852 exact_match, exact_lock, disk);
853 }
Hannes Reineckefef912b2018-09-28 08:17:19 +0200854 register_disk(parent, disk, groups);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500855 if (register_queue)
856 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700857
Tejun Heo523e1d32011-10-19 14:31:07 +0200858 /*
859 * Take an extra ref on queue which will be put on disk_release()
860 * so that it sticks around as long as @disk is there.
861 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100862 WARN_ON_ONCE(!blk_get_queue(disk->queue));
Tejun Heo523e1d32011-10-19 14:31:07 +0200863
Tejun Heo77ea8872010-12-08 20:57:37 +0100864 disk_add_events(disk);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400865 blk_integrity_add(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500867
Hannes Reineckefef912b2018-09-28 08:17:19 +0200868void device_add_disk(struct device *parent, struct gendisk *disk,
869 const struct attribute_group **groups)
870
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500871{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200872 __device_add_disk(parent, disk, groups, true);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500873}
Dan Williamse63a46b2016-06-15 18:17:27 -0700874EXPORT_SYMBOL(device_add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500876void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
877{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200878 __device_add_disk(parent, disk, NULL, false);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500879}
880EXPORT_SYMBOL(device_add_disk_no_queue_reg);
881
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200882static void invalidate_partition(struct gendisk *disk, int partno)
883{
884 struct block_device *bdev;
885
886 bdev = bdget_disk(disk, partno);
887 if (!bdev)
888 return;
889
890 fsync_bdev(bdev);
891 __invalidate_device(bdev, true);
Christoph Hellwig9bc5c392020-04-14 09:29:02 +0200892
893 /*
894 * Unhash the bdev inode for this device so that it gets evicted as soon
895 * as last inode reference is dropped.
896 */
897 remove_inode_hash(bdev->bd_inode);
Christoph Hellwig02d33b62020-04-14 09:29:01 +0200898 bdput(bdev);
899}
900
Tejun Heod2bf1b62010-12-08 20:57:36 +0100901void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Tejun Heod2bf1b62010-12-08 20:57:36 +0100903 struct disk_part_iter piter;
904 struct hd_struct *part;
905
Martin K. Petersen25520d52015-10-21 13:19:49 -0400906 blk_integrity_del(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +0100907 disk_del_events(disk);
908
Jan Kara56c09082018-02-26 13:01:41 +0100909 /*
910 * Block lookups of the disk until all bdevs are unhashed and the
911 * disk is marked as dead (GENHD_FL_UP cleared).
912 */
913 down_write(&disk->lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100914 /* invalidate stuff */
915 disk_part_iter_init(&piter, disk,
916 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
917 while ((part = disk_part_iter_next(&piter))) {
918 invalidate_partition(disk, part->partno);
Christoph Hellwigcddae802020-04-14 09:28:54 +0200919 delete_partition(disk, part);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100920 }
921 disk_part_iter_exit(&piter);
922
923 invalidate_partition(disk, 0);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100924 set_capacity(disk, 0);
925 disk->flags &= ~GENHD_FL_UP;
Jan Kara56c09082018-02-26 13:01:41 +0100926 up_write(&disk->lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100927
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300928 if (!(disk->flags & GENHD_FL_HIDDEN))
929 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Jan Kara90f16fd2017-03-08 17:48:33 +0100930 if (disk->queue) {
931 /*
932 * Unregister bdi before releasing device numbers (as they can
933 * get reused and we'd get clashes in sysfs).
934 */
Mike Snitzerbc8d0622018-01-09 20:46:49 -0500935 if (!(disk->flags & GENHD_FL_HIDDEN))
936 bdi_unregister(disk->queue->backing_dev_info);
Jan Kara90f16fd2017-03-08 17:48:33 +0100937 blk_unregister_queue(disk);
938 } else {
939 WARN_ON(1);
940 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100941
Hannes Reinecke17eac092017-11-09 17:57:06 +0100942 if (!(disk->flags & GENHD_FL_HIDDEN))
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300943 blk_unregister_region(disk_devt(disk), disk->minors);
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800944 /*
945 * Remove gendisk pointer from idr so that it cannot be looked up
946 * while RCU period before freeing gendisk is running to prevent
947 * use-after-free issues. Note that the device number stays
948 * "in-use" until we really free the gendisk.
949 */
950 blk_invalidate_devt(disk_devt(disk));
Tejun Heod2bf1b62010-12-08 20:57:36 +0100951
952 kobject_put(disk->part0.holder_dir);
953 kobject_put(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 part_stat_set_all(&disk->part0, 0);
Tejun Heoed9e1982008-08-25 19:56:05 +0900956 disk->part0.stamp = 0;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100957 if (!sysfs_deprecated)
958 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
Ming Lei25e823c2013-02-22 16:34:13 -0800959 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100960 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100962EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Vishal Verma99e66082016-01-09 08:36:51 -0800964/* sysfs access to bad-blocks list. */
965static ssize_t disk_badblocks_show(struct device *dev,
966 struct device_attribute *attr,
967 char *page)
968{
969 struct gendisk *disk = dev_to_disk(dev);
970
971 if (!disk->bb)
972 return sprintf(page, "\n");
973
974 return badblocks_show(disk->bb, page, 0);
975}
976
977static ssize_t disk_badblocks_store(struct device *dev,
978 struct device_attribute *attr,
979 const char *page, size_t len)
980{
981 struct gendisk *disk = dev_to_disk(dev);
982
983 if (!disk->bb)
984 return -ENXIO;
985
986 return badblocks_store(disk->bb, page, len, 0);
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/**
990 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200991 * @devt: device to get partitioning information for
Randy Dunlap496aa8a2008-10-16 07:46:23 +0200992 * @partno: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 *
994 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200995 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200997struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Tejun Heobcce3de2008-08-25 19:47:22 +0900999 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001000
Tejun Heobcce3de2008-08-25 19:47:22 +09001001 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
1002 struct kobject *kobj;
1003
1004 kobj = kobj_lookup(bdev_map, devt, partno);
1005 if (kobj)
1006 disk = dev_to_disk(kobj_to_dev(kobj));
1007 } else {
1008 struct hd_struct *part;
1009
Dan Williams4d66e5e2015-06-10 23:47:14 -04001010 spin_lock_bh(&ext_devt_lock);
Tejun Heo870d6652008-08-25 19:47:25 +09001011 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Jan Kara3079c222018-02-26 13:01:38 +01001012 if (part && get_disk_and_module(part_to_disk(part))) {
Tejun Heobcce3de2008-08-25 19:47:22 +09001013 *partno = part->partno;
1014 disk = part_to_disk(part);
1015 }
Dan Williams4d66e5e2015-06-10 23:47:14 -04001016 spin_unlock_bh(&ext_devt_lock);
Tejun Heobcce3de2008-08-25 19:47:22 +09001017 }
1018
Jan Kara56c09082018-02-26 13:01:41 +01001019 if (!disk)
1020 return NULL;
1021
1022 /*
1023 * Synchronize with del_gendisk() to not return disk that is being
1024 * destroyed.
1025 */
1026 down_read(&disk->lookup_sem);
1027 if (unlikely((disk->flags & GENHD_FL_HIDDEN) ||
1028 !(disk->flags & GENHD_FL_UP))) {
1029 up_read(&disk->lookup_sem);
Jan Kara9df6c292018-02-26 13:01:39 +01001030 put_disk_and_module(disk);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001031 disk = NULL;
Jan Kara56c09082018-02-26 13:01:41 +01001032 } else {
1033 up_read(&disk->lookup_sem);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001034 }
Tejun Heobcce3de2008-08-25 19:47:22 +09001035 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Tejun Heof331c022008-09-03 09:01:48 +02001038/**
1039 * bdget_disk - do bdget() by gendisk and partition number
1040 * @disk: gendisk of interest
1041 * @partno: partition number
1042 *
1043 * Find partition @partno from @disk, do bdget() on it.
1044 *
1045 * CONTEXT:
1046 * Don't care.
1047 *
1048 * RETURNS:
1049 * Resulting block_device on success, NULL on failure.
1050 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +02001051struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +02001052{
Tejun Heo548b10e2008-08-29 09:01:47 +02001053 struct hd_struct *part;
1054 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +02001055
Tejun Heo548b10e2008-08-29 09:01:47 +02001056 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001057 if (part)
Tejun Heo548b10e2008-08-29 09:01:47 +02001058 bdev = bdget(part_devt(part));
1059 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +02001060
Tejun Heo548b10e2008-08-29 09:01:47 +02001061 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +02001062}
1063EXPORT_SYMBOL(bdget_disk);
1064
Dave Gilbertdd2a3452007-05-09 02:33:24 -07001065/*
1066 * print a full list of all partitions - intended for places where the root
1067 * filesystem can't be mounted and thus to give the victim some idea of what
1068 * went wrong
1069 */
1070void __init printk_all_partitions(void)
1071{
Tejun Heodef4e382008-09-03 08:57:12 +02001072 struct class_dev_iter iter;
1073 struct device *dev;
1074
1075 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1076 while ((dev = class_dev_iter_next(&iter))) {
1077 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001078 struct disk_part_iter piter;
1079 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +09001080 char name_buf[BDEVNAME_SIZE];
1081 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +02001082
1083 /*
1084 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001085 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +02001086 */
1087 if (get_capacity(disk) == 0 ||
1088 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
1089 continue;
1090
1091 /*
1092 * Note, unlike /proc/partitions, I am showing the
1093 * numbers in hex - the same format as the root=
1094 * option takes.
1095 */
Tejun Heo074a7ac2008-08-25 19:56:14 +09001096 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
1097 while ((part = disk_part_iter_next(&piter))) {
1098 bool is_part0 = part == &disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +02001099
Will Drewryb5af9212010-08-31 15:47:07 -05001100 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Tejun Heo1f014292008-08-25 19:47:23 +09001101 bdevt_str(part_devt(part), devt_buf),
Vivek Goyalc83f6bf2012-08-01 12:24:18 +02001102 (unsigned long long)part_nr_sects_read(part) >> 1
1103 , disk_name(disk, part->partno, name_buf),
Stephen Warren1ad7e892012-11-08 16:12:25 -08001104 part->info ? part->info->uuid : "");
Tejun Heo074a7ac2008-08-25 19:56:14 +09001105 if (is_part0) {
Dan Williams52c44d92016-06-15 19:43:07 -07001106 if (dev->parent && dev->parent->driver)
Tejun Heo074a7ac2008-08-25 19:56:14 +09001107 printk(" driver: %s\n",
Dan Williams52c44d92016-06-15 19:43:07 -07001108 dev->parent->driver->name);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001109 else
1110 printk(" (driver?)\n");
1111 } else
1112 printk("\n");
1113 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001114 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +02001115 }
1116 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -07001117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119#ifdef CONFIG_PROC_FS
1120/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +02001121static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001122{
Tejun Heodef4e382008-09-03 08:57:12 +02001123 loff_t skip = *pos;
1124 struct class_dev_iter *iter;
1125 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001126
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +02001127 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +02001128 if (!iter)
1129 return ERR_PTR(-ENOMEM);
1130
1131 seqf->private = iter;
1132 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
1133 do {
1134 dev = class_dev_iter_next(iter);
1135 if (!dev)
1136 return NULL;
1137 } while (skip--);
1138
1139 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001140}
1141
Tejun Heodef4e382008-09-03 08:57:12 +02001142static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001144 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -04001145
Tejun Heodef4e382008-09-03 08:57:12 +02001146 (*pos)++;
1147 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +02001148 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001149 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +02001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return NULL;
1152}
1153
Tejun Heodef4e382008-09-03 08:57:12 +02001154static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -04001155{
Tejun Heodef4e382008-09-03 08:57:12 +02001156 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -04001157
Tejun Heodef4e382008-09-03 08:57:12 +02001158 /* stop is called even after start failed :-( */
1159 if (iter) {
1160 class_dev_iter_exit(iter);
1161 kfree(iter);
Vegard Nossum77da1602016-07-29 10:40:31 +02001162 seqf->private = NULL;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Tejun Heodef4e382008-09-03 08:57:12 +02001166static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Jianpeng Ma06768062012-08-03 10:42:00 +02001168 void *p;
Tejun Heodef4e382008-09-03 08:57:12 +02001169
1170 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +01001171 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +02001172 seq_puts(seqf, "major minor #blocks name\n\n");
1173 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Tejun Heocf771cb2008-09-03 09:01:09 +02001176static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001179 struct disk_part_iter piter;
1180 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 char buf[BDEVNAME_SIZE];
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +02001184 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +02001185 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return 0;
1187 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
1188 return 0;
1189
1190 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +09001191 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001192 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +09001193 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +02001194 MAJOR(part_devt(part)), MINOR(part_devt(part)),
Vivek Goyalc83f6bf2012-08-01 12:24:18 +02001195 (unsigned long long)part_nr_sects_read(part) >> 1,
Tejun Heof331c022008-09-03 09:01:48 +02001196 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001197 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 return 0;
1200}
1201
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001202static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001203 .start = show_partition_start,
1204 .next = disk_seqf_next,
1205 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001206 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207};
1208#endif
1209
1210
Tejun Heocf771cb2008-09-03 09:01:09 +02001211static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001213 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001215 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return NULL;
1217}
1218
1219static int __init genhd_device_init(void)
1220{
Dan Williamse105b8b2008-04-21 10:51:07 -07001221 int error;
1222
1223 block_class.dev_kobj = sysfs_dev_block_kobj;
1224 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -07001225 if (unlikely(error))
1226 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001227 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001229
Zhang, Yanmin561ec682008-11-14 08:26:30 +01001230 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1231
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001232 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +02001233 if (!sysfs_deprecated)
1234 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -08001235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238subsys_initcall(genhd_device_init);
1239
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001240static ssize_t disk_range_show(struct device *dev,
1241 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001243 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001245 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Tejun Heo1f014292008-08-25 19:47:23 +09001248static ssize_t disk_ext_range_show(struct device *dev,
1249 struct device_attribute *attr, char *buf)
1250{
1251 struct gendisk *disk = dev_to_disk(dev);
1252
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001253 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +09001254}
1255
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001256static ssize_t disk_removable_show(struct device *dev,
1257 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +02001258{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001259 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001260
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001261 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001263}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001265static ssize_t disk_hidden_show(struct device *dev,
1266 struct device_attribute *attr, char *buf)
1267{
1268 struct gendisk *disk = dev_to_disk(dev);
1269
1270 return sprintf(buf, "%d\n",
1271 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1272}
1273
Kay Sievers1c9ce522008-06-13 09:41:00 +02001274static ssize_t disk_ro_show(struct device *dev,
1275 struct device_attribute *attr, char *buf)
1276{
1277 struct gendisk *disk = dev_to_disk(dev);
1278
Tejun Heob7db9952008-08-25 19:56:10 +09001279 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +02001280}
1281
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001282ssize_t part_size_show(struct device *dev,
1283 struct device_attribute *attr, char *buf)
1284{
1285 struct hd_struct *p = dev_to_part(dev);
1286
1287 return sprintf(buf, "%llu\n",
1288 (unsigned long long)part_nr_sects_read(p));
1289}
1290
1291ssize_t part_stat_show(struct device *dev,
1292 struct device_attribute *attr, char *buf)
1293{
1294 struct hd_struct *p = dev_to_part(dev);
1295 struct request_queue *q = part_to_disk(p)->queue;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001296 struct disk_stats stat;
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001297 unsigned int inflight;
1298
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001299 part_stat_read_all(p, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001300 if (queue_is_mq(q))
1301 inflight = blk_mq_in_flight(q, p);
1302 else
1303 inflight = part_in_flight(q, p);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001304
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001305 return sprintf(buf,
1306 "%8lu %8lu %8llu %8u "
1307 "%8lu %8lu %8llu %8u "
1308 "%8u %8u %8u "
1309 "%8lu %8lu %8llu %8u "
1310 "%8lu %8u"
1311 "\n",
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001312 stat.ios[STAT_READ],
1313 stat.merges[STAT_READ],
1314 (unsigned long long)stat.sectors[STAT_READ],
1315 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
1316 stat.ios[STAT_WRITE],
1317 stat.merges[STAT_WRITE],
1318 (unsigned long long)stat.sectors[STAT_WRITE],
1319 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001320 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001321 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001322 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1323 stat.nsecs[STAT_WRITE] +
1324 stat.nsecs[STAT_DISCARD] +
1325 stat.nsecs[STAT_FLUSH],
1326 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001327 stat.ios[STAT_DISCARD],
1328 stat.merges[STAT_DISCARD],
1329 (unsigned long long)stat.sectors[STAT_DISCARD],
1330 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
1331 stat.ios[STAT_FLUSH],
1332 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001333}
1334
1335ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
1336 char *buf)
1337{
1338 struct hd_struct *p = dev_to_part(dev);
1339 struct request_queue *q = part_to_disk(p)->queue;
1340 unsigned int inflight[2];
1341
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001342 if (queue_is_mq(q))
1343 blk_mq_in_flight_rw(q, p, inflight);
1344 else
1345 part_in_flight_rw(q, p, inflight);
1346
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001347 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1348}
1349
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001350static ssize_t disk_capability_show(struct device *dev,
1351 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001352{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001353 struct gendisk *disk = dev_to_disk(dev);
1354
1355 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001356}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001357
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001358static ssize_t disk_alignment_offset_show(struct device *dev,
1359 struct device_attribute *attr,
1360 char *buf)
1361{
1362 struct gendisk *disk = dev_to_disk(dev);
1363
1364 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1365}
1366
Martin K. Petersen86b37282009-11-10 11:50:21 +01001367static ssize_t disk_discard_alignment_show(struct device *dev,
1368 struct device_attribute *attr,
1369 char *buf)
1370{
1371 struct gendisk *disk = dev_to_disk(dev);
1372
Martin K. Petersendd3d1452010-01-11 03:21:48 -05001373 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +01001374}
1375
Joe Perches5657a812018-05-24 13:38:59 -06001376static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1377static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1378static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1379static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1380static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1381static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1382static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1383static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1384static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1385static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1386static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1387static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001388
Akinobu Mitac17bb492006-12-08 02:39:46 -08001389#ifdef CONFIG_FAIL_MAKE_REQUEST
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001390ssize_t part_fail_show(struct device *dev,
1391 struct device_attribute *attr, char *buf)
1392{
1393 struct hd_struct *p = dev_to_part(dev);
1394
1395 return sprintf(buf, "%d\n", p->make_it_fail);
1396}
1397
1398ssize_t part_fail_store(struct device *dev,
1399 struct device_attribute *attr,
1400 const char *buf, size_t count)
1401{
1402 struct hd_struct *p = dev_to_part(dev);
1403 int i;
1404
1405 if (count > 0 && sscanf(buf, "%d", &i) > 0)
1406 p->make_it_fail = (i == 0) ? 0 : 1;
1407
1408 return count;
1409}
1410
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001411static struct device_attribute dev_attr_fail =
Joe Perches5657a812018-05-24 13:38:59 -06001412 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
Christoph Hellwig3ad5cee2020-03-24 08:25:13 +01001413#endif /* CONFIG_FAIL_MAKE_REQUEST */
1414
Jens Axboe581d4e22008-09-14 05:56:33 -07001415#ifdef CONFIG_FAIL_IO_TIMEOUT
1416static struct device_attribute dev_attr_fail_timeout =
Joe Perches5657a812018-05-24 13:38:59 -06001417 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
Jens Axboe581d4e22008-09-14 05:56:33 -07001418#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001419
1420static struct attribute *disk_attrs[] = {
1421 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +09001422 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001423 &dev_attr_removable.attr,
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001424 &dev_attr_hidden.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001425 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001426 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001427 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001428 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001429 &dev_attr_capability.attr,
1430 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001431 &dev_attr_inflight.attr,
Vishal Verma99e66082016-01-09 08:36:51 -08001432 &dev_attr_badblocks.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001433#ifdef CONFIG_FAIL_MAKE_REQUEST
1434 &dev_attr_fail.attr,
1435#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001436#ifdef CONFIG_FAIL_IO_TIMEOUT
1437 &dev_attr_fail_timeout.attr,
1438#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001439 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440};
1441
Dan Williams9438b3e02017-04-27 14:46:26 -07001442static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1443{
1444 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1445 struct gendisk *disk = dev_to_disk(dev);
1446
1447 if (a == &dev_attr_badblocks.attr && !disk->bb)
1448 return 0;
1449 return a->mode;
1450}
1451
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001452static struct attribute_group disk_attr_group = {
1453 .attrs = disk_attrs,
Dan Williams9438b3e02017-04-27 14:46:26 -07001454 .is_visible = disk_visible,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001455};
1456
David Brownella4dbd672009-06-24 10:06:31 -07001457static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001458 &disk_attr_group,
1459 NULL
1460};
1461
Tejun Heo540eed52008-08-25 19:56:15 +09001462/**
1463 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1464 * @disk: disk to replace part_tbl for
1465 * @new_ptbl: new part_tbl to install
1466 *
1467 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1468 * original ptbl is freed using RCU callback.
1469 *
1470 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001471 * Matching bd_mutex locked or the caller is the only user of @disk.
Tejun Heo540eed52008-08-25 19:56:15 +09001472 */
1473static void disk_replace_part_tbl(struct gendisk *disk,
1474 struct disk_part_tbl *new_ptbl)
1475{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001476 struct disk_part_tbl *old_ptbl =
1477 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001478
1479 rcu_assign_pointer(disk->part_tbl, new_ptbl);
Jens Axboea6f23652008-10-24 12:52:42 +02001480
1481 if (old_ptbl) {
1482 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
Lai Jiangshan57bdfbf2011-03-18 11:42:58 +08001483 kfree_rcu(old_ptbl, rcu_head);
Jens Axboea6f23652008-10-24 12:52:42 +02001484 }
Tejun Heo540eed52008-08-25 19:56:15 +09001485}
1486
1487/**
1488 * disk_expand_part_tbl - expand disk->part_tbl
1489 * @disk: disk to expand part_tbl for
1490 * @partno: expand such that this partno can fit in
1491 *
1492 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1493 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1494 *
1495 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001496 * Matching bd_mutex locked or the caller is the only user of @disk.
1497 * Might sleep.
Tejun Heo540eed52008-08-25 19:56:15 +09001498 *
1499 * RETURNS:
1500 * 0 on success, -errno on failure.
1501 */
1502int disk_expand_part_tbl(struct gendisk *disk, int partno)
1503{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001504 struct disk_part_tbl *old_ptbl =
1505 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001506 struct disk_part_tbl *new_ptbl;
1507 int len = old_ptbl ? old_ptbl->len : 0;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001508 int i, target;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001509
1510 /*
1511 * check for int overflow, since we can get here from blkpg_ioctl()
1512 * with a user passed 'partno'.
1513 */
1514 target = partno + 1;
1515 if (target < 0)
1516 return -EINVAL;
Tejun Heo540eed52008-08-25 19:56:15 +09001517
1518 /* disk_max_parts() is zero during initialization, ignore if so */
1519 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1520 return -EINVAL;
1521
1522 if (target <= len)
1523 return 0;
1524
Gustavo A. R. Silva78b90a22019-05-31 13:47:54 -05001525 new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
1526 disk->node_id);
Tejun Heo540eed52008-08-25 19:56:15 +09001527 if (!new_ptbl)
1528 return -ENOMEM;
1529
Tejun Heo540eed52008-08-25 19:56:15 +09001530 new_ptbl->len = target;
1531
1532 for (i = 0; i < len; i++)
1533 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1534
1535 disk_replace_part_tbl(disk, new_ptbl);
1536 return 0;
1537}
1538
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001539static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001541 struct gendisk *disk = dev_to_disk(dev);
1542
Keith Busch2da78092014-08-26 09:05:36 -06001543 blk_free_devt(dev->devt);
Tejun Heo77ea8872010-12-08 20:57:37 +01001544 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +09001546 disk_replace_part_tbl(disk, NULL);
Ming Leib54e5ed2015-07-16 11:16:44 +08001547 hd_free_part(&disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001548 if (disk->queue)
1549 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 kfree(disk);
1551}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001552struct class block_class = {
1553 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554};
1555
Kay Sievers3c2670e2013-04-06 09:56:00 -07001556static char *block_devnode(struct device *dev, umode_t *mode,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001557 kuid_t *uid, kgid_t *gid)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001558{
1559 struct gendisk *disk = dev_to_disk(dev);
1560
Christoph Hellwig348e1142020-03-27 09:07:17 +01001561 if (disk->fops->devnode)
1562 return disk->fops->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001563 return NULL;
1564}
1565
Bart Van Asscheedf8ff52017-06-20 11:15:48 -07001566static const struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001567 .name = "disk",
1568 .groups = disk_attr_groups,
1569 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001570 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571};
1572
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001573#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001574/*
1575 * aggregate disk stat collector. Uses the same stats that the sysfs
1576 * entries do, above, but makes them available through one seq_file.
1577 *
1578 * The output looks suspiciously like /proc/partitions with a bunch of
1579 * extra fields.
1580 */
1581static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001584 struct disk_part_iter piter;
1585 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 char buf[BDEVNAME_SIZE];
Mikulas Patockae016b782018-12-06 11:41:21 -05001587 unsigned int inflight;
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001588 struct disk_stats stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001591 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001592 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 " rio rmerge rsect ruse wio wmerge "
1594 "wsect wuse running use aveq"
1595 "\n\n");
1596 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001597
Tejun Heo71982a42009-04-17 08:34:48 +02001598 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001599 while ((hd = disk_part_iter_next(&piter))) {
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001600 part_stat_read_all(hd, &stat);
Christoph Hellwigb2f609e2020-05-13 12:49:33 +02001601 if (queue_is_mq(gp->queue))
1602 inflight = blk_mq_in_flight(gp->queue, hd);
1603 else
1604 inflight = part_in_flight(gp->queue, hd);
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001605
Michael Callahanbdca3c82018-07-18 04:47:40 -07001606 seq_printf(seqf, "%4d %7d %s "
1607 "%lu %lu %lu %u "
1608 "%lu %lu %lu %u "
1609 "%u %u %u "
Konstantin Khlebnikovb6866312019-11-21 13:40:26 +03001610 "%lu %lu %lu %u "
1611 "%lu %u"
1612 "\n",
Tejun Heof331c022008-09-03 09:01:48 +02001613 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1614 disk_name(gp, hd->partno, buf),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001615 stat.ios[STAT_READ],
1616 stat.merges[STAT_READ],
1617 stat.sectors[STAT_READ],
1618 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1619 NSEC_PER_MSEC),
1620 stat.ios[STAT_WRITE],
1621 stat.merges[STAT_WRITE],
1622 stat.sectors[STAT_WRITE],
1623 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1624 NSEC_PER_MSEC),
Mikulas Patockae016b782018-12-06 11:41:21 -05001625 inflight,
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001626 jiffies_to_msecs(stat.io_ticks),
Konstantin Khlebnikov8cd5b8f2020-03-25 16:07:08 +03001627 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1628 stat.nsecs[STAT_WRITE] +
1629 stat.nsecs[STAT_DISCARD] +
1630 stat.nsecs[STAT_FLUSH],
1631 NSEC_PER_MSEC),
Konstantin Khlebnikovea18e0f2020-03-25 16:07:06 +03001632 stat.ios[STAT_DISCARD],
1633 stat.merges[STAT_DISCARD],
1634 stat.sectors[STAT_DISCARD],
1635 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1636 NSEC_PER_MSEC),
1637 stat.ios[STAT_FLUSH],
1638 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1639 NSEC_PER_MSEC)
Jerome Marchand28f39d52008-02-08 11:04:56 +01001640 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001642 disk_part_iter_exit(&piter);
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
1645}
1646
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001647static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001648 .start = disk_seqf_start,
1649 .next = disk_seqf_next,
1650 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 .show = diskstats_show
1652};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001653
1654static int __init proc_genhd_init(void)
1655{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001656 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1657 proc_create_seq("partitions", 0, NULL, &partitions_op);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001658 return 0;
1659}
1660module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001661#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Tejun Heocf771cb2008-09-03 09:01:09 +02001663dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001664{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001665 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001666 struct class_dev_iter iter;
1667 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001668
Tejun Heodef4e382008-09-03 08:57:12 +02001669 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1670 while ((dev = class_dev_iter_next(&iter))) {
1671 struct gendisk *disk = dev_to_disk(dev);
Tejun Heo548b10e2008-08-29 09:01:47 +02001672 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001673
Kay Sievers3ada8b72009-01-06 10:44:43 -08001674 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001675 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001676
Neil Brown41b8c852009-02-18 10:33:59 +01001677 if (partno < disk->minors) {
1678 /* We need to return the right devno, even
1679 * if the partition doesn't exist yet.
1680 */
1681 devt = MKDEV(MAJOR(dev->devt),
1682 MINOR(dev->devt) + partno);
1683 break;
1684 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001685 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001686 if (part) {
Tejun Heof331c022008-09-03 09:01:48 +02001687 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001688 disk_put_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001689 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001690 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001691 disk_put_part(part);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001692 }
Tejun Heodef4e382008-09-03 08:57:12 +02001693 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001694 return devt;
1695}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001696
Byungchul Parke319e1f2017-10-25 17:56:05 +09001697struct gendisk *__alloc_disk_node(int minors, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001698{
1699 struct gendisk *disk;
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001700 struct disk_part_tbl *ptbl;
Christoph Lameter19460892005-06-23 00:08:19 -07001701
Christoph Hellwigde65b012017-08-23 19:10:29 +02001702 if (minors > DISK_MAX_PARTS) {
1703 printk(KERN_ERR
Randy Dunlap7fb52622017-11-18 17:43:38 -08001704 "block: can't allocate more than %d partitions\n",
Christoph Hellwigde65b012017-08-23 19:10:29 +02001705 DISK_MAX_PARTS);
1706 minors = DISK_MAX_PARTS;
1707 }
Christoph Lameter19460892005-06-23 00:08:19 -07001708
Joe Perchesc1b511e2013-08-29 15:21:42 -07001709 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (disk) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001711 if (!init_part_stats(&disk->part0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 kfree(disk);
1713 return NULL;
1714 }
Jan Kara56c09082018-02-26 13:01:41 +01001715 init_rwsem(&disk->lookup_sem);
Cheng Renquanbf91db12008-11-20 08:37:37 +01001716 disk->node_id = node_id;
Tejun Heo540eed52008-08-25 19:56:15 +09001717 if (disk_expand_part_tbl(disk, 0)) {
1718 free_part_stats(&disk->part0);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001719 kfree(disk);
1720 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001722 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
1723 rcu_assign_pointer(ptbl->part[0], &disk->part0);
Jens Axboe6c23a962011-01-07 08:43:37 +01001724
Vivek Goyalc83f6bf2012-08-01 12:24:18 +02001725 /*
1726 * set_capacity() and get_capacity() currently don't use
1727 * seqcounter to read/update the part0->nr_sects. Still init
1728 * the counter as we can read the sectors in IO submission
1729 * patch using seqence counters.
1730 *
1731 * TODO: Ideally set_capacity() and get_capacity() should be
1732 * converted to make use of bd_mutex and sequence counters.
1733 */
Ming Lei07c4e1e2020-05-08 16:17:56 +08001734 hd_sects_seq_init(&disk->part0);
Ming Lei6c710132015-07-16 11:16:45 +08001735 if (hd_ref_init(&disk->part0)) {
1736 hd_free_part(&disk->part0);
1737 kfree(disk);
1738 return NULL;
1739 }
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 disk->minors = minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001743 disk_to_dev(disk)->class = &block_class;
1744 disk_to_dev(disk)->type = &disk_type;
1745 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747 return disk;
1748}
Byungchul Parke319e1f2017-10-25 17:56:05 +09001749EXPORT_SYMBOL(__alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Jan Kara3079c222018-02-26 13:01:38 +01001751struct kobject *get_disk_and_module(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 struct module *owner;
1754 struct kobject *kobj;
1755
1756 if (!disk->fops)
1757 return NULL;
1758 owner = disk->fops->owner;
1759 if (owner && !try_module_get(owner))
1760 return NULL;
Jan Karad01b2dc2017-03-23 01:37:02 +01001761 kobj = kobject_get_unless_zero(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (kobj == NULL) {
1763 module_put(owner);
1764 return NULL;
1765 }
1766 return kobj;
1767
1768}
Jan Kara3079c222018-02-26 13:01:38 +01001769EXPORT_SYMBOL(get_disk_and_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771void put_disk(struct gendisk *disk)
1772{
1773 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001774 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776EXPORT_SYMBOL(put_disk);
1777
Jan Kara9df6c292018-02-26 13:01:39 +01001778/*
1779 * This is a counterpart of get_disk_and_module() and thus also of
1780 * get_gendisk().
1781 */
1782void put_disk_and_module(struct gendisk *disk)
1783{
1784 if (disk) {
1785 struct module *owner = disk->fops->owner;
1786
1787 put_disk(disk);
1788 module_put(owner);
1789 }
1790}
1791EXPORT_SYMBOL(put_disk_and_module);
1792
Hannes Reineckee3264a42009-07-28 09:13:13 +02001793static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1794{
1795 char event[] = "DISK_RO=1";
1796 char *envp[] = { event, NULL };
1797
1798 if (!ro)
1799 event[8] = '0';
1800 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803void set_device_ro(struct block_device *bdev, int flag)
1804{
Tejun Heob7db9952008-08-25 19:56:10 +09001805 bdev->bd_part->policy = flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
1808EXPORT_SYMBOL(set_device_ro);
1809
1810void set_disk_ro(struct gendisk *disk, int flag)
1811{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001812 struct disk_part_iter piter;
1813 struct hd_struct *part;
1814
Hannes Reineckee3264a42009-07-28 09:13:13 +02001815 if (disk->part0.policy != flag) {
1816 set_disk_ro_uevent(disk, flag);
1817 disk->part0.policy = flag;
1818 }
1819
1820 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001821 while ((part = disk_part_iter_next(&piter)))
1822 part->policy = flag;
1823 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
1826EXPORT_SYMBOL(set_disk_ro);
1827
1828int bdev_read_only(struct block_device *bdev)
1829{
1830 if (!bdev)
1831 return 0;
Tejun Heob7db9952008-08-25 19:56:10 +09001832 return bdev->bd_part->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
1835EXPORT_SYMBOL(bdev_read_only);
1836
Tejun Heo77ea8872010-12-08 20:57:37 +01001837/*
1838 * Disk events - monitor disk events like media change and eject request.
1839 */
1840struct disk_events {
1841 struct list_head node; /* all disk_event's */
1842 struct gendisk *disk; /* the associated disk */
1843 spinlock_t lock;
1844
Tejun Heofdd514e2011-06-09 20:43:59 +02001845 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001846 int block; /* event blocking depth */
1847 unsigned int pending; /* events already sent out */
1848 unsigned int clearing; /* events being cleared */
1849
1850 long poll_msecs; /* interval, -1 for default */
1851 struct delayed_work dwork;
1852};
1853
1854static const char *disk_events_strs[] = {
1855 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1856 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1857};
1858
1859static char *disk_uevents[] = {
1860 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1861 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1862};
1863
1864/* list of all disk_events */
1865static DEFINE_MUTEX(disk_events_mutex);
1866static LIST_HEAD(disk_events);
1867
1868/* disable in-kernel polling by default */
Wei Tang1fe8f342015-11-24 09:58:46 +08001869static unsigned long disk_events_dfl_poll_msecs;
Tejun Heo77ea8872010-12-08 20:57:37 +01001870
1871static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1872{
1873 struct disk_events *ev = disk->ev;
1874 long intv_msecs = 0;
1875
1876 /*
1877 * If device-specific poll interval is set, always use it. If
Martin Wilck673387a2019-03-27 14:51:01 +01001878 * the default is being used, poll if the POLL flag is set.
Tejun Heo77ea8872010-12-08 20:57:37 +01001879 */
1880 if (ev->poll_msecs >= 0)
1881 intv_msecs = ev->poll_msecs;
Martin Wilckc92e2f02019-03-27 14:51:02 +01001882 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
Tejun Heo77ea8872010-12-08 20:57:37 +01001883 intv_msecs = disk_events_dfl_poll_msecs;
1884
1885 return msecs_to_jiffies(intv_msecs);
1886}
1887
Tejun Heoc3af54a2011-06-09 20:43:55 +02001888/**
1889 * disk_block_events - block and flush disk event checking
1890 * @disk: disk to block events for
1891 *
1892 * On return from this function, it is guaranteed that event checking
1893 * isn't in progress and won't happen until unblocked by
1894 * disk_unblock_events(). Events blocking is counted and the actual
1895 * unblocking happens after the matching number of unblocks are done.
1896 *
1897 * Note that this intentionally does not block event checking from
1898 * disk_clear_events().
1899 *
1900 * CONTEXT:
1901 * Might sleep.
1902 */
1903void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001904{
1905 struct disk_events *ev = disk->ev;
1906 unsigned long flags;
1907 bool cancel;
1908
Tejun Heoc3af54a2011-06-09 20:43:55 +02001909 if (!ev)
1910 return;
1911
Tejun Heofdd514e2011-06-09 20:43:59 +02001912 /*
1913 * Outer mutex ensures that the first blocker completes canceling
1914 * the event work before further blockers are allowed to finish.
1915 */
1916 mutex_lock(&ev->block_mutex);
1917
Tejun Heo77ea8872010-12-08 20:57:37 +01001918 spin_lock_irqsave(&ev->lock, flags);
1919 cancel = !ev->block++;
1920 spin_unlock_irqrestore(&ev->lock, flags);
1921
Tejun Heoc3af54a2011-06-09 20:43:55 +02001922 if (cancel)
1923 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001924
1925 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001926}
1927
1928static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1929{
1930 struct disk_events *ev = disk->ev;
1931 unsigned long intv;
1932 unsigned long flags;
1933
1934 spin_lock_irqsave(&ev->lock, flags);
1935
1936 if (WARN_ON_ONCE(ev->block <= 0))
1937 goto out_unlock;
1938
1939 if (--ev->block)
1940 goto out_unlock;
1941
Tejun Heo77ea8872010-12-08 20:57:37 +01001942 intv = disk_events_poll_jiffies(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001943 if (check_now)
Viresh Kumar695588f2013-04-24 17:12:56 +05301944 queue_delayed_work(system_freezable_power_efficient_wq,
1945 &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001946 else if (intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301947 queue_delayed_work(system_freezable_power_efficient_wq,
1948 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001949out_unlock:
1950 spin_unlock_irqrestore(&ev->lock, flags);
1951}
1952
1953/**
Tejun Heo77ea8872010-12-08 20:57:37 +01001954 * disk_unblock_events - unblock disk event checking
1955 * @disk: disk to unblock events for
1956 *
1957 * Undo disk_block_events(). When the block count reaches zero, it
1958 * starts events polling if configured.
1959 *
1960 * CONTEXT:
1961 * Don't care. Safe to call from irq context.
1962 */
1963void disk_unblock_events(struct gendisk *disk)
1964{
1965 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01001966 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001967}
1968
1969/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02001970 * disk_flush_events - schedule immediate event checking and flushing
1971 * @disk: disk to check and flush events for
1972 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01001973 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02001974 * Schedule immediate event checking on @disk if not blocked. Events in
1975 * @mask are scheduled to be cleared from the driver. Note that this
1976 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01001977 *
1978 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02001979 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01001980 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001981void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001982{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001983 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001984
1985 if (!ev)
1986 return;
1987
Tejun Heo85ef06d2011-07-01 16:17:47 +02001988 spin_lock_irq(&ev->lock);
1989 ev->clearing |= mask;
Tejun Heo41f63c52012-08-03 10:30:47 -07001990 if (!ev->block)
Viresh Kumar695588f2013-04-24 17:12:56 +05301991 mod_delayed_work(system_freezable_power_efficient_wq,
1992 &ev->dwork, 0);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001993 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001994}
Tejun Heo77ea8872010-12-08 20:57:37 +01001995
1996/**
1997 * disk_clear_events - synchronously check, clear and return pending events
1998 * @disk: disk to fetch and clear events from
Masanari Iidada3dae52014-09-09 01:27:23 +09001999 * @mask: mask of events to be fetched and cleared
Tejun Heo77ea8872010-12-08 20:57:37 +01002000 *
2001 * Disk events are synchronously checked and pending events in @mask
2002 * are cleared and returned. This ignores the block count.
2003 *
2004 * CONTEXT:
2005 * Might sleep.
2006 */
2007unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
2008{
2009 const struct block_device_operations *bdops = disk->fops;
2010 struct disk_events *ev = disk->ev;
2011 unsigned int pending;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002012 unsigned int clearing = mask;
Tejun Heo77ea8872010-12-08 20:57:37 +01002013
2014 if (!ev) {
2015 /* for drivers still using the old ->media_changed method */
2016 if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
2017 bdops->media_changed && bdops->media_changed(disk))
2018 return DISK_EVENT_MEDIA_CHANGE;
2019 return 0;
2020 }
2021
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002022 disk_block_events(disk);
2023
2024 /*
2025 * store the union of mask and ev->clearing on the stack so that the
2026 * race with disk_flush_events does not cause ambiguity (ev->clearing
2027 * can still be modified even if events are blocked).
2028 */
Tejun Heo77ea8872010-12-08 20:57:37 +01002029 spin_lock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002030 clearing |= ev->clearing;
2031 ev->clearing = 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01002032 spin_unlock_irq(&ev->lock);
2033
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002034 disk_check_events(ev, &clearing);
Derek Basehoreaea24a82012-12-18 12:27:18 -08002035 /*
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002036 * if ev->clearing is not 0, the disk_flush_events got called in the
2037 * middle of this function, so we want to run the workfn without delay.
Derek Basehoreaea24a82012-12-18 12:27:18 -08002038 */
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002039 __disk_unblock_events(disk, ev->clearing ? true : false);
Tejun Heo77ea8872010-12-08 20:57:37 +01002040
2041 /* then, fetch and clear pending events */
2042 spin_lock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01002043 pending = ev->pending & mask;
2044 ev->pending &= ~mask;
2045 spin_unlock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002046 WARN_ON_ONCE(clearing & mask);
Tejun Heo77ea8872010-12-08 20:57:37 +01002047
2048 return pending;
2049}
2050
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002051/*
2052 * Separate this part out so that a different pointer for clearing_ptr can be
2053 * passed in for disk_clear_events.
2054 */
Tejun Heo77ea8872010-12-08 20:57:37 +01002055static void disk_events_workfn(struct work_struct *work)
2056{
2057 struct delayed_work *dwork = to_delayed_work(work);
2058 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002059
2060 disk_check_events(ev, &ev->clearing);
2061}
2062
2063static void disk_check_events(struct disk_events *ev,
2064 unsigned int *clearing_ptr)
2065{
Tejun Heo77ea8872010-12-08 20:57:37 +01002066 struct gendisk *disk = ev->disk;
2067 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002068 unsigned int clearing = *clearing_ptr;
Tejun Heo77ea8872010-12-08 20:57:37 +01002069 unsigned int events;
2070 unsigned long intv;
2071 int nr_events = 0, i;
2072
2073 /* check events */
2074 events = disk->fops->check_events(disk, clearing);
2075
2076 /* accumulate pending events and schedule next poll if necessary */
2077 spin_lock_irq(&ev->lock);
2078
2079 events &= ~ev->pending;
2080 ev->pending |= events;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08002081 *clearing_ptr &= ~clearing;
Tejun Heo77ea8872010-12-08 20:57:37 +01002082
2083 intv = disk_events_poll_jiffies(disk);
2084 if (!ev->block && intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05302085 queue_delayed_work(system_freezable_power_efficient_wq,
2086 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01002087
2088 spin_unlock_irq(&ev->lock);
2089
Tejun Heo7c88a162011-04-21 19:43:58 +02002090 /*
2091 * Tell userland about new events. Only the events listed in
Martin Wilckc92e2f02019-03-27 14:51:02 +01002092 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
2093 * is set. Otherwise, events are processed internally but never
2094 * get reported to userland.
Tejun Heo7c88a162011-04-21 19:43:58 +02002095 */
Tejun Heo77ea8872010-12-08 20:57:37 +01002096 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Martin Wilckc92e2f02019-03-27 14:51:02 +01002097 if ((events & disk->events & (1 << i)) &&
2098 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
Tejun Heo77ea8872010-12-08 20:57:37 +01002099 envp[nr_events++] = disk_uevents[i];
2100
2101 if (nr_events)
2102 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
2103}
2104
2105/*
2106 * A disk events enabled device has the following sysfs nodes under
2107 * its /sys/block/X/ directory.
2108 *
2109 * events : list of all supported events
2110 * events_async : list of events which can be detected w/o polling
Martin Wilck673387a2019-03-27 14:51:01 +01002111 * (always empty, only for backwards compatibility)
Tejun Heo77ea8872010-12-08 20:57:37 +01002112 * events_poll_msecs : polling interval, 0: disable, -1: system default
2113 */
2114static ssize_t __disk_events_show(unsigned int events, char *buf)
2115{
2116 const char *delim = "";
2117 ssize_t pos = 0;
2118 int i;
2119
2120 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
2121 if (events & (1 << i)) {
2122 pos += sprintf(buf + pos, "%s%s",
2123 delim, disk_events_strs[i]);
2124 delim = " ";
2125 }
2126 if (pos)
2127 pos += sprintf(buf + pos, "\n");
2128 return pos;
2129}
2130
2131static ssize_t disk_events_show(struct device *dev,
2132 struct device_attribute *attr, char *buf)
2133{
2134 struct gendisk *disk = dev_to_disk(dev);
2135
Martin Wilckc92e2f02019-03-27 14:51:02 +01002136 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
2137 return 0;
2138
Tejun Heo77ea8872010-12-08 20:57:37 +01002139 return __disk_events_show(disk->events, buf);
2140}
2141
2142static ssize_t disk_events_async_show(struct device *dev,
2143 struct device_attribute *attr, char *buf)
2144{
Martin Wilck673387a2019-03-27 14:51:01 +01002145 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01002146}
2147
2148static ssize_t disk_events_poll_msecs_show(struct device *dev,
2149 struct device_attribute *attr,
2150 char *buf)
2151{
2152 struct gendisk *disk = dev_to_disk(dev);
2153
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002154 if (!disk->ev)
2155 return sprintf(buf, "-1\n");
2156
Tejun Heo77ea8872010-12-08 20:57:37 +01002157 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
2158}
2159
2160static ssize_t disk_events_poll_msecs_store(struct device *dev,
2161 struct device_attribute *attr,
2162 const char *buf, size_t count)
2163{
2164 struct gendisk *disk = dev_to_disk(dev);
2165 long intv;
2166
2167 if (!count || !sscanf(buf, "%ld", &intv))
2168 return -EINVAL;
2169
2170 if (intv < 0 && intv != -1)
2171 return -EINVAL;
2172
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002173 if (!disk->ev)
2174 return -ENODEV;
2175
Tejun Heoc3af54a2011-06-09 20:43:55 +02002176 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01002177 disk->ev->poll_msecs = intv;
2178 __disk_unblock_events(disk, true);
2179
2180 return count;
2181}
2182
Joe Perches5657a812018-05-24 13:38:59 -06002183static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
2184static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
2185static const DEVICE_ATTR(events_poll_msecs, 0644,
Tejun Heo77ea8872010-12-08 20:57:37 +01002186 disk_events_poll_msecs_show,
2187 disk_events_poll_msecs_store);
2188
2189static const struct attribute *disk_events_attrs[] = {
2190 &dev_attr_events.attr,
2191 &dev_attr_events_async.attr,
2192 &dev_attr_events_poll_msecs.attr,
2193 NULL,
2194};
2195
2196/*
2197 * The default polling interval can be specified by the kernel
2198 * parameter block.events_dfl_poll_msecs which defaults to 0
2199 * (disable). This can also be modified runtime by writing to
Akinobu Mita1624b0b2019-07-16 21:59:35 +09002200 * /sys/module/block/parameters/events_dfl_poll_msecs.
Tejun Heo77ea8872010-12-08 20:57:37 +01002201 */
2202static int disk_events_set_dfl_poll_msecs(const char *val,
2203 const struct kernel_param *kp)
2204{
2205 struct disk_events *ev;
2206 int ret;
2207
2208 ret = param_set_ulong(val, kp);
2209 if (ret < 0)
2210 return ret;
2211
2212 mutex_lock(&disk_events_mutex);
2213
2214 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02002215 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01002216
2217 mutex_unlock(&disk_events_mutex);
2218
2219 return 0;
2220}
2221
2222static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
2223 .set = disk_events_set_dfl_poll_msecs,
2224 .get = param_get_ulong,
2225};
2226
2227#undef MODULE_PARAM_PREFIX
2228#define MODULE_PARAM_PREFIX "block."
2229
2230module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
2231 &disk_events_dfl_poll_msecs, 0644);
2232
2233/*
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002234 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
Tejun Heo77ea8872010-12-08 20:57:37 +01002235 */
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002236static void disk_alloc_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01002237{
2238 struct disk_events *ev;
2239
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002240 if (!disk->fops->check_events || !disk->events)
Tejun Heo77ea8872010-12-08 20:57:37 +01002241 return;
2242
2243 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
2244 if (!ev) {
2245 pr_warn("%s: failed to initialize events\n", disk->disk_name);
2246 return;
2247 }
2248
Tejun Heo77ea8872010-12-08 20:57:37 +01002249 INIT_LIST_HEAD(&ev->node);
2250 ev->disk = disk;
2251 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02002252 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01002253 ev->block = 1;
2254 ev->poll_msecs = -1;
2255 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2256
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002257 disk->ev = ev;
2258}
2259
2260static void disk_add_events(struct gendisk *disk)
2261{
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002262 /* FIXME: error handling */
2263 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2264 pr_warn("%s: failed to create sysfs files for events\n",
2265 disk->disk_name);
2266
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002267 if (!disk->ev)
2268 return;
2269
Tejun Heo77ea8872010-12-08 20:57:37 +01002270 mutex_lock(&disk_events_mutex);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002271 list_add_tail(&disk->ev->node, &disk_events);
Tejun Heo77ea8872010-12-08 20:57:37 +01002272 mutex_unlock(&disk_events_mutex);
2273
2274 /*
2275 * Block count is initialized to 1 and the following initial
2276 * unblock kicks it into action.
2277 */
2278 __disk_unblock_events(disk, true);
2279}
2280
2281static void disk_del_events(struct gendisk *disk)
2282{
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002283 if (disk->ev) {
2284 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01002285
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002286 mutex_lock(&disk_events_mutex);
2287 list_del_init(&disk->ev->node);
2288 mutex_unlock(&disk_events_mutex);
2289 }
Tejun Heo77ea8872010-12-08 20:57:37 +01002290
2291 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2292}
2293
2294static void disk_release_events(struct gendisk *disk)
2295{
2296 /* the block count should be 1 from disk_del_events() */
2297 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
2298 kfree(disk->ev);
2299}