dm-ioctl.c 44 KB

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