dm-ioctl.c 34 KB

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