dm-ioctl.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  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(sizeof(*hc), GFP_KERNEL);
  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. down_read(&table->devices_lock);
  1354. deps = get_result_buffer(param, param_size, &len);
  1355. /*
  1356. * Count the devices.
  1357. */
  1358. list_for_each(tmp, dm_table_get_devices(table))
  1359. count++;
  1360. /*
  1361. * Check we have enough space.
  1362. */
  1363. needed = struct_size(deps, dev, count);
  1364. if (len < needed) {
  1365. param->flags |= DM_BUFFER_FULL_FLAG;
  1366. goto out;
  1367. }
  1368. /*
  1369. * Fill in the devices.
  1370. */
  1371. deps->count = count;
  1372. count = 0;
  1373. list_for_each_entry(dd, dm_table_get_devices(table), list)
  1374. deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
  1375. param->data_size = param->data_start + needed;
  1376. out:
  1377. up_read(&table->devices_lock);
  1378. }
  1379. static int table_deps(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1380. {
  1381. struct mapped_device *md;
  1382. struct dm_table *table;
  1383. int srcu_idx;
  1384. md = find_device(param);
  1385. if (!md)
  1386. return -ENXIO;
  1387. __dev_status(md, param);
  1388. table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
  1389. if (table)
  1390. retrieve_deps(table, param, param_size);
  1391. dm_put_live_table(md, srcu_idx);
  1392. dm_put(md);
  1393. return 0;
  1394. }
  1395. /*
  1396. * Return the status of a device as a text string for each
  1397. * target.
  1398. */
  1399. static int table_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1400. {
  1401. struct mapped_device *md;
  1402. struct dm_table *table;
  1403. int srcu_idx;
  1404. md = find_device(param);
  1405. if (!md)
  1406. return -ENXIO;
  1407. __dev_status(md, param);
  1408. table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
  1409. if (table)
  1410. retrieve_status(table, param, param_size);
  1411. dm_put_live_table(md, srcu_idx);
  1412. dm_put(md);
  1413. return 0;
  1414. }
  1415. /*
  1416. * Process device-mapper dependent messages. Messages prefixed with '@'
  1417. * are processed by the DM core. All others are delivered to the target.
  1418. * Returns a number <= 1 if message was processed by device mapper.
  1419. * Returns 2 if message should be delivered to the target.
  1420. */
  1421. static int message_for_md(struct mapped_device *md, unsigned int argc, char **argv,
  1422. char *result, unsigned int maxlen)
  1423. {
  1424. int r;
  1425. if (**argv != '@')
  1426. return 2; /* no '@' prefix, deliver to target */
  1427. if (!strcasecmp(argv[0], "@cancel_deferred_remove")) {
  1428. if (argc != 1) {
  1429. DMERR("Invalid arguments for @cancel_deferred_remove");
  1430. return -EINVAL;
  1431. }
  1432. return dm_cancel_deferred_remove(md);
  1433. }
  1434. r = dm_stats_message(md, argc, argv, result, maxlen);
  1435. if (r < 2)
  1436. return r;
  1437. DMERR("Unsupported message sent to DM core: %s", argv[0]);
  1438. return -EINVAL;
  1439. }
  1440. /*
  1441. * Pass a message to the target that's at the supplied device offset.
  1442. */
  1443. static int target_message(struct file *filp, struct dm_ioctl *param, size_t param_size)
  1444. {
  1445. int r, argc;
  1446. char **argv;
  1447. struct mapped_device *md;
  1448. struct dm_table *table;
  1449. struct dm_target *ti;
  1450. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  1451. size_t maxlen;
  1452. char *result = get_result_buffer(param, param_size, &maxlen);
  1453. int srcu_idx;
  1454. md = find_device(param);
  1455. if (!md)
  1456. return -ENXIO;
  1457. if (tmsg < (struct dm_target_msg *) param->data ||
  1458. invalid_str(tmsg->message, (void *) param + param_size)) {
  1459. DMERR("Invalid target message parameters.");
  1460. r = -EINVAL;
  1461. goto out;
  1462. }
  1463. r = dm_split_args(&argc, &argv, tmsg->message);
  1464. if (r) {
  1465. DMERR("Failed to split target message parameters");
  1466. goto out;
  1467. }
  1468. if (!argc) {
  1469. DMERR("Empty message received.");
  1470. r = -EINVAL;
  1471. goto out_argv;
  1472. }
  1473. r = message_for_md(md, argc, argv, result, maxlen);
  1474. if (r <= 1)
  1475. goto out_argv;
  1476. table = dm_get_live_table(md, &srcu_idx);
  1477. if (!table)
  1478. goto out_table;
  1479. if (dm_deleting_md(md)) {
  1480. r = -ENXIO;
  1481. goto out_table;
  1482. }
  1483. ti = dm_table_find_target(table, tmsg->sector);
  1484. if (!ti) {
  1485. DMERR("Target message sector outside device.");
  1486. r = -EINVAL;
  1487. } else if (ti->type->message)
  1488. r = ti->type->message(ti, argc, argv, result, maxlen);
  1489. else {
  1490. DMERR("Target type does not support messages");
  1491. r = -EINVAL;
  1492. }
  1493. out_table:
  1494. dm_put_live_table(md, srcu_idx);
  1495. out_argv:
  1496. kfree(argv);
  1497. out:
  1498. if (r >= 0)
  1499. __dev_status(md, param);
  1500. if (r == 1) {
  1501. param->flags |= DM_DATA_OUT_FLAG;
  1502. if (dm_message_test_buffer_overflow(result, maxlen))
  1503. param->flags |= DM_BUFFER_FULL_FLAG;
  1504. else
  1505. param->data_size = param->data_start + strlen(result) + 1;
  1506. r = 0;
  1507. }
  1508. dm_put(md);
  1509. return r;
  1510. }
  1511. /*
  1512. * The ioctl parameter block consists of two parts, a dm_ioctl struct
  1513. * followed by a data buffer. This flag is set if the second part,
  1514. * which has a variable size, is not used by the function processing
  1515. * the ioctl.
  1516. */
  1517. #define IOCTL_FLAGS_NO_PARAMS 1
  1518. #define IOCTL_FLAGS_ISSUE_GLOBAL_EVENT 2
  1519. /*
  1520. *---------------------------------------------------------------
  1521. * Implementation of open/close/ioctl on the special char device.
  1522. *---------------------------------------------------------------
  1523. */
  1524. static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
  1525. {
  1526. static const struct {
  1527. int cmd;
  1528. int flags;
  1529. ioctl_fn fn;
  1530. } _ioctls[] = {
  1531. {DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
  1532. {DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, remove_all},
  1533. {DM_LIST_DEVICES_CMD, 0, list_devices},
  1534. {DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_create},
  1535. {DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_remove},
  1536. {DM_DEV_RENAME_CMD, IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_rename},
  1537. {DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
  1538. {DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
  1539. {DM_DEV_WAIT_CMD, 0, dev_wait},
  1540. {DM_TABLE_LOAD_CMD, 0, table_load},
  1541. {DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
  1542. {DM_TABLE_DEPS_CMD, 0, table_deps},
  1543. {DM_TABLE_STATUS_CMD, 0, table_status},
  1544. {DM_LIST_VERSIONS_CMD, 0, list_versions},
  1545. {DM_TARGET_MSG_CMD, 0, target_message},
  1546. {DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry},
  1547. {DM_DEV_ARM_POLL_CMD, IOCTL_FLAGS_NO_PARAMS, dev_arm_poll},
  1548. {DM_GET_TARGET_VERSION_CMD, 0, get_target_version},
  1549. };
  1550. if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
  1551. return NULL;
  1552. cmd = array_index_nospec(cmd, ARRAY_SIZE(_ioctls));
  1553. *ioctl_flags = _ioctls[cmd].flags;
  1554. return _ioctls[cmd].fn;
  1555. }
  1556. /*
  1557. * As well as checking the version compatibility this always
  1558. * copies the kernel interface version out.
  1559. */
  1560. static int check_version(unsigned int cmd, struct dm_ioctl __user *user,
  1561. struct dm_ioctl *kernel_params)
  1562. {
  1563. int r = 0;
  1564. /* Make certain version is first member of dm_ioctl struct */
  1565. BUILD_BUG_ON(offsetof(struct dm_ioctl, version) != 0);
  1566. if (copy_from_user(kernel_params->version, user->version, sizeof(kernel_params->version)))
  1567. return -EFAULT;
  1568. if ((kernel_params->version[0] != DM_VERSION_MAJOR) ||
  1569. (kernel_params->version[1] > DM_VERSION_MINOR)) {
  1570. DMERR("ioctl interface mismatch: kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1571. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1572. DM_VERSION_PATCHLEVEL,
  1573. kernel_params->version[0],
  1574. kernel_params->version[1],
  1575. kernel_params->version[2],
  1576. cmd);
  1577. r = -EINVAL;
  1578. }
  1579. /*
  1580. * Fill in the kernel version.
  1581. */
  1582. kernel_params->version[0] = DM_VERSION_MAJOR;
  1583. kernel_params->version[1] = DM_VERSION_MINOR;
  1584. kernel_params->version[2] = DM_VERSION_PATCHLEVEL;
  1585. if (copy_to_user(user->version, kernel_params->version, sizeof(kernel_params->version)))
  1586. return -EFAULT;
  1587. return r;
  1588. }
  1589. #define DM_PARAMS_MALLOC 0x0001 /* Params allocated with kvmalloc() */
  1590. #define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
  1591. static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
  1592. {
  1593. if (param_flags & DM_WIPE_BUFFER)
  1594. memset(param, 0, param_size);
  1595. if (param_flags & DM_PARAMS_MALLOC)
  1596. kvfree(param);
  1597. }
  1598. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
  1599. int ioctl_flags, struct dm_ioctl **param, int *param_flags)
  1600. {
  1601. struct dm_ioctl *dmi;
  1602. int secure_data;
  1603. const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
  1604. /* check_version() already copied version from userspace, avoid TOCTOU */
  1605. if (copy_from_user((char *)param_kernel + sizeof(param_kernel->version),
  1606. (char __user *)user + sizeof(param_kernel->version),
  1607. minimum_data_size - sizeof(param_kernel->version)))
  1608. return -EFAULT;
  1609. if (unlikely(param_kernel->data_size < minimum_data_size) ||
  1610. unlikely(param_kernel->data_size > DM_MAX_TARGETS * DM_MAX_TARGET_PARAMS)) {
  1611. DMERR("Invalid data size in the ioctl structure: %u",
  1612. param_kernel->data_size);
  1613. return -EINVAL;
  1614. }
  1615. secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
  1616. *param_flags = secure_data ? DM_WIPE_BUFFER : 0;
  1617. if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
  1618. dmi = param_kernel;
  1619. dmi->data_size = minimum_data_size;
  1620. goto data_copied;
  1621. }
  1622. /*
  1623. * Use __GFP_HIGH to avoid low memory issues when a device is
  1624. * suspended and the ioctl is needed to resume it.
  1625. * Use kmalloc() rather than vmalloc() when we can.
  1626. */
  1627. dmi = NULL;
  1628. dmi = kvmalloc(param_kernel->data_size, GFP_NOIO | __GFP_HIGH);
  1629. if (!dmi) {
  1630. if (secure_data && clear_user(user, param_kernel->data_size))
  1631. return -EFAULT;
  1632. return -ENOMEM;
  1633. }
  1634. *param_flags |= DM_PARAMS_MALLOC;
  1635. /* Copy from param_kernel (which was already copied from user) */
  1636. memcpy(dmi, param_kernel, minimum_data_size);
  1637. if (copy_from_user(&dmi->data, (char __user *)user + minimum_data_size,
  1638. param_kernel->data_size - minimum_data_size))
  1639. goto bad;
  1640. data_copied:
  1641. /* Wipe the user buffer so we do not return it to userspace */
  1642. if (secure_data && clear_user(user, param_kernel->data_size))
  1643. goto bad;
  1644. *param = dmi;
  1645. return 0;
  1646. bad:
  1647. free_params(dmi, param_kernel->data_size, *param_flags);
  1648. return -EFAULT;
  1649. }
  1650. static int validate_params(uint cmd, struct dm_ioctl *param)
  1651. {
  1652. /* Always clear this flag */
  1653. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1654. param->flags &= ~DM_UEVENT_GENERATED_FLAG;
  1655. param->flags &= ~DM_SECURE_DATA_FLAG;
  1656. param->flags &= ~DM_DATA_OUT_FLAG;
  1657. /* Ignores parameters */
  1658. if (cmd == DM_REMOVE_ALL_CMD ||
  1659. cmd == DM_LIST_DEVICES_CMD ||
  1660. cmd == DM_LIST_VERSIONS_CMD)
  1661. return 0;
  1662. if (cmd == DM_DEV_CREATE_CMD) {
  1663. if (!*param->name) {
  1664. DMERR("name not supplied when creating device");
  1665. return -EINVAL;
  1666. }
  1667. } else if (*param->uuid && *param->name) {
  1668. DMERR("only supply one of name or uuid, cmd(%u)", cmd);
  1669. return -EINVAL;
  1670. }
  1671. /* Ensure strings are terminated */
  1672. param->name[DM_NAME_LEN - 1] = '\0';
  1673. param->uuid[DM_UUID_LEN - 1] = '\0';
  1674. return 0;
  1675. }
  1676. static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *user)
  1677. {
  1678. int r = 0;
  1679. int ioctl_flags;
  1680. int param_flags;
  1681. unsigned int cmd;
  1682. struct dm_ioctl *param;
  1683. ioctl_fn fn = NULL;
  1684. size_t input_param_size;
  1685. struct dm_ioctl param_kernel;
  1686. /* only root can play with this */
  1687. if (!capable(CAP_SYS_ADMIN))
  1688. return -EACCES;
  1689. if (_IOC_TYPE(command) != DM_IOCTL)
  1690. return -ENOTTY;
  1691. cmd = _IOC_NR(command);
  1692. /*
  1693. * Check the interface version passed in. This also
  1694. * writes out the kernel's interface version.
  1695. */
  1696. r = check_version(cmd, user, &param_kernel);
  1697. if (r)
  1698. return r;
  1699. /*
  1700. * Nothing more to do for the version command.
  1701. */
  1702. if (cmd == DM_VERSION_CMD)
  1703. return 0;
  1704. fn = lookup_ioctl(cmd, &ioctl_flags);
  1705. if (!fn) {
  1706. DMERR("dm_ctl_ioctl: unknown command 0x%x", command);
  1707. return -ENOTTY;
  1708. }
  1709. /*
  1710. * Copy the parameters into kernel space.
  1711. */
  1712. r = copy_params(user, &param_kernel, ioctl_flags, &param, &param_flags);
  1713. if (r)
  1714. return r;
  1715. input_param_size = param->data_size;
  1716. r = validate_params(cmd, param);
  1717. if (r)
  1718. goto out;
  1719. param->data_size = offsetof(struct dm_ioctl, data);
  1720. r = fn(file, param, input_param_size);
  1721. if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
  1722. unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
  1723. DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
  1724. if (!r && ioctl_flags & IOCTL_FLAGS_ISSUE_GLOBAL_EVENT)
  1725. dm_issue_global_event();
  1726. /*
  1727. * Copy the results back to userland.
  1728. */
  1729. if (!r && copy_to_user(user, param, param->data_size))
  1730. r = -EFAULT;
  1731. out:
  1732. free_params(param, input_param_size, param_flags);
  1733. return r;
  1734. }
  1735. static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
  1736. {
  1737. return (long)ctl_ioctl(file, command, (struct dm_ioctl __user *)u);
  1738. }
  1739. #ifdef CONFIG_COMPAT
  1740. static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
  1741. {
  1742. return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
  1743. }
  1744. #else
  1745. #define dm_compat_ctl_ioctl NULL
  1746. #endif
  1747. static int dm_open(struct inode *inode, struct file *filp)
  1748. {
  1749. int r;
  1750. struct dm_file *priv;
  1751. r = nonseekable_open(inode, filp);
  1752. if (unlikely(r))
  1753. return r;
  1754. priv = filp->private_data = kmalloc(sizeof(struct dm_file), GFP_KERNEL);
  1755. if (!priv)
  1756. return -ENOMEM;
  1757. priv->global_event_nr = atomic_read(&dm_global_event_nr);
  1758. return 0;
  1759. }
  1760. static int dm_release(struct inode *inode, struct file *filp)
  1761. {
  1762. kfree(filp->private_data);
  1763. return 0;
  1764. }
  1765. static __poll_t dm_poll(struct file *filp, poll_table *wait)
  1766. {
  1767. struct dm_file *priv = filp->private_data;
  1768. __poll_t mask = 0;
  1769. poll_wait(filp, &dm_global_eventq, wait);
  1770. if ((int)(atomic_read(&dm_global_event_nr) - priv->global_event_nr) > 0)
  1771. mask |= EPOLLIN;
  1772. return mask;
  1773. }
  1774. static const struct file_operations _ctl_fops = {
  1775. .open = dm_open,
  1776. .release = dm_release,
  1777. .poll = dm_poll,
  1778. .unlocked_ioctl = dm_ctl_ioctl,
  1779. .compat_ioctl = dm_compat_ctl_ioctl,
  1780. .owner = THIS_MODULE,
  1781. .llseek = noop_llseek,
  1782. };
  1783. static struct miscdevice _dm_misc = {
  1784. .minor = MAPPER_CTRL_MINOR,
  1785. .name = DM_NAME,
  1786. .nodename = DM_DIR "/" DM_CONTROL_NODE,
  1787. .fops = &_ctl_fops
  1788. };
  1789. MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
  1790. MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
  1791. /*
  1792. * Create misc character device and link to DM_DIR/control.
  1793. */
  1794. int __init dm_interface_init(void)
  1795. {
  1796. int r;
  1797. r = misc_register(&_dm_misc);
  1798. if (r) {
  1799. DMERR("misc_register failed for control device");
  1800. return r;
  1801. }
  1802. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1803. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1804. DM_DRIVER_EMAIL);
  1805. return 0;
  1806. }
  1807. void dm_interface_exit(void)
  1808. {
  1809. misc_deregister(&_dm_misc);
  1810. dm_hash_exit();
  1811. }
  1812. /**
  1813. * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
  1814. * @md: Pointer to mapped_device
  1815. * @name: Buffer (size DM_NAME_LEN) for name
  1816. * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
  1817. */
  1818. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
  1819. {
  1820. int r = 0;
  1821. struct hash_cell *hc;
  1822. if (!md)
  1823. return -ENXIO;
  1824. mutex_lock(&dm_hash_cells_mutex);
  1825. hc = dm_get_mdptr(md);
  1826. if (!hc) {
  1827. r = -ENXIO;
  1828. goto out;
  1829. }
  1830. if (name)
  1831. strcpy(name, hc->name);
  1832. if (uuid)
  1833. strcpy(uuid, hc->uuid ? : "");
  1834. out:
  1835. mutex_unlock(&dm_hash_cells_mutex);
  1836. return r;
  1837. }
  1838. EXPORT_SYMBOL_GPL(dm_copy_name_and_uuid);
  1839. /**
  1840. * dm_early_create - create a mapped device in early boot.
  1841. *
  1842. * @dmi: Contains main information of the device mapping to be created.
  1843. * @spec_array: array of pointers to struct dm_target_spec. Describes the
  1844. * mapping table of the device.
  1845. * @target_params_array: array of strings with the parameters to a specific
  1846. * target.
  1847. *
  1848. * Instead of having the struct dm_target_spec and the parameters for every
  1849. * target embedded at the end of struct dm_ioctl (as performed in a normal
  1850. * ioctl), pass them as arguments, so the caller doesn't need to serialize them.
  1851. * The size of the spec_array and target_params_array is given by
  1852. * @dmi->target_count.
  1853. * This function is supposed to be called in early boot, so locking mechanisms
  1854. * to protect against concurrent loads are not required.
  1855. */
  1856. int __init dm_early_create(struct dm_ioctl *dmi,
  1857. struct dm_target_spec **spec_array,
  1858. char **target_params_array)
  1859. {
  1860. int r, m = DM_ANY_MINOR;
  1861. struct dm_table *t, *old_map;
  1862. struct mapped_device *md;
  1863. unsigned int i;
  1864. if (!dmi->target_count)
  1865. return -EINVAL;
  1866. r = check_name(dmi->name);
  1867. if (r)
  1868. return r;
  1869. if (dmi->flags & DM_PERSISTENT_DEV_FLAG)
  1870. m = MINOR(huge_decode_dev(dmi->dev));
  1871. /* alloc dm device */
  1872. r = dm_create(m, &md);
  1873. if (r)
  1874. return r;
  1875. /* hash insert */
  1876. r = dm_hash_insert(dmi->name, *dmi->uuid ? dmi->uuid : NULL, md);
  1877. if (r)
  1878. goto err_destroy_dm;
  1879. /* alloc table */
  1880. r = dm_table_create(&t, get_mode(dmi), dmi->target_count, md);
  1881. if (r)
  1882. goto err_hash_remove;
  1883. /* add targets */
  1884. for (i = 0; i < dmi->target_count; i++) {
  1885. r = dm_table_add_target(t, spec_array[i]->target_type,
  1886. (sector_t) spec_array[i]->sector_start,
  1887. (sector_t) spec_array[i]->length,
  1888. target_params_array[i]);
  1889. if (r) {
  1890. DMERR("error adding target to table");
  1891. goto err_destroy_table;
  1892. }
  1893. }
  1894. /* finish table */
  1895. r = dm_table_complete(t);
  1896. if (r)
  1897. goto err_destroy_table;
  1898. /* setup md->queue to reflect md's type (may block) */
  1899. r = dm_setup_md_queue(md, t);
  1900. if (r) {
  1901. DMERR("unable to set up device queue for new table.");
  1902. goto err_destroy_table;
  1903. }
  1904. /* Set new map */
  1905. dm_suspend(md, 0);
  1906. old_map = dm_swap_table(md, t);
  1907. if (IS_ERR(old_map)) {
  1908. r = PTR_ERR(old_map);
  1909. goto err_destroy_table;
  1910. }
  1911. set_disk_ro(dm_disk(md), !!(dmi->flags & DM_READONLY_FLAG));
  1912. /* resume device */
  1913. r = dm_resume(md);
  1914. if (r)
  1915. goto err_destroy_table;
  1916. DMINFO("%s (%s) is ready", md->disk->disk_name, dmi->name);
  1917. dm_put(md);
  1918. return 0;
  1919. err_destroy_table:
  1920. dm_table_destroy(t);
  1921. err_hash_remove:
  1922. down_write(&_hash_lock);
  1923. (void) __hash_remove(__get_name_cell(dmi->name));
  1924. up_write(&_hash_lock);
  1925. /* release reference from __get_name_cell */
  1926. dm_put(md);
  1927. err_destroy_dm:
  1928. dm_put(md);
  1929. dm_destroy(md);
  1930. return r;
  1931. }