dm-ioctl.c 51 KB

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