dm-ioctl.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
  4. * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved.
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #include "dm-core.h"
  9. #include "dm-ima.h"
  10. #include <linux/module.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/sched/mm.h>
  14. #include <linux/init.h>
  15. #include <linux/wait.h>
  16. #include <linux/slab.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/dm-ioctl.h>
  19. #include <linux/hdreg.h>
  20. #include <linux/compat.h>
  21. #include <linux/nospec.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/ima.h>
  24. #define DM_MSG_PREFIX "ioctl"
  25. #define DM_DRIVER_EMAIL "dm-devel@lists.linux.dev"
  26. struct dm_file {
  27. /*
  28. * poll will wait until the global event number is greater than
  29. * this value.
  30. */
  31. volatile unsigned int global_event_nr;
  32. };
  33. /*
  34. *---------------------------------------------------------------
  35. * The ioctl interface needs to be able to look up devices by
  36. * name or uuid.
  37. *---------------------------------------------------------------
  38. */
  39. struct hash_cell {
  40. struct rb_node name_node;
  41. struct rb_node uuid_node;
  42. bool name_set;
  43. bool uuid_set;
  44. char *name;
  45. char *uuid;
  46. struct mapped_device *md;
  47. struct dm_table *new_map;
  48. };
  49. struct vers_iter {
  50. size_t param_size;
  51. struct dm_target_versions *vers, *old_vers;
  52. char *end;
  53. uint32_t flags;
  54. };
  55. static struct rb_root name_rb_tree = RB_ROOT;
  56. static struct rb_root uuid_rb_tree = RB_ROOT;
  57. static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred);
  58. /*
  59. * Guards access to both hash tables.
  60. */
  61. static DECLARE_RWSEM(_hash_lock);
  62. /*
  63. * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
  64. */
  65. static DEFINE_MUTEX(dm_hash_cells_mutex);
  66. static void dm_hash_exit(void)
  67. {
  68. dm_hash_remove_all(false, false, false);
  69. }
  70. /*
  71. *---------------------------------------------------------------
  72. * Code for looking up a device by name
  73. *---------------------------------------------------------------
  74. */
  75. static struct hash_cell *__get_name_cell(const char *str)
  76. {
  77. struct rb_node *n = name_rb_tree.rb_node;
  78. while (n) {
  79. struct hash_cell *hc = container_of(n, struct hash_cell, name_node);
  80. int c;
  81. c = strcmp(hc->name, str);
  82. if (!c) {
  83. dm_get(hc->md);
  84. return hc;
  85. }
  86. n = c >= 0 ? n->rb_left : n->rb_right;
  87. }
  88. return NULL;
  89. }
  90. static struct hash_cell *__get_uuid_cell(const char *str)
  91. {
  92. struct rb_node *n = uuid_rb_tree.rb_node;
  93. while (n) {
  94. struct hash_cell *hc = container_of(n, struct hash_cell, uuid_node);
  95. int c;
  96. c = strcmp(hc->uuid, str);
  97. if (!c) {
  98. dm_get(hc->md);
  99. return hc;
  100. }
  101. n = c >= 0 ? n->rb_left : n->rb_right;
  102. }
  103. return NULL;
  104. }
  105. static void __unlink_name(struct hash_cell *hc)
  106. {
  107. if (hc->name_set) {
  108. hc->name_set = false;
  109. rb_erase(&hc->name_node, &name_rb_tree);
  110. }
  111. }
  112. static void __unlink_uuid(struct hash_cell *hc)
  113. {
  114. if (hc->uuid_set) {
  115. hc->uuid_set = false;
  116. rb_erase(&hc->uuid_node, &uuid_rb_tree);
  117. }
  118. }
  119. static void __link_name(struct hash_cell *new_hc)
  120. {
  121. struct rb_node **n, *parent;
  122. __unlink_name(new_hc);
  123. new_hc->name_set = true;
  124. n = &name_rb_tree.rb_node;
  125. parent = NULL;
  126. while (*n) {
  127. struct hash_cell *hc = container_of(*n, struct hash_cell, name_node);
  128. int c;
  129. c = strcmp(hc->name, new_hc->name);
  130. BUG_ON(!c);
  131. parent = *n;
  132. n = c >= 0 ? &hc->name_node.rb_left : &hc->name_node.rb_right;
  133. }
  134. rb_link_node(&new_hc->name_node, parent, n);
  135. rb_insert_color(&new_hc->name_node, &name_rb_tree);
  136. }
  137. static void __link_uuid(struct hash_cell *new_hc)
  138. {
  139. struct rb_node **n, *parent;
  140. __unlink_uuid(new_hc);
  141. new_hc->uuid_set = true;
  142. n = &uuid_rb_tree.rb_node;
  143. parent = NULL;
  144. while (*n) {
  145. struct hash_cell *hc = container_of(*n, struct hash_cell, uuid_node);
  146. int c;
  147. c = strcmp(hc->uuid, new_hc->uuid);
  148. BUG_ON(!c);
  149. parent = *n;
  150. n = c > 0 ? &hc->uuid_node.rb_left : &hc->uuid_node.rb_right;
  151. }
  152. rb_link_node(&new_hc->uuid_node, parent, n);
  153. rb_insert_color(&new_hc->uuid_node, &uuid_rb_tree);
  154. }
  155. static struct hash_cell *__get_dev_cell(uint64_t dev)
  156. {
  157. struct mapped_device *md;
  158. struct hash_cell *hc;
  159. md = dm_get_md(huge_decode_dev(dev));
  160. if (!md)
  161. return NULL;
  162. hc = dm_get_mdptr(md);
  163. if (!hc) {
  164. dm_put(md);
  165. return NULL;
  166. }
  167. return hc;
  168. }
  169. /*
  170. *---------------------------------------------------------------
  171. * Inserting, removing and renaming a device.
  172. *---------------------------------------------------------------
  173. */
  174. static struct hash_cell *alloc_cell(const char *name, const char *uuid,
  175. struct mapped_device *md)
  176. {
  177. struct hash_cell *hc;
  178. hc = kmalloc_obj(*hc);
  179. if (!hc)
  180. return NULL;
  181. hc->name = kstrdup(name, GFP_KERNEL);
  182. if (!hc->name) {
  183. kfree(hc);
  184. return NULL;
  185. }
  186. if (!uuid)
  187. hc->uuid = NULL;
  188. else {
  189. hc->uuid = kstrdup(uuid, GFP_KERNEL);
  190. if (!hc->uuid) {
  191. kfree(hc->name);
  192. kfree(hc);
  193. return NULL;
  194. }
  195. }
  196. hc->name_set = hc->uuid_set = false;
  197. hc->md = md;
  198. hc->new_map = NULL;
  199. return hc;
  200. }
  201. static void free_cell(struct hash_cell *hc)
  202. {
  203. if (hc) {
  204. kfree(hc->name);
  205. kfree(hc->uuid);
  206. kfree(hc);
  207. }
  208. }
  209. /*
  210. * The kdev_t and uuid of a device can never change once it is
  211. * initially inserted.
  212. */
  213. static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
  214. {
  215. struct hash_cell *cell, *hc;
  216. /*
  217. * Allocate the new cells.
  218. */
  219. cell = alloc_cell(name, uuid, md);
  220. if (!cell)
  221. return -ENOMEM;
  222. /*
  223. * Insert the cell into both hash tables.
  224. */
  225. down_write(&_hash_lock);
  226. hc = __get_name_cell(name);
  227. if (hc) {
  228. dm_put(hc->md);
  229. goto bad;
  230. }
  231. __link_name(cell);
  232. if (uuid) {
  233. hc = __get_uuid_cell(uuid);
  234. if (hc) {
  235. __unlink_name(cell);
  236. dm_put(hc->md);
  237. goto bad;
  238. }
  239. __link_uuid(cell);
  240. }
  241. dm_get(md);
  242. mutex_lock(&dm_hash_cells_mutex);
  243. dm_set_mdptr(md, cell);
  244. mutex_unlock(&dm_hash_cells_mutex);
  245. up_write(&_hash_lock);
  246. return 0;
  247. bad:
  248. up_write(&_hash_lock);
  249. free_cell(cell);
  250. return -EBUSY;
  251. }
  252. static struct dm_table *__hash_remove(struct hash_cell *hc)
  253. {
  254. struct dm_table *table;
  255. int srcu_idx;
  256. lockdep_assert_held(&_hash_lock);
  257. /* remove from the dev trees */
  258. __unlink_name(hc);
  259. __unlink_uuid(hc);
  260. mutex_lock(&dm_hash_cells_mutex);
  261. dm_set_mdptr(hc->md, NULL);
  262. mutex_unlock(&dm_hash_cells_mutex);
  263. table = dm_get_live_table(hc->md, &srcu_idx);
  264. if (table)
  265. dm_table_event(table);
  266. dm_put_live_table(hc->md, srcu_idx);
  267. table = NULL;
  268. if (hc->new_map)
  269. table = hc->new_map;
  270. dm_put(hc->md);
  271. free_cell(hc);
  272. return table;
  273. }
  274. static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred)
  275. {
  276. int dev_skipped;
  277. struct rb_node *n;
  278. struct hash_cell *hc;
  279. struct mapped_device *md;
  280. struct dm_table *t;
  281. retry:
  282. dev_skipped = 0;
  283. down_write(&_hash_lock);
  284. for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
  285. hc = container_of(n, struct hash_cell, name_node);
  286. md = hc->md;
  287. dm_get(md);
  288. if (keep_open_devices &&
  289. dm_lock_for_deletion(md, mark_deferred, only_deferred)) {
  290. dm_put(md);
  291. dev_skipped++;
  292. continue;
  293. }
  294. t = __hash_remove(hc);
  295. up_write(&_hash_lock);
  296. if (t) {
  297. dm_sync_table(md);
  298. dm_table_destroy(t);
  299. }
  300. dm_ima_measure_on_device_remove(md, true);
  301. dm_put(md);
  302. if (likely(keep_open_devices))
  303. dm_destroy(md);
  304. else
  305. dm_destroy_immediate(md);
  306. /*
  307. * Some mapped devices may be using other mapped
  308. * devices, so repeat until we make no further
  309. * progress. If a new mapped device is created
  310. * here it will also get removed.
  311. */
  312. goto retry;
  313. }
  314. up_write(&_hash_lock);
  315. if (dev_skipped)
  316. DMWARN("remove_all left %d open device(s)", dev_skipped);
  317. }
  318. /*
  319. * Set the uuid of a hash_cell that isn't already set.
  320. */
  321. static void __set_cell_uuid(struct hash_cell *hc, char *new_uuid)
  322. {
  323. mutex_lock(&dm_hash_cells_mutex);
  324. hc->uuid = new_uuid;
  325. mutex_unlock(&dm_hash_cells_mutex);
  326. __link_uuid(hc);
  327. }
  328. /*
  329. * Changes the name of a hash_cell and returns the old name for
  330. * the caller to free.
  331. */
  332. static char *__change_cell_name(struct hash_cell *hc, char *new_name)
  333. {
  334. char *old_name;
  335. /*
  336. * Rename and move the name cell.
  337. */
  338. __unlink_name(hc);
  339. old_name = hc->name;
  340. mutex_lock(&dm_hash_cells_mutex);
  341. hc->name = new_name;
  342. mutex_unlock(&dm_hash_cells_mutex);
  343. __link_name(hc);
  344. return old_name;
  345. }
  346. static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
  347. const char *new)
  348. {
  349. char *new_data, *old_name = NULL;
  350. struct hash_cell *hc;
  351. struct dm_table *table;
  352. struct mapped_device *md;
  353. unsigned int change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
  354. int srcu_idx;
  355. /*
  356. * duplicate new.
  357. */
  358. new_data = kstrdup(new, GFP_KERNEL);
  359. if (!new_data)
  360. return ERR_PTR(-ENOMEM);
  361. down_write(&_hash_lock);
  362. /*
  363. * Is new free ?
  364. */
  365. if (change_uuid)
  366. hc = __get_uuid_cell(new);
  367. else
  368. hc = __get_name_cell(new);
  369. if (hc) {
  370. DMERR("Unable to change %s on mapped device %s to one that already exists: %s",
  371. change_uuid ? "uuid" : "name",
  372. param->name, new);
  373. dm_put(hc->md);
  374. up_write(&_hash_lock);
  375. kfree(new_data);
  376. return ERR_PTR(-EBUSY);
  377. }
  378. /*
  379. * Is there such a device as 'old' ?
  380. */
  381. hc = __get_name_cell(param->name);
  382. if (!hc) {
  383. DMERR("Unable to rename non-existent device, %s to %s%s",
  384. param->name, change_uuid ? "uuid " : "", new);
  385. up_write(&_hash_lock);
  386. kfree(new_data);
  387. return ERR_PTR(-ENXIO);
  388. }
  389. /*
  390. * Does this device already have a uuid?
  391. */
  392. if (change_uuid && hc->uuid) {
  393. DMERR("Unable to change uuid of mapped device %s to %s "
  394. "because uuid is already set to %s",
  395. param->name, new, hc->uuid);
  396. dm_put(hc->md);
  397. up_write(&_hash_lock);
  398. kfree(new_data);
  399. return ERR_PTR(-EINVAL);
  400. }
  401. if (change_uuid)
  402. __set_cell_uuid(hc, new_data);
  403. else
  404. old_name = __change_cell_name(hc, new_data);
  405. /*
  406. * Wake up any dm event waiters.
  407. */
  408. table = dm_get_live_table(hc->md, &srcu_idx);
  409. if (table)
  410. dm_table_event(table);
  411. dm_put_live_table(hc->md, srcu_idx);
  412. if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr, false))
  413. param->flags |= DM_UEVENT_GENERATED_FLAG;
  414. md = hc->md;
  415. dm_ima_measure_on_device_rename(md);
  416. up_write(&_hash_lock);
  417. kfree(old_name);
  418. return md;
  419. }
  420. void dm_deferred_remove(void)
  421. {
  422. dm_hash_remove_all(true, false, true);
  423. }
  424. /*
  425. *---------------------------------------------------------------
  426. * Implementation of the ioctl commands
  427. *---------------------------------------------------------------
  428. */
  429. /*
  430. * All the ioctl commands get dispatched to functions with this
  431. * prototype.
  432. */
  433. typedef int (*ioctl_fn)(struct file *filp, struct dm_ioctl *param, size_t param_size);
  434. static int remove_all(struct file *filp, struct dm_ioctl *param, size_t param_size)
  435. {
  436. dm_hash_remove_all(true, !!(param->flags & DM_DEFERRED_REMOVE), false);
  437. param->data_size = 0;
  438. return 0;
  439. }
  440. /*
  441. * Round up the ptr to an 8-byte boundary.
  442. */
  443. #define ALIGN_MASK 7
  444. static inline size_t align_val(size_t val)
  445. {
  446. return (val + ALIGN_MASK) & ~ALIGN_MASK;
  447. }
  448. static inline void *align_ptr(void *ptr)
  449. {
  450. return (void *)align_val((size_t)ptr);
  451. }
  452. /*
  453. * Retrieves the data payload buffer from an already allocated
  454. * struct dm_ioctl.
  455. */
  456. static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
  457. size_t *len)
  458. {
  459. param->data_start = align_ptr(param + 1) - (void *) param;
  460. if (param->data_start < param_size)
  461. *len = param_size - param->data_start;
  462. else
  463. *len = 0;
  464. return ((void *) param) + param->data_start;
  465. }
  466. static bool filter_device(struct hash_cell *hc, const char *pfx_name, const char *pfx_uuid)
  467. {
  468. const char *val;
  469. size_t val_len, pfx_len;
  470. val = hc->name;
  471. val_len = strlen(val);
  472. pfx_len = strnlen(pfx_name, DM_NAME_LEN);
  473. if (pfx_len > val_len)
  474. return false;
  475. if (memcmp(val, pfx_name, pfx_len))
  476. return false;
  477. val = hc->uuid ? hc->uuid : "";
  478. val_len = strlen(val);
  479. pfx_len = strnlen(pfx_uuid, DM_UUID_LEN);
  480. if (pfx_len > val_len)
  481. return false;
  482. if (memcmp(val, pfx_uuid, pfx_len))
  483. return false;
  484. return true;
  485. }
  486. static int list_devices(struct file *filp, struct dm_ioctl *param, size_t param_size)
  487. {
  488. struct rb_node *n;
  489. struct hash_cell *hc;
  490. size_t len, needed = 0;
  491. struct gendisk *disk;
  492. struct dm_name_list *orig_nl, *nl, *old_nl = NULL;
  493. uint32_t *event_nr;
  494. down_write(&_hash_lock);
  495. /*
  496. * Loop through all the devices working out how much
  497. * space we need.
  498. */
  499. for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
  500. hc = container_of(n, struct hash_cell, name_node);
  501. if (!filter_device(hc, param->name, param->uuid))
  502. continue;
  503. needed += align_val(offsetof(struct dm_name_list, name) + strlen(hc->name) + 1);
  504. needed += align_val(sizeof(uint32_t) * 2);
  505. if (param->flags & DM_UUID_FLAG && hc->uuid)
  506. needed += align_val(strlen(hc->uuid) + 1);
  507. }
  508. /*
  509. * Grab our output buffer.
  510. */
  511. nl = orig_nl = get_result_buffer(param, param_size, &len);
  512. if (len < needed || len < sizeof(nl->dev)) {
  513. param->flags |= DM_BUFFER_FULL_FLAG;
  514. goto out;
  515. }
  516. param->data_size = param->data_start + needed;
  517. nl->dev = 0; /* Flags no data */
  518. /*
  519. * Now loop through filling out the names.
  520. */
  521. for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
  522. void *uuid_ptr;
  523. hc = container_of(n, struct hash_cell, name_node);
  524. if (!filter_device(hc, param->name, param->uuid))
  525. continue;
  526. if (old_nl)
  527. old_nl->next = (uint32_t) ((void *) nl -
  528. (void *) old_nl);
  529. disk = dm_disk(hc->md);
  530. nl->dev = huge_encode_dev(disk_devt(disk));
  531. nl->next = 0;
  532. strcpy(nl->name, hc->name);
  533. old_nl = nl;
  534. event_nr = align_ptr(nl->name + strlen(hc->name) + 1);
  535. event_nr[0] = dm_get_event_nr(hc->md);
  536. event_nr[1] = 0;
  537. uuid_ptr = align_ptr(event_nr + 2);
  538. if (param->flags & DM_UUID_FLAG) {
  539. if (hc->uuid) {
  540. event_nr[1] |= DM_NAME_LIST_FLAG_HAS_UUID;
  541. strcpy(uuid_ptr, hc->uuid);
  542. uuid_ptr = align_ptr(uuid_ptr + strlen(hc->uuid) + 1);
  543. } else {
  544. event_nr[1] |= DM_NAME_LIST_FLAG_DOESNT_HAVE_UUID;
  545. }
  546. }
  547. nl = uuid_ptr;
  548. }
  549. /*
  550. * If mismatch happens, security may be compromised due to buffer
  551. * overflow, so it's better to crash.
  552. */
  553. BUG_ON((char *)nl - (char *)orig_nl != needed);
  554. out:
  555. up_write(&_hash_lock);
  556. return 0;
  557. }
  558. static void list_version_get_needed(struct target_type *tt, void *needed_param)
  559. {
  560. size_t *needed = needed_param;
  561. *needed += sizeof(struct dm_target_versions);
  562. *needed += strlen(tt->name) + 1;
  563. *needed += ALIGN_MASK;
  564. }
  565. static void list_version_get_info(struct target_type *tt, void *param)
  566. {
  567. struct vers_iter *info = param;
  568. /* Check space - it might have changed since the first iteration */
  569. if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 > info->end) {
  570. info->flags = DM_BUFFER_FULL_FLAG;
  571. return;
  572. }
  573. if (info->old_vers)
  574. info->old_vers->next = (uint32_t) ((void *)info->vers - (void *)info->old_vers);
  575. info->vers->version[0] = tt->version[0];
  576. info->vers->version[1] = tt->version[1];
  577. info->vers->version[2] = tt->version[2];
  578. info->vers->next = 0;
  579. strcpy(info->vers->name, tt->name);
  580. info->old_vers = info->vers;
  581. info->vers = align_ptr((void *)(info->vers + 1) + strlen(tt->name) + 1);
  582. }
  583. static int __list_versions(struct dm_ioctl *param, size_t param_size, const char *name)
  584. {
  585. size_t len, needed = 0;
  586. struct dm_target_versions *vers;
  587. struct vers_iter iter_info;
  588. struct target_type *tt = NULL;
  589. if (name) {
  590. tt = dm_get_target_type(name);
  591. if (!tt)
  592. return -EINVAL;
  593. }
  594. /*
  595. * Loop through all the devices working out how much
  596. * space we need.
  597. */
  598. if (!tt)
  599. dm_target_iterate(list_version_get_needed, &needed);
  600. else
  601. list_version_get_needed(tt, &needed);
  602. /*
  603. * Grab our output buffer.
  604. */
  605. vers = get_result_buffer(param, param_size, &len);
  606. if (len < needed) {
  607. param->flags |= DM_BUFFER_FULL_FLAG;
  608. goto out;
  609. }
  610. param->data_size = param->data_start + needed;
  611. iter_info.param_size = param_size;
  612. iter_info.old_vers = NULL;
  613. iter_info.vers = vers;
  614. iter_info.flags = 0;
  615. iter_info.end = (char *)vers + needed;
  616. /*
  617. * Now loop through filling out the names & versions.
  618. */
  619. if (!tt)
  620. dm_target_iterate(list_version_get_info, &iter_info);
  621. else
  622. list_version_get_info(tt, &iter_info);
  623. param->flags |= iter_info.flags;
  624. out:
  625. if (tt)
  626. dm_put_target_type(tt);
  627. return 0;
  628. }
  629. static int list_versions(struct file *filp, struct dm_ioctl *param, size_t param_size)
  630. {
  631. return __list_versions(param, param_size, NULL);
  632. }
  633. static int get_target_version(struct file *filp, struct dm_ioctl *param, size_t param_size)
  634. {
  635. return __list_versions(param, param_size, param->name);
  636. }
  637. static int check_name(const char *name)
  638. {
  639. if (strchr(name, '/')) {
  640. DMERR("device name cannot contain '/'");
  641. return -EINVAL;
  642. }
  643. if (strcmp(name, DM_CONTROL_NODE) == 0 ||
  644. strcmp(name, ".") == 0 ||
  645. strcmp(name, "..") == 0) {
  646. DMERR("device name cannot be \"%s\", \".\", or \"..\"", DM_CONTROL_NODE);
  647. return -EINVAL;
  648. }
  649. return 0;
  650. }
  651. /*
  652. * On successful return, the caller must not attempt to acquire
  653. * _hash_lock without first calling dm_put_live_table, because dm_table_destroy
  654. * waits for this dm_put_live_table and could be called under this lock.
  655. */
  656. static struct dm_table *dm_get_inactive_table(struct mapped_device *md, int *srcu_idx)
  657. {
  658. struct hash_cell *hc;
  659. struct dm_table *table = NULL;
  660. /* increment rcu count, we don't care about the table pointer */
  661. dm_get_live_table(md, srcu_idx);
  662. down_read(&_hash_lock);
  663. hc = dm_get_mdptr(md);
  664. if (!hc) {
  665. DMERR("device has been removed from the dev hash table.");
  666. goto out;
  667. }
  668. table = hc->new_map;
  669. out:
  670. up_read(&_hash_lock);
  671. return table;
  672. }
  673. static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
  674. struct dm_ioctl *param,
  675. int *srcu_idx)
  676. {
  677. return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
  678. dm_get_inactive_table(md, srcu_idx) : dm_get_live_table(md, srcu_idx);
  679. }
  680. /*
  681. * Fills in a dm_ioctl structure, ready for sending back to
  682. * userland.
  683. */
  684. static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
  685. {
  686. struct gendisk *disk = dm_disk(md);
  687. struct dm_table *table;
  688. int srcu_idx;
  689. param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
  690. DM_ACTIVE_PRESENT_FLAG | DM_INTERNAL_SUSPEND_FLAG);
  691. if (dm_suspended_md(md))
  692. param->flags |= DM_SUSPEND_FLAG;
  693. if (dm_suspended_internally_md(md))
  694. param->flags |= DM_INTERNAL_SUSPEND_FLAG;
  695. if (dm_test_deferred_remove_flag(md))
  696. param->flags |= DM_DEFERRED_REMOVE;
  697. param->dev = huge_encode_dev(disk_devt(disk));
  698. /*
  699. * Yes, this will be out of date by the time it gets back
  700. * to userland, but it is still very useful for
  701. * debugging.
  702. */
  703. param->open_count = dm_open_count(md);
  704. param->event_nr = dm_get_event_nr(md);
  705. param->target_count = 0;
  706. table = dm_get_live_table(md, &srcu_idx);
  707. if (table) {
  708. if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
  709. if (get_disk_ro(disk))
  710. param->flags |= DM_READONLY_FLAG;
  711. param->target_count = table->num_targets;
  712. }
  713. param->flags |= DM_ACTIVE_PRESENT_FLAG;
  714. }
  715. dm_put_live_table(md, srcu_idx);
  716. if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
  717. int srcu_idx;
  718. table = dm_get_inactive_table(md, &srcu_idx);
  719. if (table) {
  720. if (!(dm_table_get_mode(table) & BLK_OPEN_WRITE))
  721. param->flags |= DM_READONLY_FLAG;
  722. param->target_count = table->num_targets;
  723. }
  724. dm_put_live_table(md, srcu_idx);
  725. }
  726. }
  727. static int dev_create(struct file *filp, struct dm_ioctl *param, size_t param_size)
  728. {
  729. int r, m = DM_ANY_MINOR;
  730. struct mapped_device *md;
  731. r = check_name(param->name);
  732. if (r)
  733. return r;
  734. if (param->flags & DM_PERSISTENT_DEV_FLAG)
  735. m = MINOR(huge_decode_dev(param->dev));
  736. r = dm_create(m, &md);
  737. if (r)
  738. return r;
  739. r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
  740. if (r) {
  741. dm_put(md);
  742. dm_destroy(md);
  743. return r;
  744. }
  745. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  746. __dev_status(md, param);
  747. dm_put(md);
  748. return 0;
  749. }
  750. /*
  751. * Always use UUID for lookups if it's present, otherwise use name or dev.
  752. */
  753. static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
  754. {
  755. struct hash_cell *hc = NULL;
  756. if (*param->uuid) {
  757. if (*param->name || param->dev) {
  758. DMERR("Invalid ioctl structure: uuid %s, name %s, dev %llx",
  759. param->uuid, param->name, (unsigned long long)param->dev);
  760. return NULL;
  761. }
  762. hc = __get_uuid_cell(param->uuid);
  763. if (!hc)
  764. return NULL;
  765. } else if (*param->name) {
  766. if (param->dev) {
  767. DMERR("Invalid ioctl structure: name %s, dev %llx",
  768. param->name, (unsigned long long)param->dev);
  769. return NULL;
  770. }
  771. hc = __get_name_cell(param->name);
  772. if (!hc)
  773. return NULL;
  774. } else if (param->dev) {
  775. hc = __get_dev_cell(param->dev);
  776. if (!hc)
  777. return NULL;
  778. } else
  779. return NULL;
  780. /*
  781. * Sneakily write in both the name and the uuid
  782. * while we have the cell.
  783. */
  784. strscpy(param->name, hc->name, sizeof(param->name));
  785. if (hc->uuid)
  786. strscpy(param->uuid, hc->uuid, sizeof(param->uuid));
  787. else
  788. param->uuid[0] = '\0';
  789. if (hc->new_map)
  790. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  791. else
  792. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  793. return hc;
  794. }
  795. static struct mapped_device *find_device(struct dm_ioctl *param)
  796. {
  797. struct hash_cell *hc;
  798. struct mapped_device *md = NULL;
  799. down_read(&_hash_lock);
  800. hc = __find_device_hash_cell(param);
  801. if (hc)
  802. md = hc->md;
  803. up_read(&_hash_lock);
  804. return md;
  805. }
  806. static int dev_remove(struct file *filp, struct dm_ioctl *param, size_t param_size)
  807. {
  808. struct hash_cell *hc;
  809. struct mapped_device *md;
  810. int r;
  811. struct dm_table *t;
  812. down_write(&_hash_lock);
  813. hc = __find_device_hash_cell(param);
  814. if (!hc) {
  815. DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
  816. up_write(&_hash_lock);
  817. return -ENXIO;
  818. }
  819. md = hc->md;
  820. /*
  821. * Ensure the device is not open and nothing further can open it.
  822. */
  823. r = dm_lock_for_deletion(md, !!(param->flags & DM_DEFERRED_REMOVE), false);
  824. if (r) {
  825. if (r == -EBUSY && param->flags & DM_DEFERRED_REMOVE) {
  826. up_write(&_hash_lock);
  827. dm_put(md);
  828. return 0;
  829. }
  830. DMDEBUG_LIMIT("unable to remove open device %s", hc->name);
  831. up_write(&_hash_lock);
  832. dm_put(md);
  833. return r;
  834. }
  835. t = __hash_remove(hc);
  836. up_write(&_hash_lock);
  837. if (t) {
  838. dm_sync_table(md);
  839. dm_table_destroy(t);
  840. }
  841. param->flags &= ~DM_DEFERRED_REMOVE;
  842. dm_ima_measure_on_device_remove(md, false);
  843. if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr, false))
  844. param->flags |= DM_UEVENT_GENERATED_FLAG;
  845. dm_put(md);
  846. dm_destroy(md);
  847. return 0;
  848. }
  849. /*
  850. * Check a string doesn't overrun the chunk of
  851. * memory we copied from userland.
  852. */
  853. static int invalid_str(char *str, void *end)
  854. {
  855. while ((void *) str < end)
  856. if (!*str++)
  857. return 0;
  858. return -EINVAL;
  859. }
  860. static int dev_rename(struct file *filp, struct dm_ioctl *param, size_t param_size)
  861. {
  862. int r;
  863. char *new_data = (char *) param + param->data_start;
  864. struct mapped_device *md;
  865. unsigned int change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
  866. if (new_data < param->data ||
  867. invalid_str(new_data, (void *) param + param_size) || !*new_data ||
  868. strlen(new_data) > (change_uuid ? DM_UUID_LEN - 1 : DM_NAME_LEN - 1)) {
  869. DMERR("Invalid new mapped device name or uuid string supplied.");
  870. return -EINVAL;
  871. }
  872. if (!change_uuid) {
  873. r = check_name(new_data);
  874. if (r)
  875. return r;
  876. }
  877. md = dm_hash_rename(param, new_data);
  878. if (IS_ERR(md))
  879. return PTR_ERR(md);
  880. __dev_status(md, param);
  881. dm_put(md);
  882. return 0;
  883. }
  884. static int dev_set_geometry(struct file *filp, struct dm_ioctl *param, size_t param_size)
  885. {
  886. int r = -EINVAL, x;
  887. struct mapped_device *md;
  888. struct hd_geometry geometry;
  889. unsigned long indata[4];
  890. char *geostr = (char *) param + param->data_start;
  891. char dummy;
  892. md = find_device(param);
  893. if (!md)
  894. return -ENXIO;
  895. if (geostr < param->data ||
  896. invalid_str(geostr, (void *) param + param_size)) {
  897. DMERR("Invalid geometry supplied.");
  898. goto out;
  899. }
  900. x = sscanf(geostr, "%lu %lu %lu %lu%c", indata,
  901. indata + 1, indata + 2, indata + 3, &dummy);
  902. if (x != 4) {
  903. DMERR("Unable to interpret geometry settings.");
  904. goto out;
  905. }
  906. if (indata[0] > 65535 || indata[1] > 255 || indata[2] > 255) {
  907. DMERR("Geometry exceeds range limits.");
  908. goto out;
  909. }
  910. geometry.cylinders = indata[0];
  911. geometry.heads = indata[1];
  912. geometry.sectors = indata[2];
  913. geometry.start = indata[3];
  914. r = dm_set_geometry(md, &geometry);
  915. param->data_size = 0;
  916. out:
  917. dm_put(md);
  918. return r;
  919. }
  920. static int do_suspend(struct dm_ioctl *param)
  921. {
  922. int r = 0;
  923. unsigned int suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  924. struct mapped_device *md;
  925. md = find_device(param);
  926. if (!md)
  927. return -ENXIO;
  928. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  929. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  930. if (param->flags & DM_NOFLUSH_FLAG)
  931. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  932. if (!dm_suspended_md(md)) {
  933. r = dm_suspend(md, suspend_flags);
  934. if (r)
  935. goto out;
  936. }
  937. __dev_status(md, param);
  938. out:
  939. dm_put(md);
  940. return r;
  941. }
  942. static int do_resume(struct dm_ioctl *param)
  943. {
  944. int r = 0;
  945. unsigned int suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  946. struct hash_cell *hc;
  947. struct mapped_device *md;
  948. struct dm_table *new_map, *old_map = NULL;
  949. bool need_resize_uevent = false;
  950. down_write(&_hash_lock);
  951. hc = __find_device_hash_cell(param);
  952. if (!hc) {
  953. DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
  954. up_write(&_hash_lock);
  955. return -ENXIO;
  956. }
  957. md = hc->md;
  958. new_map = hc->new_map;
  959. hc->new_map = NULL;
  960. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  961. up_write(&_hash_lock);
  962. /* Do we need to load a new map ? */
  963. if (new_map) {
  964. sector_t old_size, new_size;
  965. /* Suspend if it isn't already suspended */
  966. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  967. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  968. if (param->flags & DM_NOFLUSH_FLAG)
  969. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  970. if (!dm_suspended_md(md)) {
  971. r = dm_suspend(md, suspend_flags);
  972. if (r) {
  973. down_write(&_hash_lock);
  974. hc = dm_get_mdptr(md);
  975. if (hc && !hc->new_map) {
  976. hc->new_map = new_map;
  977. new_map = NULL;
  978. } else {
  979. r = -ENXIO;
  980. }
  981. up_write(&_hash_lock);
  982. if (new_map) {
  983. dm_sync_table(md);
  984. dm_table_destroy(new_map);
  985. }
  986. dm_put(md);
  987. return r;
  988. }
  989. }
  990. old_size = dm_get_size(md);
  991. old_map = dm_swap_table(md, new_map);
  992. if (IS_ERR(old_map)) {
  993. dm_sync_table(md);
  994. dm_table_destroy(new_map);
  995. dm_put(md);
  996. return PTR_ERR(old_map);
  997. }
  998. new_size = dm_get_size(md);
  999. if (old_size && new_size && old_size != new_size)
  1000. need_resize_uevent = true;
  1001. if (dm_table_get_mode(new_map) & BLK_OPEN_WRITE)
  1002. set_disk_ro(dm_disk(md), 0);
  1003. else
  1004. set_disk_ro(dm_disk(md), 1);
  1005. }
  1006. if (dm_suspended_md(md)) {
  1007. r = dm_resume(md);
  1008. if (!r) {
  1009. dm_ima_measure_on_device_resume(md, new_map ? true : false);
  1010. if (!dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr, need_resize_uevent))
  1011. param->flags |= DM_UEVENT_GENERATED_FLAG;
  1012. }
  1013. }
  1014. /*
  1015. * Since dm_swap_table synchronizes RCU, nobody should be in
  1016. * read-side critical section already.
  1017. */
  1018. if (old_map)
  1019. dm_table_destroy(old_map);
  1020. if (!r)
  1021. __dev_status(md, param);
  1022. dm_put(md);
  1023. return r;
  1024. }
  1025. /*
  1026. * Set or unset the suspension state of a device.
  1027. * If the device already is in the requested state we just return its status.
  1028. */
  1029. static int dev_suspend(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1030. {
  1031. if (param->flags & DM_SUSPEND_FLAG)
  1032. return do_suspend(param);
  1033. return do_resume(param);
  1034. }
  1035. /*
  1036. * Copies device info back to user space, used by
  1037. * the create and info ioctls.
  1038. */
  1039. static int dev_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1040. {
  1041. struct mapped_device *md;
  1042. md = find_device(param);
  1043. if (!md)
  1044. return -ENXIO;
  1045. __dev_status(md, param);
  1046. dm_put(md);
  1047. return 0;
  1048. }
  1049. /*
  1050. * Build up the status struct for each target
  1051. */
  1052. static void retrieve_status(struct dm_table *table,
  1053. struct dm_ioctl *param, size_t param_size)
  1054. {
  1055. unsigned int i, num_targets;
  1056. struct dm_target_spec *spec;
  1057. char *outbuf, *outptr;
  1058. status_type_t type;
  1059. size_t remaining, len, used = 0;
  1060. unsigned int status_flags = 0;
  1061. outptr = outbuf = get_result_buffer(param, param_size, &len);
  1062. if (param->flags & DM_STATUS_TABLE_FLAG)
  1063. type = STATUSTYPE_TABLE;
  1064. else if (param->flags & DM_IMA_MEASUREMENT_FLAG)
  1065. type = STATUSTYPE_IMA;
  1066. else
  1067. type = STATUSTYPE_INFO;
  1068. /* Get all the target info */
  1069. num_targets = table->num_targets;
  1070. for (i = 0; i < num_targets; i++) {
  1071. struct dm_target *ti = dm_table_get_target(table, i);
  1072. size_t l;
  1073. remaining = len - (outptr - outbuf);
  1074. if (remaining <= sizeof(struct dm_target_spec)) {
  1075. param->flags |= DM_BUFFER_FULL_FLAG;
  1076. break;
  1077. }
  1078. spec = (struct dm_target_spec *) outptr;
  1079. spec->status = 0;
  1080. spec->sector_start = ti->begin;
  1081. spec->length = ti->len;
  1082. strscpy_pad(spec->target_type, ti->type->name,
  1083. sizeof(spec->target_type));
  1084. outptr += sizeof(struct dm_target_spec);
  1085. remaining = len - (outptr - outbuf);
  1086. if (remaining <= 0) {
  1087. param->flags |= DM_BUFFER_FULL_FLAG;
  1088. break;
  1089. }
  1090. /* Get the status/table string from the target driver */
  1091. if (ti->type->status) {
  1092. if (param->flags & DM_NOFLUSH_FLAG)
  1093. status_flags |= DM_STATUS_NOFLUSH_FLAG;
  1094. ti->type->status(ti, type, status_flags, outptr, remaining);
  1095. } else
  1096. outptr[0] = '\0';
  1097. l = strlen(outptr) + 1;
  1098. if (l == remaining) {
  1099. param->flags |= DM_BUFFER_FULL_FLAG;
  1100. break;
  1101. }
  1102. outptr += l;
  1103. used = param->data_start + (outptr - outbuf);
  1104. outptr = align_ptr(outptr);
  1105. spec->next = outptr - outbuf;
  1106. }
  1107. if (used)
  1108. param->data_size = used;
  1109. param->target_count = num_targets;
  1110. }
  1111. /*
  1112. * Wait for a device to report an event
  1113. */
  1114. static int dev_wait(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1115. {
  1116. int r = 0;
  1117. struct mapped_device *md;
  1118. struct dm_table *table;
  1119. int srcu_idx;
  1120. md = find_device(param);
  1121. if (!md)
  1122. return -ENXIO;
  1123. /*
  1124. * Wait for a notification event
  1125. */
  1126. if (dm_wait_event(md, param->event_nr)) {
  1127. r = -ERESTARTSYS;
  1128. goto out;
  1129. }
  1130. /*
  1131. * The userland program is going to want to know what
  1132. * changed to trigger the event, so we may as well tell
  1133. * him and save an ioctl.
  1134. */
  1135. __dev_status(md, param);
  1136. table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
  1137. if (table)
  1138. retrieve_status(table, param, param_size);
  1139. dm_put_live_table(md, srcu_idx);
  1140. out:
  1141. dm_put(md);
  1142. return r;
  1143. }
  1144. /*
  1145. * Remember the global event number and make it possible to poll
  1146. * for further events.
  1147. */
  1148. static int dev_arm_poll(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1149. {
  1150. struct dm_file *priv = filp->private_data;
  1151. priv->global_event_nr = atomic_read(&dm_global_event_nr);
  1152. return 0;
  1153. }
  1154. static inline blk_mode_t get_mode(struct dm_ioctl *param)
  1155. {
  1156. blk_mode_t mode = BLK_OPEN_READ | BLK_OPEN_WRITE;
  1157. if (param->flags & DM_READONLY_FLAG)
  1158. mode = BLK_OPEN_READ;
  1159. return mode;
  1160. }
  1161. static int next_target(struct dm_target_spec *last, uint32_t next, const char *end,
  1162. struct dm_target_spec **spec, char **target_params)
  1163. {
  1164. static_assert(__alignof__(struct dm_target_spec) <= 8,
  1165. "struct dm_target_spec must not require more than 8-byte alignment");
  1166. /*
  1167. * Number of bytes remaining, starting with last. This is always
  1168. * sizeof(struct dm_target_spec) or more, as otherwise *last was
  1169. * out of bounds already.
  1170. */
  1171. size_t remaining = end - (char *)last;
  1172. /*
  1173. * There must be room for both the next target spec and the
  1174. * NUL-terminator of the target itself.
  1175. */
  1176. if (remaining - sizeof(struct dm_target_spec) <= next) {
  1177. DMERR("Target spec extends beyond end of parameters");
  1178. return -EINVAL;
  1179. }
  1180. if (next % __alignof__(struct dm_target_spec)) {
  1181. DMERR("Next dm_target_spec (offset %u) is not %zu-byte aligned",
  1182. next, __alignof__(struct dm_target_spec));
  1183. return -EINVAL;
  1184. }
  1185. *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
  1186. *target_params = (char *) (*spec + 1);
  1187. return 0;
  1188. }
  1189. static int populate_table(struct dm_table *table,
  1190. struct dm_ioctl *param, size_t param_size)
  1191. {
  1192. int r;
  1193. unsigned int i = 0;
  1194. struct dm_target_spec *spec = (struct dm_target_spec *) param;
  1195. uint32_t next = param->data_start;
  1196. const char *const end = (const char *) param + param_size;
  1197. char *target_params;
  1198. size_t min_size = sizeof(struct dm_ioctl);
  1199. if (!param->target_count) {
  1200. DMERR("%s: no targets specified", __func__);
  1201. return -EINVAL;
  1202. }
  1203. for (i = 0; i < param->target_count; i++) {
  1204. const char *nul_terminator;
  1205. if (next < min_size) {
  1206. DMERR("%s: next target spec (offset %u) overlaps %s",
  1207. __func__, next, i ? "previous target" : "'struct dm_ioctl'");
  1208. return -EINVAL;
  1209. }
  1210. r = next_target(spec, next, end, &spec, &target_params);
  1211. if (r) {
  1212. DMERR("unable to find target");
  1213. return r;
  1214. }
  1215. nul_terminator = memchr(target_params, 0, (size_t)(end - target_params));
  1216. if (nul_terminator == NULL) {
  1217. DMERR("%s: target parameters not NUL-terminated", __func__);
  1218. return -EINVAL;
  1219. }
  1220. /* Add 1 for NUL terminator */
  1221. min_size = (size_t)(nul_terminator - (const char *)spec) + 1;
  1222. r = dm_table_add_target(table, spec->target_type,
  1223. (sector_t) spec->sector_start,
  1224. (sector_t) spec->length,
  1225. target_params);
  1226. if (r) {
  1227. DMERR("error adding target to table");
  1228. return r;
  1229. }
  1230. next = spec->next;
  1231. }
  1232. return dm_table_complete(table);
  1233. }
  1234. static bool is_valid_type(enum dm_queue_mode cur, enum dm_queue_mode new)
  1235. {
  1236. if (cur == new ||
  1237. (cur == DM_TYPE_BIO_BASED && new == DM_TYPE_DAX_BIO_BASED))
  1238. return true;
  1239. return false;
  1240. }
  1241. static int table_load(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1242. {
  1243. int r;
  1244. struct hash_cell *hc;
  1245. struct dm_table *t, *old_map = NULL;
  1246. struct mapped_device *md;
  1247. struct target_type *immutable_target_type;
  1248. md = find_device(param);
  1249. if (!md)
  1250. return -ENXIO;
  1251. r = dm_table_create(&t, get_mode(param), param->target_count, md);
  1252. if (r)
  1253. goto err;
  1254. /* Protect md->type and md->queue against concurrent table loads. */
  1255. dm_lock_md_type(md);
  1256. r = populate_table(t, param, param_size);
  1257. if (r)
  1258. goto err_unlock_md_type;
  1259. dm_ima_measure_on_table_load(t, STATUSTYPE_IMA);
  1260. immutable_target_type = dm_get_immutable_target_type(md);
  1261. if (immutable_target_type &&
  1262. (immutable_target_type != dm_table_get_immutable_target_type(t)) &&
  1263. !dm_table_get_wildcard_target(t)) {
  1264. DMERR("can't replace immutable target type %s",
  1265. immutable_target_type->name);
  1266. r = -EINVAL;
  1267. goto err_unlock_md_type;
  1268. }
  1269. if (dm_get_md_type(md) == DM_TYPE_NONE) {
  1270. /* setup md->queue to reflect md's type (may block) */
  1271. r = dm_setup_md_queue(md, t);
  1272. if (r) {
  1273. DMERR("unable to set up device queue for new table.");
  1274. goto err_unlock_md_type;
  1275. }
  1276. } else if (!is_valid_type(dm_get_md_type(md), dm_table_get_type(t))) {
  1277. DMERR("can't change device type (old=%u vs new=%u) after initial table load.",
  1278. dm_get_md_type(md), dm_table_get_type(t));
  1279. r = -EINVAL;
  1280. goto err_unlock_md_type;
  1281. }
  1282. dm_unlock_md_type(md);
  1283. /* stage inactive table */
  1284. down_write(&_hash_lock);
  1285. hc = dm_get_mdptr(md);
  1286. if (!hc) {
  1287. DMERR("device has been removed from the dev hash table.");
  1288. up_write(&_hash_lock);
  1289. r = -ENXIO;
  1290. goto err_destroy_table;
  1291. }
  1292. if (hc->new_map)
  1293. old_map = hc->new_map;
  1294. hc->new_map = t;
  1295. up_write(&_hash_lock);
  1296. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  1297. __dev_status(md, param);
  1298. if (old_map) {
  1299. dm_sync_table(md);
  1300. dm_table_destroy(old_map);
  1301. }
  1302. dm_put(md);
  1303. return 0;
  1304. err_unlock_md_type:
  1305. dm_unlock_md_type(md);
  1306. err_destroy_table:
  1307. dm_table_destroy(t);
  1308. err:
  1309. dm_put(md);
  1310. return r;
  1311. }
  1312. static int table_clear(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1313. {
  1314. struct hash_cell *hc;
  1315. struct mapped_device *md;
  1316. struct dm_table *old_map = NULL;
  1317. bool has_new_map = false;
  1318. down_write(&_hash_lock);
  1319. hc = __find_device_hash_cell(param);
  1320. if (!hc) {
  1321. DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
  1322. up_write(&_hash_lock);
  1323. return -ENXIO;
  1324. }
  1325. if (hc->new_map) {
  1326. old_map = hc->new_map;
  1327. hc->new_map = NULL;
  1328. has_new_map = true;
  1329. }
  1330. md = hc->md;
  1331. up_write(&_hash_lock);
  1332. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  1333. __dev_status(md, param);
  1334. if (old_map) {
  1335. dm_sync_table(md);
  1336. dm_table_destroy(old_map);
  1337. }
  1338. dm_ima_measure_on_table_clear(md, has_new_map);
  1339. dm_put(md);
  1340. return 0;
  1341. }
  1342. /*
  1343. * Retrieves a list of devices used by a particular dm device.
  1344. */
  1345. static void retrieve_deps(struct dm_table *table,
  1346. struct dm_ioctl *param, size_t param_size)
  1347. {
  1348. unsigned int count = 0;
  1349. struct list_head *tmp;
  1350. size_t len, needed;
  1351. struct dm_dev_internal *dd;
  1352. struct dm_target_deps *deps;
  1353. deps = get_result_buffer(param, param_size, &len);
  1354. /*
  1355. * Count the devices.
  1356. */
  1357. list_for_each(tmp, dm_table_get_devices(table))
  1358. count++;
  1359. /*
  1360. * Check we have enough space.
  1361. */
  1362. needed = struct_size(deps, dev, count);
  1363. if (len < needed) {
  1364. param->flags |= DM_BUFFER_FULL_FLAG;
  1365. return;
  1366. }
  1367. /*
  1368. * Fill in the devices.
  1369. */
  1370. deps->count = count;
  1371. count = 0;
  1372. list_for_each_entry(dd, dm_table_get_devices(table), list)
  1373. deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
  1374. param->data_size = param->data_start + needed;
  1375. }
  1376. static int table_deps(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1377. {
  1378. struct mapped_device *md;
  1379. struct dm_table *table;
  1380. int srcu_idx;
  1381. md = find_device(param);
  1382. if (!md)
  1383. return -ENXIO;
  1384. __dev_status(md, param);
  1385. table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
  1386. if (table)
  1387. retrieve_deps(table, param, param_size);
  1388. dm_put_live_table(md, srcu_idx);
  1389. dm_put(md);
  1390. return 0;
  1391. }
  1392. /*
  1393. * Return the status of a device as a text string for each
  1394. * target.
  1395. */
  1396. static int table_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1397. {
  1398. struct mapped_device *md;
  1399. struct dm_table *table;
  1400. int srcu_idx;
  1401. md = find_device(param);
  1402. if (!md)
  1403. return -ENXIO;
  1404. __dev_status(md, param);
  1405. table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
  1406. if (table)
  1407. retrieve_status(table, param, param_size);
  1408. dm_put_live_table(md, srcu_idx);
  1409. dm_put(md);
  1410. return 0;
  1411. }
  1412. /*
  1413. * Process device-mapper dependent messages. Messages prefixed with '@'
  1414. * are processed by the DM core. All others are delivered to the target.
  1415. * Returns a number <= 1 if message was processed by device mapper.
  1416. * Returns 2 if message should be delivered to the target.
  1417. */
  1418. static int message_for_md(struct mapped_device *md, unsigned int argc, char **argv,
  1419. char *result, unsigned int maxlen)
  1420. {
  1421. int r;
  1422. if (**argv != '@')
  1423. return 2; /* no '@' prefix, deliver to target */
  1424. if (!strcasecmp(argv[0], "@cancel_deferred_remove")) {
  1425. if (argc != 1) {
  1426. DMERR("Invalid arguments for @cancel_deferred_remove");
  1427. return -EINVAL;
  1428. }
  1429. return dm_cancel_deferred_remove(md);
  1430. }
  1431. r = dm_stats_message(md, argc, argv, result, maxlen);
  1432. if (r < 2)
  1433. return r;
  1434. DMERR("Unsupported message sent to DM core: %s", argv[0]);
  1435. return -EINVAL;
  1436. }
  1437. /*
  1438. * Pass a message to the target that's at the supplied device offset.
  1439. */
  1440. static int target_message(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1441. {
  1442. int r, argc;
  1443. char **argv;
  1444. struct mapped_device *md;
  1445. struct dm_table *table;
  1446. struct dm_target *ti;
  1447. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  1448. size_t maxlen;
  1449. char *result = get_result_buffer(param, param_size, &maxlen);
  1450. int srcu_idx;
  1451. md = find_device(param);
  1452. if (!md)
  1453. return -ENXIO;
  1454. if (tmsg < (struct dm_target_msg *) param->data ||
  1455. invalid_str(tmsg->message, (void *) param + param_size)) {
  1456. DMERR("Invalid target message parameters.");
  1457. r = -EINVAL;
  1458. goto out;
  1459. }
  1460. r = dm_split_args(&argc, &argv, tmsg->message);
  1461. if (r) {
  1462. DMERR("Failed to split target message parameters");
  1463. goto out;
  1464. }
  1465. if (!argc) {
  1466. DMERR("Empty message received.");
  1467. r = -EINVAL;
  1468. goto out_argv;
  1469. }
  1470. r = message_for_md(md, argc, argv, result, maxlen);
  1471. if (r <= 1)
  1472. goto out_argv;
  1473. table = dm_get_live_table(md, &srcu_idx);
  1474. if (!table)
  1475. goto out_table;
  1476. if (dm_deleting_md(md)) {
  1477. r = -ENXIO;
  1478. goto out_table;
  1479. }
  1480. ti = dm_table_find_target(table, tmsg->sector);
  1481. if (!ti) {
  1482. DMERR("Target message sector outside device.");
  1483. r = -EINVAL;
  1484. } else if (ti->type->message)
  1485. r = ti->type->message(ti, argc, argv, result, maxlen);
  1486. else {
  1487. DMERR("Target type does not support messages");
  1488. r = -EINVAL;
  1489. }
  1490. out_table:
  1491. dm_put_live_table(md, srcu_idx);
  1492. out_argv:
  1493. kfree(argv);
  1494. out:
  1495. if (r >= 0)
  1496. __dev_status(md, param);
  1497. if (r == 1) {
  1498. param->flags |= DM_DATA_OUT_FLAG;
  1499. if (dm_message_test_buffer_overflow(result, maxlen))
  1500. param->flags |= DM_BUFFER_FULL_FLAG;
  1501. else
  1502. param->data_size = param->data_start + strlen(result) + 1;
  1503. r = 0;
  1504. }
  1505. dm_put(md);
  1506. return r;
  1507. }
  1508. /*
  1509. * The ioctl parameter block consists of two parts, a dm_ioctl struct
  1510. * followed by a data buffer. This flag is set if the second part,
  1511. * which has a variable size, is not used by the function processing
  1512. * the ioctl.
  1513. */
  1514. #define IOCTL_FLAGS_NO_PARAMS 1
  1515. #define IOCTL_FLAGS_ISSUE_GLOBAL_EVENT 2
  1516. /*
  1517. *---------------------------------------------------------------
  1518. * Implementation of open/close/ioctl on the special char device.
  1519. *---------------------------------------------------------------
  1520. */
  1521. static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
  1522. {
  1523. static const struct {
  1524. int cmd;
  1525. int flags;
  1526. ioctl_fn fn;
  1527. } _ioctls[] = {
  1528. {DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
  1529. {DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, remove_all},
  1530. {DM_LIST_DEVICES_CMD, 0, list_devices},
  1531. {DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_create},
  1532. {DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_remove},
  1533. {DM_DEV_RENAME_CMD, IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_rename},
  1534. {DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
  1535. {DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
  1536. {DM_DEV_WAIT_CMD, 0, dev_wait},
  1537. {DM_TABLE_LOAD_CMD, 0, table_load},
  1538. {DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
  1539. {DM_TABLE_DEPS_CMD, 0, table_deps},
  1540. {DM_TABLE_STATUS_CMD, 0, table_status},
  1541. {DM_LIST_VERSIONS_CMD, 0, list_versions},
  1542. {DM_TARGET_MSG_CMD, 0, target_message},
  1543. {DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry},
  1544. {DM_DEV_ARM_POLL_CMD, IOCTL_FLAGS_NO_PARAMS, dev_arm_poll},
  1545. {DM_GET_TARGET_VERSION_CMD, 0, get_target_version},
  1546. {DM_MPATH_PROBE_PATHS_CMD, 0, NULL}, /* block device ioctl */
  1547. };
  1548. if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
  1549. return NULL;
  1550. cmd = array_index_nospec(cmd, ARRAY_SIZE(_ioctls));
  1551. *ioctl_flags = _ioctls[cmd].flags;
  1552. return _ioctls[cmd].fn;
  1553. }
  1554. /*
  1555. * As well as checking the version compatibility this always
  1556. * copies the kernel interface version out.
  1557. */
  1558. static int check_version(unsigned int cmd, struct dm_ioctl __user *user,
  1559. struct dm_ioctl *kernel_params)
  1560. {
  1561. int r = 0;
  1562. /* Make certain version is first member of dm_ioctl struct */
  1563. BUILD_BUG_ON(offsetof(struct dm_ioctl, version) != 0);
  1564. if (copy_from_user(kernel_params->version, user->version, sizeof(kernel_params->version)))
  1565. return -EFAULT;
  1566. if ((kernel_params->version[0] != DM_VERSION_MAJOR) ||
  1567. (kernel_params->version[1] > DM_VERSION_MINOR)) {
  1568. DMERR_LIMIT("ioctl interface mismatch: kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1569. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1570. DM_VERSION_PATCHLEVEL,
  1571. kernel_params->version[0],
  1572. kernel_params->version[1],
  1573. kernel_params->version[2],
  1574. cmd);
  1575. r = -EINVAL;
  1576. }
  1577. /*
  1578. * Fill in the kernel version.
  1579. */
  1580. kernel_params->version[0] = DM_VERSION_MAJOR;
  1581. kernel_params->version[1] = DM_VERSION_MINOR;
  1582. kernel_params->version[2] = DM_VERSION_PATCHLEVEL;
  1583. if (copy_to_user(user->version, kernel_params->version, sizeof(kernel_params->version)))
  1584. return -EFAULT;
  1585. return r;
  1586. }
  1587. #define DM_PARAMS_MALLOC 0x0001 /* Params allocated with kvmalloc() */
  1588. #define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
  1589. static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
  1590. {
  1591. if (param_flags & DM_WIPE_BUFFER)
  1592. memset(param, 0, param_size);
  1593. if (param_flags & DM_PARAMS_MALLOC)
  1594. kvfree(param);
  1595. }
  1596. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
  1597. int ioctl_flags, struct dm_ioctl **param, int *param_flags)
  1598. {
  1599. struct dm_ioctl *dmi;
  1600. int secure_data;
  1601. const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
  1602. /* check_version() already copied version from userspace, avoid TOCTOU */
  1603. if (copy_from_user((char *)param_kernel + sizeof(param_kernel->version),
  1604. (char __user *)user + sizeof(param_kernel->version),
  1605. minimum_data_size - sizeof(param_kernel->version)))
  1606. return -EFAULT;
  1607. if (unlikely(param_kernel->data_size < minimum_data_size) ||
  1608. unlikely(param_kernel->data_size > DM_MAX_TARGETS * DM_MAX_TARGET_PARAMS)) {
  1609. DMERR_LIMIT("Invalid data size in the ioctl structure: %u",
  1610. param_kernel->data_size);
  1611. return -EINVAL;
  1612. }
  1613. secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
  1614. *param_flags = secure_data ? DM_WIPE_BUFFER : 0;
  1615. if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
  1616. dmi = param_kernel;
  1617. dmi->data_size = minimum_data_size;
  1618. goto data_copied;
  1619. }
  1620. /*
  1621. * Use __GFP_HIGH to avoid low memory issues when a device is
  1622. * suspended and the ioctl is needed to resume it.
  1623. * Use kmalloc() rather than vmalloc() when we can.
  1624. */
  1625. dmi = NULL;
  1626. dmi = kvmalloc(param_kernel->data_size, GFP_NOIO | __GFP_HIGH);
  1627. if (!dmi) {
  1628. if (secure_data && clear_user(user, param_kernel->data_size))
  1629. return -EFAULT;
  1630. return -ENOMEM;
  1631. }
  1632. *param_flags |= DM_PARAMS_MALLOC;
  1633. /* Copy from param_kernel (which was already copied from user) */
  1634. memcpy(dmi, param_kernel, minimum_data_size);
  1635. if (copy_from_user(&dmi->data, (char __user *)user + minimum_data_size,
  1636. param_kernel->data_size - minimum_data_size))
  1637. goto bad;
  1638. data_copied:
  1639. /* Wipe the user buffer so we do not return it to userspace */
  1640. if (secure_data && clear_user(user, param_kernel->data_size))
  1641. goto bad;
  1642. *param = dmi;
  1643. return 0;
  1644. bad:
  1645. free_params(dmi, param_kernel->data_size, *param_flags);
  1646. return -EFAULT;
  1647. }
  1648. static int validate_params(uint cmd, struct dm_ioctl *param)
  1649. {
  1650. /* Always clear this flag */
  1651. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1652. param->flags &= ~DM_UEVENT_GENERATED_FLAG;
  1653. param->flags &= ~DM_SECURE_DATA_FLAG;
  1654. param->flags &= ~DM_DATA_OUT_FLAG;
  1655. /* Ignores parameters */
  1656. if (cmd == DM_REMOVE_ALL_CMD ||
  1657. cmd == DM_LIST_DEVICES_CMD ||
  1658. cmd == DM_LIST_VERSIONS_CMD)
  1659. return 0;
  1660. if (cmd == DM_DEV_CREATE_CMD) {
  1661. if (!*param->name) {
  1662. DMERR("name not supplied when creating device");
  1663. return -EINVAL;
  1664. }
  1665. } else if (*param->uuid && *param->name) {
  1666. DMERR("only supply one of name or uuid, cmd(%u)", cmd);
  1667. return -EINVAL;
  1668. }
  1669. /* Ensure strings are terminated */
  1670. param->name[DM_NAME_LEN - 1] = '\0';
  1671. param->uuid[DM_UUID_LEN - 1] = '\0';
  1672. return 0;
  1673. }
  1674. static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *user)
  1675. {
  1676. int r = 0;
  1677. int ioctl_flags;
  1678. int param_flags;
  1679. unsigned int cmd;
  1680. struct dm_ioctl *param;
  1681. ioctl_fn fn = NULL;
  1682. size_t input_param_size;
  1683. struct dm_ioctl param_kernel;
  1684. /* only root can play with this */
  1685. if (!capable(CAP_SYS_ADMIN))
  1686. return -EACCES;
  1687. if (_IOC_TYPE(command) != DM_IOCTL)
  1688. return -ENOTTY;
  1689. cmd = _IOC_NR(command);
  1690. /*
  1691. * Check the interface version passed in. This also
  1692. * writes out the kernel's interface version.
  1693. */
  1694. r = check_version(cmd, user, &param_kernel);
  1695. if (r)
  1696. return r;
  1697. /*
  1698. * Nothing more to do for the version command.
  1699. */
  1700. if (cmd == DM_VERSION_CMD)
  1701. return 0;
  1702. fn = lookup_ioctl(cmd, &ioctl_flags);
  1703. if (!fn) {
  1704. DMERR("dm_ctl_ioctl: unknown command 0x%x", command);
  1705. return -ENOTTY;
  1706. }
  1707. /*
  1708. * Copy the parameters into kernel space.
  1709. */
  1710. r = copy_params(user, &param_kernel, ioctl_flags, &param, &param_flags);
  1711. if (r)
  1712. return r;
  1713. input_param_size = param->data_size;
  1714. r = validate_params(cmd, param);
  1715. if (r)
  1716. goto out;
  1717. param->data_size = offsetof(struct dm_ioctl, data);
  1718. r = fn(file, param, input_param_size);
  1719. if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
  1720. unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
  1721. DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
  1722. if (!r && ioctl_flags & IOCTL_FLAGS_ISSUE_GLOBAL_EVENT)
  1723. dm_issue_global_event();
  1724. /*
  1725. * Copy the results back to userland.
  1726. */
  1727. if (!r && copy_to_user(user, param, param->data_size))
  1728. r = -EFAULT;
  1729. out:
  1730. free_params(param, input_param_size, param_flags);
  1731. return r;
  1732. }
  1733. static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
  1734. {
  1735. return (long)ctl_ioctl(file, command, (struct dm_ioctl __user *)u);
  1736. }
  1737. #ifdef CONFIG_COMPAT
  1738. static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
  1739. {
  1740. return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
  1741. }
  1742. #else
  1743. #define dm_compat_ctl_ioctl NULL
  1744. #endif
  1745. static int dm_open(struct inode *inode, struct file *filp)
  1746. {
  1747. int r;
  1748. struct dm_file *priv;
  1749. r = nonseekable_open(inode, filp);
  1750. if (unlikely(r))
  1751. return r;
  1752. priv = filp->private_data = kmalloc_obj(struct dm_file);
  1753. if (!priv)
  1754. return -ENOMEM;
  1755. priv->global_event_nr = atomic_read(&dm_global_event_nr);
  1756. return 0;
  1757. }
  1758. static int dm_release(struct inode *inode, struct file *filp)
  1759. {
  1760. kfree(filp->private_data);
  1761. return 0;
  1762. }
  1763. static __poll_t dm_poll(struct file *filp, poll_table *wait)
  1764. {
  1765. struct dm_file *priv = filp->private_data;
  1766. __poll_t mask = 0;
  1767. poll_wait(filp, &dm_global_eventq, wait);
  1768. if ((int)(atomic_read(&dm_global_event_nr) - priv->global_event_nr) > 0)
  1769. mask |= EPOLLIN;
  1770. return mask;
  1771. }
  1772. static const struct file_operations _ctl_fops = {
  1773. .open = dm_open,
  1774. .release = dm_release,
  1775. .poll = dm_poll,
  1776. .unlocked_ioctl = dm_ctl_ioctl,
  1777. .compat_ioctl = dm_compat_ctl_ioctl,
  1778. .owner = THIS_MODULE,
  1779. .llseek = noop_llseek,
  1780. };
  1781. static struct miscdevice _dm_misc = {
  1782. .minor = MAPPER_CTRL_MINOR,
  1783. .name = DM_NAME,
  1784. .nodename = DM_DIR "/" DM_CONTROL_NODE,
  1785. .fops = &_ctl_fops
  1786. };
  1787. MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
  1788. MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
  1789. /*
  1790. * Create misc character device and link to DM_DIR/control.
  1791. */
  1792. int __init dm_interface_init(void)
  1793. {
  1794. int r;
  1795. r = misc_register(&_dm_misc);
  1796. if (r) {
  1797. DMERR("misc_register failed for control device");
  1798. return r;
  1799. }
  1800. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1801. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1802. DM_DRIVER_EMAIL);
  1803. return 0;
  1804. }
  1805. void dm_interface_exit(void)
  1806. {
  1807. misc_deregister(&_dm_misc);
  1808. dm_hash_exit();
  1809. }
  1810. /**
  1811. * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
  1812. * @md: Pointer to mapped_device
  1813. * @name: Buffer (size DM_NAME_LEN) for name
  1814. * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
  1815. */
  1816. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
  1817. {
  1818. int r = 0;
  1819. struct hash_cell *hc;
  1820. if (!md)
  1821. return -ENXIO;
  1822. mutex_lock(&dm_hash_cells_mutex);
  1823. hc = dm_get_mdptr(md);
  1824. if (!hc) {
  1825. r = -ENXIO;
  1826. goto out;
  1827. }
  1828. if (name)
  1829. strcpy(name, hc->name);
  1830. if (uuid)
  1831. strcpy(uuid, hc->uuid ? : "");
  1832. out:
  1833. mutex_unlock(&dm_hash_cells_mutex);
  1834. return r;
  1835. }
  1836. EXPORT_SYMBOL_GPL(dm_copy_name_and_uuid);
  1837. /**
  1838. * dm_early_create - create a mapped device in early boot.
  1839. *
  1840. * @dmi: Contains main information of the device mapping to be created.
  1841. * @spec_array: array of pointers to struct dm_target_spec. Describes the
  1842. * mapping table of the device.
  1843. * @target_params_array: array of strings with the parameters to a specific
  1844. * target.
  1845. *
  1846. * Instead of having the struct dm_target_spec and the parameters for every
  1847. * target embedded at the end of struct dm_ioctl (as performed in a normal
  1848. * ioctl), pass them as arguments, so the caller doesn't need to serialize them.
  1849. * The size of the spec_array and target_params_array is given by
  1850. * @dmi->target_count.
  1851. * This function is supposed to be called in early boot, so locking mechanisms
  1852. * to protect against concurrent loads are not required.
  1853. */
  1854. int __init dm_early_create(struct dm_ioctl *dmi,
  1855. struct dm_target_spec **spec_array,
  1856. char **target_params_array)
  1857. {
  1858. int r, m = DM_ANY_MINOR;
  1859. struct dm_table *t, *old_map;
  1860. struct mapped_device *md;
  1861. unsigned int i;
  1862. if (!dmi->target_count)
  1863. return -EINVAL;
  1864. r = check_name(dmi->name);
  1865. if (r)
  1866. return r;
  1867. if (dmi->flags & DM_PERSISTENT_DEV_FLAG)
  1868. m = MINOR(huge_decode_dev(dmi->dev));
  1869. /* alloc dm device */
  1870. r = dm_create(m, &md);
  1871. if (r)
  1872. return r;
  1873. /* hash insert */
  1874. r = dm_hash_insert(dmi->name, *dmi->uuid ? dmi->uuid : NULL, md);
  1875. if (r)
  1876. goto err_destroy_dm;
  1877. /* alloc table */
  1878. r = dm_table_create(&t, get_mode(dmi), dmi->target_count, md);
  1879. if (r)
  1880. goto err_hash_remove;
  1881. /* add targets */
  1882. for (i = 0; i < dmi->target_count; i++) {
  1883. r = dm_table_add_target(t, spec_array[i]->target_type,
  1884. (sector_t) spec_array[i]->sector_start,
  1885. (sector_t) spec_array[i]->length,
  1886. target_params_array[i]);
  1887. if (r) {
  1888. DMERR("error adding target to table");
  1889. goto err_destroy_table;
  1890. }
  1891. }
  1892. /* finish table */
  1893. r = dm_table_complete(t);
  1894. if (r)
  1895. goto err_destroy_table;
  1896. /* setup md->queue to reflect md's type (may block) */
  1897. r = dm_setup_md_queue(md, t);
  1898. if (r) {
  1899. DMERR("unable to set up device queue for new table.");
  1900. goto err_destroy_table;
  1901. }
  1902. /* Set new map */
  1903. dm_suspend(md, 0);
  1904. old_map = dm_swap_table(md, t);
  1905. if (IS_ERR(old_map)) {
  1906. r = PTR_ERR(old_map);
  1907. goto err_destroy_table;
  1908. }
  1909. set_disk_ro(dm_disk(md), !!(dmi->flags & DM_READONLY_FLAG));
  1910. /* resume device */
  1911. r = dm_resume(md);
  1912. if (r)
  1913. goto err_destroy_table;
  1914. DMINFO("%s (%s) is ready", md->disk->disk_name, dmi->name);
  1915. dm_put(md);
  1916. return 0;
  1917. err_destroy_table:
  1918. dm_table_destroy(t);
  1919. err_hash_remove:
  1920. down_write(&_hash_lock);
  1921. (void) __hash_remove(__get_name_cell(dmi->name));
  1922. up_write(&_hash_lock);
  1923. /* release reference from __get_name_cell */
  1924. dm_put(md);
  1925. err_destroy_dm:
  1926. dm_put(md);
  1927. dm_destroy(md);
  1928. return r;
  1929. }