Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 3 | * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
| 8 | #include "dm.h" |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/vmalloc.h> |
| 12 | #include <linux/miscdevice.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/wait.h> |
| 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/dm-ioctl.h> |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 17 | #include <linux/hdreg.h> |
Milan Broz | 76c072b | 2008-02-08 02:09:56 +0000 | [diff] [blame] | 18 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <asm/uaccess.h> |
| 21 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 22 | #define DM_MSG_PREFIX "ioctl" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #define DM_DRIVER_EMAIL "dm-devel@redhat.com" |
| 24 | |
| 25 | /*----------------------------------------------------------------- |
| 26 | * The ioctl interface needs to be able to look up devices by |
| 27 | * name or uuid. |
| 28 | *---------------------------------------------------------------*/ |
| 29 | struct hash_cell { |
| 30 | struct list_head name_list; |
| 31 | struct list_head uuid_list; |
| 32 | |
| 33 | char *name; |
| 34 | char *uuid; |
| 35 | struct mapped_device *md; |
| 36 | struct dm_table *new_map; |
| 37 | }; |
| 38 | |
| 39 | struct vers_iter { |
| 40 | size_t param_size; |
| 41 | struct dm_target_versions *vers, *old_vers; |
| 42 | char *end; |
| 43 | uint32_t flags; |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | #define NUM_BUCKETS 64 |
| 48 | #define MASK_BUCKETS (NUM_BUCKETS - 1) |
| 49 | static struct list_head _name_buckets[NUM_BUCKETS]; |
| 50 | static struct list_head _uuid_buckets[NUM_BUCKETS]; |
| 51 | |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 52 | static void dm_hash_remove_all(int keep_open_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Guards access to both hash tables. |
| 56 | */ |
| 57 | static DECLARE_RWSEM(_hash_lock); |
| 58 | |
Mikulas Patocka | 6076905 | 2009-12-10 23:51:52 +0000 | [diff] [blame] | 59 | /* |
| 60 | * Protects use of mdptr to obtain hash cell name and uuid from mapped device. |
| 61 | */ |
| 62 | static DEFINE_MUTEX(dm_hash_cells_mutex); |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static void init_buckets(struct list_head *buckets) |
| 65 | { |
| 66 | unsigned int i; |
| 67 | |
| 68 | for (i = 0; i < NUM_BUCKETS; i++) |
| 69 | INIT_LIST_HEAD(buckets + i); |
| 70 | } |
| 71 | |
| 72 | static int dm_hash_init(void) |
| 73 | { |
| 74 | init_buckets(_name_buckets); |
| 75 | init_buckets(_uuid_buckets); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static void dm_hash_exit(void) |
| 80 | { |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 81 | dm_hash_remove_all(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /*----------------------------------------------------------------- |
| 85 | * Hash function: |
| 86 | * We're not really concerned with the str hash function being |
| 87 | * fast since it's only used by the ioctl interface. |
| 88 | *---------------------------------------------------------------*/ |
| 89 | static unsigned int hash_str(const char *str) |
| 90 | { |
| 91 | const unsigned int hash_mult = 2654435387U; |
| 92 | unsigned int h = 0; |
| 93 | |
| 94 | while (*str) |
| 95 | h = (h + (unsigned int) *str++) * hash_mult; |
| 96 | |
| 97 | return h & MASK_BUCKETS; |
| 98 | } |
| 99 | |
| 100 | /*----------------------------------------------------------------- |
| 101 | * Code for looking up a device by name |
| 102 | *---------------------------------------------------------------*/ |
| 103 | static struct hash_cell *__get_name_cell(const char *str) |
| 104 | { |
| 105 | struct hash_cell *hc; |
| 106 | unsigned int h = hash_str(str); |
| 107 | |
| 108 | list_for_each_entry (hc, _name_buckets + h, name_list) |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 109 | if (!strcmp(hc->name, str)) { |
| 110 | dm_get(hc->md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return hc; |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | static struct hash_cell *__get_uuid_cell(const char *str) |
| 118 | { |
| 119 | struct hash_cell *hc; |
| 120 | unsigned int h = hash_str(str); |
| 121 | |
| 122 | list_for_each_entry (hc, _uuid_buckets + h, uuid_list) |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 123 | if (!strcmp(hc->uuid, str)) { |
| 124 | dm_get(hc->md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return hc; |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | /*----------------------------------------------------------------- |
| 132 | * Inserting, removing and renaming a device. |
| 133 | *---------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | static struct hash_cell *alloc_cell(const char *name, const char *uuid, |
| 135 | struct mapped_device *md) |
| 136 | { |
| 137 | struct hash_cell *hc; |
| 138 | |
| 139 | hc = kmalloc(sizeof(*hc), GFP_KERNEL); |
| 140 | if (!hc) |
| 141 | return NULL; |
| 142 | |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 143 | hc->name = kstrdup(name, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | if (!hc->name) { |
| 145 | kfree(hc); |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | if (!uuid) |
| 150 | hc->uuid = NULL; |
| 151 | |
| 152 | else { |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 153 | hc->uuid = kstrdup(uuid, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | if (!hc->uuid) { |
| 155 | kfree(hc->name); |
| 156 | kfree(hc); |
| 157 | return NULL; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | INIT_LIST_HEAD(&hc->name_list); |
| 162 | INIT_LIST_HEAD(&hc->uuid_list); |
| 163 | hc->md = md; |
| 164 | hc->new_map = NULL; |
| 165 | return hc; |
| 166 | } |
| 167 | |
| 168 | static void free_cell(struct hash_cell *hc) |
| 169 | { |
| 170 | if (hc) { |
| 171 | kfree(hc->name); |
| 172 | kfree(hc->uuid); |
| 173 | kfree(hc); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | * The kdev_t and uuid of a device can never change once it is |
| 179 | * initially inserted. |
| 180 | */ |
| 181 | static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md) |
| 182 | { |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 183 | struct hash_cell *cell, *hc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * Allocate the new cells. |
| 187 | */ |
| 188 | cell = alloc_cell(name, uuid, md); |
| 189 | if (!cell) |
| 190 | return -ENOMEM; |
| 191 | |
| 192 | /* |
| 193 | * Insert the cell into both hash tables. |
| 194 | */ |
| 195 | down_write(&_hash_lock); |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 196 | hc = __get_name_cell(name); |
| 197 | if (hc) { |
| 198 | dm_put(hc->md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | goto bad; |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | list_add(&cell->name_list, _name_buckets + hash_str(name)); |
| 203 | |
| 204 | if (uuid) { |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 205 | hc = __get_uuid_cell(uuid); |
| 206 | if (hc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | list_del(&cell->name_list); |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 208 | dm_put(hc->md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | goto bad; |
| 210 | } |
| 211 | list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid)); |
| 212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | dm_get(md); |
Mikulas Patocka | 6076905 | 2009-12-10 23:51:52 +0000 | [diff] [blame] | 214 | mutex_lock(&dm_hash_cells_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | dm_set_mdptr(md, cell); |
Mikulas Patocka | 6076905 | 2009-12-10 23:51:52 +0000 | [diff] [blame] | 216 | mutex_unlock(&dm_hash_cells_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | up_write(&_hash_lock); |
| 218 | |
| 219 | return 0; |
| 220 | |
| 221 | bad: |
| 222 | up_write(&_hash_lock); |
| 223 | free_cell(cell); |
| 224 | return -EBUSY; |
| 225 | } |
| 226 | |
| 227 | static void __hash_remove(struct hash_cell *hc) |
| 228 | { |
goggin, edward | 269fd2a | 2005-09-27 21:45:44 -0700 | [diff] [blame] | 229 | struct dm_table *table; |
| 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | /* remove from the dev hash */ |
| 232 | list_del(&hc->uuid_list); |
| 233 | list_del(&hc->name_list); |
Mikulas Patocka | 6076905 | 2009-12-10 23:51:52 +0000 | [diff] [blame] | 234 | mutex_lock(&dm_hash_cells_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | dm_set_mdptr(hc->md, NULL); |
Mikulas Patocka | 6076905 | 2009-12-10 23:51:52 +0000 | [diff] [blame] | 236 | mutex_unlock(&dm_hash_cells_mutex); |
goggin, edward | 269fd2a | 2005-09-27 21:45:44 -0700 | [diff] [blame] | 237 | |
Alasdair G Kergon | 7c66641 | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 238 | table = dm_get_live_table(hc->md); |
goggin, edward | 269fd2a | 2005-09-27 21:45:44 -0700 | [diff] [blame] | 239 | if (table) { |
| 240 | dm_table_event(table); |
| 241 | dm_table_put(table); |
| 242 | } |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | if (hc->new_map) |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 245 | dm_table_destroy(hc->new_map); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 246 | dm_put(hc->md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | free_cell(hc); |
| 248 | } |
| 249 | |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 250 | static void dm_hash_remove_all(int keep_open_devices) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 252 | int i, dev_skipped, dev_removed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | struct hash_cell *hc; |
| 254 | struct list_head *tmp, *n; |
| 255 | |
| 256 | down_write(&_hash_lock); |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 257 | |
| 258 | retry: |
| 259 | dev_skipped = dev_removed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | for (i = 0; i < NUM_BUCKETS; i++) { |
| 261 | list_for_each_safe (tmp, n, _name_buckets + i) { |
| 262 | hc = list_entry(tmp, struct hash_cell, name_list); |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 263 | |
| 264 | if (keep_open_devices && |
| 265 | dm_lock_for_deletion(hc->md)) { |
| 266 | dev_skipped++; |
| 267 | continue; |
| 268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | __hash_remove(hc); |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 270 | dev_removed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | * Some mapped devices may be using other mapped devices, so if any |
| 276 | * still exist, repeat until we make no further progress. |
| 277 | */ |
| 278 | if (dev_skipped) { |
| 279 | if (dev_removed) |
| 280 | goto retry; |
| 281 | |
| 282 | DMWARN("remove_all left %d open device(s)", dev_skipped); |
| 283 | } |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | up_write(&_hash_lock); |
| 286 | } |
| 287 | |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 288 | static int dm_hash_rename(uint32_t cookie, const char *old, const char *new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | char *new_name, *old_name; |
| 291 | struct hash_cell *hc; |
goggin, edward | 81f1777 | 2006-01-06 00:20:01 -0800 | [diff] [blame] | 292 | struct dm_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | /* |
| 295 | * duplicate new. |
| 296 | */ |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 297 | new_name = kstrdup(new, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | if (!new_name) |
| 299 | return -ENOMEM; |
| 300 | |
| 301 | down_write(&_hash_lock); |
| 302 | |
| 303 | /* |
| 304 | * Is new free ? |
| 305 | */ |
| 306 | hc = __get_name_cell(new); |
| 307 | if (hc) { |
| 308 | DMWARN("asked to rename to an already existing name %s -> %s", |
| 309 | old, new); |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 310 | dm_put(hc->md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | up_write(&_hash_lock); |
| 312 | kfree(new_name); |
| 313 | return -EBUSY; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Is there such a device as 'old' ? |
| 318 | */ |
| 319 | hc = __get_name_cell(old); |
| 320 | if (!hc) { |
| 321 | DMWARN("asked to rename a non existent device %s -> %s", |
| 322 | old, new); |
| 323 | up_write(&_hash_lock); |
| 324 | kfree(new_name); |
| 325 | return -ENXIO; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * rename and move the name cell. |
| 330 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | list_del(&hc->name_list); |
| 332 | old_name = hc->name; |
Mikulas Patocka | 6076905 | 2009-12-10 23:51:52 +0000 | [diff] [blame] | 333 | mutex_lock(&dm_hash_cells_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | hc->name = new_name; |
Mikulas Patocka | 6076905 | 2009-12-10 23:51:52 +0000 | [diff] [blame] | 335 | mutex_unlock(&dm_hash_cells_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | list_add(&hc->name_list, _name_buckets + hash_str(new_name)); |
| 337 | |
goggin, edward | 81f1777 | 2006-01-06 00:20:01 -0800 | [diff] [blame] | 338 | /* |
| 339 | * Wake up any dm event waiters. |
| 340 | */ |
Alasdair G Kergon | 7c66641 | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 341 | table = dm_get_live_table(hc->md); |
goggin, edward | 81f1777 | 2006-01-06 00:20:01 -0800 | [diff] [blame] | 342 | if (table) { |
| 343 | dm_table_event(table); |
| 344 | dm_table_put(table); |
| 345 | } |
| 346 | |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 347 | dm_kobject_uevent(hc->md, KOBJ_CHANGE, cookie); |
Alasdair G Kergon | 69267a3 | 2007-12-13 14:15:57 +0000 | [diff] [blame] | 348 | |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 349 | dm_put(hc->md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | up_write(&_hash_lock); |
| 351 | kfree(old_name); |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | /*----------------------------------------------------------------- |
| 356 | * Implementation of the ioctl commands |
| 357 | *---------------------------------------------------------------*/ |
| 358 | /* |
| 359 | * All the ioctl commands get dispatched to functions with this |
| 360 | * prototype. |
| 361 | */ |
| 362 | typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size); |
| 363 | |
| 364 | static int remove_all(struct dm_ioctl *param, size_t param_size) |
| 365 | { |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 366 | dm_hash_remove_all(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | param->data_size = 0; |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Round up the ptr to an 8-byte boundary. |
| 373 | */ |
| 374 | #define ALIGN_MASK 7 |
| 375 | static inline void *align_ptr(void *ptr) |
| 376 | { |
| 377 | return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK); |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Retrieves the data payload buffer from an already allocated |
| 382 | * struct dm_ioctl. |
| 383 | */ |
| 384 | static void *get_result_buffer(struct dm_ioctl *param, size_t param_size, |
| 385 | size_t *len) |
| 386 | { |
| 387 | param->data_start = align_ptr(param + 1) - (void *) param; |
| 388 | |
| 389 | if (param->data_start < param_size) |
| 390 | *len = param_size - param->data_start; |
| 391 | else |
| 392 | *len = 0; |
| 393 | |
| 394 | return ((void *) param) + param->data_start; |
| 395 | } |
| 396 | |
| 397 | static int list_devices(struct dm_ioctl *param, size_t param_size) |
| 398 | { |
| 399 | unsigned int i; |
| 400 | struct hash_cell *hc; |
| 401 | size_t len, needed = 0; |
| 402 | struct gendisk *disk; |
| 403 | struct dm_name_list *nl, *old_nl = NULL; |
| 404 | |
| 405 | down_write(&_hash_lock); |
| 406 | |
| 407 | /* |
| 408 | * Loop through all the devices working out how much |
| 409 | * space we need. |
| 410 | */ |
| 411 | for (i = 0; i < NUM_BUCKETS; i++) { |
| 412 | list_for_each_entry (hc, _name_buckets + i, name_list) { |
| 413 | needed += sizeof(struct dm_name_list); |
| 414 | needed += strlen(hc->name) + 1; |
| 415 | needed += ALIGN_MASK; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Grab our output buffer. |
| 421 | */ |
| 422 | nl = get_result_buffer(param, param_size, &len); |
| 423 | if (len < needed) { |
| 424 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 425 | goto out; |
| 426 | } |
| 427 | param->data_size = param->data_start + needed; |
| 428 | |
| 429 | nl->dev = 0; /* Flags no data */ |
| 430 | |
| 431 | /* |
| 432 | * Now loop through filling out the names. |
| 433 | */ |
| 434 | for (i = 0; i < NUM_BUCKETS; i++) { |
| 435 | list_for_each_entry (hc, _name_buckets + i, name_list) { |
| 436 | if (old_nl) |
| 437 | old_nl->next = (uint32_t) ((void *) nl - |
| 438 | (void *) old_nl); |
| 439 | disk = dm_disk(hc->md); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 440 | nl->dev = huge_encode_dev(disk_devt(disk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | nl->next = 0; |
| 442 | strcpy(nl->name, hc->name); |
| 443 | |
| 444 | old_nl = nl; |
| 445 | nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | out: |
| 450 | up_write(&_hash_lock); |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static void list_version_get_needed(struct target_type *tt, void *needed_param) |
| 455 | { |
| 456 | size_t *needed = needed_param; |
| 457 | |
Alasdair G Kergon | c4cc663 | 2005-11-21 21:32:33 -0800 | [diff] [blame] | 458 | *needed += sizeof(struct dm_target_versions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | *needed += strlen(tt->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | *needed += ALIGN_MASK; |
| 461 | } |
| 462 | |
| 463 | static void list_version_get_info(struct target_type *tt, void *param) |
| 464 | { |
| 465 | struct vers_iter *info = param; |
| 466 | |
| 467 | /* Check space - it might have changed since the first iteration */ |
| 468 | if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 > |
| 469 | info->end) { |
| 470 | |
| 471 | info->flags = DM_BUFFER_FULL_FLAG; |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | if (info->old_vers) |
| 476 | info->old_vers->next = (uint32_t) ((void *)info->vers - |
| 477 | (void *)info->old_vers); |
| 478 | info->vers->version[0] = tt->version[0]; |
| 479 | info->vers->version[1] = tt->version[1]; |
| 480 | info->vers->version[2] = tt->version[2]; |
| 481 | info->vers->next = 0; |
| 482 | strcpy(info->vers->name, tt->name); |
| 483 | |
| 484 | info->old_vers = info->vers; |
| 485 | info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1); |
| 486 | } |
| 487 | |
| 488 | static int list_versions(struct dm_ioctl *param, size_t param_size) |
| 489 | { |
| 490 | size_t len, needed = 0; |
| 491 | struct dm_target_versions *vers; |
| 492 | struct vers_iter iter_info; |
| 493 | |
| 494 | /* |
| 495 | * Loop through all the devices working out how much |
| 496 | * space we need. |
| 497 | */ |
| 498 | dm_target_iterate(list_version_get_needed, &needed); |
| 499 | |
| 500 | /* |
| 501 | * Grab our output buffer. |
| 502 | */ |
| 503 | vers = get_result_buffer(param, param_size, &len); |
| 504 | if (len < needed) { |
| 505 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 506 | goto out; |
| 507 | } |
| 508 | param->data_size = param->data_start + needed; |
| 509 | |
| 510 | iter_info.param_size = param_size; |
| 511 | iter_info.old_vers = NULL; |
| 512 | iter_info.vers = vers; |
| 513 | iter_info.flags = 0; |
| 514 | iter_info.end = (char *)vers+len; |
| 515 | |
| 516 | /* |
| 517 | * Now loop through filling out the names & versions. |
| 518 | */ |
| 519 | dm_target_iterate(list_version_get_info, &iter_info); |
| 520 | param->flags |= iter_info.flags; |
| 521 | |
| 522 | out: |
| 523 | return 0; |
| 524 | } |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | static int check_name(const char *name) |
| 527 | { |
| 528 | if (strchr(name, '/')) { |
| 529 | DMWARN("invalid device name"); |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | /* |
Mike Snitzer | 1d0f3ce | 2009-12-10 23:52:22 +0000 | [diff] [blame] | 537 | * On successful return, the caller must not attempt to acquire |
| 538 | * _hash_lock without first calling dm_table_put, because dm_table_destroy |
| 539 | * waits for this dm_table_put and could be called under this lock. |
| 540 | */ |
| 541 | static struct dm_table *dm_get_inactive_table(struct mapped_device *md) |
| 542 | { |
| 543 | struct hash_cell *hc; |
| 544 | struct dm_table *table = NULL; |
| 545 | |
| 546 | down_read(&_hash_lock); |
| 547 | hc = dm_get_mdptr(md); |
| 548 | if (!hc || hc->md != md) { |
| 549 | DMWARN("device has been removed from the dev hash table."); |
| 550 | goto out; |
| 551 | } |
| 552 | |
| 553 | table = hc->new_map; |
| 554 | if (table) |
| 555 | dm_table_get(table); |
| 556 | |
| 557 | out: |
| 558 | up_read(&_hash_lock); |
| 559 | |
| 560 | return table; |
| 561 | } |
| 562 | |
| 563 | static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md, |
| 564 | struct dm_ioctl *param) |
| 565 | { |
| 566 | return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ? |
| 567 | dm_get_inactive_table(md) : dm_get_live_table(md); |
| 568 | } |
| 569 | |
| 570 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | * Fills in a dm_ioctl structure, ready for sending back to |
| 572 | * userland. |
| 573 | */ |
| 574 | static int __dev_status(struct mapped_device *md, struct dm_ioctl *param) |
| 575 | { |
| 576 | struct gendisk *disk = dm_disk(md); |
| 577 | struct dm_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG | |
| 580 | DM_ACTIVE_PRESENT_FLAG); |
| 581 | |
Kiyoshi Ueda | 4f186f8 | 2009-12-10 23:52:26 +0000 | [diff] [blame] | 582 | if (dm_suspended_md(md)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | param->flags |= DM_SUSPEND_FLAG; |
| 584 | |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 585 | param->dev = huge_encode_dev(disk_devt(disk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 587 | /* |
| 588 | * Yes, this will be out of date by the time it gets back |
| 589 | * to userland, but it is still very useful for |
| 590 | * debugging. |
| 591 | */ |
| 592 | param->open_count = dm_open_count(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | param->event_nr = dm_get_event_nr(md); |
Mike Snitzer | 1d0f3ce | 2009-12-10 23:52:22 +0000 | [diff] [blame] | 595 | param->target_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Alasdair G Kergon | 7c66641 | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 597 | table = dm_get_live_table(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | if (table) { |
Mike Snitzer | 1d0f3ce | 2009-12-10 23:52:22 +0000 | [diff] [blame] | 599 | if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) { |
| 600 | if (get_disk_ro(disk)) |
| 601 | param->flags |= DM_READONLY_FLAG; |
| 602 | param->target_count = dm_table_get_num_targets(table); |
| 603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | dm_table_put(table); |
Mike Snitzer | 1d0f3ce | 2009-12-10 23:52:22 +0000 | [diff] [blame] | 605 | |
| 606 | param->flags |= DM_ACTIVE_PRESENT_FLAG; |
| 607 | } |
| 608 | |
| 609 | if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) { |
| 610 | table = dm_get_inactive_table(md); |
| 611 | if (table) { |
| 612 | if (!(dm_table_get_mode(table) & FMODE_WRITE)) |
| 613 | param->flags |= DM_READONLY_FLAG; |
| 614 | param->target_count = dm_table_get_num_targets(table); |
| 615 | dm_table_put(table); |
| 616 | } |
| 617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static int dev_create(struct dm_ioctl *param, size_t param_size) |
| 623 | { |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 624 | int r, m = DM_ANY_MINOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | struct mapped_device *md; |
| 626 | |
| 627 | r = check_name(param->name); |
| 628 | if (r) |
| 629 | return r; |
| 630 | |
| 631 | if (param->flags & DM_PERSISTENT_DEV_FLAG) |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 632 | m = MINOR(huge_decode_dev(param->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 634 | r = dm_create(m, &md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | if (r) |
| 636 | return r; |
| 637 | |
| 638 | r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md); |
| 639 | if (r) { |
| 640 | dm_put(md); |
| 641 | return r; |
| 642 | } |
| 643 | |
| 644 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; |
| 645 | |
| 646 | r = __dev_status(md, param); |
| 647 | dm_put(md); |
| 648 | |
| 649 | return r; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Always use UUID for lookups if it's present, otherwise use name or dev. |
| 654 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 655 | static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 657 | struct mapped_device *md; |
| 658 | void *mdptr = NULL; |
| 659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | if (*param->uuid) |
| 661 | return __get_uuid_cell(param->uuid); |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 662 | |
| 663 | if (*param->name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | return __get_name_cell(param->name); |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 665 | |
| 666 | md = dm_get_md(huge_decode_dev(param->dev)); |
Alasdair G Kergon | bfc5ecd | 2006-11-08 17:44:42 -0800 | [diff] [blame] | 667 | if (!md) |
| 668 | goto out; |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 669 | |
Alasdair G Kergon | bfc5ecd | 2006-11-08 17:44:42 -0800 | [diff] [blame] | 670 | mdptr = dm_get_mdptr(md); |
| 671 | if (!mdptr) |
| 672 | dm_put(md); |
| 673 | |
| 674 | out: |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 675 | return mdptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 678 | static struct mapped_device *find_device(struct dm_ioctl *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
| 680 | struct hash_cell *hc; |
| 681 | struct mapped_device *md = NULL; |
| 682 | |
| 683 | down_read(&_hash_lock); |
| 684 | hc = __find_device_hash_cell(param); |
| 685 | if (hc) { |
| 686 | md = hc->md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
| 688 | /* |
| 689 | * Sneakily write in both the name and the uuid |
| 690 | * while we have the cell. |
| 691 | */ |
Roel Kluin | a518b86 | 2009-12-10 23:52:07 +0000 | [diff] [blame] | 692 | strlcpy(param->name, hc->name, sizeof(param->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | if (hc->uuid) |
Roel Kluin | a518b86 | 2009-12-10 23:52:07 +0000 | [diff] [blame] | 694 | strlcpy(param->uuid, hc->uuid, sizeof(param->uuid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | else |
| 696 | param->uuid[0] = '\0'; |
| 697 | |
| 698 | if (hc->new_map) |
| 699 | param->flags |= DM_INACTIVE_PRESENT_FLAG; |
| 700 | else |
| 701 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; |
| 702 | } |
| 703 | up_read(&_hash_lock); |
| 704 | |
| 705 | return md; |
| 706 | } |
| 707 | |
| 708 | static int dev_remove(struct dm_ioctl *param, size_t param_size) |
| 709 | { |
| 710 | struct hash_cell *hc; |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 711 | struct mapped_device *md; |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 712 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
| 714 | down_write(&_hash_lock); |
| 715 | hc = __find_device_hash_cell(param); |
| 716 | |
| 717 | if (!hc) { |
| 718 | DMWARN("device doesn't appear to be in the dev hash table."); |
| 719 | up_write(&_hash_lock); |
| 720 | return -ENXIO; |
| 721 | } |
| 722 | |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 723 | md = hc->md; |
| 724 | |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 725 | /* |
| 726 | * Ensure the device is not open and nothing further can open it. |
| 727 | */ |
| 728 | r = dm_lock_for_deletion(md); |
| 729 | if (r) { |
| 730 | DMWARN("unable to remove open device %s", hc->name); |
| 731 | up_write(&_hash_lock); |
| 732 | dm_put(md); |
| 733 | return r; |
| 734 | } |
| 735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | __hash_remove(hc); |
| 737 | up_write(&_hash_lock); |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 738 | |
| 739 | dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr); |
| 740 | |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 741 | dm_put(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | param->data_size = 0; |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Check a string doesn't overrun the chunk of |
| 748 | * memory we copied from userland. |
| 749 | */ |
| 750 | static int invalid_str(char *str, void *end) |
| 751 | { |
| 752 | while ((void *) str < end) |
| 753 | if (!*str++) |
| 754 | return 0; |
| 755 | |
| 756 | return -EINVAL; |
| 757 | } |
| 758 | |
| 759 | static int dev_rename(struct dm_ioctl *param, size_t param_size) |
| 760 | { |
| 761 | int r; |
| 762 | char *new_name = (char *) param + param->data_start; |
| 763 | |
Alasdair G Kergon | 27238b2 | 2008-02-08 02:09:53 +0000 | [diff] [blame] | 764 | if (new_name < param->data || |
Milan Broz | bc0fd67 | 2009-03-16 16:56:01 +0000 | [diff] [blame] | 765 | invalid_str(new_name, (void *) param + param_size) || |
| 766 | strlen(new_name) > DM_NAME_LEN - 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | DMWARN("Invalid new logical volume name supplied."); |
| 768 | return -EINVAL; |
| 769 | } |
| 770 | |
| 771 | r = check_name(new_name); |
| 772 | if (r) |
| 773 | return r; |
| 774 | |
| 775 | param->data_size = 0; |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 776 | return dm_hash_rename(param->event_nr, param->name, new_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 779 | static int dev_set_geometry(struct dm_ioctl *param, size_t param_size) |
| 780 | { |
| 781 | int r = -EINVAL, x; |
| 782 | struct mapped_device *md; |
| 783 | struct hd_geometry geometry; |
| 784 | unsigned long indata[4]; |
| 785 | char *geostr = (char *) param + param->data_start; |
| 786 | |
| 787 | md = find_device(param); |
| 788 | if (!md) |
| 789 | return -ENXIO; |
| 790 | |
Alasdair G Kergon | 27238b2 | 2008-02-08 02:09:53 +0000 | [diff] [blame] | 791 | if (geostr < param->data || |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 792 | invalid_str(geostr, (void *) param + param_size)) { |
| 793 | DMWARN("Invalid geometry supplied."); |
| 794 | goto out; |
| 795 | } |
| 796 | |
| 797 | x = sscanf(geostr, "%lu %lu %lu %lu", indata, |
| 798 | indata + 1, indata + 2, indata + 3); |
| 799 | |
| 800 | if (x != 4) { |
| 801 | DMWARN("Unable to interpret geometry settings."); |
| 802 | goto out; |
| 803 | } |
| 804 | |
| 805 | if (indata[0] > 65535 || indata[1] > 255 || |
| 806 | indata[2] > 255 || indata[3] > ULONG_MAX) { |
| 807 | DMWARN("Geometry exceeds range limits."); |
| 808 | goto out; |
| 809 | } |
| 810 | |
| 811 | geometry.cylinders = indata[0]; |
| 812 | geometry.heads = indata[1]; |
| 813 | geometry.sectors = indata[2]; |
| 814 | geometry.start = indata[3]; |
| 815 | |
| 816 | r = dm_set_geometry(md, &geometry); |
| 817 | if (!r) |
| 818 | r = __dev_status(md, param); |
| 819 | |
| 820 | param->data_size = 0; |
| 821 | |
| 822 | out: |
| 823 | dm_put(md); |
| 824 | return r; |
| 825 | } |
| 826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | static int do_suspend(struct dm_ioctl *param) |
| 828 | { |
| 829 | int r = 0; |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 830 | unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | struct mapped_device *md; |
| 832 | |
| 833 | md = find_device(param); |
| 834 | if (!md) |
| 835 | return -ENXIO; |
| 836 | |
Alasdair G Kergon | 6da487d | 2006-01-06 00:20:07 -0800 | [diff] [blame] | 837 | if (param->flags & DM_SKIP_LOCKFS_FLAG) |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 838 | suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG; |
Kiyoshi Ueda | 81fdb09 | 2006-12-08 02:41:07 -0800 | [diff] [blame] | 839 | if (param->flags & DM_NOFLUSH_FLAG) |
| 840 | suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG; |
Alasdair G Kergon | 6da487d | 2006-01-06 00:20:07 -0800 | [diff] [blame] | 841 | |
Kiyoshi Ueda | 4f186f8 | 2009-12-10 23:52:26 +0000 | [diff] [blame] | 842 | if (!dm_suspended_md(md)) |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 843 | r = dm_suspend(md, suspend_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
| 845 | if (!r) |
| 846 | r = __dev_status(md, param); |
| 847 | |
| 848 | dm_put(md); |
| 849 | return r; |
| 850 | } |
| 851 | |
| 852 | static int do_resume(struct dm_ioctl *param) |
| 853 | { |
| 854 | int r = 0; |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 855 | unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | struct hash_cell *hc; |
| 857 | struct mapped_device *md; |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 858 | struct dm_table *new_map, *old_map = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
| 860 | down_write(&_hash_lock); |
| 861 | |
| 862 | hc = __find_device_hash_cell(param); |
| 863 | if (!hc) { |
| 864 | DMWARN("device doesn't appear to be in the dev hash table."); |
| 865 | up_write(&_hash_lock); |
| 866 | return -ENXIO; |
| 867 | } |
| 868 | |
| 869 | md = hc->md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
| 871 | new_map = hc->new_map; |
| 872 | hc->new_map = NULL; |
| 873 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; |
| 874 | |
| 875 | up_write(&_hash_lock); |
| 876 | |
| 877 | /* Do we need to load a new map ? */ |
| 878 | if (new_map) { |
| 879 | /* Suspend if it isn't already suspended */ |
Alasdair G Kergon | 6da487d | 2006-01-06 00:20:07 -0800 | [diff] [blame] | 880 | if (param->flags & DM_SKIP_LOCKFS_FLAG) |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 881 | suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG; |
Kiyoshi Ueda | 81fdb09 | 2006-12-08 02:41:07 -0800 | [diff] [blame] | 882 | if (param->flags & DM_NOFLUSH_FLAG) |
| 883 | suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG; |
Kiyoshi Ueda | 4f186f8 | 2009-12-10 23:52:26 +0000 | [diff] [blame] | 884 | if (!dm_suspended_md(md)) |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 885 | dm_suspend(md, suspend_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 887 | old_map = dm_swap_table(md, new_map); |
| 888 | if (IS_ERR(old_map)) { |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 889 | dm_table_destroy(new_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | dm_put(md); |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 891 | return PTR_ERR(old_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | if (dm_table_get_mode(new_map) & FMODE_WRITE) |
| 895 | set_disk_ro(dm_disk(md), 0); |
| 896 | else |
| 897 | set_disk_ro(dm_disk(md), 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Kiyoshi Ueda | 4f186f8 | 2009-12-10 23:52:26 +0000 | [diff] [blame] | 900 | if (dm_suspended_md(md)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | r = dm_resume(md); |
| 902 | |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 903 | if (old_map) |
| 904 | dm_table_destroy(old_map); |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 905 | |
| 906 | if (!r) { |
| 907 | dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | r = __dev_status(md, param); |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 909 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
| 911 | dm_put(md); |
| 912 | return r; |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | * Set or unset the suspension state of a device. |
| 917 | * If the device already is in the requested state we just return its status. |
| 918 | */ |
| 919 | static int dev_suspend(struct dm_ioctl *param, size_t param_size) |
| 920 | { |
| 921 | if (param->flags & DM_SUSPEND_FLAG) |
| 922 | return do_suspend(param); |
| 923 | |
| 924 | return do_resume(param); |
| 925 | } |
| 926 | |
| 927 | /* |
| 928 | * Copies device info back to user space, used by |
| 929 | * the create and info ioctls. |
| 930 | */ |
| 931 | static int dev_status(struct dm_ioctl *param, size_t param_size) |
| 932 | { |
| 933 | int r; |
| 934 | struct mapped_device *md; |
| 935 | |
| 936 | md = find_device(param); |
| 937 | if (!md) |
| 938 | return -ENXIO; |
| 939 | |
| 940 | r = __dev_status(md, param); |
| 941 | dm_put(md); |
| 942 | return r; |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Build up the status struct for each target |
| 947 | */ |
| 948 | static void retrieve_status(struct dm_table *table, |
| 949 | struct dm_ioctl *param, size_t param_size) |
| 950 | { |
| 951 | unsigned int i, num_targets; |
| 952 | struct dm_target_spec *spec; |
| 953 | char *outbuf, *outptr; |
| 954 | status_type_t type; |
| 955 | size_t remaining, len, used = 0; |
| 956 | |
| 957 | outptr = outbuf = get_result_buffer(param, param_size, &len); |
| 958 | |
| 959 | if (param->flags & DM_STATUS_TABLE_FLAG) |
| 960 | type = STATUSTYPE_TABLE; |
| 961 | else |
| 962 | type = STATUSTYPE_INFO; |
| 963 | |
| 964 | /* Get all the target info */ |
| 965 | num_targets = dm_table_get_num_targets(table); |
| 966 | for (i = 0; i < num_targets; i++) { |
| 967 | struct dm_target *ti = dm_table_get_target(table, i); |
| 968 | |
| 969 | remaining = len - (outptr - outbuf); |
| 970 | if (remaining <= sizeof(struct dm_target_spec)) { |
| 971 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 972 | break; |
| 973 | } |
| 974 | |
| 975 | spec = (struct dm_target_spec *) outptr; |
| 976 | |
| 977 | spec->status = 0; |
| 978 | spec->sector_start = ti->begin; |
| 979 | spec->length = ti->len; |
| 980 | strncpy(spec->target_type, ti->type->name, |
| 981 | sizeof(spec->target_type)); |
| 982 | |
| 983 | outptr += sizeof(struct dm_target_spec); |
| 984 | remaining = len - (outptr - outbuf); |
| 985 | if (remaining <= 0) { |
| 986 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 987 | break; |
| 988 | } |
| 989 | |
| 990 | /* Get the status/table string from the target driver */ |
| 991 | if (ti->type->status) { |
| 992 | if (ti->type->status(ti, type, outptr, remaining)) { |
| 993 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 994 | break; |
| 995 | } |
| 996 | } else |
| 997 | outptr[0] = '\0'; |
| 998 | |
| 999 | outptr += strlen(outptr) + 1; |
| 1000 | used = param->data_start + (outptr - outbuf); |
| 1001 | |
| 1002 | outptr = align_ptr(outptr); |
| 1003 | spec->next = outptr - outbuf; |
| 1004 | } |
| 1005 | |
| 1006 | if (used) |
| 1007 | param->data_size = used; |
| 1008 | |
| 1009 | param->target_count = num_targets; |
| 1010 | } |
| 1011 | |
| 1012 | /* |
| 1013 | * Wait for a device to report an event |
| 1014 | */ |
| 1015 | static int dev_wait(struct dm_ioctl *param, size_t param_size) |
| 1016 | { |
| 1017 | int r; |
| 1018 | struct mapped_device *md; |
| 1019 | struct dm_table *table; |
| 1020 | |
| 1021 | md = find_device(param); |
| 1022 | if (!md) |
| 1023 | return -ENXIO; |
| 1024 | |
| 1025 | /* |
| 1026 | * Wait for a notification event |
| 1027 | */ |
| 1028 | if (dm_wait_event(md, param->event_nr)) { |
| 1029 | r = -ERESTARTSYS; |
| 1030 | goto out; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
| 1034 | * The userland program is going to want to know what |
| 1035 | * changed to trigger the event, so we may as well tell |
| 1036 | * him and save an ioctl. |
| 1037 | */ |
| 1038 | r = __dev_status(md, param); |
| 1039 | if (r) |
| 1040 | goto out; |
| 1041 | |
Mike Snitzer | 1d0f3ce | 2009-12-10 23:52:22 +0000 | [diff] [blame] | 1042 | table = dm_get_live_or_inactive_table(md, param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | if (table) { |
| 1044 | retrieve_status(table, param, param_size); |
| 1045 | dm_table_put(table); |
| 1046 | } |
| 1047 | |
| 1048 | out: |
| 1049 | dm_put(md); |
| 1050 | return r; |
| 1051 | } |
| 1052 | |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 1053 | static inline fmode_t get_mode(struct dm_ioctl *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | { |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 1055 | fmode_t mode = FMODE_READ | FMODE_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
| 1057 | if (param->flags & DM_READONLY_FLAG) |
| 1058 | mode = FMODE_READ; |
| 1059 | |
| 1060 | return mode; |
| 1061 | } |
| 1062 | |
| 1063 | static int next_target(struct dm_target_spec *last, uint32_t next, void *end, |
| 1064 | struct dm_target_spec **spec, char **target_params) |
| 1065 | { |
| 1066 | *spec = (struct dm_target_spec *) ((unsigned char *) last + next); |
| 1067 | *target_params = (char *) (*spec + 1); |
| 1068 | |
| 1069 | if (*spec < (last + 1)) |
| 1070 | return -EINVAL; |
| 1071 | |
| 1072 | return invalid_str(*target_params, end); |
| 1073 | } |
| 1074 | |
| 1075 | static int populate_table(struct dm_table *table, |
| 1076 | struct dm_ioctl *param, size_t param_size) |
| 1077 | { |
| 1078 | int r; |
| 1079 | unsigned int i = 0; |
| 1080 | struct dm_target_spec *spec = (struct dm_target_spec *) param; |
| 1081 | uint32_t next = param->data_start; |
| 1082 | void *end = (void *) param + param_size; |
| 1083 | char *target_params; |
| 1084 | |
| 1085 | if (!param->target_count) { |
| 1086 | DMWARN("populate_table: no targets specified"); |
| 1087 | return -EINVAL; |
| 1088 | } |
| 1089 | |
| 1090 | for (i = 0; i < param->target_count; i++) { |
| 1091 | |
| 1092 | r = next_target(spec, next, end, &spec, &target_params); |
| 1093 | if (r) { |
| 1094 | DMWARN("unable to find target"); |
| 1095 | return r; |
| 1096 | } |
| 1097 | |
| 1098 | r = dm_table_add_target(table, spec->target_type, |
| 1099 | (sector_t) spec->sector_start, |
| 1100 | (sector_t) spec->length, |
| 1101 | target_params); |
| 1102 | if (r) { |
| 1103 | DMWARN("error adding target to table"); |
| 1104 | return r; |
| 1105 | } |
| 1106 | |
| 1107 | next = spec->next; |
| 1108 | } |
| 1109 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 1110 | r = dm_table_set_type(table); |
| 1111 | if (r) { |
| 1112 | DMWARN("unable to set table type"); |
| 1113 | return r; |
| 1114 | } |
| 1115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | return dm_table_complete(table); |
| 1117 | } |
| 1118 | |
Martin K. Petersen | 9c47008 | 2009-04-09 00:27:12 +0100 | [diff] [blame] | 1119 | static int table_prealloc_integrity(struct dm_table *t, |
| 1120 | struct mapped_device *md) |
| 1121 | { |
| 1122 | struct list_head *devices = dm_table_get_devices(t); |
| 1123 | struct dm_dev_internal *dd; |
| 1124 | |
| 1125 | list_for_each_entry(dd, devices, list) |
| 1126 | if (bdev_get_integrity(dd->dm_dev.bdev)) |
| 1127 | return blk_integrity_register(dm_disk(md), NULL); |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | static int table_load(struct dm_ioctl *param, size_t param_size) |
| 1133 | { |
| 1134 | int r; |
| 1135 | struct hash_cell *hc; |
| 1136 | struct dm_table *t; |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1137 | struct mapped_device *md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1139 | md = find_device(param); |
| 1140 | if (!md) |
| 1141 | return -ENXIO; |
| 1142 | |
| 1143 | r = dm_table_create(&t, get_mode(param), param->target_count, md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | if (r) |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1145 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | |
| 1147 | r = populate_table(t, param, param_size); |
| 1148 | if (r) { |
Mikulas Patocka | f80a557 | 2009-03-16 17:44:26 +0000 | [diff] [blame] | 1149 | dm_table_destroy(t); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1150 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Martin K. Petersen | 9c47008 | 2009-04-09 00:27:12 +0100 | [diff] [blame] | 1153 | r = table_prealloc_integrity(t, md); |
| 1154 | if (r) { |
| 1155 | DMERR("%s: could not register integrity profile.", |
| 1156 | dm_device_name(md)); |
| 1157 | dm_table_destroy(t); |
| 1158 | goto out; |
| 1159 | } |
| 1160 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 1161 | r = dm_table_alloc_md_mempools(t); |
| 1162 | if (r) { |
| 1163 | DMWARN("unable to allocate mempools for this table"); |
| 1164 | dm_table_destroy(t); |
| 1165 | goto out; |
| 1166 | } |
| 1167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | down_write(&_hash_lock); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1169 | hc = dm_get_mdptr(md); |
| 1170 | if (!hc || hc->md != md) { |
| 1171 | DMWARN("device has been removed from the dev hash table."); |
Mikulas Patocka | f80a557 | 2009-03-16 17:44:26 +0000 | [diff] [blame] | 1172 | dm_table_destroy(t); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1173 | up_write(&_hash_lock); |
| 1174 | r = -ENXIO; |
| 1175 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | if (hc->new_map) |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 1179 | dm_table_destroy(hc->new_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | hc->new_map = t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | up_write(&_hash_lock); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1182 | |
| 1183 | param->flags |= DM_INACTIVE_PRESENT_FLAG; |
| 1184 | r = __dev_status(md, param); |
| 1185 | |
| 1186 | out: |
| 1187 | dm_put(md); |
| 1188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | return r; |
| 1190 | } |
| 1191 | |
| 1192 | static int table_clear(struct dm_ioctl *param, size_t param_size) |
| 1193 | { |
| 1194 | int r; |
| 1195 | struct hash_cell *hc; |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 1196 | struct mapped_device *md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | |
| 1198 | down_write(&_hash_lock); |
| 1199 | |
| 1200 | hc = __find_device_hash_cell(param); |
| 1201 | if (!hc) { |
| 1202 | DMWARN("device doesn't appear to be in the dev hash table."); |
| 1203 | up_write(&_hash_lock); |
| 1204 | return -ENXIO; |
| 1205 | } |
| 1206 | |
| 1207 | if (hc->new_map) { |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 1208 | dm_table_destroy(hc->new_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | hc->new_map = NULL; |
| 1210 | } |
| 1211 | |
| 1212 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; |
| 1213 | |
| 1214 | r = __dev_status(hc->md, param); |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 1215 | md = hc->md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | up_write(&_hash_lock); |
Jeff Mahoney | 7ec75f2 | 2006-06-26 00:27:24 -0700 | [diff] [blame] | 1217 | dm_put(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | return r; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Retrieves a list of devices used by a particular dm device. |
| 1223 | */ |
| 1224 | static void retrieve_deps(struct dm_table *table, |
| 1225 | struct dm_ioctl *param, size_t param_size) |
| 1226 | { |
| 1227 | unsigned int count = 0; |
| 1228 | struct list_head *tmp; |
| 1229 | size_t len, needed; |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 1230 | struct dm_dev_internal *dd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | struct dm_target_deps *deps; |
| 1232 | |
| 1233 | deps = get_result_buffer(param, param_size, &len); |
| 1234 | |
| 1235 | /* |
| 1236 | * Count the devices. |
| 1237 | */ |
| 1238 | list_for_each (tmp, dm_table_get_devices(table)) |
| 1239 | count++; |
| 1240 | |
| 1241 | /* |
| 1242 | * Check we have enough space. |
| 1243 | */ |
| 1244 | needed = sizeof(*deps) + (sizeof(*deps->dev) * count); |
| 1245 | if (len < needed) { |
| 1246 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * Fill in the devices. |
| 1252 | */ |
| 1253 | deps->count = count; |
| 1254 | count = 0; |
| 1255 | list_for_each_entry (dd, dm_table_get_devices(table), list) |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 1256 | deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
| 1258 | param->data_size = param->data_start + needed; |
| 1259 | } |
| 1260 | |
| 1261 | static int table_deps(struct dm_ioctl *param, size_t param_size) |
| 1262 | { |
| 1263 | int r = 0; |
| 1264 | struct mapped_device *md; |
| 1265 | struct dm_table *table; |
| 1266 | |
| 1267 | md = find_device(param); |
| 1268 | if (!md) |
| 1269 | return -ENXIO; |
| 1270 | |
| 1271 | r = __dev_status(md, param); |
| 1272 | if (r) |
| 1273 | goto out; |
| 1274 | |
Mike Snitzer | 1d0f3ce | 2009-12-10 23:52:22 +0000 | [diff] [blame] | 1275 | table = dm_get_live_or_inactive_table(md, param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | if (table) { |
| 1277 | retrieve_deps(table, param, param_size); |
| 1278 | dm_table_put(table); |
| 1279 | } |
| 1280 | |
| 1281 | out: |
| 1282 | dm_put(md); |
| 1283 | return r; |
| 1284 | } |
| 1285 | |
| 1286 | /* |
| 1287 | * Return the status of a device as a text string for each |
| 1288 | * target. |
| 1289 | */ |
| 1290 | static int table_status(struct dm_ioctl *param, size_t param_size) |
| 1291 | { |
| 1292 | int r; |
| 1293 | struct mapped_device *md; |
| 1294 | struct dm_table *table; |
| 1295 | |
| 1296 | md = find_device(param); |
| 1297 | if (!md) |
| 1298 | return -ENXIO; |
| 1299 | |
| 1300 | r = __dev_status(md, param); |
| 1301 | if (r) |
| 1302 | goto out; |
| 1303 | |
Mike Snitzer | 1d0f3ce | 2009-12-10 23:52:22 +0000 | [diff] [blame] | 1304 | table = dm_get_live_or_inactive_table(md, param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | if (table) { |
| 1306 | retrieve_status(table, param, param_size); |
| 1307 | dm_table_put(table); |
| 1308 | } |
| 1309 | |
Mike Snitzer | 1d0f3ce | 2009-12-10 23:52:22 +0000 | [diff] [blame] | 1310 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | dm_put(md); |
| 1312 | return r; |
| 1313 | } |
| 1314 | |
| 1315 | /* |
| 1316 | * Pass a message to the target that's at the supplied device offset. |
| 1317 | */ |
| 1318 | static int target_message(struct dm_ioctl *param, size_t param_size) |
| 1319 | { |
| 1320 | int r, argc; |
| 1321 | char **argv; |
| 1322 | struct mapped_device *md; |
| 1323 | struct dm_table *table; |
| 1324 | struct dm_target *ti; |
| 1325 | struct dm_target_msg *tmsg = (void *) param + param->data_start; |
| 1326 | |
| 1327 | md = find_device(param); |
| 1328 | if (!md) |
| 1329 | return -ENXIO; |
| 1330 | |
| 1331 | r = __dev_status(md, param); |
| 1332 | if (r) |
| 1333 | goto out; |
| 1334 | |
Milan Broz | 027d50f | 2007-10-19 22:38:36 +0100 | [diff] [blame] | 1335 | if (tmsg < (struct dm_target_msg *) param->data || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | invalid_str(tmsg->message, (void *) param + param_size)) { |
| 1337 | DMWARN("Invalid target message parameters."); |
| 1338 | r = -EINVAL; |
| 1339 | goto out; |
| 1340 | } |
| 1341 | |
| 1342 | r = dm_split_args(&argc, &argv, tmsg->message); |
| 1343 | if (r) { |
| 1344 | DMWARN("Failed to split target message parameters"); |
| 1345 | goto out; |
| 1346 | } |
| 1347 | |
Alasdair G Kergon | 7c66641 | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 1348 | table = dm_get_live_table(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | if (!table) |
| 1350 | goto out_argv; |
| 1351 | |
Mike Anderson | c50abeb | 2009-12-10 23:52:20 +0000 | [diff] [blame] | 1352 | if (dm_deleting_md(md)) { |
| 1353 | r = -ENXIO; |
| 1354 | goto out_table; |
| 1355 | } |
| 1356 | |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1357 | ti = dm_table_find_target(table, tmsg->sector); |
| 1358 | if (!dm_target_is_valid(ti)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | DMWARN("Target message sector outside device."); |
| 1360 | r = -EINVAL; |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1361 | } else if (ti->type->message) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | r = ti->type->message(ti, argc, argv); |
| 1363 | else { |
| 1364 | DMWARN("Target type does not support messages"); |
| 1365 | r = -EINVAL; |
| 1366 | } |
| 1367 | |
Mike Anderson | c50abeb | 2009-12-10 23:52:20 +0000 | [diff] [blame] | 1368 | out_table: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | dm_table_put(table); |
| 1370 | out_argv: |
| 1371 | kfree(argv); |
| 1372 | out: |
| 1373 | param->data_size = 0; |
| 1374 | dm_put(md); |
| 1375 | return r; |
| 1376 | } |
| 1377 | |
| 1378 | /*----------------------------------------------------------------- |
| 1379 | * Implementation of open/close/ioctl on the special char |
| 1380 | * device. |
| 1381 | *---------------------------------------------------------------*/ |
| 1382 | static ioctl_fn lookup_ioctl(unsigned int cmd) |
| 1383 | { |
| 1384 | static struct { |
| 1385 | int cmd; |
| 1386 | ioctl_fn fn; |
| 1387 | } _ioctls[] = { |
| 1388 | {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */ |
| 1389 | {DM_REMOVE_ALL_CMD, remove_all}, |
| 1390 | {DM_LIST_DEVICES_CMD, list_devices}, |
| 1391 | |
| 1392 | {DM_DEV_CREATE_CMD, dev_create}, |
| 1393 | {DM_DEV_REMOVE_CMD, dev_remove}, |
| 1394 | {DM_DEV_RENAME_CMD, dev_rename}, |
| 1395 | {DM_DEV_SUSPEND_CMD, dev_suspend}, |
| 1396 | {DM_DEV_STATUS_CMD, dev_status}, |
| 1397 | {DM_DEV_WAIT_CMD, dev_wait}, |
| 1398 | |
| 1399 | {DM_TABLE_LOAD_CMD, table_load}, |
| 1400 | {DM_TABLE_CLEAR_CMD, table_clear}, |
| 1401 | {DM_TABLE_DEPS_CMD, table_deps}, |
| 1402 | {DM_TABLE_STATUS_CMD, table_status}, |
| 1403 | |
| 1404 | {DM_LIST_VERSIONS_CMD, list_versions}, |
| 1405 | |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1406 | {DM_TARGET_MSG_CMD, target_message}, |
| 1407 | {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | }; |
| 1409 | |
| 1410 | return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
| 1414 | * As well as checking the version compatibility this always |
| 1415 | * copies the kernel interface version out. |
| 1416 | */ |
| 1417 | static int check_version(unsigned int cmd, struct dm_ioctl __user *user) |
| 1418 | { |
| 1419 | uint32_t version[3]; |
| 1420 | int r = 0; |
| 1421 | |
| 1422 | if (copy_from_user(version, user->version, sizeof(version))) |
| 1423 | return -EFAULT; |
| 1424 | |
| 1425 | if ((DM_VERSION_MAJOR != version[0]) || |
| 1426 | (DM_VERSION_MINOR < version[1])) { |
| 1427 | DMWARN("ioctl interface mismatch: " |
| 1428 | "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)", |
| 1429 | DM_VERSION_MAJOR, DM_VERSION_MINOR, |
| 1430 | DM_VERSION_PATCHLEVEL, |
| 1431 | version[0], version[1], version[2], cmd); |
| 1432 | r = -EINVAL; |
| 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * Fill in the kernel version. |
| 1437 | */ |
| 1438 | version[0] = DM_VERSION_MAJOR; |
| 1439 | version[1] = DM_VERSION_MINOR; |
| 1440 | version[2] = DM_VERSION_PATCHLEVEL; |
| 1441 | if (copy_to_user(user->version, version, sizeof(version))) |
| 1442 | return -EFAULT; |
| 1443 | |
| 1444 | return r; |
| 1445 | } |
| 1446 | |
| 1447 | static void free_params(struct dm_ioctl *param) |
| 1448 | { |
| 1449 | vfree(param); |
| 1450 | } |
| 1451 | |
| 1452 | static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param) |
| 1453 | { |
| 1454 | struct dm_ioctl tmp, *dmi; |
| 1455 | |
Milan Broz | 76c072b | 2008-02-08 02:09:56 +0000 | [diff] [blame] | 1456 | if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | return -EFAULT; |
| 1458 | |
Milan Broz | 76c072b | 2008-02-08 02:09:56 +0000 | [diff] [blame] | 1459 | if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | return -EINVAL; |
| 1461 | |
Jesper Juhl | bb56acf | 2007-10-19 22:38:54 +0100 | [diff] [blame] | 1462 | dmi = vmalloc(tmp.data_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | if (!dmi) |
| 1464 | return -ENOMEM; |
| 1465 | |
| 1466 | if (copy_from_user(dmi, user, tmp.data_size)) { |
| 1467 | vfree(dmi); |
| 1468 | return -EFAULT; |
| 1469 | } |
| 1470 | |
| 1471 | *param = dmi; |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | static int validate_params(uint cmd, struct dm_ioctl *param) |
| 1476 | { |
| 1477 | /* Always clear this flag */ |
| 1478 | param->flags &= ~DM_BUFFER_FULL_FLAG; |
| 1479 | |
| 1480 | /* Ignores parameters */ |
| 1481 | if (cmd == DM_REMOVE_ALL_CMD || |
| 1482 | cmd == DM_LIST_DEVICES_CMD || |
| 1483 | cmd == DM_LIST_VERSIONS_CMD) |
| 1484 | return 0; |
| 1485 | |
| 1486 | if ((cmd == DM_DEV_CREATE_CMD)) { |
| 1487 | if (!*param->name) { |
| 1488 | DMWARN("name not supplied when creating device"); |
| 1489 | return -EINVAL; |
| 1490 | } |
| 1491 | } else if ((*param->uuid && *param->name)) { |
| 1492 | DMWARN("only supply one of name or uuid, cmd(%u)", cmd); |
| 1493 | return -EINVAL; |
| 1494 | } |
| 1495 | |
| 1496 | /* Ensure strings are terminated */ |
| 1497 | param->name[DM_NAME_LEN - 1] = '\0'; |
| 1498 | param->uuid[DM_UUID_LEN - 1] = '\0'; |
| 1499 | |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
Alasdair G Kergon | 27238b2 | 2008-02-08 02:09:53 +0000 | [diff] [blame] | 1503 | static int ctl_ioctl(uint command, struct dm_ioctl __user *user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | { |
| 1505 | int r = 0; |
| 1506 | unsigned int cmd; |
Andrew Morton | a26ffd4 | 2008-02-08 02:10:16 +0000 | [diff] [blame] | 1507 | struct dm_ioctl *uninitialized_var(param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | ioctl_fn fn = NULL; |
| 1509 | size_t param_size; |
| 1510 | |
| 1511 | /* only root can play with this */ |
| 1512 | if (!capable(CAP_SYS_ADMIN)) |
| 1513 | return -EACCES; |
| 1514 | |
| 1515 | if (_IOC_TYPE(command) != DM_IOCTL) |
| 1516 | return -ENOTTY; |
| 1517 | |
| 1518 | cmd = _IOC_NR(command); |
| 1519 | |
| 1520 | /* |
| 1521 | * Check the interface version passed in. This also |
| 1522 | * writes out the kernel's interface version. |
| 1523 | */ |
| 1524 | r = check_version(cmd, user); |
| 1525 | if (r) |
| 1526 | return r; |
| 1527 | |
| 1528 | /* |
| 1529 | * Nothing more to do for the version command. |
| 1530 | */ |
| 1531 | if (cmd == DM_VERSION_CMD) |
| 1532 | return 0; |
| 1533 | |
| 1534 | fn = lookup_ioctl(cmd); |
| 1535 | if (!fn) { |
| 1536 | DMWARN("dm_ctl_ioctl: unknown command 0x%x", command); |
| 1537 | return -ENOTTY; |
| 1538 | } |
| 1539 | |
| 1540 | /* |
| 1541 | * Trying to avoid low memory issues when a device is |
| 1542 | * suspended. |
| 1543 | */ |
| 1544 | current->flags |= PF_MEMALLOC; |
| 1545 | |
| 1546 | /* |
| 1547 | * Copy the parameters into kernel space. |
| 1548 | */ |
| 1549 | r = copy_params(user, ¶m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
Alasdair G Kergon | dab6a42 | 2006-02-01 03:04:52 -0800 | [diff] [blame] | 1551 | current->flags &= ~PF_MEMALLOC; |
| 1552 | |
| 1553 | if (r) |
| 1554 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
| 1556 | r = validate_params(cmd, param); |
| 1557 | if (r) |
| 1558 | goto out; |
| 1559 | |
| 1560 | param_size = param->data_size; |
| 1561 | param->data_size = sizeof(*param); |
| 1562 | r = fn(param, param_size); |
| 1563 | |
| 1564 | /* |
| 1565 | * Copy the results back to userland. |
| 1566 | */ |
| 1567 | if (!r && copy_to_user(user, param, param->data_size)) |
| 1568 | r = -EFAULT; |
| 1569 | |
| 1570 | out: |
| 1571 | free_params(param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | return r; |
| 1573 | } |
| 1574 | |
Alasdair G Kergon | 27238b2 | 2008-02-08 02:09:53 +0000 | [diff] [blame] | 1575 | static long dm_ctl_ioctl(struct file *file, uint command, ulong u) |
| 1576 | { |
| 1577 | return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u); |
| 1578 | } |
| 1579 | |
Milan Broz | 76c072b | 2008-02-08 02:09:56 +0000 | [diff] [blame] | 1580 | #ifdef CONFIG_COMPAT |
| 1581 | static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u) |
| 1582 | { |
| 1583 | return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u)); |
| 1584 | } |
| 1585 | #else |
| 1586 | #define dm_compat_ctl_ioctl NULL |
| 1587 | #endif |
| 1588 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 1589 | static const struct file_operations _ctl_fops = { |
Alasdair G Kergon | 27238b2 | 2008-02-08 02:09:53 +0000 | [diff] [blame] | 1590 | .unlocked_ioctl = dm_ctl_ioctl, |
Milan Broz | 76c072b | 2008-02-08 02:09:56 +0000 | [diff] [blame] | 1591 | .compat_ioctl = dm_compat_ctl_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | .owner = THIS_MODULE, |
| 1593 | }; |
| 1594 | |
| 1595 | static struct miscdevice _dm_misc = { |
| 1596 | .minor = MISC_DYNAMIC_MINOR, |
| 1597 | .name = DM_NAME, |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1598 | .nodename = "mapper/control", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | .fops = &_ctl_fops |
| 1600 | }; |
| 1601 | |
| 1602 | /* |
| 1603 | * Create misc character device and link to DM_DIR/control. |
| 1604 | */ |
| 1605 | int __init dm_interface_init(void) |
| 1606 | { |
| 1607 | int r; |
| 1608 | |
| 1609 | r = dm_hash_init(); |
| 1610 | if (r) |
| 1611 | return r; |
| 1612 | |
| 1613 | r = misc_register(&_dm_misc); |
| 1614 | if (r) { |
| 1615 | DMERR("misc_register failed for control device"); |
| 1616 | dm_hash_exit(); |
| 1617 | return r; |
| 1618 | } |
| 1619 | |
| 1620 | DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR, |
| 1621 | DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA, |
| 1622 | DM_DRIVER_EMAIL); |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
| 1626 | void dm_interface_exit(void) |
| 1627 | { |
| 1628 | if (misc_deregister(&_dm_misc) < 0) |
| 1629 | DMERR("misc_deregister failed for control device"); |
| 1630 | |
| 1631 | dm_hash_exit(); |
| 1632 | } |
Mike Anderson | 96a1f7d | 2007-10-19 22:47:59 +0100 | [diff] [blame] | 1633 | |
| 1634 | /** |
| 1635 | * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers |
| 1636 | * @md: Pointer to mapped_device |
| 1637 | * @name: Buffer (size DM_NAME_LEN) for name |
| 1638 | * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined |
| 1639 | */ |
| 1640 | int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid) |
| 1641 | { |
| 1642 | int r = 0; |
| 1643 | struct hash_cell *hc; |
| 1644 | |
| 1645 | if (!md) |
| 1646 | return -ENXIO; |
| 1647 | |
Mikulas Patocka | 6076905 | 2009-12-10 23:51:52 +0000 | [diff] [blame] | 1648 | mutex_lock(&dm_hash_cells_mutex); |
Mike Anderson | 96a1f7d | 2007-10-19 22:47:59 +0100 | [diff] [blame] | 1649 | hc = dm_get_mdptr(md); |
| 1650 | if (!hc || hc->md != md) { |
| 1651 | r = -ENXIO; |
| 1652 | goto out; |
| 1653 | } |
| 1654 | |
Milan Broz | 23d39f6 | 2009-01-06 03:05:04 +0000 | [diff] [blame] | 1655 | if (name) |
| 1656 | strcpy(name, hc->name); |
| 1657 | if (uuid) |
| 1658 | strcpy(uuid, hc->uuid ? : ""); |
Mike Anderson | 96a1f7d | 2007-10-19 22:47:59 +0100 | [diff] [blame] | 1659 | |
| 1660 | out: |
Mikulas Patocka | 6076905 | 2009-12-10 23:51:52 +0000 | [diff] [blame] | 1661 | mutex_unlock(&dm_hash_cells_mutex); |
Mike Anderson | 96a1f7d | 2007-10-19 22:47:59 +0100 | [diff] [blame] | 1662 | |
| 1663 | return r; |
| 1664 | } |