dm-ioctl.c 42 KB

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