dm-ioctl.c 32 KB

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