Christoph Hellwig | 3dcf60b | 2019-04-30 14:42:43 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * gendisk handling |
Christoph Hellwig | 7b51e70 | 2020-12-10 08:55:44 +0100 | [diff] [blame] | 4 | * |
| 5 | * Portions Copyright (C) 2020 Christoph Hellwig |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 9 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/fs.h> |
| 11 | #include <linux/genhd.h> |
Andrew Morton | b446b60 | 2007-02-20 13:57:48 -0800 | [diff] [blame] | 12 | #include <linux/kdev_t.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/blkdev.h> |
Tejun Heo | 66114ca | 2015-05-22 17:13:32 -0400 | [diff] [blame] | 15 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/spinlock.h> |
Alexey Dobriyan | f500975 | 2008-10-04 23:53:21 +0400 | [diff] [blame] | 18 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/seq_file.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/kmod.h> |
Jes Sorensen | 58383af | 2006-02-06 14:12:43 -0800 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 23 | #include <linux/idr.h> |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 24 | #include <linux/log2.h> |
Ming Lei | 25e823c | 2013-02-22 16:34:13 -0800 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
Vishal Verma | 99e6608 | 2016-01-09 08:36:51 -0800 | [diff] [blame] | 26 | #include <linux/badblocks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Adrian Bunk | ff88972 | 2008-03-04 11:23:45 +0100 | [diff] [blame] | 28 | #include "blk.h" |
| 29 | |
Christoph Hellwig | 31eb618 | 2020-03-25 16:48:35 +0100 | [diff] [blame] | 30 | static struct kobject *block_depr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 32 | DECLARE_RWSEM(bdev_lookup_sem); |
Christoph Hellwig | 62b508f | 2020-10-29 15:58:25 +0100 | [diff] [blame] | 33 | |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 34 | /* for extended dynamic devt allocation, currently only one major is used */ |
Tejun Heo | ce23bba | 2013-02-27 17:03:56 -0800 | [diff] [blame] | 35 | #define NR_EXT_DEVT (1 << MINORBITS) |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 36 | static DEFINE_IDA(ext_devt_ida); |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 37 | |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 38 | static void disk_check_events(struct disk_events *ev, |
| 39 | unsigned int *clearing_ptr); |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 40 | static void disk_alloc_events(struct gendisk *disk); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 41 | static void disk_add_events(struct gendisk *disk); |
| 42 | static void disk_del_events(struct gendisk *disk); |
| 43 | static void disk_release_events(struct gendisk *disk); |
| 44 | |
Christoph Hellwig | a782483 | 2020-11-26 18:43:37 +0100 | [diff] [blame] | 45 | void set_capacity(struct gendisk *disk, sector_t sectors) |
| 46 | { |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 47 | struct block_device *bdev = disk->part0; |
Christoph Hellwig | a782483 | 2020-11-26 18:43:37 +0100 | [diff] [blame] | 48 | |
Damien Le Moal | 0f47227 | 2021-03-01 12:04:02 +0900 | [diff] [blame] | 49 | spin_lock(&bdev->bd_size_lock); |
Christoph Hellwig | a782483 | 2020-11-26 18:43:37 +0100 | [diff] [blame] | 50 | i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT); |
Damien Le Moal | 0f47227 | 2021-03-01 12:04:02 +0900 | [diff] [blame] | 51 | spin_unlock(&bdev->bd_size_lock); |
Christoph Hellwig | a782483 | 2020-11-26 18:43:37 +0100 | [diff] [blame] | 52 | } |
| 53 | EXPORT_SYMBOL(set_capacity); |
| 54 | |
Balbir Singh | e598a72 | 2020-03-13 05:30:05 +0000 | [diff] [blame] | 55 | /* |
Christoph Hellwig | 449f4ec | 2020-11-16 15:56:56 +0100 | [diff] [blame] | 56 | * Set disk capacity and notify if the size is not currently zero and will not |
| 57 | * be set to zero. Returns true if a uevent was sent, otherwise false. |
Balbir Singh | e598a72 | 2020-03-13 05:30:05 +0000 | [diff] [blame] | 58 | */ |
Christoph Hellwig | 449f4ec | 2020-11-16 15:56:56 +0100 | [diff] [blame] | 59 | bool set_capacity_and_notify(struct gendisk *disk, sector_t size) |
Balbir Singh | e598a72 | 2020-03-13 05:30:05 +0000 | [diff] [blame] | 60 | { |
| 61 | sector_t capacity = get_capacity(disk); |
Christoph Hellwig | a782483 | 2020-11-26 18:43:37 +0100 | [diff] [blame] | 62 | char *envp[] = { "RESIZE=1", NULL }; |
Balbir Singh | e598a72 | 2020-03-13 05:30:05 +0000 | [diff] [blame] | 63 | |
| 64 | set_capacity(disk, size); |
Balbir Singh | e598a72 | 2020-03-13 05:30:05 +0000 | [diff] [blame] | 65 | |
Christoph Hellwig | a782483 | 2020-11-26 18:43:37 +0100 | [diff] [blame] | 66 | /* |
| 67 | * Only print a message and send a uevent if the gendisk is user visible |
| 68 | * and alive. This avoids spamming the log and udev when setting the |
| 69 | * initial capacity during probing. |
| 70 | */ |
| 71 | if (size == capacity || |
| 72 | (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP) |
| 73 | return false; |
Balbir Singh | e598a72 | 2020-03-13 05:30:05 +0000 | [diff] [blame] | 74 | |
Christoph Hellwig | a782483 | 2020-11-26 18:43:37 +0100 | [diff] [blame] | 75 | pr_info("%s: detected capacity change from %lld to %lld\n", |
Ming Lei | 452c0bf | 2021-02-23 16:50:15 +0800 | [diff] [blame] | 76 | disk->disk_name, capacity, size); |
Christoph Hellwig | 7e890c3 | 2020-11-12 17:50:04 +0100 | [diff] [blame] | 77 | |
Christoph Hellwig | a782483 | 2020-11-26 18:43:37 +0100 | [diff] [blame] | 78 | /* |
| 79 | * Historically we did not send a uevent for changes to/from an empty |
| 80 | * device. |
| 81 | */ |
| 82 | if (!capacity || !size) |
| 83 | return false; |
| 84 | kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp); |
| 85 | return true; |
Balbir Singh | e598a72 | 2020-03-13 05:30:05 +0000 | [diff] [blame] | 86 | } |
Christoph Hellwig | 449f4ec | 2020-11-16 15:56:56 +0100 | [diff] [blame] | 87 | EXPORT_SYMBOL_GPL(set_capacity_and_notify); |
Balbir Singh | e598a72 | 2020-03-13 05:30:05 +0000 | [diff] [blame] | 88 | |
Christoph Hellwig | 5cbd28e | 2020-03-24 08:25:12 +0100 | [diff] [blame] | 89 | /* |
| 90 | * Format the device name of the indicated disk into the supplied buffer and |
| 91 | * return a pointer to that same buffer for convenience. |
| 92 | */ |
| 93 | char *disk_name(struct gendisk *hd, int partno, char *buf) |
| 94 | { |
| 95 | if (!partno) |
| 96 | snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name); |
| 97 | else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) |
| 98 | snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno); |
| 99 | else |
| 100 | snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno); |
| 101 | |
| 102 | return buf; |
| 103 | } |
| 104 | |
| 105 | const char *bdevname(struct block_device *bdev, char *buf) |
| 106 | { |
Christoph Hellwig | 8a63a86 | 2020-09-03 07:41:03 +0200 | [diff] [blame] | 107 | return disk_name(bdev->bd_disk, bdev->bd_partno, buf); |
Christoph Hellwig | 5cbd28e | 2020-03-24 08:25:12 +0100 | [diff] [blame] | 108 | } |
| 109 | EXPORT_SYMBOL(bdevname); |
Balbir Singh | e598a72 | 2020-03-13 05:30:05 +0000 | [diff] [blame] | 110 | |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 111 | static void part_stat_read_all(struct block_device *part, |
| 112 | struct disk_stats *stat) |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 113 | { |
| 114 | int cpu; |
| 115 | |
| 116 | memset(stat, 0, sizeof(struct disk_stats)); |
| 117 | for_each_possible_cpu(cpu) { |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 118 | struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu); |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 119 | int group; |
| 120 | |
| 121 | for (group = 0; group < NR_STAT_GROUPS; group++) { |
| 122 | stat->nsecs[group] += ptr->nsecs[group]; |
| 123 | stat->sectors[group] += ptr->sectors[group]; |
| 124 | stat->ios[group] += ptr->ios[group]; |
| 125 | stat->merges[group] += ptr->merges[group]; |
| 126 | } |
| 127 | |
| 128 | stat->io_ticks += ptr->io_ticks; |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 129 | } |
| 130 | } |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 131 | |
Christoph Hellwig | 8446fe9 | 2020-11-24 09:36:54 +0100 | [diff] [blame] | 132 | static unsigned int part_in_flight(struct block_device *part) |
Jens Axboe | f299b7c | 2017-08-08 17:51:45 -0600 | [diff] [blame] | 133 | { |
Christoph Hellwig | b2f609e | 2020-05-13 12:49:33 +0200 | [diff] [blame] | 134 | unsigned int inflight = 0; |
Mikulas Patocka | 1226b8d | 2018-12-06 11:41:20 -0500 | [diff] [blame] | 135 | int cpu; |
| 136 | |
Mikulas Patocka | 1226b8d | 2018-12-06 11:41:20 -0500 | [diff] [blame] | 137 | for_each_possible_cpu(cpu) { |
Mikulas Patocka | e016b78 | 2018-12-06 11:41:21 -0500 | [diff] [blame] | 138 | inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) + |
| 139 | part_stat_local_read_cpu(part, in_flight[1], cpu); |
Mikulas Patocka | 1226b8d | 2018-12-06 11:41:20 -0500 | [diff] [blame] | 140 | } |
Mikulas Patocka | e016b78 | 2018-12-06 11:41:21 -0500 | [diff] [blame] | 141 | if ((int)inflight < 0) |
| 142 | inflight = 0; |
Mikulas Patocka | 1226b8d | 2018-12-06 11:41:20 -0500 | [diff] [blame] | 143 | |
Mikulas Patocka | e016b78 | 2018-12-06 11:41:21 -0500 | [diff] [blame] | 144 | return inflight; |
Jens Axboe | f299b7c | 2017-08-08 17:51:45 -0600 | [diff] [blame] | 145 | } |
| 146 | |
Christoph Hellwig | 8446fe9 | 2020-11-24 09:36:54 +0100 | [diff] [blame] | 147 | static void part_in_flight_rw(struct block_device *part, |
| 148 | unsigned int inflight[2]) |
Omar Sandoval | bf0ddab | 2018-04-26 00:21:59 -0700 | [diff] [blame] | 149 | { |
Mikulas Patocka | 1226b8d | 2018-12-06 11:41:20 -0500 | [diff] [blame] | 150 | int cpu; |
| 151 | |
Mikulas Patocka | 1226b8d | 2018-12-06 11:41:20 -0500 | [diff] [blame] | 152 | inflight[0] = 0; |
| 153 | inflight[1] = 0; |
| 154 | for_each_possible_cpu(cpu) { |
| 155 | inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu); |
| 156 | inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu); |
| 157 | } |
| 158 | if ((int)inflight[0] < 0) |
| 159 | inflight[0] = 0; |
| 160 | if ((int)inflight[1] < 0) |
| 161 | inflight[1] = 0; |
Omar Sandoval | bf0ddab | 2018-04-26 00:21:59 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 164 | /** |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 165 | * disk_part_iter_init - initialize partition iterator |
| 166 | * @piter: iterator to initialize |
| 167 | * @disk: disk to iterate over |
| 168 | * @flags: DISK_PITER_* flags |
| 169 | * |
| 170 | * Initialize @piter so that it iterates over partitions of @disk. |
| 171 | * |
| 172 | * CONTEXT: |
| 173 | * Don't care. |
| 174 | */ |
| 175 | void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk, |
| 176 | unsigned int flags) |
| 177 | { |
| 178 | piter->disk = disk; |
| 179 | piter->part = NULL; |
Christoph Hellwig | 0470dd9 | 2021-01-24 11:02:40 +0100 | [diff] [blame] | 180 | if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0)) |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 181 | piter->idx = 0; |
Tejun Heo | b5d0b9d | 2008-09-03 09:06:42 +0200 | [diff] [blame] | 182 | else |
| 183 | piter->idx = 1; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 184 | piter->flags = flags; |
| 185 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * disk_part_iter_next - proceed iterator to the next partition and return it |
| 189 | * @piter: iterator of interest |
| 190 | * |
| 191 | * Proceed @piter to the next partition and return it. |
| 192 | * |
| 193 | * CONTEXT: |
| 194 | * Don't care. |
| 195 | */ |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 196 | struct block_device *disk_part_iter_next(struct disk_part_iter *piter) |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 197 | { |
Christoph Hellwig | a33df75 | 2021-01-24 11:02:41 +0100 | [diff] [blame] | 198 | struct block_device *part; |
| 199 | unsigned long idx; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 200 | |
| 201 | /* put the last partition */ |
Christoph Hellwig | e79319a | 2020-11-10 06:48:53 +0100 | [diff] [blame] | 202 | disk_part_iter_exit(piter); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 203 | |
| 204 | rcu_read_lock(); |
Christoph Hellwig | a33df75 | 2021-01-24 11:02:41 +0100 | [diff] [blame] | 205 | xa_for_each_start(&piter->disk->part_tbl, idx, part, piter->idx) { |
Ming Lei | aebf5db | 2020-12-21 12:33:35 +0800 | [diff] [blame] | 206 | if (!bdev_nr_sectors(part) && |
| 207 | !(piter->flags & DISK_PITER_INCL_EMPTY) && |
| 208 | !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 && |
Christoph Hellwig | a33df75 | 2021-01-24 11:02:41 +0100 | [diff] [blame] | 209 | piter->idx == 0)) |
Ming Lei | aebf5db | 2020-12-21 12:33:35 +0800 | [diff] [blame] | 210 | continue; |
Ming Lei | aebf5db | 2020-12-21 12:33:35 +0800 | [diff] [blame] | 211 | |
Christoph Hellwig | 0470dd9 | 2021-01-24 11:02:40 +0100 | [diff] [blame] | 212 | piter->part = bdgrab(part); |
| 213 | if (!piter->part) |
| 214 | continue; |
Christoph Hellwig | a33df75 | 2021-01-24 11:02:41 +0100 | [diff] [blame] | 215 | piter->idx = idx + 1; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 216 | break; |
| 217 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 218 | rcu_read_unlock(); |
| 219 | |
| 220 | return piter->part; |
| 221 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 222 | |
| 223 | /** |
| 224 | * disk_part_iter_exit - finish up partition iteration |
| 225 | * @piter: iter of interest |
| 226 | * |
| 227 | * Called when iteration is over. Cleans up @piter. |
| 228 | * |
| 229 | * CONTEXT: |
| 230 | * Don't care. |
| 231 | */ |
| 232 | void disk_part_iter_exit(struct disk_part_iter *piter) |
| 233 | { |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 234 | if (piter->part) |
| 235 | bdput(piter->part); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 236 | piter->part = NULL; |
| 237 | } |
Shin'ichiro Kawasaki | b53df2e | 2020-02-21 10:37:08 +0900 | [diff] [blame] | 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | /* |
| 240 | * Can be deleted altogether. Later. |
| 241 | * |
| 242 | */ |
Logan Gunthorpe | 133d55c | 2017-06-16 17:48:21 -0600 | [diff] [blame] | 243 | #define BLKDEV_MAJOR_HASH_SIZE 255 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | static struct blk_major_name { |
| 245 | struct blk_major_name *next; |
| 246 | int major; |
| 247 | char name[16]; |
Christoph Hellwig | a160c61 | 2020-10-29 15:58:28 +0100 | [diff] [blame] | 248 | void (*probe)(dev_t devt); |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 249 | } *major_names[BLKDEV_MAJOR_HASH_SIZE]; |
Christoph Hellwig | e49fbbb | 2020-10-29 15:58:26 +0100 | [diff] [blame] | 250 | static DEFINE_MUTEX(major_names_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | /* index in the above - for now: assume no multimajor ranges */ |
Yang Zhang | e61eb2e | 2010-12-17 09:00:18 +0100 | [diff] [blame] | 253 | static inline int major_to_index(unsigned major) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 255 | return major % BLKDEV_MAJOR_HASH_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 258 | #ifdef CONFIG_PROC_FS |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 259 | void blkdev_show(struct seq_file *seqf, off_t offset) |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 260 | { |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 261 | struct blk_major_name *dp; |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 262 | |
Christoph Hellwig | e49fbbb | 2020-10-29 15:58:26 +0100 | [diff] [blame] | 263 | mutex_lock(&major_names_lock); |
Logan Gunthorpe | 133d55c | 2017-06-16 17:48:21 -0600 | [diff] [blame] | 264 | for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next) |
| 265 | if (dp->major == offset) |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 266 | seq_printf(seqf, "%3d %s\n", dp->major, dp->name); |
Christoph Hellwig | e49fbbb | 2020-10-29 15:58:26 +0100 | [diff] [blame] | 267 | mutex_unlock(&major_names_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 269 | #endif /* CONFIG_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Márton Németh | 9e8c0bc | 2009-02-20 08:12:51 +0100 | [diff] [blame] | 271 | /** |
Christoph Hellwig | e2b6b30 | 2020-11-14 18:08:21 +0100 | [diff] [blame] | 272 | * __register_blkdev - register a new block device |
Márton Németh | 9e8c0bc | 2009-02-20 08:12:51 +0100 | [diff] [blame] | 273 | * |
Srivatsa S. Bhat | f33ff11 | 2018-02-05 18:25:27 -0800 | [diff] [blame] | 274 | * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If |
| 275 | * @major = 0, try to allocate any unused major number. |
Márton Németh | 9e8c0bc | 2009-02-20 08:12:51 +0100 | [diff] [blame] | 276 | * @name: the name of the new block device as a zero terminated string |
Christoph Hellwig | e2b6b30 | 2020-11-14 18:08:21 +0100 | [diff] [blame] | 277 | * @probe: allback that is called on access to any minor number of @major |
Márton Németh | 9e8c0bc | 2009-02-20 08:12:51 +0100 | [diff] [blame] | 278 | * |
| 279 | * The @name must be unique within the system. |
| 280 | * |
mchehab@s-opensource.com | 0e056eb | 2017-03-30 17:11:36 -0300 | [diff] [blame] | 281 | * The return value depends on the @major input parameter: |
| 282 | * |
Srivatsa S. Bhat | f33ff11 | 2018-02-05 18:25:27 -0800 | [diff] [blame] | 283 | * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1] |
| 284 | * then the function returns zero on success, or a negative error code |
mchehab@s-opensource.com | 0e056eb | 2017-03-30 17:11:36 -0300 | [diff] [blame] | 285 | * - if any unused major number was requested with @major = 0 parameter |
Márton Németh | 9e8c0bc | 2009-02-20 08:12:51 +0100 | [diff] [blame] | 286 | * then the return value is the allocated major number in range |
Srivatsa S. Bhat | f33ff11 | 2018-02-05 18:25:27 -0800 | [diff] [blame] | 287 | * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise |
| 288 | * |
| 289 | * See Documentation/admin-guide/devices.txt for the list of allocated |
| 290 | * major numbers. |
Christoph Hellwig | e2b6b30 | 2020-11-14 18:08:21 +0100 | [diff] [blame] | 291 | * |
| 292 | * Use register_blkdev instead for any new code. |
Márton Németh | 9e8c0bc | 2009-02-20 08:12:51 +0100 | [diff] [blame] | 293 | */ |
Christoph Hellwig | a160c61 | 2020-10-29 15:58:28 +0100 | [diff] [blame] | 294 | int __register_blkdev(unsigned int major, const char *name, |
| 295 | void (*probe)(dev_t devt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
| 297 | struct blk_major_name **n, *p; |
| 298 | int index, ret = 0; |
| 299 | |
Christoph Hellwig | e49fbbb | 2020-10-29 15:58:26 +0100 | [diff] [blame] | 300 | mutex_lock(&major_names_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | /* temporary */ |
| 303 | if (major == 0) { |
| 304 | for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) { |
| 305 | if (major_names[index] == NULL) |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | if (index == 0) { |
Keyur Patel | dfc76d1 | 2019-02-17 10:21:56 -0500 | [diff] [blame] | 310 | printk("%s: failed to get major for %s\n", |
| 311 | __func__, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | ret = -EBUSY; |
| 313 | goto out; |
| 314 | } |
| 315 | major = index; |
| 316 | ret = major; |
| 317 | } |
| 318 | |
Logan Gunthorpe | 133d55c | 2017-06-16 17:48:21 -0600 | [diff] [blame] | 319 | if (major >= BLKDEV_MAJOR_MAX) { |
Keyur Patel | dfc76d1 | 2019-02-17 10:21:56 -0500 | [diff] [blame] | 320 | pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n", |
| 321 | __func__, major, BLKDEV_MAJOR_MAX-1, name); |
Logan Gunthorpe | 133d55c | 2017-06-16 17:48:21 -0600 | [diff] [blame] | 322 | |
| 323 | ret = -EINVAL; |
| 324 | goto out; |
| 325 | } |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL); |
| 328 | if (p == NULL) { |
| 329 | ret = -ENOMEM; |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | p->major = major; |
Christoph Hellwig | a160c61 | 2020-10-29 15:58:28 +0100 | [diff] [blame] | 334 | p->probe = probe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | strlcpy(p->name, name, sizeof(p->name)); |
| 336 | p->next = NULL; |
| 337 | index = major_to_index(major); |
| 338 | |
| 339 | for (n = &major_names[index]; *n; n = &(*n)->next) { |
| 340 | if ((*n)->major == major) |
| 341 | break; |
| 342 | } |
| 343 | if (!*n) |
| 344 | *n = p; |
| 345 | else |
| 346 | ret = -EBUSY; |
| 347 | |
| 348 | if (ret < 0) { |
Srivatsa S. Bhat | f33ff11 | 2018-02-05 18:25:27 -0800 | [diff] [blame] | 349 | printk("register_blkdev: cannot get major %u for %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | major, name); |
| 351 | kfree(p); |
| 352 | } |
| 353 | out: |
Christoph Hellwig | e49fbbb | 2020-10-29 15:58:26 +0100 | [diff] [blame] | 354 | mutex_unlock(&major_names_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return ret; |
| 356 | } |
Christoph Hellwig | a160c61 | 2020-10-29 15:58:28 +0100 | [diff] [blame] | 357 | EXPORT_SYMBOL(__register_blkdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Akinobu Mita | f448024 | 2007-07-17 04:03:47 -0700 | [diff] [blame] | 359 | void unregister_blkdev(unsigned int major, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
| 361 | struct blk_major_name **n; |
| 362 | struct blk_major_name *p = NULL; |
| 363 | int index = major_to_index(major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Christoph Hellwig | e49fbbb | 2020-10-29 15:58:26 +0100 | [diff] [blame] | 365 | mutex_lock(&major_names_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | for (n = &major_names[index]; *n; n = &(*n)->next) |
| 367 | if ((*n)->major == major) |
| 368 | break; |
Akinobu Mita | 294462a | 2007-07-17 04:03:45 -0700 | [diff] [blame] | 369 | if (!*n || strcmp((*n)->name, name)) { |
| 370 | WARN_ON(1); |
Akinobu Mita | 294462a | 2007-07-17 04:03:45 -0700 | [diff] [blame] | 371 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | p = *n; |
| 373 | *n = p->next; |
| 374 | } |
Christoph Hellwig | e49fbbb | 2020-10-29 15:58:26 +0100 | [diff] [blame] | 375 | mutex_unlock(&major_names_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | EXPORT_SYMBOL(unregister_blkdev); |
| 380 | |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 381 | /** |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 382 | * blk_mangle_minor - scatter minor numbers apart |
| 383 | * @minor: minor number to mangle |
| 384 | * |
| 385 | * Scatter consecutively allocated @minor number apart if MANGLE_DEVT |
| 386 | * is enabled. Mangling twice gives the original value. |
| 387 | * |
| 388 | * RETURNS: |
| 389 | * Mangled value. |
| 390 | * |
| 391 | * CONTEXT: |
| 392 | * Don't care. |
| 393 | */ |
| 394 | static int blk_mangle_minor(int minor) |
| 395 | { |
| 396 | #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT |
| 397 | int i; |
| 398 | |
| 399 | for (i = 0; i < MINORBITS / 2; i++) { |
| 400 | int low = minor & (1 << i); |
| 401 | int high = minor & (1 << (MINORBITS - 1 - i)); |
| 402 | int distance = MINORBITS - 1 - 2 * i; |
| 403 | |
| 404 | minor ^= low | high; /* clear both bits */ |
| 405 | low <<= distance; /* swap the positions */ |
| 406 | high >>= distance; |
| 407 | minor |= low | high; /* and set */ |
| 408 | } |
| 409 | #endif |
| 410 | return minor; |
| 411 | } |
| 412 | |
| 413 | /** |
Christoph Hellwig | 9fc995a | 2020-11-24 09:17:46 +0100 | [diff] [blame] | 414 | * blk_alloc_devt - allocate a dev_t for a block device |
| 415 | * @bdev: block device to allocate dev_t for |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 416 | * @devt: out parameter for resulting dev_t |
| 417 | * |
| 418 | * Allocate a dev_t for block device. |
| 419 | * |
| 420 | * RETURNS: |
| 421 | * 0 on success, allocated dev_t is returned in *@devt. -errno on |
| 422 | * failure. |
| 423 | * |
| 424 | * CONTEXT: |
| 425 | * Might sleep. |
| 426 | */ |
Christoph Hellwig | 9fc995a | 2020-11-24 09:17:46 +0100 | [diff] [blame] | 427 | int blk_alloc_devt(struct block_device *bdev, dev_t *devt) |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 428 | { |
Christoph Hellwig | 9fc995a | 2020-11-24 09:17:46 +0100 | [diff] [blame] | 429 | struct gendisk *disk = bdev->bd_disk; |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 430 | int idx; |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 431 | |
| 432 | /* in consecutive minor range? */ |
Christoph Hellwig | 9fc995a | 2020-11-24 09:17:46 +0100 | [diff] [blame] | 433 | if (bdev->bd_partno < disk->minors) { |
| 434 | *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno); |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 438 | idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL); |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 439 | if (idx < 0) |
| 440 | return idx == -ENOSPC ? -EBUSY : idx; |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 441 | |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 442 | *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx)); |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * blk_free_devt - free a dev_t |
| 448 | * @devt: dev_t to free |
| 449 | * |
| 450 | * Free @devt which was allocated using blk_alloc_devt(). |
| 451 | * |
| 452 | * CONTEXT: |
| 453 | * Might sleep. |
| 454 | */ |
| 455 | void blk_free_devt(dev_t devt) |
| 456 | { |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 457 | if (MAJOR(devt) == BLOCK_EXT_MAJOR) |
| 458 | ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt))); |
Yufen Yu | 6fcc44d | 2019-04-02 20:06:34 +0800 | [diff] [blame] | 459 | } |
| 460 | |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 461 | static char *bdevt_str(dev_t devt, char *buf) |
| 462 | { |
| 463 | if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) { |
| 464 | char tbuf[BDEVT_SIZE]; |
| 465 | snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt)); |
| 466 | snprintf(buf, BDEVT_SIZE, "%-9s", tbuf); |
| 467 | } else |
| 468 | snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt)); |
| 469 | |
| 470 | return buf; |
| 471 | } |
| 472 | |
Christoph Hellwig | bc359d0 | 2021-01-24 11:02:39 +0100 | [diff] [blame] | 473 | void disk_uevent(struct gendisk *disk, enum kobject_action action) |
| 474 | { |
| 475 | struct disk_part_iter piter; |
| 476 | struct block_device *part; |
| 477 | |
Christoph Hellwig | 75ab6af | 2021-02-23 15:41:25 -0700 | [diff] [blame] | 478 | disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY_PART0); |
Christoph Hellwig | bc359d0 | 2021-01-24 11:02:39 +0100 | [diff] [blame] | 479 | while ((part = disk_part_iter_next(&piter))) |
| 480 | kobject_uevent(bdev_kobj(part), action); |
| 481 | disk_part_iter_exit(&piter); |
| 482 | } |
| 483 | EXPORT_SYMBOL_GPL(disk_uevent); |
| 484 | |
Christoph Hellwig | 9301fe7 | 2020-09-21 09:19:46 +0200 | [diff] [blame] | 485 | static void disk_scan_partitions(struct gendisk *disk) |
| 486 | { |
| 487 | struct block_device *bdev; |
| 488 | |
| 489 | if (!get_capacity(disk) || !disk_part_scan_enabled(disk)) |
| 490 | return; |
| 491 | |
| 492 | set_bit(GD_NEED_PART_SCAN, &disk->state); |
| 493 | bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL); |
| 494 | if (!IS_ERR(bdev)) |
| 495 | blkdev_put(bdev, FMODE_READ); |
| 496 | } |
| 497 | |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 498 | static void register_disk(struct device *parent, struct gendisk *disk, |
| 499 | const struct attribute_group **groups) |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 500 | { |
| 501 | struct device *ddev = disk_to_dev(disk); |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 502 | int err; |
| 503 | |
Dan Williams | e63a46b | 2016-06-15 18:17:27 -0700 | [diff] [blame] | 504 | ddev->parent = parent; |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 505 | |
Kees Cook | ffc8b30 | 2013-07-03 15:01:14 -0700 | [diff] [blame] | 506 | dev_set_name(ddev, "%s", disk->disk_name); |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 507 | |
| 508 | /* delay uevents, until we scanned partition table */ |
| 509 | dev_set_uevent_suppress(ddev, 1); |
| 510 | |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 511 | if (groups) { |
| 512 | WARN_ON(ddev->groups); |
| 513 | ddev->groups = groups; |
| 514 | } |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 515 | if (device_add(ddev)) |
| 516 | return; |
| 517 | if (!sysfs_deprecated) { |
| 518 | err = sysfs_create_link(block_depr, &ddev->kobj, |
| 519 | kobject_name(&ddev->kobj)); |
| 520 | if (err) { |
| 521 | device_del(ddev); |
| 522 | return; |
| 523 | } |
| 524 | } |
Ming Lei | 25e823c | 2013-02-22 16:34:13 -0800 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | * avoid probable deadlock caused by allocating memory with |
| 528 | * GFP_KERNEL in runtime_resume callback of its all ancestor |
| 529 | * devices |
| 530 | */ |
| 531 | pm_runtime_set_memalloc_noio(ddev, true); |
| 532 | |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 533 | disk->part0->bd_holder_dir = |
| 534 | kobject_create_and_add("holders", &ddev->kobj); |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 535 | disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj); |
| 536 | |
Daniel Wagner | 9ec4914 | 2021-03-11 16:19:17 +0100 | [diff] [blame] | 537 | if (disk->flags & GENHD_FL_HIDDEN) |
Christoph Hellwig | 8ddcd65 | 2017-11-02 21:29:53 +0300 | [diff] [blame] | 538 | return; |
Christoph Hellwig | 8ddcd65 | 2017-11-02 21:29:53 +0300 | [diff] [blame] | 539 | |
Christoph Hellwig | 9301fe7 | 2020-09-21 09:19:46 +0200 | [diff] [blame] | 540 | disk_scan_partitions(disk); |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 541 | |
Christoph Hellwig | bc359d0 | 2021-01-24 11:02:39 +0100 | [diff] [blame] | 542 | /* announce the disk and partitions after all partitions are created */ |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 543 | dev_set_uevent_suppress(ddev, 0); |
Christoph Hellwig | bc359d0 | 2021-01-24 11:02:39 +0100 | [diff] [blame] | 544 | disk_uevent(disk, KOBJ_ADD); |
Christoph Hellwig | 8ddcd65 | 2017-11-02 21:29:53 +0300 | [diff] [blame] | 545 | |
zhengbin | 4d7c1d3 | 2019-02-20 21:27:05 +0800 | [diff] [blame] | 546 | if (disk->queue->backing_dev_info->dev) { |
| 547 | err = sysfs_create_link(&ddev->kobj, |
| 548 | &disk->queue->backing_dev_info->dev->kobj, |
| 549 | "bdi"); |
| 550 | WARN_ON(err); |
| 551 | } |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | /** |
Mike Snitzer | fa70d2e | 2018-01-08 22:01:13 -0500 | [diff] [blame] | 555 | * __device_add_disk - add disk information to kernel list |
Dan Williams | e63a46b | 2016-06-15 18:17:27 -0700 | [diff] [blame] | 556 | * @parent: parent device for the disk |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | * @disk: per-device partitioning information |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 558 | * @groups: Additional per-device sysfs groups |
Mike Snitzer | fa70d2e | 2018-01-08 22:01:13 -0500 | [diff] [blame] | 559 | * @register_queue: register the queue if set to true |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | * |
| 561 | * This function registers the partitioning information in @disk |
| 562 | * with the kernel. |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 563 | * |
| 564 | * FIXME: error handling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | */ |
Mike Snitzer | fa70d2e | 2018-01-08 22:01:13 -0500 | [diff] [blame] | 566 | static void __device_add_disk(struct device *parent, struct gendisk *disk, |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 567 | const struct attribute_group **groups, |
Mike Snitzer | fa70d2e | 2018-01-08 22:01:13 -0500 | [diff] [blame] | 568 | bool register_queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 570 | dev_t devt; |
Greg Kroah-Hartman | 6ffeea7 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 571 | int retval; |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 572 | |
Damien Le Moal | 737eb78 | 2019-09-05 18:51:33 +0900 | [diff] [blame] | 573 | /* |
| 574 | * The disk queue should now be all set with enough information about |
| 575 | * the device for the elevator code to pick an adequate default |
| 576 | * elevator if one is needed, that is, for devices requesting queue |
| 577 | * registration. |
| 578 | */ |
| 579 | if (register_queue) |
| 580 | elevator_init_mq(disk->queue); |
| 581 | |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 582 | /* minors == 0 indicates to use ext devt from part0 and should |
| 583 | * be accompanied with EXT_DEVT flag. Make sure all |
| 584 | * parameters make sense. |
| 585 | */ |
| 586 | WARN_ON(disk->minors && !(disk->major || disk->first_minor)); |
Christoph Hellwig | 8ddcd65 | 2017-11-02 21:29:53 +0300 | [diff] [blame] | 587 | WARN_ON(!disk->minors && |
| 588 | !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN))); |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | disk->flags |= GENHD_FL_UP; |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 591 | |
Christoph Hellwig | 9fc995a | 2020-11-24 09:17:46 +0100 | [diff] [blame] | 592 | retval = blk_alloc_devt(disk->part0, &devt); |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 593 | if (retval) { |
| 594 | WARN_ON(1); |
| 595 | return; |
| 596 | } |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 597 | disk->major = MAJOR(devt); |
| 598 | disk->first_minor = MINOR(devt); |
| 599 | |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 600 | disk_alloc_events(disk); |
| 601 | |
Christoph Hellwig | 8ddcd65 | 2017-11-02 21:29:53 +0300 | [diff] [blame] | 602 | if (disk->flags & GENHD_FL_HIDDEN) { |
| 603 | /* |
| 604 | * Don't let hidden disks show up in /proc/partitions, |
| 605 | * and don't bother scanning for partitions either. |
| 606 | */ |
| 607 | disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO; |
| 608 | disk->flags |= GENHD_FL_NO_PART_SCAN; |
| 609 | } else { |
Christoph Hellwig | 3c5d202 | 2020-05-04 14:47:59 +0200 | [diff] [blame] | 610 | struct backing_dev_info *bdi = disk->queue->backing_dev_info; |
| 611 | struct device *dev = disk_to_dev(disk); |
weiping zhang | 3a92168 | 2017-10-31 18:38:59 +0800 | [diff] [blame] | 612 | int ret; |
| 613 | |
Christoph Hellwig | 8ddcd65 | 2017-11-02 21:29:53 +0300 | [diff] [blame] | 614 | /* Register BDI before referencing it from bdev */ |
Christoph Hellwig | 3c5d202 | 2020-05-04 14:47:59 +0200 | [diff] [blame] | 615 | dev->devt = devt; |
| 616 | ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt)); |
weiping zhang | 3a92168 | 2017-10-31 18:38:59 +0800 | [diff] [blame] | 617 | WARN_ON(ret); |
Christoph Hellwig | 3c5d202 | 2020-05-04 14:47:59 +0200 | [diff] [blame] | 618 | bdi_set_owner(bdi, dev); |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 619 | bdev_add(disk->part0, devt); |
Christoph Hellwig | 8ddcd65 | 2017-11-02 21:29:53 +0300 | [diff] [blame] | 620 | } |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 621 | register_disk(parent, disk, groups); |
Mike Snitzer | fa70d2e | 2018-01-08 22:01:13 -0500 | [diff] [blame] | 622 | if (register_queue) |
| 623 | blk_register_queue(disk); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 624 | |
Tejun Heo | 523e1d3 | 2011-10-19 14:31:07 +0200 | [diff] [blame] | 625 | /* |
| 626 | * Take an extra ref on queue which will be put on disk_release() |
| 627 | * so that it sticks around as long as @disk is there. |
| 628 | */ |
Tejun Heo | 09ac46c | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 629 | WARN_ON_ONCE(!blk_get_queue(disk->queue)); |
Tejun Heo | 523e1d3 | 2011-10-19 14:31:07 +0200 | [diff] [blame] | 630 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 631 | disk_add_events(disk); |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 632 | blk_integrity_add(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
Mike Snitzer | fa70d2e | 2018-01-08 22:01:13 -0500 | [diff] [blame] | 634 | |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 635 | void device_add_disk(struct device *parent, struct gendisk *disk, |
| 636 | const struct attribute_group **groups) |
| 637 | |
Mike Snitzer | fa70d2e | 2018-01-08 22:01:13 -0500 | [diff] [blame] | 638 | { |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 639 | __device_add_disk(parent, disk, groups, true); |
Mike Snitzer | fa70d2e | 2018-01-08 22:01:13 -0500 | [diff] [blame] | 640 | } |
Dan Williams | e63a46b | 2016-06-15 18:17:27 -0700 | [diff] [blame] | 641 | EXPORT_SYMBOL(device_add_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Mike Snitzer | fa70d2e | 2018-01-08 22:01:13 -0500 | [diff] [blame] | 643 | void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk) |
| 644 | { |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 645 | __device_add_disk(parent, disk, NULL, false); |
Mike Snitzer | fa70d2e | 2018-01-08 22:01:13 -0500 | [diff] [blame] | 646 | } |
| 647 | EXPORT_SYMBOL(device_add_disk_no_queue_reg); |
| 648 | |
Luis Chamberlain | b5bd357 | 2020-06-19 20:47:23 +0000 | [diff] [blame] | 649 | /** |
| 650 | * del_gendisk - remove the gendisk |
| 651 | * @disk: the struct gendisk to remove |
| 652 | * |
| 653 | * Removes the gendisk and all its associated resources. This deletes the |
| 654 | * partitions associated with the gendisk, and unregisters the associated |
| 655 | * request_queue. |
| 656 | * |
| 657 | * This is the counter to the respective __device_add_disk() call. |
| 658 | * |
| 659 | * The final removal of the struct gendisk happens when its refcount reaches 0 |
| 660 | * with put_disk(), which should be called after del_gendisk(), if |
| 661 | * __device_add_disk() was used. |
Luis Chamberlain | e8c7d14 | 2020-06-19 20:47:25 +0000 | [diff] [blame] | 662 | * |
| 663 | * Drivers exist which depend on the release of the gendisk to be synchronous, |
| 664 | * it should not be deferred. |
| 665 | * |
| 666 | * Context: can sleep |
Luis Chamberlain | b5bd357 | 2020-06-19 20:47:23 +0000 | [diff] [blame] | 667 | */ |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 668 | void del_gendisk(struct gendisk *disk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | { |
Luis Chamberlain | e8c7d14 | 2020-06-19 20:47:25 +0000 | [diff] [blame] | 670 | might_sleep(); |
| 671 | |
Christoph Hellwig | 6b3ba97 | 2020-10-29 15:58:24 +0100 | [diff] [blame] | 672 | if (WARN_ON_ONCE(!disk->queue)) |
| 673 | return; |
| 674 | |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 675 | blk_integrity_del(disk); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 676 | disk_del_events(disk); |
| 677 | |
Jan Kara | 56c0908 | 2018-02-26 13:01:41 +0100 | [diff] [blame] | 678 | /* |
| 679 | * Block lookups of the disk until all bdevs are unhashed and the |
| 680 | * disk is marked as dead (GENHD_FL_UP cleared). |
| 681 | */ |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 682 | down_write(&bdev_lookup_sem); |
Christoph Hellwig | d3c4a43 | 2021-04-06 08:22:55 +0200 | [diff] [blame^] | 683 | blk_drop_partitions(disk); |
Christoph Hellwig | 4561183 | 2021-04-06 08:22:53 +0200 | [diff] [blame] | 684 | fsync_bdev(disk->part0); |
| 685 | __invalidate_device(disk->part0, true); |
| 686 | |
| 687 | /* |
| 688 | * Unhash the bdev inode for this device so that it can't be looked |
| 689 | * up any more even if openers still hold references to it. |
| 690 | */ |
| 691 | remove_inode_hash(disk->part0->bd_inode); |
| 692 | |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 693 | set_capacity(disk, 0); |
| 694 | disk->flags &= ~GENHD_FL_UP; |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 695 | up_write(&bdev_lookup_sem); |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 696 | |
Christoph Hellwig | 6b3ba97 | 2020-10-29 15:58:24 +0100 | [diff] [blame] | 697 | if (!(disk->flags & GENHD_FL_HIDDEN)) { |
Christoph Hellwig | 8ddcd65 | 2017-11-02 21:29:53 +0300 | [diff] [blame] | 698 | sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi"); |
Christoph Hellwig | 6b3ba97 | 2020-10-29 15:58:24 +0100 | [diff] [blame] | 699 | |
Jan Kara | 90f16fd | 2017-03-08 17:48:33 +0100 | [diff] [blame] | 700 | /* |
| 701 | * Unregister bdi before releasing device numbers (as they can |
| 702 | * get reused and we'd get clashes in sysfs). |
| 703 | */ |
Christoph Hellwig | 6b3ba97 | 2020-10-29 15:58:24 +0100 | [diff] [blame] | 704 | bdi_unregister(disk->queue->backing_dev_info); |
Jan Kara | 90f16fd | 2017-03-08 17:48:33 +0100 | [diff] [blame] | 705 | } |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 706 | |
Christoph Hellwig | 6b3ba97 | 2020-10-29 15:58:24 +0100 | [diff] [blame] | 707 | blk_unregister_queue(disk); |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 708 | |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 709 | kobject_put(disk->part0->bd_holder_dir); |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 710 | kobject_put(disk->slave_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Christoph Hellwig | 8446fe9 | 2020-11-24 09:36:54 +0100 | [diff] [blame] | 712 | part_stat_set_all(disk->part0, 0); |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 713 | disk->part0->bd_stamp = 0; |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 714 | if (!sysfs_deprecated) |
| 715 | sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk))); |
Ming Lei | 25e823c | 2013-02-22 16:34:13 -0800 | [diff] [blame] | 716 | pm_runtime_set_memalloc_noio(disk_to_dev(disk), false); |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 717 | device_del(disk_to_dev(disk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | } |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 719 | EXPORT_SYMBOL(del_gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Vishal Verma | 99e6608 | 2016-01-09 08:36:51 -0800 | [diff] [blame] | 721 | /* sysfs access to bad-blocks list. */ |
| 722 | static ssize_t disk_badblocks_show(struct device *dev, |
| 723 | struct device_attribute *attr, |
| 724 | char *page) |
| 725 | { |
| 726 | struct gendisk *disk = dev_to_disk(dev); |
| 727 | |
| 728 | if (!disk->bb) |
| 729 | return sprintf(page, "\n"); |
| 730 | |
| 731 | return badblocks_show(disk->bb, page, 0); |
| 732 | } |
| 733 | |
| 734 | static ssize_t disk_badblocks_store(struct device *dev, |
| 735 | struct device_attribute *attr, |
| 736 | const char *page, size_t len) |
| 737 | { |
| 738 | struct gendisk *disk = dev_to_disk(dev); |
| 739 | |
| 740 | if (!disk->bb) |
| 741 | return -ENXIO; |
| 742 | |
| 743 | return badblocks_store(disk->bb, page, len, 0); |
| 744 | } |
| 745 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 746 | void blk_request_module(dev_t devt) |
Christoph Hellwig | bd8eff3 | 2020-10-29 15:58:27 +0100 | [diff] [blame] | 747 | { |
Christoph Hellwig | a160c61 | 2020-10-29 15:58:28 +0100 | [diff] [blame] | 748 | unsigned int major = MAJOR(devt); |
| 749 | struct blk_major_name **n; |
| 750 | |
| 751 | mutex_lock(&major_names_lock); |
| 752 | for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) { |
| 753 | if ((*n)->major == major && (*n)->probe) { |
| 754 | (*n)->probe(devt); |
| 755 | mutex_unlock(&major_names_lock); |
| 756 | return; |
| 757 | } |
| 758 | } |
| 759 | mutex_unlock(&major_names_lock); |
| 760 | |
Christoph Hellwig | bd8eff3 | 2020-10-29 15:58:27 +0100 | [diff] [blame] | 761 | if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0) |
| 762 | /* Make old-style 2.4 aliases work */ |
| 763 | request_module("block-major-%d", MAJOR(devt)); |
| 764 | } |
| 765 | |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 766 | /** |
| 767 | * bdget_disk - do bdget() by gendisk and partition number |
| 768 | * @disk: gendisk of interest |
| 769 | * @partno: partition number |
| 770 | * |
| 771 | * Find partition @partno from @disk, do bdget() on it. |
| 772 | * |
| 773 | * CONTEXT: |
| 774 | * Don't care. |
| 775 | * |
| 776 | * RETURNS: |
| 777 | * Resulting block_device on success, NULL on failure. |
| 778 | */ |
Harvey Harrison | aeb3d3a | 2008-08-28 09:27:42 +0200 | [diff] [blame] | 779 | struct block_device *bdget_disk(struct gendisk *disk, int partno) |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 780 | { |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 781 | struct block_device *bdev = NULL; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 782 | |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 783 | rcu_read_lock(); |
Christoph Hellwig | a33df75 | 2021-01-24 11:02:41 +0100 | [diff] [blame] | 784 | bdev = xa_load(&disk->part_tbl, partno); |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 785 | if (bdev && !bdgrab(bdev)) |
| 786 | bdev = NULL; |
| 787 | rcu_read_unlock(); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 788 | |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 789 | return bdev; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 790 | } |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 791 | |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 792 | /* |
| 793 | * print a full list of all partitions - intended for places where the root |
| 794 | * filesystem can't be mounted and thus to give the victim some idea of what |
| 795 | * went wrong |
| 796 | */ |
| 797 | void __init printk_all_partitions(void) |
| 798 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 799 | struct class_dev_iter iter; |
| 800 | struct device *dev; |
| 801 | |
| 802 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); |
| 803 | while ((dev = class_dev_iter_next(&iter))) { |
| 804 | struct gendisk *disk = dev_to_disk(dev); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 805 | struct disk_part_iter piter; |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 806 | struct block_device *part; |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 807 | char name_buf[BDEVNAME_SIZE]; |
| 808 | char devt_buf[BDEVT_SIZE]; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 809 | |
| 810 | /* |
| 811 | * Don't show empty devices or things that have been |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 812 | * suppressed |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 813 | */ |
| 814 | if (get_capacity(disk) == 0 || |
| 815 | (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)) |
| 816 | continue; |
| 817 | |
| 818 | /* |
| 819 | * Note, unlike /proc/partitions, I am showing the |
| 820 | * numbers in hex - the same format as the root= |
| 821 | * option takes. |
| 822 | */ |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 823 | disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0); |
| 824 | while ((part = disk_part_iter_next(&piter))) { |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 825 | bool is_part0 = part == disk->part0; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 826 | |
Will Drewry | b5af921 | 2010-08-31 15:47:07 -0500 | [diff] [blame] | 827 | printk("%s%s %10llu %s %s", is_part0 ? "" : " ", |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 828 | bdevt_str(part->bd_dev, devt_buf), |
| 829 | bdev_nr_sectors(part) >> 1, |
| 830 | disk_name(disk, part->bd_partno, name_buf), |
| 831 | part->bd_meta_info ? |
| 832 | part->bd_meta_info->uuid : ""); |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 833 | if (is_part0) { |
Dan Williams | 52c44d9 | 2016-06-15 19:43:07 -0700 | [diff] [blame] | 834 | if (dev->parent && dev->parent->driver) |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 835 | printk(" driver: %s\n", |
Dan Williams | 52c44d9 | 2016-06-15 19:43:07 -0700 | [diff] [blame] | 836 | dev->parent->driver->name); |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 837 | else |
| 838 | printk(" (driver?)\n"); |
| 839 | } else |
| 840 | printk("\n"); |
| 841 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 842 | disk_part_iter_exit(&piter); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 843 | } |
| 844 | class_dev_iter_exit(&iter); |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | #ifdef CONFIG_PROC_FS |
| 848 | /* iterator */ |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 849 | static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos) |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 850 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 851 | loff_t skip = *pos; |
| 852 | struct class_dev_iter *iter; |
| 853 | struct device *dev; |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 854 | |
Harvey Harrison | aeb3d3a | 2008-08-28 09:27:42 +0200 | [diff] [blame] | 855 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 856 | if (!iter) |
| 857 | return ERR_PTR(-ENOMEM); |
| 858 | |
| 859 | seqf->private = iter; |
| 860 | class_dev_iter_init(iter, &block_class, NULL, &disk_type); |
| 861 | do { |
| 862 | dev = class_dev_iter_next(iter); |
| 863 | if (!dev) |
| 864 | return NULL; |
| 865 | } while (skip--); |
| 866 | |
| 867 | return dev_to_disk(dev); |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 868 | } |
| 869 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 870 | static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 872 | struct device *dev; |
Greg Kroah-Hartman | 66c64af | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 873 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 874 | (*pos)++; |
| 875 | dev = class_dev_iter_next(seqf->private); |
Tejun Heo | 2ac3cee | 2008-09-03 08:53:37 +0200 | [diff] [blame] | 876 | if (dev) |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 877 | return dev_to_disk(dev); |
Tejun Heo | 2ac3cee | 2008-09-03 08:53:37 +0200 | [diff] [blame] | 878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return NULL; |
| 880 | } |
| 881 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 882 | static void disk_seqf_stop(struct seq_file *seqf, void *v) |
Greg Kroah-Hartman | 27f3025 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 883 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 884 | struct class_dev_iter *iter = seqf->private; |
Greg Kroah-Hartman | 27f3025 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 885 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 886 | /* stop is called even after start failed :-( */ |
| 887 | if (iter) { |
| 888 | class_dev_iter_exit(iter); |
| 889 | kfree(iter); |
Vegard Nossum | 77da160 | 2016-07-29 10:40:31 +0200 | [diff] [blame] | 890 | seqf->private = NULL; |
Kay Sievers | 5c0ef6d | 2008-08-16 14:30:30 +0200 | [diff] [blame] | 891 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 894 | static void *show_partition_start(struct seq_file *seqf, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
Jianpeng Ma | 0676806 | 2012-08-03 10:42:00 +0200 | [diff] [blame] | 896 | void *p; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 897 | |
| 898 | p = disk_seqf_start(seqf, pos); |
Yang Zhang | b9f985b | 2010-12-17 08:58:36 +0100 | [diff] [blame] | 899 | if (!IS_ERR_OR_NULL(p) && !*pos) |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 900 | seq_puts(seqf, "major minor #blocks name\n\n"); |
| 901 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 904 | static int show_partition(struct seq_file *seqf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
| 906 | struct gendisk *sgp = v; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 907 | struct disk_part_iter piter; |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 908 | struct block_device *part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | char buf[BDEVNAME_SIZE]; |
| 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | /* Don't show non-partitionable removeable devices or empty devices */ |
Tejun Heo | d27769e | 2011-08-23 20:01:04 +0200 | [diff] [blame] | 912 | if (!get_capacity(sgp) || (!disk_max_parts(sgp) && |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 913 | (sgp->flags & GENHD_FL_REMOVABLE))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | return 0; |
| 915 | if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO) |
| 916 | return 0; |
| 917 | |
| 918 | /* show the full disk and all non-0 size partitions of it */ |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 919 | disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 920 | while ((part = disk_part_iter_next(&piter))) |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 921 | seq_printf(seqf, "%4d %7d %10llu %s\n", |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 922 | MAJOR(part->bd_dev), MINOR(part->bd_dev), |
| 923 | bdev_nr_sectors(part) >> 1, |
| 924 | disk_name(sgp, part->bd_partno, buf)); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 925 | disk_part_iter_exit(&piter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
Alexey Dobriyan | f500975 | 2008-10-04 23:53:21 +0400 | [diff] [blame] | 930 | static const struct seq_operations partitions_op = { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 931 | .start = show_partition_start, |
| 932 | .next = disk_seqf_next, |
| 933 | .stop = disk_seqf_stop, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 934 | .show = show_partition |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | }; |
| 936 | #endif |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | static int __init genhd_device_init(void) |
| 939 | { |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 940 | int error; |
| 941 | |
| 942 | block_class.dev_kobj = sysfs_dev_block_kobj; |
| 943 | error = class_register(&block_class); |
Roland McGrath | ee27a55 | 2008-03-11 17:13:15 -0700 | [diff] [blame] | 944 | if (unlikely(error)) |
| 945 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | blk_dev_init(); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 947 | |
Zhang, Yanmin | 561ec68 | 2008-11-14 08:26:30 +0100 | [diff] [blame] | 948 | register_blkdev(BLOCK_EXT_MAJOR, "blkext"); |
| 949 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 950 | /* create top-level block dir */ |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 951 | if (!sysfs_deprecated) |
| 952 | block_depr = kobject_create_and_add("block", NULL); |
Greg Kroah-Hartman | 830d3cf | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 953 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | subsys_initcall(genhd_device_init); |
| 957 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 958 | static ssize_t disk_range_show(struct device *dev, |
| 959 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 961 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 963 | return sprintf(buf, "%d\n", disk->minors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 966 | static ssize_t disk_ext_range_show(struct device *dev, |
| 967 | struct device_attribute *attr, char *buf) |
| 968 | { |
| 969 | struct gendisk *disk = dev_to_disk(dev); |
| 970 | |
Tejun Heo | b5d0b9d | 2008-09-03 09:06:42 +0200 | [diff] [blame] | 971 | return sprintf(buf, "%d\n", disk_max_parts(disk)); |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 972 | } |
| 973 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 974 | static ssize_t disk_removable_show(struct device *dev, |
| 975 | struct device_attribute *attr, char *buf) |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 976 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 977 | struct gendisk *disk = dev_to_disk(dev); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 978 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 979 | return sprintf(buf, "%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 981 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
Christoph Hellwig | 8ddcd65 | 2017-11-02 21:29:53 +0300 | [diff] [blame] | 983 | static ssize_t disk_hidden_show(struct device *dev, |
| 984 | struct device_attribute *attr, char *buf) |
| 985 | { |
| 986 | struct gendisk *disk = dev_to_disk(dev); |
| 987 | |
| 988 | return sprintf(buf, "%d\n", |
| 989 | (disk->flags & GENHD_FL_HIDDEN ? 1 : 0)); |
| 990 | } |
| 991 | |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 992 | static ssize_t disk_ro_show(struct device *dev, |
| 993 | struct device_attribute *attr, char *buf) |
| 994 | { |
| 995 | struct gendisk *disk = dev_to_disk(dev); |
| 996 | |
Tejun Heo | b7db995 | 2008-08-25 19:56:10 +0900 | [diff] [blame] | 997 | return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0); |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 998 | } |
| 999 | |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1000 | ssize_t part_size_show(struct device *dev, |
| 1001 | struct device_attribute *attr, char *buf) |
| 1002 | { |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1003 | return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev))); |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | ssize_t part_stat_show(struct device *dev, |
| 1007 | struct device_attribute *attr, char *buf) |
| 1008 | { |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1009 | struct block_device *bdev = dev_to_bdev(dev); |
| 1010 | struct request_queue *q = bdev->bd_disk->queue; |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 1011 | struct disk_stats stat; |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1012 | unsigned int inflight; |
| 1013 | |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1014 | part_stat_read_all(bdev, &stat); |
Christoph Hellwig | b2f609e | 2020-05-13 12:49:33 +0200 | [diff] [blame] | 1015 | if (queue_is_mq(q)) |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1016 | inflight = blk_mq_in_flight(q, bdev); |
Christoph Hellwig | b2f609e | 2020-05-13 12:49:33 +0200 | [diff] [blame] | 1017 | else |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1018 | inflight = part_in_flight(bdev); |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 1019 | |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1020 | return sprintf(buf, |
| 1021 | "%8lu %8lu %8llu %8u " |
| 1022 | "%8lu %8lu %8llu %8u " |
| 1023 | "%8u %8u %8u " |
| 1024 | "%8lu %8lu %8llu %8u " |
| 1025 | "%8lu %8u" |
| 1026 | "\n", |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 1027 | stat.ios[STAT_READ], |
| 1028 | stat.merges[STAT_READ], |
| 1029 | (unsigned long long)stat.sectors[STAT_READ], |
| 1030 | (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC), |
| 1031 | stat.ios[STAT_WRITE], |
| 1032 | stat.merges[STAT_WRITE], |
| 1033 | (unsigned long long)stat.sectors[STAT_WRITE], |
| 1034 | (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC), |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1035 | inflight, |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 1036 | jiffies_to_msecs(stat.io_ticks), |
Konstantin Khlebnikov | 8cd5b8f | 2020-03-25 16:07:08 +0300 | [diff] [blame] | 1037 | (unsigned int)div_u64(stat.nsecs[STAT_READ] + |
| 1038 | stat.nsecs[STAT_WRITE] + |
| 1039 | stat.nsecs[STAT_DISCARD] + |
| 1040 | stat.nsecs[STAT_FLUSH], |
| 1041 | NSEC_PER_MSEC), |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 1042 | stat.ios[STAT_DISCARD], |
| 1043 | stat.merges[STAT_DISCARD], |
| 1044 | (unsigned long long)stat.sectors[STAT_DISCARD], |
| 1045 | (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC), |
| 1046 | stat.ios[STAT_FLUSH], |
| 1047 | (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC)); |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr, |
| 1051 | char *buf) |
| 1052 | { |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1053 | struct block_device *bdev = dev_to_bdev(dev); |
| 1054 | struct request_queue *q = bdev->bd_disk->queue; |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1055 | unsigned int inflight[2]; |
| 1056 | |
Christoph Hellwig | b2f609e | 2020-05-13 12:49:33 +0200 | [diff] [blame] | 1057 | if (queue_is_mq(q)) |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1058 | blk_mq_in_flight_rw(q, bdev, inflight); |
Christoph Hellwig | b2f609e | 2020-05-13 12:49:33 +0200 | [diff] [blame] | 1059 | else |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1060 | part_in_flight_rw(bdev, inflight); |
Christoph Hellwig | b2f609e | 2020-05-13 12:49:33 +0200 | [diff] [blame] | 1061 | |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1062 | return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]); |
| 1063 | } |
| 1064 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1065 | static ssize_t disk_capability_show(struct device *dev, |
| 1066 | struct device_attribute *attr, char *buf) |
Kristen Carlson Accardi | 86ce18d | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 1067 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1068 | struct gendisk *disk = dev_to_disk(dev); |
| 1069 | |
| 1070 | return sprintf(buf, "%x\n", disk->flags); |
Kristen Carlson Accardi | 86ce18d | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 1071 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1072 | |
Martin K. Petersen | c72758f | 2009-05-22 17:17:53 -0400 | [diff] [blame] | 1073 | static ssize_t disk_alignment_offset_show(struct device *dev, |
| 1074 | struct device_attribute *attr, |
| 1075 | char *buf) |
| 1076 | { |
| 1077 | struct gendisk *disk = dev_to_disk(dev); |
| 1078 | |
| 1079 | return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue)); |
| 1080 | } |
| 1081 | |
Martin K. Petersen | 86b3728 | 2009-11-10 11:50:21 +0100 | [diff] [blame] | 1082 | static ssize_t disk_discard_alignment_show(struct device *dev, |
| 1083 | struct device_attribute *attr, |
| 1084 | char *buf) |
| 1085 | { |
| 1086 | struct gendisk *disk = dev_to_disk(dev); |
| 1087 | |
Martin K. Petersen | dd3d145 | 2010-01-11 03:21:48 -0500 | [diff] [blame] | 1088 | return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue)); |
Martin K. Petersen | 86b3728 | 2009-11-10 11:50:21 +0100 | [diff] [blame] | 1089 | } |
| 1090 | |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 1091 | static DEVICE_ATTR(range, 0444, disk_range_show, NULL); |
| 1092 | static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL); |
| 1093 | static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL); |
| 1094 | static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL); |
| 1095 | static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL); |
| 1096 | static DEVICE_ATTR(size, 0444, part_size_show, NULL); |
| 1097 | static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL); |
| 1098 | static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL); |
| 1099 | static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL); |
| 1100 | static DEVICE_ATTR(stat, 0444, part_stat_show, NULL); |
| 1101 | static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL); |
| 1102 | static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store); |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1103 | |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 1104 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1105 | ssize_t part_fail_show(struct device *dev, |
| 1106 | struct device_attribute *attr, char *buf) |
| 1107 | { |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1108 | return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail); |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | ssize_t part_fail_store(struct device *dev, |
| 1112 | struct device_attribute *attr, |
| 1113 | const char *buf, size_t count) |
| 1114 | { |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1115 | int i; |
| 1116 | |
| 1117 | if (count > 0 && sscanf(buf, "%d", &i) > 0) |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1118 | dev_to_bdev(dev)->bd_make_it_fail = i; |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1119 | |
| 1120 | return count; |
| 1121 | } |
| 1122 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1123 | static struct device_attribute dev_attr_fail = |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 1124 | __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store); |
Christoph Hellwig | 3ad5cee | 2020-03-24 08:25:13 +0100 | [diff] [blame] | 1125 | #endif /* CONFIG_FAIL_MAKE_REQUEST */ |
| 1126 | |
Jens Axboe | 581d4e2 | 2008-09-14 05:56:33 -0700 | [diff] [blame] | 1127 | #ifdef CONFIG_FAIL_IO_TIMEOUT |
| 1128 | static struct device_attribute dev_attr_fail_timeout = |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 1129 | __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store); |
Jens Axboe | 581d4e2 | 2008-09-14 05:56:33 -0700 | [diff] [blame] | 1130 | #endif |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1131 | |
| 1132 | static struct attribute *disk_attrs[] = { |
| 1133 | &dev_attr_range.attr, |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 1134 | &dev_attr_ext_range.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1135 | &dev_attr_removable.attr, |
Christoph Hellwig | 8ddcd65 | 2017-11-02 21:29:53 +0300 | [diff] [blame] | 1136 | &dev_attr_hidden.attr, |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 1137 | &dev_attr_ro.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1138 | &dev_attr_size.attr, |
Martin K. Petersen | c72758f | 2009-05-22 17:17:53 -0400 | [diff] [blame] | 1139 | &dev_attr_alignment_offset.attr, |
Martin K. Petersen | 86b3728 | 2009-11-10 11:50:21 +0100 | [diff] [blame] | 1140 | &dev_attr_discard_alignment.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1141 | &dev_attr_capability.attr, |
| 1142 | &dev_attr_stat.attr, |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 1143 | &dev_attr_inflight.attr, |
Vishal Verma | 99e6608 | 2016-01-09 08:36:51 -0800 | [diff] [blame] | 1144 | &dev_attr_badblocks.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1145 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 1146 | &dev_attr_fail.attr, |
| 1147 | #endif |
Jens Axboe | 581d4e2 | 2008-09-14 05:56:33 -0700 | [diff] [blame] | 1148 | #ifdef CONFIG_FAIL_IO_TIMEOUT |
| 1149 | &dev_attr_fail_timeout.attr, |
| 1150 | #endif |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1151 | NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | }; |
| 1153 | |
Dan Williams | 9438b3e0 | 2017-04-27 14:46:26 -0700 | [diff] [blame] | 1154 | static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n) |
| 1155 | { |
| 1156 | struct device *dev = container_of(kobj, typeof(*dev), kobj); |
| 1157 | struct gendisk *disk = dev_to_disk(dev); |
| 1158 | |
| 1159 | if (a == &dev_attr_badblocks.attr && !disk->bb) |
| 1160 | return 0; |
| 1161 | return a->mode; |
| 1162 | } |
| 1163 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1164 | static struct attribute_group disk_attr_group = { |
| 1165 | .attrs = disk_attrs, |
Dan Williams | 9438b3e0 | 2017-04-27 14:46:26 -0700 | [diff] [blame] | 1166 | .is_visible = disk_visible, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1167 | }; |
| 1168 | |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 1169 | static const struct attribute_group *disk_attr_groups[] = { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1170 | &disk_attr_group, |
| 1171 | NULL |
| 1172 | }; |
| 1173 | |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 1174 | /** |
Luis Chamberlain | b5bd357 | 2020-06-19 20:47:23 +0000 | [diff] [blame] | 1175 | * disk_release - releases all allocated resources of the gendisk |
| 1176 | * @dev: the device representing this disk |
| 1177 | * |
| 1178 | * This function releases all allocated resources of the gendisk. |
| 1179 | * |
Luis Chamberlain | b5bd357 | 2020-06-19 20:47:23 +0000 | [diff] [blame] | 1180 | * Drivers which used __device_add_disk() have a gendisk with a request_queue |
| 1181 | * assigned. Since the request_queue sits on top of the gendisk for these |
| 1182 | * drivers we also call blk_put_queue() for them, and we expect the |
| 1183 | * request_queue refcount to reach 0 at this point, and so the request_queue |
| 1184 | * will also be freed prior to the disk. |
Luis Chamberlain | e8c7d14 | 2020-06-19 20:47:25 +0000 | [diff] [blame] | 1185 | * |
| 1186 | * Context: can sleep |
Luis Chamberlain | b5bd357 | 2020-06-19 20:47:23 +0000 | [diff] [blame] | 1187 | */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1188 | static void disk_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1190 | struct gendisk *disk = dev_to_disk(dev); |
| 1191 | |
Luis Chamberlain | e8c7d14 | 2020-06-19 20:47:25 +0000 | [diff] [blame] | 1192 | might_sleep(); |
| 1193 | |
Keith Busch | 2da7809 | 2014-08-26 09:05:36 -0600 | [diff] [blame] | 1194 | blk_free_devt(dev->devt); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1195 | disk_release_events(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | kfree(disk->random); |
Christoph Hellwig | a33df75 | 2021-01-24 11:02:41 +0100 | [diff] [blame] | 1197 | xa_destroy(&disk->part_tbl); |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 1198 | bdput(disk->part0); |
Tejun Heo | 523e1d3 | 2011-10-19 14:31:07 +0200 | [diff] [blame] | 1199 | if (disk->queue) |
| 1200 | blk_put_queue(disk->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | kfree(disk); |
| 1202 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1203 | struct class block_class = { |
| 1204 | .name = "block", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | }; |
| 1206 | |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 1207 | static char *block_devnode(struct device *dev, umode_t *mode, |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 1208 | kuid_t *uid, kgid_t *gid) |
Kay Sievers | b03f38b | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1209 | { |
| 1210 | struct gendisk *disk = dev_to_disk(dev); |
| 1211 | |
Christoph Hellwig | 348e114 | 2020-03-27 09:07:17 +0100 | [diff] [blame] | 1212 | if (disk->fops->devnode) |
| 1213 | return disk->fops->devnode(disk, mode); |
Kay Sievers | b03f38b | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1214 | return NULL; |
| 1215 | } |
| 1216 | |
Boris Burkov | ef45fe4 | 2020-06-01 13:12:05 -0700 | [diff] [blame] | 1217 | const struct device_type disk_type = { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1218 | .name = "disk", |
| 1219 | .groups = disk_attr_groups, |
| 1220 | .release = disk_release, |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1221 | .devnode = block_devnode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | }; |
| 1223 | |
Randy Dunlap | a6e2ba8 | 2008-05-23 09:44:11 -0700 | [diff] [blame] | 1224 | #ifdef CONFIG_PROC_FS |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1225 | /* |
| 1226 | * aggregate disk stat collector. Uses the same stats that the sysfs |
| 1227 | * entries do, above, but makes them available through one seq_file. |
| 1228 | * |
| 1229 | * The output looks suspiciously like /proc/partitions with a bunch of |
| 1230 | * extra fields. |
| 1231 | */ |
| 1232 | static int diskstats_show(struct seq_file *seqf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | { |
| 1234 | struct gendisk *gp = v; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1235 | struct disk_part_iter piter; |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 1236 | struct block_device *hd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | char buf[BDEVNAME_SIZE]; |
Mikulas Patocka | e016b78 | 2018-12-06 11:41:21 -0500 | [diff] [blame] | 1238 | unsigned int inflight; |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 1239 | struct disk_stats stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
| 1241 | /* |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 1242 | if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next) |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1243 | seq_puts(seqf, "major minor name" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | " rio rmerge rsect ruse wio wmerge " |
| 1245 | "wsect wuse running use aveq" |
| 1246 | "\n\n"); |
| 1247 | */ |
Wanlong Gao | 9f5e486 | 2011-06-13 10:45:43 +0200 | [diff] [blame] | 1248 | |
Tejun Heo | 71982a4 | 2009-04-17 08:34:48 +0200 | [diff] [blame] | 1249 | disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1250 | while ((hd = disk_part_iter_next(&piter))) { |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1251 | part_stat_read_all(hd, &stat); |
Christoph Hellwig | b2f609e | 2020-05-13 12:49:33 +0200 | [diff] [blame] | 1252 | if (queue_is_mq(gp->queue)) |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 1253 | inflight = blk_mq_in_flight(gp->queue, hd); |
Christoph Hellwig | b2f609e | 2020-05-13 12:49:33 +0200 | [diff] [blame] | 1254 | else |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 1255 | inflight = part_in_flight(hd); |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 1256 | |
Michael Callahan | bdca3c8 | 2018-07-18 04:47:40 -0700 | [diff] [blame] | 1257 | seq_printf(seqf, "%4d %7d %s " |
| 1258 | "%lu %lu %lu %u " |
| 1259 | "%lu %lu %lu %u " |
| 1260 | "%u %u %u " |
Konstantin Khlebnikov | b686631 | 2019-11-21 13:40:26 +0300 | [diff] [blame] | 1261 | "%lu %lu %lu %u " |
| 1262 | "%lu %u" |
| 1263 | "\n", |
Christoph Hellwig | ad1eaa5 | 2020-11-24 09:52:59 +0100 | [diff] [blame] | 1264 | MAJOR(hd->bd_dev), MINOR(hd->bd_dev), |
| 1265 | disk_name(gp, hd->bd_partno, buf), |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 1266 | stat.ios[STAT_READ], |
| 1267 | stat.merges[STAT_READ], |
| 1268 | stat.sectors[STAT_READ], |
| 1269 | (unsigned int)div_u64(stat.nsecs[STAT_READ], |
| 1270 | NSEC_PER_MSEC), |
| 1271 | stat.ios[STAT_WRITE], |
| 1272 | stat.merges[STAT_WRITE], |
| 1273 | stat.sectors[STAT_WRITE], |
| 1274 | (unsigned int)div_u64(stat.nsecs[STAT_WRITE], |
| 1275 | NSEC_PER_MSEC), |
Mikulas Patocka | e016b78 | 2018-12-06 11:41:21 -0500 | [diff] [blame] | 1276 | inflight, |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 1277 | jiffies_to_msecs(stat.io_ticks), |
Konstantin Khlebnikov | 8cd5b8f | 2020-03-25 16:07:08 +0300 | [diff] [blame] | 1278 | (unsigned int)div_u64(stat.nsecs[STAT_READ] + |
| 1279 | stat.nsecs[STAT_WRITE] + |
| 1280 | stat.nsecs[STAT_DISCARD] + |
| 1281 | stat.nsecs[STAT_FLUSH], |
| 1282 | NSEC_PER_MSEC), |
Konstantin Khlebnikov | ea18e0f | 2020-03-25 16:07:06 +0300 | [diff] [blame] | 1283 | stat.ios[STAT_DISCARD], |
| 1284 | stat.merges[STAT_DISCARD], |
| 1285 | stat.sectors[STAT_DISCARD], |
| 1286 | (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], |
| 1287 | NSEC_PER_MSEC), |
| 1288 | stat.ios[STAT_FLUSH], |
| 1289 | (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], |
| 1290 | NSEC_PER_MSEC) |
Jerome Marchand | 28f39d5 | 2008-02-08 11:04:56 +0100 | [diff] [blame] | 1291 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1293 | disk_part_iter_exit(&piter); |
Wanlong Gao | 9f5e486 | 2011-06-13 10:45:43 +0200 | [diff] [blame] | 1294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | return 0; |
| 1296 | } |
| 1297 | |
Alexey Dobriyan | 31d85ab | 2008-10-06 12:55:38 +0400 | [diff] [blame] | 1298 | static const struct seq_operations diskstats_op = { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1299 | .start = disk_seqf_start, |
| 1300 | .next = disk_seqf_next, |
| 1301 | .stop = disk_seqf_stop, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | .show = diskstats_show |
| 1303 | }; |
Alexey Dobriyan | f500975 | 2008-10-04 23:53:21 +0400 | [diff] [blame] | 1304 | |
| 1305 | static int __init proc_genhd_init(void) |
| 1306 | { |
Christoph Hellwig | fddda2b | 2018-04-13 19:44:18 +0200 | [diff] [blame] | 1307 | proc_create_seq("diskstats", 0, NULL, &diskstats_op); |
| 1308 | proc_create_seq("partitions", 0, NULL, &partitions_op); |
Alexey Dobriyan | f500975 | 2008-10-04 23:53:21 +0400 | [diff] [blame] | 1309 | return 0; |
| 1310 | } |
| 1311 | module_init(proc_genhd_init); |
Randy Dunlap | a6e2ba8 | 2008-05-23 09:44:11 -0700 | [diff] [blame] | 1312 | #endif /* CONFIG_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1314 | dev_t blk_lookup_devt(const char *name, int partno) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1315 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1316 | dev_t devt = MKDEV(0, 0); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1317 | struct class_dev_iter iter; |
| 1318 | struct device *dev; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1319 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1320 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); |
| 1321 | while ((dev = class_dev_iter_next(&iter))) { |
| 1322 | struct gendisk *disk = dev_to_disk(dev); |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1323 | struct block_device *part; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1324 | |
Kay Sievers | 3ada8b7 | 2009-01-06 10:44:43 -0800 | [diff] [blame] | 1325 | if (strcmp(dev_name(dev), name)) |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 1326 | continue; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 1327 | |
Neil Brown | 41b8c85 | 2009-02-18 10:33:59 +0100 | [diff] [blame] | 1328 | if (partno < disk->minors) { |
| 1329 | /* We need to return the right devno, even |
| 1330 | * if the partition doesn't exist yet. |
| 1331 | */ |
| 1332 | devt = MKDEV(MAJOR(dev->devt), |
| 1333 | MINOR(dev->devt) + partno); |
| 1334 | break; |
| 1335 | } |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1336 | part = bdget_disk(disk, partno); |
Tejun Heo | 2bbedcb | 2008-08-29 11:41:51 +0200 | [diff] [blame] | 1337 | if (part) { |
Christoph Hellwig | 0d02129 | 2020-11-27 16:43:51 +0100 | [diff] [blame] | 1338 | devt = part->bd_dev; |
| 1339 | bdput(part); |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 1340 | break; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1341 | } |
Kay Sievers | 5c0ef6d | 2008-08-16 14:30:30 +0200 | [diff] [blame] | 1342 | } |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1343 | class_dev_iter_exit(&iter); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1344 | return devt; |
| 1345 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1346 | |
Byungchul Park | e319e1f | 2017-10-25 17:56:05 +0900 | [diff] [blame] | 1347 | struct gendisk *__alloc_disk_node(int minors, int node_id) |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 1348 | { |
| 1349 | struct gendisk *disk; |
| 1350 | |
Christoph Hellwig | de65b01 | 2017-08-23 19:10:29 +0200 | [diff] [blame] | 1351 | if (minors > DISK_MAX_PARTS) { |
| 1352 | printk(KERN_ERR |
Randy Dunlap | 7fb5262 | 2017-11-18 17:43:38 -0800 | [diff] [blame] | 1353 | "block: can't allocate more than %d partitions\n", |
Christoph Hellwig | de65b01 | 2017-08-23 19:10:29 +0200 | [diff] [blame] | 1354 | DISK_MAX_PARTS); |
| 1355 | minors = DISK_MAX_PARTS; |
| 1356 | } |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 1357 | |
Joe Perches | c1b511e | 2013-08-29 15:21:42 -0700 | [diff] [blame] | 1358 | disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id); |
Christoph Hellwig | f93af2a | 2020-08-31 20:02:37 +0200 | [diff] [blame] | 1359 | if (!disk) |
| 1360 | return NULL; |
Jens Axboe | 6c23a96 | 2011-01-07 08:43:37 +0100 | [diff] [blame] | 1361 | |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 1362 | disk->part0 = bdev_alloc(disk, 0); |
| 1363 | if (!disk->part0) |
Christoph Hellwig | f93af2a | 2020-08-31 20:02:37 +0200 | [diff] [blame] | 1364 | goto out_free_disk; |
Tejun Heo | b5d0b9d | 2008-09-03 09:06:42 +0200 | [diff] [blame] | 1365 | |
Christoph Hellwig | f93af2a | 2020-08-31 20:02:37 +0200 | [diff] [blame] | 1366 | disk->node_id = node_id; |
Christoph Hellwig | a33df75 | 2021-01-24 11:02:41 +0100 | [diff] [blame] | 1367 | xa_init(&disk->part_tbl); |
| 1368 | if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL)) |
| 1369 | goto out_destroy_part_tbl; |
Christoph Hellwig | f93af2a | 2020-08-31 20:02:37 +0200 | [diff] [blame] | 1370 | |
| 1371 | disk->minors = minors; |
| 1372 | rand_initialize_disk(disk); |
| 1373 | disk_to_dev(disk)->class = &block_class; |
| 1374 | disk_to_dev(disk)->type = &disk_type; |
| 1375 | device_initialize(disk_to_dev(disk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | return disk; |
Christoph Hellwig | f93af2a | 2020-08-31 20:02:37 +0200 | [diff] [blame] | 1377 | |
Christoph Hellwig | a33df75 | 2021-01-24 11:02:41 +0100 | [diff] [blame] | 1378 | out_destroy_part_tbl: |
| 1379 | xa_destroy(&disk->part_tbl); |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 1380 | bdput(disk->part0); |
Christoph Hellwig | f93af2a | 2020-08-31 20:02:37 +0200 | [diff] [blame] | 1381 | out_free_disk: |
| 1382 | kfree(disk); |
| 1383 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | } |
Byungchul Park | e319e1f | 2017-10-25 17:56:05 +0900 | [diff] [blame] | 1385 | EXPORT_SYMBOL(__alloc_disk_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
Luis Chamberlain | b5bd357 | 2020-06-19 20:47:23 +0000 | [diff] [blame] | 1387 | /** |
Luis Chamberlain | b5bd357 | 2020-06-19 20:47:23 +0000 | [diff] [blame] | 1388 | * put_disk - decrements the gendisk refcount |
Randy Dunlap | 0d20dcc | 2020-07-30 18:42:30 -0700 | [diff] [blame] | 1389 | * @disk: the struct gendisk to decrement the refcount for |
Luis Chamberlain | b5bd357 | 2020-06-19 20:47:23 +0000 | [diff] [blame] | 1390 | * |
| 1391 | * This decrements the refcount for the struct gendisk. When this reaches 0 |
| 1392 | * we'll have disk_release() called. |
Luis Chamberlain | e8c7d14 | 2020-06-19 20:47:25 +0000 | [diff] [blame] | 1393 | * |
| 1394 | * Context: Any context, but the last reference must not be dropped from |
| 1395 | * atomic context. |
Luis Chamberlain | b5bd357 | 2020-06-19 20:47:23 +0000 | [diff] [blame] | 1396 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | void put_disk(struct gendisk *disk) |
| 1398 | { |
| 1399 | if (disk) |
Christoph Hellwig | efdc41c | 2020-11-10 07:25:37 +0100 | [diff] [blame] | 1400 | put_device(disk_to_dev(disk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | EXPORT_SYMBOL(put_disk); |
| 1403 | |
Hannes Reinecke | e3264a4 | 2009-07-28 09:13:13 +0200 | [diff] [blame] | 1404 | static void set_disk_ro_uevent(struct gendisk *gd, int ro) |
| 1405 | { |
| 1406 | char event[] = "DISK_RO=1"; |
| 1407 | char *envp[] = { event, NULL }; |
| 1408 | |
| 1409 | if (!ro) |
| 1410 | event[8] = '0'; |
| 1411 | kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp); |
| 1412 | } |
| 1413 | |
Christoph Hellwig | 52f019d | 2021-01-09 11:42:51 +0100 | [diff] [blame] | 1414 | /** |
| 1415 | * set_disk_ro - set a gendisk read-only |
| 1416 | * @disk: gendisk to operate on |
Lukas Bulwahn | 7f31bee | 2021-01-29 05:55:05 +0100 | [diff] [blame] | 1417 | * @read_only: %true to set the disk read-only, %false set the disk read/write |
Christoph Hellwig | 52f019d | 2021-01-09 11:42:51 +0100 | [diff] [blame] | 1418 | * |
| 1419 | * This function is used to indicate whether a given disk device should have its |
| 1420 | * read-only flag set. set_disk_ro() is typically used by device drivers to |
| 1421 | * indicate whether the underlying physical device is write-protected. |
| 1422 | */ |
| 1423 | void set_disk_ro(struct gendisk *disk, bool read_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | { |
Christoph Hellwig | 52f019d | 2021-01-09 11:42:51 +0100 | [diff] [blame] | 1425 | if (read_only) { |
| 1426 | if (test_and_set_bit(GD_READ_ONLY, &disk->state)) |
| 1427 | return; |
| 1428 | } else { |
| 1429 | if (!test_and_clear_bit(GD_READ_ONLY, &disk->state)) |
| 1430 | return; |
Hannes Reinecke | e3264a4 | 2009-07-28 09:13:13 +0200 | [diff] [blame] | 1431 | } |
Christoph Hellwig | 52f019d | 2021-01-09 11:42:51 +0100 | [diff] [blame] | 1432 | set_disk_ro_uevent(disk, read_only); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | EXPORT_SYMBOL(set_disk_ro); |
| 1435 | |
| 1436 | int bdev_read_only(struct block_device *bdev) |
| 1437 | { |
Christoph Hellwig | 947139b | 2021-01-09 11:42:52 +0100 | [diff] [blame] | 1438 | return bdev->bd_read_only || get_disk_ro(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | EXPORT_SYMBOL(bdev_read_only); |
| 1441 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1442 | /* |
| 1443 | * Disk events - monitor disk events like media change and eject request. |
| 1444 | */ |
| 1445 | struct disk_events { |
| 1446 | struct list_head node; /* all disk_event's */ |
| 1447 | struct gendisk *disk; /* the associated disk */ |
| 1448 | spinlock_t lock; |
| 1449 | |
Tejun Heo | fdd514e | 2011-06-09 20:43:59 +0200 | [diff] [blame] | 1450 | struct mutex block_mutex; /* protects blocking */ |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1451 | int block; /* event blocking depth */ |
| 1452 | unsigned int pending; /* events already sent out */ |
| 1453 | unsigned int clearing; /* events being cleared */ |
| 1454 | |
| 1455 | long poll_msecs; /* interval, -1 for default */ |
| 1456 | struct delayed_work dwork; |
| 1457 | }; |
| 1458 | |
| 1459 | static const char *disk_events_strs[] = { |
| 1460 | [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change", |
| 1461 | [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request", |
| 1462 | }; |
| 1463 | |
| 1464 | static char *disk_uevents[] = { |
| 1465 | [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1", |
| 1466 | [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1", |
| 1467 | }; |
| 1468 | |
| 1469 | /* list of all disk_events */ |
| 1470 | static DEFINE_MUTEX(disk_events_mutex); |
| 1471 | static LIST_HEAD(disk_events); |
| 1472 | |
| 1473 | /* disable in-kernel polling by default */ |
Wei Tang | 1fe8f34 | 2015-11-24 09:58:46 +0800 | [diff] [blame] | 1474 | static unsigned long disk_events_dfl_poll_msecs; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1475 | |
| 1476 | static unsigned long disk_events_poll_jiffies(struct gendisk *disk) |
| 1477 | { |
| 1478 | struct disk_events *ev = disk->ev; |
| 1479 | long intv_msecs = 0; |
| 1480 | |
| 1481 | /* |
| 1482 | * If device-specific poll interval is set, always use it. If |
Martin Wilck | 673387a | 2019-03-27 14:51:01 +0100 | [diff] [blame] | 1483 | * the default is being used, poll if the POLL flag is set. |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1484 | */ |
| 1485 | if (ev->poll_msecs >= 0) |
| 1486 | intv_msecs = ev->poll_msecs; |
Martin Wilck | c92e2f0 | 2019-03-27 14:51:02 +0100 | [diff] [blame] | 1487 | else if (disk->event_flags & DISK_EVENT_FLAG_POLL) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1488 | intv_msecs = disk_events_dfl_poll_msecs; |
| 1489 | |
| 1490 | return msecs_to_jiffies(intv_msecs); |
| 1491 | } |
| 1492 | |
Tejun Heo | c3af54a | 2011-06-09 20:43:55 +0200 | [diff] [blame] | 1493 | /** |
| 1494 | * disk_block_events - block and flush disk event checking |
| 1495 | * @disk: disk to block events for |
| 1496 | * |
| 1497 | * On return from this function, it is guaranteed that event checking |
| 1498 | * isn't in progress and won't happen until unblocked by |
| 1499 | * disk_unblock_events(). Events blocking is counted and the actual |
| 1500 | * unblocking happens after the matching number of unblocks are done. |
| 1501 | * |
| 1502 | * Note that this intentionally does not block event checking from |
| 1503 | * disk_clear_events(). |
| 1504 | * |
| 1505 | * CONTEXT: |
| 1506 | * Might sleep. |
| 1507 | */ |
| 1508 | void disk_block_events(struct gendisk *disk) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1509 | { |
| 1510 | struct disk_events *ev = disk->ev; |
| 1511 | unsigned long flags; |
| 1512 | bool cancel; |
| 1513 | |
Tejun Heo | c3af54a | 2011-06-09 20:43:55 +0200 | [diff] [blame] | 1514 | if (!ev) |
| 1515 | return; |
| 1516 | |
Tejun Heo | fdd514e | 2011-06-09 20:43:59 +0200 | [diff] [blame] | 1517 | /* |
| 1518 | * Outer mutex ensures that the first blocker completes canceling |
| 1519 | * the event work before further blockers are allowed to finish. |
| 1520 | */ |
| 1521 | mutex_lock(&ev->block_mutex); |
| 1522 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1523 | spin_lock_irqsave(&ev->lock, flags); |
| 1524 | cancel = !ev->block++; |
| 1525 | spin_unlock_irqrestore(&ev->lock, flags); |
| 1526 | |
Tejun Heo | c3af54a | 2011-06-09 20:43:55 +0200 | [diff] [blame] | 1527 | if (cancel) |
| 1528 | cancel_delayed_work_sync(&disk->ev->dwork); |
Tejun Heo | fdd514e | 2011-06-09 20:43:59 +0200 | [diff] [blame] | 1529 | |
| 1530 | mutex_unlock(&ev->block_mutex); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static void __disk_unblock_events(struct gendisk *disk, bool check_now) |
| 1534 | { |
| 1535 | struct disk_events *ev = disk->ev; |
| 1536 | unsigned long intv; |
| 1537 | unsigned long flags; |
| 1538 | |
| 1539 | spin_lock_irqsave(&ev->lock, flags); |
| 1540 | |
| 1541 | if (WARN_ON_ONCE(ev->block <= 0)) |
| 1542 | goto out_unlock; |
| 1543 | |
| 1544 | if (--ev->block) |
| 1545 | goto out_unlock; |
| 1546 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1547 | intv = disk_events_poll_jiffies(disk); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1548 | if (check_now) |
Viresh Kumar | 695588f | 2013-04-24 17:12:56 +0530 | [diff] [blame] | 1549 | queue_delayed_work(system_freezable_power_efficient_wq, |
| 1550 | &ev->dwork, 0); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1551 | else if (intv) |
Viresh Kumar | 695588f | 2013-04-24 17:12:56 +0530 | [diff] [blame] | 1552 | queue_delayed_work(system_freezable_power_efficient_wq, |
| 1553 | &ev->dwork, intv); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1554 | out_unlock: |
| 1555 | spin_unlock_irqrestore(&ev->lock, flags); |
| 1556 | } |
| 1557 | |
| 1558 | /** |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1559 | * disk_unblock_events - unblock disk event checking |
| 1560 | * @disk: disk to unblock events for |
| 1561 | * |
| 1562 | * Undo disk_block_events(). When the block count reaches zero, it |
| 1563 | * starts events polling if configured. |
| 1564 | * |
| 1565 | * CONTEXT: |
| 1566 | * Don't care. Safe to call from irq context. |
| 1567 | */ |
| 1568 | void disk_unblock_events(struct gendisk *disk) |
| 1569 | { |
| 1570 | if (disk->ev) |
Tejun Heo | facc31d | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 1571 | __disk_unblock_events(disk, false); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /** |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1575 | * disk_flush_events - schedule immediate event checking and flushing |
| 1576 | * @disk: disk to check and flush events for |
| 1577 | * @mask: events to flush |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1578 | * |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1579 | * Schedule immediate event checking on @disk if not blocked. Events in |
| 1580 | * @mask are scheduled to be cleared from the driver. Note that this |
| 1581 | * doesn't clear the events from @disk->ev. |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1582 | * |
| 1583 | * CONTEXT: |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1584 | * If @mask is non-zero must be called with bdev->bd_mutex held. |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1585 | */ |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1586 | void disk_flush_events(struct gendisk *disk, unsigned int mask) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1587 | { |
Tejun Heo | a9dce2a | 2011-06-09 20:43:54 +0200 | [diff] [blame] | 1588 | struct disk_events *ev = disk->ev; |
Tejun Heo | a9dce2a | 2011-06-09 20:43:54 +0200 | [diff] [blame] | 1589 | |
| 1590 | if (!ev) |
| 1591 | return; |
| 1592 | |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1593 | spin_lock_irq(&ev->lock); |
| 1594 | ev->clearing |= mask; |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 1595 | if (!ev->block) |
Viresh Kumar | 695588f | 2013-04-24 17:12:56 +0530 | [diff] [blame] | 1596 | mod_delayed_work(system_freezable_power_efficient_wq, |
| 1597 | &ev->dwork, 0); |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1598 | spin_unlock_irq(&ev->lock); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1599 | } |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1600 | |
| 1601 | /** |
| 1602 | * disk_clear_events - synchronously check, clear and return pending events |
| 1603 | * @disk: disk to fetch and clear events from |
Masanari Iida | da3dae5 | 2014-09-09 01:27:23 +0900 | [diff] [blame] | 1604 | * @mask: mask of events to be fetched and cleared |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1605 | * |
| 1606 | * Disk events are synchronously checked and pending events in @mask |
| 1607 | * are cleared and returned. This ignores the block count. |
| 1608 | * |
| 1609 | * CONTEXT: |
| 1610 | * Might sleep. |
| 1611 | */ |
Christoph Hellwig | 95f6f3a | 2020-09-08 16:53:29 +0200 | [diff] [blame] | 1612 | static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1613 | { |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1614 | struct disk_events *ev = disk->ev; |
| 1615 | unsigned int pending; |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1616 | unsigned int clearing = mask; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1617 | |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 1618 | if (!ev) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1619 | return 0; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1620 | |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1621 | disk_block_events(disk); |
| 1622 | |
| 1623 | /* |
| 1624 | * store the union of mask and ev->clearing on the stack so that the |
| 1625 | * race with disk_flush_events does not cause ambiguity (ev->clearing |
| 1626 | * can still be modified even if events are blocked). |
| 1627 | */ |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1628 | spin_lock_irq(&ev->lock); |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1629 | clearing |= ev->clearing; |
| 1630 | ev->clearing = 0; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1631 | spin_unlock_irq(&ev->lock); |
| 1632 | |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1633 | disk_check_events(ev, &clearing); |
Derek Basehore | aea24a8 | 2012-12-18 12:27:18 -0800 | [diff] [blame] | 1634 | /* |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1635 | * if ev->clearing is not 0, the disk_flush_events got called in the |
| 1636 | * middle of this function, so we want to run the workfn without delay. |
Derek Basehore | aea24a8 | 2012-12-18 12:27:18 -0800 | [diff] [blame] | 1637 | */ |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1638 | __disk_unblock_events(disk, ev->clearing ? true : false); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1639 | |
| 1640 | /* then, fetch and clear pending events */ |
| 1641 | spin_lock_irq(&ev->lock); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1642 | pending = ev->pending & mask; |
| 1643 | ev->pending &= ~mask; |
| 1644 | spin_unlock_irq(&ev->lock); |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1645 | WARN_ON_ONCE(clearing & mask); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1646 | |
| 1647 | return pending; |
| 1648 | } |
| 1649 | |
Christoph Hellwig | 95f6f3a | 2020-09-08 16:53:29 +0200 | [diff] [blame] | 1650 | /** |
| 1651 | * bdev_check_media_change - check if a removable media has been changed |
| 1652 | * @bdev: block device to check |
| 1653 | * |
| 1654 | * Check whether a removable media has been changed, and attempt to free all |
| 1655 | * dentries and inodes and invalidates all block device page cache entries in |
| 1656 | * that case. |
| 1657 | * |
| 1658 | * Returns %true if the block device changed, or %false if not. |
| 1659 | */ |
| 1660 | bool bdev_check_media_change(struct block_device *bdev) |
| 1661 | { |
| 1662 | unsigned int events; |
| 1663 | |
| 1664 | events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE | |
| 1665 | DISK_EVENT_EJECT_REQUEST); |
| 1666 | if (!(events & DISK_EVENT_MEDIA_CHANGE)) |
| 1667 | return false; |
| 1668 | |
| 1669 | if (__invalidate_device(bdev, true)) |
| 1670 | pr_warn("VFS: busy inodes on changed media %s\n", |
| 1671 | bdev->bd_disk->disk_name); |
Christoph Hellwig | 38430f0 | 2020-09-21 09:19:45 +0200 | [diff] [blame] | 1672 | set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state); |
Christoph Hellwig | 95f6f3a | 2020-09-08 16:53:29 +0200 | [diff] [blame] | 1673 | return true; |
| 1674 | } |
| 1675 | EXPORT_SYMBOL(bdev_check_media_change); |
| 1676 | |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1677 | /* |
| 1678 | * Separate this part out so that a different pointer for clearing_ptr can be |
| 1679 | * passed in for disk_clear_events. |
| 1680 | */ |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1681 | static void disk_events_workfn(struct work_struct *work) |
| 1682 | { |
| 1683 | struct delayed_work *dwork = to_delayed_work(work); |
| 1684 | struct disk_events *ev = container_of(dwork, struct disk_events, dwork); |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1685 | |
| 1686 | disk_check_events(ev, &ev->clearing); |
| 1687 | } |
| 1688 | |
| 1689 | static void disk_check_events(struct disk_events *ev, |
| 1690 | unsigned int *clearing_ptr) |
| 1691 | { |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1692 | struct gendisk *disk = ev->disk; |
| 1693 | char *envp[ARRAY_SIZE(disk_uevents) + 1] = { }; |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1694 | unsigned int clearing = *clearing_ptr; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1695 | unsigned int events; |
| 1696 | unsigned long intv; |
| 1697 | int nr_events = 0, i; |
| 1698 | |
| 1699 | /* check events */ |
| 1700 | events = disk->fops->check_events(disk, clearing); |
| 1701 | |
| 1702 | /* accumulate pending events and schedule next poll if necessary */ |
| 1703 | spin_lock_irq(&ev->lock); |
| 1704 | |
| 1705 | events &= ~ev->pending; |
| 1706 | ev->pending |= events; |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame] | 1707 | *clearing_ptr &= ~clearing; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1708 | |
| 1709 | intv = disk_events_poll_jiffies(disk); |
| 1710 | if (!ev->block && intv) |
Viresh Kumar | 695588f | 2013-04-24 17:12:56 +0530 | [diff] [blame] | 1711 | queue_delayed_work(system_freezable_power_efficient_wq, |
| 1712 | &ev->dwork, intv); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1713 | |
| 1714 | spin_unlock_irq(&ev->lock); |
| 1715 | |
Tejun Heo | 7c88a16 | 2011-04-21 19:43:58 +0200 | [diff] [blame] | 1716 | /* |
| 1717 | * Tell userland about new events. Only the events listed in |
Martin Wilck | c92e2f0 | 2019-03-27 14:51:02 +0100 | [diff] [blame] | 1718 | * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT |
| 1719 | * is set. Otherwise, events are processed internally but never |
| 1720 | * get reported to userland. |
Tejun Heo | 7c88a16 | 2011-04-21 19:43:58 +0200 | [diff] [blame] | 1721 | */ |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1722 | for (i = 0; i < ARRAY_SIZE(disk_uevents); i++) |
Martin Wilck | c92e2f0 | 2019-03-27 14:51:02 +0100 | [diff] [blame] | 1723 | if ((events & disk->events & (1 << i)) && |
| 1724 | (disk->event_flags & DISK_EVENT_FLAG_UEVENT)) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1725 | envp[nr_events++] = disk_uevents[i]; |
| 1726 | |
| 1727 | if (nr_events) |
| 1728 | kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp); |
| 1729 | } |
| 1730 | |
| 1731 | /* |
| 1732 | * A disk events enabled device has the following sysfs nodes under |
| 1733 | * its /sys/block/X/ directory. |
| 1734 | * |
| 1735 | * events : list of all supported events |
| 1736 | * events_async : list of events which can be detected w/o polling |
Martin Wilck | 673387a | 2019-03-27 14:51:01 +0100 | [diff] [blame] | 1737 | * (always empty, only for backwards compatibility) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1738 | * events_poll_msecs : polling interval, 0: disable, -1: system default |
| 1739 | */ |
| 1740 | static ssize_t __disk_events_show(unsigned int events, char *buf) |
| 1741 | { |
| 1742 | const char *delim = ""; |
| 1743 | ssize_t pos = 0; |
| 1744 | int i; |
| 1745 | |
| 1746 | for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++) |
| 1747 | if (events & (1 << i)) { |
| 1748 | pos += sprintf(buf + pos, "%s%s", |
| 1749 | delim, disk_events_strs[i]); |
| 1750 | delim = " "; |
| 1751 | } |
| 1752 | if (pos) |
| 1753 | pos += sprintf(buf + pos, "\n"); |
| 1754 | return pos; |
| 1755 | } |
| 1756 | |
| 1757 | static ssize_t disk_events_show(struct device *dev, |
| 1758 | struct device_attribute *attr, char *buf) |
| 1759 | { |
| 1760 | struct gendisk *disk = dev_to_disk(dev); |
| 1761 | |
Martin Wilck | c92e2f0 | 2019-03-27 14:51:02 +0100 | [diff] [blame] | 1762 | if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT)) |
| 1763 | return 0; |
| 1764 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1765 | return __disk_events_show(disk->events, buf); |
| 1766 | } |
| 1767 | |
| 1768 | static ssize_t disk_events_async_show(struct device *dev, |
| 1769 | struct device_attribute *attr, char *buf) |
| 1770 | { |
Martin Wilck | 673387a | 2019-03-27 14:51:01 +0100 | [diff] [blame] | 1771 | return 0; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | static ssize_t disk_events_poll_msecs_show(struct device *dev, |
| 1775 | struct device_attribute *attr, |
| 1776 | char *buf) |
| 1777 | { |
| 1778 | struct gendisk *disk = dev_to_disk(dev); |
| 1779 | |
Martin Wilck | cdf3e3d | 2019-03-27 14:51:05 +0100 | [diff] [blame] | 1780 | if (!disk->ev) |
| 1781 | return sprintf(buf, "-1\n"); |
| 1782 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1783 | return sprintf(buf, "%ld\n", disk->ev->poll_msecs); |
| 1784 | } |
| 1785 | |
| 1786 | static ssize_t disk_events_poll_msecs_store(struct device *dev, |
| 1787 | struct device_attribute *attr, |
| 1788 | const char *buf, size_t count) |
| 1789 | { |
| 1790 | struct gendisk *disk = dev_to_disk(dev); |
| 1791 | long intv; |
| 1792 | |
| 1793 | if (!count || !sscanf(buf, "%ld", &intv)) |
| 1794 | return -EINVAL; |
| 1795 | |
| 1796 | if (intv < 0 && intv != -1) |
| 1797 | return -EINVAL; |
| 1798 | |
Martin Wilck | cdf3e3d | 2019-03-27 14:51:05 +0100 | [diff] [blame] | 1799 | if (!disk->ev) |
| 1800 | return -ENODEV; |
| 1801 | |
Tejun Heo | c3af54a | 2011-06-09 20:43:55 +0200 | [diff] [blame] | 1802 | disk_block_events(disk); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1803 | disk->ev->poll_msecs = intv; |
| 1804 | __disk_unblock_events(disk, true); |
| 1805 | |
| 1806 | return count; |
| 1807 | } |
| 1808 | |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 1809 | static const DEVICE_ATTR(events, 0444, disk_events_show, NULL); |
| 1810 | static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL); |
| 1811 | static const DEVICE_ATTR(events_poll_msecs, 0644, |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1812 | disk_events_poll_msecs_show, |
| 1813 | disk_events_poll_msecs_store); |
| 1814 | |
| 1815 | static const struct attribute *disk_events_attrs[] = { |
| 1816 | &dev_attr_events.attr, |
| 1817 | &dev_attr_events_async.attr, |
| 1818 | &dev_attr_events_poll_msecs.attr, |
| 1819 | NULL, |
| 1820 | }; |
| 1821 | |
| 1822 | /* |
| 1823 | * The default polling interval can be specified by the kernel |
| 1824 | * parameter block.events_dfl_poll_msecs which defaults to 0 |
| 1825 | * (disable). This can also be modified runtime by writing to |
Akinobu Mita | 1624b0b | 2019-07-16 21:59:35 +0900 | [diff] [blame] | 1826 | * /sys/module/block/parameters/events_dfl_poll_msecs. |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1827 | */ |
| 1828 | static int disk_events_set_dfl_poll_msecs(const char *val, |
| 1829 | const struct kernel_param *kp) |
| 1830 | { |
| 1831 | struct disk_events *ev; |
| 1832 | int ret; |
| 1833 | |
| 1834 | ret = param_set_ulong(val, kp); |
| 1835 | if (ret < 0) |
| 1836 | return ret; |
| 1837 | |
| 1838 | mutex_lock(&disk_events_mutex); |
| 1839 | |
| 1840 | list_for_each_entry(ev, &disk_events, node) |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1841 | disk_flush_events(ev->disk, 0); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1842 | |
| 1843 | mutex_unlock(&disk_events_mutex); |
| 1844 | |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = { |
| 1849 | .set = disk_events_set_dfl_poll_msecs, |
| 1850 | .get = param_get_ulong, |
| 1851 | }; |
| 1852 | |
| 1853 | #undef MODULE_PARAM_PREFIX |
| 1854 | #define MODULE_PARAM_PREFIX "block." |
| 1855 | |
| 1856 | module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops, |
| 1857 | &disk_events_dfl_poll_msecs, 0644); |
| 1858 | |
| 1859 | /* |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 1860 | * disk_{alloc|add|del|release}_events - initialize and destroy disk_events. |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1861 | */ |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 1862 | static void disk_alloc_events(struct gendisk *disk) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1863 | { |
| 1864 | struct disk_events *ev; |
| 1865 | |
Martin Wilck | cdf3e3d | 2019-03-27 14:51:05 +0100 | [diff] [blame] | 1866 | if (!disk->fops->check_events || !disk->events) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1867 | return; |
| 1868 | |
| 1869 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 1870 | if (!ev) { |
| 1871 | pr_warn("%s: failed to initialize events\n", disk->disk_name); |
| 1872 | return; |
| 1873 | } |
| 1874 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1875 | INIT_LIST_HEAD(&ev->node); |
| 1876 | ev->disk = disk; |
| 1877 | spin_lock_init(&ev->lock); |
Tejun Heo | fdd514e | 2011-06-09 20:43:59 +0200 | [diff] [blame] | 1878 | mutex_init(&ev->block_mutex); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1879 | ev->block = 1; |
| 1880 | ev->poll_msecs = -1; |
| 1881 | INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn); |
| 1882 | |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 1883 | disk->ev = ev; |
| 1884 | } |
| 1885 | |
| 1886 | static void disk_add_events(struct gendisk *disk) |
| 1887 | { |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 1888 | /* FIXME: error handling */ |
| 1889 | if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0) |
| 1890 | pr_warn("%s: failed to create sysfs files for events\n", |
| 1891 | disk->disk_name); |
| 1892 | |
Martin Wilck | cdf3e3d | 2019-03-27 14:51:05 +0100 | [diff] [blame] | 1893 | if (!disk->ev) |
| 1894 | return; |
| 1895 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1896 | mutex_lock(&disk_events_mutex); |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 1897 | list_add_tail(&disk->ev->node, &disk_events); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1898 | mutex_unlock(&disk_events_mutex); |
| 1899 | |
| 1900 | /* |
| 1901 | * Block count is initialized to 1 and the following initial |
| 1902 | * unblock kicks it into action. |
| 1903 | */ |
| 1904 | __disk_unblock_events(disk, true); |
| 1905 | } |
| 1906 | |
| 1907 | static void disk_del_events(struct gendisk *disk) |
| 1908 | { |
Martin Wilck | cdf3e3d | 2019-03-27 14:51:05 +0100 | [diff] [blame] | 1909 | if (disk->ev) { |
| 1910 | disk_block_events(disk); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1911 | |
Martin Wilck | cdf3e3d | 2019-03-27 14:51:05 +0100 | [diff] [blame] | 1912 | mutex_lock(&disk_events_mutex); |
| 1913 | list_del_init(&disk->ev->node); |
| 1914 | mutex_unlock(&disk_events_mutex); |
| 1915 | } |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1916 | |
| 1917 | sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs); |
| 1918 | } |
| 1919 | |
| 1920 | static void disk_release_events(struct gendisk *disk) |
| 1921 | { |
| 1922 | /* the block count should be 1 from disk_del_events() */ |
| 1923 | WARN_ON_ONCE(disk->ev && disk->ev->block != 1); |
| 1924 | kfree(disk->ev); |
| 1925 | } |