dm-ioctl.c 42 KB

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