Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * gendisk handling | |
3 | */ | |
4 | ||
1da177e4 LT |
5 | #include <linux/module.h> |
6 | #include <linux/fs.h> | |
7 | #include <linux/genhd.h> | |
b446b60e | 8 | #include <linux/kdev_t.h> |
1da177e4 LT |
9 | #include <linux/kernel.h> |
10 | #include <linux/blkdev.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/spinlock.h> | |
f500975a | 13 | #include <linux/proc_fs.h> |
1da177e4 LT |
14 | #include <linux/seq_file.h> |
15 | #include <linux/slab.h> | |
16 | #include <linux/kmod.h> | |
17 | #include <linux/kobj_map.h> | |
58383af6 | 18 | #include <linux/mutex.h> |
bcce3de1 | 19 | #include <linux/idr.h> |
77ea887e | 20 | #include <linux/log2.h> |
1da177e4 | 21 | |
ff88972c AB |
22 | #include "blk.h" |
23 | ||
edfaa7c3 | 24 | static DEFINE_MUTEX(block_class_lock); |
edfaa7c3 | 25 | struct kobject *block_depr; |
1da177e4 | 26 | |
bcce3de1 TH |
27 | /* for extended dynamic devt allocation, currently only one major is used */ |
28 | #define MAX_EXT_DEVT (1 << MINORBITS) | |
29 | ||
30 | /* For extended devt allocation. ext_devt_mutex prevents look up | |
31 | * results from going away underneath its user. | |
32 | */ | |
33 | static DEFINE_MUTEX(ext_devt_mutex); | |
34 | static DEFINE_IDR(ext_devt_idr); | |
35 | ||
1826eadf AB |
36 | static struct device_type disk_type; |
37 | ||
12c2bdb2 DB |
38 | static void disk_check_events(struct disk_events *ev, |
39 | unsigned int *clearing_ptr); | |
9f53d2fe | 40 | static void disk_alloc_events(struct gendisk *disk); |
77ea887e TH |
41 | static void disk_add_events(struct gendisk *disk); |
42 | static void disk_del_events(struct gendisk *disk); | |
43 | static void disk_release_events(struct gendisk *disk); | |
44 | ||
e71bf0d0 TH |
45 | /** |
46 | * disk_get_part - get partition | |
47 | * @disk: disk to look partition from | |
48 | * @partno: partition number | |
49 | * | |
50 | * Look for partition @partno from @disk. If found, increment | |
51 | * reference count and return it. | |
52 | * | |
53 | * CONTEXT: | |
54 | * Don't care. | |
55 | * | |
56 | * RETURNS: | |
57 | * Pointer to the found partition on success, NULL if not found. | |
58 | */ | |
59 | struct hd_struct *disk_get_part(struct gendisk *disk, int partno) | |
60 | { | |
540eed56 TH |
61 | struct hd_struct *part = NULL; |
62 | struct disk_part_tbl *ptbl; | |
e71bf0d0 | 63 | |
540eed56 | 64 | if (unlikely(partno < 0)) |
e71bf0d0 | 65 | return NULL; |
540eed56 | 66 | |
e71bf0d0 | 67 | rcu_read_lock(); |
540eed56 TH |
68 | |
69 | ptbl = rcu_dereference(disk->part_tbl); | |
70 | if (likely(partno < ptbl->len)) { | |
71 | part = rcu_dereference(ptbl->part[partno]); | |
72 | if (part) | |
73 | get_device(part_to_dev(part)); | |
74 | } | |
75 | ||
e71bf0d0 TH |
76 | rcu_read_unlock(); |
77 | ||
78 | return part; | |
79 | } | |
80 | EXPORT_SYMBOL_GPL(disk_get_part); | |
81 | ||
82 | /** | |
83 | * disk_part_iter_init - initialize partition iterator | |
84 | * @piter: iterator to initialize | |
85 | * @disk: disk to iterate over | |
86 | * @flags: DISK_PITER_* flags | |
87 | * | |
88 | * Initialize @piter so that it iterates over partitions of @disk. | |
89 | * | |
90 | * CONTEXT: | |
91 | * Don't care. | |
92 | */ | |
93 | void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk, | |
94 | unsigned int flags) | |
95 | { | |
540eed56 TH |
96 | struct disk_part_tbl *ptbl; |
97 | ||
98 | rcu_read_lock(); | |
99 | ptbl = rcu_dereference(disk->part_tbl); | |
100 | ||
e71bf0d0 TH |
101 | piter->disk = disk; |
102 | piter->part = NULL; | |
103 | ||
104 | if (flags & DISK_PITER_REVERSE) | |
540eed56 | 105 | piter->idx = ptbl->len - 1; |
71982a40 | 106 | else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0)) |
e71bf0d0 | 107 | piter->idx = 0; |
b5d0b9df TH |
108 | else |
109 | piter->idx = 1; | |
e71bf0d0 TH |
110 | |
111 | piter->flags = flags; | |
540eed56 TH |
112 | |
113 | rcu_read_unlock(); | |
e71bf0d0 TH |
114 | } |
115 | EXPORT_SYMBOL_GPL(disk_part_iter_init); | |
116 | ||
117 | /** | |
118 | * disk_part_iter_next - proceed iterator to the next partition and return it | |
119 | * @piter: iterator of interest | |
120 | * | |
121 | * Proceed @piter to the next partition and return it. | |
122 | * | |
123 | * CONTEXT: | |
124 | * Don't care. | |
125 | */ | |
126 | struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter) | |
127 | { | |
540eed56 | 128 | struct disk_part_tbl *ptbl; |
e71bf0d0 TH |
129 | int inc, end; |
130 | ||
131 | /* put the last partition */ | |
132 | disk_put_part(piter->part); | |
133 | piter->part = NULL; | |
134 | ||
540eed56 | 135 | /* get part_tbl */ |
e71bf0d0 | 136 | rcu_read_lock(); |
540eed56 | 137 | ptbl = rcu_dereference(piter->disk->part_tbl); |
e71bf0d0 TH |
138 | |
139 | /* determine iteration parameters */ | |
140 | if (piter->flags & DISK_PITER_REVERSE) { | |
141 | inc = -1; | |
71982a40 TH |
142 | if (piter->flags & (DISK_PITER_INCL_PART0 | |
143 | DISK_PITER_INCL_EMPTY_PART0)) | |
b5d0b9df TH |
144 | end = -1; |
145 | else | |
146 | end = 0; | |
e71bf0d0 TH |
147 | } else { |
148 | inc = 1; | |
540eed56 | 149 | end = ptbl->len; |
e71bf0d0 TH |
150 | } |
151 | ||
152 | /* iterate to the next partition */ | |
153 | for (; piter->idx != end; piter->idx += inc) { | |
154 | struct hd_struct *part; | |
155 | ||
540eed56 | 156 | part = rcu_dereference(ptbl->part[piter->idx]); |
e71bf0d0 TH |
157 | if (!part) |
158 | continue; | |
c83f6bf9 | 159 | if (!part_nr_sects_read(part) && |
71982a40 TH |
160 | !(piter->flags & DISK_PITER_INCL_EMPTY) && |
161 | !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 && | |
162 | piter->idx == 0)) | |
e71bf0d0 TH |
163 | continue; |
164 | ||
ed9e1982 | 165 | get_device(part_to_dev(part)); |
e71bf0d0 TH |
166 | piter->part = part; |
167 | piter->idx += inc; | |
168 | break; | |
169 | } | |
170 | ||
171 | rcu_read_unlock(); | |
172 | ||
173 | return piter->part; | |
174 | } | |
175 | EXPORT_SYMBOL_GPL(disk_part_iter_next); | |
176 | ||
177 | /** | |
178 | * disk_part_iter_exit - finish up partition iteration | |
179 | * @piter: iter of interest | |
180 | * | |
181 | * Called when iteration is over. Cleans up @piter. | |
182 | * | |
183 | * CONTEXT: | |
184 | * Don't care. | |
185 | */ | |
186 | void disk_part_iter_exit(struct disk_part_iter *piter) | |
187 | { | |
188 | disk_put_part(piter->part); | |
189 | piter->part = NULL; | |
190 | } | |
191 | EXPORT_SYMBOL_GPL(disk_part_iter_exit); | |
192 | ||
a6f23657 JA |
193 | static inline int sector_in_part(struct hd_struct *part, sector_t sector) |
194 | { | |
195 | return part->start_sect <= sector && | |
c83f6bf9 | 196 | sector < part->start_sect + part_nr_sects_read(part); |
a6f23657 JA |
197 | } |
198 | ||
e71bf0d0 TH |
199 | /** |
200 | * disk_map_sector_rcu - map sector to partition | |
201 | * @disk: gendisk of interest | |
202 | * @sector: sector to map | |
203 | * | |
204 | * Find out which partition @sector maps to on @disk. This is | |
205 | * primarily used for stats accounting. | |
206 | * | |
207 | * CONTEXT: | |
208 | * RCU read locked. The returned partition pointer is valid only | |
209 | * while preemption is disabled. | |
210 | * | |
211 | * RETURNS: | |
074a7aca | 212 | * Found partition on success, part0 is returned if no partition matches |
e71bf0d0 TH |
213 | */ |
214 | struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector) | |
215 | { | |
540eed56 | 216 | struct disk_part_tbl *ptbl; |
a6f23657 | 217 | struct hd_struct *part; |
e71bf0d0 TH |
218 | int i; |
219 | ||
540eed56 TH |
220 | ptbl = rcu_dereference(disk->part_tbl); |
221 | ||
a6f23657 JA |
222 | part = rcu_dereference(ptbl->last_lookup); |
223 | if (part && sector_in_part(part, sector)) | |
224 | return part; | |
225 | ||
540eed56 | 226 | for (i = 1; i < ptbl->len; i++) { |
a6f23657 | 227 | part = rcu_dereference(ptbl->part[i]); |
e71bf0d0 | 228 | |
a6f23657 JA |
229 | if (part && sector_in_part(part, sector)) { |
230 | rcu_assign_pointer(ptbl->last_lookup, part); | |
e71bf0d0 | 231 | return part; |
a6f23657 | 232 | } |
e71bf0d0 | 233 | } |
074a7aca | 234 | return &disk->part0; |
e71bf0d0 TH |
235 | } |
236 | EXPORT_SYMBOL_GPL(disk_map_sector_rcu); | |
237 | ||
1da177e4 LT |
238 | /* |
239 | * Can be deleted altogether. Later. | |
240 | * | |
241 | */ | |
242 | static struct blk_major_name { | |
243 | struct blk_major_name *next; | |
244 | int major; | |
245 | char name[16]; | |
68eef3b4 | 246 | } *major_names[BLKDEV_MAJOR_HASH_SIZE]; |
1da177e4 LT |
247 | |
248 | /* index in the above - for now: assume no multimajor ranges */ | |
e61eb2e9 | 249 | static inline int major_to_index(unsigned major) |
1da177e4 | 250 | { |
68eef3b4 | 251 | return major % BLKDEV_MAJOR_HASH_SIZE; |
7170be5f NH |
252 | } |
253 | ||
68eef3b4 | 254 | #ifdef CONFIG_PROC_FS |
cf771cb5 | 255 | void blkdev_show(struct seq_file *seqf, off_t offset) |
7170be5f | 256 | { |
68eef3b4 | 257 | struct blk_major_name *dp; |
7170be5f | 258 | |
68eef3b4 | 259 | if (offset < BLKDEV_MAJOR_HASH_SIZE) { |
edfaa7c3 | 260 | mutex_lock(&block_class_lock); |
68eef3b4 | 261 | for (dp = major_names[offset]; dp; dp = dp->next) |
cf771cb5 | 262 | seq_printf(seqf, "%3d %s\n", dp->major, dp->name); |
edfaa7c3 | 263 | mutex_unlock(&block_class_lock); |
1da177e4 | 264 | } |
1da177e4 | 265 | } |
68eef3b4 | 266 | #endif /* CONFIG_PROC_FS */ |
1da177e4 | 267 | |
9e8c0bcc MN |
268 | /** |
269 | * register_blkdev - register a new block device | |
270 | * | |
271 | * @major: the requested major device number [1..255]. If @major=0, try to | |
272 | * allocate any unused major number. | |
273 | * @name: the name of the new block device as a zero terminated string | |
274 | * | |
275 | * The @name must be unique within the system. | |
276 | * | |
277 | * The return value depends on the @major input parameter. | |
278 | * - if a major device number was requested in range [1..255] then the | |
279 | * function returns zero on success, or a negative error code | |
280 | * - if any unused major number was requested with @major=0 parameter | |
281 | * then the return value is the allocated major number in range | |
282 | * [1..255] or a negative error code otherwise | |
283 | */ | |
1da177e4 LT |
284 | int register_blkdev(unsigned int major, const char *name) |
285 | { | |
286 | struct blk_major_name **n, *p; | |
287 | int index, ret = 0; | |
288 | ||
edfaa7c3 | 289 | mutex_lock(&block_class_lock); |
1da177e4 LT |
290 | |
291 | /* temporary */ | |
292 | if (major == 0) { | |
293 | for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) { | |
294 | if (major_names[index] == NULL) | |
295 | break; | |
296 | } | |
297 | ||
298 | if (index == 0) { | |
299 | printk("register_blkdev: failed to get major for %s\n", | |
300 | name); | |
301 | ret = -EBUSY; | |
302 | goto out; | |
303 | } | |
304 | major = index; | |
305 | ret = major; | |
306 | } | |
307 | ||
308 | p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL); | |
309 | if (p == NULL) { | |
310 | ret = -ENOMEM; | |
311 | goto out; | |
312 | } | |
313 | ||
314 | p->major = major; | |
315 | strlcpy(p->name, name, sizeof(p->name)); | |
316 | p->next = NULL; | |
317 | index = major_to_index(major); | |
318 | ||
319 | for (n = &major_names[index]; *n; n = &(*n)->next) { | |
320 | if ((*n)->major == major) | |
321 | break; | |
322 | } | |
323 | if (!*n) | |
324 | *n = p; | |
325 | else | |
326 | ret = -EBUSY; | |
327 | ||
328 | if (ret < 0) { | |
329 | printk("register_blkdev: cannot get major %d for %s\n", | |
330 | major, name); | |
331 | kfree(p); | |
332 | } | |
333 | out: | |
edfaa7c3 | 334 | mutex_unlock(&block_class_lock); |
1da177e4 LT |
335 | return ret; |
336 | } | |
337 | ||
338 | EXPORT_SYMBOL(register_blkdev); | |
339 | ||
f4480240 | 340 | void unregister_blkdev(unsigned int major, const char *name) |
1da177e4 LT |
341 | { |
342 | struct blk_major_name **n; | |
343 | struct blk_major_name *p = NULL; | |
344 | int index = major_to_index(major); | |
1da177e4 | 345 | |
edfaa7c3 | 346 | mutex_lock(&block_class_lock); |
1da177e4 LT |
347 | for (n = &major_names[index]; *n; n = &(*n)->next) |
348 | if ((*n)->major == major) | |
349 | break; | |
294462a5 AM |
350 | if (!*n || strcmp((*n)->name, name)) { |
351 | WARN_ON(1); | |
294462a5 | 352 | } else { |
1da177e4 LT |
353 | p = *n; |
354 | *n = p->next; | |
355 | } | |
edfaa7c3 | 356 | mutex_unlock(&block_class_lock); |
1da177e4 | 357 | kfree(p); |
1da177e4 LT |
358 | } |
359 | ||
360 | EXPORT_SYMBOL(unregister_blkdev); | |
361 | ||
362 | static struct kobj_map *bdev_map; | |
363 | ||
870d6656 TH |
364 | /** |
365 | * blk_mangle_minor - scatter minor numbers apart | |
366 | * @minor: minor number to mangle | |
367 | * | |
368 | * Scatter consecutively allocated @minor number apart if MANGLE_DEVT | |
369 | * is enabled. Mangling twice gives the original value. | |
370 | * | |
371 | * RETURNS: | |
372 | * Mangled value. | |
373 | * | |
374 | * CONTEXT: | |
375 | * Don't care. | |
376 | */ | |
377 | static int blk_mangle_minor(int minor) | |
378 | { | |
379 | #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT | |
380 | int i; | |
381 | ||
382 | for (i = 0; i < MINORBITS / 2; i++) { | |
383 | int low = minor & (1 << i); | |
384 | int high = minor & (1 << (MINORBITS - 1 - i)); | |
385 | int distance = MINORBITS - 1 - 2 * i; | |
386 | ||
387 | minor ^= low | high; /* clear both bits */ | |
388 | low <<= distance; /* swap the positions */ | |
389 | high >>= distance; | |
390 | minor |= low | high; /* and set */ | |
391 | } | |
392 | #endif | |
393 | return minor; | |
394 | } | |
395 | ||
bcce3de1 TH |
396 | /** |
397 | * blk_alloc_devt - allocate a dev_t for a partition | |
398 | * @part: partition to allocate dev_t for | |
bcce3de1 TH |
399 | * @devt: out parameter for resulting dev_t |
400 | * | |
401 | * Allocate a dev_t for block device. | |
402 | * | |
403 | * RETURNS: | |
404 | * 0 on success, allocated dev_t is returned in *@devt. -errno on | |
405 | * failure. | |
406 | * | |
407 | * CONTEXT: | |
408 | * Might sleep. | |
409 | */ | |
410 | int blk_alloc_devt(struct hd_struct *part, dev_t *devt) | |
411 | { | |
412 | struct gendisk *disk = part_to_disk(part); | |
413 | int idx, rc; | |
414 | ||
415 | /* in consecutive minor range? */ | |
416 | if (part->partno < disk->minors) { | |
417 | *devt = MKDEV(disk->major, disk->first_minor + part->partno); | |
418 | return 0; | |
419 | } | |
420 | ||
421 | /* allocate ext devt */ | |
422 | do { | |
423 | if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL)) | |
424 | return -ENOMEM; | |
425 | rc = idr_get_new(&ext_devt_idr, part, &idx); | |
426 | } while (rc == -EAGAIN); | |
427 | ||
428 | if (rc) | |
429 | return rc; | |
430 | ||
431 | if (idx > MAX_EXT_DEVT) { | |
432 | idr_remove(&ext_devt_idr, idx); | |
433 | return -EBUSY; | |
434 | } | |
435 | ||
870d6656 | 436 | *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx)); |
bcce3de1 TH |
437 | return 0; |
438 | } | |
439 | ||
440 | /** | |
441 | * blk_free_devt - free a dev_t | |
442 | * @devt: dev_t to free | |
443 | * | |
444 | * Free @devt which was allocated using blk_alloc_devt(). | |
445 | * | |
446 | * CONTEXT: | |
447 | * Might sleep. | |
448 | */ | |
449 | void blk_free_devt(dev_t devt) | |
450 | { | |
451 | might_sleep(); | |
452 | ||
453 | if (devt == MKDEV(0, 0)) | |
454 | return; | |
455 | ||
456 | if (MAJOR(devt) == BLOCK_EXT_MAJOR) { | |
457 | mutex_lock(&ext_devt_mutex); | |
870d6656 | 458 | idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt))); |
bcce3de1 TH |
459 | mutex_unlock(&ext_devt_mutex); |
460 | } | |
461 | } | |
462 | ||
1f014290 TH |
463 | static char *bdevt_str(dev_t devt, char *buf) |
464 | { | |
465 | if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) { | |
466 | char tbuf[BDEVT_SIZE]; | |
467 | snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt)); | |
468 | snprintf(buf, BDEVT_SIZE, "%-9s", tbuf); | |
469 | } else | |
470 | snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt)); | |
471 | ||
472 | return buf; | |
473 | } | |
474 | ||
1da177e4 LT |
475 | /* |
476 | * Register device numbers dev..(dev+range-1) | |
477 | * range must be nonzero | |
478 | * The hash chain is sorted on range, so that subranges can override. | |
479 | */ | |
edfaa7c3 | 480 | void blk_register_region(dev_t devt, unsigned long range, struct module *module, |
1da177e4 LT |
481 | struct kobject *(*probe)(dev_t, int *, void *), |
482 | int (*lock)(dev_t, void *), void *data) | |
483 | { | |
edfaa7c3 | 484 | kobj_map(bdev_map, devt, range, module, probe, lock, data); |
1da177e4 LT |
485 | } |
486 | ||
487 | EXPORT_SYMBOL(blk_register_region); | |
488 | ||
edfaa7c3 | 489 | void blk_unregister_region(dev_t devt, unsigned long range) |
1da177e4 | 490 | { |
edfaa7c3 | 491 | kobj_unmap(bdev_map, devt, range); |
1da177e4 LT |
492 | } |
493 | ||
494 | EXPORT_SYMBOL(blk_unregister_region); | |
495 | ||
cf771cb5 | 496 | static struct kobject *exact_match(dev_t devt, int *partno, void *data) |
1da177e4 LT |
497 | { |
498 | struct gendisk *p = data; | |
edfaa7c3 | 499 | |
ed9e1982 | 500 | return &disk_to_dev(p)->kobj; |
1da177e4 LT |
501 | } |
502 | ||
edfaa7c3 | 503 | static int exact_lock(dev_t devt, void *data) |
1da177e4 LT |
504 | { |
505 | struct gendisk *p = data; | |
506 | ||
507 | if (!get_disk(p)) | |
508 | return -1; | |
509 | return 0; | |
510 | } | |
511 | ||
4752bc30 | 512 | static void register_disk(struct gendisk *disk) |
d2bf1b67 TH |
513 | { |
514 | struct device *ddev = disk_to_dev(disk); | |
515 | struct block_device *bdev; | |
516 | struct disk_part_iter piter; | |
517 | struct hd_struct *part; | |
518 | int err; | |
519 | ||
520 | ddev->parent = disk->driverfs_dev; | |
521 | ||
522 | dev_set_name(ddev, disk->disk_name); | |
523 | ||
524 | /* delay uevents, until we scanned partition table */ | |
525 | dev_set_uevent_suppress(ddev, 1); | |
526 | ||
527 | if (device_add(ddev)) | |
528 | return; | |
529 | if (!sysfs_deprecated) { | |
530 | err = sysfs_create_link(block_depr, &ddev->kobj, | |
531 | kobject_name(&ddev->kobj)); | |
532 | if (err) { | |
533 | device_del(ddev); | |
534 | return; | |
535 | } | |
536 | } | |
537 | disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj); | |
538 | disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj); | |
539 | ||
540 | /* No minors to use for partitions */ | |
d27769ec | 541 | if (!disk_part_scan_enabled(disk)) |
d2bf1b67 TH |
542 | goto exit; |
543 | ||
544 | /* No such device (e.g., media were just removed) */ | |
545 | if (!get_capacity(disk)) | |
546 | goto exit; | |
547 | ||
548 | bdev = bdget_disk(disk, 0); | |
549 | if (!bdev) | |
550 | goto exit; | |
551 | ||
552 | bdev->bd_invalidated = 1; | |
553 | err = blkdev_get(bdev, FMODE_READ, NULL); | |
554 | if (err < 0) | |
555 | goto exit; | |
556 | blkdev_put(bdev, FMODE_READ); | |
557 | ||
558 | exit: | |
559 | /* announce disk after possible partitions are created */ | |
560 | dev_set_uevent_suppress(ddev, 0); | |
561 | kobject_uevent(&ddev->kobj, KOBJ_ADD); | |
562 | ||
563 | /* announce possible partitions */ | |
564 | disk_part_iter_init(&piter, disk, 0); | |
565 | while ((part = disk_part_iter_next(&piter))) | |
566 | kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD); | |
567 | disk_part_iter_exit(&piter); | |
568 | } | |
569 | ||
1da177e4 LT |
570 | /** |
571 | * add_disk - add partitioning information to kernel list | |
572 | * @disk: per-device partitioning information | |
573 | * | |
574 | * This function registers the partitioning information in @disk | |
575 | * with the kernel. | |
3e1a7ff8 TH |
576 | * |
577 | * FIXME: error handling | |
1da177e4 LT |
578 | */ |
579 | void add_disk(struct gendisk *disk) | |
580 | { | |
cf0ca9fe | 581 | struct backing_dev_info *bdi; |
3e1a7ff8 | 582 | dev_t devt; |
6ffeea77 | 583 | int retval; |
cf0ca9fe | 584 | |
3e1a7ff8 TH |
585 | /* minors == 0 indicates to use ext devt from part0 and should |
586 | * be accompanied with EXT_DEVT flag. Make sure all | |
587 | * parameters make sense. | |
588 | */ | |
589 | WARN_ON(disk->minors && !(disk->major || disk->first_minor)); | |
590 | WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT)); | |
591 | ||
1da177e4 | 592 | disk->flags |= GENHD_FL_UP; |
3e1a7ff8 TH |
593 | |
594 | retval = blk_alloc_devt(&disk->part0, &devt); | |
595 | if (retval) { | |
596 | WARN_ON(1); | |
597 | return; | |
598 | } | |
599 | disk_to_dev(disk)->devt = devt; | |
600 | ||
601 | /* ->major and ->first_minor aren't supposed to be | |
602 | * dereferenced from here on, but set them just in case. | |
603 | */ | |
604 | disk->major = MAJOR(devt); | |
605 | disk->first_minor = MINOR(devt); | |
606 | ||
9f53d2fe SG |
607 | disk_alloc_events(disk); |
608 | ||
9f5e4865 | 609 | /* Register BDI before referencing it from bdev */ |
01ea5063 SJK |
610 | bdi = &disk->queue->backing_dev_info; |
611 | bdi_register_dev(bdi, disk_devt(disk)); | |
612 | ||
f331c029 TH |
613 | blk_register_region(disk_devt(disk), disk->minors, NULL, |
614 | exact_match, exact_lock, disk); | |
1da177e4 LT |
615 | register_disk(disk); |
616 | blk_register_queue(disk); | |
cf0ca9fe | 617 | |
523e1d39 TH |
618 | /* |
619 | * Take an extra ref on queue which will be put on disk_release() | |
620 | * so that it sticks around as long as @disk is there. | |
621 | */ | |
09ac46c4 | 622 | WARN_ON_ONCE(!blk_get_queue(disk->queue)); |
523e1d39 | 623 | |
ed9e1982 TH |
624 | retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj, |
625 | "bdi"); | |
6ffeea77 | 626 | WARN_ON(retval); |
1da177e4 | 627 | |
77ea887e | 628 | disk_add_events(disk); |
1da177e4 | 629 | } |
1da177e4 | 630 | EXPORT_SYMBOL(add_disk); |
1da177e4 | 631 | |
d2bf1b67 | 632 | void del_gendisk(struct gendisk *disk) |
1da177e4 | 633 | { |
d2bf1b67 TH |
634 | struct disk_part_iter piter; |
635 | struct hd_struct *part; | |
636 | ||
77ea887e TH |
637 | disk_del_events(disk); |
638 | ||
d2bf1b67 TH |
639 | /* invalidate stuff */ |
640 | disk_part_iter_init(&piter, disk, | |
641 | DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE); | |
642 | while ((part = disk_part_iter_next(&piter))) { | |
643 | invalidate_partition(disk, part->partno); | |
644 | delete_partition(disk, part->partno); | |
645 | } | |
646 | disk_part_iter_exit(&piter); | |
647 | ||
648 | invalidate_partition(disk, 0); | |
649 | blk_free_devt(disk_to_dev(disk)->devt); | |
650 | set_capacity(disk, 0); | |
651 | disk->flags &= ~GENHD_FL_UP; | |
652 | ||
ed9e1982 | 653 | sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi"); |
cf0ca9fe | 654 | bdi_unregister(&disk->queue->backing_dev_info); |
1da177e4 | 655 | blk_unregister_queue(disk); |
f331c029 | 656 | blk_unregister_region(disk_devt(disk), disk->minors); |
d2bf1b67 TH |
657 | |
658 | part_stat_set_all(&disk->part0, 0); | |
659 | disk->part0.stamp = 0; | |
660 | ||
661 | kobject_put(disk->part0.holder_dir); | |
662 | kobject_put(disk->slave_dir); | |
663 | disk->driverfs_dev = NULL; | |
664 | if (!sysfs_deprecated) | |
665 | sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk))); | |
666 | device_del(disk_to_dev(disk)); | |
1da177e4 | 667 | } |
d2bf1b67 | 668 | EXPORT_SYMBOL(del_gendisk); |
1da177e4 | 669 | |
1da177e4 LT |
670 | /** |
671 | * get_gendisk - get partitioning information for a given device | |
710027a4 | 672 | * @devt: device to get partitioning information for |
496aa8a9 | 673 | * @partno: returned partition index |
1da177e4 LT |
674 | * |
675 | * This function gets the structure containing partitioning | |
710027a4 | 676 | * information for the given device @devt. |
1da177e4 | 677 | */ |
cf771cb5 | 678 | struct gendisk *get_gendisk(dev_t devt, int *partno) |
1da177e4 | 679 | { |
bcce3de1 TH |
680 | struct gendisk *disk = NULL; |
681 | ||
682 | if (MAJOR(devt) != BLOCK_EXT_MAJOR) { | |
683 | struct kobject *kobj; | |
684 | ||
685 | kobj = kobj_lookup(bdev_map, devt, partno); | |
686 | if (kobj) | |
687 | disk = dev_to_disk(kobj_to_dev(kobj)); | |
688 | } else { | |
689 | struct hd_struct *part; | |
690 | ||
691 | mutex_lock(&ext_devt_mutex); | |
870d6656 | 692 | part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt))); |
bcce3de1 TH |
693 | if (part && get_disk(part_to_disk(part))) { |
694 | *partno = part->partno; | |
695 | disk = part_to_disk(part); | |
696 | } | |
697 | mutex_unlock(&ext_devt_mutex); | |
698 | } | |
edfaa7c3 | 699 | |
bcce3de1 | 700 | return disk; |
1da177e4 | 701 | } |
b6ac23af | 702 | EXPORT_SYMBOL(get_gendisk); |
1da177e4 | 703 | |
f331c029 TH |
704 | /** |
705 | * bdget_disk - do bdget() by gendisk and partition number | |
706 | * @disk: gendisk of interest | |
707 | * @partno: partition number | |
708 | * | |
709 | * Find partition @partno from @disk, do bdget() on it. | |
710 | * | |
711 | * CONTEXT: | |
712 | * Don't care. | |
713 | * | |
714 | * RETURNS: | |
715 | * Resulting block_device on success, NULL on failure. | |
716 | */ | |
aeb3d3a8 | 717 | struct block_device *bdget_disk(struct gendisk *disk, int partno) |
f331c029 | 718 | { |
548b10eb TH |
719 | struct hd_struct *part; |
720 | struct block_device *bdev = NULL; | |
f331c029 | 721 | |
548b10eb | 722 | part = disk_get_part(disk, partno); |
2bbedcb4 | 723 | if (part) |
548b10eb TH |
724 | bdev = bdget(part_devt(part)); |
725 | disk_put_part(part); | |
f331c029 | 726 | |
548b10eb | 727 | return bdev; |
f331c029 TH |
728 | } |
729 | EXPORT_SYMBOL(bdget_disk); | |
730 | ||
5c6f35c5 GKH |
731 | /* |
732 | * print a full list of all partitions - intended for places where the root | |
733 | * filesystem can't be mounted and thus to give the victim some idea of what | |
734 | * went wrong | |
735 | */ | |
736 | void __init printk_all_partitions(void) | |
737 | { | |
def4e38d TH |
738 | struct class_dev_iter iter; |
739 | struct device *dev; | |
740 | ||
741 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); | |
742 | while ((dev = class_dev_iter_next(&iter))) { | |
743 | struct gendisk *disk = dev_to_disk(dev); | |
e71bf0d0 TH |
744 | struct disk_part_iter piter; |
745 | struct hd_struct *part; | |
1f014290 TH |
746 | char name_buf[BDEVNAME_SIZE]; |
747 | char devt_buf[BDEVT_SIZE]; | |
def4e38d TH |
748 | |
749 | /* | |
750 | * Don't show empty devices or things that have been | |
25985edc | 751 | * suppressed |
def4e38d TH |
752 | */ |
753 | if (get_capacity(disk) == 0 || | |
754 | (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)) | |
755 | continue; | |
756 | ||
757 | /* | |
758 | * Note, unlike /proc/partitions, I am showing the | |
759 | * numbers in hex - the same format as the root= | |
760 | * option takes. | |
761 | */ | |
074a7aca TH |
762 | disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0); |
763 | while ((part = disk_part_iter_next(&piter))) { | |
764 | bool is_part0 = part == &disk->part0; | |
def4e38d | 765 | |
b5af921e | 766 | printk("%s%s %10llu %s %s", is_part0 ? "" : " ", |
1f014290 | 767 | bdevt_str(part_devt(part), devt_buf), |
c83f6bf9 VG |
768 | (unsigned long long)part_nr_sects_read(part) >> 1 |
769 | , disk_name(disk, part->partno, name_buf), | |
1ad7e899 | 770 | part->info ? part->info->uuid : ""); |
074a7aca TH |
771 | if (is_part0) { |
772 | if (disk->driverfs_dev != NULL && | |
773 | disk->driverfs_dev->driver != NULL) | |
774 | printk(" driver: %s\n", | |
775 | disk->driverfs_dev->driver->name); | |
776 | else | |
777 | printk(" (driver?)\n"); | |
778 | } else | |
779 | printk("\n"); | |
780 | } | |
e71bf0d0 | 781 | disk_part_iter_exit(&piter); |
def4e38d TH |
782 | } |
783 | class_dev_iter_exit(&iter); | |
dd2a345f DG |
784 | } |
785 | ||
1da177e4 LT |
786 | #ifdef CONFIG_PROC_FS |
787 | /* iterator */ | |
def4e38d | 788 | static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos) |
68c4d4a7 | 789 | { |
def4e38d TH |
790 | loff_t skip = *pos; |
791 | struct class_dev_iter *iter; | |
792 | struct device *dev; | |
68c4d4a7 | 793 | |
aeb3d3a8 | 794 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); |
def4e38d TH |
795 | if (!iter) |
796 | return ERR_PTR(-ENOMEM); | |
797 | ||
798 | seqf->private = iter; | |
799 | class_dev_iter_init(iter, &block_class, NULL, &disk_type); | |
800 | do { | |
801 | dev = class_dev_iter_next(iter); | |
802 | if (!dev) | |
803 | return NULL; | |
804 | } while (skip--); | |
805 | ||
806 | return dev_to_disk(dev); | |
68c4d4a7 GKH |
807 | } |
808 | ||
def4e38d | 809 | static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos) |
1da177e4 | 810 | { |
edfaa7c3 | 811 | struct device *dev; |
1da177e4 | 812 | |
def4e38d TH |
813 | (*pos)++; |
814 | dev = class_dev_iter_next(seqf->private); | |
2ac3cee5 | 815 | if (dev) |
68c4d4a7 | 816 | return dev_to_disk(dev); |
2ac3cee5 | 817 | |
1da177e4 LT |
818 | return NULL; |
819 | } | |
820 | ||
def4e38d | 821 | static void disk_seqf_stop(struct seq_file *seqf, void *v) |
27f30251 | 822 | { |
def4e38d | 823 | struct class_dev_iter *iter = seqf->private; |
27f30251 | 824 | |
def4e38d TH |
825 | /* stop is called even after start failed :-( */ |
826 | if (iter) { | |
827 | class_dev_iter_exit(iter); | |
828 | kfree(iter); | |
5c0ef6d0 | 829 | } |
1da177e4 LT |
830 | } |
831 | ||
def4e38d | 832 | static void *show_partition_start(struct seq_file *seqf, loff_t *pos) |
1da177e4 | 833 | { |
06768067 | 834 | void *p; |
def4e38d TH |
835 | |
836 | p = disk_seqf_start(seqf, pos); | |
b9f985b6 | 837 | if (!IS_ERR_OR_NULL(p) && !*pos) |
def4e38d TH |
838 | seq_puts(seqf, "major minor #blocks name\n\n"); |
839 | return p; | |
1da177e4 LT |
840 | } |
841 | ||
cf771cb5 | 842 | static int show_partition(struct seq_file *seqf, void *v) |
1da177e4 LT |
843 | { |
844 | struct gendisk *sgp = v; | |
e71bf0d0 TH |
845 | struct disk_part_iter piter; |
846 | struct hd_struct *part; | |
1da177e4 LT |
847 | char buf[BDEVNAME_SIZE]; |
848 | ||
1da177e4 | 849 | /* Don't show non-partitionable removeable devices or empty devices */ |
d27769ec | 850 | if (!get_capacity(sgp) || (!disk_max_parts(sgp) && |
f331c029 | 851 | (sgp->flags & GENHD_FL_REMOVABLE))) |
1da177e4 LT |
852 | return 0; |
853 | if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO) | |
854 | return 0; | |
855 | ||
856 | /* show the full disk and all non-0 size partitions of it */ | |
074a7aca | 857 | disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0); |
e71bf0d0 | 858 | while ((part = disk_part_iter_next(&piter))) |
1f014290 | 859 | seq_printf(seqf, "%4d %7d %10llu %s\n", |
f331c029 | 860 | MAJOR(part_devt(part)), MINOR(part_devt(part)), |
c83f6bf9 | 861 | (unsigned long long)part_nr_sects_read(part) >> 1, |
f331c029 | 862 | disk_name(sgp, part->partno, buf)); |
e71bf0d0 | 863 | disk_part_iter_exit(&piter); |
1da177e4 LT |
864 | |
865 | return 0; | |
866 | } | |
867 | ||
f500975a | 868 | static const struct seq_operations partitions_op = { |
def4e38d TH |
869 | .start = show_partition_start, |
870 | .next = disk_seqf_next, | |
871 | .stop = disk_seqf_stop, | |
edfaa7c3 | 872 | .show = show_partition |
1da177e4 | 873 | }; |
f500975a AD |
874 | |
875 | static int partitions_open(struct inode *inode, struct file *file) | |
876 | { | |
877 | return seq_open(file, &partitions_op); | |
878 | } | |
879 | ||
880 | static const struct file_operations proc_partitions_operations = { | |
881 | .open = partitions_open, | |
882 | .read = seq_read, | |
883 | .llseek = seq_lseek, | |
884 | .release = seq_release, | |
885 | }; | |
1da177e4 LT |
886 | #endif |
887 | ||
888 | ||
cf771cb5 | 889 | static struct kobject *base_probe(dev_t devt, int *partno, void *data) |
1da177e4 | 890 | { |
edfaa7c3 | 891 | if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0) |
1da177e4 | 892 | /* Make old-style 2.4 aliases work */ |
edfaa7c3 | 893 | request_module("block-major-%d", MAJOR(devt)); |
1da177e4 LT |
894 | return NULL; |
895 | } | |
896 | ||
897 | static int __init genhd_device_init(void) | |
898 | { | |
e105b8bf DW |
899 | int error; |
900 | ||
901 | block_class.dev_kobj = sysfs_dev_block_kobj; | |
902 | error = class_register(&block_class); | |
ee27a558 RM |
903 | if (unlikely(error)) |
904 | return error; | |
edfaa7c3 | 905 | bdev_map = kobj_map_init(base_probe, &block_class_lock); |
1da177e4 | 906 | blk_dev_init(); |
edfaa7c3 | 907 | |
561ec68e ZY |
908 | register_blkdev(BLOCK_EXT_MAJOR, "blkext"); |
909 | ||
edfaa7c3 | 910 | /* create top-level block dir */ |
e52eec13 AK |
911 | if (!sysfs_deprecated) |
912 | block_depr = kobject_create_and_add("block", NULL); | |
830d3cfb | 913 | return 0; |
1da177e4 LT |
914 | } |
915 | ||
916 | subsys_initcall(genhd_device_init); | |
917 | ||
edfaa7c3 KS |
918 | static ssize_t disk_range_show(struct device *dev, |
919 | struct device_attribute *attr, char *buf) | |
1da177e4 | 920 | { |
edfaa7c3 | 921 | struct gendisk *disk = dev_to_disk(dev); |
1da177e4 | 922 | |
edfaa7c3 | 923 | return sprintf(buf, "%d\n", disk->minors); |
1da177e4 LT |
924 | } |
925 | ||
1f014290 TH |
926 | static ssize_t disk_ext_range_show(struct device *dev, |
927 | struct device_attribute *attr, char *buf) | |
928 | { | |
929 | struct gendisk *disk = dev_to_disk(dev); | |
930 | ||
b5d0b9df | 931 | return sprintf(buf, "%d\n", disk_max_parts(disk)); |
1f014290 TH |
932 | } |
933 | ||
edfaa7c3 KS |
934 | static ssize_t disk_removable_show(struct device *dev, |
935 | struct device_attribute *attr, char *buf) | |
a7fd6706 | 936 | { |
edfaa7c3 | 937 | struct gendisk *disk = dev_to_disk(dev); |
a7fd6706 | 938 | |
edfaa7c3 KS |
939 | return sprintf(buf, "%d\n", |
940 | (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); | |
a7fd6706 KS |
941 | } |
942 | ||
1c9ce527 KS |
943 | static ssize_t disk_ro_show(struct device *dev, |
944 | struct device_attribute *attr, char *buf) | |
945 | { | |
946 | struct gendisk *disk = dev_to_disk(dev); | |
947 | ||
b7db9956 | 948 | return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0); |
1c9ce527 KS |
949 | } |
950 | ||
edfaa7c3 KS |
951 | static ssize_t disk_capability_show(struct device *dev, |
952 | struct device_attribute *attr, char *buf) | |
86ce18d7 | 953 | { |
edfaa7c3 KS |
954 | struct gendisk *disk = dev_to_disk(dev); |
955 | ||
956 | return sprintf(buf, "%x\n", disk->flags); | |
86ce18d7 | 957 | } |
edfaa7c3 | 958 | |
c72758f3 MP |
959 | static ssize_t disk_alignment_offset_show(struct device *dev, |
960 | struct device_attribute *attr, | |
961 | char *buf) | |
962 | { | |
963 | struct gendisk *disk = dev_to_disk(dev); | |
964 | ||
965 | return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue)); | |
966 | } | |
967 | ||
86b37281 MP |
968 | static ssize_t disk_discard_alignment_show(struct device *dev, |
969 | struct device_attribute *attr, | |
970 | char *buf) | |
971 | { | |
972 | struct gendisk *disk = dev_to_disk(dev); | |
973 | ||
dd3d145d | 974 | return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue)); |
86b37281 MP |
975 | } |
976 | ||
edfaa7c3 | 977 | static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL); |
1f014290 | 978 | static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL); |
edfaa7c3 | 979 | static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL); |
1c9ce527 | 980 | static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL); |
e5610521 | 981 | static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL); |
c72758f3 | 982 | static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL); |
86b37281 MP |
983 | static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show, |
984 | NULL); | |
edfaa7c3 | 985 | static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL); |
074a7aca | 986 | static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL); |
316d315b | 987 | static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL); |
c17bb495 | 988 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
edfaa7c3 | 989 | static struct device_attribute dev_attr_fail = |
eddb2e26 | 990 | __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store); |
c17bb495 | 991 | #endif |
581d4e28 JA |
992 | #ifdef CONFIG_FAIL_IO_TIMEOUT |
993 | static struct device_attribute dev_attr_fail_timeout = | |
994 | __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show, | |
995 | part_timeout_store); | |
996 | #endif | |
edfaa7c3 KS |
997 | |
998 | static struct attribute *disk_attrs[] = { | |
999 | &dev_attr_range.attr, | |
1f014290 | 1000 | &dev_attr_ext_range.attr, |
edfaa7c3 | 1001 | &dev_attr_removable.attr, |
1c9ce527 | 1002 | &dev_attr_ro.attr, |
edfaa7c3 | 1003 | &dev_attr_size.attr, |
c72758f3 | 1004 | &dev_attr_alignment_offset.attr, |
86b37281 | 1005 | &dev_attr_discard_alignment.attr, |
edfaa7c3 KS |
1006 | &dev_attr_capability.attr, |
1007 | &dev_attr_stat.attr, | |
316d315b | 1008 | &dev_attr_inflight.attr, |
edfaa7c3 KS |
1009 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
1010 | &dev_attr_fail.attr, | |
581d4e28 JA |
1011 | #endif |
1012 | #ifdef CONFIG_FAIL_IO_TIMEOUT | |
1013 | &dev_attr_fail_timeout.attr, | |
edfaa7c3 KS |
1014 | #endif |
1015 | NULL | |
1016 | }; | |
1017 | ||
1018 | static struct attribute_group disk_attr_group = { | |
1019 | .attrs = disk_attrs, | |
1020 | }; | |
1021 | ||
a4dbd674 | 1022 | static const struct attribute_group *disk_attr_groups[] = { |
edfaa7c3 KS |
1023 | &disk_attr_group, |
1024 | NULL | |
1da177e4 LT |
1025 | }; |
1026 | ||
540eed56 TH |
1027 | /** |
1028 | * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way | |
1029 | * @disk: disk to replace part_tbl for | |
1030 | * @new_ptbl: new part_tbl to install | |
1031 | * | |
1032 | * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The | |
1033 | * original ptbl is freed using RCU callback. | |
1034 | * | |
1035 | * LOCKING: | |
1036 | * Matching bd_mutx locked. | |
1037 | */ | |
1038 | static void disk_replace_part_tbl(struct gendisk *disk, | |
1039 | struct disk_part_tbl *new_ptbl) | |
1040 | { | |
1041 | struct disk_part_tbl *old_ptbl = disk->part_tbl; | |
1042 | ||
1043 | rcu_assign_pointer(disk->part_tbl, new_ptbl); | |
a6f23657 JA |
1044 | |
1045 | if (old_ptbl) { | |
1046 | rcu_assign_pointer(old_ptbl->last_lookup, NULL); | |
57bdfbf9 | 1047 | kfree_rcu(old_ptbl, rcu_head); |
a6f23657 | 1048 | } |
540eed56 TH |
1049 | } |
1050 | ||
1051 | /** | |
1052 | * disk_expand_part_tbl - expand disk->part_tbl | |
1053 | * @disk: disk to expand part_tbl for | |
1054 | * @partno: expand such that this partno can fit in | |
1055 | * | |
1056 | * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl | |
1057 | * uses RCU to allow unlocked dereferencing for stats and other stuff. | |
1058 | * | |
1059 | * LOCKING: | |
1060 | * Matching bd_mutex locked, might sleep. | |
1061 | * | |
1062 | * RETURNS: | |
1063 | * 0 on success, -errno on failure. | |
1064 | */ | |
1065 | int disk_expand_part_tbl(struct gendisk *disk, int partno) | |
1066 | { | |
1067 | struct disk_part_tbl *old_ptbl = disk->part_tbl; | |
1068 | struct disk_part_tbl *new_ptbl; | |
1069 | int len = old_ptbl ? old_ptbl->len : 0; | |
1070 | int target = partno + 1; | |
1071 | size_t size; | |
1072 | int i; | |
1073 | ||
1074 | /* disk_max_parts() is zero during initialization, ignore if so */ | |
1075 | if (disk_max_parts(disk) && target > disk_max_parts(disk)) | |
1076 | return -EINVAL; | |
1077 | ||
1078 | if (target <= len) | |
1079 | return 0; | |
1080 | ||
1081 | size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]); | |
1082 | new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id); | |
1083 | if (!new_ptbl) | |
1084 | return -ENOMEM; | |
1085 | ||
540eed56 TH |
1086 | new_ptbl->len = target; |
1087 | ||
1088 | for (i = 0; i < len; i++) | |
1089 | rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]); | |
1090 | ||
1091 | disk_replace_part_tbl(disk, new_ptbl); | |
1092 | return 0; | |
1093 | } | |
1094 | ||
edfaa7c3 | 1095 | static void disk_release(struct device *dev) |
1da177e4 | 1096 | { |
edfaa7c3 KS |
1097 | struct gendisk *disk = dev_to_disk(dev); |
1098 | ||
77ea887e | 1099 | disk_release_events(disk); |
1da177e4 | 1100 | kfree(disk->random); |
540eed56 | 1101 | disk_replace_part_tbl(disk, NULL); |
074a7aca | 1102 | free_part_stats(&disk->part0); |
6d1d8050 | 1103 | free_part_info(&disk->part0); |
523e1d39 TH |
1104 | if (disk->queue) |
1105 | blk_put_queue(disk->queue); | |
1da177e4 LT |
1106 | kfree(disk); |
1107 | } | |
edfaa7c3 KS |
1108 | struct class block_class = { |
1109 | .name = "block", | |
1da177e4 LT |
1110 | }; |
1111 | ||
2c9ede55 | 1112 | static char *block_devnode(struct device *dev, umode_t *mode) |
b03f38b6 KS |
1113 | { |
1114 | struct gendisk *disk = dev_to_disk(dev); | |
1115 | ||
e454cea2 KS |
1116 | if (disk->devnode) |
1117 | return disk->devnode(disk, mode); | |
b03f38b6 KS |
1118 | return NULL; |
1119 | } | |
1120 | ||
1826eadf | 1121 | static struct device_type disk_type = { |
edfaa7c3 KS |
1122 | .name = "disk", |
1123 | .groups = disk_attr_groups, | |
1124 | .release = disk_release, | |
e454cea2 | 1125 | .devnode = block_devnode, |
1da177e4 LT |
1126 | }; |
1127 | ||
a6e2ba88 | 1128 | #ifdef CONFIG_PROC_FS |
cf771cb5 TH |
1129 | /* |
1130 | * aggregate disk stat collector. Uses the same stats that the sysfs | |
1131 | * entries do, above, but makes them available through one seq_file. | |
1132 | * | |
1133 | * The output looks suspiciously like /proc/partitions with a bunch of | |
1134 | * extra fields. | |
1135 | */ | |
1136 | static int diskstats_show(struct seq_file *seqf, void *v) | |
1da177e4 LT |
1137 | { |
1138 | struct gendisk *gp = v; | |
e71bf0d0 TH |
1139 | struct disk_part_iter piter; |
1140 | struct hd_struct *hd; | |
1da177e4 | 1141 | char buf[BDEVNAME_SIZE]; |
c9959059 | 1142 | int cpu; |
1da177e4 LT |
1143 | |
1144 | /* | |
ed9e1982 | 1145 | if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next) |
cf771cb5 | 1146 | seq_puts(seqf, "major minor name" |
1da177e4 LT |
1147 | " rio rmerge rsect ruse wio wmerge " |
1148 | "wsect wuse running use aveq" | |
1149 | "\n\n"); | |
1150 | */ | |
9f5e4865 | 1151 | |
71982a40 | 1152 | disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0); |
e71bf0d0 | 1153 | while ((hd = disk_part_iter_next(&piter))) { |
074a7aca | 1154 | cpu = part_stat_lock(); |
c9959059 | 1155 | part_round_stats(cpu, hd); |
074a7aca | 1156 | part_stat_unlock(); |
f95fe9cf HP |
1157 | seq_printf(seqf, "%4d %7d %s %lu %lu %lu " |
1158 | "%u %lu %lu %lu %u %u %u %u\n", | |
f331c029 TH |
1159 | MAJOR(part_devt(hd)), MINOR(part_devt(hd)), |
1160 | disk_name(gp, hd->partno, buf), | |
53f22956 LY |
1161 | part_stat_read(hd, ios[READ]), |
1162 | part_stat_read(hd, merges[READ]), | |
f95fe9cf | 1163 | part_stat_read(hd, sectors[READ]), |
53f22956 LY |
1164 | jiffies_to_msecs(part_stat_read(hd, ticks[READ])), |
1165 | part_stat_read(hd, ios[WRITE]), | |
1166 | part_stat_read(hd, merges[WRITE]), | |
f95fe9cf | 1167 | part_stat_read(hd, sectors[WRITE]), |
53f22956 | 1168 | jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])), |
316d315b | 1169 | part_in_flight(hd), |
28f39d55 JM |
1170 | jiffies_to_msecs(part_stat_read(hd, io_ticks)), |
1171 | jiffies_to_msecs(part_stat_read(hd, time_in_queue)) | |
1172 | ); | |
1da177e4 | 1173 | } |
e71bf0d0 | 1174 | disk_part_iter_exit(&piter); |
9f5e4865 | 1175 | |
1da177e4 LT |
1176 | return 0; |
1177 | } | |
1178 | ||
31d85ab2 | 1179 | static const struct seq_operations diskstats_op = { |
def4e38d TH |
1180 | .start = disk_seqf_start, |
1181 | .next = disk_seqf_next, | |
1182 | .stop = disk_seqf_stop, | |
1da177e4 LT |
1183 | .show = diskstats_show |
1184 | }; | |
f500975a | 1185 | |
31d85ab2 AD |
1186 | static int diskstats_open(struct inode *inode, struct file *file) |
1187 | { | |
1188 | return seq_open(file, &diskstats_op); | |
1189 | } | |
1190 | ||
1191 | static const struct file_operations proc_diskstats_operations = { | |
1192 | .open = diskstats_open, | |
1193 | .read = seq_read, | |
1194 | .llseek = seq_lseek, | |
1195 | .release = seq_release, | |
1196 | }; | |
1197 | ||
f500975a AD |
1198 | static int __init proc_genhd_init(void) |
1199 | { | |
31d85ab2 | 1200 | proc_create("diskstats", 0, NULL, &proc_diskstats_operations); |
f500975a AD |
1201 | proc_create("partitions", 0, NULL, &proc_partitions_operations); |
1202 | return 0; | |
1203 | } | |
1204 | module_init(proc_genhd_init); | |
a6e2ba88 | 1205 | #endif /* CONFIG_PROC_FS */ |
1da177e4 | 1206 | |
cf771cb5 | 1207 | dev_t blk_lookup_devt(const char *name, int partno) |
a142be85 | 1208 | { |
def4e38d TH |
1209 | dev_t devt = MKDEV(0, 0); |
1210 | struct class_dev_iter iter; | |
1211 | struct device *dev; | |
a142be85 | 1212 | |
def4e38d TH |
1213 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); |
1214 | while ((dev = class_dev_iter_next(&iter))) { | |
a142be85 | 1215 | struct gendisk *disk = dev_to_disk(dev); |
548b10eb | 1216 | struct hd_struct *part; |
a142be85 | 1217 | |
3ada8b7e | 1218 | if (strcmp(dev_name(dev), name)) |
f331c029 | 1219 | continue; |
f331c029 | 1220 | |
41b8c853 NB |
1221 | if (partno < disk->minors) { |
1222 | /* We need to return the right devno, even | |
1223 | * if the partition doesn't exist yet. | |
1224 | */ | |
1225 | devt = MKDEV(MAJOR(dev->devt), | |
1226 | MINOR(dev->devt) + partno); | |
1227 | break; | |
1228 | } | |
548b10eb | 1229 | part = disk_get_part(disk, partno); |
2bbedcb4 | 1230 | if (part) { |
f331c029 | 1231 | devt = part_devt(part); |
e71bf0d0 | 1232 | disk_put_part(part); |
548b10eb | 1233 | break; |
def4e38d | 1234 | } |
548b10eb | 1235 | disk_put_part(part); |
5c0ef6d0 | 1236 | } |
def4e38d | 1237 | class_dev_iter_exit(&iter); |
edfaa7c3 KS |
1238 | return devt; |
1239 | } | |
edfaa7c3 KS |
1240 | EXPORT_SYMBOL(blk_lookup_devt); |
1241 | ||
1da177e4 LT |
1242 | struct gendisk *alloc_disk(int minors) |
1243 | { | |
c304a51b | 1244 | return alloc_disk_node(minors, NUMA_NO_NODE); |
1946089a | 1245 | } |
689d6fac | 1246 | EXPORT_SYMBOL(alloc_disk); |
1946089a CL |
1247 | |
1248 | struct gendisk *alloc_disk_node(int minors, int node_id) | |
1249 | { | |
1250 | struct gendisk *disk; | |
1251 | ||
94f6030c CL |
1252 | disk = kmalloc_node(sizeof(struct gendisk), |
1253 | GFP_KERNEL | __GFP_ZERO, node_id); | |
1da177e4 | 1254 | if (disk) { |
074a7aca | 1255 | if (!init_part_stats(&disk->part0)) { |
1da177e4 LT |
1256 | kfree(disk); |
1257 | return NULL; | |
1258 | } | |
bf91db18 | 1259 | disk->node_id = node_id; |
540eed56 TH |
1260 | if (disk_expand_part_tbl(disk, 0)) { |
1261 | free_part_stats(&disk->part0); | |
b5d0b9df TH |
1262 | kfree(disk); |
1263 | return NULL; | |
1da177e4 | 1264 | } |
540eed56 | 1265 | disk->part_tbl->part[0] = &disk->part0; |
6c23a968 | 1266 | |
c83f6bf9 VG |
1267 | /* |
1268 | * set_capacity() and get_capacity() currently don't use | |
1269 | * seqcounter to read/update the part0->nr_sects. Still init | |
1270 | * the counter as we can read the sectors in IO submission | |
1271 | * patch using seqence counters. | |
1272 | * | |
1273 | * TODO: Ideally set_capacity() and get_capacity() should be | |
1274 | * converted to make use of bd_mutex and sequence counters. | |
1275 | */ | |
1276 | seqcount_init(&disk->part0.nr_sects_seq); | |
6c23a968 | 1277 | hd_ref_init(&disk->part0); |
b5d0b9df | 1278 | |
1da177e4 | 1279 | disk->minors = minors; |
1da177e4 | 1280 | rand_initialize_disk(disk); |
ed9e1982 TH |
1281 | disk_to_dev(disk)->class = &block_class; |
1282 | disk_to_dev(disk)->type = &disk_type; | |
1283 | device_initialize(disk_to_dev(disk)); | |
1da177e4 LT |
1284 | } |
1285 | return disk; | |
1286 | } | |
1946089a | 1287 | EXPORT_SYMBOL(alloc_disk_node); |
1da177e4 LT |
1288 | |
1289 | struct kobject *get_disk(struct gendisk *disk) | |
1290 | { | |
1291 | struct module *owner; | |
1292 | struct kobject *kobj; | |
1293 | ||
1294 | if (!disk->fops) | |
1295 | return NULL; | |
1296 | owner = disk->fops->owner; | |
1297 | if (owner && !try_module_get(owner)) | |
1298 | return NULL; | |
ed9e1982 | 1299 | kobj = kobject_get(&disk_to_dev(disk)->kobj); |
1da177e4 LT |
1300 | if (kobj == NULL) { |
1301 | module_put(owner); | |
1302 | return NULL; | |
1303 | } | |
1304 | return kobj; | |
1305 | ||
1306 | } | |
1307 | ||
1308 | EXPORT_SYMBOL(get_disk); | |
1309 | ||
1310 | void put_disk(struct gendisk *disk) | |
1311 | { | |
1312 | if (disk) | |
ed9e1982 | 1313 | kobject_put(&disk_to_dev(disk)->kobj); |
1da177e4 LT |
1314 | } |
1315 | ||
1316 | EXPORT_SYMBOL(put_disk); | |
1317 | ||
e3264a4d HR |
1318 | static void set_disk_ro_uevent(struct gendisk *gd, int ro) |
1319 | { | |
1320 | char event[] = "DISK_RO=1"; | |
1321 | char *envp[] = { event, NULL }; | |
1322 | ||
1323 | if (!ro) | |
1324 | event[8] = '0'; | |
1325 | kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp); | |
1326 | } | |
1327 | ||
1da177e4 LT |
1328 | void set_device_ro(struct block_device *bdev, int flag) |
1329 | { | |
b7db9956 | 1330 | bdev->bd_part->policy = flag; |
1da177e4 LT |
1331 | } |
1332 | ||
1333 | EXPORT_SYMBOL(set_device_ro); | |
1334 | ||
1335 | void set_disk_ro(struct gendisk *disk, int flag) | |
1336 | { | |
e71bf0d0 TH |
1337 | struct disk_part_iter piter; |
1338 | struct hd_struct *part; | |
1339 | ||
e3264a4d HR |
1340 | if (disk->part0.policy != flag) { |
1341 | set_disk_ro_uevent(disk, flag); | |
1342 | disk->part0.policy = flag; | |
1343 | } | |
1344 | ||
1345 | disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY); | |
e71bf0d0 TH |
1346 | while ((part = disk_part_iter_next(&piter))) |
1347 | part->policy = flag; | |
1348 | disk_part_iter_exit(&piter); | |
1da177e4 LT |
1349 | } |
1350 | ||
1351 | EXPORT_SYMBOL(set_disk_ro); | |
1352 | ||
1353 | int bdev_read_only(struct block_device *bdev) | |
1354 | { | |
1355 | if (!bdev) | |
1356 | return 0; | |
b7db9956 | 1357 | return bdev->bd_part->policy; |
1da177e4 LT |
1358 | } |
1359 | ||
1360 | EXPORT_SYMBOL(bdev_read_only); | |
1361 | ||
cf771cb5 | 1362 | int invalidate_partition(struct gendisk *disk, int partno) |
1da177e4 LT |
1363 | { |
1364 | int res = 0; | |
cf771cb5 | 1365 | struct block_device *bdev = bdget_disk(disk, partno); |
1da177e4 | 1366 | if (bdev) { |
2ef41634 | 1367 | fsync_bdev(bdev); |
93b270f7 | 1368 | res = __invalidate_device(bdev, true); |
1da177e4 LT |
1369 | bdput(bdev); |
1370 | } | |
1371 | return res; | |
1372 | } | |
1373 | ||
1374 | EXPORT_SYMBOL(invalidate_partition); | |
77ea887e TH |
1375 | |
1376 | /* | |
1377 | * Disk events - monitor disk events like media change and eject request. | |
1378 | */ | |
1379 | struct disk_events { | |
1380 | struct list_head node; /* all disk_event's */ | |
1381 | struct gendisk *disk; /* the associated disk */ | |
1382 | spinlock_t lock; | |
1383 | ||
fdd514e1 | 1384 | struct mutex block_mutex; /* protects blocking */ |
77ea887e TH |
1385 | int block; /* event blocking depth */ |
1386 | unsigned int pending; /* events already sent out */ | |
1387 | unsigned int clearing; /* events being cleared */ | |
1388 | ||
1389 | long poll_msecs; /* interval, -1 for default */ | |
1390 | struct delayed_work dwork; | |
1391 | }; | |
1392 | ||
1393 | static const char *disk_events_strs[] = { | |
1394 | [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change", | |
1395 | [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request", | |
1396 | }; | |
1397 | ||
1398 | static char *disk_uevents[] = { | |
1399 | [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1", | |
1400 | [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1", | |
1401 | }; | |
1402 | ||
1403 | /* list of all disk_events */ | |
1404 | static DEFINE_MUTEX(disk_events_mutex); | |
1405 | static LIST_HEAD(disk_events); | |
1406 | ||
1407 | /* disable in-kernel polling by default */ | |
1408 | static unsigned long disk_events_dfl_poll_msecs = 0; | |
1409 | ||
1410 | static unsigned long disk_events_poll_jiffies(struct gendisk *disk) | |
1411 | { | |
1412 | struct disk_events *ev = disk->ev; | |
1413 | long intv_msecs = 0; | |
1414 | ||
1415 | /* | |
1416 | * If device-specific poll interval is set, always use it. If | |
1417 | * the default is being used, poll iff there are events which | |
1418 | * can't be monitored asynchronously. | |
1419 | */ | |
1420 | if (ev->poll_msecs >= 0) | |
1421 | intv_msecs = ev->poll_msecs; | |
1422 | else if (disk->events & ~disk->async_events) | |
1423 | intv_msecs = disk_events_dfl_poll_msecs; | |
1424 | ||
1425 | return msecs_to_jiffies(intv_msecs); | |
1426 | } | |
1427 | ||
c3af54af TH |
1428 | /** |
1429 | * disk_block_events - block and flush disk event checking | |
1430 | * @disk: disk to block events for | |
1431 | * | |
1432 | * On return from this function, it is guaranteed that event checking | |
1433 | * isn't in progress and won't happen until unblocked by | |
1434 | * disk_unblock_events(). Events blocking is counted and the actual | |
1435 | * unblocking happens after the matching number of unblocks are done. | |
1436 | * | |
1437 | * Note that this intentionally does not block event checking from | |
1438 | * disk_clear_events(). | |
1439 | * | |
1440 | * CONTEXT: | |
1441 | * Might sleep. | |
1442 | */ | |
1443 | void disk_block_events(struct gendisk *disk) | |
77ea887e TH |
1444 | { |
1445 | struct disk_events *ev = disk->ev; | |
1446 | unsigned long flags; | |
1447 | bool cancel; | |
1448 | ||
c3af54af TH |
1449 | if (!ev) |
1450 | return; | |
1451 | ||
fdd514e1 TH |
1452 | /* |
1453 | * Outer mutex ensures that the first blocker completes canceling | |
1454 | * the event work before further blockers are allowed to finish. | |
1455 | */ | |
1456 | mutex_lock(&ev->block_mutex); | |
1457 | ||
77ea887e TH |
1458 | spin_lock_irqsave(&ev->lock, flags); |
1459 | cancel = !ev->block++; | |
1460 | spin_unlock_irqrestore(&ev->lock, flags); | |
1461 | ||
c3af54af TH |
1462 | if (cancel) |
1463 | cancel_delayed_work_sync(&disk->ev->dwork); | |
fdd514e1 TH |
1464 | |
1465 | mutex_unlock(&ev->block_mutex); | |
77ea887e TH |
1466 | } |
1467 | ||
1468 | static void __disk_unblock_events(struct gendisk *disk, bool check_now) | |
1469 | { | |
1470 | struct disk_events *ev = disk->ev; | |
1471 | unsigned long intv; | |
1472 | unsigned long flags; | |
1473 | ||
1474 | spin_lock_irqsave(&ev->lock, flags); | |
1475 | ||
1476 | if (WARN_ON_ONCE(ev->block <= 0)) | |
1477 | goto out_unlock; | |
1478 | ||
1479 | if (--ev->block) | |
1480 | goto out_unlock; | |
1481 | ||
1482 | /* | |
1483 | * Not exactly a latency critical operation, set poll timer | |
1484 | * slack to 25% and kick event check. | |
1485 | */ | |
1486 | intv = disk_events_poll_jiffies(disk); | |
1487 | set_timer_slack(&ev->dwork.timer, intv / 4); | |
1488 | if (check_now) | |
3b07e9ca | 1489 | queue_delayed_work(system_freezable_wq, &ev->dwork, 0); |
77ea887e | 1490 | else if (intv) |
3b07e9ca | 1491 | queue_delayed_work(system_freezable_wq, &ev->dwork, intv); |
77ea887e TH |
1492 | out_unlock: |
1493 | spin_unlock_irqrestore(&ev->lock, flags); | |
1494 | } | |
1495 | ||
77ea887e TH |
1496 | /** |
1497 | * disk_unblock_events - unblock disk event checking | |
1498 | * @disk: disk to unblock events for | |
1499 | * | |
1500 | * Undo disk_block_events(). When the block count reaches zero, it | |
1501 | * starts events polling if configured. | |
1502 | * | |
1503 | * CONTEXT: | |
1504 | * Don't care. Safe to call from irq context. | |
1505 | */ | |
1506 | void disk_unblock_events(struct gendisk *disk) | |
1507 | { | |
1508 | if (disk->ev) | |
facc31dd | 1509 | __disk_unblock_events(disk, false); |
77ea887e TH |
1510 | } |
1511 | ||
1512 | /** | |
85ef06d1 TH |
1513 | * disk_flush_events - schedule immediate event checking and flushing |
1514 | * @disk: disk to check and flush events for | |
1515 | * @mask: events to flush | |
77ea887e | 1516 | * |
85ef06d1 TH |
1517 | * Schedule immediate event checking on @disk if not blocked. Events in |
1518 | * @mask are scheduled to be cleared from the driver. Note that this | |
1519 | * doesn't clear the events from @disk->ev. | |
77ea887e TH |
1520 | * |
1521 | * CONTEXT: | |
85ef06d1 | 1522 | * If @mask is non-zero must be called with bdev->bd_mutex held. |
77ea887e | 1523 | */ |
85ef06d1 | 1524 | void disk_flush_events(struct gendisk *disk, unsigned int mask) |
77ea887e | 1525 | { |
a9dce2a3 | 1526 | struct disk_events *ev = disk->ev; |
a9dce2a3 TH |
1527 | |
1528 | if (!ev) | |
1529 | return; | |
1530 | ||
85ef06d1 TH |
1531 | spin_lock_irq(&ev->lock); |
1532 | ev->clearing |= mask; | |
41f63c53 | 1533 | if (!ev->block) |
3b07e9ca | 1534 | mod_delayed_work(system_freezable_wq, &ev->dwork, 0); |
85ef06d1 | 1535 | spin_unlock_irq(&ev->lock); |
77ea887e | 1536 | } |
77ea887e TH |
1537 | |
1538 | /** | |
1539 | * disk_clear_events - synchronously check, clear and return pending events | |
1540 | * @disk: disk to fetch and clear events from | |
1541 | * @mask: mask of events to be fetched and clearted | |
1542 | * | |
1543 | * Disk events are synchronously checked and pending events in @mask | |
1544 | * are cleared and returned. This ignores the block count. | |
1545 | * | |
1546 | * CONTEXT: | |
1547 | * Might sleep. | |
1548 | */ | |
1549 | unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask) | |
1550 | { | |
1551 | const struct block_device_operations *bdops = disk->fops; | |
1552 | struct disk_events *ev = disk->ev; | |
1553 | unsigned int pending; | |
12c2bdb2 | 1554 | unsigned int clearing = mask; |
77ea887e TH |
1555 | |
1556 | if (!ev) { | |
1557 | /* for drivers still using the old ->media_changed method */ | |
1558 | if ((mask & DISK_EVENT_MEDIA_CHANGE) && | |
1559 | bdops->media_changed && bdops->media_changed(disk)) | |
1560 | return DISK_EVENT_MEDIA_CHANGE; | |
1561 | return 0; | |
1562 | } | |
1563 | ||
12c2bdb2 DB |
1564 | disk_block_events(disk); |
1565 | ||
1566 | /* | |
1567 | * store the union of mask and ev->clearing on the stack so that the | |
1568 | * race with disk_flush_events does not cause ambiguity (ev->clearing | |
1569 | * can still be modified even if events are blocked). | |
1570 | */ | |
77ea887e | 1571 | spin_lock_irq(&ev->lock); |
12c2bdb2 DB |
1572 | clearing |= ev->clearing; |
1573 | ev->clearing = 0; | |
77ea887e TH |
1574 | spin_unlock_irq(&ev->lock); |
1575 | ||
12c2bdb2 | 1576 | disk_check_events(ev, &clearing); |
aea24a8b | 1577 | /* |
12c2bdb2 DB |
1578 | * if ev->clearing is not 0, the disk_flush_events got called in the |
1579 | * middle of this function, so we want to run the workfn without delay. | |
aea24a8b | 1580 | */ |
12c2bdb2 | 1581 | __disk_unblock_events(disk, ev->clearing ? true : false); |
77ea887e TH |
1582 | |
1583 | /* then, fetch and clear pending events */ | |
1584 | spin_lock_irq(&ev->lock); | |
77ea887e TH |
1585 | pending = ev->pending & mask; |
1586 | ev->pending &= ~mask; | |
1587 | spin_unlock_irq(&ev->lock); | |
12c2bdb2 | 1588 | WARN_ON_ONCE(clearing & mask); |
77ea887e TH |
1589 | |
1590 | return pending; | |
1591 | } | |
1592 | ||
12c2bdb2 DB |
1593 | /* |
1594 | * Separate this part out so that a different pointer for clearing_ptr can be | |
1595 | * passed in for disk_clear_events. | |
1596 | */ | |
77ea887e TH |
1597 | static void disk_events_workfn(struct work_struct *work) |
1598 | { | |
1599 | struct delayed_work *dwork = to_delayed_work(work); | |
1600 | struct disk_events *ev = container_of(dwork, struct disk_events, dwork); | |
12c2bdb2 DB |
1601 | |
1602 | disk_check_events(ev, &ev->clearing); | |
1603 | } | |
1604 | ||
1605 | static void disk_check_events(struct disk_events *ev, | |
1606 | unsigned int *clearing_ptr) | |
1607 | { | |
77ea887e TH |
1608 | struct gendisk *disk = ev->disk; |
1609 | char *envp[ARRAY_SIZE(disk_uevents) + 1] = { }; | |
12c2bdb2 | 1610 | unsigned int clearing = *clearing_ptr; |
77ea887e TH |
1611 | unsigned int events; |
1612 | unsigned long intv; | |
1613 | int nr_events = 0, i; | |
1614 | ||
1615 | /* check events */ | |
1616 | events = disk->fops->check_events(disk, clearing); | |
1617 | ||
1618 | /* accumulate pending events and schedule next poll if necessary */ | |
1619 | spin_lock_irq(&ev->lock); | |
1620 | ||
1621 | events &= ~ev->pending; | |
1622 | ev->pending |= events; | |
12c2bdb2 | 1623 | *clearing_ptr &= ~clearing; |
77ea887e TH |
1624 | |
1625 | intv = disk_events_poll_jiffies(disk); | |
1626 | if (!ev->block && intv) | |
3b07e9ca | 1627 | queue_delayed_work(system_freezable_wq, &ev->dwork, intv); |
77ea887e TH |
1628 | |
1629 | spin_unlock_irq(&ev->lock); | |
1630 | ||
7c88a168 TH |
1631 | /* |
1632 | * Tell userland about new events. Only the events listed in | |
1633 | * @disk->events are reported. Unlisted events are processed the | |
1634 | * same internally but never get reported to userland. | |
1635 | */ | |
77ea887e | 1636 | for (i = 0; i < ARRAY_SIZE(disk_uevents); i++) |
7c88a168 | 1637 | if (events & disk->events & (1 << i)) |
77ea887e TH |
1638 | envp[nr_events++] = disk_uevents[i]; |
1639 | ||
1640 | if (nr_events) | |
1641 | kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp); | |
1642 | } | |
1643 | ||
1644 | /* | |
1645 | * A disk events enabled device has the following sysfs nodes under | |
1646 | * its /sys/block/X/ directory. | |
1647 | * | |
1648 | * events : list of all supported events | |
1649 | * events_async : list of events which can be detected w/o polling | |
1650 | * events_poll_msecs : polling interval, 0: disable, -1: system default | |
1651 | */ | |
1652 | static ssize_t __disk_events_show(unsigned int events, char *buf) | |
1653 | { | |
1654 | const char *delim = ""; | |
1655 | ssize_t pos = 0; | |
1656 | int i; | |
1657 | ||
1658 | for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++) | |
1659 | if (events & (1 << i)) { | |
1660 | pos += sprintf(buf + pos, "%s%s", | |
1661 | delim, disk_events_strs[i]); | |
1662 | delim = " "; | |
1663 | } | |
1664 | if (pos) | |
1665 | pos += sprintf(buf + pos, "\n"); | |
1666 | return pos; | |
1667 | } | |
1668 | ||
1669 | static ssize_t disk_events_show(struct device *dev, | |
1670 | struct device_attribute *attr, char *buf) | |
1671 | { | |
1672 | struct gendisk *disk = dev_to_disk(dev); | |
1673 | ||
1674 | return __disk_events_show(disk->events, buf); | |
1675 | } | |
1676 | ||
1677 | static ssize_t disk_events_async_show(struct device *dev, | |
1678 | struct device_attribute *attr, char *buf) | |
1679 | { | |
1680 | struct gendisk *disk = dev_to_disk(dev); | |
1681 | ||
1682 | return __disk_events_show(disk->async_events, buf); | |
1683 | } | |
1684 | ||
1685 | static ssize_t disk_events_poll_msecs_show(struct device *dev, | |
1686 | struct device_attribute *attr, | |
1687 | char *buf) | |
1688 | { | |
1689 | struct gendisk *disk = dev_to_disk(dev); | |
1690 | ||
1691 | return sprintf(buf, "%ld\n", disk->ev->poll_msecs); | |
1692 | } | |
1693 | ||
1694 | static ssize_t disk_events_poll_msecs_store(struct device *dev, | |
1695 | struct device_attribute *attr, | |
1696 | const char *buf, size_t count) | |
1697 | { | |
1698 | struct gendisk *disk = dev_to_disk(dev); | |
1699 | long intv; | |
1700 | ||
1701 | if (!count || !sscanf(buf, "%ld", &intv)) | |
1702 | return -EINVAL; | |
1703 | ||
1704 | if (intv < 0 && intv != -1) | |
1705 | return -EINVAL; | |
1706 | ||
c3af54af | 1707 | disk_block_events(disk); |
77ea887e TH |
1708 | disk->ev->poll_msecs = intv; |
1709 | __disk_unblock_events(disk, true); | |
1710 | ||
1711 | return count; | |
1712 | } | |
1713 | ||
1714 | static const DEVICE_ATTR(events, S_IRUGO, disk_events_show, NULL); | |
1715 | static const DEVICE_ATTR(events_async, S_IRUGO, disk_events_async_show, NULL); | |
1716 | static const DEVICE_ATTR(events_poll_msecs, S_IRUGO|S_IWUSR, | |
1717 | disk_events_poll_msecs_show, | |
1718 | disk_events_poll_msecs_store); | |
1719 | ||
1720 | static const struct attribute *disk_events_attrs[] = { | |
1721 | &dev_attr_events.attr, | |
1722 | &dev_attr_events_async.attr, | |
1723 | &dev_attr_events_poll_msecs.attr, | |
1724 | NULL, | |
1725 | }; | |
1726 | ||
1727 | /* | |
1728 | * The default polling interval can be specified by the kernel | |
1729 | * parameter block.events_dfl_poll_msecs which defaults to 0 | |
1730 | * (disable). This can also be modified runtime by writing to | |
1731 | * /sys/module/block/events_dfl_poll_msecs. | |
1732 | */ | |
1733 | static int disk_events_set_dfl_poll_msecs(const char *val, | |
1734 | const struct kernel_param *kp) | |
1735 | { | |
1736 | struct disk_events *ev; | |
1737 | int ret; | |
1738 | ||
1739 | ret = param_set_ulong(val, kp); | |
1740 | if (ret < 0) | |
1741 | return ret; | |
1742 | ||
1743 | mutex_lock(&disk_events_mutex); | |
1744 | ||
1745 | list_for_each_entry(ev, &disk_events, node) | |
85ef06d1 | 1746 | disk_flush_events(ev->disk, 0); |
77ea887e TH |
1747 | |
1748 | mutex_unlock(&disk_events_mutex); | |
1749 | ||
1750 | return 0; | |
1751 | } | |
1752 | ||
1753 | static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = { | |
1754 | .set = disk_events_set_dfl_poll_msecs, | |
1755 | .get = param_get_ulong, | |
1756 | }; | |
1757 | ||
1758 | #undef MODULE_PARAM_PREFIX | |
1759 | #define MODULE_PARAM_PREFIX "block." | |
1760 | ||
1761 | module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops, | |
1762 | &disk_events_dfl_poll_msecs, 0644); | |
1763 | ||
1764 | /* | |
9f53d2fe | 1765 | * disk_{alloc|add|del|release}_events - initialize and destroy disk_events. |
77ea887e | 1766 | */ |
9f53d2fe | 1767 | static void disk_alloc_events(struct gendisk *disk) |
77ea887e TH |
1768 | { |
1769 | struct disk_events *ev; | |
1770 | ||
75e3f3ee | 1771 | if (!disk->fops->check_events) |
77ea887e TH |
1772 | return; |
1773 | ||
1774 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); | |
1775 | if (!ev) { | |
1776 | pr_warn("%s: failed to initialize events\n", disk->disk_name); | |
1777 | return; | |
1778 | } | |
1779 | ||
77ea887e TH |
1780 | INIT_LIST_HEAD(&ev->node); |
1781 | ev->disk = disk; | |
1782 | spin_lock_init(&ev->lock); | |
fdd514e1 | 1783 | mutex_init(&ev->block_mutex); |
77ea887e TH |
1784 | ev->block = 1; |
1785 | ev->poll_msecs = -1; | |
1786 | INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn); | |
1787 | ||
9f53d2fe SG |
1788 | disk->ev = ev; |
1789 | } | |
1790 | ||
1791 | static void disk_add_events(struct gendisk *disk) | |
1792 | { | |
1793 | if (!disk->ev) | |
1794 | return; | |
1795 | ||
1796 | /* FIXME: error handling */ | |
1797 | if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0) | |
1798 | pr_warn("%s: failed to create sysfs files for events\n", | |
1799 | disk->disk_name); | |
1800 | ||
77ea887e | 1801 | mutex_lock(&disk_events_mutex); |
9f53d2fe | 1802 | list_add_tail(&disk->ev->node, &disk_events); |
77ea887e TH |
1803 | mutex_unlock(&disk_events_mutex); |
1804 | ||
1805 | /* | |
1806 | * Block count is initialized to 1 and the following initial | |
1807 | * unblock kicks it into action. | |
1808 | */ | |
1809 | __disk_unblock_events(disk, true); | |
1810 | } | |
1811 | ||
1812 | static void disk_del_events(struct gendisk *disk) | |
1813 | { | |
1814 | if (!disk->ev) | |
1815 | return; | |
1816 | ||
c3af54af | 1817 | disk_block_events(disk); |
77ea887e TH |
1818 | |
1819 | mutex_lock(&disk_events_mutex); | |
1820 | list_del_init(&disk->ev->node); | |
1821 | mutex_unlock(&disk_events_mutex); | |
1822 | ||
1823 | sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs); | |
1824 | } | |
1825 | ||
1826 | static void disk_release_events(struct gendisk *disk) | |
1827 | { | |
1828 | /* the block count should be 1 from disk_del_events() */ | |
1829 | WARN_ON_ONCE(disk->ev && disk->ev->block != 1); | |
1830 | kfree(disk->ev); | |
1831 | } |