genhd.c (5c89c2c7fbfa9124dd521c375b9c82b9ed75bc28) genhd.c (d5870edfa3afc4608231267ea3b8e4beb3eab1ee)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * gendisk handling
4 *
5 * Portions Copyright (C) 2020 Christoph Hellwig
6 */
7
8#include <linux/module.h>

--- 19 unchanged lines hidden (view full) ---

28#include "blk.h"
29
30static struct kobject *block_depr;
31
32/* for extended dynamic devt allocation, currently only one major is used */
33#define NR_EXT_DEVT (1 << MINORBITS)
34static DEFINE_IDA(ext_devt_ida);
35
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * gendisk handling
4 *
5 * Portions Copyright (C) 2020 Christoph Hellwig
6 */
7
8#include <linux/module.h>

--- 19 unchanged lines hidden (view full) ---

28#include "blk.h"
29
30static struct kobject *block_depr;
31
32/* for extended dynamic devt allocation, currently only one major is used */
33#define NR_EXT_DEVT (1 << MINORBITS)
34static DEFINE_IDA(ext_devt_ida);
35
36static void disk_check_events(struct disk_events *ev,
37 unsigned int *clearing_ptr);
38static void disk_alloc_events(struct gendisk *disk);
39static void disk_add_events(struct gendisk *disk);
40static void disk_del_events(struct gendisk *disk);
41static void disk_release_events(struct gendisk *disk);
42
43void set_capacity(struct gendisk *disk, sector_t sectors)
44{
45 struct block_device *bdev = disk->part0;
46
47 spin_lock(&bdev->bd_size_lock);
48 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
49 spin_unlock(&bdev->bd_size_lock);
50}

--- 277 unchanged lines hidden (view full) ---

328 low <<= distance; /* swap the positions */
329 high >>= distance;
330 minor |= low | high; /* and set */
331 }
332#endif
333 return minor;
334}
335
36void set_capacity(struct gendisk *disk, sector_t sectors)
37{
38 struct block_device *bdev = disk->part0;
39
40 spin_lock(&bdev->bd_size_lock);
41 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
42 spin_unlock(&bdev->bd_size_lock);
43}

--- 277 unchanged lines hidden (view full) ---

