dm-ioctl.c 32 KB

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