dm-ioctl.c 42 KB

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