321 low <<= distance; /* swap the positions */
322 high >>= distance;
323 minor |= low | high; /* and set */
324 }
325#endif
326 return minor;
327}
328
336/**
337 * blk_alloc_devt - allocate a dev_t for a block device
338 * @bdev: block device to allocate dev_t for
339 * @devt: out parameter for resulting dev_t
340 *
341 * Allocate a dev_t for block device.
342 *
343 * RETURNS:
344 * 0 on success, allocated dev_t is returned in *@devt. -errno on
345 * failure.
346 *
347 * CONTEXT:
348 * Might sleep.
349 */
350int blk_alloc_devt(struct block_device *bdev, dev_t *devt)
329int blk_alloc_ext_minor(void)
351{
330{
352 struct gendisk *disk = bdev->bd_disk;
353 int idx;
354
331 int idx;
332
355 /* in consecutive minor range? */
356 if (bdev->bd_partno < disk->minors) {
357 *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
358 return 0;
359 }
360
361 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
333 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
362 if (idx < 0)
363 return idx == -ENOSPC ? -EBUSY : idx;
364
365 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
366 return 0;
334 if (idx < 0) {
335 if (idx == -ENOSPC)
336 return -EBUSY;
337 return idx;
338 }
339 return blk_mangle_minor(idx);
367}
368
340}
341
369/**
370 * blk_free_devt - free a dev_t
371 * @devt: dev_t to free
372 *
373 * Free @devt which was allocated using blk_alloc_devt().
374 *
375 * CONTEXT:
376 * Might sleep.
377 */
378void blk_free_devt(dev_t devt)
342void blk_free_ext_minor(unsigned int minor)
379{
343{
380 if (MAJOR(devt) == BLOCK_EXT_MAJOR)
381 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
344 ida_free(&ext_devt_ida, blk_mangle_minor(minor));
382}
383
384static char *bdevt_str(dev_t devt, char *buf)
385{
386 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
387 char tbuf[BDEVT_SIZE];
388 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
389 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);

--- 104 unchanged lines hidden (view full) ---

494 * with the kernel.
495 *
496 * FIXME: error handling
497 */
498static void __device_add_disk(struct device *parent, struct gendisk *disk,
499 const struct attribute_group **groups,
500 bool register_queue)
501{
345}
346
347static char *bdevt_str(dev_t devt, char *buf)
348{
349 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
350 char tbuf[BDEVT_SIZE];
351 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
352 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);

--- 104 unchanged lines hidden (view full) ---

457 * with the kernel.
458 *
459 * FIXME: error handling
460 */
461static void __device_add_disk(struct device *parent, struct gendisk *disk,
462 const struct attribute_group **groups,
463 bool register_queue)
464{
502 dev_t devt;
503 int retval;
465 int ret;
504
505 /*
506 * The disk queue should now be all set with enough information about
507 * the device for the elevator code to pick an adequate default
508 * elevator if one is needed, that is, for devices requesting queue
509 * registration.
510 */
511 if (register_queue)
512 elevator_init_mq(disk->queue);
513
466
467 /*
468 * The disk queue should now be all set with enough information about
469 * the device for the elevator code to pick an adequate default
470 * elevator if one is needed, that is, for devices requesting queue
471 * registration.
472 */
473 if (register_queue)
474 elevator_init_mq(disk->queue);
475
514 /* minors == 0 indicates to use ext devt from part0 and should
515 * be accompanied with EXT_DEVT flag. Make sure all
516 * parameters make sense.
476 /*
477 * If the driver provides an explicit major number it also must provide
478 * the number of minors numbers supported, and those will be used to
479 * setup the gendisk.
480 * Otherwise just allocate the device numbers for both the whole device
481 * and all partitions from the extended dev_t space.
517 */
482 */
518 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
519 WARN_ON(!disk->minors &&
520 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
483 if (disk->major) {
484 WARN_ON(!disk->minors);
521
485
522 disk->flags |= GENHD_FL_UP;
486 if (disk->minors > DISK_MAX_PARTS) {
487 pr_err("block: can't allocate more than %d partitions\n",
488 DISK_MAX_PARTS);
489 disk->minors = DISK_MAX_PARTS;
490 }
491 } else {
492 WARN_ON(disk->minors);
523
493
524 retval = blk_alloc_devt(disk->part0, &devt);
525 if (retval) {
526 WARN_ON(1);
527 return;
494 ret = blk_alloc_ext_minor();
495 if (ret < 0) {
496 WARN_ON(1);
497 return;
498 }
499 disk->major = BLOCK_EXT_MAJOR;
500 disk->first_minor = MINOR(ret);
501 disk->flags |= GENHD_FL_EXT_DEVT;
528 }
502 }
529 disk->major = MAJOR(devt);
530 disk->first_minor = MINOR(devt);
531
503
504 disk->flags |= GENHD_FL_UP;
505
532 disk_alloc_events(disk);
533
534 if (disk->flags & GENHD_FL_HIDDEN) {
535 /*
536 * Don't let hidden disks show up in /proc/partitions,
537 * and don't bother scanning for partitions either.
538 */
539 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
540 disk->flags |= GENHD_FL_NO_PART_SCAN;
541 } else {
542 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
543 struct device *dev = disk_to_dev(disk);
506 disk_alloc_events(disk);
507
508 if (disk->flags & GENHD_FL_HIDDEN) {
509 /*
510 * Don't let hidden disks show up in /proc/partitions,
511 * and don't bother scanning for partitions either.
512 */
513 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
514 disk->flags |= GENHD_FL_NO_PART_SCAN;
515 } else {
516 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
517 struct device *dev = disk_to_dev(disk);
544 int ret;
545
546 /* Register BDI before referencing it from bdev */
518
519 /* Register BDI before referencing it from bdev */
547 dev->devt = devt;
548 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
520 dev->devt = MKDEV(disk->major, disk->first_minor);
521 ret = bdi_register(bdi, "%u:%u",
522 disk->major, disk->first_minor);
549 WARN_ON(ret);
550 bdi_set_owner(bdi, dev);
523 WARN_ON(ret);
524 bdi_set_owner(bdi, dev);
551 bdev_add(disk->part0, devt);
525 bdev_add(disk->part0, dev->devt);
552 }
553 register_disk(parent, disk, groups);
554 if (register_queue)
555 blk_register_queue(disk);
556
557 /*
558 * Take an extra ref on queue which will be put on disk_release()
559 * so that it sticks around as long as @disk is there.
560 */
526 }
527 register_disk(parent, disk, groups);
528 if (register_queue)
529 blk_register_queue(disk);
530
531 /*
532 * Take an extra ref on queue which will be put on disk_release()
533 * so that it sticks around as long as @disk is there.
534 */
561 WARN_ON_ONCE(!blk_get_queue(disk->queue));
535 if (blk_get_queue(disk->queue))
536 set_bit(GD_QUEUE_REF, &disk->state);
537 else
538 WARN_ON_ONCE(1);
562
563 disk_add_events(disk);
564 blk_integrity_add(disk);
565}
566
567void device_add_disk(struct device *parent, struct gendisk *disk,
568 const struct attribute_group **groups)
569

--- 32 unchanged lines hidden (view full) ---

602 might_sleep();
603
604 if (WARN_ON_ONCE(!disk->queue))
605 return;
606
607 blk_integrity_del(disk);
608 disk_del_events(disk);
609
539
540 disk_add_events(disk);
541 blk_integrity_add(disk);
542}
543
544void device_add_disk(struct device *parent, struct gendisk *disk,
545 const struct attribute_group **groups)
546

--- 32 unchanged lines hidden (view full) ---

579 might_sleep();
580
581 if (WARN_ON_ONCE(!disk->queue))
582 return;
583
584 blk_integrity_del(disk);
585 disk_del_events(disk);
586
610 mutex_lock(&disk->part0->bd_mutex);
587 mutex_lock(&disk->open_mutex);
611 disk->flags &= ~GENHD_FL_UP;
612 blk_drop_partitions(disk);
588 disk->flags &= ~GENHD_FL_UP;
589 blk_drop_partitions(disk);
613 mutex_unlock(&disk->part0->bd_mutex);
590 mutex_unlock(&disk->open_mutex);
614
615 fsync_bdev(disk->part0);
616 __invalidate_device(disk->part0, true);
617
618 /*
619 * Unhash the bdev inode for this device so that it can't be looked
620 * up any more even if openers still hold references to it.
621 */

--- 65 unchanged lines hidden (view full) ---

687 }
688 mutex_unlock(&major_names_lock);
689
690 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
691 /* Make old-style 2.4 aliases work */
692 request_module("block-major-%d", MAJOR(devt));
693}
694
591
592 fsync_bdev(disk->part0);
593 __invalidate_device(disk->part0, true);
594
595 /*
596 * Unhash the bdev inode for this device so that it can't be looked
597 * up any more even if openers still hold references to it.
598 */

