dm-ioctl.c 34 KB

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