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_destroy(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_table_destroy(new_map);
  678. dm_put(md);
  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. }
  686. if (dm_suspended(md))
  687. r = dm_resume(md);
  688. if (!r)
  689. r = __dev_status(md, param);
  690. dm_put(md);
  691. return r;
  692. }
  693. /*
  694. * Set or unset the suspension state of a device.
  695. * If the device already is in the requested state we just return its status.
  696. */
  697. static int dev_suspend(struct dm_ioctl *param, size_t param_size)
  698. {
  699. if (param->flags & DM_SUSPEND_FLAG)
  700. return do_suspend(param);
  701. return do_resume(param);
  702. }
  703. /*
  704. * Copies device info back to user space, used by
  705. * the create and info ioctls.
  706. */
  707. static int dev_status(struct dm_ioctl *param, size_t param_size)
  708. {
  709. int r;
  710. struct mapped_device *md;
  711. md = find_device(param);
  712. if (!md)
  713. return -ENXIO;
  714. r = __dev_status(md, param);
  715. dm_put(md);
  716. return r;
  717. }
  718. /*
  719. * Build up the status struct for each target
  720. */
  721. static void retrieve_status(struct dm_table *table,
  722. struct dm_ioctl *param, size_t param_size)
  723. {
  724. unsigned int i, num_targets;
  725. struct dm_target_spec *spec;
  726. char *outbuf, *outptr;
  727. status_type_t type;
  728. size_t remaining, len, used = 0;
  729. outptr = outbuf = get_result_buffer(param, param_size, &len);
  730. if (param->flags & DM_STATUS_TABLE_FLAG)
  731. type = STATUSTYPE_TABLE;
  732. else
  733. type = STATUSTYPE_INFO;
  734. /* Get all the target info */
  735. num_targets = dm_table_get_num_targets(table);
  736. for (i = 0; i < num_targets; i++) {
  737. struct dm_target *ti = dm_table_get_target(table, i);
  738. remaining = len - (outptr - outbuf);
  739. if (remaining <= sizeof(struct dm_target_spec)) {
  740. param->flags |= DM_BUFFER_FULL_FLAG;
  741. break;
  742. }
  743. spec = (struct dm_target_spec *) outptr;
  744. spec->status = 0;
  745. spec->sector_start = ti->begin;
  746. spec->length = ti->len;
  747. strncpy(spec->target_type, ti->type->name,
  748. sizeof(spec->target_type));
  749. outptr += sizeof(struct dm_target_spec);
  750. remaining = len - (outptr - outbuf);
  751. if (remaining <= 0) {
  752. param->flags |= DM_BUFFER_FULL_FLAG;
  753. break;
  754. }
  755. /* Get the status/table string from the target driver */
  756. if (ti->type->status) {
  757. if (ti->type->status(ti, type, outptr, remaining)) {
  758. param->flags |= DM_BUFFER_FULL_FLAG;
  759. break;
  760. }
  761. } else
  762. outptr[0] = '\0';
  763. outptr += strlen(outptr) + 1;
  764. used = param->data_start + (outptr - outbuf);
  765. outptr = align_ptr(outptr);
  766. spec->next = outptr - outbuf;
  767. }
  768. if (used)
  769. param->data_size = used;
  770. param->target_count = num_targets;
  771. }
  772. /*
  773. * Wait for a device to report an event
  774. */
  775. static int dev_wait(struct dm_ioctl *param, size_t param_size)
  776. {
  777. int r;
  778. struct mapped_device *md;
  779. struct dm_table *table;
  780. md = find_device(param);
  781. if (!md)
  782. return -ENXIO;
  783. /*
  784. * Wait for a notification event
  785. */
  786. if (dm_wait_event(md, param->event_nr)) {
  787. r = -ERESTARTSYS;
  788. goto out;
  789. }
  790. /*
  791. * The userland program is going to want to know what
  792. * changed to trigger the event, so we may as well tell
  793. * him and save an ioctl.
  794. */
  795. r = __dev_status(md, param);
  796. if (r)
  797. goto out;
  798. table = dm_get_table(md);
  799. if (table) {
  800. retrieve_status(table, param, param_size);
  801. dm_table_put(table);
  802. }
  803. out:
  804. dm_put(md);
  805. return r;
  806. }
  807. static inline fmode_t get_mode(struct dm_ioctl *param)
  808. {
  809. fmode_t mode = FMODE_READ | FMODE_WRITE;
  810. if (param->flags & DM_READONLY_FLAG)
  811. mode = FMODE_READ;
  812. return mode;
  813. }
  814. static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
  815. struct dm_target_spec **spec, char **target_params)
  816. {
  817. *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
  818. *target_params = (char *) (*spec + 1);
  819. if (*spec < (last + 1))
  820. return -EINVAL;
  821. return invalid_str(*target_params, end);
  822. }
  823. static int populate_table(struct dm_table *table,
  824. struct dm_ioctl *param, size_t param_size)
  825. {
  826. int r;
  827. unsigned int i = 0;
  828. struct dm_target_spec *spec = (struct dm_target_spec *) param;
  829. uint32_t next = param->data_start;
  830. void *end = (void *) param + param_size;
  831. char *target_params;
  832. if (!param->target_count) {
  833. DMWARN("populate_table: no targets specified");
  834. return -EINVAL;
  835. }
  836. for (i = 0; i < param->target_count; i++) {
  837. r = next_target(spec, next, end, &spec, &target_params);
  838. if (r) {
  839. DMWARN("unable to find target");
  840. return r;
  841. }
  842. r = dm_table_add_target(table, spec->target_type,
  843. (sector_t) spec->sector_start,
  844. (sector_t) spec->length,
  845. target_params);
  846. if (r) {
  847. DMWARN("error adding target to table");
  848. return r;
  849. }
  850. next = spec->next;
  851. }
  852. return dm_table_complete(table);
  853. }
  854. static int table_load(struct dm_ioctl *param, size_t param_size)
  855. {
  856. int r;
  857. struct hash_cell *hc;
  858. struct dm_table *t;
  859. struct mapped_device *md;
  860. md = find_device(param);
  861. if (!md)
  862. return -ENXIO;
  863. r = dm_table_create(&t, get_mode(param), param->target_count, md);
  864. if (r)
  865. goto out;
  866. r = populate_table(t, param, param_size);
  867. if (r) {
  868. dm_table_put(t);
  869. goto out;
  870. }
  871. down_write(&_hash_lock);
  872. hc = dm_get_mdptr(md);
  873. if (!hc || hc->md != md) {
  874. DMWARN("device has been removed from the dev hash table.");
  875. dm_table_put(t);
  876. up_write(&_hash_lock);
  877. r = -ENXIO;
  878. goto out;
  879. }
  880. if (hc->new_map)
  881. dm_table_destroy(hc->new_map);
  882. hc->new_map = t;
  883. up_write(&_hash_lock);
  884. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  885. r = __dev_status(md, param);
  886. out:
  887. dm_put(md);
  888. return r;
  889. }
  890. static int table_clear(struct dm_ioctl *param, size_t param_size)
  891. {
  892. int r;
  893. struct hash_cell *hc;
  894. struct mapped_device *md;
  895. down_write(&_hash_lock);
  896. hc = __find_device_hash_cell(param);
  897. if (!hc) {
  898. DMWARN("device doesn't appear to be in the dev hash table.");
  899. up_write(&_hash_lock);
  900. return -ENXIO;
  901. }
  902. if (hc->new_map) {
  903. dm_table_destroy(hc->new_map);
  904. hc->new_map = NULL;
  905. }
  906. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  907. r = __dev_status(hc->md, param);
  908. md = hc->md;
  909. up_write(&_hash_lock);
  910. dm_put(md);
  911. return r;
  912. }
  913. /*
  914. * Retrieves a list of devices used by a particular dm device.
  915. */
  916. static void retrieve_deps(struct dm_table *table,
  917. struct dm_ioctl *param, size_t param_size)
  918. {
  919. unsigned int count = 0;
  920. struct list_head *tmp;
  921. size_t len, needed;
  922. struct dm_dev_internal *dd;
  923. struct dm_target_deps *deps;
  924. deps = get_result_buffer(param, param_size, &len);
  925. /*
  926. * Count the devices.
  927. */
  928. list_for_each (tmp, dm_table_get_devices(table))
  929. count++;
  930. /*
  931. * Check we have enough space.
  932. */
  933. needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
  934. if (len < needed) {
  935. param->flags |= DM_BUFFER_FULL_FLAG;
  936. return;
  937. }
  938. /*
  939. * Fill in the devices.
  940. */
  941. deps->count = count;
  942. count = 0;
  943. list_for_each_entry (dd, dm_table_get_devices(table), list)
  944. deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
  945. param->data_size = param->data_start + needed;
  946. }
  947. static int table_deps(struct dm_ioctl *param, size_t param_size)
  948. {
  949. int r = 0;
  950. struct mapped_device *md;
  951. struct dm_table *table;
  952. md = find_device(param);
  953. if (!md)
  954. return -ENXIO;
  955. r = __dev_status(md, param);
  956. if (r)
  957. goto out;
  958. table = dm_get_table(md);
  959. if (table) {
  960. retrieve_deps(table, param, param_size);
  961. dm_table_put(table);
  962. }
  963. out:
  964. dm_put(md);
  965. return r;
  966. }
  967. /*
  968. * Return the status of a device as a text string for each
  969. * target.
  970. */
  971. static int table_status(struct dm_ioctl *param, size_t param_size)
  972. {
  973. int r;
  974. struct mapped_device *md;
  975. struct dm_table *table;
  976. md = find_device(param);
  977. if (!md)
  978. return -ENXIO;
  979. r = __dev_status(md, param);
  980. if (r)
  981. goto out;
  982. table = dm_get_table(md);
  983. if (table) {
  984. retrieve_status(table, param, param_size);
  985. dm_table_put(table);
  986. }
  987. out:
  988. dm_put(md);
  989. return r;
  990. }
  991. /*
  992. * Pass a message to the target that's at the supplied device offset.
  993. */
  994. static int target_message(struct dm_ioctl *param, size_t param_size)
  995. {
  996. int r, argc;
  997. char **argv;
  998. struct mapped_device *md;
  999. struct dm_table *table;
  1000. struct dm_target *ti;
  1001. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  1002. md = find_device(param);
  1003. if (!md)
  1004. return -ENXIO;
  1005. r = __dev_status(md, param);
  1006. if (r)
  1007. goto out;
  1008. if (tmsg < (struct dm_target_msg *) param->data ||
  1009. invalid_str(tmsg->message, (void *) param + param_size)) {
  1010. DMWARN("Invalid target message parameters.");
  1011. r = -EINVAL;
  1012. goto out;
  1013. }
  1014. r = dm_split_args(&argc, &argv, tmsg->message);
  1015. if (r) {
  1016. DMWARN("Failed to split target message parameters");
  1017. goto out;
  1018. }
  1019. table = dm_get_table(md);
  1020. if (!table)
  1021. goto out_argv;
  1022. ti = dm_table_find_target(table, tmsg->sector);
  1023. if (!dm_target_is_valid(ti)) {
  1024. DMWARN("Target message sector outside device.");
  1025. r = -EINVAL;
  1026. } else if (ti->type->message)
  1027. r = ti->type->message(ti, argc, argv);
  1028. else {
  1029. DMWARN("Target type does not support messages");
  1030. r = -EINVAL;
  1031. }
  1032. dm_table_put(table);
  1033. out_argv:
  1034. kfree(argv);
  1035. out:
  1036. param->data_size = 0;
  1037. dm_put(md);
  1038. return r;
  1039. }
  1040. /*-----------------------------------------------------------------
  1041. * Implementation of open/close/ioctl on the special char
  1042. * device.
  1043. *---------------------------------------------------------------*/
  1044. static ioctl_fn lookup_ioctl(unsigned int cmd)
  1045. {
  1046. static struct {
  1047. int cmd;
  1048. ioctl_fn fn;
  1049. } _ioctls[] = {
  1050. {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
  1051. {DM_REMOVE_ALL_CMD, remove_all},
  1052. {DM_LIST_DEVICES_CMD, list_devices},
  1053. {DM_DEV_CREATE_CMD, dev_create},
  1054. {DM_DEV_REMOVE_CMD, dev_remove},
  1055. {DM_DEV_RENAME_CMD, dev_rename},
  1056. {DM_DEV_SUSPEND_CMD, dev_suspend},
  1057. {DM_DEV_STATUS_CMD, dev_status},
  1058. {DM_DEV_WAIT_CMD, dev_wait},
  1059. {DM_TABLE_LOAD_CMD, table_load},
  1060. {DM_TABLE_CLEAR_CMD, table_clear},
  1061. {DM_TABLE_DEPS_CMD, table_deps},
  1062. {DM_TABLE_STATUS_CMD, table_status},
  1063. {DM_LIST_VERSIONS_CMD, list_versions},
  1064. {DM_TARGET_MSG_CMD, target_message},
  1065. {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
  1066. };
  1067. return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
  1068. }
  1069. /*
  1070. * As well as checking the version compatibility this always
  1071. * copies the kernel interface version out.
  1072. */
  1073. static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
  1074. {
  1075. uint32_t version[3];
  1076. int r = 0;
  1077. if (copy_from_user(version, user->version, sizeof(version)))
  1078. return -EFAULT;
  1079. if ((DM_VERSION_MAJOR != version[0]) ||
  1080. (DM_VERSION_MINOR < version[1])) {
  1081. DMWARN("ioctl interface mismatch: "
  1082. "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1083. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1084. DM_VERSION_PATCHLEVEL,
  1085. version[0], version[1], version[2], cmd);
  1086. r = -EINVAL;
  1087. }
  1088. /*
  1089. * Fill in the kernel version.
  1090. */
  1091. version[0] = DM_VERSION_MAJOR;
  1092. version[1] = DM_VERSION_MINOR;
  1093. version[2] = DM_VERSION_PATCHLEVEL;
  1094. if (copy_to_user(user->version, version, sizeof(version)))
  1095. return -EFAULT;
  1096. return r;
  1097. }
  1098. static void free_params(struct dm_ioctl *param)
  1099. {
  1100. vfree(param);
  1101. }
  1102. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
  1103. {
  1104. struct dm_ioctl tmp, *dmi;
  1105. if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
  1106. return -EFAULT;
  1107. if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
  1108. return -EINVAL;
  1109. dmi = vmalloc(tmp.data_size);
  1110. if (!dmi)
  1111. return -ENOMEM;
  1112. if (copy_from_user(dmi, user, tmp.data_size)) {
  1113. vfree(dmi);
  1114. return -EFAULT;
  1115. }
  1116. *param = dmi;
  1117. return 0;
  1118. }
  1119. static int validate_params(uint cmd, struct dm_ioctl *param)
  1120. {
  1121. /* Always clear this flag */
  1122. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1123. /* Ignores parameters */
  1124. if (cmd == DM_REMOVE_ALL_CMD ||
  1125. cmd == DM_LIST_DEVICES_CMD ||
  1126. cmd == DM_LIST_VERSIONS_CMD)
  1127. return 0;
  1128. if ((cmd == DM_DEV_CREATE_CMD)) {
  1129. if (!*param->name) {
  1130. DMWARN("name not supplied when creating device");
  1131. return -EINVAL;
  1132. }
  1133. } else if ((*param->uuid && *param->name)) {
  1134. DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
  1135. return -EINVAL;
  1136. }
  1137. /* Ensure strings are terminated */
  1138. param->name[DM_NAME_LEN - 1] = '\0';
  1139. param->uuid[DM_UUID_LEN - 1] = '\0';
  1140. return 0;
  1141. }
  1142. static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
  1143. {
  1144. int r = 0;
  1145. unsigned int cmd;
  1146. struct dm_ioctl *uninitialized_var(param);
  1147. ioctl_fn fn = NULL;
  1148. size_t param_size;
  1149. /* only root can play with this */
  1150. if (!capable(CAP_SYS_ADMIN))
  1151. return -EACCES;
  1152. if (_IOC_TYPE(command) != DM_IOCTL)
  1153. return -ENOTTY;
  1154. cmd = _IOC_NR(command);
  1155. /*
  1156. * Check the interface version passed in. This also
  1157. * writes out the kernel's interface version.
  1158. */
  1159. r = check_version(cmd, user);
  1160. if (r)
  1161. return r;
  1162. /*
  1163. * Nothing more to do for the version command.
  1164. */
  1165. if (cmd == DM_VERSION_CMD)
  1166. return 0;
  1167. fn = lookup_ioctl(cmd);
  1168. if (!fn) {
  1169. DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
  1170. return -ENOTTY;
  1171. }
  1172. /*
  1173. * Trying to avoid low memory issues when a device is
  1174. * suspended.
  1175. */
  1176. current->flags |= PF_MEMALLOC;
  1177. /*
  1178. * Copy the parameters into kernel space.
  1179. */
  1180. r = copy_params(user, &param);
  1181. current->flags &= ~PF_MEMALLOC;
  1182. if (r)
  1183. return r;
  1184. r = validate_params(cmd, param);
  1185. if (r)
  1186. goto out;
  1187. param_size = param->data_size;
  1188. param->data_size = sizeof(*param);
  1189. r = fn(param, param_size);
  1190. /*
  1191. * Copy the results back to userland.
  1192. */
  1193. if (!r && copy_to_user(user, param, param->data_size))
  1194. r = -EFAULT;
  1195. out:
  1196. free_params(param);
  1197. return r;
  1198. }
  1199. static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
  1200. {
  1201. return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
  1202. }
  1203. #ifdef CONFIG_COMPAT
  1204. static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
  1205. {
  1206. return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
  1207. }
  1208. #else
  1209. #define dm_compat_ctl_ioctl NULL
  1210. #endif
  1211. static const struct file_operations _ctl_fops = {
  1212. .unlocked_ioctl = dm_ctl_ioctl,
  1213. .compat_ioctl = dm_compat_ctl_ioctl,
  1214. .owner = THIS_MODULE,
  1215. };
  1216. static struct miscdevice _dm_misc = {
  1217. .minor = MISC_DYNAMIC_MINOR,
  1218. .name = DM_NAME,
  1219. .fops = &_ctl_fops
  1220. };
  1221. /*
  1222. * Create misc character device and link to DM_DIR/control.
  1223. */
  1224. int __init dm_interface_init(void)
  1225. {
  1226. int r;
  1227. r = dm_hash_init();
  1228. if (r)
  1229. return r;
  1230. r = misc_register(&_dm_misc);
  1231. if (r) {
  1232. DMERR("misc_register failed for control device");
  1233. dm_hash_exit();
  1234. return r;
  1235. }
  1236. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1237. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1238. DM_DRIVER_EMAIL);
  1239. return 0;
  1240. }
  1241. void dm_interface_exit(void)
  1242. {
  1243. if (misc_deregister(&_dm_misc) < 0)
  1244. DMERR("misc_deregister failed for control device");
  1245. dm_hash_exit();
  1246. }
  1247. /**
  1248. * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
  1249. * @md: Pointer to mapped_device
  1250. * @name: Buffer (size DM_NAME_LEN) for name
  1251. * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
  1252. */
  1253. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
  1254. {
  1255. int r = 0;
  1256. struct hash_cell *hc;
  1257. if (!md)
  1258. return -ENXIO;
  1259. dm_get(md);
  1260. down_read(&_hash_lock);
  1261. hc = dm_get_mdptr(md);
  1262. if (!hc || hc->md != md) {
  1263. r = -ENXIO;
  1264. goto out;
  1265. }
  1266. if (name)
  1267. strcpy(name, hc->name);
  1268. if (uuid)
  1269. strcpy(uuid, hc->uuid ? : "");
  1270. out:
  1271. up_read(&_hash_lock);
  1272. dm_put(md);
  1273. return r;
  1274. }