dm-ioctl.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  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. /*-----------------------------------------------------------------
  22. * The ioctl interface needs to be able to look up devices by
  23. * name or uuid.
  24. *---------------------------------------------------------------*/
  25. struct hash_cell {
  26. struct list_head name_list;
  27. struct list_head uuid_list;
  28. char *name;
  29. char *uuid;
  30. struct mapped_device *md;
  31. struct dm_table *new_map;
  32. };
  33. /*
  34. * A dummy definition to make RCU happy.
  35. * struct dm_table should never be dereferenced in this file.
  36. */
  37. struct dm_table {
  38. int undefined__;
  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 dm_ioctl *param, size_t param_size);
  387. static int remove_all(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 void *align_ptr(void *ptr)
  398. {
  399. return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
  400. }
  401. /*
  402. * Retrieves the data payload buffer from an already allocated
  403. * struct dm_ioctl.
  404. */
  405. static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
  406. size_t *len)
  407. {
  408. param->data_start = align_ptr(param + 1) - (void *) param;
  409. if (param->data_start < param_size)
  410. *len = param_size - param->data_start;
  411. else
  412. *len = 0;
  413. return ((void *) param) + param->data_start;
  414. }
  415. static int list_devices(struct dm_ioctl *param, size_t param_size)
  416. {
  417. unsigned int i;
  418. struct hash_cell *hc;
  419. size_t len, needed = 0;
  420. struct gendisk *disk;
  421. struct dm_name_list *nl, *old_nl = NULL;
  422. down_write(&_hash_lock);
  423. /*
  424. * Loop through all the devices working out how much
  425. * space we need.
  426. */
  427. for (i = 0; i < NUM_BUCKETS; i++) {
  428. list_for_each_entry (hc, _name_buckets + i, name_list) {
  429. needed += sizeof(struct dm_name_list);
  430. needed += strlen(hc->name) + 1;
  431. needed += ALIGN_MASK;
  432. }
  433. }
  434. /*
  435. * Grab our output buffer.
  436. */
  437. nl = get_result_buffer(param, param_size, &len);
  438. if (len < needed) {
  439. param->flags |= DM_BUFFER_FULL_FLAG;
  440. goto out;
  441. }
  442. param->data_size = param->data_start + needed;
  443. nl->dev = 0; /* Flags no data */
  444. /*
  445. * Now loop through filling out the names.
  446. */
  447. for (i = 0; i < NUM_BUCKETS; i++) {
  448. list_for_each_entry (hc, _name_buckets + i, name_list) {
  449. if (old_nl)
  450. old_nl->next = (uint32_t) ((void *) nl -
  451. (void *) old_nl);
  452. disk = dm_disk(hc->md);
  453. nl->dev = huge_encode_dev(disk_devt(disk));
  454. nl->next = 0;
  455. strcpy(nl->name, hc->name);
  456. old_nl = nl;
  457. nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
  458. }
  459. }
  460. out:
  461. up_write(&_hash_lock);
  462. return 0;
  463. }
  464. static void list_version_get_needed(struct target_type *tt, void *needed_param)
  465. {
  466. size_t *needed = needed_param;
  467. *needed += sizeof(struct dm_target_versions);
  468. *needed += strlen(tt->name);
  469. *needed += ALIGN_MASK;
  470. }
  471. static void list_version_get_info(struct target_type *tt, void *param)
  472. {
  473. struct vers_iter *info = param;
  474. /* Check space - it might have changed since the first iteration */
  475. if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
  476. info->end) {
  477. info->flags = DM_BUFFER_FULL_FLAG;
  478. return;
  479. }
  480. if (info->old_vers)
  481. info->old_vers->next = (uint32_t) ((void *)info->vers -
  482. (void *)info->old_vers);
  483. info->vers->version[0] = tt->version[0];
  484. info->vers->version[1] = tt->version[1];
  485. info->vers->version[2] = tt->version[2];
  486. info->vers->next = 0;
  487. strcpy(info->vers->name, tt->name);
  488. info->old_vers = info->vers;
  489. info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
  490. }
  491. static int list_versions(struct dm_ioctl *param, size_t param_size)
  492. {
  493. size_t len, needed = 0;
  494. struct dm_target_versions *vers;
  495. struct vers_iter iter_info;
  496. /*
  497. * Loop through all the devices working out how much
  498. * space we need.
  499. */
  500. dm_target_iterate(list_version_get_needed, &needed);
  501. /*
  502. * Grab our output buffer.
  503. */
  504. vers = get_result_buffer(param, param_size, &len);
  505. if (len < needed) {
  506. param->flags |= DM_BUFFER_FULL_FLAG;
  507. goto out;
  508. }
  509. param->data_size = param->data_start + needed;
  510. iter_info.param_size = param_size;
  511. iter_info.old_vers = NULL;
  512. iter_info.vers = vers;
  513. iter_info.flags = 0;
  514. iter_info.end = (char *)vers+len;
  515. /*
  516. * Now loop through filling out the names & versions.
  517. */
  518. dm_target_iterate(list_version_get_info, &iter_info);
  519. param->flags |= iter_info.flags;
  520. out:
  521. return 0;
  522. }
  523. static int check_name(const char *name)
  524. {
  525. if (strchr(name, '/')) {
  526. DMWARN("invalid device name");
  527. return -EINVAL;
  528. }
  529. return 0;
  530. }
  531. /*
  532. * On successful return, the caller must not attempt to acquire
  533. * _hash_lock without first calling dm_put_live_table, because dm_table_destroy
  534. * waits for this dm_put_live_table and could be called under this lock.
  535. */
  536. static struct dm_table *dm_get_inactive_table(struct mapped_device *md, int *srcu_idx)
  537. {
  538. struct hash_cell *hc;
  539. struct dm_table *table = NULL;
  540. /* increment rcu count, we don't care about the table pointer */
  541. dm_get_live_table(md, srcu_idx);
  542. down_read(&_hash_lock);
  543. hc = dm_get_mdptr(md);
  544. if (!hc || hc->md != md) {
  545. DMWARN("device has been removed from the dev hash table.");
  546. goto out;
  547. }
  548. table = hc->new_map;
  549. out:
  550. up_read(&_hash_lock);
  551. return table;
  552. }
  553. static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
  554. struct dm_ioctl *param,
  555. int *srcu_idx)
  556. {
  557. return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
  558. dm_get_inactive_table(md, srcu_idx) : dm_get_live_table(md, srcu_idx);
  559. }
  560. /*
  561. * Fills in a dm_ioctl structure, ready for sending back to
  562. * userland.
  563. */
  564. static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
  565. {
  566. struct gendisk *disk = dm_disk(md);
  567. struct dm_table *table;
  568. int srcu_idx;
  569. param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
  570. DM_ACTIVE_PRESENT_FLAG | DM_INTERNAL_SUSPEND_FLAG);
  571. if (dm_suspended_md(md))
  572. param->flags |= DM_SUSPEND_FLAG;
  573. if (dm_suspended_internally_md(md))
  574. param->flags |= DM_INTERNAL_SUSPEND_FLAG;
  575. if (dm_test_deferred_remove_flag(md))
  576. param->flags |= DM_DEFERRED_REMOVE;
  577. param->dev = huge_encode_dev(disk_devt(disk));
  578. /*
  579. * Yes, this will be out of date by the time it gets back
  580. * to userland, but it is still very useful for
  581. * debugging.
  582. */
  583. param->open_count = dm_open_count(md);
  584. param->event_nr = dm_get_event_nr(md);
  585. param->target_count = 0;
  586. table = dm_get_live_table(md, &srcu_idx);
  587. if (table) {
  588. if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
  589. if (get_disk_ro(disk))
  590. param->flags |= DM_READONLY_FLAG;
  591. param->target_count = dm_table_get_num_targets(table);
  592. }
  593. param->flags |= DM_ACTIVE_PRESENT_FLAG;
  594. }
  595. dm_put_live_table(md, srcu_idx);
  596. if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
  597. int srcu_idx;
  598. table = dm_get_inactive_table(md, &srcu_idx);
  599. if (table) {
  600. if (!(dm_table_get_mode(table) & FMODE_WRITE))
  601. param->flags |= DM_READONLY_FLAG;
  602. param->target_count = dm_table_get_num_targets(table);
  603. }
  604. dm_put_live_table(md, srcu_idx);
  605. }
  606. }
  607. static int dev_create(struct dm_ioctl *param, size_t param_size)
  608. {
  609. int r, m = DM_ANY_MINOR;
  610. struct mapped_device *md;
  611. r = check_name(param->name);
  612. if (r)
  613. return r;
  614. if (param->flags & DM_PERSISTENT_DEV_FLAG)
  615. m = MINOR(huge_decode_dev(param->dev));
  616. r = dm_create(m, &md);
  617. if (r)
  618. return r;
  619. r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
  620. if (r) {
  621. dm_put(md);
  622. dm_destroy(md);
  623. return r;
  624. }
  625. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  626. __dev_status(md, param);
  627. dm_put(md);
  628. return 0;
  629. }
  630. /*
  631. * Always use UUID for lookups if it's present, otherwise use name or dev.
  632. */
  633. static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
  634. {
  635. struct hash_cell *hc = NULL;
  636. if (*param->uuid) {
  637. if (*param->name || param->dev)
  638. return NULL;
  639. hc = __get_uuid_cell(param->uuid);
  640. if (!hc)
  641. return NULL;
  642. } else if (*param->name) {
  643. if (param->dev)
  644. return NULL;
  645. hc = __get_name_cell(param->name);
  646. if (!hc)
  647. return NULL;
  648. } else if (param->dev) {
  649. hc = __get_dev_cell(param->dev);
  650. if (!hc)
  651. return NULL;
  652. } else
  653. return NULL;
  654. /*
  655. * Sneakily write in both the name and the uuid
  656. * while we have the cell.
  657. */
  658. strlcpy(param->name, hc->name, sizeof(param->name));
  659. if (hc->uuid)
  660. strlcpy(param->uuid, hc->uuid, sizeof(param->uuid));
  661. else
  662. param->uuid[0] = '\0';
  663. if (hc->new_map)
  664. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  665. else
  666. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  667. return hc;
  668. }
  669. static struct mapped_device *find_device(struct dm_ioctl *param)
  670. {
  671. struct hash_cell *hc;
  672. struct mapped_device *md = NULL;
  673. down_read(&_hash_lock);
  674. hc = __find_device_hash_cell(param);
  675. if (hc)
  676. md = hc->md;
  677. up_read(&_hash_lock);
  678. return md;
  679. }
  680. static int dev_remove(struct dm_ioctl *param, size_t param_size)
  681. {
  682. struct hash_cell *hc;
  683. struct mapped_device *md;
  684. int r;
  685. struct dm_table *t;
  686. down_write(&_hash_lock);
  687. hc = __find_device_hash_cell(param);
  688. if (!hc) {
  689. DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
  690. up_write(&_hash_lock);
  691. return -ENXIO;
  692. }
  693. md = hc->md;
  694. /*
  695. * Ensure the device is not open and nothing further can open it.
  696. */
  697. r = dm_lock_for_deletion(md, !!(param->flags & DM_DEFERRED_REMOVE), false);
  698. if (r) {
  699. if (r == -EBUSY && param->flags & DM_DEFERRED_REMOVE) {
  700. up_write(&_hash_lock);
  701. dm_put(md);
  702. return 0;
  703. }
  704. DMDEBUG_LIMIT("unable to remove open device %s", hc->name);
  705. up_write(&_hash_lock);
  706. dm_put(md);
  707. return r;
  708. }
  709. t = __hash_remove(hc);
  710. up_write(&_hash_lock);
  711. if (t) {
  712. dm_sync_table(md);
  713. dm_table_destroy(t);
  714. }
  715. param->flags &= ~DM_DEFERRED_REMOVE;
  716. if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr))
  717. param->flags |= DM_UEVENT_GENERATED_FLAG;
  718. dm_put(md);
  719. dm_destroy(md);
  720. return 0;
  721. }
  722. /*
  723. * Check a string doesn't overrun the chunk of
  724. * memory we copied from userland.
  725. */
  726. static int invalid_str(char *str, void *end)
  727. {
  728. while ((void *) str < end)
  729. if (!*str++)
  730. return 0;
  731. return -EINVAL;
  732. }
  733. static int dev_rename(struct dm_ioctl *param, size_t param_size)
  734. {
  735. int r;
  736. char *new_data = (char *) param + param->data_start;
  737. struct mapped_device *md;
  738. unsigned change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
  739. if (new_data < param->data ||
  740. invalid_str(new_data, (void *) param + param_size) || !*new_data ||
  741. strlen(new_data) > (change_uuid ? DM_UUID_LEN - 1 : DM_NAME_LEN - 1)) {
  742. DMWARN("Invalid new mapped device name or uuid string supplied.");
  743. return -EINVAL;
  744. }
  745. if (!change_uuid) {
  746. r = check_name(new_data);
  747. if (r)
  748. return r;
  749. }
  750. md = dm_hash_rename(param, new_data);
  751. if (IS_ERR(md))
  752. return PTR_ERR(md);
  753. __dev_status(md, param);
  754. dm_put(md);
  755. return 0;
  756. }
  757. static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
  758. {
  759. int r = -EINVAL, x;
  760. struct mapped_device *md;
  761. struct hd_geometry geometry;
  762. unsigned long indata[4];
  763. char *geostr = (char *) param + param->data_start;
  764. char dummy;
  765. md = find_device(param);
  766. if (!md)
  767. return -ENXIO;
  768. if (geostr < param->data ||
  769. invalid_str(geostr, (void *) param + param_size)) {
  770. DMWARN("Invalid geometry supplied.");
  771. goto out;
  772. }
  773. x = sscanf(geostr, "%lu %lu %lu %lu%c", indata,
  774. indata + 1, indata + 2, indata + 3, &dummy);
  775. if (x != 4) {
  776. DMWARN("Unable to interpret geometry settings.");
  777. goto out;
  778. }
  779. if (indata[0] > 65535 || indata[1] > 255 ||
  780. indata[2] > 255 || indata[3] > ULONG_MAX) {
  781. DMWARN("Geometry exceeds range limits.");
  782. goto out;
  783. }
  784. geometry.cylinders = indata[0];
  785. geometry.heads = indata[1];
  786. geometry.sectors = indata[2];
  787. geometry.start = indata[3];
  788. r = dm_set_geometry(md, &geometry);
  789. param->data_size = 0;
  790. out:
  791. dm_put(md);
  792. return r;
  793. }
  794. static int do_suspend(struct dm_ioctl *param)
  795. {
  796. int r = 0;
  797. unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  798. struct mapped_device *md;
  799. md = find_device(param);
  800. if (!md)
  801. return -ENXIO;
  802. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  803. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  804. if (param->flags & DM_NOFLUSH_FLAG)
  805. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  806. if (!dm_suspended_md(md)) {
  807. r = dm_suspend(md, suspend_flags);
  808. if (r)
  809. goto out;
  810. }
  811. __dev_status(md, param);
  812. out:
  813. dm_put(md);
  814. return r;
  815. }
  816. static int do_resume(struct dm_ioctl *param)
  817. {
  818. int r = 0;
  819. unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  820. struct hash_cell *hc;
  821. struct mapped_device *md;
  822. struct dm_table *new_map, *old_map = NULL;
  823. down_write(&_hash_lock);
  824. hc = __find_device_hash_cell(param);
  825. if (!hc) {
  826. DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
  827. up_write(&_hash_lock);
  828. return -ENXIO;
  829. }
  830. md = hc->md;
  831. new_map = hc->new_map;
  832. hc->new_map = NULL;
  833. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  834. up_write(&_hash_lock);
  835. /* Do we need to load a new map ? */
  836. if (new_map) {
  837. /* Suspend if it isn't already suspended */
  838. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  839. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  840. if (param->flags & DM_NOFLUSH_FLAG)
  841. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  842. if (!dm_suspended_md(md))
  843. dm_suspend(md, suspend_flags);
  844. old_map = dm_swap_table(md, new_map);
  845. if (IS_ERR(old_map)) {
  846. dm_sync_table(md);
  847. dm_table_destroy(new_map);
  848. dm_put(md);
  849. return PTR_ERR(old_map);
  850. }
  851. if (dm_table_get_mode(new_map) & FMODE_WRITE)
  852. set_disk_ro(dm_disk(md), 0);
  853. else
  854. set_disk_ro(dm_disk(md), 1);
  855. }
  856. if (dm_suspended_md(md)) {
  857. r = dm_resume(md);
  858. if (!r && !dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr))
  859. param->flags |= DM_UEVENT_GENERATED_FLAG;
  860. }
  861. /*
  862. * Since dm_swap_table synchronizes RCU, nobody should be in
  863. * read-side critical section already.
  864. */
  865. if (old_map)
  866. dm_table_destroy(old_map);
  867. if (!r)
  868. __dev_status(md, param);
  869. dm_put(md);
  870. return r;
  871. }
  872. /*
  873. * Set or unset the suspension state of a device.
  874. * If the device already is in the requested state we just return its status.
  875. */
  876. static int dev_suspend(struct dm_ioctl *param, size_t param_size)
  877. {
  878. if (param->flags & DM_SUSPEND_FLAG)
  879. return do_suspend(param);
  880. return do_resume(param);
  881. }
  882. /*
  883. * Copies device info back to user space, used by
  884. * the create and info ioctls.
  885. */
  886. static int dev_status(struct dm_ioctl *param, size_t param_size)
  887. {
  888. struct mapped_device *md;
  889. md = find_device(param);
  890. if (!md)
  891. return -ENXIO;
  892. __dev_status(md, param);
  893. dm_put(md);
  894. return 0;
  895. }
  896. /*
  897. * Build up the status struct for each target
  898. */
  899. static void retrieve_status(struct dm_table *table,
  900. struct dm_ioctl *param, size_t param_size)
  901. {
  902. unsigned int i, num_targets;
  903. struct dm_target_spec *spec;
  904. char *outbuf, *outptr;
  905. status_type_t type;
  906. size_t remaining, len, used = 0;
  907. unsigned status_flags = 0;
  908. outptr = outbuf = get_result_buffer(param, param_size, &len);
  909. if (param->flags & DM_STATUS_TABLE_FLAG)
  910. type = STATUSTYPE_TABLE;
  911. else
  912. type = STATUSTYPE_INFO;
  913. /* Get all the target info */
  914. num_targets = dm_table_get_num_targets(table);
  915. for (i = 0; i < num_targets; i++) {
  916. struct dm_target *ti = dm_table_get_target(table, i);
  917. size_t l;
  918. remaining = len - (outptr - outbuf);
  919. if (remaining <= sizeof(struct dm_target_spec)) {
  920. param->flags |= DM_BUFFER_FULL_FLAG;
  921. break;
  922. }
  923. spec = (struct dm_target_spec *) outptr;
  924. spec->status = 0;
  925. spec->sector_start = ti->begin;
  926. spec->length = ti->len;
  927. strncpy(spec->target_type, ti->type->name,
  928. sizeof(spec->target_type));
  929. outptr += sizeof(struct dm_target_spec);
  930. remaining = len - (outptr - outbuf);
  931. if (remaining <= 0) {
  932. param->flags |= DM_BUFFER_FULL_FLAG;
  933. break;
  934. }
  935. /* Get the status/table string from the target driver */
  936. if (ti->type->status) {
  937. if (param->flags & DM_NOFLUSH_FLAG)
  938. status_flags |= DM_STATUS_NOFLUSH_FLAG;
  939. ti->type->status(ti, type, status_flags, outptr, remaining);
  940. } else
  941. outptr[0] = '\0';
  942. l = strlen(outptr) + 1;
  943. if (l == remaining) {
  944. param->flags |= DM_BUFFER_FULL_FLAG;
  945. break;
  946. }
  947. outptr += l;
  948. used = param->data_start + (outptr - outbuf);
  949. outptr = align_ptr(outptr);
  950. spec->next = outptr - outbuf;
  951. }
  952. if (used)
  953. param->data_size = used;
  954. param->target_count = num_targets;
  955. }
  956. /*
  957. * Wait for a device to report an event
  958. */
  959. static int dev_wait(struct dm_ioctl *param, size_t param_size)
  960. {
  961. int r = 0;
  962. struct mapped_device *md;
  963. struct dm_table *table;
  964. int srcu_idx;
  965. md = find_device(param);
  966. if (!md)
  967. return -ENXIO;
  968. /*
  969. * Wait for a notification event
  970. */
  971. if (dm_wait_event(md, param->event_nr)) {
  972. r = -ERESTARTSYS;
  973. goto out;
  974. }
  975. /*
  976. * The userland program is going to want to know what
  977. * changed to trigger the event, so we may as well tell
  978. * him and save an ioctl.
  979. */
  980. __dev_status(md, param);
  981. table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
  982. if (table)
  983. retrieve_status(table, param, param_size);
  984. dm_put_live_table(md, srcu_idx);
  985. out:
  986. dm_put(md);
  987. return r;
  988. }
  989. static inline fmode_t get_mode(struct dm_ioctl *param)
  990. {
  991. fmode_t mode = FMODE_READ | FMODE_WRITE;
  992. if (param->flags & DM_READONLY_FLAG)
  993. mode = FMODE_READ;
  994. return mode;
  995. }
  996. static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
  997. struct dm_target_spec **spec, char **target_params)
  998. {
  999. *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
  1000. *target_params = (char *) (*spec + 1);
  1001. if (*spec < (last + 1))
  1002. return -EINVAL;
  1003. return invalid_str(*target_params, end);
  1004. }
  1005. static int populate_table(struct dm_table *table,
  1006. struct dm_ioctl *param, size_t param_size)
  1007. {
  1008. int r;
  1009. unsigned int i = 0;
  1010. struct dm_target_spec *spec = (struct dm_target_spec *) param;
  1011. uint32_t next = param->data_start;
  1012. void *end = (void *) param + param_size;
  1013. char *target_params;
  1014. if (!param->target_count) {
  1015. DMWARN("populate_table: no targets specified");
  1016. return -EINVAL;
  1017. }
  1018. for (i = 0; i < param->target_count; i++) {
  1019. r = next_target(spec, next, end, &spec, &target_params);
  1020. if (r) {
  1021. DMWARN("unable to find target");
  1022. return r;
  1023. }
  1024. r = dm_table_add_target(table, spec->target_type,
  1025. (sector_t) spec->sector_start,
  1026. (sector_t) spec->length,
  1027. target_params);
  1028. if (r) {
  1029. DMWARN("error adding target to table");
  1030. return r;
  1031. }
  1032. next = spec->next;
  1033. }
  1034. return dm_table_complete(table);
  1035. }
  1036. static bool is_valid_type(unsigned cur, unsigned new)
  1037. {
  1038. if (cur == new ||
  1039. (cur == DM_TYPE_BIO_BASED && new == DM_TYPE_DAX_BIO_BASED))
  1040. return true;
  1041. return false;
  1042. }
  1043. static int table_load(struct dm_ioctl *param, size_t param_size)
  1044. {
  1045. int r;
  1046. struct hash_cell *hc;
  1047. struct dm_table *t, *old_map = NULL;
  1048. struct mapped_device *md;
  1049. struct target_type *immutable_target_type;
  1050. md = find_device(param);
  1051. if (!md)
  1052. return -ENXIO;
  1053. r = dm_table_create(&t, get_mode(param), param->target_count, md);
  1054. if (r)
  1055. goto err;
  1056. /* Protect md->type and md->queue against concurrent table loads. */
  1057. dm_lock_md_type(md);
  1058. r = populate_table(t, param, param_size);
  1059. if (r)
  1060. goto err_unlock_md_type;
  1061. immutable_target_type = dm_get_immutable_target_type(md);
  1062. if (immutable_target_type &&
  1063. (immutable_target_type != dm_table_get_immutable_target_type(t)) &&
  1064. !dm_table_get_wildcard_target(t)) {
  1065. DMWARN("can't replace immutable target type %s",
  1066. immutable_target_type->name);
  1067. r = -EINVAL;
  1068. goto err_unlock_md_type;
  1069. }
  1070. if (dm_get_md_type(md) == DM_TYPE_NONE) {
  1071. /* Initial table load: acquire type of table. */
  1072. dm_set_md_type(md, dm_table_get_type(t));
  1073. /* setup md->queue to reflect md's type (may block) */
  1074. r = dm_setup_md_queue(md, t);
  1075. if (r) {
  1076. DMWARN("unable to set up device queue for new table.");
  1077. goto err_unlock_md_type;
  1078. }
  1079. } else if (!is_valid_type(dm_get_md_type(md), dm_table_get_type(t))) {
  1080. DMWARN("can't change device type after initial table load.");
  1081. r = -EINVAL;
  1082. goto err_unlock_md_type;
  1083. }
  1084. dm_unlock_md_type(md);
  1085. /* stage inactive table */
  1086. down_write(&_hash_lock);
  1087. hc = dm_get_mdptr(md);
  1088. if (!hc || hc->md != md) {
  1089. DMWARN("device has been removed from the dev hash table.");
  1090. up_write(&_hash_lock);
  1091. r = -ENXIO;
  1092. goto err_destroy_table;
  1093. }
  1094. if (hc->new_map)
  1095. old_map = hc->new_map;
  1096. hc->new_map = t;
  1097. up_write(&_hash_lock);
  1098. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  1099. __dev_status(md, param);
  1100. if (old_map) {
  1101. dm_sync_table(md);
  1102. dm_table_destroy(old_map);
  1103. }
  1104. dm_put(md);
  1105. return 0;
  1106. err_unlock_md_type:
  1107. dm_unlock_md_type(md);
  1108. err_destroy_table:
  1109. dm_table_destroy(t);
  1110. err:
  1111. dm_put(md);
  1112. return r;
  1113. }
  1114. static int table_clear(struct dm_ioctl *param, size_t param_size)
  1115. {
  1116. struct hash_cell *hc;
  1117. struct mapped_device *md;
  1118. struct dm_table *old_map = NULL;
  1119. down_write(&_hash_lock);
  1120. hc = __find_device_hash_cell(param);
  1121. if (!hc) {
  1122. DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
  1123. up_write(&_hash_lock);
  1124. return -ENXIO;
  1125. }
  1126. if (hc->new_map) {
  1127. old_map = hc->new_map;
  1128. hc->new_map = NULL;
  1129. }
  1130. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  1131. __dev_status(hc->md, param);
  1132. md = hc->md;
  1133. up_write(&_hash_lock);
  1134. if (old_map) {
  1135. dm_sync_table(md);
  1136. dm_table_destroy(old_map);
  1137. }
  1138. dm_put(md);
  1139. return 0;
  1140. }
  1141. /*
  1142. * Retrieves a list of devices used by a particular dm device.
  1143. */
  1144. static void retrieve_deps(struct dm_table *table,
  1145. struct dm_ioctl *param, size_t param_size)
  1146. {
  1147. unsigned int count = 0;
  1148. struct list_head *tmp;
  1149. size_t len, needed;
  1150. struct dm_dev_internal *dd;
  1151. struct dm_target_deps *deps;
  1152. deps = get_result_buffer(param, param_size, &len);
  1153. /*
  1154. * Count the devices.
  1155. */
  1156. list_for_each (tmp, dm_table_get_devices(table))
  1157. count++;
  1158. /*
  1159. * Check we have enough space.
  1160. */
  1161. needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
  1162. if (len < needed) {
  1163. param->flags |= DM_BUFFER_FULL_FLAG;
  1164. return;
  1165. }
  1166. /*
  1167. * Fill in the devices.
  1168. */
  1169. deps->count = count;
  1170. count = 0;
  1171. list_for_each_entry (dd, dm_table_get_devices(table), list)
  1172. deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
  1173. param->data_size = param->data_start + needed;
  1174. }
  1175. static int table_deps(struct dm_ioctl *param, size_t param_size)
  1176. {
  1177. struct mapped_device *md;
  1178. struct dm_table *table;
  1179. int srcu_idx;
  1180. md = find_device(param);
  1181. if (!md)
  1182. return -ENXIO;
  1183. __dev_status(md, param);
  1184. table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
  1185. if (table)
  1186. retrieve_deps(table, param, param_size);
  1187. dm_put_live_table(md, srcu_idx);
  1188. dm_put(md);
  1189. return 0;
  1190. }
  1191. /*
  1192. * Return the status of a device as a text string for each
  1193. * target.
  1194. */
  1195. static int table_status(struct dm_ioctl *param, size_t param_size)
  1196. {
  1197. struct mapped_device *md;
  1198. struct dm_table *table;
  1199. int srcu_idx;
  1200. md = find_device(param);
  1201. if (!md)
  1202. return -ENXIO;
  1203. __dev_status(md, param);
  1204. table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
  1205. if (table)
  1206. retrieve_status(table, param, param_size);
  1207. dm_put_live_table(md, srcu_idx);
  1208. dm_put(md);
  1209. return 0;
  1210. }
  1211. /*
  1212. * Process device-mapper dependent messages. Messages prefixed with '@'
  1213. * are processed by the DM core. All others are delivered to the target.
  1214. * Returns a number <= 1 if message was processed by device mapper.
  1215. * Returns 2 if message should be delivered to the target.
  1216. */
  1217. static int message_for_md(struct mapped_device *md, unsigned argc, char **argv,
  1218. char *result, unsigned maxlen)
  1219. {
  1220. int r;
  1221. if (**argv != '@')
  1222. return 2; /* no '@' prefix, deliver to target */
  1223. if (!strcasecmp(argv[0], "@cancel_deferred_remove")) {
  1224. if (argc != 1) {
  1225. DMERR("Invalid arguments for @cancel_deferred_remove");
  1226. return -EINVAL;
  1227. }
  1228. return dm_cancel_deferred_remove(md);
  1229. }
  1230. r = dm_stats_message(md, argc, argv, result, maxlen);
  1231. if (r < 2)
  1232. return r;
  1233. DMERR("Unsupported message sent to DM core: %s", argv[0]);
  1234. return -EINVAL;
  1235. }
  1236. /*
  1237. * Pass a message to the target that's at the supplied device offset.
  1238. */
  1239. static int target_message(struct dm_ioctl *param, size_t param_size)
  1240. {
  1241. int r, argc;
  1242. char **argv;
  1243. struct mapped_device *md;
  1244. struct dm_table *table;
  1245. struct dm_target *ti;
  1246. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  1247. size_t maxlen;
  1248. char *result = get_result_buffer(param, param_size, &maxlen);
  1249. int srcu_idx;
  1250. md = find_device(param);
  1251. if (!md)
  1252. return -ENXIO;
  1253. if (tmsg < (struct dm_target_msg *) param->data ||
  1254. invalid_str(tmsg->message, (void *) param + param_size)) {
  1255. DMWARN("Invalid target message parameters.");
  1256. r = -EINVAL;
  1257. goto out;
  1258. }
  1259. r = dm_split_args(&argc, &argv, tmsg->message);
  1260. if (r) {
  1261. DMWARN("Failed to split target message parameters");
  1262. goto out;
  1263. }
  1264. if (!argc) {
  1265. DMWARN("Empty message received.");
  1266. goto out_argv;
  1267. }
  1268. r = message_for_md(md, argc, argv, result, maxlen);
  1269. if (r <= 1)
  1270. goto out_argv;
  1271. table = dm_get_live_table(md, &srcu_idx);
  1272. if (!table)
  1273. goto out_table;
  1274. if (dm_deleting_md(md)) {
  1275. r = -ENXIO;
  1276. goto out_table;
  1277. }
  1278. ti = dm_table_find_target(table, tmsg->sector);
  1279. if (!dm_target_is_valid(ti)) {
  1280. DMWARN("Target message sector outside device.");
  1281. r = -EINVAL;
  1282. } else if (ti->type->message)
  1283. r = ti->type->message(ti, argc, argv);
  1284. else {
  1285. DMWARN("Target type does not support messages");
  1286. r = -EINVAL;
  1287. }
  1288. out_table:
  1289. dm_put_live_table(md, srcu_idx);
  1290. out_argv:
  1291. kfree(argv);
  1292. out:
  1293. if (r >= 0)
  1294. __dev_status(md, param);
  1295. if (r == 1) {
  1296. param->flags |= DM_DATA_OUT_FLAG;
  1297. if (dm_message_test_buffer_overflow(result, maxlen))
  1298. param->flags |= DM_BUFFER_FULL_FLAG;
  1299. else
  1300. param->data_size = param->data_start + strlen(result) + 1;
  1301. r = 0;
  1302. }
  1303. dm_put(md);
  1304. return r;
  1305. }
  1306. /*
  1307. * The ioctl parameter block consists of two parts, a dm_ioctl struct
  1308. * followed by a data buffer. This flag is set if the second part,
  1309. * which has a variable size, is not used by the function processing
  1310. * the ioctl.
  1311. */
  1312. #define IOCTL_FLAGS_NO_PARAMS 1
  1313. /*-----------------------------------------------------------------
  1314. * Implementation of open/close/ioctl on the special char
  1315. * device.
  1316. *---------------------------------------------------------------*/
  1317. static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
  1318. {
  1319. static struct {
  1320. int cmd;
  1321. int flags;
  1322. ioctl_fn fn;
  1323. } _ioctls[] = {
  1324. {DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
  1325. {DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS, remove_all},
  1326. {DM_LIST_DEVICES_CMD, 0, list_devices},
  1327. {DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS, dev_create},
  1328. {DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS, dev_remove},
  1329. {DM_DEV_RENAME_CMD, 0, dev_rename},
  1330. {DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
  1331. {DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
  1332. {DM_DEV_WAIT_CMD, 0, dev_wait},
  1333. {DM_TABLE_LOAD_CMD, 0, table_load},
  1334. {DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
  1335. {DM_TABLE_DEPS_CMD, 0, table_deps},
  1336. {DM_TABLE_STATUS_CMD, 0, table_status},
  1337. {DM_LIST_VERSIONS_CMD, 0, list_versions},
  1338. {DM_TARGET_MSG_CMD, 0, target_message},
  1339. {DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry}
  1340. };
  1341. if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
  1342. return NULL;
  1343. *ioctl_flags = _ioctls[cmd].flags;
  1344. return _ioctls[cmd].fn;
  1345. }
  1346. /*
  1347. * As well as checking the version compatibility this always
  1348. * copies the kernel interface version out.
  1349. */
  1350. static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
  1351. {
  1352. uint32_t version[3];
  1353. int r = 0;
  1354. if (copy_from_user(version, user->version, sizeof(version)))
  1355. return -EFAULT;
  1356. if ((DM_VERSION_MAJOR != version[0]) ||
  1357. (DM_VERSION_MINOR < version[1])) {
  1358. DMWARN("ioctl interface mismatch: "
  1359. "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1360. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1361. DM_VERSION_PATCHLEVEL,
  1362. version[0], version[1], version[2], cmd);
  1363. r = -EINVAL;
  1364. }
  1365. /*
  1366. * Fill in the kernel version.
  1367. */
  1368. version[0] = DM_VERSION_MAJOR;
  1369. version[1] = DM_VERSION_MINOR;
  1370. version[2] = DM_VERSION_PATCHLEVEL;
  1371. if (copy_to_user(user->version, version, sizeof(version)))
  1372. return -EFAULT;
  1373. return r;
  1374. }
  1375. #define DM_PARAMS_MALLOC 0x0001 /* Params allocated with kvmalloc() */
  1376. #define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
  1377. static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
  1378. {
  1379. if (param_flags & DM_WIPE_BUFFER)
  1380. memset(param, 0, param_size);
  1381. if (param_flags & DM_PARAMS_MALLOC)
  1382. kvfree(param);
  1383. }
  1384. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
  1385. int ioctl_flags,
  1386. struct dm_ioctl **param, int *param_flags)
  1387. {
  1388. struct dm_ioctl *dmi;
  1389. int secure_data;
  1390. const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
  1391. if (copy_from_user(param_kernel, user, minimum_data_size))
  1392. return -EFAULT;
  1393. if (param_kernel->data_size < minimum_data_size)
  1394. return -EINVAL;
  1395. secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
  1396. *param_flags = secure_data ? DM_WIPE_BUFFER : 0;
  1397. if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
  1398. dmi = param_kernel;
  1399. dmi->data_size = minimum_data_size;
  1400. goto data_copied;
  1401. }
  1402. /*
  1403. * Try to avoid low memory issues when a device is suspended.
  1404. * Use kmalloc() rather than vmalloc() when we can.
  1405. */
  1406. dmi = NULL;
  1407. if (param_kernel->data_size <= KMALLOC_MAX_SIZE)
  1408. dmi = kmalloc(param_kernel->data_size, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  1409. if (!dmi) {
  1410. unsigned noio_flag;
  1411. noio_flag = memalloc_noio_save();
  1412. dmi = __vmalloc(param_kernel->data_size, GFP_NOIO | __GFP_HIGH | __GFP_HIGHMEM, PAGE_KERNEL);
  1413. memalloc_noio_restore(noio_flag);
  1414. }
  1415. if (!dmi) {
  1416. if (secure_data && clear_user(user, param_kernel->data_size))
  1417. return -EFAULT;
  1418. return -ENOMEM;
  1419. }
  1420. *param_flags |= DM_PARAMS_MALLOC;
  1421. if (copy_from_user(dmi, user, param_kernel->data_size))
  1422. goto bad;
  1423. data_copied:
  1424. /*
  1425. * Abort if something changed the ioctl data while it was being copied.
  1426. */
  1427. if (dmi->data_size != param_kernel->data_size) {
  1428. DMERR("rejecting ioctl: data size modified while processing parameters");
  1429. goto bad;
  1430. }
  1431. /* Wipe the user buffer so we do not return it to userspace */
  1432. if (secure_data && clear_user(user, param_kernel->data_size))
  1433. goto bad;
  1434. *param = dmi;
  1435. return 0;
  1436. bad:
  1437. free_params(dmi, param_kernel->data_size, *param_flags);
  1438. return -EFAULT;
  1439. }
  1440. static int validate_params(uint cmd, struct dm_ioctl *param)
  1441. {
  1442. /* Always clear this flag */
  1443. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1444. param->flags &= ~DM_UEVENT_GENERATED_FLAG;
  1445. param->flags &= ~DM_SECURE_DATA_FLAG;
  1446. param->flags &= ~DM_DATA_OUT_FLAG;
  1447. /* Ignores parameters */
  1448. if (cmd == DM_REMOVE_ALL_CMD ||
  1449. cmd == DM_LIST_DEVICES_CMD ||
  1450. cmd == DM_LIST_VERSIONS_CMD)
  1451. return 0;
  1452. if ((cmd == DM_DEV_CREATE_CMD)) {
  1453. if (!*param->name) {
  1454. DMWARN("name not supplied when creating device");
  1455. return -EINVAL;
  1456. }
  1457. } else if ((*param->uuid && *param->name)) {
  1458. DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
  1459. return -EINVAL;
  1460. }
  1461. /* Ensure strings are terminated */
  1462. param->name[DM_NAME_LEN - 1] = '\0';
  1463. param->uuid[DM_UUID_LEN - 1] = '\0';
  1464. return 0;
  1465. }
  1466. static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
  1467. {
  1468. int r = 0;
  1469. int ioctl_flags;
  1470. int param_flags;
  1471. unsigned int cmd;
  1472. struct dm_ioctl *uninitialized_var(param);
  1473. ioctl_fn fn = NULL;
  1474. size_t input_param_size;
  1475. struct dm_ioctl param_kernel;
  1476. /* only root can play with this */
  1477. if (!capable(CAP_SYS_ADMIN))
  1478. return -EACCES;
  1479. if (_IOC_TYPE(command) != DM_IOCTL)
  1480. return -ENOTTY;
  1481. cmd = _IOC_NR(command);
  1482. /*
  1483. * Check the interface version passed in. This also
  1484. * writes out the kernel's interface version.
  1485. */
  1486. r = check_version(cmd, user);
  1487. if (r)
  1488. return r;
  1489. /*
  1490. * Nothing more to do for the version command.
  1491. */
  1492. if (cmd == DM_VERSION_CMD)
  1493. return 0;
  1494. fn = lookup_ioctl(cmd, &ioctl_flags);
  1495. if (!fn) {
  1496. DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
  1497. return -ENOTTY;
  1498. }
  1499. /*
  1500. * Copy the parameters into kernel space.
  1501. */
  1502. r = copy_params(user, &param_kernel, ioctl_flags, &param, &param_flags);
  1503. if (r)
  1504. return r;
  1505. input_param_size = param->data_size;
  1506. r = validate_params(cmd, param);
  1507. if (r)
  1508. goto out;
  1509. param->data_size = sizeof(*param);
  1510. r = fn(param, input_param_size);
  1511. if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
  1512. unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
  1513. DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
  1514. /*
  1515. * Copy the results back to userland.
  1516. */
  1517. if (!r && copy_to_user(user, param, param->data_size))
  1518. r = -EFAULT;
  1519. out:
  1520. free_params(param, input_param_size, param_flags);
  1521. return r;
  1522. }
  1523. static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
  1524. {
  1525. return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
  1526. }
  1527. #ifdef CONFIG_COMPAT
  1528. static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
  1529. {
  1530. return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
  1531. }
  1532. #else
  1533. #define dm_compat_ctl_ioctl NULL
  1534. #endif
  1535. static const struct file_operations _ctl_fops = {
  1536. .open = nonseekable_open,
  1537. .unlocked_ioctl = dm_ctl_ioctl,
  1538. .compat_ioctl = dm_compat_ctl_ioctl,
  1539. .owner = THIS_MODULE,
  1540. .llseek = noop_llseek,
  1541. };
  1542. static struct miscdevice _dm_misc = {
  1543. .minor = MAPPER_CTRL_MINOR,
  1544. .name = DM_NAME,
  1545. .nodename = DM_DIR "/" DM_CONTROL_NODE,
  1546. .fops = &_ctl_fops
  1547. };
  1548. MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
  1549. MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
  1550. /*
  1551. * Create misc character device and link to DM_DIR/control.
  1552. */
  1553. int __init dm_interface_init(void)
  1554. {
  1555. int r;
  1556. r = dm_hash_init();
  1557. if (r)
  1558. return r;
  1559. r = misc_register(&_dm_misc);
  1560. if (r) {
  1561. DMERR("misc_register failed for control device");
  1562. dm_hash_exit();
  1563. return r;
  1564. }
  1565. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1566. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1567. DM_DRIVER_EMAIL);
  1568. return 0;
  1569. }
  1570. void dm_interface_exit(void)
  1571. {
  1572. misc_deregister(&_dm_misc);
  1573. dm_hash_exit();
  1574. }
  1575. /**
  1576. * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
  1577. * @md: Pointer to mapped_device
  1578. * @name: Buffer (size DM_NAME_LEN) for name
  1579. * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
  1580. */
  1581. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
  1582. {
  1583. int r = 0;
  1584. struct hash_cell *hc;
  1585. if (!md)
  1586. return -ENXIO;
  1587. mutex_lock(&dm_hash_cells_mutex);
  1588. hc = dm_get_mdptr(md);
  1589. if (!hc || hc->md != md) {
  1590. r = -ENXIO;
  1591. goto out;
  1592. }
  1593. if (name)
  1594. strcpy(name, hc->name);
  1595. if (uuid)
  1596. strcpy(uuid, hc->uuid ? : "");
  1597. out:
  1598. mutex_unlock(&dm_hash_cells_mutex);
  1599. return r;
  1600. }