--- 65 unchanged lines hidden (view full) ---

664 }
665 mutex_unlock(&major_names_lock);
666
667 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
668 /* Make old-style 2.4 aliases work */
669 request_module("block-major-%d", MAJOR(devt));
670}
671
695/**
696 * bdget_disk - do bdget() by gendisk and partition number
697 * @disk: gendisk of interest
698 * @partno: partition number
699 *
700 * Find partition @partno from @disk, do bdget() on it.
701 *
702 * CONTEXT:
703 * Don't care.
704 *
705 * RETURNS:
706 * Resulting block_device on success, NULL on failure.
707 */
708struct block_device *bdget_disk(struct gendisk *disk, int partno)
709{
710 struct block_device *bdev = NULL;
711
712 rcu_read_lock();
713 bdev = xa_load(&disk->part_tbl, partno);
714 if (bdev && !bdgrab(bdev))
715 bdev = NULL;
716 rcu_read_unlock();
717
718 return bdev;
719}
720
721/*
722 * print a full list of all partitions - intended for places where the root
723 * filesystem can't be mounted and thus to give the victim some idea of what
724 * went wrong
725 */
726void __init printk_all_partitions(void)
727{
728 struct class_dev_iter iter;

--- 386 unchanged lines hidden (view full) ---

1115 * Context: can sleep
1116 */
1117static void disk_release(struct device *dev)
1118{
1119 struct gendisk *disk = dev_to_disk(dev);
1120
1121 might_sleep();
1122
672/*
673 * print a full list of all partitions - intended for places where the root
674 * filesystem can't be mounted and thus to give the victim some idea of what
675 * went wrong
676 */
677void __init printk_all_partitions(void)
678{
679 struct class_dev_iter iter;

--- 386 unchanged lines hidden (view full) ---

1066 * Context: can sleep
1067 */
1068static void disk_release(struct device *dev)
1069{
1070 struct gendisk *disk = dev_to_disk(dev);
1071
1072 might_sleep();
1073
1123 blk_free_devt(dev->devt);
1074 if (MAJOR(dev->devt) == BLOCK_EXT_MAJOR)
1075 blk_free_ext_minor(MINOR(dev->devt));
1124 disk_release_events(disk);
1125 kfree(disk->random);
1126 xa_destroy(&disk->part_tbl);
1127 bdput(disk->part0);
1076 disk_release_events(disk);
1077 kfree(disk->random);
1078 xa_destroy(&disk->part_tbl);
1079 bdput(disk->part0);
1128 if (disk->queue)
1080 if (test_bit(GD_QUEUE_REF, &disk->state) && disk->queue)
1129 blk_put_queue(disk->queue);
1130 kfree(disk);
1131}
1132struct class block_class = {
1133 .name = "block",
1134};
1135
1136static char *block_devnode(struct device *dev, umode_t *mode,

--- 100 unchanged lines hidden (view full) ---

1237{
1238 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1239 proc_create_seq("partitions", 0, NULL, &partitions_op);
1240 return 0;
1241}
1242module_init(proc_genhd_init);
1243#endif /* CONFIG_PROC_FS */
1244
1081 blk_put_queue(disk->queue);
1082 kfree(disk);
1083}
1084struct class block_class = {
1085 .name = "block",
1086};
1087
1088static char *block_devnode(struct device *dev, umode_t *mode,

--- 100 unchanged lines hidden (view full) ---

1189{
1190 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1191 proc_create_seq("partitions", 0, NULL, &partitions_op);
1192 return 0;
1193}
1194module_init(proc_genhd_init);
1195#endif /* CONFIG_PROC_FS */
1196
1197dev_t part_devt(struct gendisk *disk, u8 partno)
1198{
1199 struct block_device *part;
1200 dev_t devt = 0;
1201
1202 rcu_read_lock();
1203 part = xa_load(&disk->part_tbl, partno);
1204 if (part)
1205 devt = part->bd_dev;
1206 rcu_read_unlock();
1207
1208 return devt;
1209}
1210
1245dev_t blk_lookup_devt(const char *name, int partno)
1246{
1247 dev_t devt = MKDEV(0, 0);
1248 struct class_dev_iter iter;
1249 struct device *dev;
1250
1251 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1252 while ((dev = class_dev_iter_next(&iter))) {
1253 struct gendisk *disk = dev_to_disk(dev);
1211dev_t blk_lookup_devt(const char *name, int partno)
1212{
1213 dev_t devt = MKDEV(0, 0);
1214 struct class_dev_iter iter;
1215 struct device *dev;
1216
1217 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1218 while ((dev = class_dev_iter_next(&iter))) {
1219 struct gendisk *disk = dev_to_disk(dev);
1254 struct block_device *part;
1255
1256 if (strcmp(dev_name(dev), name))
1257 continue;
1258
1259 if (partno < disk->minors) {
1260 /* We need to return the right devno, even
1261 * if the partition doesn't exist yet.
1262 */
1263 devt = MKDEV(MAJOR(dev->devt),
1264 MINOR(dev->devt) + partno);
1220
1221 if (strcmp(dev_name(dev), name))
1222 continue;
1223
1224 if (partno < disk->minors) {
1225 /* We need to return the right devno, even
1226 * if the partition doesn't exist yet.
1227 */
1228 devt = MKDEV(MAJOR(dev->devt),
1229 MINOR(dev->devt) + partno);
1265 break;
1230 } else {
1231 devt = part_devt(disk, partno);
1232 if (devt)
1233 break;
1266 }
1234 }
1267 part = bdget_disk(disk, partno);
1268 if (part) {
1269 devt = part->bd_dev;
1270 bdput(part);
1271 break;
1272 }
1273 }
1274 class_dev_iter_exit(&iter);
1275 return devt;
1276}
1277
1278struct gendisk *__alloc_disk_node(int minors, int node_id)
1279{
1280 struct gendisk *disk;
1281
1235 }
1236 class_dev_iter_exit(&iter);
1237 return devt;
1238}
1239
1240struct gendisk *__alloc_disk_node(int minors, int node_id)
1241{
1242 struct gendisk *disk;
1243
1282 if (minors > DISK_MAX_PARTS) {
1283 printk(KERN_ERR
1284 "block: can't allocate more than %d partitions\n",
1285 DISK_MAX_PARTS);
1286 minors = DISK_MAX_PARTS;
1287 }
1288
1289 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1290 if (!disk)
1291 return NULL;
1292
1293 disk->part0 = bdev_alloc(disk, 0);
1294 if (!disk->part0)
1295 goto out_free_disk;
1296
1297 disk->node_id = node_id;
1244 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1245 if (!disk)
1246 return NULL;
1247
1248 disk->part0 = bdev_alloc(disk, 0);
1249 if (!disk->part0)
1250 goto out_free_disk;
1251
1252 disk->node_id = node_id;
1253 mutex_init(&disk->open_mutex);
1298 xa_init(&disk->part_tbl);
1299 if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL))
1300 goto out_destroy_part_tbl;
1301
1302 disk->minors = minors;
1303 rand_initialize_disk(disk);
1304 disk_to_dev(disk)->class = &block_class;
1305 disk_to_dev(disk)->type = &disk_type;

--- 4 unchanged lines hidden (view full) ---

1310 xa_destroy(&disk->part_tbl);
1311 bdput(disk->part0);
1312out_free_disk:
1313 kfree(disk);
1314 return NULL;
1315}
1316EXPORT_SYMBOL(__alloc_disk_node);
1317
1254 xa_init(&disk->part_tbl);
1255 if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL))
1256 goto out_destroy_part_tbl;
1257
1258 disk->minors = minors;
1259 rand_initialize_disk(disk);
1260 disk_to_dev(disk)->class = &block_class;
1261 disk_to_dev(disk)->type = &disk_type;

--- 4 unchanged lines hidden (view full) ---

1266 xa_destroy(&disk->part_tbl);
1267 bdput(disk->part0);
1268out_free_disk:
1269 kfree(disk);
1270 return NULL;
1271}
1272EXPORT_SYMBOL(__alloc_disk_node);
1273
1274struct gendisk *__blk_alloc_disk(int node)
1275{
1276 struct request_queue *q;
1277 struct gendisk *disk;
1278
1279 q = blk_alloc_queue(node);
1280 if (!q)
1281 return NULL;
1282
1283 disk = __alloc_disk_node(0, node);
1284 if (!disk) {
1285 blk_cleanup_queue(q);
1286 return NULL;
1287 }
1288 disk->queue = q;
1289 return disk;
1290}
1291EXPORT_SYMBOL(__blk_alloc_disk);
1292
1318/**
1319 * put_disk - decrements the gendisk refcount
1320 * @disk: the struct gendisk to decrement the refcount for
1321 *
1322 * This decrements the refcount for the struct gendisk. When this reaches 0
1323 * we'll have disk_release() called.
1324 *
1325 * Context: Any context, but the last reference must not be dropped from
1326 * atomic context.
1327 */
1328void put_disk(struct gendisk *disk)
1329{
1330 if (disk)
1331 put_device(disk_to_dev(disk));
1332}
1333EXPORT_SYMBOL(put_disk);
1334
1293/**
1294 * put_disk - decrements the gendisk refcount
1295 * @disk: the struct gendisk to decrement the refcount for
1296 *
1297 * This decrements the refcount for the struct gendisk. When this reaches 0
1298 * we'll have disk_release() called.
1299 *
1300 * Context: Any context, but the last reference must not be dropped from
1301 * atomic context.
1302 */
1303void put_disk(struct gendisk *disk)
1304{
1305 if (disk)
1306 put_device(disk_to_dev(disk));
1307}
1308EXPORT_SYMBOL(put_disk);
1309
1310/**
1311 * blk_cleanup_disk - shutdown a gendisk allocated by blk_alloc_disk
1312 * @disk: gendisk to shutdown
1313 *
1314 * Mark the queue hanging off @disk DYING, drain all pending requests, then mark
1315 * the queue DEAD, destroy and put it and the gendisk structure.
1316 *
1317 * Context: can sleep
1318 */
1319void blk_cleanup_disk(struct gendisk *disk)
1320{
1321 blk_cleanup_queue(disk->queue);
1322 put_disk(disk);
1323}
1324EXPORT_SYMBOL(blk_cleanup_disk);
1325
1335static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1336{
1337 char event[] = "DISK_RO=1";
1338 char *envp[] = { event, NULL };
1339
1340 if (!ro)
1341 event[8] = '0';
1342 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);

