dm-ioctl.c 29 KB

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