dm-ioctl.c 44 KB

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