dm-ioctl.c 48 KB

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