dm-ioctl.c 31 KB

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