dm-ioctl.c 30 KB

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