dm-ioctl.c 42 KB

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