dm-ioctl.c 31 KB

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