--- 21 unchanged lines hidden (view full) ---

1364}
1365EXPORT_SYMBOL(set_disk_ro);
1366
1367int bdev_read_only(struct block_device *bdev)
1368{
1369 return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
1370}
1371EXPORT_SYMBOL(bdev_read_only);
1326static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1327{
1328 char event[] = "DISK_RO=1";
1329 char *envp[] = { event, NULL };
1330
1331 if (!ro)
1332 event[8] = '0';
1333 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);

--- 21 unchanged lines hidden (view full) ---

1355}
1356EXPORT_SYMBOL(set_disk_ro);
1357
1358int bdev_read_only(struct block_device *bdev)
1359{
1360 return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
1361}
1362EXPORT_SYMBOL(bdev_read_only);
1372
1373/*
1374 * Disk events - monitor disk events like media change and eject request.
1375 */
1376struct disk_events {
1377 struct list_head node; /* all disk_event's */
1378 struct gendisk *disk; /* the associated disk */
1379 spinlock_t lock;
1380
1381 struct mutex block_mutex; /* protects blocking */
1382 int block; /* event blocking depth */
1383 unsigned int pending; /* events already sent out */
1384 unsigned int clearing; /* events being cleared */
1385
1386 long poll_msecs; /* interval, -1 for default */
1387 struct delayed_work dwork;
1388};
1389
1390static const char *disk_events_strs[] = {
1391 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1392 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1393};
1394
1395static char *disk_uevents[] = {
1396 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1397 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1398};
1399
1400/* list of all disk_events */
1401static DEFINE_MUTEX(disk_events_mutex);
1402static LIST_HEAD(disk_events);
1403
1404/* disable in-kernel polling by default */
1405static unsigned long disk_events_dfl_poll_msecs;
1406
1407static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1408{
1409 struct disk_events *ev = disk->ev;
1410 long intv_msecs = 0;
1411
1412 /*
1413 * If device-specific poll interval is set, always use it. If
1414 * the default is being used, poll if the POLL flag is set.
1415 */
1416 if (ev->poll_msecs >= 0)
1417 intv_msecs = ev->poll_msecs;
1418 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
1419 intv_msecs = disk_events_dfl_poll_msecs;
1420
1421 return msecs_to_jiffies(intv_msecs);
1422}
1423
1424/**
1425 * disk_block_events - block and flush disk event checking
1426 * @disk: disk to block events for
1427 *
1428 * On return from this function, it is guaranteed that event checking
1429 * isn't in progress and won't happen until unblocked by
1430 * disk_unblock_events(). Events blocking is counted and the actual
1431 * unblocking happens after the matching number of unblocks are done.
1432 *
1433 * Note that this intentionally does not block event checking from
1434 * disk_clear_events().
1435 *
1436 * CONTEXT:
1437 * Might sleep.
1438 */
1439void disk_block_events(struct gendisk *disk)
1440{
1441 struct disk_events *ev = disk->ev;
1442 unsigned long flags;
1443 bool cancel;
1444
1445 if (!ev)
1446 return;
1447
1448 /*
1449 * Outer mutex ensures that the first blocker completes canceling
1450 * the event work before further blockers are allowed to finish.
1451 */
1452 mutex_lock(&ev->block_mutex);
1453
1454 spin_lock_irqsave(&ev->lock, flags);
1455 cancel = !ev->block++;
1456 spin_unlock_irqrestore(&ev->lock, flags);
1457
1458 if (cancel)
1459 cancel_delayed_work_sync(&disk->ev->dwork);
1460
1461 mutex_unlock(&ev->block_mutex);
1462}
1463
1464static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1465{
1466 struct disk_events *ev = disk->ev;
1467 unsigned long intv;
1468 unsigned long flags;
1469
1470 spin_lock_irqsave(&ev->lock, flags);
1471
1472 if (WARN_ON_ONCE(ev->block <= 0))
1473 goto out_unlock;
1474
1475 if (--ev->block)
1476 goto out_unlock;
1477
1478 intv = disk_events_poll_jiffies(disk);
1479 if (check_now)
1480 queue_delayed_work(system_freezable_power_efficient_wq,
1481 &ev->dwork, 0);
1482 else if (intv)
1483 queue_delayed_work(system_freezable_power_efficient_wq,
1484 &ev->dwork, intv);
1485out_unlock:
1486 spin_unlock_irqrestore(&ev->lock, flags);
1487}
1488
1489/**
1490 * disk_unblock_events - unblock disk event checking
1491 * @disk: disk to unblock events for
1492 *
1493 * Undo disk_block_events(). When the block count reaches zero, it
1494 * starts events polling if configured.
1495 *
1496 * CONTEXT:
1497 * Don't care. Safe to call from irq context.
1498 */
1499void disk_unblock_events(struct gendisk *disk)
1500{
1501 if (disk->ev)
1502 __disk_unblock_events(disk, false);
1503}
1504
1505/**
1506 * disk_flush_events - schedule immediate event checking and flushing
1507 * @disk: disk to check and flush events for
1508 * @mask: events to flush
1509 *
1510 * Schedule immediate event checking on @disk if not blocked. Events in
1511 * @mask are scheduled to be cleared from the driver. Note that this
1512 * doesn't clear the events from @disk->ev.
1513 *
1514 * CONTEXT:
1515 * If @mask is non-zero must be called with bdev->bd_mutex held.
1516 */
1517void disk_flush_events(struct gendisk *disk, unsigned int mask)
1518{
1519 struct disk_events *ev = disk->ev;
1520
1521 if (!ev)
1522 return;
1523
1524 spin_lock_irq(&ev->lock);
1525 ev->clearing |= mask;
1526 if (!ev->block)
1527 mod_delayed_work(system_freezable_power_efficient_wq,
1528 &ev->dwork, 0);
1529 spin_unlock_irq(&ev->lock);
1530}
1531
1532/**
1533 * disk_clear_events - synchronously check, clear and return pending events
1534 * @disk: disk to fetch and clear events from
1535 * @mask: mask of events to be fetched and cleared
1536 *
1537 * Disk events are synchronously checked and pending events in @mask
1538 * are cleared and returned. This ignores the block count.
1539 *
1540 * CONTEXT:
1541 * Might sleep.
1542 */
1543static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1544{
1545 struct disk_events *ev = disk->ev;
1546 unsigned int pending;
1547 unsigned int clearing = mask;
1548
1549 if (!ev)
1550 return 0;
1551
1552 disk_block_events(disk);
1553
1554 /*
1555 * store the union of mask and ev->clearing on the stack so that the
1556 * race with disk_flush_events does not cause ambiguity (ev->clearing
1557 * can still be modified even if events are blocked).
1558 */
1559 spin_lock_irq(&ev->lock);
1560 clearing |= ev->clearing;
1561 ev->clearing = 0;
1562 spin_unlock_irq(&ev->lock);
1563
1564 disk_check_events(ev, &clearing);
1565 /*
1566 * if ev->clearing is not 0, the disk_flush_events got called in the
1567 * middle of this function, so we want to run the workfn without delay.
1568 */
1569 __disk_unblock_events(disk, ev->clearing ? true : false);
1570
1571 /* then, fetch and clear pending events */
1572 spin_lock_irq(&ev->lock);
1573 pending = ev->pending & mask;
1574 ev->pending &= ~mask;
1575 spin_unlock_irq(&ev->lock);
1576 WARN_ON_ONCE(clearing & mask);
1577
1578 return pending;
1579}
1580
1581/**
1582 * bdev_check_media_change - check if a removable media has been changed
1583 * @bdev: block device to check
1584 *
1585 * Check whether a removable media has been changed, and attempt to free all
1586 * dentries and inodes and invalidates all block device page cache entries in
1587 * that case.
1588 *
1589 * Returns %true if the block device changed, or %false if not.
1590 */
1591bool bdev_check_media_change(struct block_device *bdev)
1592{
1593 unsigned int events;
1594
1595 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1596 DISK_EVENT_EJECT_REQUEST);
1597 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1598 return false;
1599
1600 if (__invalidate_device(bdev, true))
1601 pr_warn("VFS: busy inodes on changed media %s\n",
1602 bdev->bd_disk->disk_name);
1603 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1604 return true;
1605}
1606EXPORT_SYMBOL(bdev_check_media_change);
1607
1608/*
1609 * Separate this part out so that a different pointer for clearing_ptr can be
1610 * passed in for disk_clear_events.
1611 */
1612static void disk_events_workfn(struct work_struct *work)
1613{
1614 struct delayed_work *dwork = to_delayed_work(work);
1615 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
1616
1617 disk_check_events(ev, &ev->clearing);
1618}
1619
1620static void disk_check_events(struct disk_events *ev,
1621 unsigned int *clearing_ptr)
1622{
1623 struct gendisk *disk = ev->disk;
1624 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
1625 unsigned int clearing = *clearing_ptr;
1626 unsigned int events;
1627 unsigned long intv;
1628 int nr_events = 0, i;
1629
1630 /* check events */
1631 events = disk->fops->check_events(disk, clearing);
1632
1633 /* accumulate pending events and schedule next poll if necessary */
1634 spin_lock_irq(&ev->lock);
1635
1636 events &= ~ev->pending;
1637 ev->pending |= events;
1638 *clearing_ptr &= ~clearing;
1639
1640 intv = disk_events_poll_jiffies(disk);
1641 if (!ev->block && intv)
1642 queue_delayed_work(system_freezable_power_efficient_wq,
1643 &ev->dwork, intv);
1644
1645 spin_unlock_irq(&ev->lock);
1646
1647 /*
1648 * Tell userland about new events. Only the events listed in
1649 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1650 * is set. Otherwise, events are processed internally but never
1651 * get reported to userland.
1652 */
1653 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
1654 if ((events & disk->events & (1 << i)) &&
1655 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1656 envp[nr_events++] = disk_uevents[i];
1657
1658 if (nr_events)
1659 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1660}
1661
1662/*
1663 * A disk events enabled device has the following sysfs nodes under
1664 * its /sys/block/X/ directory.
1665 *
1666 * events : list of all supported events
1667 * events_async : list of events which can be detected w/o polling
1668 * (always empty, only for backwards compatibility)
1669 * events_poll_msecs : polling interval, 0: disable, -1: system default
1670 */
1671static ssize_t __disk_events_show(unsigned int events, char *buf)
1672{
1673 const char *delim = "";
1674 ssize_t pos = 0;
1675 int i;
1676
1677 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1678 if (events & (1 << i)) {
1679 pos += sprintf(buf + pos, "%s%s",
1680 delim, disk_events_strs[i]);
1681 delim = " ";
1682 }
1683 if (pos)
1684 pos += sprintf(buf + pos, "\n");
1685 return pos;
1686}
1687
1688static ssize_t disk_events_show(struct device *dev,
1689 struct device_attribute *attr, char *buf)
1690{
1691 struct gendisk *disk = dev_to_disk(dev);
1692
1693 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1694 return 0;
1695
1696 return __disk_events_show(disk->events, buf);
1697}
1698
1699static ssize_t disk_events_async_show(struct device *dev,
1700 struct device_attribute *attr, char *buf)
1701{
1702 return 0;
1703}
1704
1705static ssize_t disk_events_poll_msecs_show(struct device *dev,
1706 struct device_attribute *attr,
1707 char *buf)
1708{
1709 struct gendisk *disk = dev_to_disk(dev);
1710
1711 if (!disk->ev)
1712 return sprintf(buf, "-1\n");
1713
1714 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1715}
1716
1717static ssize_t disk_events_poll_msecs_store(struct device *dev,
1718 struct device_attribute *attr,
1719 const char *buf, size_t count)
1720{
1721 struct gendisk *disk = dev_to_disk(dev);
1722 long intv;
1723
1724 if (!count || !sscanf(buf, "%ld", &intv))
1725 return -EINVAL;
1726
1727 if (intv < 0 && intv != -1)
1728 return -EINVAL;
1729
1730 if (!disk->ev)
1731 return -ENODEV;
1732
1733 disk_block_events(disk);
1734 disk->ev->poll_msecs = intv;
1735 __disk_unblock_events(disk, true);
1736
1737 return count;
1738}
1739
1740static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
1741static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
1742static const DEVICE_ATTR(events_poll_msecs, 0644,
1743 disk_events_poll_msecs_show,
1744 disk_events_poll_msecs_store);
1745
1746static const struct attribute *disk_events_attrs[] = {
1747 &dev_attr_events.attr,
1748 &dev_attr_events_async.attr,
1749 &dev_attr_events_poll_msecs.attr,
1750 NULL,
1751};
1752
1753/*
1754 * The default polling interval can be specified by the kernel
1755 * parameter block.events_dfl_poll_msecs which defaults to 0
1756 * (disable). This can also be modified runtime by writing to
1757 * /sys/module/block/parameters/events_dfl_poll_msecs.
1758 */
1759static int disk_events_set_dfl_poll_msecs(const char *val,
1760 const struct kernel_param *kp)
1761{
1762 struct disk_events *ev;
1763 int ret;
1764
1765 ret = param_set_ulong(val, kp);
1766 if (ret < 0)
1767 return ret;
1768
1769 mutex_lock(&disk_events_mutex);
1770
1771 list_for_each_entry(ev, &disk_events, node)
1772 disk_flush_events(ev->disk, 0);
1773
1774 mutex_unlock(&disk_events_mutex);
1775
1776 return 0;
1777}
1778
1779static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1780 .set = disk_events_set_dfl_poll_msecs,
1781 .get = param_get_ulong,
1782};
1783
1784#undef MODULE_PARAM_PREFIX
1785#define MODULE_PARAM_PREFIX "block."
1786
1787module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1788 &disk_events_dfl_poll_msecs, 0644);
1789
1790/*
1791 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
1792 */
1793static void disk_alloc_events(struct gendisk *disk)
1794{
1795 struct disk_events *ev;
1796
1797 if (!disk->fops->check_events || !disk->events)
1798 return;
1799
1800 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1801 if (!ev) {
1802 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1803 return;
1804 }
1805
1806 INIT_LIST_HEAD(&ev->node);
1807 ev->disk = disk;
1808 spin_lock_init(&ev->lock);
1809 mutex_init(&ev->block_mutex);
1810 ev->block = 1;
1811 ev->poll_msecs = -1;
1812 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
1813
1814 disk->ev = ev;
1815}
1816
1817static void disk_add_events(struct gendisk *disk)
1818{
1819 /* FIXME: error handling */
1820 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
1821 pr_warn("%s: failed to create sysfs files for events\n",
1822 disk->disk_name);
1823
1824 if (!disk->ev)
1825 return;
1826
1827 mutex_lock(&disk_events_mutex);
1828 list_add_tail(&disk->ev->node, &disk_events);
1829 mutex_unlock(&disk_events_mutex);
1830
1831 /*
1832 * Block count is initialized to 1 and the following initial
1833 * unblock kicks it into action.
1834 */
1835 __disk_unblock_events(disk, true);
1836}
1837
1838static void disk_del_events(struct gendisk *disk)
1839{
1840 if (disk->ev) {
1841 disk_block_events(disk);
1842
1843 mutex_lock(&disk_events_mutex);
1844 list_del_init(&disk->ev->node);
1845 mutex_unlock(&disk_events_mutex);
1846 }
1847
1848 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
1849}
1850
1851static void disk_release_events(struct gendisk *disk)
1852{
1853 /* the block count should be 1 from disk_del_events() */
1854 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
1855 kfree(disk->ev);
1856}