dm-ioctl.c 30 KB

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