Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | #include <linux/genhd.h> |
| 3 | |
| 4 | struct bd_holder_disk { |
| 5 | struct list_head list; |
Christoph Hellwig | 0dbcfe2 | 2021-08-04 11:41:42 +0200 | [diff] [blame] | 6 | struct block_device *bdev; |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 7 | int refcnt; |
| 8 | }; |
| 9 | |
| 10 | static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev, |
| 11 | struct gendisk *disk) |
| 12 | { |
| 13 | struct bd_holder_disk *holder; |
| 14 | |
Christoph Hellwig | 0dbcfe2 | 2021-08-04 11:41:42 +0200 | [diff] [blame] | 15 | list_for_each_entry(holder, &disk->slave_bdevs, list) |
| 16 | if (holder->bdev == bdev) |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 17 | return holder; |
| 18 | return NULL; |
| 19 | } |
| 20 | |
| 21 | static int add_symlink(struct kobject *from, struct kobject *to) |
| 22 | { |
| 23 | return sysfs_create_link(from, to, kobject_name(to)); |
| 24 | } |
| 25 | |
| 26 | static void del_symlink(struct kobject *from, struct kobject *to) |
| 27 | { |
| 28 | sysfs_remove_link(from, kobject_name(to)); |
| 29 | } |
| 30 | |
Christoph Hellwig | d626338 | 2021-08-04 11:41:43 +0200 | [diff] [blame] | 31 | static int __link_disk_holder(struct block_device *bdev, struct gendisk *disk) |
| 32 | { |
| 33 | int ret; |
| 34 | |
| 35 | ret = add_symlink(disk->slave_dir, bdev_kobj(bdev)); |
| 36 | if (ret) |
| 37 | return ret; |
| 38 | ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); |
| 39 | if (ret) |
| 40 | del_symlink(disk->slave_dir, bdev_kobj(bdev)); |
| 41 | return ret; |
| 42 | } |
| 43 | |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 44 | /** |
| 45 | * bd_link_disk_holder - create symlinks between holding disk and slave bdev |
| 46 | * @bdev: the claimed slave bdev |
| 47 | * @disk: the holding disk |
| 48 | * |
| 49 | * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT. |
| 50 | * |
| 51 | * This functions creates the following sysfs symlinks. |
| 52 | * |
| 53 | * - from "slaves" directory of the holder @disk to the claimed @bdev |
| 54 | * - from "holders" directory of the @bdev to the holder @disk |
| 55 | * |
| 56 | * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is |
| 57 | * passed to bd_link_disk_holder(), then: |
| 58 | * |
| 59 | * /sys/block/dm-0/slaves/sda --> /sys/block/sda |
| 60 | * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0 |
| 61 | * |
| 62 | * The caller must have claimed @bdev before calling this function and |
| 63 | * ensure that both @bdev and @disk are valid during the creation and |
| 64 | * lifetime of these symlinks. |
| 65 | * |
| 66 | * CONTEXT: |
| 67 | * Might sleep. |
| 68 | * |
| 69 | * RETURNS: |
| 70 | * 0 on success, -errno on failure. |
| 71 | */ |
| 72 | int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) |
| 73 | { |
| 74 | struct bd_holder_disk *holder; |
| 75 | int ret = 0; |
| 76 | |
Christoph Hellwig | 0dbcfe2 | 2021-08-04 11:41:42 +0200 | [diff] [blame] | 77 | mutex_lock(&disk->open_mutex); |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 78 | |
| 79 | WARN_ON_ONCE(!bdev->bd_holder); |
| 80 | |
| 81 | /* FIXME: remove the following once add_disk() handles errors */ |
Christoph Hellwig | d626338 | 2021-08-04 11:41:43 +0200 | [diff] [blame] | 82 | if (WARN_ON(!bdev->bd_holder_dir)) |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 83 | goto out_unlock; |
| 84 | |
| 85 | holder = bd_find_holder_disk(bdev, disk); |
| 86 | if (holder) { |
| 87 | holder->refcnt++; |
| 88 | goto out_unlock; |
| 89 | } |
| 90 | |
| 91 | holder = kzalloc(sizeof(*holder), GFP_KERNEL); |
| 92 | if (!holder) { |
| 93 | ret = -ENOMEM; |
| 94 | goto out_unlock; |
| 95 | } |
| 96 | |
| 97 | INIT_LIST_HEAD(&holder->list); |
Christoph Hellwig | 0dbcfe2 | 2021-08-04 11:41:42 +0200 | [diff] [blame] | 98 | holder->bdev = bdev; |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 99 | holder->refcnt = 1; |
Christoph Hellwig | d626338 | 2021-08-04 11:41:43 +0200 | [diff] [blame] | 100 | if (disk->slave_dir) { |
| 101 | ret = __link_disk_holder(bdev, disk); |
| 102 | if (ret) { |
| 103 | kfree(holder); |
| 104 | goto out_unlock; |
| 105 | } |
| 106 | } |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 107 | |
Christoph Hellwig | 0dbcfe2 | 2021-08-04 11:41:42 +0200 | [diff] [blame] | 108 | list_add(&holder->list, &disk->slave_bdevs); |
Christoph Hellwig | 759e0fd | 2021-08-20 11:49:29 +0200 | [diff] [blame] | 109 | /* |
| 110 | * del_gendisk drops the initial reference to bd_holder_dir, so we need |
| 111 | * to keep our own here to allow for cleanup past that point. |
| 112 | */ |
| 113 | kobject_get(bdev->bd_holder_dir); |
| 114 | |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 115 | out_unlock: |
Christoph Hellwig | 0dbcfe2 | 2021-08-04 11:41:42 +0200 | [diff] [blame] | 116 | mutex_unlock(&disk->open_mutex); |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 117 | return ret; |
| 118 | } |
| 119 | EXPORT_SYMBOL_GPL(bd_link_disk_holder); |
| 120 | |
Christoph Hellwig | d626338 | 2021-08-04 11:41:43 +0200 | [diff] [blame] | 121 | static void __unlink_disk_holder(struct block_device *bdev, |
| 122 | struct gendisk *disk) |
| 123 | { |
| 124 | del_symlink(disk->slave_dir, bdev_kobj(bdev)); |
| 125 | del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); |
| 126 | } |
| 127 | |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 128 | /** |
| 129 | * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder() |
| 130 | * @bdev: the calimed slave bdev |
| 131 | * @disk: the holding disk |
| 132 | * |
| 133 | * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT. |
| 134 | * |
| 135 | * CONTEXT: |
| 136 | * Might sleep. |
| 137 | */ |
| 138 | void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk) |
| 139 | { |
| 140 | struct bd_holder_disk *holder; |
| 141 | |
Christoph Hellwig | 0dbcfe2 | 2021-08-04 11:41:42 +0200 | [diff] [blame] | 142 | mutex_lock(&disk->open_mutex); |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 143 | holder = bd_find_holder_disk(bdev, disk); |
| 144 | if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) { |
Christoph Hellwig | d626338 | 2021-08-04 11:41:43 +0200 | [diff] [blame] | 145 | if (disk->slave_dir) |
| 146 | __unlink_disk_holder(bdev, disk); |
Christoph Hellwig | 759e0fd | 2021-08-20 11:49:29 +0200 | [diff] [blame] | 147 | kobject_put(bdev->bd_holder_dir); |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 148 | list_del_init(&holder->list); |
| 149 | kfree(holder); |
| 150 | } |
Christoph Hellwig | 0dbcfe2 | 2021-08-04 11:41:42 +0200 | [diff] [blame] | 151 | mutex_unlock(&disk->open_mutex); |
Christoph Hellwig | c66fd01 | 2021-08-04 11:41:40 +0200 | [diff] [blame] | 152 | } |
| 153 | EXPORT_SYMBOL_GPL(bd_unlink_disk_holder); |
Christoph Hellwig | d626338 | 2021-08-04 11:41:43 +0200 | [diff] [blame] | 154 | |
| 155 | int bd_register_pending_holders(struct gendisk *disk) |
| 156 | { |
| 157 | struct bd_holder_disk *holder; |
| 158 | int ret; |
| 159 | |
| 160 | mutex_lock(&disk->open_mutex); |
| 161 | list_for_each_entry(holder, &disk->slave_bdevs, list) { |
| 162 | ret = __link_disk_holder(holder->bdev, disk); |
| 163 | if (ret) |
| 164 | goto out_undo; |
| 165 | } |
| 166 | mutex_unlock(&disk->open_mutex); |
| 167 | return 0; |
| 168 | |
| 169 | out_undo: |
| 170 | list_for_each_entry_continue_reverse(holder, &disk->slave_bdevs, list) |
| 171 | __unlink_disk_holder(holder->bdev, disk); |
| 172 | mutex_unlock(&disk->open_mutex); |
| 173 | return ret; |
| 174 | } |