dm-ioctl.c 29 KB

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