dm-ioctl.c 29 KB

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