dm-ioctl.c 29 KB

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