dm-ioctl.c 37 KB

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