dm-ioctl.c 36 KB

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