dm-ioctl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. /*
  2. * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2004 - 2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm.h"
  8. #include <linux/module.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/miscdevice.h>
  11. #include <linux/init.h>
  12. #include <linux/wait.h>
  13. #include <linux/slab.h>
  14. #include <linux/devfs_fs_kernel.h>
  15. #include <linux/dm-ioctl.h>
  16. #include <asm/uaccess.h>
  17. #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
  18. /*-----------------------------------------------------------------
  19. * The ioctl interface needs to be able to look up devices by
  20. * name or uuid.
  21. *---------------------------------------------------------------*/
  22. struct hash_cell {
  23. struct list_head name_list;
  24. struct list_head uuid_list;
  25. char *name;
  26. char *uuid;
  27. struct mapped_device *md;
  28. struct dm_table *new_map;
  29. };
  30. struct vers_iter {
  31. size_t param_size;
  32. struct dm_target_versions *vers, *old_vers;
  33. char *end;
  34. uint32_t flags;
  35. };
  36. #define NUM_BUCKETS 64
  37. #define MASK_BUCKETS (NUM_BUCKETS - 1)
  38. static struct list_head _name_buckets[NUM_BUCKETS];
  39. static struct list_head _uuid_buckets[NUM_BUCKETS];
  40. static void dm_hash_remove_all(void);
  41. /*
  42. * Guards access to both hash tables.
  43. */
  44. static DECLARE_RWSEM(_hash_lock);
  45. static void init_buckets(struct list_head *buckets)
  46. {
  47. unsigned int i;
  48. for (i = 0; i < NUM_BUCKETS; i++)
  49. INIT_LIST_HEAD(buckets + i);
  50. }
  51. static int dm_hash_init(void)
  52. {
  53. init_buckets(_name_buckets);
  54. init_buckets(_uuid_buckets);
  55. devfs_mk_dir(DM_DIR);
  56. return 0;
  57. }
  58. static void dm_hash_exit(void)
  59. {
  60. dm_hash_remove_all();
  61. devfs_remove(DM_DIR);
  62. }
  63. /*-----------------------------------------------------------------
  64. * Hash function:
  65. * We're not really concerned with the str hash function being
  66. * fast since it's only used by the ioctl interface.
  67. *---------------------------------------------------------------*/
  68. static unsigned int hash_str(const char *str)
  69. {
  70. const unsigned int hash_mult = 2654435387U;
  71. unsigned int h = 0;
  72. while (*str)
  73. h = (h + (unsigned int) *str++) * hash_mult;
  74. return h & MASK_BUCKETS;
  75. }
  76. /*-----------------------------------------------------------------
  77. * Code for looking up a device by name
  78. *---------------------------------------------------------------*/
  79. static struct hash_cell *__get_name_cell(const char *str)
  80. {
  81. struct hash_cell *hc;
  82. unsigned int h = hash_str(str);
  83. list_for_each_entry (hc, _name_buckets + h, name_list)
  84. if (!strcmp(hc->name, str))
  85. return hc;
  86. return NULL;
  87. }
  88. static struct hash_cell *__get_uuid_cell(const char *str)
  89. {
  90. struct hash_cell *hc;
  91. unsigned int h = hash_str(str);
  92. list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
  93. if (!strcmp(hc->uuid, str))
  94. return hc;
  95. return NULL;
  96. }
  97. /*-----------------------------------------------------------------
  98. * Inserting, removing and renaming a device.
  99. *---------------------------------------------------------------*/
  100. static struct hash_cell *alloc_cell(const char *name, const char *uuid,
  101. struct mapped_device *md)
  102. {
  103. struct hash_cell *hc;
  104. hc = kmalloc(sizeof(*hc), GFP_KERNEL);
  105. if (!hc)
  106. return NULL;
  107. hc->name = kstrdup(name, GFP_KERNEL);
  108. if (!hc->name) {
  109. kfree(hc);
  110. return NULL;
  111. }
  112. if (!uuid)
  113. hc->uuid = NULL;
  114. else {
  115. hc->uuid = kstrdup(uuid, GFP_KERNEL);
  116. if (!hc->uuid) {
  117. kfree(hc->name);
  118. kfree(hc);
  119. return NULL;
  120. }
  121. }
  122. INIT_LIST_HEAD(&hc->name_list);
  123. INIT_LIST_HEAD(&hc->uuid_list);
  124. hc->md = md;
  125. hc->new_map = NULL;
  126. return hc;
  127. }
  128. static void free_cell(struct hash_cell *hc)
  129. {
  130. if (hc) {
  131. kfree(hc->name);
  132. kfree(hc->uuid);
  133. kfree(hc);
  134. }
  135. }
  136. /*
  137. * devfs stuff.
  138. */
  139. static int register_with_devfs(struct hash_cell *hc)
  140. {
  141. struct gendisk *disk = dm_disk(hc->md);
  142. devfs_mk_bdev(MKDEV(disk->major, disk->first_minor),
  143. S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP,
  144. DM_DIR "/%s", hc->name);
  145. return 0;
  146. }
  147. static int unregister_with_devfs(struct hash_cell *hc)
  148. {
  149. devfs_remove(DM_DIR"/%s", hc->name);
  150. return 0;
  151. }
  152. /*
  153. * The kdev_t and uuid of a device can never change once it is
  154. * initially inserted.
  155. */
  156. static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
  157. {
  158. struct hash_cell *cell;
  159. /*
  160. * Allocate the new cells.
  161. */
  162. cell = alloc_cell(name, uuid, md);
  163. if (!cell)
  164. return -ENOMEM;
  165. /*
  166. * Insert the cell into both hash tables.
  167. */
  168. down_write(&_hash_lock);
  169. if (__get_name_cell(name))
  170. goto bad;
  171. list_add(&cell->name_list, _name_buckets + hash_str(name));
  172. if (uuid) {
  173. if (__get_uuid_cell(uuid)) {
  174. list_del(&cell->name_list);
  175. goto bad;
  176. }
  177. list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
  178. }
  179. register_with_devfs(cell);
  180. dm_get(md);
  181. dm_set_mdptr(md, cell);
  182. up_write(&_hash_lock);
  183. return 0;
  184. bad:
  185. up_write(&_hash_lock);
  186. free_cell(cell);
  187. return -EBUSY;
  188. }
  189. static void __hash_remove(struct hash_cell *hc)
  190. {
  191. struct dm_table *table;
  192. /* remove from the dev hash */
  193. list_del(&hc->uuid_list);
  194. list_del(&hc->name_list);
  195. unregister_with_devfs(hc);
  196. dm_set_mdptr(hc->md, NULL);
  197. table = dm_get_table(hc->md);
  198. if (table) {
  199. dm_table_event(table);
  200. dm_table_put(table);
  201. }
  202. dm_put(hc->md);
  203. if (hc->new_map)
  204. dm_table_put(hc->new_map);
  205. free_cell(hc);
  206. }
  207. static void dm_hash_remove_all(void)
  208. {
  209. int i;
  210. struct hash_cell *hc;
  211. struct list_head *tmp, *n;
  212. down_write(&_hash_lock);
  213. for (i = 0; i < NUM_BUCKETS; i++) {
  214. list_for_each_safe (tmp, n, _name_buckets + i) {
  215. hc = list_entry(tmp, struct hash_cell, name_list);
  216. __hash_remove(hc);
  217. }
  218. }
  219. up_write(&_hash_lock);
  220. }
  221. static int dm_hash_rename(const char *old, const char *new)
  222. {
  223. char *new_name, *old_name;
  224. struct hash_cell *hc;
  225. struct dm_table *table;
  226. /*
  227. * duplicate new.
  228. */
  229. new_name = kstrdup(new, GFP_KERNEL);
  230. if (!new_name)
  231. return -ENOMEM;
  232. down_write(&_hash_lock);
  233. /*
  234. * Is new free ?
  235. */
  236. hc = __get_name_cell(new);
  237. if (hc) {
  238. DMWARN("asked to rename to an already existing name %s -> %s",
  239. old, new);
  240. up_write(&_hash_lock);
  241. kfree(new_name);
  242. return -EBUSY;
  243. }
  244. /*
  245. * Is there such a device as 'old' ?
  246. */
  247. hc = __get_name_cell(old);
  248. if (!hc) {
  249. DMWARN("asked to rename a non existent device %s -> %s",
  250. old, new);
  251. up_write(&_hash_lock);
  252. kfree(new_name);
  253. return -ENXIO;
  254. }
  255. /*
  256. * rename and move the name cell.
  257. */
  258. unregister_with_devfs(hc);
  259. list_del(&hc->name_list);
  260. old_name = hc->name;
  261. hc->name = new_name;
  262. list_add(&hc->name_list, _name_buckets + hash_str(new_name));
  263. /* rename the device node in devfs */
  264. register_with_devfs(hc);
  265. /*
  266. * Wake up any dm event waiters.
  267. */
  268. table = dm_get_table(hc->md);
  269. if (table) {
  270. dm_table_event(table);
  271. dm_table_put(table);
  272. }
  273. up_write(&_hash_lock);
  274. kfree(old_name);
  275. return 0;
  276. }
  277. /*-----------------------------------------------------------------
  278. * Implementation of the ioctl commands
  279. *---------------------------------------------------------------*/
  280. /*
  281. * All the ioctl commands get dispatched to functions with this
  282. * prototype.
  283. */
  284. typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
  285. static int remove_all(struct dm_ioctl *param, size_t param_size)
  286. {
  287. dm_hash_remove_all();
  288. param->data_size = 0;
  289. return 0;
  290. }
  291. /*
  292. * Round up the ptr to an 8-byte boundary.
  293. */
  294. #define ALIGN_MASK 7
  295. static inline void *align_ptr(void *ptr)
  296. {
  297. return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
  298. }
  299. /*
  300. * Retrieves the data payload buffer from an already allocated
  301. * struct dm_ioctl.
  302. */
  303. static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
  304. size_t *len)
  305. {
  306. param->data_start = align_ptr(param + 1) - (void *) param;
  307. if (param->data_start < param_size)
  308. *len = param_size - param->data_start;
  309. else
  310. *len = 0;
  311. return ((void *) param) + param->data_start;
  312. }
  313. static int list_devices(struct dm_ioctl *param, size_t param_size)
  314. {
  315. unsigned int i;
  316. struct hash_cell *hc;
  317. size_t len, needed = 0;
  318. struct gendisk *disk;
  319. struct dm_name_list *nl, *old_nl = NULL;
  320. down_write(&_hash_lock);
  321. /*
  322. * Loop through all the devices working out how much
  323. * space we need.
  324. */
  325. for (i = 0; i < NUM_BUCKETS; i++) {
  326. list_for_each_entry (hc, _name_buckets + i, name_list) {
  327. needed += sizeof(struct dm_name_list);
  328. needed += strlen(hc->name) + 1;
  329. needed += ALIGN_MASK;
  330. }
  331. }
  332. /*
  333. * Grab our output buffer.
  334. */
  335. nl = get_result_buffer(param, param_size, &len);
  336. if (len < needed) {
  337. param->flags |= DM_BUFFER_FULL_FLAG;
  338. goto out;
  339. }
  340. param->data_size = param->data_start + needed;
  341. nl->dev = 0; /* Flags no data */
  342. /*
  343. * Now loop through filling out the names.
  344. */
  345. for (i = 0; i < NUM_BUCKETS; i++) {
  346. list_for_each_entry (hc, _name_buckets + i, name_list) {
  347. if (old_nl)
  348. old_nl->next = (uint32_t) ((void *) nl -
  349. (void *) old_nl);
  350. disk = dm_disk(hc->md);
  351. nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
  352. nl->next = 0;
  353. strcpy(nl->name, hc->name);
  354. old_nl = nl;
  355. nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
  356. }
  357. }
  358. out:
  359. up_write(&_hash_lock);
  360. return 0;
  361. }
  362. static void list_version_get_needed(struct target_type *tt, void *needed_param)
  363. {
  364. size_t *needed = needed_param;
  365. *needed += sizeof(struct dm_target_versions);
  366. *needed += strlen(tt->name);
  367. *needed += ALIGN_MASK;
  368. }
  369. static void list_version_get_info(struct target_type *tt, void *param)
  370. {
  371. struct vers_iter *info = param;
  372. /* Check space - it might have changed since the first iteration */
  373. if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
  374. info->end) {
  375. info->flags = DM_BUFFER_FULL_FLAG;
  376. return;
  377. }
  378. if (info->old_vers)
  379. info->old_vers->next = (uint32_t) ((void *)info->vers -
  380. (void *)info->old_vers);
  381. info->vers->version[0] = tt->version[0];
  382. info->vers->version[1] = tt->version[1];
  383. info->vers->version[2] = tt->version[2];
  384. info->vers->next = 0;
  385. strcpy(info->vers->name, tt->name);
  386. info->old_vers = info->vers;
  387. info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
  388. }
  389. static int list_versions(struct dm_ioctl *param, size_t param_size)
  390. {
  391. size_t len, needed = 0;
  392. struct dm_target_versions *vers;
  393. struct vers_iter iter_info;
  394. /*
  395. * Loop through all the devices working out how much
  396. * space we need.
  397. */
  398. dm_target_iterate(list_version_get_needed, &needed);
  399. /*
  400. * Grab our output buffer.
  401. */
  402. vers = get_result_buffer(param, param_size, &len);
  403. if (len < needed) {
  404. param->flags |= DM_BUFFER_FULL_FLAG;
  405. goto out;
  406. }
  407. param->data_size = param->data_start + needed;
  408. iter_info.param_size = param_size;
  409. iter_info.old_vers = NULL;
  410. iter_info.vers = vers;
  411. iter_info.flags = 0;
  412. iter_info.end = (char *)vers+len;
  413. /*
  414. * Now loop through filling out the names & versions.
  415. */
  416. dm_target_iterate(list_version_get_info, &iter_info);
  417. param->flags |= iter_info.flags;
  418. out:
  419. return 0;
  420. }
  421. static int check_name(const char *name)
  422. {
  423. if (strchr(name, '/')) {
  424. DMWARN("invalid device name");
  425. return -EINVAL;
  426. }
  427. return 0;
  428. }
  429. /*
  430. * Fills in a dm_ioctl structure, ready for sending back to
  431. * userland.
  432. */
  433. static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
  434. {
  435. struct gendisk *disk = dm_disk(md);
  436. struct dm_table *table;
  437. struct block_device *bdev;
  438. param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
  439. DM_ACTIVE_PRESENT_FLAG);
  440. if (dm_suspended(md))
  441. param->flags |= DM_SUSPEND_FLAG;
  442. param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
  443. if (!(param->flags & DM_SKIP_BDGET_FLAG)) {
  444. bdev = bdget_disk(disk, 0);
  445. if (!bdev)
  446. return -ENXIO;
  447. /*
  448. * Yes, this will be out of date by the time it gets back
  449. * to userland, but it is still very useful for
  450. * debugging.
  451. */
  452. param->open_count = bdev->bd_openers;
  453. bdput(bdev);
  454. } else
  455. param->open_count = -1;
  456. if (disk->policy)
  457. param->flags |= DM_READONLY_FLAG;
  458. param->event_nr = dm_get_event_nr(md);
  459. table = dm_get_table(md);
  460. if (table) {
  461. param->flags |= DM_ACTIVE_PRESENT_FLAG;
  462. param->target_count = dm_table_get_num_targets(table);
  463. dm_table_put(table);
  464. } else
  465. param->target_count = 0;
  466. return 0;
  467. }
  468. static int dev_create(struct dm_ioctl *param, size_t param_size)
  469. {
  470. int r;
  471. struct mapped_device *md;
  472. r = check_name(param->name);
  473. if (r)
  474. return r;
  475. if (param->flags & DM_PERSISTENT_DEV_FLAG)
  476. r = dm_create_with_minor(MINOR(huge_decode_dev(param->dev)), &md);
  477. else
  478. r = dm_create(&md);
  479. if (r)
  480. return r;
  481. r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
  482. if (r) {
  483. dm_put(md);
  484. return r;
  485. }
  486. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  487. r = __dev_status(md, param);
  488. dm_put(md);
  489. return r;
  490. }
  491. /*
  492. * Always use UUID for lookups if it's present, otherwise use name or dev.
  493. */
  494. static inline struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
  495. {
  496. if (*param->uuid)
  497. return __get_uuid_cell(param->uuid);
  498. else if (*param->name)
  499. return __get_name_cell(param->name);
  500. else
  501. return dm_get_mdptr(huge_decode_dev(param->dev));
  502. }
  503. static inline struct mapped_device *find_device(struct dm_ioctl *param)
  504. {
  505. struct hash_cell *hc;
  506. struct mapped_device *md = NULL;
  507. down_read(&_hash_lock);
  508. hc = __find_device_hash_cell(param);
  509. if (hc) {
  510. md = hc->md;
  511. dm_get(md);
  512. /*
  513. * Sneakily write in both the name and the uuid
  514. * while we have the cell.
  515. */
  516. strncpy(param->name, hc->name, sizeof(param->name));
  517. if (hc->uuid)
  518. strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
  519. else
  520. param->uuid[0] = '\0';
  521. if (hc->new_map)
  522. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  523. else
  524. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  525. }
  526. up_read(&_hash_lock);
  527. return md;
  528. }
  529. static int dev_remove(struct dm_ioctl *param, size_t param_size)
  530. {
  531. struct hash_cell *hc;
  532. down_write(&_hash_lock);
  533. hc = __find_device_hash_cell(param);
  534. if (!hc) {
  535. DMWARN("device doesn't appear to be in the dev hash table.");
  536. up_write(&_hash_lock);
  537. return -ENXIO;
  538. }
  539. __hash_remove(hc);
  540. up_write(&_hash_lock);
  541. param->data_size = 0;
  542. return 0;
  543. }
  544. /*
  545. * Check a string doesn't overrun the chunk of
  546. * memory we copied from userland.
  547. */
  548. static int invalid_str(char *str, void *end)
  549. {
  550. while ((void *) str < end)
  551. if (!*str++)
  552. return 0;
  553. return -EINVAL;
  554. }
  555. static int dev_rename(struct dm_ioctl *param, size_t param_size)
  556. {
  557. int r;
  558. char *new_name = (char *) param + param->data_start;
  559. if (new_name < (char *) (param + 1) ||
  560. invalid_str(new_name, (void *) param + param_size)) {
  561. DMWARN("Invalid new logical volume name supplied.");
  562. return -EINVAL;
  563. }
  564. r = check_name(new_name);
  565. if (r)
  566. return r;
  567. param->data_size = 0;
  568. return dm_hash_rename(param->name, new_name);
  569. }
  570. static int do_suspend(struct dm_ioctl *param)
  571. {
  572. int r = 0;
  573. struct mapped_device *md;
  574. md = find_device(param);
  575. if (!md)
  576. return -ENXIO;
  577. if (!dm_suspended(md))
  578. r = dm_suspend(md, 1);
  579. if (!r)
  580. r = __dev_status(md, param);
  581. dm_put(md);
  582. return r;
  583. }
  584. static int do_resume(struct dm_ioctl *param)
  585. {
  586. int r = 0;
  587. struct hash_cell *hc;
  588. struct mapped_device *md;
  589. struct dm_table *new_map;
  590. down_write(&_hash_lock);
  591. hc = __find_device_hash_cell(param);
  592. if (!hc) {
  593. DMWARN("device doesn't appear to be in the dev hash table.");
  594. up_write(&_hash_lock);
  595. return -ENXIO;
  596. }
  597. md = hc->md;
  598. dm_get(md);
  599. new_map = hc->new_map;
  600. hc->new_map = NULL;
  601. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  602. up_write(&_hash_lock);
  603. /* Do we need to load a new map ? */
  604. if (new_map) {
  605. /* Suspend if it isn't already suspended */
  606. if (!dm_suspended(md))
  607. dm_suspend(md, 1);
  608. r = dm_swap_table(md, new_map);
  609. if (r) {
  610. dm_put(md);
  611. dm_table_put(new_map);
  612. return r;
  613. }
  614. if (dm_table_get_mode(new_map) & FMODE_WRITE)
  615. set_disk_ro(dm_disk(md), 0);
  616. else
  617. set_disk_ro(dm_disk(md), 1);
  618. dm_table_put(new_map);
  619. }
  620. if (dm_suspended(md))
  621. r = dm_resume(md);
  622. if (!r)
  623. r = __dev_status(md, param);
  624. dm_put(md);
  625. return r;
  626. }
  627. /*
  628. * Set or unset the suspension state of a device.
  629. * If the device already is in the requested state we just return its status.
  630. */
  631. static int dev_suspend(struct dm_ioctl *param, size_t param_size)
  632. {
  633. if (param->flags & DM_SUSPEND_FLAG)
  634. return do_suspend(param);
  635. return do_resume(param);
  636. }
  637. /*
  638. * Copies device info back to user space, used by
  639. * the create and info ioctls.
  640. */
  641. static int dev_status(struct dm_ioctl *param, size_t param_size)
  642. {
  643. int r;
  644. struct mapped_device *md;
  645. md = find_device(param);
  646. if (!md)
  647. return -ENXIO;
  648. r = __dev_status(md, param);
  649. dm_put(md);
  650. return r;
  651. }
  652. /*
  653. * Build up the status struct for each target
  654. */
  655. static void retrieve_status(struct dm_table *table,
  656. struct dm_ioctl *param, size_t param_size)
  657. {
  658. unsigned int i, num_targets;
  659. struct dm_target_spec *spec;
  660. char *outbuf, *outptr;
  661. status_type_t type;
  662. size_t remaining, len, used = 0;
  663. outptr = outbuf = get_result_buffer(param, param_size, &len);
  664. if (param->flags & DM_STATUS_TABLE_FLAG)
  665. type = STATUSTYPE_TABLE;
  666. else
  667. type = STATUSTYPE_INFO;
  668. /* Get all the target info */
  669. num_targets = dm_table_get_num_targets(table);
  670. for (i = 0; i < num_targets; i++) {
  671. struct dm_target *ti = dm_table_get_target(table, i);
  672. remaining = len - (outptr - outbuf);
  673. if (remaining <= sizeof(struct dm_target_spec)) {
  674. param->flags |= DM_BUFFER_FULL_FLAG;
  675. break;
  676. }
  677. spec = (struct dm_target_spec *) outptr;
  678. spec->status = 0;
  679. spec->sector_start = ti->begin;
  680. spec->length = ti->len;
  681. strncpy(spec->target_type, ti->type->name,
  682. sizeof(spec->target_type));
  683. outptr += sizeof(struct dm_target_spec);
  684. remaining = len - (outptr - outbuf);
  685. if (remaining <= 0) {
  686. param->flags |= DM_BUFFER_FULL_FLAG;
  687. break;
  688. }
  689. /* Get the status/table string from the target driver */
  690. if (ti->type->status) {
  691. if (ti->type->status(ti, type, outptr, remaining)) {
  692. param->flags |= DM_BUFFER_FULL_FLAG;
  693. break;
  694. }
  695. } else
  696. outptr[0] = '\0';
  697. outptr += strlen(outptr) + 1;
  698. used = param->data_start + (outptr - outbuf);
  699. outptr = align_ptr(outptr);
  700. spec->next = outptr - outbuf;
  701. }
  702. if (used)
  703. param->data_size = used;
  704. param->target_count = num_targets;
  705. }
  706. /*
  707. * Wait for a device to report an event
  708. */
  709. static int dev_wait(struct dm_ioctl *param, size_t param_size)
  710. {
  711. int r;
  712. struct mapped_device *md;
  713. struct dm_table *table;
  714. md = find_device(param);
  715. if (!md)
  716. return -ENXIO;
  717. /*
  718. * Wait for a notification event
  719. */
  720. if (dm_wait_event(md, param->event_nr)) {
  721. r = -ERESTARTSYS;
  722. goto out;
  723. }
  724. /*
  725. * The userland program is going to want to know what
  726. * changed to trigger the event, so we may as well tell
  727. * him and save an ioctl.
  728. */
  729. r = __dev_status(md, param);
  730. if (r)
  731. goto out;
  732. table = dm_get_table(md);
  733. if (table) {
  734. retrieve_status(table, param, param_size);
  735. dm_table_put(table);
  736. }
  737. out:
  738. dm_put(md);
  739. return r;
  740. }
  741. static inline int get_mode(struct dm_ioctl *param)
  742. {
  743. int mode = FMODE_READ | FMODE_WRITE;
  744. if (param->flags & DM_READONLY_FLAG)
  745. mode = FMODE_READ;
  746. return mode;
  747. }
  748. static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
  749. struct dm_target_spec **spec, char **target_params)
  750. {
  751. *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
  752. *target_params = (char *) (*spec + 1);
  753. if (*spec < (last + 1))
  754. return -EINVAL;
  755. return invalid_str(*target_params, end);
  756. }
  757. static int populate_table(struct dm_table *table,
  758. struct dm_ioctl *param, size_t param_size)
  759. {
  760. int r;
  761. unsigned int i = 0;
  762. struct dm_target_spec *spec = (struct dm_target_spec *) param;
  763. uint32_t next = param->data_start;
  764. void *end = (void *) param + param_size;
  765. char *target_params;
  766. if (!param->target_count) {
  767. DMWARN("populate_table: no targets specified");
  768. return -EINVAL;
  769. }
  770. for (i = 0; i < param->target_count; i++) {
  771. r = next_target(spec, next, end, &spec, &target_params);
  772. if (r) {
  773. DMWARN("unable to find target");
  774. return r;
  775. }
  776. r = dm_table_add_target(table, spec->target_type,
  777. (sector_t) spec->sector_start,
  778. (sector_t) spec->length,
  779. target_params);
  780. if (r) {
  781. DMWARN("error adding target to table");
  782. return r;
  783. }
  784. next = spec->next;
  785. }
  786. return dm_table_complete(table);
  787. }
  788. static int table_load(struct dm_ioctl *param, size_t param_size)
  789. {
  790. int r;
  791. struct hash_cell *hc;
  792. struct dm_table *t;
  793. r = dm_table_create(&t, get_mode(param), param->target_count);
  794. if (r)
  795. return r;
  796. r = populate_table(t, param, param_size);
  797. if (r) {
  798. dm_table_put(t);
  799. return r;
  800. }
  801. down_write(&_hash_lock);
  802. hc = __find_device_hash_cell(param);
  803. if (!hc) {
  804. DMWARN("device doesn't appear to be in the dev hash table.");
  805. up_write(&_hash_lock);
  806. dm_table_put(t);
  807. return -ENXIO;
  808. }
  809. if (hc->new_map)
  810. dm_table_put(hc->new_map);
  811. hc->new_map = t;
  812. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  813. r = __dev_status(hc->md, param);
  814. up_write(&_hash_lock);
  815. return r;
  816. }
  817. static int table_clear(struct dm_ioctl *param, size_t param_size)
  818. {
  819. int r;
  820. struct hash_cell *hc;
  821. down_write(&_hash_lock);
  822. hc = __find_device_hash_cell(param);
  823. if (!hc) {
  824. DMWARN("device doesn't appear to be in the dev hash table.");
  825. up_write(&_hash_lock);
  826. return -ENXIO;
  827. }
  828. if (hc->new_map) {
  829. dm_table_put(hc->new_map);
  830. hc->new_map = NULL;
  831. }
  832. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  833. r = __dev_status(hc->md, param);
  834. up_write(&_hash_lock);
  835. return r;
  836. }
  837. /*
  838. * Retrieves a list of devices used by a particular dm device.
  839. */
  840. static void retrieve_deps(struct dm_table *table,
  841. struct dm_ioctl *param, size_t param_size)
  842. {
  843. unsigned int count = 0;
  844. struct list_head *tmp;
  845. size_t len, needed;
  846. struct dm_dev *dd;
  847. struct dm_target_deps *deps;
  848. deps = get_result_buffer(param, param_size, &len);
  849. /*
  850. * Count the devices.
  851. */
  852. list_for_each (tmp, dm_table_get_devices(table))
  853. count++;
  854. /*
  855. * Check we have enough space.
  856. */
  857. needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
  858. if (len < needed) {
  859. param->flags |= DM_BUFFER_FULL_FLAG;
  860. return;
  861. }
  862. /*
  863. * Fill in the devices.
  864. */
  865. deps->count = count;
  866. count = 0;
  867. list_for_each_entry (dd, dm_table_get_devices(table), list)
  868. deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
  869. param->data_size = param->data_start + needed;
  870. }
  871. static int table_deps(struct dm_ioctl *param, size_t param_size)
  872. {
  873. int r = 0;
  874. struct mapped_device *md;
  875. struct dm_table *table;
  876. md = find_device(param);
  877. if (!md)
  878. return -ENXIO;
  879. r = __dev_status(md, param);
  880. if (r)
  881. goto out;
  882. table = dm_get_table(md);
  883. if (table) {
  884. retrieve_deps(table, param, param_size);
  885. dm_table_put(table);
  886. }
  887. out:
  888. dm_put(md);
  889. return r;
  890. }
  891. /*
  892. * Return the status of a device as a text string for each
  893. * target.
  894. */
  895. static int table_status(struct dm_ioctl *param, size_t param_size)
  896. {
  897. int r;
  898. struct mapped_device *md;
  899. struct dm_table *table;
  900. md = find_device(param);
  901. if (!md)
  902. return -ENXIO;
  903. r = __dev_status(md, param);
  904. if (r)
  905. goto out;
  906. table = dm_get_table(md);
  907. if (table) {
  908. retrieve_status(table, param, param_size);
  909. dm_table_put(table);
  910. }
  911. out:
  912. dm_put(md);
  913. return r;
  914. }
  915. /*
  916. * Pass a message to the target that's at the supplied device offset.
  917. */
  918. static int target_message(struct dm_ioctl *param, size_t param_size)
  919. {
  920. int r, argc;
  921. char **argv;
  922. struct mapped_device *md;
  923. struct dm_table *table;
  924. struct dm_target *ti;
  925. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  926. md = find_device(param);
  927. if (!md)
  928. return -ENXIO;
  929. r = __dev_status(md, param);
  930. if (r)
  931. goto out;
  932. if (tmsg < (struct dm_target_msg *) (param + 1) ||
  933. invalid_str(tmsg->message, (void *) param + param_size)) {
  934. DMWARN("Invalid target message parameters.");
  935. r = -EINVAL;
  936. goto out;
  937. }
  938. r = dm_split_args(&argc, &argv, tmsg->message);
  939. if (r) {
  940. DMWARN("Failed to split target message parameters");
  941. goto out;
  942. }
  943. table = dm_get_table(md);
  944. if (!table)
  945. goto out_argv;
  946. if (tmsg->sector >= dm_table_get_size(table)) {
  947. DMWARN("Target message sector outside device.");
  948. r = -EINVAL;
  949. goto out_table;
  950. }
  951. ti = dm_table_find_target(table, tmsg->sector);
  952. if (ti->type->message)
  953. r = ti->type->message(ti, argc, argv);
  954. else {
  955. DMWARN("Target type does not support messages");
  956. r = -EINVAL;
  957. }
  958. out_table:
  959. dm_table_put(table);
  960. out_argv:
  961. kfree(argv);
  962. out:
  963. param->data_size = 0;
  964. dm_put(md);
  965. return r;
  966. }
  967. /*-----------------------------------------------------------------
  968. * Implementation of open/close/ioctl on the special char
  969. * device.
  970. *---------------------------------------------------------------*/
  971. static ioctl_fn lookup_ioctl(unsigned int cmd)
  972. {
  973. static struct {
  974. int cmd;
  975. ioctl_fn fn;
  976. } _ioctls[] = {
  977. {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
  978. {DM_REMOVE_ALL_CMD, remove_all},
  979. {DM_LIST_DEVICES_CMD, list_devices},
  980. {DM_DEV_CREATE_CMD, dev_create},
  981. {DM_DEV_REMOVE_CMD, dev_remove},
  982. {DM_DEV_RENAME_CMD, dev_rename},
  983. {DM_DEV_SUSPEND_CMD, dev_suspend},
  984. {DM_DEV_STATUS_CMD, dev_status},
  985. {DM_DEV_WAIT_CMD, dev_wait},
  986. {DM_TABLE_LOAD_CMD, table_load},
  987. {DM_TABLE_CLEAR_CMD, table_clear},
  988. {DM_TABLE_DEPS_CMD, table_deps},
  989. {DM_TABLE_STATUS_CMD, table_status},
  990. {DM_LIST_VERSIONS_CMD, list_versions},
  991. {DM_TARGET_MSG_CMD, target_message}
  992. };
  993. return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
  994. }
  995. /*
  996. * As well as checking the version compatibility this always
  997. * copies the kernel interface version out.
  998. */
  999. static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
  1000. {
  1001. uint32_t version[3];
  1002. int r = 0;
  1003. if (copy_from_user(version, user->version, sizeof(version)))
  1004. return -EFAULT;
  1005. if ((DM_VERSION_MAJOR != version[0]) ||
  1006. (DM_VERSION_MINOR < version[1])) {
  1007. DMWARN("ioctl interface mismatch: "
  1008. "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1009. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1010. DM_VERSION_PATCHLEVEL,
  1011. version[0], version[1], version[2], cmd);
  1012. r = -EINVAL;
  1013. }
  1014. /*
  1015. * Fill in the kernel version.
  1016. */
  1017. version[0] = DM_VERSION_MAJOR;
  1018. version[1] = DM_VERSION_MINOR;
  1019. version[2] = DM_VERSION_PATCHLEVEL;
  1020. if (copy_to_user(user->version, version, sizeof(version)))
  1021. return -EFAULT;
  1022. return r;
  1023. }
  1024. static void free_params(struct dm_ioctl *param)
  1025. {
  1026. vfree(param);
  1027. }
  1028. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
  1029. {
  1030. struct dm_ioctl tmp, *dmi;
  1031. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1032. return -EFAULT;
  1033. if (tmp.data_size < sizeof(tmp))
  1034. return -EINVAL;
  1035. dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
  1036. if (!dmi)
  1037. return -ENOMEM;
  1038. if (copy_from_user(dmi, user, tmp.data_size)) {
  1039. vfree(dmi);
  1040. return -EFAULT;
  1041. }
  1042. *param = dmi;
  1043. return 0;
  1044. }
  1045. static int validate_params(uint cmd, struct dm_ioctl *param)
  1046. {
  1047. /* Always clear this flag */
  1048. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1049. /* Ignores parameters */
  1050. if (cmd == DM_REMOVE_ALL_CMD ||
  1051. cmd == DM_LIST_DEVICES_CMD ||
  1052. cmd == DM_LIST_VERSIONS_CMD)
  1053. return 0;
  1054. if ((cmd == DM_DEV_CREATE_CMD)) {
  1055. if (!*param->name) {
  1056. DMWARN("name not supplied when creating device");
  1057. return -EINVAL;
  1058. }
  1059. } else if ((*param->uuid && *param->name)) {
  1060. DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
  1061. return -EINVAL;
  1062. }
  1063. /* Ensure strings are terminated */
  1064. param->name[DM_NAME_LEN - 1] = '\0';
  1065. param->uuid[DM_UUID_LEN - 1] = '\0';
  1066. return 0;
  1067. }
  1068. static int ctl_ioctl(struct inode *inode, struct file *file,
  1069. uint command, ulong u)
  1070. {
  1071. int r = 0;
  1072. unsigned int cmd;
  1073. struct dm_ioctl *param;
  1074. struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
  1075. ioctl_fn fn = NULL;
  1076. size_t param_size;
  1077. /* only root can play with this */
  1078. if (!capable(CAP_SYS_ADMIN))
  1079. return -EACCES;
  1080. if (_IOC_TYPE(command) != DM_IOCTL)
  1081. return -ENOTTY;
  1082. cmd = _IOC_NR(command);
  1083. /*
  1084. * Check the interface version passed in. This also
  1085. * writes out the kernel's interface version.
  1086. */
  1087. r = check_version(cmd, user);
  1088. if (r)
  1089. return r;
  1090. /*
  1091. * Nothing more to do for the version command.
  1092. */
  1093. if (cmd == DM_VERSION_CMD)
  1094. return 0;
  1095. fn = lookup_ioctl(cmd);
  1096. if (!fn) {
  1097. DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
  1098. return -ENOTTY;
  1099. }
  1100. /*
  1101. * Trying to avoid low memory issues when a device is
  1102. * suspended.
  1103. */
  1104. current->flags |= PF_MEMALLOC;
  1105. /*
  1106. * Copy the parameters into kernel space.
  1107. */
  1108. r = copy_params(user, &param);
  1109. if (r) {
  1110. current->flags &= ~PF_MEMALLOC;
  1111. return r;
  1112. }
  1113. /*
  1114. * FIXME: eventually we will remove the PF_MEMALLOC flag
  1115. * here. However the tools still do nasty things like
  1116. * 'load' while a device is suspended.
  1117. */
  1118. r = validate_params(cmd, param);
  1119. if (r)
  1120. goto out;
  1121. param_size = param->data_size;
  1122. param->data_size = sizeof(*param);
  1123. r = fn(param, param_size);
  1124. /*
  1125. * Copy the results back to userland.
  1126. */
  1127. if (!r && copy_to_user(user, param, param->data_size))
  1128. r = -EFAULT;
  1129. out:
  1130. free_params(param);
  1131. current->flags &= ~PF_MEMALLOC;
  1132. return r;
  1133. }
  1134. static struct file_operations _ctl_fops = {
  1135. .ioctl = ctl_ioctl,
  1136. .owner = THIS_MODULE,
  1137. };
  1138. static struct miscdevice _dm_misc = {
  1139. .minor = MISC_DYNAMIC_MINOR,
  1140. .name = DM_NAME,
  1141. .devfs_name = "mapper/control",
  1142. .fops = &_ctl_fops
  1143. };
  1144. /*
  1145. * Create misc character device and link to DM_DIR/control.
  1146. */
  1147. int __init dm_interface_init(void)
  1148. {
  1149. int r;
  1150. r = dm_hash_init();
  1151. if (r)
  1152. return r;
  1153. r = misc_register(&_dm_misc);
  1154. if (r) {
  1155. DMERR("misc_register failed for control device");
  1156. dm_hash_exit();
  1157. return r;
  1158. }
  1159. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1160. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1161. DM_DRIVER_EMAIL);
  1162. return 0;
  1163. }
  1164. void dm_interface_exit(void)
  1165. {
  1166. if (misc_deregister(&_dm_misc) < 0)
  1167. DMERR("misc_deregister failed for control device");
  1168. dm_hash_exit();
  1169. }