dm-ioctl.c 29 KB

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