dm-ioctl.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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. strlen(new_name) > DM_NAME_LEN - 1) {
  582. DMWARN("Invalid new logical volume name supplied.");
  583. return -EINVAL;
  584. }
  585. r = check_name(new_name);
  586. if (r)
  587. return r;
  588. param->data_size = 0;
  589. return dm_hash_rename(param->name, new_name);
  590. }
  591. static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
  592. {
  593. int r = -EINVAL, x;
  594. struct mapped_device *md;
  595. struct hd_geometry geometry;
  596. unsigned long indata[4];
  597. char *geostr = (char *) param + param->data_start;
  598. md = find_device(param);
  599. if (!md)
  600. return -ENXIO;
  601. if (geostr < param->data ||
  602. invalid_str(geostr, (void *) param + param_size)) {
  603. DMWARN("Invalid geometry supplied.");
  604. goto out;
  605. }
  606. x = sscanf(geostr, "%lu %lu %lu %lu", indata,
  607. indata + 1, indata + 2, indata + 3);
  608. if (x != 4) {
  609. DMWARN("Unable to interpret geometry settings.");
  610. goto out;
  611. }
  612. if (indata[0] > 65535 || indata[1] > 255 ||
  613. indata[2] > 255 || indata[3] > ULONG_MAX) {
  614. DMWARN("Geometry exceeds range limits.");
  615. goto out;
  616. }
  617. geometry.cylinders = indata[0];
  618. geometry.heads = indata[1];
  619. geometry.sectors = indata[2];
  620. geometry.start = indata[3];
  621. r = dm_set_geometry(md, &geometry);
  622. if (!r)
  623. r = __dev_status(md, param);
  624. param->data_size = 0;
  625. out:
  626. dm_put(md);
  627. return r;
  628. }
  629. static int do_suspend(struct dm_ioctl *param)
  630. {
  631. int r = 0;
  632. unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  633. struct mapped_device *md;
  634. md = find_device(param);
  635. if (!md)
  636. return -ENXIO;
  637. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  638. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  639. if (param->flags & DM_NOFLUSH_FLAG)
  640. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  641. if (!dm_suspended(md))
  642. r = dm_suspend(md, suspend_flags);
  643. if (!r)
  644. r = __dev_status(md, param);
  645. dm_put(md);
  646. return r;
  647. }
  648. static int do_resume(struct dm_ioctl *param)
  649. {
  650. int r = 0;
  651. unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  652. struct hash_cell *hc;
  653. struct mapped_device *md;
  654. struct dm_table *new_map;
  655. down_write(&_hash_lock);
  656. hc = __find_device_hash_cell(param);
  657. if (!hc) {
  658. DMWARN("device doesn't appear to be in the dev hash table.");
  659. up_write(&_hash_lock);
  660. return -ENXIO;
  661. }
  662. md = hc->md;
  663. new_map = hc->new_map;
  664. hc->new_map = NULL;
  665. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  666. up_write(&_hash_lock);
  667. /* Do we need to load a new map ? */
  668. if (new_map) {
  669. /* Suspend if it isn't already suspended */
  670. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  671. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  672. if (param->flags & DM_NOFLUSH_FLAG)
  673. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  674. if (!dm_suspended(md))
  675. dm_suspend(md, suspend_flags);
  676. r = dm_swap_table(md, new_map);
  677. if (r) {
  678. dm_table_destroy(new_map);
  679. dm_put(md);
  680. return r;
  681. }
  682. if (dm_table_get_mode(new_map) & FMODE_WRITE)
  683. set_disk_ro(dm_disk(md), 0);
  684. else
  685. set_disk_ro(dm_disk(md), 1);
  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 fmode_t get_mode(struct dm_ioctl *param)
  809. {
  810. fmode_t 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_prealloc_integrity(struct dm_table *t,
  856. struct mapped_device *md)
  857. {
  858. struct list_head *devices = dm_table_get_devices(t);
  859. struct dm_dev_internal *dd;
  860. list_for_each_entry(dd, devices, list)
  861. if (bdev_get_integrity(dd->dm_dev.bdev))
  862. return blk_integrity_register(dm_disk(md), NULL);
  863. return 0;
  864. }
  865. static int table_load(struct dm_ioctl *param, size_t param_size)
  866. {
  867. int r;
  868. struct hash_cell *hc;
  869. struct dm_table *t;
  870. struct mapped_device *md;
  871. md = find_device(param);
  872. if (!md)
  873. return -ENXIO;
  874. r = dm_table_create(&t, get_mode(param), param->target_count, md);
  875. if (r)
  876. goto out;
  877. r = populate_table(t, param, param_size);
  878. if (r) {
  879. dm_table_destroy(t);
  880. goto out;
  881. }
  882. r = table_prealloc_integrity(t, md);
  883. if (r) {
  884. DMERR("%s: could not register integrity profile.",
  885. dm_device_name(md));
  886. dm_table_destroy(t);
  887. goto out;
  888. }
  889. down_write(&_hash_lock);
  890. hc = dm_get_mdptr(md);
  891. if (!hc || hc->md != md) {
  892. DMWARN("device has been removed from the dev hash table.");
  893. dm_table_destroy(t);
  894. up_write(&_hash_lock);
  895. r = -ENXIO;
  896. goto out;
  897. }
  898. if (hc->new_map)
  899. dm_table_destroy(hc->new_map);
  900. hc->new_map = t;
  901. up_write(&_hash_lock);
  902. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  903. r = __dev_status(md, param);
  904. out:
  905. dm_put(md);
  906. return r;
  907. }
  908. static int table_clear(struct dm_ioctl *param, size_t param_size)
  909. {
  910. int r;
  911. struct hash_cell *hc;
  912. struct mapped_device *md;
  913. down_write(&_hash_lock);
  914. hc = __find_device_hash_cell(param);
  915. if (!hc) {
  916. DMWARN("device doesn't appear to be in the dev hash table.");
  917. up_write(&_hash_lock);
  918. return -ENXIO;
  919. }
  920. if (hc->new_map) {
  921. dm_table_destroy(hc->new_map);
  922. hc->new_map = NULL;
  923. }
  924. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  925. r = __dev_status(hc->md, param);
  926. md = hc->md;
  927. up_write(&_hash_lock);
  928. dm_put(md);
  929. return r;
  930. }
  931. /*
  932. * Retrieves a list of devices used by a particular dm device.
  933. */
  934. static void retrieve_deps(struct dm_table *table,
  935. struct dm_ioctl *param, size_t param_size)
  936. {
  937. unsigned int count = 0;
  938. struct list_head *tmp;
  939. size_t len, needed;
  940. struct dm_dev_internal *dd;
  941. struct dm_target_deps *deps;
  942. deps = get_result_buffer(param, param_size, &len);
  943. /*
  944. * Count the devices.
  945. */
  946. list_for_each (tmp, dm_table_get_devices(table))
  947. count++;
  948. /*
  949. * Check we have enough space.
  950. */
  951. needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
  952. if (len < needed) {
  953. param->flags |= DM_BUFFER_FULL_FLAG;
  954. return;
  955. }
  956. /*
  957. * Fill in the devices.
  958. */
  959. deps->count = count;
  960. count = 0;
  961. list_for_each_entry (dd, dm_table_get_devices(table), list)
  962. deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
  963. param->data_size = param->data_start + needed;
  964. }
  965. static int table_deps(struct dm_ioctl *param, size_t param_size)
  966. {
  967. int r = 0;
  968. struct mapped_device *md;
  969. struct dm_table *table;
  970. md = find_device(param);
  971. if (!md)
  972. return -ENXIO;
  973. r = __dev_status(md, param);
  974. if (r)
  975. goto out;
  976. table = dm_get_table(md);
  977. if (table) {
  978. retrieve_deps(table, param, param_size);
  979. dm_table_put(table);
  980. }
  981. out:
  982. dm_put(md);
  983. return r;
  984. }
  985. /*
  986. * Return the status of a device as a text string for each
  987. * target.
  988. */
  989. static int table_status(struct dm_ioctl *param, size_t param_size)
  990. {
  991. int r;
  992. struct mapped_device *md;
  993. struct dm_table *table;
  994. md = find_device(param);
  995. if (!md)
  996. return -ENXIO;
  997. r = __dev_status(md, param);
  998. if (r)
  999. goto out;
  1000. table = dm_get_table(md);
  1001. if (table) {
  1002. retrieve_status(table, param, param_size);
  1003. dm_table_put(table);
  1004. }
  1005. out:
  1006. dm_put(md);
  1007. return r;
  1008. }
  1009. /*
  1010. * Pass a message to the target that's at the supplied device offset.
  1011. */
  1012. static int target_message(struct dm_ioctl *param, size_t param_size)
  1013. {
  1014. int r, argc;
  1015. char **argv;
  1016. struct mapped_device *md;
  1017. struct dm_table *table;
  1018. struct dm_target *ti;
  1019. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  1020. md = find_device(param);
  1021. if (!md)
  1022. return -ENXIO;
  1023. r = __dev_status(md, param);
  1024. if (r)
  1025. goto out;
  1026. if (tmsg < (struct dm_target_msg *) param->data ||
  1027. invalid_str(tmsg->message, (void *) param + param_size)) {
  1028. DMWARN("Invalid target message parameters.");
  1029. r = -EINVAL;
  1030. goto out;
  1031. }
  1032. r = dm_split_args(&argc, &argv, tmsg->message);
  1033. if (r) {
  1034. DMWARN("Failed to split target message parameters");
  1035. goto out;
  1036. }
  1037. table = dm_get_table(md);
  1038. if (!table)
  1039. goto out_argv;
  1040. ti = dm_table_find_target(table, tmsg->sector);
  1041. if (!dm_target_is_valid(ti)) {
  1042. DMWARN("Target message sector outside device.");
  1043. r = -EINVAL;
  1044. } else if (ti->type->message)
  1045. r = ti->type->message(ti, argc, argv);
  1046. else {
  1047. DMWARN("Target type does not support messages");
  1048. r = -EINVAL;
  1049. }
  1050. dm_table_put(table);
  1051. out_argv:
  1052. kfree(argv);
  1053. out:
  1054. param->data_size = 0;
  1055. dm_put(md);
  1056. return r;
  1057. }
  1058. /*-----------------------------------------------------------------
  1059. * Implementation of open/close/ioctl on the special char
  1060. * device.
  1061. *---------------------------------------------------------------*/
  1062. static ioctl_fn lookup_ioctl(unsigned int cmd)
  1063. {
  1064. static struct {
  1065. int cmd;
  1066. ioctl_fn fn;
  1067. } _ioctls[] = {
  1068. {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
  1069. {DM_REMOVE_ALL_CMD, remove_all},
  1070. {DM_LIST_DEVICES_CMD, list_devices},
  1071. {DM_DEV_CREATE_CMD, dev_create},
  1072. {DM_DEV_REMOVE_CMD, dev_remove},
  1073. {DM_DEV_RENAME_CMD, dev_rename},
  1074. {DM_DEV_SUSPEND_CMD, dev_suspend},
  1075. {DM_DEV_STATUS_CMD, dev_status},
  1076. {DM_DEV_WAIT_CMD, dev_wait},
  1077. {DM_TABLE_LOAD_CMD, table_load},
  1078. {DM_TABLE_CLEAR_CMD, table_clear},
  1079. {DM_TABLE_DEPS_CMD, table_deps},
  1080. {DM_TABLE_STATUS_CMD, table_status},
  1081. {DM_LIST_VERSIONS_CMD, list_versions},
  1082. {DM_TARGET_MSG_CMD, target_message},
  1083. {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
  1084. };
  1085. return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
  1086. }
  1087. /*
  1088. * As well as checking the version compatibility this always
  1089. * copies the kernel interface version out.
  1090. */
  1091. static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
  1092. {
  1093. uint32_t version[3];
  1094. int r = 0;
  1095. if (copy_from_user(version, user->version, sizeof(version)))
  1096. return -EFAULT;
  1097. if ((DM_VERSION_MAJOR != version[0]) ||
  1098. (DM_VERSION_MINOR < version[1])) {
  1099. DMWARN("ioctl interface mismatch: "
  1100. "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1101. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1102. DM_VERSION_PATCHLEVEL,
  1103. version[0], version[1], version[2], cmd);
  1104. r = -EINVAL;
  1105. }
  1106. /*
  1107. * Fill in the kernel version.
  1108. */
  1109. version[0] = DM_VERSION_MAJOR;
  1110. version[1] = DM_VERSION_MINOR;
  1111. version[2] = DM_VERSION_PATCHLEVEL;
  1112. if (copy_to_user(user->version, version, sizeof(version)))
  1113. return -EFAULT;
  1114. return r;
  1115. }
  1116. static void free_params(struct dm_ioctl *param)
  1117. {
  1118. vfree(param);
  1119. }
  1120. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
  1121. {
  1122. struct dm_ioctl tmp, *dmi;
  1123. if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
  1124. return -EFAULT;
  1125. if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
  1126. return -EINVAL;
  1127. dmi = vmalloc(tmp.data_size);
  1128. if (!dmi)
  1129. return -ENOMEM;
  1130. if (copy_from_user(dmi, user, tmp.data_size)) {
  1131. vfree(dmi);
  1132. return -EFAULT;
  1133. }
  1134. *param = dmi;
  1135. return 0;
  1136. }
  1137. static int validate_params(uint cmd, struct dm_ioctl *param)
  1138. {
  1139. /* Always clear this flag */
  1140. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1141. /* Ignores parameters */
  1142. if (cmd == DM_REMOVE_ALL_CMD ||
  1143. cmd == DM_LIST_DEVICES_CMD ||
  1144. cmd == DM_LIST_VERSIONS_CMD)
  1145. return 0;
  1146. if ((cmd == DM_DEV_CREATE_CMD)) {
  1147. if (!*param->name) {
  1148. DMWARN("name not supplied when creating device");
  1149. return -EINVAL;
  1150. }
  1151. } else if ((*param->uuid && *param->name)) {
  1152. DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
  1153. return -EINVAL;
  1154. }
  1155. /* Ensure strings are terminated */
  1156. param->name[DM_NAME_LEN - 1] = '\0';
  1157. param->uuid[DM_UUID_LEN - 1] = '\0';
  1158. return 0;
  1159. }
  1160. static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
  1161. {
  1162. int r = 0;
  1163. unsigned int cmd;
  1164. struct dm_ioctl *uninitialized_var(param);
  1165. ioctl_fn fn = NULL;
  1166. size_t param_size;
  1167. /* only root can play with this */
  1168. if (!capable(CAP_SYS_ADMIN))
  1169. return -EACCES;
  1170. if (_IOC_TYPE(command) != DM_IOCTL)
  1171. return -ENOTTY;
  1172. cmd = _IOC_NR(command);
  1173. /*
  1174. * Check the interface version passed in. This also
  1175. * writes out the kernel's interface version.
  1176. */
  1177. r = check_version(cmd, user);
  1178. if (r)
  1179. return r;
  1180. /*
  1181. * Nothing more to do for the version command.
  1182. */
  1183. if (cmd == DM_VERSION_CMD)
  1184. return 0;
  1185. fn = lookup_ioctl(cmd);
  1186. if (!fn) {
  1187. DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
  1188. return -ENOTTY;
  1189. }
  1190. /*
  1191. * Trying to avoid low memory issues when a device is
  1192. * suspended.
  1193. */
  1194. current->flags |= PF_MEMALLOC;
  1195. /*
  1196. * Copy the parameters into kernel space.
  1197. */
  1198. r = copy_params(user, &param);
  1199. current->flags &= ~PF_MEMALLOC;
  1200. if (r)
  1201. return r;
  1202. r = validate_params(cmd, param);
  1203. if (r)
  1204. goto out;
  1205. param_size = param->data_size;
  1206. param->data_size = sizeof(*param);
  1207. r = fn(param, param_size);
  1208. /*
  1209. * Copy the results back to userland.
  1210. */
  1211. if (!r && copy_to_user(user, param, param->data_size))
  1212. r = -EFAULT;
  1213. out:
  1214. free_params(param);
  1215. return r;
  1216. }
  1217. static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
  1218. {
  1219. return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
  1220. }
  1221. #ifdef CONFIG_COMPAT
  1222. static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
  1223. {
  1224. return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
  1225. }
  1226. #else
  1227. #define dm_compat_ctl_ioctl NULL
  1228. #endif
  1229. static const struct file_operations _ctl_fops = {
  1230. .unlocked_ioctl = dm_ctl_ioctl,
  1231. .compat_ioctl = dm_compat_ctl_ioctl,
  1232. .owner = THIS_MODULE,
  1233. };
  1234. static struct miscdevice _dm_misc = {
  1235. .minor = MISC_DYNAMIC_MINOR,
  1236. .name = DM_NAME,
  1237. .fops = &_ctl_fops
  1238. };
  1239. /*
  1240. * Create misc character device and link to DM_DIR/control.
  1241. */
  1242. int __init dm_interface_init(void)
  1243. {
  1244. int r;
  1245. r = dm_hash_init();
  1246. if (r)
  1247. return r;
  1248. r = misc_register(&_dm_misc);
  1249. if (r) {
  1250. DMERR("misc_register failed for control device");
  1251. dm_hash_exit();
  1252. return r;
  1253. }
  1254. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1255. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1256. DM_DRIVER_EMAIL);
  1257. return 0;
  1258. }
  1259. void dm_interface_exit(void)
  1260. {
  1261. if (misc_deregister(&_dm_misc) < 0)
  1262. DMERR("misc_deregister failed for control device");
  1263. dm_hash_exit();
  1264. }
  1265. /**
  1266. * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
  1267. * @md: Pointer to mapped_device
  1268. * @name: Buffer (size DM_NAME_LEN) for name
  1269. * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
  1270. */
  1271. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
  1272. {
  1273. int r = 0;
  1274. struct hash_cell *hc;
  1275. if (!md)
  1276. return -ENXIO;
  1277. dm_get(md);
  1278. down_read(&_hash_lock);
  1279. hc = dm_get_mdptr(md);
  1280. if (!hc || hc->md != md) {
  1281. r = -ENXIO;
  1282. goto out;
  1283. }
  1284. if (name)
  1285. strcpy(name, hc->name);
  1286. if (uuid)
  1287. strcpy(uuid, hc->uuid ? : "");
  1288. out:
  1289. up_read(&_hash_lock);
  1290. dm_put(md);
  1291. return r;
  1292. }