dm-ioctl.c 35 KB

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