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