dm-ioctl.c 37 KB

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