dm-ioctl.c 36 KB

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