dm-ioctl.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  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. char dummy;
  727. md = find_device(param);
  728. if (!md)
  729. return -ENXIO;
  730. if (geostr < param->data ||
  731. invalid_str(geostr, (void *) param + param_size)) {
  732. DMWARN("Invalid geometry supplied.");
  733. goto out;
  734. }
  735. x = sscanf(geostr, "%lu %lu %lu %lu%c", indata,
  736. indata + 1, indata + 2, indata + 3, &dummy);
  737. if (x != 4) {
  738. DMWARN("Unable to interpret geometry settings.");
  739. goto out;
  740. }
  741. if (indata[0] > 65535 || indata[1] > 255 ||
  742. indata[2] > 255 || indata[3] > ULONG_MAX) {
  743. DMWARN("Geometry exceeds range limits.");
  744. goto out;
  745. }
  746. geometry.cylinders = indata[0];
  747. geometry.heads = indata[1];
  748. geometry.sectors = indata[2];
  749. geometry.start = indata[3];
  750. r = dm_set_geometry(md, &geometry);
  751. param->data_size = 0;
  752. out:
  753. dm_put(md);
  754. return r;
  755. }
  756. static int do_suspend(struct dm_ioctl *param)
  757. {
  758. int r = 0;
  759. unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  760. struct mapped_device *md;
  761. md = find_device(param);
  762. if (!md)
  763. return -ENXIO;
  764. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  765. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  766. if (param->flags & DM_NOFLUSH_FLAG)
  767. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  768. if (!dm_suspended_md(md)) {
  769. r = dm_suspend(md, suspend_flags);
  770. if (r)
  771. goto out;
  772. }
  773. __dev_status(md, param);
  774. out:
  775. dm_put(md);
  776. return r;
  777. }
  778. static int do_resume(struct dm_ioctl *param)
  779. {
  780. int r = 0;
  781. unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  782. struct hash_cell *hc;
  783. struct mapped_device *md;
  784. struct dm_table *new_map, *old_map = NULL;
  785. down_write(&_hash_lock);
  786. hc = __find_device_hash_cell(param);
  787. if (!hc) {
  788. DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
  789. up_write(&_hash_lock);
  790. return -ENXIO;
  791. }
  792. md = hc->md;
  793. new_map = hc->new_map;
  794. hc->new_map = NULL;
  795. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  796. up_write(&_hash_lock);
  797. /* Do we need to load a new map ? */
  798. if (new_map) {
  799. /* Suspend if it isn't already suspended */
  800. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  801. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  802. if (param->flags & DM_NOFLUSH_FLAG)
  803. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  804. if (!dm_suspended_md(md))
  805. dm_suspend(md, suspend_flags);
  806. old_map = dm_swap_table(md, new_map);
  807. if (IS_ERR(old_map)) {
  808. dm_table_destroy(new_map);
  809. dm_put(md);
  810. return PTR_ERR(old_map);
  811. }
  812. if (dm_table_get_mode(new_map) & FMODE_WRITE)
  813. set_disk_ro(dm_disk(md), 0);
  814. else
  815. set_disk_ro(dm_disk(md), 1);
  816. }
  817. if (dm_suspended_md(md)) {
  818. r = dm_resume(md);
  819. if (!r && !dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr))
  820. param->flags |= DM_UEVENT_GENERATED_FLAG;
  821. }
  822. if (old_map)
  823. dm_table_destroy(old_map);
  824. if (!r)
  825. __dev_status(md, param);
  826. dm_put(md);
  827. return r;
  828. }
  829. /*
  830. * Set or unset the suspension state of a device.
  831. * If the device already is in the requested state we just return its status.
  832. */
  833. static int dev_suspend(struct dm_ioctl *param, size_t param_size)
  834. {
  835. if (param->flags & DM_SUSPEND_FLAG)
  836. return do_suspend(param);
  837. return do_resume(param);
  838. }
  839. /*
  840. * Copies device info back to user space, used by
  841. * the create and info ioctls.
  842. */
  843. static int dev_status(struct dm_ioctl *param, size_t param_size)
  844. {
  845. struct mapped_device *md;
  846. md = find_device(param);
  847. if (!md)
  848. return -ENXIO;
  849. __dev_status(md, param);
  850. dm_put(md);
  851. return 0;
  852. }
  853. /*
  854. * Build up the status struct for each target
  855. */
  856. static void retrieve_status(struct dm_table *table,
  857. struct dm_ioctl *param, size_t param_size)
  858. {
  859. unsigned int i, num_targets;
  860. struct dm_target_spec *spec;
  861. char *outbuf, *outptr;
  862. status_type_t type;
  863. size_t remaining, len, used = 0;
  864. outptr = outbuf = get_result_buffer(param, param_size, &len);
  865. if (param->flags & DM_STATUS_TABLE_FLAG)
  866. type = STATUSTYPE_TABLE;
  867. else
  868. type = STATUSTYPE_INFO;
  869. /* Get all the target info */
  870. num_targets = dm_table_get_num_targets(table);
  871. for (i = 0; i < num_targets; i++) {
  872. struct dm_target *ti = dm_table_get_target(table, i);
  873. remaining = len - (outptr - outbuf);
  874. if (remaining <= sizeof(struct dm_target_spec)) {
  875. param->flags |= DM_BUFFER_FULL_FLAG;
  876. break;
  877. }
  878. spec = (struct dm_target_spec *) outptr;
  879. spec->status = 0;
  880. spec->sector_start = ti->begin;
  881. spec->length = ti->len;
  882. strncpy(spec->target_type, ti->type->name,
  883. sizeof(spec->target_type));
  884. outptr += sizeof(struct dm_target_spec);
  885. remaining = len - (outptr - outbuf);
  886. if (remaining <= 0) {
  887. param->flags |= DM_BUFFER_FULL_FLAG;
  888. break;
  889. }
  890. /* Get the status/table string from the target driver */
  891. if (ti->type->status) {
  892. if (ti->type->status(ti, type, outptr, remaining)) {
  893. param->flags |= DM_BUFFER_FULL_FLAG;
  894. break;
  895. }
  896. } else
  897. outptr[0] = '\0';
  898. outptr += strlen(outptr) + 1;
  899. used = param->data_start + (outptr - outbuf);
  900. outptr = align_ptr(outptr);
  901. spec->next = outptr - outbuf;
  902. }
  903. if (used)
  904. param->data_size = used;
  905. param->target_count = num_targets;
  906. }
  907. /*
  908. * Wait for a device to report an event
  909. */
  910. static int dev_wait(struct dm_ioctl *param, size_t param_size)
  911. {
  912. int r = 0;
  913. struct mapped_device *md;
  914. struct dm_table *table;
  915. md = find_device(param);
  916. if (!md)
  917. return -ENXIO;
  918. /*
  919. * Wait for a notification event
  920. */
  921. if (dm_wait_event(md, param->event_nr)) {
  922. r = -ERESTARTSYS;
  923. goto out;
  924. }
  925. /*
  926. * The userland program is going to want to know what
  927. * changed to trigger the event, so we may as well tell
  928. * him and save an ioctl.
  929. */
  930. __dev_status(md, param);
  931. table = dm_get_live_or_inactive_table(md, param);
  932. if (table) {
  933. retrieve_status(table, param, param_size);
  934. dm_table_put(table);
  935. }
  936. out:
  937. dm_put(md);
  938. return r;
  939. }
  940. static inline fmode_t get_mode(struct dm_ioctl *param)
  941. {
  942. fmode_t mode = FMODE_READ | FMODE_WRITE;
  943. if (param->flags & DM_READONLY_FLAG)
  944. mode = FMODE_READ;
  945. return mode;
  946. }
  947. static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
  948. struct dm_target_spec **spec, char **target_params)
  949. {
  950. *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
  951. *target_params = (char *) (*spec + 1);
  952. if (*spec < (last + 1))
  953. return -EINVAL;
  954. return invalid_str(*target_params, end);
  955. }
  956. static int populate_table(struct dm_table *table,
  957. struct dm_ioctl *param, size_t param_size)
  958. {
  959. int r;
  960. unsigned int i = 0;
  961. struct dm_target_spec *spec = (struct dm_target_spec *) param;
  962. uint32_t next = param->data_start;
  963. void *end = (void *) param + param_size;
  964. char *target_params;
  965. if (!param->target_count) {
  966. DMWARN("populate_table: no targets specified");
  967. return -EINVAL;
  968. }
  969. for (i = 0; i < param->target_count; i++) {
  970. r = next_target(spec, next, end, &spec, &target_params);
  971. if (r) {
  972. DMWARN("unable to find target");
  973. return r;
  974. }
  975. r = dm_table_add_target(table, spec->target_type,
  976. (sector_t) spec->sector_start,
  977. (sector_t) spec->length,
  978. target_params);
  979. if (r) {
  980. DMWARN("error adding target to table");
  981. return r;
  982. }
  983. next = spec->next;
  984. }
  985. return dm_table_complete(table);
  986. }
  987. static int table_load(struct dm_ioctl *param, size_t param_size)
  988. {
  989. int r;
  990. struct hash_cell *hc;
  991. struct dm_table *t;
  992. struct mapped_device *md;
  993. struct target_type *immutable_target_type;
  994. md = find_device(param);
  995. if (!md)
  996. return -ENXIO;
  997. r = dm_table_create(&t, get_mode(param), param->target_count, md);
  998. if (r)
  999. goto out;
  1000. r = populate_table(t, param, param_size);
  1001. if (r) {
  1002. dm_table_destroy(t);
  1003. goto out;
  1004. }
  1005. immutable_target_type = dm_get_immutable_target_type(md);
  1006. if (immutable_target_type &&
  1007. (immutable_target_type != dm_table_get_immutable_target_type(t))) {
  1008. DMWARN("can't replace immutable target type %s",
  1009. immutable_target_type->name);
  1010. dm_table_destroy(t);
  1011. r = -EINVAL;
  1012. goto out;
  1013. }
  1014. /* Protect md->type and md->queue against concurrent table loads. */
  1015. dm_lock_md_type(md);
  1016. if (dm_get_md_type(md) == DM_TYPE_NONE)
  1017. /* Initial table load: acquire type of table. */
  1018. dm_set_md_type(md, dm_table_get_type(t));
  1019. else if (dm_get_md_type(md) != dm_table_get_type(t)) {
  1020. DMWARN("can't change device type after initial table load.");
  1021. dm_table_destroy(t);
  1022. dm_unlock_md_type(md);
  1023. r = -EINVAL;
  1024. goto out;
  1025. }
  1026. /* setup md->queue to reflect md's type (may block) */
  1027. r = dm_setup_md_queue(md);
  1028. if (r) {
  1029. DMWARN("unable to set up device queue for new table.");
  1030. dm_table_destroy(t);
  1031. dm_unlock_md_type(md);
  1032. goto out;
  1033. }
  1034. dm_unlock_md_type(md);
  1035. /* stage inactive table */
  1036. down_write(&_hash_lock);
  1037. hc = dm_get_mdptr(md);
  1038. if (!hc || hc->md != md) {
  1039. DMWARN("device has been removed from the dev hash table.");
  1040. dm_table_destroy(t);
  1041. up_write(&_hash_lock);
  1042. r = -ENXIO;
  1043. goto out;
  1044. }
  1045. if (hc->new_map)
  1046. dm_table_destroy(hc->new_map);
  1047. hc->new_map = t;
  1048. up_write(&_hash_lock);
  1049. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  1050. __dev_status(md, param);
  1051. out:
  1052. dm_put(md);
  1053. return r;
  1054. }
  1055. static int table_clear(struct dm_ioctl *param, size_t param_size)
  1056. {
  1057. struct hash_cell *hc;
  1058. struct mapped_device *md;
  1059. down_write(&_hash_lock);
  1060. hc = __find_device_hash_cell(param);
  1061. if (!hc) {
  1062. DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
  1063. up_write(&_hash_lock);
  1064. return -ENXIO;
  1065. }
  1066. if (hc->new_map) {
  1067. dm_table_destroy(hc->new_map);
  1068. hc->new_map = NULL;
  1069. }
  1070. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  1071. __dev_status(hc->md, param);
  1072. md = hc->md;
  1073. up_write(&_hash_lock);
  1074. dm_put(md);
  1075. return 0;
  1076. }
  1077. /*
  1078. * Retrieves a list of devices used by a particular dm device.
  1079. */
  1080. static void retrieve_deps(struct dm_table *table,
  1081. struct dm_ioctl *param, size_t param_size)
  1082. {
  1083. unsigned int count = 0;
  1084. struct list_head *tmp;
  1085. size_t len, needed;
  1086. struct dm_dev_internal *dd;
  1087. struct dm_target_deps *deps;
  1088. deps = get_result_buffer(param, param_size, &len);
  1089. /*
  1090. * Count the devices.
  1091. */
  1092. list_for_each (tmp, dm_table_get_devices(table))
  1093. count++;
  1094. /*
  1095. * Check we have enough space.
  1096. */
  1097. needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
  1098. if (len < needed) {
  1099. param->flags |= DM_BUFFER_FULL_FLAG;
  1100. return;
  1101. }
  1102. /*
  1103. * Fill in the devices.
  1104. */
  1105. deps->count = count;
  1106. count = 0;
  1107. list_for_each_entry (dd, dm_table_get_devices(table), list)
  1108. deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
  1109. param->data_size = param->data_start + needed;
  1110. }
  1111. static int table_deps(struct dm_ioctl *param, size_t param_size)
  1112. {
  1113. struct mapped_device *md;
  1114. struct dm_table *table;
  1115. md = find_device(param);
  1116. if (!md)
  1117. return -ENXIO;
  1118. __dev_status(md, param);
  1119. table = dm_get_live_or_inactive_table(md, param);
  1120. if (table) {
  1121. retrieve_deps(table, param, param_size);
  1122. dm_table_put(table);
  1123. }
  1124. dm_put(md);
  1125. return 0;
  1126. }
  1127. /*
  1128. * Return the status of a device as a text string for each
  1129. * target.
  1130. */
  1131. static int table_status(struct dm_ioctl *param, size_t param_size)
  1132. {
  1133. struct mapped_device *md;
  1134. struct dm_table *table;
  1135. md = find_device(param);
  1136. if (!md)
  1137. return -ENXIO;
  1138. __dev_status(md, param);
  1139. table = dm_get_live_or_inactive_table(md, param);
  1140. if (table) {
  1141. retrieve_status(table, param, param_size);
  1142. dm_table_put(table);
  1143. }
  1144. dm_put(md);
  1145. return 0;
  1146. }
  1147. /*
  1148. * Pass a message to the target that's at the supplied device offset.
  1149. */
  1150. static int target_message(struct dm_ioctl *param, size_t param_size)
  1151. {
  1152. int r, argc;
  1153. char **argv;
  1154. struct mapped_device *md;
  1155. struct dm_table *table;
  1156. struct dm_target *ti;
  1157. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  1158. md = find_device(param);
  1159. if (!md)
  1160. return -ENXIO;
  1161. if (tmsg < (struct dm_target_msg *) param->data ||
  1162. invalid_str(tmsg->message, (void *) param + param_size)) {
  1163. DMWARN("Invalid target message parameters.");
  1164. r = -EINVAL;
  1165. goto out;
  1166. }
  1167. r = dm_split_args(&argc, &argv, tmsg->message);
  1168. if (r) {
  1169. DMWARN("Failed to split target message parameters");
  1170. goto out;
  1171. }
  1172. if (!argc) {
  1173. DMWARN("Empty message received.");
  1174. goto out_argv;
  1175. }
  1176. table = dm_get_live_table(md);
  1177. if (!table)
  1178. goto out_argv;
  1179. if (dm_deleting_md(md)) {
  1180. r = -ENXIO;
  1181. goto out_table;
  1182. }
  1183. ti = dm_table_find_target(table, tmsg->sector);
  1184. if (!dm_target_is_valid(ti)) {
  1185. DMWARN("Target message sector outside device.");
  1186. r = -EINVAL;
  1187. } else if (ti->type->message)
  1188. r = ti->type->message(ti, argc, argv);
  1189. else {
  1190. DMWARN("Target type does not support messages");
  1191. r = -EINVAL;
  1192. }
  1193. out_table:
  1194. dm_table_put(table);
  1195. out_argv:
  1196. kfree(argv);
  1197. out:
  1198. param->data_size = 0;
  1199. dm_put(md);
  1200. return r;
  1201. }
  1202. /*-----------------------------------------------------------------
  1203. * Implementation of open/close/ioctl on the special char
  1204. * device.
  1205. *---------------------------------------------------------------*/
  1206. static ioctl_fn lookup_ioctl(unsigned int cmd)
  1207. {
  1208. static struct {
  1209. int cmd;
  1210. ioctl_fn fn;
  1211. } _ioctls[] = {
  1212. {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
  1213. {DM_REMOVE_ALL_CMD, remove_all},
  1214. {DM_LIST_DEVICES_CMD, list_devices},
  1215. {DM_DEV_CREATE_CMD, dev_create},
  1216. {DM_DEV_REMOVE_CMD, dev_remove},
  1217. {DM_DEV_RENAME_CMD, dev_rename},
  1218. {DM_DEV_SUSPEND_CMD, dev_suspend},
  1219. {DM_DEV_STATUS_CMD, dev_status},
  1220. {DM_DEV_WAIT_CMD, dev_wait},
  1221. {DM_TABLE_LOAD_CMD, table_load},
  1222. {DM_TABLE_CLEAR_CMD, table_clear},
  1223. {DM_TABLE_DEPS_CMD, table_deps},
  1224. {DM_TABLE_STATUS_CMD, table_status},
  1225. {DM_LIST_VERSIONS_CMD, list_versions},
  1226. {DM_TARGET_MSG_CMD, target_message},
  1227. {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
  1228. };
  1229. return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
  1230. }
  1231. /*
  1232. * As well as checking the version compatibility this always
  1233. * copies the kernel interface version out.
  1234. */
  1235. static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
  1236. {
  1237. uint32_t version[3];
  1238. int r = 0;
  1239. if (copy_from_user(version, user->version, sizeof(version)))
  1240. return -EFAULT;
  1241. if ((DM_VERSION_MAJOR != version[0]) ||
  1242. (DM_VERSION_MINOR < version[1])) {
  1243. DMWARN("ioctl interface mismatch: "
  1244. "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1245. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1246. DM_VERSION_PATCHLEVEL,
  1247. version[0], version[1], version[2], cmd);
  1248. r = -EINVAL;
  1249. }
  1250. /*
  1251. * Fill in the kernel version.
  1252. */
  1253. version[0] = DM_VERSION_MAJOR;
  1254. version[1] = DM_VERSION_MINOR;
  1255. version[2] = DM_VERSION_PATCHLEVEL;
  1256. if (copy_to_user(user->version, version, sizeof(version)))
  1257. return -EFAULT;
  1258. return r;
  1259. }
  1260. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
  1261. {
  1262. struct dm_ioctl tmp, *dmi;
  1263. int secure_data;
  1264. if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
  1265. return -EFAULT;
  1266. if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
  1267. return -EINVAL;
  1268. secure_data = tmp.flags & DM_SECURE_DATA_FLAG;
  1269. dmi = vmalloc(tmp.data_size);
  1270. if (!dmi) {
  1271. if (secure_data && clear_user(user, tmp.data_size))
  1272. return -EFAULT;
  1273. return -ENOMEM;
  1274. }
  1275. if (copy_from_user(dmi, user, tmp.data_size))
  1276. goto bad;
  1277. /* Wipe the user buffer so we do not return it to userspace */
  1278. if (secure_data && clear_user(user, tmp.data_size))
  1279. goto bad;
  1280. *param = dmi;
  1281. return 0;
  1282. bad:
  1283. if (secure_data)
  1284. memset(dmi, 0, tmp.data_size);
  1285. vfree(dmi);
  1286. return -EFAULT;
  1287. }
  1288. static int validate_params(uint cmd, struct dm_ioctl *param)
  1289. {
  1290. /* Always clear this flag */
  1291. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1292. param->flags &= ~DM_UEVENT_GENERATED_FLAG;
  1293. param->flags &= ~DM_SECURE_DATA_FLAG;
  1294. /* Ignores parameters */
  1295. if (cmd == DM_REMOVE_ALL_CMD ||
  1296. cmd == DM_LIST_DEVICES_CMD ||
  1297. cmd == DM_LIST_VERSIONS_CMD)
  1298. return 0;
  1299. if ((cmd == DM_DEV_CREATE_CMD)) {
  1300. if (!*param->name) {
  1301. DMWARN("name not supplied when creating device");
  1302. return -EINVAL;
  1303. }
  1304. } else if ((*param->uuid && *param->name)) {
  1305. DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
  1306. return -EINVAL;
  1307. }
  1308. /* Ensure strings are terminated */
  1309. param->name[DM_NAME_LEN - 1] = '\0';
  1310. param->uuid[DM_UUID_LEN - 1] = '\0';
  1311. return 0;
  1312. }
  1313. static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
  1314. {
  1315. int r = 0;
  1316. int wipe_buffer;
  1317. unsigned int cmd;
  1318. struct dm_ioctl *uninitialized_var(param);
  1319. ioctl_fn fn = NULL;
  1320. size_t input_param_size;
  1321. /* only root can play with this */
  1322. if (!capable(CAP_SYS_ADMIN))
  1323. return -EACCES;
  1324. if (_IOC_TYPE(command) != DM_IOCTL)
  1325. return -ENOTTY;
  1326. cmd = _IOC_NR(command);
  1327. /*
  1328. * Check the interface version passed in. This also
  1329. * writes out the kernel's interface version.
  1330. */
  1331. r = check_version(cmd, user);
  1332. if (r)
  1333. return r;
  1334. /*
  1335. * Nothing more to do for the version command.
  1336. */
  1337. if (cmd == DM_VERSION_CMD)
  1338. return 0;
  1339. fn = lookup_ioctl(cmd);
  1340. if (!fn) {
  1341. DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
  1342. return -ENOTTY;
  1343. }
  1344. /*
  1345. * Trying to avoid low memory issues when a device is
  1346. * suspended.
  1347. */
  1348. current->flags |= PF_MEMALLOC;
  1349. /*
  1350. * Copy the parameters into kernel space.
  1351. */
  1352. r = copy_params(user, &param);
  1353. current->flags &= ~PF_MEMALLOC;
  1354. if (r)
  1355. return r;
  1356. input_param_size = param->data_size;
  1357. wipe_buffer = param->flags & DM_SECURE_DATA_FLAG;
  1358. r = validate_params(cmd, param);
  1359. if (r)
  1360. goto out;
  1361. param->data_size = sizeof(*param);
  1362. r = fn(param, input_param_size);
  1363. /*
  1364. * Copy the results back to userland.
  1365. */
  1366. if (!r && copy_to_user(user, param, param->data_size))
  1367. r = -EFAULT;
  1368. out:
  1369. if (wipe_buffer)
  1370. memset(param, 0, input_param_size);
  1371. vfree(param);
  1372. return r;
  1373. }
  1374. static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
  1375. {
  1376. return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
  1377. }
  1378. #ifdef CONFIG_COMPAT
  1379. static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
  1380. {
  1381. return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
  1382. }
  1383. #else
  1384. #define dm_compat_ctl_ioctl NULL
  1385. #endif
  1386. static const struct file_operations _ctl_fops = {
  1387. .open = nonseekable_open,
  1388. .unlocked_ioctl = dm_ctl_ioctl,
  1389. .compat_ioctl = dm_compat_ctl_ioctl,
  1390. .owner = THIS_MODULE,
  1391. .llseek = noop_llseek,
  1392. };
  1393. static struct miscdevice _dm_misc = {
  1394. .minor = MAPPER_CTRL_MINOR,
  1395. .name = DM_NAME,
  1396. .nodename = DM_DIR "/" DM_CONTROL_NODE,
  1397. .fops = &_ctl_fops
  1398. };
  1399. MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
  1400. MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
  1401. /*
  1402. * Create misc character device and link to DM_DIR/control.
  1403. */
  1404. int __init dm_interface_init(void)
  1405. {
  1406. int r;
  1407. r = dm_hash_init();
  1408. if (r)
  1409. return r;
  1410. r = misc_register(&_dm_misc);
  1411. if (r) {
  1412. DMERR("misc_register failed for control device");
  1413. dm_hash_exit();
  1414. return r;
  1415. }
  1416. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1417. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1418. DM_DRIVER_EMAIL);
  1419. return 0;
  1420. }
  1421. void dm_interface_exit(void)
  1422. {
  1423. if (misc_deregister(&_dm_misc) < 0)
  1424. DMERR("misc_deregister failed for control device");
  1425. dm_hash_exit();
  1426. }
  1427. /**
  1428. * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
  1429. * @md: Pointer to mapped_device
  1430. * @name: Buffer (size DM_NAME_LEN) for name
  1431. * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
  1432. */
  1433. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
  1434. {
  1435. int r = 0;
  1436. struct hash_cell *hc;
  1437. if (!md)
  1438. return -ENXIO;
  1439. mutex_lock(&dm_hash_cells_mutex);
  1440. hc = dm_get_mdptr(md);
  1441. if (!hc || hc->md != md) {
  1442. r = -ENXIO;
  1443. goto out;
  1444. }
  1445. if (name)
  1446. strcpy(name, hc->name);
  1447. if (uuid)
  1448. strcpy(uuid, hc->uuid ? : "");
  1449. out:
  1450. mutex_unlock(&dm_hash_cells_mutex);
  1451. return r;
  1452. }