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