dm-ioctl.c 33 KB

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