dm-ioctl.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  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. mdptr = dm_get_mdptr(md);
  502. return mdptr;
  503. }
  504. static struct mapped_device *find_device(struct dm_ioctl *param)
  505. {
  506. struct hash_cell *hc;
  507. struct mapped_device *md = NULL;
  508. down_read(&_hash_lock);
  509. hc = __find_device_hash_cell(param);
  510. if (hc) {
  511. md = hc->md;
  512. /*
  513. * Sneakily write in both the name and the uuid
  514. * while we have the cell.
  515. */
  516. strncpy(param->name, hc->name, sizeof(param->name));
  517. if (hc->uuid)
  518. strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
  519. else
  520. param->uuid[0] = '\0';
  521. if (hc->new_map)
  522. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  523. else
  524. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  525. }
  526. up_read(&_hash_lock);
  527. return md;
  528. }
  529. static int dev_remove(struct dm_ioctl *param, size_t param_size)
  530. {
  531. struct hash_cell *hc;
  532. struct mapped_device *md;
  533. int r;
  534. down_write(&_hash_lock);
  535. hc = __find_device_hash_cell(param);
  536. if (!hc) {
  537. DMWARN("device doesn't appear to be in the dev hash table.");
  538. up_write(&_hash_lock);
  539. return -ENXIO;
  540. }
  541. md = hc->md;
  542. /*
  543. * Ensure the device is not open and nothing further can open it.
  544. */
  545. r = dm_lock_for_deletion(md);
  546. if (r) {
  547. DMWARN("unable to remove open device %s", hc->name);
  548. up_write(&_hash_lock);
  549. dm_put(md);
  550. return r;
  551. }
  552. __hash_remove(hc);
  553. up_write(&_hash_lock);
  554. dm_put(md);
  555. param->data_size = 0;
  556. return 0;
  557. }
  558. /*
  559. * Check a string doesn't overrun the chunk of
  560. * memory we copied from userland.
  561. */
  562. static int invalid_str(char *str, void *end)
  563. {
  564. while ((void *) str < end)
  565. if (!*str++)
  566. return 0;
  567. return -EINVAL;
  568. }
  569. static int dev_rename(struct dm_ioctl *param, size_t param_size)
  570. {
  571. int r;
  572. char *new_name = (char *) param + param->data_start;
  573. if (new_name < (char *) (param + 1) ||
  574. invalid_str(new_name, (void *) param + param_size)) {
  575. DMWARN("Invalid new logical volume name supplied.");
  576. return -EINVAL;
  577. }
  578. r = check_name(new_name);
  579. if (r)
  580. return r;
  581. param->data_size = 0;
  582. return dm_hash_rename(param->name, new_name);
  583. }
  584. static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
  585. {
  586. int r = -EINVAL, x;
  587. struct mapped_device *md;
  588. struct hd_geometry geometry;
  589. unsigned long indata[4];
  590. char *geostr = (char *) param + param->data_start;
  591. md = find_device(param);
  592. if (!md)
  593. return -ENXIO;
  594. if (geostr < (char *) (param + 1) ||
  595. invalid_str(geostr, (void *) param + param_size)) {
  596. DMWARN("Invalid geometry supplied.");
  597. goto out;
  598. }
  599. x = sscanf(geostr, "%lu %lu %lu %lu", indata,
  600. indata + 1, indata + 2, indata + 3);
  601. if (x != 4) {
  602. DMWARN("Unable to interpret geometry settings.");
  603. goto out;
  604. }
  605. if (indata[0] > 65535 || indata[1] > 255 ||
  606. indata[2] > 255 || indata[3] > ULONG_MAX) {
  607. DMWARN("Geometry exceeds range limits.");
  608. goto out;
  609. }
  610. geometry.cylinders = indata[0];
  611. geometry.heads = indata[1];
  612. geometry.sectors = indata[2];
  613. geometry.start = indata[3];
  614. r = dm_set_geometry(md, &geometry);
  615. if (!r)
  616. r = __dev_status(md, param);
  617. param->data_size = 0;
  618. out:
  619. dm_put(md);
  620. return r;
  621. }
  622. static int do_suspend(struct dm_ioctl *param)
  623. {
  624. int r = 0;
  625. int do_lockfs = 1;
  626. struct mapped_device *md;
  627. md = find_device(param);
  628. if (!md)
  629. return -ENXIO;
  630. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  631. do_lockfs = 0;
  632. if (!dm_suspended(md))
  633. r = dm_suspend(md, do_lockfs);
  634. if (!r)
  635. r = __dev_status(md, param);
  636. dm_put(md);
  637. return r;
  638. }
  639. static int do_resume(struct dm_ioctl *param)
  640. {
  641. int r = 0;
  642. int do_lockfs = 1;
  643. struct hash_cell *hc;
  644. struct mapped_device *md;
  645. struct dm_table *new_map;
  646. down_write(&_hash_lock);
  647. hc = __find_device_hash_cell(param);
  648. if (!hc) {
  649. DMWARN("device doesn't appear to be in the dev hash table.");
  650. up_write(&_hash_lock);
  651. return -ENXIO;
  652. }
  653. md = hc->md;
  654. new_map = hc->new_map;
  655. hc->new_map = NULL;
  656. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  657. up_write(&_hash_lock);
  658. /* Do we need to load a new map ? */
  659. if (new_map) {
  660. /* Suspend if it isn't already suspended */
  661. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  662. do_lockfs = 0;
  663. if (!dm_suspended(md))
  664. dm_suspend(md, do_lockfs);
  665. r = dm_swap_table(md, new_map);
  666. if (r) {
  667. dm_put(md);
  668. dm_table_put(new_map);
  669. return r;
  670. }
  671. if (dm_table_get_mode(new_map) & FMODE_WRITE)
  672. set_disk_ro(dm_disk(md), 0);
  673. else
  674. set_disk_ro(dm_disk(md), 1);
  675. dm_table_put(new_map);
  676. }
  677. if (dm_suspended(md))
  678. r = dm_resume(md);
  679. if (!r)
  680. r = __dev_status(md, param);
  681. dm_put(md);
  682. return r;
  683. }
  684. /*
  685. * Set or unset the suspension state of a device.
  686. * If the device already is in the requested state we just return its status.
  687. */
  688. static int dev_suspend(struct dm_ioctl *param, size_t param_size)
  689. {
  690. if (param->flags & DM_SUSPEND_FLAG)
  691. return do_suspend(param);
  692. return do_resume(param);
  693. }
  694. /*
  695. * Copies device info back to user space, used by
  696. * the create and info ioctls.
  697. */
  698. static int dev_status(struct dm_ioctl *param, size_t param_size)
  699. {
  700. int r;
  701. struct mapped_device *md;
  702. md = find_device(param);
  703. if (!md)
  704. return -ENXIO;
  705. r = __dev_status(md, param);
  706. dm_put(md);
  707. return r;
  708. }
  709. /*
  710. * Build up the status struct for each target
  711. */
  712. static void retrieve_status(struct dm_table *table,
  713. struct dm_ioctl *param, size_t param_size)
  714. {
  715. unsigned int i, num_targets;
  716. struct dm_target_spec *spec;
  717. char *outbuf, *outptr;
  718. status_type_t type;
  719. size_t remaining, len, used = 0;
  720. outptr = outbuf = get_result_buffer(param, param_size, &len);
  721. if (param->flags & DM_STATUS_TABLE_FLAG)
  722. type = STATUSTYPE_TABLE;
  723. else
  724. type = STATUSTYPE_INFO;
  725. /* Get all the target info */
  726. num_targets = dm_table_get_num_targets(table);
  727. for (i = 0; i < num_targets; i++) {
  728. struct dm_target *ti = dm_table_get_target(table, i);
  729. remaining = len - (outptr - outbuf);
  730. if (remaining <= sizeof(struct dm_target_spec)) {
  731. param->flags |= DM_BUFFER_FULL_FLAG;
  732. break;
  733. }
  734. spec = (struct dm_target_spec *) outptr;
  735. spec->status = 0;
  736. spec->sector_start = ti->begin;
  737. spec->length = ti->len;
  738. strncpy(spec->target_type, ti->type->name,
  739. sizeof(spec->target_type));
  740. outptr += sizeof(struct dm_target_spec);
  741. remaining = len - (outptr - outbuf);
  742. if (remaining <= 0) {
  743. param->flags |= DM_BUFFER_FULL_FLAG;
  744. break;
  745. }
  746. /* Get the status/table string from the target driver */
  747. if (ti->type->status) {
  748. if (ti->type->status(ti, type, outptr, remaining)) {
  749. param->flags |= DM_BUFFER_FULL_FLAG;
  750. break;
  751. }
  752. } else
  753. outptr[0] = '\0';
  754. outptr += strlen(outptr) + 1;
  755. used = param->data_start + (outptr - outbuf);
  756. outptr = align_ptr(outptr);
  757. spec->next = outptr - outbuf;
  758. }
  759. if (used)
  760. param->data_size = used;
  761. param->target_count = num_targets;
  762. }
  763. /*
  764. * Wait for a device to report an event
  765. */
  766. static int dev_wait(struct dm_ioctl *param, size_t param_size)
  767. {
  768. int r;
  769. struct mapped_device *md;
  770. struct dm_table *table;
  771. md = find_device(param);
  772. if (!md)
  773. return -ENXIO;
  774. /*
  775. * Wait for a notification event
  776. */
  777. if (dm_wait_event(md, param->event_nr)) {
  778. r = -ERESTARTSYS;
  779. goto out;
  780. }
  781. /*
  782. * The userland program is going to want to know what
  783. * changed to trigger the event, so we may as well tell
  784. * him and save an ioctl.
  785. */
  786. r = __dev_status(md, param);
  787. if (r)
  788. goto out;
  789. table = dm_get_table(md);
  790. if (table) {
  791. retrieve_status(table, param, param_size);
  792. dm_table_put(table);
  793. }
  794. out:
  795. dm_put(md);
  796. return r;
  797. }
  798. static inline int get_mode(struct dm_ioctl *param)
  799. {
  800. int mode = FMODE_READ | FMODE_WRITE;
  801. if (param->flags & DM_READONLY_FLAG)
  802. mode = FMODE_READ;
  803. return mode;
  804. }
  805. static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
  806. struct dm_target_spec **spec, char **target_params)
  807. {
  808. *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
  809. *target_params = (char *) (*spec + 1);
  810. if (*spec < (last + 1))
  811. return -EINVAL;
  812. return invalid_str(*target_params, end);
  813. }
  814. static int populate_table(struct dm_table *table,
  815. struct dm_ioctl *param, size_t param_size)
  816. {
  817. int r;
  818. unsigned int i = 0;
  819. struct dm_target_spec *spec = (struct dm_target_spec *) param;
  820. uint32_t next = param->data_start;
  821. void *end = (void *) param + param_size;
  822. char *target_params;
  823. if (!param->target_count) {
  824. DMWARN("populate_table: no targets specified");
  825. return -EINVAL;
  826. }
  827. for (i = 0; i < param->target_count; i++) {
  828. r = next_target(spec, next, end, &spec, &target_params);
  829. if (r) {
  830. DMWARN("unable to find target");
  831. return r;
  832. }
  833. r = dm_table_add_target(table, spec->target_type,
  834. (sector_t) spec->sector_start,
  835. (sector_t) spec->length,
  836. target_params);
  837. if (r) {
  838. DMWARN("error adding target to table");
  839. return r;
  840. }
  841. next = spec->next;
  842. }
  843. return dm_table_complete(table);
  844. }
  845. static int table_load(struct dm_ioctl *param, size_t param_size)
  846. {
  847. int r;
  848. struct hash_cell *hc;
  849. struct dm_table *t;
  850. struct mapped_device *md;
  851. md = find_device(param);
  852. if (!md)
  853. return -ENXIO;
  854. r = dm_table_create(&t, get_mode(param), param->target_count, md);
  855. if (r)
  856. goto out;
  857. r = populate_table(t, param, param_size);
  858. if (r) {
  859. dm_table_put(t);
  860. goto out;
  861. }
  862. down_write(&_hash_lock);
  863. hc = dm_get_mdptr(md);
  864. if (!hc || hc->md != md) {
  865. DMWARN("device has been removed from the dev hash table.");
  866. dm_table_put(t);
  867. up_write(&_hash_lock);
  868. r = -ENXIO;
  869. goto out;
  870. }
  871. if (hc->new_map)
  872. dm_table_put(hc->new_map);
  873. hc->new_map = t;
  874. up_write(&_hash_lock);
  875. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  876. r = __dev_status(md, param);
  877. out:
  878. dm_put(md);
  879. return r;
  880. }
  881. static int table_clear(struct dm_ioctl *param, size_t param_size)
  882. {
  883. int r;
  884. struct hash_cell *hc;
  885. struct mapped_device *md;
  886. down_write(&_hash_lock);
  887. hc = __find_device_hash_cell(param);
  888. if (!hc) {
  889. DMWARN("device doesn't appear to be in the dev hash table.");
  890. up_write(&_hash_lock);
  891. return -ENXIO;
  892. }
  893. if (hc->new_map) {
  894. dm_table_put(hc->new_map);
  895. hc->new_map = NULL;
  896. }
  897. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  898. r = __dev_status(hc->md, param);
  899. md = hc->md;
  900. up_write(&_hash_lock);
  901. dm_put(md);
  902. return r;
  903. }
  904. /*
  905. * Retrieves a list of devices used by a particular dm device.
  906. */
  907. static void retrieve_deps(struct dm_table *table,
  908. struct dm_ioctl *param, size_t param_size)
  909. {
  910. unsigned int count = 0;
  911. struct list_head *tmp;
  912. size_t len, needed;
  913. struct dm_dev *dd;
  914. struct dm_target_deps *deps;
  915. deps = get_result_buffer(param, param_size, &len);
  916. /*
  917. * Count the devices.
  918. */
  919. list_for_each (tmp, dm_table_get_devices(table))
  920. count++;
  921. /*
  922. * Check we have enough space.
  923. */
  924. needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
  925. if (len < needed) {
  926. param->flags |= DM_BUFFER_FULL_FLAG;
  927. return;
  928. }
  929. /*
  930. * Fill in the devices.
  931. */
  932. deps->count = count;
  933. count = 0;
  934. list_for_each_entry (dd, dm_table_get_devices(table), list)
  935. deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
  936. param->data_size = param->data_start + needed;
  937. }
  938. static int table_deps(struct dm_ioctl *param, size_t param_size)
  939. {
  940. int r = 0;
  941. struct mapped_device *md;
  942. struct dm_table *table;
  943. md = find_device(param);
  944. if (!md)
  945. return -ENXIO;
  946. r = __dev_status(md, param);
  947. if (r)
  948. goto out;
  949. table = dm_get_table(md);
  950. if (table) {
  951. retrieve_deps(table, param, param_size);
  952. dm_table_put(table);
  953. }
  954. out:
  955. dm_put(md);
  956. return r;
  957. }
  958. /*
  959. * Return the status of a device as a text string for each
  960. * target.
  961. */
  962. static int table_status(struct dm_ioctl *param, size_t param_size)
  963. {
  964. int r;
  965. struct mapped_device *md;
  966. struct dm_table *table;
  967. md = find_device(param);
  968. if (!md)
  969. return -ENXIO;
  970. r = __dev_status(md, param);
  971. if (r)
  972. goto out;
  973. table = dm_get_table(md);
  974. if (table) {
  975. retrieve_status(table, param, param_size);
  976. dm_table_put(table);
  977. }
  978. out:
  979. dm_put(md);
  980. return r;
  981. }
  982. /*
  983. * Pass a message to the target that's at the supplied device offset.
  984. */
  985. static int target_message(struct dm_ioctl *param, size_t param_size)
  986. {
  987. int r, argc;
  988. char **argv;
  989. struct mapped_device *md;
  990. struct dm_table *table;
  991. struct dm_target *ti;
  992. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  993. md = find_device(param);
  994. if (!md)
  995. return -ENXIO;
  996. r = __dev_status(md, param);
  997. if (r)
  998. goto out;
  999. if (tmsg < (struct dm_target_msg *) (param + 1) ||
  1000. invalid_str(tmsg->message, (void *) param + param_size)) {
  1001. DMWARN("Invalid target message parameters.");
  1002. r = -EINVAL;
  1003. goto out;
  1004. }
  1005. r = dm_split_args(&argc, &argv, tmsg->message);
  1006. if (r) {
  1007. DMWARN("Failed to split target message parameters");
  1008. goto out;
  1009. }
  1010. table = dm_get_table(md);
  1011. if (!table)
  1012. goto out_argv;
  1013. if (tmsg->sector >= dm_table_get_size(table)) {
  1014. DMWARN("Target message sector outside device.");
  1015. r = -EINVAL;
  1016. goto out_table;
  1017. }
  1018. ti = dm_table_find_target(table, tmsg->sector);
  1019. if (ti->type->message)
  1020. r = ti->type->message(ti, argc, argv);
  1021. else {
  1022. DMWARN("Target type does not support messages");
  1023. r = -EINVAL;
  1024. }
  1025. out_table:
  1026. dm_table_put(table);
  1027. out_argv:
  1028. kfree(argv);
  1029. out:
  1030. param->data_size = 0;
  1031. dm_put(md);
  1032. return r;
  1033. }
  1034. /*-----------------------------------------------------------------
  1035. * Implementation of open/close/ioctl on the special char
  1036. * device.
  1037. *---------------------------------------------------------------*/
  1038. static ioctl_fn lookup_ioctl(unsigned int cmd)
  1039. {
  1040. static struct {
  1041. int cmd;
  1042. ioctl_fn fn;
  1043. } _ioctls[] = {
  1044. {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
  1045. {DM_REMOVE_ALL_CMD, remove_all},
  1046. {DM_LIST_DEVICES_CMD, list_devices},
  1047. {DM_DEV_CREATE_CMD, dev_create},
  1048. {DM_DEV_REMOVE_CMD, dev_remove},
  1049. {DM_DEV_RENAME_CMD, dev_rename},
  1050. {DM_DEV_SUSPEND_CMD, dev_suspend},
  1051. {DM_DEV_STATUS_CMD, dev_status},
  1052. {DM_DEV_WAIT_CMD, dev_wait},
  1053. {DM_TABLE_LOAD_CMD, table_load},
  1054. {DM_TABLE_CLEAR_CMD, table_clear},
  1055. {DM_TABLE_DEPS_CMD, table_deps},
  1056. {DM_TABLE_STATUS_CMD, table_status},
  1057. {DM_LIST_VERSIONS_CMD, list_versions},
  1058. {DM_TARGET_MSG_CMD, target_message},
  1059. {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
  1060. };
  1061. return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
  1062. }
  1063. /*
  1064. * As well as checking the version compatibility this always
  1065. * copies the kernel interface version out.
  1066. */
  1067. static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
  1068. {
  1069. uint32_t version[3];
  1070. int r = 0;
  1071. if (copy_from_user(version, user->version, sizeof(version)))
  1072. return -EFAULT;
  1073. if ((DM_VERSION_MAJOR != version[0]) ||
  1074. (DM_VERSION_MINOR < version[1])) {
  1075. DMWARN("ioctl interface mismatch: "
  1076. "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1077. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1078. DM_VERSION_PATCHLEVEL,
  1079. version[0], version[1], version[2], cmd);
  1080. r = -EINVAL;
  1081. }
  1082. /*
  1083. * Fill in the kernel version.
  1084. */
  1085. version[0] = DM_VERSION_MAJOR;
  1086. version[1] = DM_VERSION_MINOR;
  1087. version[2] = DM_VERSION_PATCHLEVEL;
  1088. if (copy_to_user(user->version, version, sizeof(version)))
  1089. return -EFAULT;
  1090. return r;
  1091. }
  1092. static void free_params(struct dm_ioctl *param)
  1093. {
  1094. vfree(param);
  1095. }
  1096. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
  1097. {
  1098. struct dm_ioctl tmp, *dmi;
  1099. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1100. return -EFAULT;
  1101. if (tmp.data_size < sizeof(tmp))
  1102. return -EINVAL;
  1103. dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
  1104. if (!dmi)
  1105. return -ENOMEM;
  1106. if (copy_from_user(dmi, user, tmp.data_size)) {
  1107. vfree(dmi);
  1108. return -EFAULT;
  1109. }
  1110. *param = dmi;
  1111. return 0;
  1112. }
  1113. static int validate_params(uint cmd, struct dm_ioctl *param)
  1114. {
  1115. /* Always clear this flag */
  1116. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1117. /* Ignores parameters */
  1118. if (cmd == DM_REMOVE_ALL_CMD ||
  1119. cmd == DM_LIST_DEVICES_CMD ||
  1120. cmd == DM_LIST_VERSIONS_CMD)
  1121. return 0;
  1122. if ((cmd == DM_DEV_CREATE_CMD)) {
  1123. if (!*param->name) {
  1124. DMWARN("name not supplied when creating device");
  1125. return -EINVAL;
  1126. }
  1127. } else if ((*param->uuid && *param->name)) {
  1128. DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
  1129. return -EINVAL;
  1130. }
  1131. /* Ensure strings are terminated */
  1132. param->name[DM_NAME_LEN - 1] = '\0';
  1133. param->uuid[DM_UUID_LEN - 1] = '\0';
  1134. return 0;
  1135. }
  1136. static int ctl_ioctl(struct inode *inode, struct file *file,
  1137. uint command, ulong u)
  1138. {
  1139. int r = 0;
  1140. unsigned int cmd;
  1141. struct dm_ioctl *param;
  1142. struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
  1143. ioctl_fn fn = NULL;
  1144. size_t param_size;
  1145. /* only root can play with this */
  1146. if (!capable(CAP_SYS_ADMIN))
  1147. return -EACCES;
  1148. if (_IOC_TYPE(command) != DM_IOCTL)
  1149. return -ENOTTY;
  1150. cmd = _IOC_NR(command);
  1151. /*
  1152. * Check the interface version passed in. This also
  1153. * writes out the kernel's interface version.
  1154. */
  1155. r = check_version(cmd, user);
  1156. if (r)
  1157. return r;
  1158. /*
  1159. * Nothing more to do for the version command.
  1160. */
  1161. if (cmd == DM_VERSION_CMD)
  1162. return 0;
  1163. fn = lookup_ioctl(cmd);
  1164. if (!fn) {
  1165. DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
  1166. return -ENOTTY;
  1167. }
  1168. /*
  1169. * Trying to avoid low memory issues when a device is
  1170. * suspended.
  1171. */
  1172. current->flags |= PF_MEMALLOC;
  1173. /*
  1174. * Copy the parameters into kernel space.
  1175. */
  1176. r = copy_params(user, &param);
  1177. current->flags &= ~PF_MEMALLOC;
  1178. if (r)
  1179. return r;
  1180. r = validate_params(cmd, param);
  1181. if (r)
  1182. goto out;
  1183. param_size = param->data_size;
  1184. param->data_size = sizeof(*param);
  1185. r = fn(param, param_size);
  1186. /*
  1187. * Copy the results back to userland.
  1188. */
  1189. if (!r && copy_to_user(user, param, param->data_size))
  1190. r = -EFAULT;
  1191. out:
  1192. free_params(param);
  1193. return r;
  1194. }
  1195. static struct file_operations _ctl_fops = {
  1196. .ioctl = ctl_ioctl,
  1197. .owner = THIS_MODULE,
  1198. };
  1199. static struct miscdevice _dm_misc = {
  1200. .minor = MISC_DYNAMIC_MINOR,
  1201. .name = DM_NAME,
  1202. .fops = &_ctl_fops
  1203. };
  1204. /*
  1205. * Create misc character device and link to DM_DIR/control.
  1206. */
  1207. int __init dm_interface_init(void)
  1208. {
  1209. int r;
  1210. r = dm_hash_init();
  1211. if (r)
  1212. return r;
  1213. r = misc_register(&_dm_misc);
  1214. if (r) {
  1215. DMERR("misc_register failed for control device");
  1216. dm_hash_exit();
  1217. return r;
  1218. }
  1219. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1220. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1221. DM_DRIVER_EMAIL);
  1222. return 0;
  1223. }
  1224. void dm_interface_exit(void)
  1225. {
  1226. if (misc_deregister(&_dm_misc) < 0)
  1227. DMERR("misc_deregister failed for control device");
  1228. dm_hash_exit();
  1229. }