dm-ioctl.c 30 KB

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