dm-ioctl.c 35 KB

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