dm-ioctl.c 31 KB

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