dm-ioctl.c 34 KB

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