dm-ioctl.c 29 KB

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