dm-ioctl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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. if (hc->new_map)
  203. dm_table_put(hc->new_map);
  204. dm_put(hc->md);
  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 struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
  495. {
  496. struct mapped_device *md;
  497. void *mdptr = NULL;
  498. if (*param->uuid)
  499. return __get_uuid_cell(param->uuid);
  500. if (*param->name)
  501. return __get_name_cell(param->name);
  502. md = dm_get_md(huge_decode_dev(param->dev));
  503. if (md) {
  504. mdptr = dm_get_mdptr(md);
  505. dm_put(md);
  506. }
  507. return mdptr;
  508. }
  509. static struct mapped_device *find_device(struct dm_ioctl *param)
  510. {
  511. struct hash_cell *hc;
  512. struct mapped_device *md = NULL;
  513. down_read(&_hash_lock);
  514. hc = __find_device_hash_cell(param);
  515. if (hc) {
  516. md = hc->md;
  517. dm_get(md);
  518. /*
  519. * Sneakily write in both the name and the uuid
  520. * while we have the cell.
  521. */
  522. strncpy(param->name, hc->name, sizeof(param->name));
  523. if (hc->uuid)
  524. strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
  525. else
  526. param->uuid[0] = '\0';
  527. if (hc->new_map)
  528. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  529. else
  530. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  531. }
  532. up_read(&_hash_lock);
  533. return md;
  534. }
  535. static int dev_remove(struct dm_ioctl *param, size_t param_size)
  536. {
  537. struct hash_cell *hc;
  538. down_write(&_hash_lock);
  539. hc = __find_device_hash_cell(param);
  540. if (!hc) {
  541. DMWARN("device doesn't appear to be in the dev hash table.");
  542. up_write(&_hash_lock);
  543. return -ENXIO;
  544. }
  545. __hash_remove(hc);
  546. up_write(&_hash_lock);
  547. param->data_size = 0;
  548. return 0;
  549. }
  550. /*
  551. * Check a string doesn't overrun the chunk of
  552. * memory we copied from userland.
  553. */
  554. static int invalid_str(char *str, void *end)
  555. {
  556. while ((void *) str < end)
  557. if (!*str++)
  558. return 0;
  559. return -EINVAL;
  560. }
  561. static int dev_rename(struct dm_ioctl *param, size_t param_size)
  562. {
  563. int r;
  564. char *new_name = (char *) param + param->data_start;
  565. if (new_name < (char *) (param + 1) ||
  566. invalid_str(new_name, (void *) param + param_size)) {
  567. DMWARN("Invalid new logical volume name supplied.");
  568. return -EINVAL;
  569. }
  570. r = check_name(new_name);
  571. if (r)
  572. return r;
  573. param->data_size = 0;
  574. return dm_hash_rename(param->name, new_name);
  575. }
  576. static int do_suspend(struct dm_ioctl *param)
  577. {
  578. int r = 0;
  579. int do_lockfs = 1;
  580. struct mapped_device *md;
  581. md = find_device(param);
  582. if (!md)
  583. return -ENXIO;
  584. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  585. do_lockfs = 0;
  586. if (!dm_suspended(md))
  587. r = dm_suspend(md, do_lockfs);
  588. if (!r)
  589. r = __dev_status(md, param);
  590. dm_put(md);
  591. return r;
  592. }
  593. static int do_resume(struct dm_ioctl *param)
  594. {
  595. int r = 0;
  596. int do_lockfs = 1;
  597. struct hash_cell *hc;
  598. struct mapped_device *md;
  599. struct dm_table *new_map;
  600. down_write(&_hash_lock);
  601. hc = __find_device_hash_cell(param);
  602. if (!hc) {
  603. DMWARN("device doesn't appear to be in the dev hash table.");
  604. up_write(&_hash_lock);
  605. return -ENXIO;
  606. }
  607. md = hc->md;
  608. dm_get(md);
  609. new_map = hc->new_map;
  610. hc->new_map = NULL;
  611. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  612. up_write(&_hash_lock);
  613. /* Do we need to load a new map ? */
  614. if (new_map) {
  615. /* Suspend if it isn't already suspended */
  616. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  617. do_lockfs = 0;
  618. if (!dm_suspended(md))
  619. dm_suspend(md, do_lockfs);
  620. r = dm_swap_table(md, new_map);
  621. if (r) {
  622. dm_put(md);
  623. dm_table_put(new_map);
  624. return r;
  625. }
  626. if (dm_table_get_mode(new_map) & FMODE_WRITE)
  627. set_disk_ro(dm_disk(md), 0);
  628. else
  629. set_disk_ro(dm_disk(md), 1);
  630. dm_table_put(new_map);
  631. }
  632. if (dm_suspended(md))
  633. r = dm_resume(md);
  634. if (!r)
  635. r = __dev_status(md, param);
  636. dm_put(md);
  637. return r;
  638. }
  639. /*
  640. * Set or unset the suspension state of a device.
  641. * If the device already is in the requested state we just return its status.
  642. */
  643. static int dev_suspend(struct dm_ioctl *param, size_t param_size)
  644. {
  645. if (param->flags & DM_SUSPEND_FLAG)
  646. return do_suspend(param);
  647. return do_resume(param);
  648. }
  649. /*
  650. * Copies device info back to user space, used by
  651. * the create and info ioctls.
  652. */
  653. static int dev_status(struct dm_ioctl *param, size_t param_size)
  654. {
  655. int r;
  656. struct mapped_device *md;
  657. md = find_device(param);
  658. if (!md)
  659. return -ENXIO;
  660. r = __dev_status(md, param);
  661. dm_put(md);
  662. return r;
  663. }
  664. /*
  665. * Build up the status struct for each target
  666. */
  667. static void retrieve_status(struct dm_table *table,
  668. struct dm_ioctl *param, size_t param_size)
  669. {
  670. unsigned int i, num_targets;
  671. struct dm_target_spec *spec;
  672. char *outbuf, *outptr;
  673. status_type_t type;
  674. size_t remaining, len, used = 0;
  675. outptr = outbuf = get_result_buffer(param, param_size, &len);
  676. if (param->flags & DM_STATUS_TABLE_FLAG)
  677. type = STATUSTYPE_TABLE;
  678. else
  679. type = STATUSTYPE_INFO;
  680. /* Get all the target info */
  681. num_targets = dm_table_get_num_targets(table);
  682. for (i = 0; i < num_targets; i++) {
  683. struct dm_target *ti = dm_table_get_target(table, i);
  684. remaining = len - (outptr - outbuf);
  685. if (remaining <= sizeof(struct dm_target_spec)) {
  686. param->flags |= DM_BUFFER_FULL_FLAG;
  687. break;
  688. }
  689. spec = (struct dm_target_spec *) outptr;
  690. spec->status = 0;
  691. spec->sector_start = ti->begin;
  692. spec->length = ti->len;
  693. strncpy(spec->target_type, ti->type->name,
  694. sizeof(spec->target_type));
  695. outptr += sizeof(struct dm_target_spec);
  696. remaining = len - (outptr - outbuf);
  697. if (remaining <= 0) {
  698. param->flags |= DM_BUFFER_FULL_FLAG;
  699. break;
  700. }
  701. /* Get the status/table string from the target driver */
  702. if (ti->type->status) {
  703. if (ti->type->status(ti, type, outptr, remaining)) {
  704. param->flags |= DM_BUFFER_FULL_FLAG;
  705. break;
  706. }
  707. } else
  708. outptr[0] = '\0';
  709. outptr += strlen(outptr) + 1;
  710. used = param->data_start + (outptr - outbuf);
  711. outptr = align_ptr(outptr);
  712. spec->next = outptr - outbuf;
  713. }
  714. if (used)
  715. param->data_size = used;
  716. param->target_count = num_targets;
  717. }
  718. /*
  719. * Wait for a device to report an event
  720. */
  721. static int dev_wait(struct dm_ioctl *param, size_t param_size)
  722. {
  723. int r;
  724. struct mapped_device *md;
  725. struct dm_table *table;
  726. md = find_device(param);
  727. if (!md)
  728. return -ENXIO;
  729. /*
  730. * Wait for a notification event
  731. */
  732. if (dm_wait_event(md, param->event_nr)) {
  733. r = -ERESTARTSYS;
  734. goto out;
  735. }
  736. /*
  737. * The userland program is going to want to know what
  738. * changed to trigger the event, so we may as well tell
  739. * him and save an ioctl.
  740. */
  741. r = __dev_status(md, param);
  742. if (r)
  743. goto out;
  744. table = dm_get_table(md);
  745. if (table) {
  746. retrieve_status(table, param, param_size);
  747. dm_table_put(table);
  748. }
  749. out:
  750. dm_put(md);
  751. return r;
  752. }
  753. static inline int get_mode(struct dm_ioctl *param)
  754. {
  755. int mode = FMODE_READ | FMODE_WRITE;
  756. if (param->flags & DM_READONLY_FLAG)
  757. mode = FMODE_READ;
  758. return mode;
  759. }
  760. static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
  761. struct dm_target_spec **spec, char **target_params)
  762. {
  763. *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
  764. *target_params = (char *) (*spec + 1);
  765. if (*spec < (last + 1))
  766. return -EINVAL;
  767. return invalid_str(*target_params, end);
  768. }
  769. static int populate_table(struct dm_table *table,
  770. struct dm_ioctl *param, size_t param_size)
  771. {
  772. int r;
  773. unsigned int i = 0;
  774. struct dm_target_spec *spec = (struct dm_target_spec *) param;
  775. uint32_t next = param->data_start;
  776. void *end = (void *) param + param_size;
  777. char *target_params;
  778. if (!param->target_count) {
  779. DMWARN("populate_table: no targets specified");
  780. return -EINVAL;
  781. }
  782. for (i = 0; i < param->target_count; i++) {
  783. r = next_target(spec, next, end, &spec, &target_params);
  784. if (r) {
  785. DMWARN("unable to find target");
  786. return r;
  787. }
  788. r = dm_table_add_target(table, spec->target_type,
  789. (sector_t) spec->sector_start,
  790. (sector_t) spec->length,
  791. target_params);
  792. if (r) {
  793. DMWARN("error adding target to table");
  794. return r;
  795. }
  796. next = spec->next;
  797. }
  798. return dm_table_complete(table);
  799. }
  800. static int table_load(struct dm_ioctl *param, size_t param_size)
  801. {
  802. int r;
  803. struct hash_cell *hc;
  804. struct dm_table *t;
  805. struct mapped_device *md;
  806. md = find_device(param);
  807. if (!md)
  808. return -ENXIO;
  809. r = dm_table_create(&t, get_mode(param), param->target_count, md);
  810. if (r)
  811. goto out;
  812. r = populate_table(t, param, param_size);
  813. if (r) {
  814. dm_table_put(t);
  815. goto out;
  816. }
  817. down_write(&_hash_lock);
  818. hc = dm_get_mdptr(md);
  819. if (!hc || hc->md != md) {
  820. DMWARN("device has been removed from the dev hash table.");
  821. dm_table_put(t);
  822. up_write(&_hash_lock);
  823. r = -ENXIO;
  824. goto out;
  825. }
  826. if (hc->new_map)
  827. dm_table_put(hc->new_map);
  828. hc->new_map = t;
  829. up_write(&_hash_lock);
  830. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  831. r = __dev_status(md, param);
  832. out:
  833. dm_put(md);
  834. return r;
  835. }
  836. static int table_clear(struct dm_ioctl *param, size_t param_size)
  837. {
  838. int r;
  839. struct hash_cell *hc;
  840. down_write(&_hash_lock);
  841. hc = __find_device_hash_cell(param);
  842. if (!hc) {
  843. DMWARN("device doesn't appear to be in the dev hash table.");
  844. up_write(&_hash_lock);
  845. return -ENXIO;
  846. }
  847. if (hc->new_map) {
  848. dm_table_put(hc->new_map);
  849. hc->new_map = NULL;
  850. }
  851. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  852. r = __dev_status(hc->md, param);
  853. up_write(&_hash_lock);
  854. return r;
  855. }
  856. /*
  857. * Retrieves a list of devices used by a particular dm device.
  858. */
  859. static void retrieve_deps(struct dm_table *table,
  860. struct dm_ioctl *param, size_t param_size)
  861. {
  862. unsigned int count = 0;
  863. struct list_head *tmp;
  864. size_t len, needed;
  865. struct dm_dev *dd;
  866. struct dm_target_deps *deps;
  867. deps = get_result_buffer(param, param_size, &len);
  868. /*
  869. * Count the devices.
  870. */
  871. list_for_each (tmp, dm_table_get_devices(table))
  872. count++;
  873. /*
  874. * Check we have enough space.
  875. */
  876. needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
  877. if (len < needed) {
  878. param->flags |= DM_BUFFER_FULL_FLAG;
  879. return;
  880. }
  881. /*
  882. * Fill in the devices.
  883. */
  884. deps->count = count;
  885. count = 0;
  886. list_for_each_entry (dd, dm_table_get_devices(table), list)
  887. deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
  888. param->data_size = param->data_start + needed;
  889. }
  890. static int table_deps(struct dm_ioctl *param, size_t param_size)
  891. {
  892. int r = 0;
  893. struct mapped_device *md;
  894. struct dm_table *table;
  895. md = find_device(param);
  896. if (!md)
  897. return -ENXIO;
  898. r = __dev_status(md, param);
  899. if (r)
  900. goto out;
  901. table = dm_get_table(md);
  902. if (table) {
  903. retrieve_deps(table, param, param_size);
  904. dm_table_put(table);
  905. }
  906. out:
  907. dm_put(md);
  908. return r;
  909. }
  910. /*
  911. * Return the status of a device as a text string for each
  912. * target.
  913. */
  914. static int table_status(struct dm_ioctl *param, size_t param_size)
  915. {
  916. int r;
  917. struct mapped_device *md;
  918. struct dm_table *table;
  919. md = find_device(param);
  920. if (!md)
  921. return -ENXIO;
  922. r = __dev_status(md, param);
  923. if (r)
  924. goto out;
  925. table = dm_get_table(md);
  926. if (table) {
  927. retrieve_status(table, param, param_size);
  928. dm_table_put(table);
  929. }
  930. out:
  931. dm_put(md);
  932. return r;
  933. }
  934. /*
  935. * Pass a message to the target that's at the supplied device offset.
  936. */
  937. static int target_message(struct dm_ioctl *param, size_t param_size)
  938. {
  939. int r, argc;
  940. char **argv;
  941. struct mapped_device *md;
  942. struct dm_table *table;
  943. struct dm_target *ti;
  944. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  945. md = find_device(param);
  946. if (!md)
  947. return -ENXIO;
  948. r = __dev_status(md, param);
  949. if (r)
  950. goto out;
  951. if (tmsg < (struct dm_target_msg *) (param + 1) ||
  952. invalid_str(tmsg->message, (void *) param + param_size)) {
  953. DMWARN("Invalid target message parameters.");
  954. r = -EINVAL;
  955. goto out;
  956. }
  957. r = dm_split_args(&argc, &argv, tmsg->message);
  958. if (r) {
  959. DMWARN("Failed to split target message parameters");
  960. goto out;
  961. }
  962. table = dm_get_table(md);
  963. if (!table)
  964. goto out_argv;
  965. if (tmsg->sector >= dm_table_get_size(table)) {
  966. DMWARN("Target message sector outside device.");
  967. r = -EINVAL;
  968. goto out_table;
  969. }
  970. ti = dm_table_find_target(table, tmsg->sector);
  971. if (ti->type->message)
  972. r = ti->type->message(ti, argc, argv);
  973. else {
  974. DMWARN("Target type does not support messages");
  975. r = -EINVAL;
  976. }
  977. out_table:
  978. dm_table_put(table);
  979. out_argv:
  980. kfree(argv);
  981. out:
  982. param->data_size = 0;
  983. dm_put(md);
  984. return r;
  985. }
  986. /*-----------------------------------------------------------------
  987. * Implementation of open/close/ioctl on the special char
  988. * device.
  989. *---------------------------------------------------------------*/
  990. static ioctl_fn lookup_ioctl(unsigned int cmd)
  991. {
  992. static struct {
  993. int cmd;
  994. ioctl_fn fn;
  995. } _ioctls[] = {
  996. {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
  997. {DM_REMOVE_ALL_CMD, remove_all},
  998. {DM_LIST_DEVICES_CMD, list_devices},
  999. {DM_DEV_CREATE_CMD, dev_create},
  1000. {DM_DEV_REMOVE_CMD, dev_remove},
  1001. {DM_DEV_RENAME_CMD, dev_rename},
  1002. {DM_DEV_SUSPEND_CMD, dev_suspend},
  1003. {DM_DEV_STATUS_CMD, dev_status},
  1004. {DM_DEV_WAIT_CMD, dev_wait},
  1005. {DM_TABLE_LOAD_CMD, table_load},
  1006. {DM_TABLE_CLEAR_CMD, table_clear},
  1007. {DM_TABLE_DEPS_CMD, table_deps},
  1008. {DM_TABLE_STATUS_CMD, table_status},
  1009. {DM_LIST_VERSIONS_CMD, list_versions},
  1010. {DM_TARGET_MSG_CMD, target_message}
  1011. };
  1012. return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
  1013. }
  1014. /*
  1015. * As well as checking the version compatibility this always
  1016. * copies the kernel interface version out.
  1017. */
  1018. static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
  1019. {
  1020. uint32_t version[3];
  1021. int r = 0;
  1022. if (copy_from_user(version, user->version, sizeof(version)))
  1023. return -EFAULT;
  1024. if ((DM_VERSION_MAJOR != version[0]) ||
  1025. (DM_VERSION_MINOR < version[1])) {
  1026. DMWARN("ioctl interface mismatch: "
  1027. "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1028. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1029. DM_VERSION_PATCHLEVEL,
  1030. version[0], version[1], version[2], cmd);
  1031. r = -EINVAL;
  1032. }
  1033. /*
  1034. * Fill in the kernel version.
  1035. */
  1036. version[0] = DM_VERSION_MAJOR;
  1037. version[1] = DM_VERSION_MINOR;
  1038. version[2] = DM_VERSION_PATCHLEVEL;
  1039. if (copy_to_user(user->version, version, sizeof(version)))
  1040. return -EFAULT;
  1041. return r;
  1042. }
  1043. static void free_params(struct dm_ioctl *param)
  1044. {
  1045. vfree(param);
  1046. }
  1047. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
  1048. {
  1049. struct dm_ioctl tmp, *dmi;
  1050. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1051. return -EFAULT;
  1052. if (tmp.data_size < sizeof(tmp))
  1053. return -EINVAL;
  1054. dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
  1055. if (!dmi)
  1056. return -ENOMEM;
  1057. if (copy_from_user(dmi, user, tmp.data_size)) {
  1058. vfree(dmi);
  1059. return -EFAULT;
  1060. }
  1061. *param = dmi;
  1062. return 0;
  1063. }
  1064. static int validate_params(uint cmd, struct dm_ioctl *param)
  1065. {
  1066. /* Always clear this flag */
  1067. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1068. /* Ignores parameters */
  1069. if (cmd == DM_REMOVE_ALL_CMD ||
  1070. cmd == DM_LIST_DEVICES_CMD ||
  1071. cmd == DM_LIST_VERSIONS_CMD)
  1072. return 0;
  1073. if ((cmd == DM_DEV_CREATE_CMD)) {
  1074. if (!*param->name) {
  1075. DMWARN("name not supplied when creating device");
  1076. return -EINVAL;
  1077. }
  1078. } else if ((*param->uuid && *param->name)) {
  1079. DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
  1080. return -EINVAL;
  1081. }
  1082. /* Ensure strings are terminated */
  1083. param->name[DM_NAME_LEN - 1] = '\0';
  1084. param->uuid[DM_UUID_LEN - 1] = '\0';
  1085. return 0;
  1086. }
  1087. static int ctl_ioctl(struct inode *inode, struct file *file,
  1088. uint command, ulong u)
  1089. {
  1090. int r = 0;
  1091. unsigned int cmd;
  1092. struct dm_ioctl *param;
  1093. struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
  1094. ioctl_fn fn = NULL;
  1095. size_t param_size;
  1096. /* only root can play with this */
  1097. if (!capable(CAP_SYS_ADMIN))
  1098. return -EACCES;
  1099. if (_IOC_TYPE(command) != DM_IOCTL)
  1100. return -ENOTTY;
  1101. cmd = _IOC_NR(command);
  1102. /*
  1103. * Check the interface version passed in. This also
  1104. * writes out the kernel's interface version.
  1105. */
  1106. r = check_version(cmd, user);
  1107. if (r)
  1108. return r;
  1109. /*
  1110. * Nothing more to do for the version command.
  1111. */
  1112. if (cmd == DM_VERSION_CMD)
  1113. return 0;
  1114. fn = lookup_ioctl(cmd);
  1115. if (!fn) {
  1116. DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
  1117. return -ENOTTY;
  1118. }
  1119. /*
  1120. * Trying to avoid low memory issues when a device is
  1121. * suspended.
  1122. */
  1123. current->flags |= PF_MEMALLOC;
  1124. /*
  1125. * Copy the parameters into kernel space.
  1126. */
  1127. r = copy_params(user, &param);
  1128. current->flags &= ~PF_MEMALLOC;
  1129. if (r)
  1130. return r;
  1131. r = validate_params(cmd, param);
  1132. if (r)
  1133. goto out;
  1134. param_size = param->data_size;
  1135. param->data_size = sizeof(*param);
  1136. r = fn(param, param_size);
  1137. /*
  1138. * Copy the results back to userland.
  1139. */
  1140. if (!r && copy_to_user(user, param, param->data_size))
  1141. r = -EFAULT;
  1142. out:
  1143. free_params(param);
  1144. return r;
  1145. }
  1146. static struct file_operations _ctl_fops = {
  1147. .ioctl = ctl_ioctl,
  1148. .owner = THIS_MODULE,
  1149. };
  1150. static struct miscdevice _dm_misc = {
  1151. .minor = MISC_DYNAMIC_MINOR,
  1152. .name = DM_NAME,
  1153. .devfs_name = "mapper/control",
  1154. .fops = &_ctl_fops
  1155. };
  1156. /*
  1157. * Create misc character device and link to DM_DIR/control.
  1158. */
  1159. int __init dm_interface_init(void)
  1160. {
  1161. int r;
  1162. r = dm_hash_init();
  1163. if (r)
  1164. return r;
  1165. r = misc_register(&_dm_misc);
  1166. if (r) {
  1167. DMERR("misc_register failed for control device");
  1168. dm_hash_exit();
  1169. return r;
  1170. }
  1171. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1172. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1173. DM_DRIVER_EMAIL);
  1174. return 0;
  1175. }
  1176. void dm_interface_exit(void)
  1177. {
  1178. if (misc_deregister(&_dm_misc) < 0)
  1179. DMERR("misc_deregister failed for control device");
  1180. dm_hash_exit();
  1181. }