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