blob: 6a60131baffa67a2e581b1fae19551b84eac1269 [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>
7#include <linux/fs.h>
8#include <linux/genhd.h>
Andrew Mortonb446b602007-02-20 13:57:48 -08009#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kernel.h>
11#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040012#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/spinlock.h>
Alexey Dobriyanf5009752008-10-04 23:53:21 +040015#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/seq_file.h>
17#include <linux/slab.h>
18#include <linux/kmod.h>
19#include <linux/kobj_map.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080020#include <linux/mutex.h>
Tejun Heobcce3de2008-08-25 19:47:22 +090021#include <linux/idr.h>
Tejun Heo77ea8872010-12-08 20:57:37 +010022#include <linux/log2.h>
Ming Lei25e823c2013-02-22 16:34:13 -080023#include <linux/pm_runtime.h>
Vishal Verma99e66082016-01-09 08:36:51 -080024#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Adrian Bunkff889722008-03-04 11:23:45 +010026#include "blk.h"
27
Kay Sieversedfaa7c2007-05-21 22:08:01 +020028static DEFINE_MUTEX(block_class_lock);
Kay Sieversedfaa7c2007-05-21 22:08:01 +020029struct kobject *block_depr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Tejun Heobcce3de2008-08-25 19:47:22 +090031/* for extended dynamic devt allocation, currently only one major is used */
Tejun Heoce23bba2013-02-27 17:03:56 -080032#define NR_EXT_DEVT (1 << MINORBITS)
Tejun Heobcce3de2008-08-25 19:47:22 +090033
Keith Busch2da78092014-08-26 09:05:36 -060034/* For extended devt allocation. ext_devt_lock prevents look up
Tejun Heobcce3de2008-08-25 19:47:22 +090035 * results from going away underneath its user.
36 */
Keith Busch2da78092014-08-26 09:05:36 -060037static DEFINE_SPINLOCK(ext_devt_lock);
Tejun Heobcce3de2008-08-25 19:47:22 +090038static DEFINE_IDR(ext_devt_idr);
39
Bart Van Asscheedf8ff52017-06-20 11:15:48 -070040static const struct device_type disk_type;
Adrian Bunk1826ead2008-03-04 11:23:46 +010041
Derek Basehore12c2bdb2012-12-18 12:27:20 -080042static void disk_check_events(struct disk_events *ev,
43 unsigned int *clearing_ptr);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +010044static void disk_alloc_events(struct gendisk *disk);
Tejun Heo77ea8872010-12-08 20:57:37 +010045static void disk_add_events(struct gendisk *disk);
46static void disk_del_events(struct gendisk *disk);
47static void disk_release_events(struct gendisk *disk);
48
Balbir Singhe598a722020-03-13 05:30:05 +000049/*
50 * Set disk capacity and notify if the size is not currently
51 * zero and will not be set to zero
52 */
53void set_capacity_revalidate_and_notify(struct gendisk *disk, sector_t size,
54 bool revalidate)
55{
56 sector_t capacity = get_capacity(disk);
57
58 set_capacity(disk, size);
59
60 if (revalidate)
61 revalidate_disk(disk);
62
63 if (capacity != size && capacity != 0 && size != 0) {
64 char *envp[] = { "RESIZE=1", NULL };
65
66 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
67 }
68}
69
70EXPORT_SYMBOL_GPL(set_capacity_revalidate_and_notify);
71
72
Jens Axboef299b7c2017-08-08 17:51:45 -060073void part_inc_in_flight(struct request_queue *q, struct hd_struct *part, int rw)
74{
Jens Axboe344e9ff2018-11-15 12:22:51 -070075 if (queue_is_mq(q))
Jens Axboef299b7c2017-08-08 17:51:45 -060076 return;
77
Mikulas Patocka1226b8d2018-12-06 11:41:20 -050078 part_stat_local_inc(part, in_flight[rw]);
Jens Axboef299b7c2017-08-08 17:51:45 -060079 if (part->partno)
Mikulas Patocka1226b8d2018-12-06 11:41:20 -050080 part_stat_local_inc(&part_to_disk(part)->part0, in_flight[rw]);
Jens Axboef299b7c2017-08-08 17:51:45 -060081}
82
83void part_dec_in_flight(struct request_queue *q, struct hd_struct *part, int rw)
84{
Jens Axboe344e9ff2018-11-15 12:22:51 -070085 if (queue_is_mq(q))
Jens Axboef299b7c2017-08-08 17:51:45 -060086 return;
87
Mikulas Patocka1226b8d2018-12-06 11:41:20 -050088 part_stat_local_dec(part, in_flight[rw]);
Jens Axboef299b7c2017-08-08 17:51:45 -060089 if (part->partno)
Mikulas Patocka1226b8d2018-12-06 11:41:20 -050090 part_stat_local_dec(&part_to_disk(part)->part0, in_flight[rw]);
Jens Axboef299b7c2017-08-08 17:51:45 -060091}
92
Mikulas Patockae016b782018-12-06 11:41:21 -050093unsigned int part_in_flight(struct request_queue *q, struct hd_struct *part)
Jens Axboef299b7c2017-08-08 17:51:45 -060094{
Mikulas Patocka1226b8d2018-12-06 11:41:20 -050095 int cpu;
Mikulas Patockae016b782018-12-06 11:41:21 -050096 unsigned int inflight;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -050097
Jens Axboe344e9ff2018-11-15 12:22:51 -070098 if (queue_is_mq(q)) {
Mikulas Patockae016b782018-12-06 11:41:21 -050099 return blk_mq_in_flight(q, part);
Jens Axboef299b7c2017-08-08 17:51:45 -0600100 }
101
Mikulas Patockae016b782018-12-06 11:41:21 -0500102 inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500103 for_each_possible_cpu(cpu) {
Mikulas Patockae016b782018-12-06 11:41:21 -0500104 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
105 part_stat_local_read_cpu(part, in_flight[1], cpu);
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500106 }
Mikulas Patockae016b782018-12-06 11:41:21 -0500107 if ((int)inflight < 0)
108 inflight = 0;
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500109
Mikulas Patockae016b782018-12-06 11:41:21 -0500110 return inflight;
Jens Axboef299b7c2017-08-08 17:51:45 -0600111}
112
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700113void part_in_flight_rw(struct request_queue *q, struct hd_struct *part,
114 unsigned int inflight[2])
115{
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500116 int cpu;
117
Jens Axboe344e9ff2018-11-15 12:22:51 -0700118 if (queue_is_mq(q)) {
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700119 blk_mq_in_flight_rw(q, part, inflight);
120 return;
121 }
122
Mikulas Patocka1226b8d2018-12-06 11:41:20 -0500123 inflight[0] = 0;
124 inflight[1] = 0;
125 for_each_possible_cpu(cpu) {
126 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
127 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
128 }
129 if ((int)inflight[0] < 0)
130 inflight[0] = 0;
131 if ((int)inflight[1] < 0)
132 inflight[1] = 0;
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700133}
134
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200135struct hd_struct *__disk_get_part(struct gendisk *disk, int partno)
136{
137 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
138
139 if (unlikely(partno < 0 || partno >= ptbl->len))
140 return NULL;
141 return rcu_dereference(ptbl->part[partno]);
142}
143
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200144/**
145 * disk_get_part - get partition
146 * @disk: disk to look partition from
147 * @partno: partition number
148 *
149 * Look for partition @partno from @disk. If found, increment
150 * reference count and return it.
151 *
152 * CONTEXT:
153 * Don't care.
154 *
155 * RETURNS:
156 * Pointer to the found partition on success, NULL if not found.
157 */
158struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
159{
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200160 struct hd_struct *part;
Tejun Heo540eed52008-08-25 19:56:15 +0900161
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200162 rcu_read_lock();
Christoph Hellwig807d4af2017-08-23 19:10:30 +0200163 part = __disk_get_part(disk, partno);
164 if (part)
165 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200166 rcu_read_unlock();
167
168 return part;
169}
170EXPORT_SYMBOL_GPL(disk_get_part);
171
172/**
173 * disk_part_iter_init - initialize partition iterator
174 * @piter: iterator to initialize
175 * @disk: disk to iterate over
176 * @flags: DISK_PITER_* flags
177 *
178 * Initialize @piter so that it iterates over partitions of @disk.
179 *
180 * CONTEXT:
181 * Don't care.
182 */
183void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
184 unsigned int flags)
185{
Tejun Heo540eed52008-08-25 19:56:15 +0900186 struct disk_part_tbl *ptbl;
187
188 rcu_read_lock();
189 ptbl = rcu_dereference(disk->part_tbl);
190
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200191 piter->disk = disk;
192 piter->part = NULL;
193
194 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +0900195 piter->idx = ptbl->len - 1;
Tejun Heo71982a42009-04-17 08:34:48 +0200196 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200197 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200198 else
199 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200200
201 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900202
203 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200204}
205EXPORT_SYMBOL_GPL(disk_part_iter_init);
206
207/**
208 * disk_part_iter_next - proceed iterator to the next partition and return it
209 * @piter: iterator of interest
210 *
211 * Proceed @piter to the next partition and return it.
212 *
213 * CONTEXT:
214 * Don't care.
215 */
216struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
217{
Tejun Heo540eed52008-08-25 19:56:15 +0900218 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200219 int inc, end;
220
221 /* put the last partition */
222 disk_put_part(piter->part);
223 piter->part = NULL;
224
Tejun Heo540eed52008-08-25 19:56:15 +0900225 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200226 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900227 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200228
229 /* determine iteration parameters */
230 if (piter->flags & DISK_PITER_REVERSE) {
231 inc = -1;
Tejun Heo71982a42009-04-17 08:34:48 +0200232 if (piter->flags & (DISK_PITER_INCL_PART0 |
233 DISK_PITER_INCL_EMPTY_PART0))
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200234 end = -1;
235 else
236 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200237 } else {
238 inc = 1;
Tejun Heo540eed52008-08-25 19:56:15 +0900239 end = ptbl->len;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200240 }
241
242 /* iterate to the next partition */
243 for (; piter->idx != end; piter->idx += inc) {
244 struct hd_struct *part;
245
Tejun Heo540eed52008-08-25 19:56:15 +0900246 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200247 if (!part)
248 continue;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200249 if (!part_nr_sects_read(part) &&
Tejun Heo71982a42009-04-17 08:34:48 +0200250 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
251 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
252 piter->idx == 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200253 continue;
254
Tejun Heoed9e1982008-08-25 19:56:05 +0900255 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200256 piter->part = part;
257 piter->idx += inc;
258 break;
259 }
260
261 rcu_read_unlock();
262
263 return piter->part;
264}
265EXPORT_SYMBOL_GPL(disk_part_iter_next);
266
267/**
268 * disk_part_iter_exit - finish up partition iteration
269 * @piter: iter of interest
270 *
271 * Called when iteration is over. Cleans up @piter.
272 *
273 * CONTEXT:
274 * Don't care.
275 */
276void disk_part_iter_exit(struct disk_part_iter *piter)
277{
278 disk_put_part(piter->part);
279 piter->part = NULL;
280}
281EXPORT_SYMBOL_GPL(disk_part_iter_exit);
282
Jens Axboea6f23652008-10-24 12:52:42 +0200283static inline int sector_in_part(struct hd_struct *part, sector_t sector)
284{
285 return part->start_sect <= sector &&
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200286 sector < part->start_sect + part_nr_sects_read(part);
Jens Axboea6f23652008-10-24 12:52:42 +0200287}
288
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200289/**
290 * disk_map_sector_rcu - map sector to partition
291 * @disk: gendisk of interest
292 * @sector: sector to map
293 *
294 * Find out which partition @sector maps to on @disk. This is
295 * primarily used for stats accounting.
296 *
297 * CONTEXT:
298 * RCU read locked. The returned partition pointer is valid only
299 * while preemption is disabled.
300 *
301 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900302 * Found partition on success, part0 is returned if no partition matches
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200303 */
304struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
305{
Tejun Heo540eed52008-08-25 19:56:15 +0900306 struct disk_part_tbl *ptbl;
Jens Axboea6f23652008-10-24 12:52:42 +0200307 struct hd_struct *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200308 int i;
309
Tejun Heo540eed52008-08-25 19:56:15 +0900310 ptbl = rcu_dereference(disk->part_tbl);
311
Jens Axboea6f23652008-10-24 12:52:42 +0200312 part = rcu_dereference(ptbl->last_lookup);
313 if (part && sector_in_part(part, sector))
314 return part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200315
Jens Axboea6f23652008-10-24 12:52:42 +0200316 for (i = 1; i < ptbl->len; i++) {
317 part = rcu_dereference(ptbl->part[i]);
318
319 if (part && sector_in_part(part, sector)) {
320 rcu_assign_pointer(ptbl->last_lookup, part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200321 return part;
Jens Axboea6f23652008-10-24 12:52:42 +0200322 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200323 }
Tejun Heo074a7ac2008-08-25 19:56:14 +0900324 return &disk->part0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200325}
326EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/*
329 * Can be deleted altogether. Later.
330 *
331 */
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600332#define BLKDEV_MAJOR_HASH_SIZE 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static struct blk_major_name {
334 struct blk_major_name *next;
335 int major;
336 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800337} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100340static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Joe Korty68eef3b2006-03-31 02:30:32 -0800342 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Joe Korty68eef3b2006-03-31 02:30:32 -0800345#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200346void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800347{
Joe Korty68eef3b2006-03-31 02:30:32 -0800348 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800349
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600350 mutex_lock(&block_class_lock);
351 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
352 if (dp->major == offset)
Tejun Heocf771cb2008-09-03 09:01:09 +0200353 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600354 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Joe Korty68eef3b2006-03-31 02:30:32 -0800356#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100358/**
359 * register_blkdev - register a new block device
360 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800361 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
362 * @major = 0, try to allocate any unused major number.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100363 * @name: the name of the new block device as a zero terminated string
364 *
365 * The @name must be unique within the system.
366 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300367 * The return value depends on the @major input parameter:
368 *
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800369 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
370 * then the function returns zero on success, or a negative error code
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300371 * - if any unused major number was requested with @major = 0 parameter
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100372 * then the return value is the allocated major number in range
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800373 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
374 *
375 * See Documentation/admin-guide/devices.txt for the list of allocated
376 * major numbers.
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378int register_blkdev(unsigned int major, const char *name)
379{
380 struct blk_major_name **n, *p;
381 int index, ret = 0;
382
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200383 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /* temporary */
386 if (major == 0) {
387 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
388 if (major_names[index] == NULL)
389 break;
390 }
391
392 if (index == 0) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500393 printk("%s: failed to get major for %s\n",
394 __func__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ret = -EBUSY;
396 goto out;
397 }
398 major = index;
399 ret = major;
400 }
401
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600402 if (major >= BLKDEV_MAJOR_MAX) {
Keyur Pateldfc76d12019-02-17 10:21:56 -0500403 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
404 __func__, major, BLKDEV_MAJOR_MAX-1, name);
Logan Gunthorpe133d55c2017-06-16 17:48:21 -0600405
406 ret = -EINVAL;
407 goto out;
408 }
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
411 if (p == NULL) {
412 ret = -ENOMEM;
413 goto out;
414 }
415
416 p->major = major;
417 strlcpy(p->name, name, sizeof(p->name));
418 p->next = NULL;
419 index = major_to_index(major);
420
421 for (n = &major_names[index]; *n; n = &(*n)->next) {
422 if ((*n)->major == major)
423 break;
424 }
425 if (!*n)
426 *n = p;
427 else
428 ret = -EBUSY;
429
430 if (ret < 0) {
Srivatsa S. Bhatf33ff112018-02-05 18:25:27 -0800431 printk("register_blkdev: cannot get major %u for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 major, name);
433 kfree(p);
434 }
435out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200436 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return ret;
438}
439
440EXPORT_SYMBOL(register_blkdev);
441
Akinobu Mitaf4480242007-07-17 04:03:47 -0700442void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct blk_major_name **n;
445 struct blk_major_name *p = NULL;
446 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200448 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 for (n = &major_names[index]; *n; n = &(*n)->next)
450 if ((*n)->major == major)
451 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700452 if (!*n || strcmp((*n)->name, name)) {
453 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700454 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 p = *n;
456 *n = p->next;
457 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200458 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462EXPORT_SYMBOL(unregister_blkdev);
463
464static struct kobj_map *bdev_map;
465
Tejun Heobcce3de2008-08-25 19:47:22 +0900466/**
Tejun Heo870d6652008-08-25 19:47:25 +0900467 * blk_mangle_minor - scatter minor numbers apart
468 * @minor: minor number to mangle
469 *
470 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
471 * is enabled. Mangling twice gives the original value.
472 *
473 * RETURNS:
474 * Mangled value.
475 *
476 * CONTEXT:
477 * Don't care.
478 */
479static int blk_mangle_minor(int minor)
480{
481#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
482 int i;
483
484 for (i = 0; i < MINORBITS / 2; i++) {
485 int low = minor & (1 << i);
486 int high = minor & (1 << (MINORBITS - 1 - i));
487 int distance = MINORBITS - 1 - 2 * i;
488
489 minor ^= low | high; /* clear both bits */
490 low <<= distance; /* swap the positions */
491 high >>= distance;
492 minor |= low | high; /* and set */
493 }
494#endif
495 return minor;
496}
497
498/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900499 * blk_alloc_devt - allocate a dev_t for a partition
500 * @part: partition to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900501 * @devt: out parameter for resulting dev_t
502 *
503 * Allocate a dev_t for block device.
504 *
505 * RETURNS:
506 * 0 on success, allocated dev_t is returned in *@devt. -errno on
507 * failure.
508 *
509 * CONTEXT:
510 * Might sleep.
511 */
512int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
513{
514 struct gendisk *disk = part_to_disk(part);
Tejun Heobab998d2013-02-27 17:03:57 -0800515 int idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900516
517 /* in consecutive minor range? */
518 if (part->partno < disk->minors) {
519 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
520 return 0;
521 }
522
523 /* allocate ext devt */
Keith Busch2da78092014-08-26 09:05:36 -0600524 idr_preload(GFP_KERNEL);
525
Dan Williams4d66e5e2015-06-10 23:47:14 -0400526 spin_lock_bh(&ext_devt_lock);
Keith Busch2da78092014-08-26 09:05:36 -0600527 idx = idr_alloc(&ext_devt_idr, part, 0, NR_EXT_DEVT, GFP_NOWAIT);
Dan Williams4d66e5e2015-06-10 23:47:14 -0400528 spin_unlock_bh(&ext_devt_lock);
Keith Busch2da78092014-08-26 09:05:36 -0600529
530 idr_preload_end();
Tejun Heobab998d2013-02-27 17:03:57 -0800531 if (idx < 0)
532 return idx == -ENOSPC ? -EBUSY : idx;
Tejun Heobcce3de2008-08-25 19:47:22 +0900533
Tejun Heo870d6652008-08-25 19:47:25 +0900534 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900535 return 0;
536}
537
538/**
539 * blk_free_devt - free a dev_t
540 * @devt: dev_t to free
541 *
542 * Free @devt which was allocated using blk_alloc_devt().
543 *
544 * CONTEXT:
545 * Might sleep.
546 */
547void blk_free_devt(dev_t devt)
548{
Tejun Heobcce3de2008-08-25 19:47:22 +0900549 if (devt == MKDEV(0, 0))
550 return;
551
552 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
Dan Williams4d66e5e2015-06-10 23:47:14 -0400553 spin_lock_bh(&ext_devt_lock);
Tejun Heo870d6652008-08-25 19:47:25 +0900554 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Dan Williams4d66e5e2015-06-10 23:47:14 -0400555 spin_unlock_bh(&ext_devt_lock);
Tejun Heobcce3de2008-08-25 19:47:22 +0900556 }
557}
558
Bart Van Assche33c826e2019-05-30 17:00:47 -0700559/*
560 * We invalidate devt by assigning NULL pointer for devt in idr.
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800561 */
562void blk_invalidate_devt(dev_t devt)
563{
564 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
565 spin_lock_bh(&ext_devt_lock);
566 idr_replace(&ext_devt_idr, NULL, blk_mangle_minor(MINOR(devt)));
567 spin_unlock_bh(&ext_devt_lock);
568 }
569}
570
Tejun Heo1f014292008-08-25 19:47:23 +0900571static char *bdevt_str(dev_t devt, char *buf)
572{
573 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
574 char tbuf[BDEVT_SIZE];
575 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
576 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
577 } else
578 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
579
580 return buf;
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/*
584 * Register device numbers dev..(dev+range-1)
585 * range must be nonzero
586 * The hash chain is sorted on range, so that subranges can override.
587 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200588void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct kobject *(*probe)(dev_t, int *, void *),
590 int (*lock)(dev_t, void *), void *data)
591{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200592 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595EXPORT_SYMBOL(blk_register_region);
596
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200597void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200599 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602EXPORT_SYMBOL(blk_unregister_region);
603
Tejun Heocf771cb2008-09-03 09:01:09 +0200604static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200607
Tejun Heoed9e1982008-08-25 19:56:05 +0900608 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200611static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct gendisk *p = data;
614
Jan Kara3079c222018-02-26 13:01:38 +0100615 if (!get_disk_and_module(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -1;
617 return 0;
618}
619
Hannes Reineckefef912b2018-09-28 08:17:19 +0200620static void register_disk(struct device *parent, struct gendisk *disk,
621 const struct attribute_group **groups)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100622{
623 struct device *ddev = disk_to_dev(disk);
624 struct block_device *bdev;
625 struct disk_part_iter piter;
626 struct hd_struct *part;
627 int err;
628
Dan Williamse63a46b2016-06-15 18:17:27 -0700629 ddev->parent = parent;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100630
Kees Cookffc8b302013-07-03 15:01:14 -0700631 dev_set_name(ddev, "%s", disk->disk_name);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100632
633 /* delay uevents, until we scanned partition table */
634 dev_set_uevent_suppress(ddev, 1);
635
Hannes Reineckefef912b2018-09-28 08:17:19 +0200636 if (groups) {
637 WARN_ON(ddev->groups);
638 ddev->groups = groups;
639 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100640 if (device_add(ddev))
641 return;
642 if (!sysfs_deprecated) {
643 err = sysfs_create_link(block_depr, &ddev->kobj,
644 kobject_name(&ddev->kobj));
645 if (err) {
646 device_del(ddev);
647 return;
648 }
649 }
Ming Lei25e823c2013-02-22 16:34:13 -0800650
651 /*
652 * avoid probable deadlock caused by allocating memory with
653 * GFP_KERNEL in runtime_resume callback of its all ancestor
654 * devices
655 */
656 pm_runtime_set_memalloc_noio(ddev, true);
657
Tejun Heod2bf1b62010-12-08 20:57:36 +0100658 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
659 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
660
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300661 if (disk->flags & GENHD_FL_HIDDEN) {
662 dev_set_uevent_suppress(ddev, 0);
663 return;
664 }
665
Tejun Heod2bf1b62010-12-08 20:57:36 +0100666 /* No minors to use for partitions */
Tejun Heod27769e2011-08-23 20:01:04 +0200667 if (!disk_part_scan_enabled(disk))
Tejun Heod2bf1b62010-12-08 20:57:36 +0100668 goto exit;
669
670 /* No such device (e.g., media were just removed) */
671 if (!get_capacity(disk))
672 goto exit;
673
674 bdev = bdget_disk(disk, 0);
675 if (!bdev)
676 goto exit;
677
678 bdev->bd_invalidated = 1;
679 err = blkdev_get(bdev, FMODE_READ, NULL);
680 if (err < 0)
681 goto exit;
682 blkdev_put(bdev, FMODE_READ);
683
684exit:
685 /* announce disk after possible partitions are created */
686 dev_set_uevent_suppress(ddev, 0);
687 kobject_uevent(&ddev->kobj, KOBJ_ADD);
688
689 /* announce possible partitions */
690 disk_part_iter_init(&piter, disk, 0);
691 while ((part = disk_part_iter_next(&piter)))
692 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
693 disk_part_iter_exit(&piter);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300694
zhengbin4d7c1d32019-02-20 21:27:05 +0800695 if (disk->queue->backing_dev_info->dev) {
696 err = sysfs_create_link(&ddev->kobj,
697 &disk->queue->backing_dev_info->dev->kobj,
698 "bdi");
699 WARN_ON(err);
700 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/**
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500704 * __device_add_disk - add disk information to kernel list
Dan Williamse63a46b2016-06-15 18:17:27 -0700705 * @parent: parent device for the disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * @disk: per-device partitioning information
Hannes Reineckefef912b2018-09-28 08:17:19 +0200707 * @groups: Additional per-device sysfs groups
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500708 * @register_queue: register the queue if set to true
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 *
710 * This function registers the partitioning information in @disk
711 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900712 *
713 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500715static void __device_add_disk(struct device *parent, struct gendisk *disk,
Hannes Reineckefef912b2018-09-28 08:17:19 +0200716 const struct attribute_group **groups,
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500717 bool register_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900719 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400720 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700721
Damien Le Moal737eb782019-09-05 18:51:33 +0900722 /*
723 * The disk queue should now be all set with enough information about
724 * the device for the elevator code to pick an adequate default
725 * elevator if one is needed, that is, for devices requesting queue
726 * registration.
727 */
728 if (register_queue)
729 elevator_init_mq(disk->queue);
730
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900731 /* minors == 0 indicates to use ext devt from part0 and should
732 * be accompanied with EXT_DEVT flag. Make sure all
733 * parameters make sense.
734 */
735 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300736 WARN_ON(!disk->minors &&
737 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900740
741 retval = blk_alloc_devt(&disk->part0, &devt);
742 if (retval) {
743 WARN_ON(1);
744 return;
745 }
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900746 disk->major = MAJOR(devt);
747 disk->first_minor = MINOR(devt);
748
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +0100749 disk_alloc_events(disk);
750
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300751 if (disk->flags & GENHD_FL_HIDDEN) {
752 /*
753 * Don't let hidden disks show up in /proc/partitions,
754 * and don't bother scanning for partitions either.
755 */
756 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
757 disk->flags |= GENHD_FL_NO_PART_SCAN;
758 } else {
weiping zhang3a921682017-10-31 18:38:59 +0800759 int ret;
760
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300761 /* Register BDI before referencing it from bdev */
762 disk_to_dev(disk)->devt = devt;
weiping zhang3a921682017-10-31 18:38:59 +0800763 ret = bdi_register_owner(disk->queue->backing_dev_info,
764 disk_to_dev(disk));
765 WARN_ON(ret);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300766 blk_register_region(disk_devt(disk), disk->minors, NULL,
767 exact_match, exact_lock, disk);
768 }
Hannes Reineckefef912b2018-09-28 08:17:19 +0200769 register_disk(parent, disk, groups);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500770 if (register_queue)
771 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700772
Tejun Heo523e1d32011-10-19 14:31:07 +0200773 /*
774 * Take an extra ref on queue which will be put on disk_release()
775 * so that it sticks around as long as @disk is there.
776 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100777 WARN_ON_ONCE(!blk_get_queue(disk->queue));
Tejun Heo523e1d32011-10-19 14:31:07 +0200778
Tejun Heo77ea8872010-12-08 20:57:37 +0100779 disk_add_events(disk);
Martin K. Petersen25520d52015-10-21 13:19:49 -0400780 blk_integrity_add(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500782
Hannes Reineckefef912b2018-09-28 08:17:19 +0200783void device_add_disk(struct device *parent, struct gendisk *disk,
784 const struct attribute_group **groups)
785
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500786{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200787 __device_add_disk(parent, disk, groups, true);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500788}
Dan Williamse63a46b2016-06-15 18:17:27 -0700789EXPORT_SYMBOL(device_add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500791void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
792{
Hannes Reineckefef912b2018-09-28 08:17:19 +0200793 __device_add_disk(parent, disk, NULL, false);
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500794}
795EXPORT_SYMBOL(device_add_disk_no_queue_reg);
796
Tejun Heod2bf1b62010-12-08 20:57:36 +0100797void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Tejun Heod2bf1b62010-12-08 20:57:36 +0100799 struct disk_part_iter piter;
800 struct hd_struct *part;
801
Martin K. Petersen25520d52015-10-21 13:19:49 -0400802 blk_integrity_del(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +0100803 disk_del_events(disk);
804
Jan Kara56c09082018-02-26 13:01:41 +0100805 /*
806 * Block lookups of the disk until all bdevs are unhashed and the
807 * disk is marked as dead (GENHD_FL_UP cleared).
808 */
809 down_write(&disk->lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100810 /* invalidate stuff */
811 disk_part_iter_init(&piter, disk,
812 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
813 while ((part = disk_part_iter_next(&piter))) {
814 invalidate_partition(disk, part->partno);
Jan Kara4b8c8612017-02-21 18:09:46 +0100815 bdev_unhash_inode(part_devt(part));
Tejun Heod2bf1b62010-12-08 20:57:36 +0100816 delete_partition(disk, part->partno);
817 }
818 disk_part_iter_exit(&piter);
819
820 invalidate_partition(disk, 0);
Jan Karad06e05c2017-02-21 18:09:47 +0100821 bdev_unhash_inode(disk_devt(disk));
Tejun Heod2bf1b62010-12-08 20:57:36 +0100822 set_capacity(disk, 0);
823 disk->flags &= ~GENHD_FL_UP;
Jan Kara56c09082018-02-26 13:01:41 +0100824 up_write(&disk->lookup_sem);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100825
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300826 if (!(disk->flags & GENHD_FL_HIDDEN))
827 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Jan Kara90f16fd2017-03-08 17:48:33 +0100828 if (disk->queue) {
829 /*
830 * Unregister bdi before releasing device numbers (as they can
831 * get reused and we'd get clashes in sysfs).
832 */
Mike Snitzerbc8d0622018-01-09 20:46:49 -0500833 if (!(disk->flags & GENHD_FL_HIDDEN))
834 bdi_unregister(disk->queue->backing_dev_info);
Jan Kara90f16fd2017-03-08 17:48:33 +0100835 blk_unregister_queue(disk);
836 } else {
837 WARN_ON(1);
838 }
Tejun Heod2bf1b62010-12-08 20:57:36 +0100839
Hannes Reinecke17eac092017-11-09 17:57:06 +0100840 if (!(disk->flags & GENHD_FL_HIDDEN))
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300841 blk_unregister_region(disk_devt(disk), disk->minors);
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800842 /*
843 * Remove gendisk pointer from idr so that it cannot be looked up
844 * while RCU period before freeing gendisk is running to prevent
845 * use-after-free issues. Note that the device number stays
846 * "in-use" until we really free the gendisk.
847 */
848 blk_invalidate_devt(disk_devt(disk));
Tejun Heod2bf1b62010-12-08 20:57:36 +0100849
850 kobject_put(disk->part0.holder_dir);
851 kobject_put(disk->slave_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 part_stat_set_all(&disk->part0, 0);
Tejun Heoed9e1982008-08-25 19:56:05 +0900854 disk->part0.stamp = 0;
Tejun Heod2bf1b62010-12-08 20:57:36 +0100855 if (!sysfs_deprecated)
856 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
Ming Lei25e823c2013-02-22 16:34:13 -0800857 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100858 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100860EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Vishal Verma99e66082016-01-09 08:36:51 -0800862/* sysfs access to bad-blocks list. */
863static ssize_t disk_badblocks_show(struct device *dev,
864 struct device_attribute *attr,
865 char *page)
866{
867 struct gendisk *disk = dev_to_disk(dev);
868
869 if (!disk->bb)
870 return sprintf(page, "\n");
871
872 return badblocks_show(disk->bb, page, 0);
873}
874
875static ssize_t disk_badblocks_store(struct device *dev,
876 struct device_attribute *attr,
877 const char *page, size_t len)
878{
879 struct gendisk *disk = dev_to_disk(dev);
880
881 if (!disk->bb)
882 return -ENXIO;
883
884 return badblocks_store(disk->bb, page, len, 0);
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/**
888 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200889 * @devt: device to get partitioning information for
Randy Dunlap496aa8a2008-10-16 07:46:23 +0200890 * @partno: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 *
892 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200893 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200895struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Tejun Heobcce3de2008-08-25 19:47:22 +0900897 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200898
Tejun Heobcce3de2008-08-25 19:47:22 +0900899 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
900 struct kobject *kobj;
901
902 kobj = kobj_lookup(bdev_map, devt, partno);
903 if (kobj)
904 disk = dev_to_disk(kobj_to_dev(kobj));
905 } else {
906 struct hd_struct *part;
907
Dan Williams4d66e5e2015-06-10 23:47:14 -0400908 spin_lock_bh(&ext_devt_lock);
Tejun Heo870d6652008-08-25 19:47:25 +0900909 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Jan Kara3079c222018-02-26 13:01:38 +0100910 if (part && get_disk_and_module(part_to_disk(part))) {
Tejun Heobcce3de2008-08-25 19:47:22 +0900911 *partno = part->partno;
912 disk = part_to_disk(part);
913 }
Dan Williams4d66e5e2015-06-10 23:47:14 -0400914 spin_unlock_bh(&ext_devt_lock);
Tejun Heobcce3de2008-08-25 19:47:22 +0900915 }
916
Jan Kara56c09082018-02-26 13:01:41 +0100917 if (!disk)
918 return NULL;
919
920 /*
921 * Synchronize with del_gendisk() to not return disk that is being
922 * destroyed.
923 */
924 down_read(&disk->lookup_sem);
925 if (unlikely((disk->flags & GENHD_FL_HIDDEN) ||
926 !(disk->flags & GENHD_FL_UP))) {
927 up_read(&disk->lookup_sem);
Jan Kara9df6c292018-02-26 13:01:39 +0100928 put_disk_and_module(disk);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300929 disk = NULL;
Jan Kara56c09082018-02-26 13:01:41 +0100930 } else {
931 up_read(&disk->lookup_sem);
Christoph Hellwig8ddcd652017-11-02 21:29:53 +0300932 }
Tejun Heobcce3de2008-08-25 19:47:22 +0900933 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
Divyesh Shahb6ac23af2010-04-15 08:54:59 +0200935EXPORT_SYMBOL(get_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Tejun Heof331c022008-09-03 09:01:48 +0200937/**
938 * bdget_disk - do bdget() by gendisk and partition number
939 * @disk: gendisk of interest
940 * @partno: partition number
941 *
942 * Find partition @partno from @disk, do bdget() on it.
943 *
944 * CONTEXT:
945 * Don't care.
946 *
947 * RETURNS:
948 * Resulting block_device on success, NULL on failure.
949 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200950struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200951{
Tejun Heo548b10e2008-08-29 09:01:47 +0200952 struct hd_struct *part;
953 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200954
Tejun Heo548b10e2008-08-29 09:01:47 +0200955 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +0200956 if (part)
Tejun Heo548b10e2008-08-29 09:01:47 +0200957 bdev = bdget(part_devt(part));
958 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200959
Tejun Heo548b10e2008-08-29 09:01:47 +0200960 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200961}
962EXPORT_SYMBOL(bdget_disk);
963
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700964/*
965 * print a full list of all partitions - intended for places where the root
966 * filesystem can't be mounted and thus to give the victim some idea of what
967 * went wrong
968 */
969void __init printk_all_partitions(void)
970{
Tejun Heodef4e382008-09-03 08:57:12 +0200971 struct class_dev_iter iter;
972 struct device *dev;
973
974 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
975 while ((dev = class_dev_iter_next(&iter))) {
976 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200977 struct disk_part_iter piter;
978 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900979 char name_buf[BDEVNAME_SIZE];
980 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200981
982 /*
983 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300984 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +0200985 */
986 if (get_capacity(disk) == 0 ||
987 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
988 continue;
989
990 /*
991 * Note, unlike /proc/partitions, I am showing the
992 * numbers in hex - the same format as the root=
993 * option takes.
994 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900995 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
996 while ((part = disk_part_iter_next(&piter))) {
997 bool is_part0 = part == &disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +0200998
Will Drewryb5af9212010-08-31 15:47:07 -0500999 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Tejun Heo1f014292008-08-25 19:47:23 +09001000 bdevt_str(part_devt(part), devt_buf),
Vivek Goyalc83f6bf2012-08-01 12:24:18 +02001001 (unsigned long long)part_nr_sects_read(part) >> 1
1002 , disk_name(disk, part->partno, name_buf),
Stephen Warren1ad7e892012-11-08 16:12:25 -08001003 part->info ? part->info->uuid : "");
Tejun Heo074a7ac2008-08-25 19:56:14 +09001004 if (is_part0) {
Dan Williams52c44d92016-06-15 19:43:07 -07001005 if (dev->parent && dev->parent->driver)
Tejun Heo074a7ac2008-08-25 19:56:14 +09001006 printk(" driver: %s\n",
Dan Williams52c44d92016-06-15 19:43:07 -07001007 dev->parent->driver->name);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001008 else
1009 printk(" (driver?)\n");
1010 } else
1011 printk("\n");
1012 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001013 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +02001014 }
1015 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -07001016}
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018#ifdef CONFIG_PROC_FS
1019/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +02001020static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001021{
Tejun Heodef4e382008-09-03 08:57:12 +02001022 loff_t skip = *pos;
1023 struct class_dev_iter *iter;
1024 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001025
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +02001026 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +02001027 if (!iter)
1028 return ERR_PTR(-ENOMEM);
1029
1030 seqf->private = iter;
1031 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
1032 do {
1033 dev = class_dev_iter_next(iter);
1034 if (!dev)
1035 return NULL;
1036 } while (skip--);
1037
1038 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001039}
1040
Tejun Heodef4e382008-09-03 08:57:12 +02001041static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001043 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -04001044
Tejun Heodef4e382008-09-03 08:57:12 +02001045 (*pos)++;
1046 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +02001047 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -04001048 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +02001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return NULL;
1051}
1052
Tejun Heodef4e382008-09-03 08:57:12 +02001053static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -04001054{
Tejun Heodef4e382008-09-03 08:57:12 +02001055 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -04001056
Tejun Heodef4e382008-09-03 08:57:12 +02001057 /* stop is called even after start failed :-( */
1058 if (iter) {
1059 class_dev_iter_exit(iter);
1060 kfree(iter);
Vegard Nossum77da1602016-07-29 10:40:31 +02001061 seqf->private = NULL;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Tejun Heodef4e382008-09-03 08:57:12 +02001065static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Jianpeng Ma06768062012-08-03 10:42:00 +02001067 void *p;
Tejun Heodef4e382008-09-03 08:57:12 +02001068
1069 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +01001070 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +02001071 seq_puts(seqf, "major minor #blocks name\n\n");
1072 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Tejun Heocf771cb2008-09-03 09:01:09 +02001075static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001078 struct disk_part_iter piter;
1079 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 char buf[BDEVNAME_SIZE];
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +02001083 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +02001084 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return 0;
1086 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
1087 return 0;
1088
1089 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +09001090 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001091 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +09001092 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +02001093 MAJOR(part_devt(part)), MINOR(part_devt(part)),
Vivek Goyalc83f6bf2012-08-01 12:24:18 +02001094 (unsigned long long)part_nr_sects_read(part) >> 1,
Tejun Heof331c022008-09-03 09:01:48 +02001095 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001096 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 return 0;
1099}
1100
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001101static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001102 .start = show_partition_start,
1103 .next = disk_seqf_next,
1104 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001105 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106};
1107#endif
1108
1109
Tejun Heocf771cb2008-09-03 09:01:09 +02001110static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001112 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001114 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return NULL;
1116}
1117
1118static int __init genhd_device_init(void)
1119{
Dan Williamse105b8b2008-04-21 10:51:07 -07001120 int error;
1121
1122 block_class.dev_kobj = sysfs_dev_block_kobj;
1123 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -07001124 if (unlikely(error))
1125 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001126 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001128
Zhang, Yanmin561ec682008-11-14 08:26:30 +01001129 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1130
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001131 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +02001132 if (!sysfs_deprecated)
1133 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -08001134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137subsys_initcall(genhd_device_init);
1138
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001139static ssize_t disk_range_show(struct device *dev,
1140 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001142 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001144 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Tejun Heo1f014292008-08-25 19:47:23 +09001147static ssize_t disk_ext_range_show(struct device *dev,
1148 struct device_attribute *attr, char *buf)
1149{
1150 struct gendisk *disk = dev_to_disk(dev);
1151
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001152 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +09001153}
1154
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001155static ssize_t disk_removable_show(struct device *dev,
1156 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +02001157{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001158 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001159
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001160 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001162}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001164static ssize_t disk_hidden_show(struct device *dev,
1165 struct device_attribute *attr, char *buf)
1166{
1167 struct gendisk *disk = dev_to_disk(dev);
1168
1169 return sprintf(buf, "%d\n",
1170 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1171}
1172
Kay Sievers1c9ce522008-06-13 09:41:00 +02001173static ssize_t disk_ro_show(struct device *dev,
1174 struct device_attribute *attr, char *buf)
1175{
1176 struct gendisk *disk = dev_to_disk(dev);
1177
Tejun Heob7db9952008-08-25 19:56:10 +09001178 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +02001179}
1180
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001181static ssize_t disk_capability_show(struct device *dev,
1182 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001183{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001184 struct gendisk *disk = dev_to_disk(dev);
1185
1186 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -07001187}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001188
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001189static ssize_t disk_alignment_offset_show(struct device *dev,
1190 struct device_attribute *attr,
1191 char *buf)
1192{
1193 struct gendisk *disk = dev_to_disk(dev);
1194
1195 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1196}
1197
Martin K. Petersen86b37282009-11-10 11:50:21 +01001198static ssize_t disk_discard_alignment_show(struct device *dev,
1199 struct device_attribute *attr,
1200 char *buf)
1201{
1202 struct gendisk *disk = dev_to_disk(dev);
1203
Martin K. Petersendd3d1452010-01-11 03:21:48 -05001204 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +01001205}
1206
Joe Perches5657a812018-05-24 13:38:59 -06001207static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1208static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1209static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1210static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1211static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1212static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1213static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1214static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1215static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1216static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1217static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1218static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001219#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001220static struct device_attribute dev_attr_fail =
Joe Perches5657a812018-05-24 13:38:59 -06001221 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001222#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001223#ifdef CONFIG_FAIL_IO_TIMEOUT
1224static struct device_attribute dev_attr_fail_timeout =
Joe Perches5657a812018-05-24 13:38:59 -06001225 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
Jens Axboe581d4e22008-09-14 05:56:33 -07001226#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001227
1228static struct attribute *disk_attrs[] = {
1229 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +09001230 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001231 &dev_attr_removable.attr,
Christoph Hellwig8ddcd652017-11-02 21:29:53 +03001232 &dev_attr_hidden.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001233 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001234 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001235 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001236 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001237 &dev_attr_capability.attr,
1238 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001239 &dev_attr_inflight.attr,
Vishal Verma99e66082016-01-09 08:36:51 -08001240 &dev_attr_badblocks.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001241#ifdef CONFIG_FAIL_MAKE_REQUEST
1242 &dev_attr_fail.attr,
1243#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001244#ifdef CONFIG_FAIL_IO_TIMEOUT
1245 &dev_attr_fail_timeout.attr,
1246#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001247 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248};
1249
Dan Williams9438b3e02017-04-27 14:46:26 -07001250static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1251{
1252 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1253 struct gendisk *disk = dev_to_disk(dev);
1254
1255 if (a == &dev_attr_badblocks.attr && !disk->bb)
1256 return 0;
1257 return a->mode;
1258}
1259
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001260static struct attribute_group disk_attr_group = {
1261 .attrs = disk_attrs,
Dan Williams9438b3e02017-04-27 14:46:26 -07001262 .is_visible = disk_visible,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001263};
1264
David Brownella4dbd672009-06-24 10:06:31 -07001265static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001266 &disk_attr_group,
1267 NULL
1268};
1269
Tejun Heo540eed52008-08-25 19:56:15 +09001270/**
1271 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1272 * @disk: disk to replace part_tbl for
1273 * @new_ptbl: new part_tbl to install
1274 *
1275 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1276 * original ptbl is freed using RCU callback.
1277 *
1278 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001279 * Matching bd_mutex locked or the caller is the only user of @disk.
Tejun Heo540eed52008-08-25 19:56:15 +09001280 */
1281static void disk_replace_part_tbl(struct gendisk *disk,
1282 struct disk_part_tbl *new_ptbl)
1283{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001284 struct disk_part_tbl *old_ptbl =
1285 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001286
1287 rcu_assign_pointer(disk->part_tbl, new_ptbl);
Jens Axboea6f23652008-10-24 12:52:42 +02001288
1289 if (old_ptbl) {
1290 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
Lai Jiangshan57bdfbf2011-03-18 11:42:58 +08001291 kfree_rcu(old_ptbl, rcu_head);
Jens Axboea6f23652008-10-24 12:52:42 +02001292 }
Tejun Heo540eed52008-08-25 19:56:15 +09001293}
1294
1295/**
1296 * disk_expand_part_tbl - expand disk->part_tbl
1297 * @disk: disk to expand part_tbl for
1298 * @partno: expand such that this partno can fit in
1299 *
1300 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1301 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1302 *
1303 * LOCKING:
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001304 * Matching bd_mutex locked or the caller is the only user of @disk.
1305 * Might sleep.
Tejun Heo540eed52008-08-25 19:56:15 +09001306 *
1307 * RETURNS:
1308 * 0 on success, -errno on failure.
1309 */
1310int disk_expand_part_tbl(struct gendisk *disk, int partno)
1311{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001312 struct disk_part_tbl *old_ptbl =
1313 rcu_dereference_protected(disk->part_tbl, 1);
Tejun Heo540eed52008-08-25 19:56:15 +09001314 struct disk_part_tbl *new_ptbl;
1315 int len = old_ptbl ? old_ptbl->len : 0;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001316 int i, target;
Jens Axboe5fabcb42014-11-19 13:06:22 -07001317
1318 /*
1319 * check for int overflow, since we can get here from blkpg_ioctl()
1320 * with a user passed 'partno'.
1321 */
1322 target = partno + 1;
1323 if (target < 0)
1324 return -EINVAL;
Tejun Heo540eed52008-08-25 19:56:15 +09001325
1326 /* disk_max_parts() is zero during initialization, ignore if so */
1327 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1328 return -EINVAL;
1329
1330 if (target <= len)
1331 return 0;
1332
Gustavo A. R. Silva78b90a22019-05-31 13:47:54 -05001333 new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
1334 disk->node_id);
Tejun Heo540eed52008-08-25 19:56:15 +09001335 if (!new_ptbl)
1336 return -ENOMEM;
1337
Tejun Heo540eed52008-08-25 19:56:15 +09001338 new_ptbl->len = target;
1339
1340 for (i = 0; i < len; i++)
1341 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1342
1343 disk_replace_part_tbl(disk, new_ptbl);
1344 return 0;
1345}
1346
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001347static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001349 struct gendisk *disk = dev_to_disk(dev);
1350
Keith Busch2da78092014-08-26 09:05:36 -06001351 blk_free_devt(dev->devt);
Tejun Heo77ea8872010-12-08 20:57:37 +01001352 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +09001354 disk_replace_part_tbl(disk, NULL);
Ming Leib54e5ed2015-07-16 11:16:44 +08001355 hd_free_part(&disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001356 if (disk->queue)
1357 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 kfree(disk);
1359}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001360struct class block_class = {
1361 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362};
1363
Kay Sievers3c2670e2013-04-06 09:56:00 -07001364static char *block_devnode(struct device *dev, umode_t *mode,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001365 kuid_t *uid, kgid_t *gid)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001366{
1367 struct gendisk *disk = dev_to_disk(dev);
1368
Kay Sieverse454cea2009-09-18 23:01:12 +02001369 if (disk->devnode)
1370 return disk->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001371 return NULL;
1372}
1373
Bart Van Asscheedf8ff52017-06-20 11:15:48 -07001374static const struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001375 .name = "disk",
1376 .groups = disk_attr_groups,
1377 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001378 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379};
1380
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001381#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001382/*
1383 * aggregate disk stat collector. Uses the same stats that the sysfs
1384 * entries do, above, but makes them available through one seq_file.
1385 *
1386 * The output looks suspiciously like /proc/partitions with a bunch of
1387 * extra fields.
1388 */
1389static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001392 struct disk_part_iter piter;
1393 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 char buf[BDEVNAME_SIZE];
Mikulas Patockae016b782018-12-06 11:41:21 -05001395 unsigned int inflight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001398 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001399 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 " rio rmerge rsect ruse wio wmerge "
1401 "wsect wuse running use aveq"
1402 "\n\n");
1403 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001404
Tejun Heo71982a42009-04-17 08:34:48 +02001405 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001406 while ((hd = disk_part_iter_next(&piter))) {
Mikulas Patockae016b782018-12-06 11:41:21 -05001407 inflight = part_in_flight(gp->queue, hd);
Michael Callahanbdca3c82018-07-18 04:47:40 -07001408 seq_printf(seqf, "%4d %7d %s "
1409 "%lu %lu %lu %u "
1410 "%lu %lu %lu %u "
1411 "%u %u %u "
Konstantin Khlebnikovb6866312019-11-21 13:40:26 +03001412 "%lu %lu %lu %u "
1413 "%lu %u"
1414 "\n",
Tejun Heof331c022008-09-03 09:01:48 +02001415 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1416 disk_name(gp, hd->partno, buf),
Michael Callahandbae2c52018-07-18 04:47:38 -07001417 part_stat_read(hd, ios[STAT_READ]),
1418 part_stat_read(hd, merges[STAT_READ]),
1419 part_stat_read(hd, sectors[STAT_READ]),
Omar Sandovalb57e99b2018-09-21 16:44:34 -07001420 (unsigned int)part_stat_read_msecs(hd, STAT_READ),
Michael Callahandbae2c52018-07-18 04:47:38 -07001421 part_stat_read(hd, ios[STAT_WRITE]),
1422 part_stat_read(hd, merges[STAT_WRITE]),
1423 part_stat_read(hd, sectors[STAT_WRITE]),
Omar Sandovalb57e99b2018-09-21 16:44:34 -07001424 (unsigned int)part_stat_read_msecs(hd, STAT_WRITE),
Mikulas Patockae016b782018-12-06 11:41:21 -05001425 inflight,
Jerome Marchand28f39d52008-02-08 11:04:56 +01001426 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
Michael Callahanbdca3c82018-07-18 04:47:40 -07001427 jiffies_to_msecs(part_stat_read(hd, time_in_queue)),
1428 part_stat_read(hd, ios[STAT_DISCARD]),
1429 part_stat_read(hd, merges[STAT_DISCARD]),
1430 part_stat_read(hd, sectors[STAT_DISCARD]),
Konstantin Khlebnikovb6866312019-11-21 13:40:26 +03001431 (unsigned int)part_stat_read_msecs(hd, STAT_DISCARD),
1432 part_stat_read(hd, ios[STAT_FLUSH]),
1433 (unsigned int)part_stat_read_msecs(hd, STAT_FLUSH)
Jerome Marchand28f39d52008-02-08 11:04:56 +01001434 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001436 disk_part_iter_exit(&piter);
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return 0;
1439}
1440
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001441static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001442 .start = disk_seqf_start,
1443 .next = disk_seqf_next,
1444 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 .show = diskstats_show
1446};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001447
1448static int __init proc_genhd_init(void)
1449{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001450 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1451 proc_create_seq("partitions", 0, NULL, &partitions_op);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001452 return 0;
1453}
1454module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001455#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Tejun Heocf771cb2008-09-03 09:01:09 +02001457dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001458{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001459 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001460 struct class_dev_iter iter;
1461 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001462
Tejun Heodef4e382008-09-03 08:57:12 +02001463 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1464 while ((dev = class_dev_iter_next(&iter))) {
1465 struct gendisk *disk = dev_to_disk(dev);
Tejun Heo548b10e2008-08-29 09:01:47 +02001466 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001467
Kay Sievers3ada8b72009-01-06 10:44:43 -08001468 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001469 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001470
Neil Brown41b8c852009-02-18 10:33:59 +01001471 if (partno < disk->minors) {
1472 /* We need to return the right devno, even
1473 * if the partition doesn't exist yet.
1474 */
1475 devt = MKDEV(MAJOR(dev->devt),
1476 MINOR(dev->devt) + partno);
1477 break;
1478 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001479 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001480 if (part) {
Tejun Heof331c022008-09-03 09:01:48 +02001481 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001482 disk_put_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001483 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001484 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001485 disk_put_part(part);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001486 }
Tejun Heodef4e382008-09-03 08:57:12 +02001487 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001488 return devt;
1489}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001490EXPORT_SYMBOL(blk_lookup_devt);
1491
Byungchul Parke319e1f2017-10-25 17:56:05 +09001492struct gendisk *__alloc_disk_node(int minors, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001493{
1494 struct gendisk *disk;
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001495 struct disk_part_tbl *ptbl;
Christoph Lameter19460892005-06-23 00:08:19 -07001496
Christoph Hellwigde65b012017-08-23 19:10:29 +02001497 if (minors > DISK_MAX_PARTS) {
1498 printk(KERN_ERR
Randy Dunlap7fb52622017-11-18 17:43:38 -08001499 "block: can't allocate more than %d partitions\n",
Christoph Hellwigde65b012017-08-23 19:10:29 +02001500 DISK_MAX_PARTS);
1501 minors = DISK_MAX_PARTS;
1502 }
Christoph Lameter19460892005-06-23 00:08:19 -07001503
Joe Perchesc1b511e2013-08-29 15:21:42 -07001504 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (disk) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001506 if (!init_part_stats(&disk->part0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 kfree(disk);
1508 return NULL;
1509 }
Jan Kara56c09082018-02-26 13:01:41 +01001510 init_rwsem(&disk->lookup_sem);
Cheng Renquanbf91db12008-11-20 08:37:37 +01001511 disk->node_id = node_id;
Tejun Heo540eed52008-08-25 19:56:15 +09001512 if (disk_expand_part_tbl(disk, 0)) {
1513 free_part_stats(&disk->part0);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001514 kfree(disk);
1515 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -07001517 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
1518 rcu_assign_pointer(ptbl->part[0], &disk->part0);
Jens Axboe6c23a962011-01-07 08:43:37 +01001519
Vivek Goyalc83f6bf2012-08-01 12:24:18 +02001520 /*
1521 * set_capacity() and get_capacity() currently don't use
1522 * seqcounter to read/update the part0->nr_sects. Still init
1523 * the counter as we can read the sectors in IO submission
1524 * patch using seqence counters.
1525 *
1526 * TODO: Ideally set_capacity() and get_capacity() should be
1527 * converted to make use of bd_mutex and sequence counters.
1528 */
1529 seqcount_init(&disk->part0.nr_sects_seq);
Ming Lei6c710132015-07-16 11:16:45 +08001530 if (hd_ref_init(&disk->part0)) {
1531 hd_free_part(&disk->part0);
1532 kfree(disk);
1533 return NULL;
1534 }
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 disk->minors = minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001538 disk_to_dev(disk)->class = &block_class;
1539 disk_to_dev(disk)->type = &disk_type;
1540 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 return disk;
1543}
Byungchul Parke319e1f2017-10-25 17:56:05 +09001544EXPORT_SYMBOL(__alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Jan Kara3079c222018-02-26 13:01:38 +01001546struct kobject *get_disk_and_module(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 struct module *owner;
1549 struct kobject *kobj;
1550
1551 if (!disk->fops)
1552 return NULL;
1553 owner = disk->fops->owner;
1554 if (owner && !try_module_get(owner))
1555 return NULL;
Jan Karad01b2dc2017-03-23 01:37:02 +01001556 kobj = kobject_get_unless_zero(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (kobj == NULL) {
1558 module_put(owner);
1559 return NULL;
1560 }
1561 return kobj;
1562
1563}
Jan Kara3079c222018-02-26 13:01:38 +01001564EXPORT_SYMBOL(get_disk_and_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566void put_disk(struct gendisk *disk)
1567{
1568 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001569 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571EXPORT_SYMBOL(put_disk);
1572
Jan Kara9df6c292018-02-26 13:01:39 +01001573/*
1574 * This is a counterpart of get_disk_and_module() and thus also of
1575 * get_gendisk().
1576 */
1577void put_disk_and_module(struct gendisk *disk)
1578{
1579 if (disk) {
1580 struct module *owner = disk->fops->owner;
1581
1582 put_disk(disk);
1583 module_put(owner);
1584 }
1585}
1586EXPORT_SYMBOL(put_disk_and_module);
1587
Hannes Reineckee3264a42009-07-28 09:13:13 +02001588static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1589{
1590 char event[] = "DISK_RO=1";
1591 char *envp[] = { event, NULL };
1592
1593 if (!ro)
1594 event[8] = '0';
1595 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598void set_device_ro(struct block_device *bdev, int flag)
1599{
Tejun Heob7db9952008-08-25 19:56:10 +09001600 bdev->bd_part->policy = flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603EXPORT_SYMBOL(set_device_ro);
1604
1605void set_disk_ro(struct gendisk *disk, int flag)
1606{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001607 struct disk_part_iter piter;
1608 struct hd_struct *part;
1609
Hannes Reineckee3264a42009-07-28 09:13:13 +02001610 if (disk->part0.policy != flag) {
1611 set_disk_ro_uevent(disk, flag);
1612 disk->part0.policy = flag;
1613 }
1614
1615 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001616 while ((part = disk_part_iter_next(&piter)))
1617 part->policy = flag;
1618 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621EXPORT_SYMBOL(set_disk_ro);
1622
1623int bdev_read_only(struct block_device *bdev)
1624{
1625 if (!bdev)
1626 return 0;
Tejun Heob7db9952008-08-25 19:56:10 +09001627 return bdev->bd_part->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
1630EXPORT_SYMBOL(bdev_read_only);
1631
Tejun Heocf771cb2008-09-03 09:01:09 +02001632int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +02001635 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001637 fsync_bdev(bdev);
NeilBrown93b270f2011-02-24 17:25:47 +11001638 res = __invalidate_device(bdev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 bdput(bdev);
1640 }
1641 return res;
1642}
1643
1644EXPORT_SYMBOL(invalidate_partition);
Tejun Heo77ea8872010-12-08 20:57:37 +01001645
1646/*
1647 * Disk events - monitor disk events like media change and eject request.
1648 */
1649struct disk_events {
1650 struct list_head node; /* all disk_event's */
1651 struct gendisk *disk; /* the associated disk */
1652 spinlock_t lock;
1653
Tejun Heofdd514e2011-06-09 20:43:59 +02001654 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001655 int block; /* event blocking depth */
1656 unsigned int pending; /* events already sent out */
1657 unsigned int clearing; /* events being cleared */
1658
1659 long poll_msecs; /* interval, -1 for default */
1660 struct delayed_work dwork;
1661};
1662
1663static const char *disk_events_strs[] = {
1664 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1665 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1666};
1667
1668static char *disk_uevents[] = {
1669 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1670 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1671};
1672
1673/* list of all disk_events */
1674static DEFINE_MUTEX(disk_events_mutex);
1675static LIST_HEAD(disk_events);
1676
1677/* disable in-kernel polling by default */
Wei Tang1fe8f342015-11-24 09:58:46 +08001678static unsigned long disk_events_dfl_poll_msecs;
Tejun Heo77ea8872010-12-08 20:57:37 +01001679
1680static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1681{
1682 struct disk_events *ev = disk->ev;
1683 long intv_msecs = 0;
1684
1685 /*
1686 * If device-specific poll interval is set, always use it. If
Martin Wilck673387a2019-03-27 14:51:01 +01001687 * the default is being used, poll if the POLL flag is set.
Tejun Heo77ea8872010-12-08 20:57:37 +01001688 */
1689 if (ev->poll_msecs >= 0)
1690 intv_msecs = ev->poll_msecs;
Martin Wilckc92e2f02019-03-27 14:51:02 +01001691 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
Tejun Heo77ea8872010-12-08 20:57:37 +01001692 intv_msecs = disk_events_dfl_poll_msecs;
1693
1694 return msecs_to_jiffies(intv_msecs);
1695}
1696
Tejun Heoc3af54a2011-06-09 20:43:55 +02001697/**
1698 * disk_block_events - block and flush disk event checking
1699 * @disk: disk to block events for
1700 *
1701 * On return from this function, it is guaranteed that event checking
1702 * isn't in progress and won't happen until unblocked by
1703 * disk_unblock_events(). Events blocking is counted and the actual
1704 * unblocking happens after the matching number of unblocks are done.
1705 *
1706 * Note that this intentionally does not block event checking from
1707 * disk_clear_events().
1708 *
1709 * CONTEXT:
1710 * Might sleep.
1711 */
1712void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001713{
1714 struct disk_events *ev = disk->ev;
1715 unsigned long flags;
1716 bool cancel;
1717
Tejun Heoc3af54a2011-06-09 20:43:55 +02001718 if (!ev)
1719 return;
1720
Tejun Heofdd514e2011-06-09 20:43:59 +02001721 /*
1722 * Outer mutex ensures that the first blocker completes canceling
1723 * the event work before further blockers are allowed to finish.
1724 */
1725 mutex_lock(&ev->block_mutex);
1726
Tejun Heo77ea8872010-12-08 20:57:37 +01001727 spin_lock_irqsave(&ev->lock, flags);
1728 cancel = !ev->block++;
1729 spin_unlock_irqrestore(&ev->lock, flags);
1730
Tejun Heoc3af54a2011-06-09 20:43:55 +02001731 if (cancel)
1732 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001733
1734 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001735}
1736
1737static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1738{
1739 struct disk_events *ev = disk->ev;
1740 unsigned long intv;
1741 unsigned long flags;
1742
1743 spin_lock_irqsave(&ev->lock, flags);
1744
1745 if (WARN_ON_ONCE(ev->block <= 0))
1746 goto out_unlock;
1747
1748 if (--ev->block)
1749 goto out_unlock;
1750
Tejun Heo77ea8872010-12-08 20:57:37 +01001751 intv = disk_events_poll_jiffies(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001752 if (check_now)
Viresh Kumar695588f2013-04-24 17:12:56 +05301753 queue_delayed_work(system_freezable_power_efficient_wq,
1754 &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001755 else if (intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301756 queue_delayed_work(system_freezable_power_efficient_wq,
1757 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001758out_unlock:
1759 spin_unlock_irqrestore(&ev->lock, flags);
1760}
1761
1762/**
Tejun Heo77ea8872010-12-08 20:57:37 +01001763 * disk_unblock_events - unblock disk event checking
1764 * @disk: disk to unblock events for
1765 *
1766 * Undo disk_block_events(). When the block count reaches zero, it
1767 * starts events polling if configured.
1768 *
1769 * CONTEXT:
1770 * Don't care. Safe to call from irq context.
1771 */
1772void disk_unblock_events(struct gendisk *disk)
1773{
1774 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01001775 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001776}
1777
1778/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02001779 * disk_flush_events - schedule immediate event checking and flushing
1780 * @disk: disk to check and flush events for
1781 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01001782 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02001783 * Schedule immediate event checking on @disk if not blocked. Events in
1784 * @mask are scheduled to be cleared from the driver. Note that this
1785 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01001786 *
1787 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02001788 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01001789 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001790void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001791{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001792 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001793
1794 if (!ev)
1795 return;
1796
Tejun Heo85ef06d2011-07-01 16:17:47 +02001797 spin_lock_irq(&ev->lock);
1798 ev->clearing |= mask;
Tejun Heo41f63c52012-08-03 10:30:47 -07001799 if (!ev->block)
Viresh Kumar695588f2013-04-24 17:12:56 +05301800 mod_delayed_work(system_freezable_power_efficient_wq,
1801 &ev->dwork, 0);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001802 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001803}
Tejun Heo77ea8872010-12-08 20:57:37 +01001804
1805/**
1806 * disk_clear_events - synchronously check, clear and return pending events
1807 * @disk: disk to fetch and clear events from
Masanari Iidada3dae52014-09-09 01:27:23 +09001808 * @mask: mask of events to be fetched and cleared
Tejun Heo77ea8872010-12-08 20:57:37 +01001809 *
1810 * Disk events are synchronously checked and pending events in @mask
1811 * are cleared and returned. This ignores the block count.
1812 *
1813 * CONTEXT:
1814 * Might sleep.
1815 */
1816unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1817{
1818 const struct block_device_operations *bdops = disk->fops;
1819 struct disk_events *ev = disk->ev;
1820 unsigned int pending;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001821 unsigned int clearing = mask;
Tejun Heo77ea8872010-12-08 20:57:37 +01001822
1823 if (!ev) {
1824 /* for drivers still using the old ->media_changed method */
1825 if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
1826 bdops->media_changed && bdops->media_changed(disk))
1827 return DISK_EVENT_MEDIA_CHANGE;
1828 return 0;
1829 }
1830
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001831 disk_block_events(disk);
1832
1833 /*
1834 * store the union of mask and ev->clearing on the stack so that the
1835 * race with disk_flush_events does not cause ambiguity (ev->clearing
1836 * can still be modified even if events are blocked).
1837 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001838 spin_lock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001839 clearing |= ev->clearing;
1840 ev->clearing = 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001841 spin_unlock_irq(&ev->lock);
1842
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001843 disk_check_events(ev, &clearing);
Derek Basehoreaea24a82012-12-18 12:27:18 -08001844 /*
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001845 * if ev->clearing is not 0, the disk_flush_events got called in the
1846 * middle of this function, so we want to run the workfn without delay.
Derek Basehoreaea24a82012-12-18 12:27:18 -08001847 */
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001848 __disk_unblock_events(disk, ev->clearing ? true : false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001849
1850 /* then, fetch and clear pending events */
1851 spin_lock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001852 pending = ev->pending & mask;
1853 ev->pending &= ~mask;
1854 spin_unlock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001855 WARN_ON_ONCE(clearing & mask);
Tejun Heo77ea8872010-12-08 20:57:37 +01001856
1857 return pending;
1858}
1859
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001860/*
1861 * Separate this part out so that a different pointer for clearing_ptr can be
1862 * passed in for disk_clear_events.
1863 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001864static void disk_events_workfn(struct work_struct *work)
1865{
1866 struct delayed_work *dwork = to_delayed_work(work);
1867 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001868
1869 disk_check_events(ev, &ev->clearing);
1870}
1871
1872static void disk_check_events(struct disk_events *ev,
1873 unsigned int *clearing_ptr)
1874{
Tejun Heo77ea8872010-12-08 20:57:37 +01001875 struct gendisk *disk = ev->disk;
1876 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001877 unsigned int clearing = *clearing_ptr;
Tejun Heo77ea8872010-12-08 20:57:37 +01001878 unsigned int events;
1879 unsigned long intv;
1880 int nr_events = 0, i;
1881
1882 /* check events */
1883 events = disk->fops->check_events(disk, clearing);
1884
1885 /* accumulate pending events and schedule next poll if necessary */
1886 spin_lock_irq(&ev->lock);
1887
1888 events &= ~ev->pending;
1889 ev->pending |= events;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001890 *clearing_ptr &= ~clearing;
Tejun Heo77ea8872010-12-08 20:57:37 +01001891
1892 intv = disk_events_poll_jiffies(disk);
1893 if (!ev->block && intv)
Viresh Kumar695588f2013-04-24 17:12:56 +05301894 queue_delayed_work(system_freezable_power_efficient_wq,
1895 &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001896
1897 spin_unlock_irq(&ev->lock);
1898
Tejun Heo7c88a162011-04-21 19:43:58 +02001899 /*
1900 * Tell userland about new events. Only the events listed in
Martin Wilckc92e2f02019-03-27 14:51:02 +01001901 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1902 * is set. Otherwise, events are processed internally but never
1903 * get reported to userland.
Tejun Heo7c88a162011-04-21 19:43:58 +02001904 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001905 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Martin Wilckc92e2f02019-03-27 14:51:02 +01001906 if ((events & disk->events & (1 << i)) &&
1907 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
Tejun Heo77ea8872010-12-08 20:57:37 +01001908 envp[nr_events++] = disk_uevents[i];
1909
1910 if (nr_events)
1911 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1912}
1913
1914/*
1915 * A disk events enabled device has the following sysfs nodes under
1916 * its /sys/block/X/ directory.
1917 *
1918 * events : list of all supported events
1919 * events_async : list of events which can be detected w/o polling
Martin Wilck673387a2019-03-27 14:51:01 +01001920 * (always empty, only for backwards compatibility)
Tejun Heo77ea8872010-12-08 20:57:37 +01001921 * events_poll_msecs : polling interval, 0: disable, -1: system default
1922 */
1923static ssize_t __disk_events_show(unsigned int events, char *buf)
1924{
1925 const char *delim = "";
1926 ssize_t pos = 0;
1927 int i;
1928
1929 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1930 if (events & (1 << i)) {
1931 pos += sprintf(buf + pos, "%s%s",
1932 delim, disk_events_strs[i]);
1933 delim = " ";
1934 }
1935 if (pos)
1936 pos += sprintf(buf + pos, "\n");
1937 return pos;
1938}
1939
1940static ssize_t disk_events_show(struct device *dev,
1941 struct device_attribute *attr, char *buf)
1942{
1943 struct gendisk *disk = dev_to_disk(dev);
1944
Martin Wilckc92e2f02019-03-27 14:51:02 +01001945 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1946 return 0;
1947
Tejun Heo77ea8872010-12-08 20:57:37 +01001948 return __disk_events_show(disk->events, buf);
1949}
1950
1951static ssize_t disk_events_async_show(struct device *dev,
1952 struct device_attribute *attr, char *buf)
1953{
Martin Wilck673387a2019-03-27 14:51:01 +01001954 return 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001955}
1956
1957static ssize_t disk_events_poll_msecs_show(struct device *dev,
1958 struct device_attribute *attr,
1959 char *buf)
1960{
1961 struct gendisk *disk = dev_to_disk(dev);
1962
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001963 if (!disk->ev)
1964 return sprintf(buf, "-1\n");
1965
Tejun Heo77ea8872010-12-08 20:57:37 +01001966 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1967}
1968
1969static ssize_t disk_events_poll_msecs_store(struct device *dev,
1970 struct device_attribute *attr,
1971 const char *buf, size_t count)
1972{
1973 struct gendisk *disk = dev_to_disk(dev);
1974 long intv;
1975
1976 if (!count || !sscanf(buf, "%ld", &intv))
1977 return -EINVAL;
1978
1979 if (intv < 0 && intv != -1)
1980 return -EINVAL;
1981
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01001982 if (!disk->ev)
1983 return -ENODEV;
1984
Tejun Heoc3af54a2011-06-09 20:43:55 +02001985 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001986 disk->ev->poll_msecs = intv;
1987 __disk_unblock_events(disk, true);
1988
1989 return count;
1990}
1991
Joe Perches5657a812018-05-24 13:38:59 -06001992static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
1993static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
1994static const DEVICE_ATTR(events_poll_msecs, 0644,
Tejun Heo77ea8872010-12-08 20:57:37 +01001995 disk_events_poll_msecs_show,
1996 disk_events_poll_msecs_store);
1997
1998static const struct attribute *disk_events_attrs[] = {
1999 &dev_attr_events.attr,
2000 &dev_attr_events_async.attr,
2001 &dev_attr_events_poll_msecs.attr,
2002 NULL,
2003};
2004
2005/*
2006 * The default polling interval can be specified by the kernel
2007 * parameter block.events_dfl_poll_msecs which defaults to 0
2008 * (disable). This can also be modified runtime by writing to
Akinobu Mita1624b0b2019-07-16 21:59:35 +09002009 * /sys/module/block/parameters/events_dfl_poll_msecs.
Tejun Heo77ea8872010-12-08 20:57:37 +01002010 */
2011static int disk_events_set_dfl_poll_msecs(const char *val,
2012 const struct kernel_param *kp)
2013{
2014 struct disk_events *ev;
2015 int ret;
2016
2017 ret = param_set_ulong(val, kp);
2018 if (ret < 0)
2019 return ret;
2020
2021 mutex_lock(&disk_events_mutex);
2022
2023 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02002024 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01002025
2026 mutex_unlock(&disk_events_mutex);
2027
2028 return 0;
2029}
2030
2031static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
2032 .set = disk_events_set_dfl_poll_msecs,
2033 .get = param_get_ulong,
2034};
2035
2036#undef MODULE_PARAM_PREFIX
2037#define MODULE_PARAM_PREFIX "block."
2038
2039module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
2040 &disk_events_dfl_poll_msecs, 0644);
2041
2042/*
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002043 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
Tejun Heo77ea8872010-12-08 20:57:37 +01002044 */
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002045static void disk_alloc_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01002046{
2047 struct disk_events *ev;
2048
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002049 if (!disk->fops->check_events || !disk->events)
Tejun Heo77ea8872010-12-08 20:57:37 +01002050 return;
2051
2052 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
2053 if (!ev) {
2054 pr_warn("%s: failed to initialize events\n", disk->disk_name);
2055 return;
2056 }
2057
Tejun Heo77ea8872010-12-08 20:57:37 +01002058 INIT_LIST_HEAD(&ev->node);
2059 ev->disk = disk;
2060 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02002061 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01002062 ev->block = 1;
2063 ev->poll_msecs = -1;
2064 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2065
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002066 disk->ev = ev;
2067}
2068
2069static void disk_add_events(struct gendisk *disk)
2070{
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002071 /* FIXME: error handling */
2072 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2073 pr_warn("%s: failed to create sysfs files for events\n",
2074 disk->disk_name);
2075
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002076 if (!disk->ev)
2077 return;
2078
Tejun Heo77ea8872010-12-08 20:57:37 +01002079 mutex_lock(&disk_events_mutex);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01002080 list_add_tail(&disk->ev->node, &disk_events);
Tejun Heo77ea8872010-12-08 20:57:37 +01002081 mutex_unlock(&disk_events_mutex);
2082
2083 /*
2084 * Block count is initialized to 1 and the following initial
2085 * unblock kicks it into action.
2086 */
2087 __disk_unblock_events(disk, true);
2088}
2089
2090static void disk_del_events(struct gendisk *disk)
2091{
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002092 if (disk->ev) {
2093 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01002094
Martin Wilckcdf3e3d2019-03-27 14:51:05 +01002095 mutex_lock(&disk_events_mutex);
2096 list_del_init(&disk->ev->node);
2097 mutex_unlock(&disk_events_mutex);
2098 }
Tejun Heo77ea8872010-12-08 20:57:37 +01002099
2100 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2101}
2102
2103static void disk_release_events(struct gendisk *disk)
2104{
2105 /* the block count should be 1 from disk_del_events() */
2106 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
2107 kfree(disk->ev);
2108}