dm-ioctl.c 31 KB

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