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