dm-ioctl.c 37 KB

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