dm-ioctl.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  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. unsigned status_flags = 0;
  865. outptr = outbuf = get_result_buffer(param, param_size, &len);
  866. if (param->flags & DM_STATUS_TABLE_FLAG)
  867. type = STATUSTYPE_TABLE;
  868. else
  869. type = STATUSTYPE_INFO;
  870. /* Get all the target info */
  871. num_targets = dm_table_get_num_targets(table);
  872. for (i = 0; i < num_targets; i++) {
  873. struct dm_target *ti = dm_table_get_target(table, i);
  874. size_t l;
  875. remaining = len - (outptr - outbuf);
  876. if (remaining <= sizeof(struct dm_target_spec)) {
  877. param->flags |= DM_BUFFER_FULL_FLAG;
  878. break;
  879. }
  880. spec = (struct dm_target_spec *) outptr;
  881. spec->status = 0;
  882. spec->sector_start = ti->begin;
  883. spec->length = ti->len;
  884. strncpy(spec->target_type, ti->type->name,
  885. sizeof(spec->target_type));
  886. outptr += sizeof(struct dm_target_spec);
  887. remaining = len - (outptr - outbuf);
  888. if (remaining <= 0) {
  889. param->flags |= DM_BUFFER_FULL_FLAG;
  890. break;
  891. }
  892. /* Get the status/table string from the target driver */
  893. if (ti->type->status) {
  894. if (param->flags & DM_NOFLUSH_FLAG)
  895. status_flags |= DM_STATUS_NOFLUSH_FLAG;
  896. ti->type->status(ti, type, status_flags, outptr, remaining);
  897. } else
  898. outptr[0] = '\0';
  899. l = strlen(outptr) + 1;
  900. if (l == remaining) {
  901. param->flags |= DM_BUFFER_FULL_FLAG;
  902. break;
  903. }
  904. outptr += l;
  905. used = param->data_start + (outptr - outbuf);
  906. outptr = align_ptr(outptr);
  907. spec->next = outptr - outbuf;
  908. }
  909. if (used)
  910. param->data_size = used;
  911. param->target_count = num_targets;
  912. }
  913. /*
  914. * Wait for a device to report an event
  915. */
  916. static int dev_wait(struct dm_ioctl *param, size_t param_size)
  917. {
  918. int r = 0;
  919. struct mapped_device *md;
  920. struct dm_table *table;
  921. md = find_device(param);
  922. if (!md)
  923. return -ENXIO;
  924. /*
  925. * Wait for a notification event
  926. */
  927. if (dm_wait_event(md, param->event_nr)) {
  928. r = -ERESTARTSYS;
  929. goto out;
  930. }
  931. /*
  932. * The userland program is going to want to know what
  933. * changed to trigger the event, so we may as well tell
  934. * him and save an ioctl.
  935. */
  936. __dev_status(md, param);
  937. table = dm_get_live_or_inactive_table(md, param);
  938. if (table) {
  939. retrieve_status(table, param, param_size);
  940. dm_table_put(table);
  941. }
  942. out:
  943. dm_put(md);
  944. return r;
  945. }
  946. static inline fmode_t get_mode(struct dm_ioctl *param)
  947. {
  948. fmode_t mode = FMODE_READ | FMODE_WRITE;
  949. if (param->flags & DM_READONLY_FLAG)
  950. mode = FMODE_READ;
  951. return mode;
  952. }
  953. static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
  954. struct dm_target_spec **spec, char **target_params)
  955. {
  956. *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
  957. *target_params = (char *) (*spec + 1);
  958. if (*spec < (last + 1))
  959. return -EINVAL;
  960. return invalid_str(*target_params, end);
  961. }
  962. static int populate_table(struct dm_table *table,
  963. struct dm_ioctl *param, size_t param_size)
  964. {
  965. int r;
  966. unsigned int i = 0;
  967. struct dm_target_spec *spec = (struct dm_target_spec *) param;
  968. uint32_t next = param->data_start;
  969. void *end = (void *) param + param_size;
  970. char *target_params;
  971. if (!param->target_count) {
  972. DMWARN("populate_table: no targets specified");
  973. return -EINVAL;
  974. }
  975. for (i = 0; i < param->target_count; i++) {
  976. r = next_target(spec, next, end, &spec, &target_params);
  977. if (r) {
  978. DMWARN("unable to find target");
  979. return r;
  980. }
  981. r = dm_table_add_target(table, spec->target_type,
  982. (sector_t) spec->sector_start,
  983. (sector_t) spec->length,
  984. target_params);
  985. if (r) {
  986. DMWARN("error adding target to table");
  987. return r;
  988. }
  989. next = spec->next;
  990. }
  991. return dm_table_complete(table);
  992. }
  993. static int table_load(struct dm_ioctl *param, size_t param_size)
  994. {
  995. int r;
  996. struct hash_cell *hc;
  997. struct dm_table *t;
  998. struct mapped_device *md;
  999. struct target_type *immutable_target_type;
  1000. md = find_device(param);
  1001. if (!md)
  1002. return -ENXIO;
  1003. r = dm_table_create(&t, get_mode(param), param->target_count, md);
  1004. if (r)
  1005. goto out;
  1006. r = populate_table(t, param, param_size);
  1007. if (r) {
  1008. dm_table_destroy(t);
  1009. goto out;
  1010. }
  1011. immutable_target_type = dm_get_immutable_target_type(md);
  1012. if (immutable_target_type &&
  1013. (immutable_target_type != dm_table_get_immutable_target_type(t))) {
  1014. DMWARN("can't replace immutable target type %s",
  1015. immutable_target_type->name);
  1016. dm_table_destroy(t);
  1017. r = -EINVAL;
  1018. goto out;
  1019. }
  1020. /* Protect md->type and md->queue against concurrent table loads. */
  1021. dm_lock_md_type(md);
  1022. if (dm_get_md_type(md) == DM_TYPE_NONE)
  1023. /* Initial table load: acquire type of table. */
  1024. dm_set_md_type(md, dm_table_get_type(t));
  1025. else if (dm_get_md_type(md) != dm_table_get_type(t)) {
  1026. DMWARN("can't change device type after initial table load.");
  1027. dm_table_destroy(t);
  1028. dm_unlock_md_type(md);
  1029. r = -EINVAL;
  1030. goto out;
  1031. }
  1032. /* setup md->queue to reflect md's type (may block) */
  1033. r = dm_setup_md_queue(md);
  1034. if (r) {
  1035. DMWARN("unable to set up device queue for new table.");
  1036. dm_table_destroy(t);
  1037. dm_unlock_md_type(md);
  1038. goto out;
  1039. }
  1040. dm_unlock_md_type(md);
  1041. /* stage inactive table */
  1042. down_write(&_hash_lock);
  1043. hc = dm_get_mdptr(md);
  1044. if (!hc || hc->md != md) {
  1045. DMWARN("device has been removed from the dev hash table.");
  1046. dm_table_destroy(t);
  1047. up_write(&_hash_lock);
  1048. r = -ENXIO;
  1049. goto out;
  1050. }
  1051. if (hc->new_map)
  1052. dm_table_destroy(hc->new_map);
  1053. hc->new_map = t;
  1054. up_write(&_hash_lock);
  1055. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  1056. __dev_status(md, param);
  1057. out:
  1058. dm_put(md);
  1059. return r;
  1060. }
  1061. static int table_clear(struct dm_ioctl *param, size_t param_size)
  1062. {
  1063. struct hash_cell *hc;
  1064. struct mapped_device *md;
  1065. down_write(&_hash_lock);
  1066. hc = __find_device_hash_cell(param);
  1067. if (!hc) {
  1068. DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
  1069. up_write(&_hash_lock);
  1070. return -ENXIO;
  1071. }
  1072. if (hc->new_map) {
  1073. dm_table_destroy(hc->new_map);
  1074. hc->new_map = NULL;
  1075. }
  1076. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  1077. __dev_status(hc->md, param);
  1078. md = hc->md;
  1079. up_write(&_hash_lock);
  1080. dm_put(md);
  1081. return 0;
  1082. }
  1083. /*
  1084. * Retrieves a list of devices used by a particular dm device.
  1085. */
  1086. static void retrieve_deps(struct dm_table *table,
  1087. struct dm_ioctl *param, size_t param_size)
  1088. {
  1089. unsigned int count = 0;
  1090. struct list_head *tmp;
  1091. size_t len, needed;
  1092. struct dm_dev_internal *dd;
  1093. struct dm_target_deps *deps;
  1094. deps = get_result_buffer(param, param_size, &len);
  1095. /*
  1096. * Count the devices.
  1097. */
  1098. list_for_each (tmp, dm_table_get_devices(table))
  1099. count++;
  1100. /*
  1101. * Check we have enough space.
  1102. */
  1103. needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
  1104. if (len < needed) {
  1105. param->flags |= DM_BUFFER_FULL_FLAG;
  1106. return;
  1107. }
  1108. /*
  1109. * Fill in the devices.
  1110. */
  1111. deps->count = count;
  1112. count = 0;
  1113. list_for_each_entry (dd, dm_table_get_devices(table), list)
  1114. deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
  1115. param->data_size = param->data_start + needed;
  1116. }
  1117. static int table_deps(struct dm_ioctl *param, size_t param_size)
  1118. {
  1119. struct mapped_device *md;
  1120. struct dm_table *table;
  1121. md = find_device(param);
  1122. if (!md)
  1123. return -ENXIO;
  1124. __dev_status(md, param);
  1125. table = dm_get_live_or_inactive_table(md, param);
  1126. if (table) {
  1127. retrieve_deps(table, param, param_size);
  1128. dm_table_put(table);
  1129. }
  1130. dm_put(md);
  1131. return 0;
  1132. }
  1133. /*
  1134. * Return the status of a device as a text string for each
  1135. * target.
  1136. */
  1137. static int table_status(struct dm_ioctl *param, size_t param_size)
  1138. {
  1139. struct mapped_device *md;
  1140. struct dm_table *table;
  1141. md = find_device(param);
  1142. if (!md)
  1143. return -ENXIO;
  1144. __dev_status(md, param);
  1145. table = dm_get_live_or_inactive_table(md, param);
  1146. if (table) {
  1147. retrieve_status(table, param, param_size);
  1148. dm_table_put(table);
  1149. }
  1150. dm_put(md);
  1151. return 0;
  1152. }
  1153. static bool buffer_test_overflow(char *result, unsigned maxlen)
  1154. {
  1155. return !maxlen || strlen(result) + 1 >= maxlen;
  1156. }
  1157. /*
  1158. * Process device-mapper dependent messages.
  1159. * Returns a number <= 1 if message was processed by device mapper.
  1160. * Returns 2 if message should be delivered to the target.
  1161. */
  1162. static int message_for_md(struct mapped_device *md, unsigned argc, char **argv,
  1163. char *result, unsigned maxlen)
  1164. {
  1165. return 2;
  1166. }
  1167. /*
  1168. * Pass a message to the target that's at the supplied device offset.
  1169. */
  1170. static int target_message(struct dm_ioctl *param, size_t param_size)
  1171. {
  1172. int r, argc;
  1173. char **argv;
  1174. struct mapped_device *md;
  1175. struct dm_table *table;
  1176. struct dm_target *ti;
  1177. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  1178. size_t maxlen;
  1179. char *result = get_result_buffer(param, param_size, &maxlen);
  1180. md = find_device(param);
  1181. if (!md)
  1182. return -ENXIO;
  1183. if (tmsg < (struct dm_target_msg *) param->data ||
  1184. invalid_str(tmsg->message, (void *) param + param_size)) {
  1185. DMWARN("Invalid target message parameters.");
  1186. r = -EINVAL;
  1187. goto out;
  1188. }
  1189. r = dm_split_args(&argc, &argv, tmsg->message);
  1190. if (r) {
  1191. DMWARN("Failed to split target message parameters");
  1192. goto out;
  1193. }
  1194. if (!argc) {
  1195. DMWARN("Empty message received.");
  1196. goto out_argv;
  1197. }
  1198. r = message_for_md(md, argc, argv, result, maxlen);
  1199. if (r <= 1)
  1200. goto out_argv;
  1201. table = dm_get_live_table(md);
  1202. if (!table)
  1203. goto out_argv;
  1204. if (dm_deleting_md(md)) {
  1205. r = -ENXIO;
  1206. goto out_table;
  1207. }
  1208. ti = dm_table_find_target(table, tmsg->sector);
  1209. if (!dm_target_is_valid(ti)) {
  1210. DMWARN("Target message sector outside device.");
  1211. r = -EINVAL;
  1212. } else if (ti->type->message)
  1213. r = ti->type->message(ti, argc, argv);
  1214. else {
  1215. DMWARN("Target type does not support messages");
  1216. r = -EINVAL;
  1217. }
  1218. out_table:
  1219. dm_table_put(table);
  1220. out_argv:
  1221. kfree(argv);
  1222. out:
  1223. if (r >= 0)
  1224. __dev_status(md, param);
  1225. if (r == 1) {
  1226. param->flags |= DM_DATA_OUT_FLAG;
  1227. if (buffer_test_overflow(result, maxlen))
  1228. param->flags |= DM_BUFFER_FULL_FLAG;
  1229. else
  1230. param->data_size = param->data_start + strlen(result) + 1;
  1231. r = 0;
  1232. }
  1233. dm_put(md);
  1234. return r;
  1235. }
  1236. /*
  1237. * The ioctl parameter block consists of two parts, a dm_ioctl struct
  1238. * followed by a data buffer. This flag is set if the second part,
  1239. * which has a variable size, is not used by the function processing
  1240. * the ioctl.
  1241. */
  1242. #define IOCTL_FLAGS_NO_PARAMS 1
  1243. /*-----------------------------------------------------------------
  1244. * Implementation of open/close/ioctl on the special char
  1245. * device.
  1246. *---------------------------------------------------------------*/
  1247. static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
  1248. {
  1249. static struct {
  1250. int cmd;
  1251. int flags;
  1252. ioctl_fn fn;
  1253. } _ioctls[] = {
  1254. {DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
  1255. {DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS, remove_all},
  1256. {DM_LIST_DEVICES_CMD, 0, list_devices},
  1257. {DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS, dev_create},
  1258. {DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS, dev_remove},
  1259. {DM_DEV_RENAME_CMD, 0, dev_rename},
  1260. {DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
  1261. {DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
  1262. {DM_DEV_WAIT_CMD, 0, dev_wait},
  1263. {DM_TABLE_LOAD_CMD, 0, table_load},
  1264. {DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
  1265. {DM_TABLE_DEPS_CMD, 0, table_deps},
  1266. {DM_TABLE_STATUS_CMD, 0, table_status},
  1267. {DM_LIST_VERSIONS_CMD, 0, list_versions},
  1268. {DM_TARGET_MSG_CMD, 0, target_message},
  1269. {DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry}
  1270. };
  1271. if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
  1272. return NULL;
  1273. *ioctl_flags = _ioctls[cmd].flags;
  1274. return _ioctls[cmd].fn;
  1275. }
  1276. /*
  1277. * As well as checking the version compatibility this always
  1278. * copies the kernel interface version out.
  1279. */
  1280. static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
  1281. {
  1282. uint32_t version[3];
  1283. int r = 0;
  1284. if (copy_from_user(version, user->version, sizeof(version)))
  1285. return -EFAULT;
  1286. if ((DM_VERSION_MAJOR != version[0]) ||
  1287. (DM_VERSION_MINOR < version[1])) {
  1288. DMWARN("ioctl interface mismatch: "
  1289. "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1290. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1291. DM_VERSION_PATCHLEVEL,
  1292. version[0], version[1], version[2], cmd);
  1293. r = -EINVAL;
  1294. }
  1295. /*
  1296. * Fill in the kernel version.
  1297. */
  1298. version[0] = DM_VERSION_MAJOR;
  1299. version[1] = DM_VERSION_MINOR;
  1300. version[2] = DM_VERSION_PATCHLEVEL;
  1301. if (copy_to_user(user->version, version, sizeof(version)))
  1302. return -EFAULT;
  1303. return r;
  1304. }
  1305. #define DM_PARAMS_KMALLOC 0x0001 /* Params alloced with kmalloc */
  1306. #define DM_PARAMS_VMALLOC 0x0002 /* Params alloced with vmalloc */
  1307. #define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
  1308. static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
  1309. {
  1310. if (param_flags & DM_WIPE_BUFFER)
  1311. memset(param, 0, param_size);
  1312. if (param_flags & DM_PARAMS_KMALLOC)
  1313. kfree(param);
  1314. if (param_flags & DM_PARAMS_VMALLOC)
  1315. vfree(param);
  1316. }
  1317. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
  1318. int ioctl_flags,
  1319. struct dm_ioctl **param, int *param_flags)
  1320. {
  1321. struct dm_ioctl *dmi;
  1322. int secure_data;
  1323. const size_t minimum_data_size = sizeof(*param_kernel) - sizeof(param_kernel->data);
  1324. if (copy_from_user(param_kernel, user, minimum_data_size))
  1325. return -EFAULT;
  1326. if (param_kernel->data_size < minimum_data_size)
  1327. return -EINVAL;
  1328. secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
  1329. *param_flags = secure_data ? DM_WIPE_BUFFER : 0;
  1330. if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
  1331. dmi = param_kernel;
  1332. dmi->data_size = minimum_data_size;
  1333. goto data_copied;
  1334. }
  1335. /*
  1336. * Try to avoid low memory issues when a device is suspended.
  1337. * Use kmalloc() rather than vmalloc() when we can.
  1338. */
  1339. dmi = NULL;
  1340. if (param_kernel->data_size <= KMALLOC_MAX_SIZE) {
  1341. dmi = kmalloc(param_kernel->data_size, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  1342. if (dmi)
  1343. *param_flags |= DM_PARAMS_KMALLOC;
  1344. }
  1345. if (!dmi) {
  1346. dmi = __vmalloc(param_kernel->data_size, GFP_NOIO | __GFP_REPEAT | __GFP_HIGH, PAGE_KERNEL);
  1347. if (dmi)
  1348. *param_flags |= DM_PARAMS_VMALLOC;
  1349. }
  1350. if (!dmi) {
  1351. if (secure_data && clear_user(user, param_kernel->data_size))
  1352. return -EFAULT;
  1353. return -ENOMEM;
  1354. }
  1355. if (copy_from_user(dmi, user, param_kernel->data_size))
  1356. goto bad;
  1357. data_copied:
  1358. /*
  1359. * Abort if something changed the ioctl data while it was being copied.
  1360. */
  1361. if (dmi->data_size != param_kernel->data_size) {
  1362. DMERR("rejecting ioctl: data size modified while processing parameters");
  1363. goto bad;
  1364. }
  1365. /* Wipe the user buffer so we do not return it to userspace */
  1366. if (secure_data && clear_user(user, param_kernel->data_size))
  1367. goto bad;
  1368. *param = dmi;
  1369. return 0;
  1370. bad:
  1371. free_params(dmi, param_kernel->data_size, *param_flags);
  1372. return -EFAULT;
  1373. }
  1374. static int validate_params(uint cmd, struct dm_ioctl *param)
  1375. {
  1376. /* Always clear this flag */
  1377. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1378. param->flags &= ~DM_UEVENT_GENERATED_FLAG;
  1379. param->flags &= ~DM_SECURE_DATA_FLAG;
  1380. param->flags &= ~DM_DATA_OUT_FLAG;
  1381. /* Ignores parameters */
  1382. if (cmd == DM_REMOVE_ALL_CMD ||
  1383. cmd == DM_LIST_DEVICES_CMD ||
  1384. cmd == DM_LIST_VERSIONS_CMD)
  1385. return 0;
  1386. if ((cmd == DM_DEV_CREATE_CMD)) {
  1387. if (!*param->name) {
  1388. DMWARN("name not supplied when creating device");
  1389. return -EINVAL;
  1390. }
  1391. } else if ((*param->uuid && *param->name)) {
  1392. DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
  1393. return -EINVAL;
  1394. }
  1395. /* Ensure strings are terminated */
  1396. param->name[DM_NAME_LEN - 1] = '\0';
  1397. param->uuid[DM_UUID_LEN - 1] = '\0';
  1398. return 0;
  1399. }
  1400. static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
  1401. {
  1402. int r = 0;
  1403. int ioctl_flags;
  1404. int param_flags;
  1405. unsigned int cmd;
  1406. struct dm_ioctl *uninitialized_var(param);
  1407. ioctl_fn fn = NULL;
  1408. size_t input_param_size;
  1409. struct dm_ioctl param_kernel;
  1410. /* only root can play with this */
  1411. if (!capable(CAP_SYS_ADMIN))
  1412. return -EACCES;
  1413. if (_IOC_TYPE(command) != DM_IOCTL)
  1414. return -ENOTTY;
  1415. cmd = _IOC_NR(command);
  1416. /*
  1417. * Check the interface version passed in. This also
  1418. * writes out the kernel's interface version.
  1419. */
  1420. r = check_version(cmd, user);
  1421. if (r)
  1422. return r;
  1423. /*
  1424. * Nothing more to do for the version command.
  1425. */
  1426. if (cmd == DM_VERSION_CMD)
  1427. return 0;
  1428. fn = lookup_ioctl(cmd, &ioctl_flags);
  1429. if (!fn) {
  1430. DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
  1431. return -ENOTTY;
  1432. }
  1433. /*
  1434. * Copy the parameters into kernel space.
  1435. */
  1436. r = copy_params(user, &param_kernel, ioctl_flags, &param, &param_flags);
  1437. if (r)
  1438. return r;
  1439. input_param_size = param->data_size;
  1440. r = validate_params(cmd, param);
  1441. if (r)
  1442. goto out;
  1443. param->data_size = sizeof(*param);
  1444. r = fn(param, input_param_size);
  1445. if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
  1446. unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
  1447. DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
  1448. /*
  1449. * Copy the results back to userland.
  1450. */
  1451. if (!r && copy_to_user(user, param, param->data_size))
  1452. r = -EFAULT;
  1453. out:
  1454. free_params(param, input_param_size, param_flags);
  1455. return r;
  1456. }
  1457. static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
  1458. {
  1459. return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
  1460. }
  1461. #ifdef CONFIG_COMPAT
  1462. static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
  1463. {
  1464. return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
  1465. }
  1466. #else
  1467. #define dm_compat_ctl_ioctl NULL
  1468. #endif
  1469. static const struct file_operations _ctl_fops = {
  1470. .open = nonseekable_open,
  1471. .unlocked_ioctl = dm_ctl_ioctl,
  1472. .compat_ioctl = dm_compat_ctl_ioctl,
  1473. .owner = THIS_MODULE,
  1474. .llseek = noop_llseek,
  1475. };
  1476. static struct miscdevice _dm_misc = {
  1477. .minor = MAPPER_CTRL_MINOR,
  1478. .name = DM_NAME,
  1479. .nodename = DM_DIR "/" DM_CONTROL_NODE,
  1480. .fops = &_ctl_fops
  1481. };
  1482. MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
  1483. MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
  1484. /*
  1485. * Create misc character device and link to DM_DIR/control.
  1486. */
  1487. int __init dm_interface_init(void)
  1488. {
  1489. int r;
  1490. r = dm_hash_init();
  1491. if (r)
  1492. return r;
  1493. r = misc_register(&_dm_misc);
  1494. if (r) {
  1495. DMERR("misc_register failed for control device");
  1496. dm_hash_exit();
  1497. return r;
  1498. }
  1499. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1500. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1501. DM_DRIVER_EMAIL);
  1502. return 0;
  1503. }
  1504. void dm_interface_exit(void)
  1505. {
  1506. if (misc_deregister(&_dm_misc) < 0)
  1507. DMERR("misc_deregister failed for control device");
  1508. dm_hash_exit();
  1509. }
  1510. /**
  1511. * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
  1512. * @md: Pointer to mapped_device
  1513. * @name: Buffer (size DM_NAME_LEN) for name
  1514. * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
  1515. */
  1516. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
  1517. {
  1518. int r = 0;
  1519. struct hash_cell *hc;
  1520. if (!md)
  1521. return -ENXIO;
  1522. mutex_lock(&dm_hash_cells_mutex);
  1523. hc = dm_get_mdptr(md);
  1524. if (!hc || hc->md != md) {
  1525. r = -ENXIO;
  1526. goto out;
  1527. }
  1528. if (name)
  1529. strcpy(name, hc->name);
  1530. if (uuid)
  1531. strcpy(uuid, hc->uuid ? : "");
  1532. out:
  1533. mutex_unlock(&dm_hash_cells_mutex);
  1534. return r;
  1535. }