dm-ioctl.c 34 KB

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