dm-ioctl.c 34 KB

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