dm-thin.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428
  1. /*
  2. * Copyright (C) 2011 Red Hat UK.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-thin-metadata.h"
  7. #include <linux/device-mapper.h>
  8. #include <linux/dm-io.h>
  9. #include <linux/dm-kcopyd.h>
  10. #include <linux/list.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #define DM_MSG_PREFIX "thin"
  15. /*
  16. * Tunable constants
  17. */
  18. #define ENDIO_HOOK_POOL_SIZE 10240
  19. #define DEFERRED_SET_SIZE 64
  20. #define MAPPING_POOL_SIZE 1024
  21. #define PRISON_CELLS 1024
  22. /*
  23. * The block size of the device holding pool data must be
  24. * between 64KB and 1GB.
  25. */
  26. #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
  27. #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
  28. /*
  29. * The metadata device is currently limited in size. The limitation is
  30. * checked lower down in dm-space-map-metadata, but we also check it here
  31. * so we can fail early.
  32. *
  33. * We have one block of index, which can hold 255 index entries. Each
  34. * index entry contains allocation info about 16k metadata blocks.
  35. */
  36. #define METADATA_DEV_MAX_SECTORS (255 * (1 << 14) * (THIN_METADATA_BLOCK_SIZE / (1 << SECTOR_SHIFT)))
  37. /*
  38. * Device id is restricted to 24 bits.
  39. */
  40. #define MAX_DEV_ID ((1 << 24) - 1)
  41. /*
  42. * How do we handle breaking sharing of data blocks?
  43. * =================================================
  44. *
  45. * We use a standard copy-on-write btree to store the mappings for the
  46. * devices (note I'm talking about copy-on-write of the metadata here, not
  47. * the data). When you take an internal snapshot you clone the root node
  48. * of the origin btree. After this there is no concept of an origin or a
  49. * snapshot. They are just two device trees that happen to point to the
  50. * same data blocks.
  51. *
  52. * When we get a write in we decide if it's to a shared data block using
  53. * some timestamp magic. If it is, we have to break sharing.
  54. *
  55. * Let's say we write to a shared block in what was the origin. The
  56. * steps are:
  57. *
  58. * i) plug io further to this physical block. (see bio_prison code).
  59. *
  60. * ii) quiesce any read io to that shared data block. Obviously
  61. * including all devices that share this block. (see deferred_set code)
  62. *
  63. * iii) copy the data block to a newly allocate block. This step can be
  64. * missed out if the io covers the block. (schedule_copy).
  65. *
  66. * iv) insert the new mapping into the origin's btree
  67. * (process_prepared_mappings). This act of inserting breaks some
  68. * sharing of btree nodes between the two devices. Breaking sharing only
  69. * effects the btree of that specific device. Btrees for the other
  70. * devices that share the block never change. The btree for the origin
  71. * device as it was after the last commit is untouched, ie. we're using
  72. * persistent data structures in the functional programming sense.
  73. *
  74. * v) unplug io to this physical block, including the io that triggered
  75. * the breaking of sharing.
  76. *
  77. * Steps (ii) and (iii) occur in parallel.
  78. *
  79. * The metadata _doesn't_ need to be committed before the io continues. We
  80. * get away with this because the io is always written to a _new_ block.
  81. * If there's a crash, then:
  82. *
  83. * - The origin mapping will point to the old origin block (the shared
  84. * one). This will contain the data as it was before the io that triggered
  85. * the breaking of sharing came in.
  86. *
  87. * - The snap mapping still points to the old block. As it would after
  88. * the commit.
  89. *
  90. * The downside of this scheme is the timestamp magic isn't perfect, and
  91. * will continue to think that data block in the snapshot device is shared
  92. * even after the write to the origin has broken sharing. I suspect data
  93. * blocks will typically be shared by many different devices, so we're
  94. * breaking sharing n + 1 times, rather than n, where n is the number of
  95. * devices that reference this data block. At the moment I think the
  96. * benefits far, far outweigh the disadvantages.
  97. */
  98. /*----------------------------------------------------------------*/
  99. /*
  100. * Sometimes we can't deal with a bio straight away. We put them in prison
  101. * where they can't cause any mischief. Bios are put in a cell identified
  102. * by a key, multiple bios can be in the same cell. When the cell is
  103. * subsequently unlocked the bios become available.
  104. */
  105. struct bio_prison;
  106. struct cell_key {
  107. int virtual;
  108. dm_thin_id dev;
  109. dm_block_t block;
  110. };
  111. struct cell {
  112. struct hlist_node list;
  113. struct bio_prison *prison;
  114. struct cell_key key;
  115. unsigned count;
  116. struct bio_list bios;
  117. };
  118. struct bio_prison {
  119. spinlock_t lock;
  120. mempool_t *cell_pool;
  121. unsigned nr_buckets;
  122. unsigned hash_mask;
  123. struct hlist_head *cells;
  124. };
  125. static uint32_t calc_nr_buckets(unsigned nr_cells)
  126. {
  127. uint32_t n = 128;
  128. nr_cells /= 4;
  129. nr_cells = min(nr_cells, 8192u);
  130. while (n < nr_cells)
  131. n <<= 1;
  132. return n;
  133. }
  134. /*
  135. * @nr_cells should be the number of cells you want in use _concurrently_.
  136. * Don't confuse it with the number of distinct keys.
  137. */
  138. static struct bio_prison *prison_create(unsigned nr_cells)
  139. {
  140. unsigned i;
  141. uint32_t nr_buckets = calc_nr_buckets(nr_cells);
  142. size_t len = sizeof(struct bio_prison) +
  143. (sizeof(struct hlist_head) * nr_buckets);
  144. struct bio_prison *prison = kmalloc(len, GFP_KERNEL);
  145. if (!prison)
  146. return NULL;
  147. spin_lock_init(&prison->lock);
  148. prison->cell_pool = mempool_create_kmalloc_pool(nr_cells,
  149. sizeof(struct cell));
  150. if (!prison->cell_pool) {
  151. kfree(prison);
  152. return NULL;
  153. }
  154. prison->nr_buckets = nr_buckets;
  155. prison->hash_mask = nr_buckets - 1;
  156. prison->cells = (struct hlist_head *) (prison + 1);
  157. for (i = 0; i < nr_buckets; i++)
  158. INIT_HLIST_HEAD(prison->cells + i);
  159. return prison;
  160. }
  161. static void prison_destroy(struct bio_prison *prison)
  162. {
  163. mempool_destroy(prison->cell_pool);
  164. kfree(prison);
  165. }
  166. static uint32_t hash_key(struct bio_prison *prison, struct cell_key *key)
  167. {
  168. const unsigned long BIG_PRIME = 4294967291UL;
  169. uint64_t hash = key->block * BIG_PRIME;
  170. return (uint32_t) (hash & prison->hash_mask);
  171. }
  172. static int keys_equal(struct cell_key *lhs, struct cell_key *rhs)
  173. {
  174. return (lhs->virtual == rhs->virtual) &&
  175. (lhs->dev == rhs->dev) &&
  176. (lhs->block == rhs->block);
  177. }
  178. static struct cell *__search_bucket(struct hlist_head *bucket,
  179. struct cell_key *key)
  180. {
  181. struct cell *cell;
  182. struct hlist_node *tmp;
  183. hlist_for_each_entry(cell, tmp, bucket, list)
  184. if (keys_equal(&cell->key, key))
  185. return cell;
  186. return NULL;
  187. }
  188. /*
  189. * This may block if a new cell needs allocating. You must ensure that
  190. * cells will be unlocked even if the calling thread is blocked.
  191. *
  192. * Returns the number of entries in the cell prior to the new addition
  193. * or < 0 on failure.
  194. */
  195. static int bio_detain(struct bio_prison *prison, struct cell_key *key,
  196. struct bio *inmate, struct cell **ref)
  197. {
  198. int r;
  199. unsigned long flags;
  200. uint32_t hash = hash_key(prison, key);
  201. struct cell *uninitialized_var(cell), *cell2 = NULL;
  202. BUG_ON(hash > prison->nr_buckets);
  203. spin_lock_irqsave(&prison->lock, flags);
  204. cell = __search_bucket(prison->cells + hash, key);
  205. if (!cell) {
  206. /*
  207. * Allocate a new cell
  208. */
  209. spin_unlock_irqrestore(&prison->lock, flags);
  210. cell2 = mempool_alloc(prison->cell_pool, GFP_NOIO);
  211. spin_lock_irqsave(&prison->lock, flags);
  212. /*
  213. * We've been unlocked, so we have to double check that
  214. * nobody else has inserted this cell in the meantime.
  215. */
  216. cell = __search_bucket(prison->cells + hash, key);
  217. if (!cell) {
  218. cell = cell2;
  219. cell2 = NULL;
  220. cell->prison = prison;
  221. memcpy(&cell->key, key, sizeof(cell->key));
  222. cell->count = 0;
  223. bio_list_init(&cell->bios);
  224. hlist_add_head(&cell->list, prison->cells + hash);
  225. }
  226. }
  227. r = cell->count++;
  228. bio_list_add(&cell->bios, inmate);
  229. spin_unlock_irqrestore(&prison->lock, flags);
  230. if (cell2)
  231. mempool_free(cell2, prison->cell_pool);
  232. *ref = cell;
  233. return r;
  234. }
  235. /*
  236. * @inmates must have been initialised prior to this call
  237. */
  238. static void __cell_release(struct cell *cell, struct bio_list *inmates)
  239. {
  240. struct bio_prison *prison = cell->prison;
  241. hlist_del(&cell->list);
  242. if (inmates)
  243. bio_list_merge(inmates, &cell->bios);
  244. mempool_free(cell, prison->cell_pool);
  245. }
  246. static void cell_release(struct cell *cell, struct bio_list *bios)
  247. {
  248. unsigned long flags;
  249. struct bio_prison *prison = cell->prison;
  250. spin_lock_irqsave(&prison->lock, flags);
  251. __cell_release(cell, bios);
  252. spin_unlock_irqrestore(&prison->lock, flags);
  253. }
  254. /*
  255. * There are a couple of places where we put a bio into a cell briefly
  256. * before taking it out again. In these situations we know that no other
  257. * bio may be in the cell. This function releases the cell, and also does
  258. * a sanity check.
  259. */
  260. static void cell_release_singleton(struct cell *cell, struct bio *bio)
  261. {
  262. struct bio_prison *prison = cell->prison;
  263. struct bio_list bios;
  264. struct bio *b;
  265. unsigned long flags;
  266. bio_list_init(&bios);
  267. spin_lock_irqsave(&prison->lock, flags);
  268. __cell_release(cell, &bios);
  269. spin_unlock_irqrestore(&prison->lock, flags);
  270. b = bio_list_pop(&bios);
  271. BUG_ON(b != bio);
  272. BUG_ON(!bio_list_empty(&bios));
  273. }
  274. static void cell_error(struct cell *cell)
  275. {
  276. struct bio_prison *prison = cell->prison;
  277. struct bio_list bios;
  278. struct bio *bio;
  279. unsigned long flags;
  280. bio_list_init(&bios);
  281. spin_lock_irqsave(&prison->lock, flags);
  282. __cell_release(cell, &bios);
  283. spin_unlock_irqrestore(&prison->lock, flags);
  284. while ((bio = bio_list_pop(&bios)))
  285. bio_io_error(bio);
  286. }
  287. /*----------------------------------------------------------------*/
  288. /*
  289. * We use the deferred set to keep track of pending reads to shared blocks.
  290. * We do this to ensure the new mapping caused by a write isn't performed
  291. * until these prior reads have completed. Otherwise the insertion of the
  292. * new mapping could free the old block that the read bios are mapped to.
  293. */
  294. struct deferred_set;
  295. struct deferred_entry {
  296. struct deferred_set *ds;
  297. unsigned count;
  298. struct list_head work_items;
  299. };
  300. struct deferred_set {
  301. spinlock_t lock;
  302. unsigned current_entry;
  303. unsigned sweeper;
  304. struct deferred_entry entries[DEFERRED_SET_SIZE];
  305. };
  306. static void ds_init(struct deferred_set *ds)
  307. {
  308. int i;
  309. spin_lock_init(&ds->lock);
  310. ds->current_entry = 0;
  311. ds->sweeper = 0;
  312. for (i = 0; i < DEFERRED_SET_SIZE; i++) {
  313. ds->entries[i].ds = ds;
  314. ds->entries[i].count = 0;
  315. INIT_LIST_HEAD(&ds->entries[i].work_items);
  316. }
  317. }
  318. static struct deferred_entry *ds_inc(struct deferred_set *ds)
  319. {
  320. unsigned long flags;
  321. struct deferred_entry *entry;
  322. spin_lock_irqsave(&ds->lock, flags);
  323. entry = ds->entries + ds->current_entry;
  324. entry->count++;
  325. spin_unlock_irqrestore(&ds->lock, flags);
  326. return entry;
  327. }
  328. static unsigned ds_next(unsigned index)
  329. {
  330. return (index + 1) % DEFERRED_SET_SIZE;
  331. }
  332. static void __sweep(struct deferred_set *ds, struct list_head *head)
  333. {
  334. while ((ds->sweeper != ds->current_entry) &&
  335. !ds->entries[ds->sweeper].count) {
  336. list_splice_init(&ds->entries[ds->sweeper].work_items, head);
  337. ds->sweeper = ds_next(ds->sweeper);
  338. }
  339. if ((ds->sweeper == ds->current_entry) && !ds->entries[ds->sweeper].count)
  340. list_splice_init(&ds->entries[ds->sweeper].work_items, head);
  341. }
  342. static void ds_dec(struct deferred_entry *entry, struct list_head *head)
  343. {
  344. unsigned long flags;
  345. spin_lock_irqsave(&entry->ds->lock, flags);
  346. BUG_ON(!entry->count);
  347. --entry->count;
  348. __sweep(entry->ds, head);
  349. spin_unlock_irqrestore(&entry->ds->lock, flags);
  350. }
  351. /*
  352. * Returns 1 if deferred or 0 if no pending items to delay job.
  353. */
  354. static int ds_add_work(struct deferred_set *ds, struct list_head *work)
  355. {
  356. int r = 1;
  357. unsigned long flags;
  358. unsigned next_entry;
  359. spin_lock_irqsave(&ds->lock, flags);
  360. if ((ds->sweeper == ds->current_entry) &&
  361. !ds->entries[ds->current_entry].count)
  362. r = 0;
  363. else {
  364. list_add(work, &ds->entries[ds->current_entry].work_items);
  365. next_entry = ds_next(ds->current_entry);
  366. if (!ds->entries[next_entry].count)
  367. ds->current_entry = next_entry;
  368. }
  369. spin_unlock_irqrestore(&ds->lock, flags);
  370. return r;
  371. }
  372. /*----------------------------------------------------------------*/
  373. /*
  374. * Key building.
  375. */
  376. static void build_data_key(struct dm_thin_device *td,
  377. dm_block_t b, struct cell_key *key)
  378. {
  379. key->virtual = 0;
  380. key->dev = dm_thin_dev_id(td);
  381. key->block = b;
  382. }
  383. static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
  384. struct cell_key *key)
  385. {
  386. key->virtual = 1;
  387. key->dev = dm_thin_dev_id(td);
  388. key->block = b;
  389. }
  390. /*----------------------------------------------------------------*/
  391. /*
  392. * A pool device ties together a metadata device and a data device. It
  393. * also provides the interface for creating and destroying internal
  394. * devices.
  395. */
  396. struct new_mapping;
  397. struct pool {
  398. struct list_head list;
  399. struct dm_target *ti; /* Only set if a pool target is bound */
  400. struct mapped_device *pool_md;
  401. struct block_device *md_dev;
  402. struct dm_pool_metadata *pmd;
  403. uint32_t sectors_per_block;
  404. unsigned block_shift;
  405. dm_block_t offset_mask;
  406. dm_block_t low_water_blocks;
  407. unsigned zero_new_blocks:1;
  408. unsigned low_water_triggered:1; /* A dm event has been sent */
  409. unsigned no_free_space:1; /* A -ENOSPC warning has been issued */
  410. struct bio_prison *prison;
  411. struct dm_kcopyd_client *copier;
  412. struct workqueue_struct *wq;
  413. struct work_struct worker;
  414. unsigned ref_count;
  415. spinlock_t lock;
  416. struct bio_list deferred_bios;
  417. struct bio_list deferred_flush_bios;
  418. struct list_head prepared_mappings;
  419. struct bio_list retry_on_resume_list;
  420. struct deferred_set ds; /* FIXME: move to thin_c */
  421. struct new_mapping *next_mapping;
  422. mempool_t *mapping_pool;
  423. mempool_t *endio_hook_pool;
  424. };
  425. /*
  426. * Target context for a pool.
  427. */
  428. struct pool_c {
  429. struct dm_target *ti;
  430. struct pool *pool;
  431. struct dm_dev *data_dev;
  432. struct dm_dev *metadata_dev;
  433. struct dm_target_callbacks callbacks;
  434. dm_block_t low_water_blocks;
  435. unsigned zero_new_blocks:1;
  436. };
  437. /*
  438. * Target context for a thin.
  439. */
  440. struct thin_c {
  441. struct dm_dev *pool_dev;
  442. dm_thin_id dev_id;
  443. struct pool *pool;
  444. struct dm_thin_device *td;
  445. };
  446. /*----------------------------------------------------------------*/
  447. /*
  448. * A global list of pools that uses a struct mapped_device as a key.
  449. */
  450. static struct dm_thin_pool_table {
  451. struct mutex mutex;
  452. struct list_head pools;
  453. } dm_thin_pool_table;
  454. static void pool_table_init(void)
  455. {
  456. mutex_init(&dm_thin_pool_table.mutex);
  457. INIT_LIST_HEAD(&dm_thin_pool_table.pools);
  458. }
  459. static void __pool_table_insert(struct pool *pool)
  460. {
  461. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  462. list_add(&pool->list, &dm_thin_pool_table.pools);
  463. }
  464. static void __pool_table_remove(struct pool *pool)
  465. {
  466. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  467. list_del(&pool->list);
  468. }
  469. static struct pool *__pool_table_lookup(struct mapped_device *md)
  470. {
  471. struct pool *pool = NULL, *tmp;
  472. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  473. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  474. if (tmp->pool_md == md) {
  475. pool = tmp;
  476. break;
  477. }
  478. }
  479. return pool;
  480. }
  481. static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
  482. {
  483. struct pool *pool = NULL, *tmp;
  484. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  485. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  486. if (tmp->md_dev == md_dev) {
  487. pool = tmp;
  488. break;
  489. }
  490. }
  491. return pool;
  492. }
  493. /*----------------------------------------------------------------*/
  494. static void __requeue_bio_list(struct thin_c *tc, struct bio_list *master)
  495. {
  496. struct bio *bio;
  497. struct bio_list bios;
  498. bio_list_init(&bios);
  499. bio_list_merge(&bios, master);
  500. bio_list_init(master);
  501. while ((bio = bio_list_pop(&bios))) {
  502. if (dm_get_mapinfo(bio)->ptr == tc)
  503. bio_endio(bio, DM_ENDIO_REQUEUE);
  504. else
  505. bio_list_add(master, bio);
  506. }
  507. }
  508. static void requeue_io(struct thin_c *tc)
  509. {
  510. struct pool *pool = tc->pool;
  511. unsigned long flags;
  512. spin_lock_irqsave(&pool->lock, flags);
  513. __requeue_bio_list(tc, &pool->deferred_bios);
  514. __requeue_bio_list(tc, &pool->retry_on_resume_list);
  515. spin_unlock_irqrestore(&pool->lock, flags);
  516. }
  517. /*
  518. * This section of code contains the logic for processing a thin device's IO.
  519. * Much of the code depends on pool object resources (lists, workqueues, etc)
  520. * but most is exclusively called from the thin target rather than the thin-pool
  521. * target.
  522. */
  523. static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
  524. {
  525. return bio->bi_sector >> tc->pool->block_shift;
  526. }
  527. static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
  528. {
  529. struct pool *pool = tc->pool;
  530. bio->bi_bdev = tc->pool_dev->bdev;
  531. bio->bi_sector = (block << pool->block_shift) +
  532. (bio->bi_sector & pool->offset_mask);
  533. }
  534. static void remap_and_issue(struct thin_c *tc, struct bio *bio,
  535. dm_block_t block)
  536. {
  537. struct pool *pool = tc->pool;
  538. unsigned long flags;
  539. remap(tc, bio, block);
  540. /*
  541. * Batch together any FUA/FLUSH bios we find and then issue
  542. * a single commit for them in process_deferred_bios().
  543. */
  544. if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
  545. spin_lock_irqsave(&pool->lock, flags);
  546. bio_list_add(&pool->deferred_flush_bios, bio);
  547. spin_unlock_irqrestore(&pool->lock, flags);
  548. } else
  549. generic_make_request(bio);
  550. }
  551. /*
  552. * wake_worker() is used when new work is queued and when pool_resume is
  553. * ready to continue deferred IO processing.
  554. */
  555. static void wake_worker(struct pool *pool)
  556. {
  557. queue_work(pool->wq, &pool->worker);
  558. }
  559. /*----------------------------------------------------------------*/
  560. /*
  561. * Bio endio functions.
  562. */
  563. struct endio_hook {
  564. struct thin_c *tc;
  565. bio_end_io_t *saved_bi_end_io;
  566. struct deferred_entry *entry;
  567. };
  568. struct new_mapping {
  569. struct list_head list;
  570. int prepared;
  571. struct thin_c *tc;
  572. dm_block_t virt_block;
  573. dm_block_t data_block;
  574. struct cell *cell;
  575. int err;
  576. /*
  577. * If the bio covers the whole area of a block then we can avoid
  578. * zeroing or copying. Instead this bio is hooked. The bio will
  579. * still be in the cell, so care has to be taken to avoid issuing
  580. * the bio twice.
  581. */
  582. struct bio *bio;
  583. bio_end_io_t *saved_bi_end_io;
  584. };
  585. static void __maybe_add_mapping(struct new_mapping *m)
  586. {
  587. struct pool *pool = m->tc->pool;
  588. if (list_empty(&m->list) && m->prepared) {
  589. list_add(&m->list, &pool->prepared_mappings);
  590. wake_worker(pool);
  591. }
  592. }
  593. static void copy_complete(int read_err, unsigned long write_err, void *context)
  594. {
  595. unsigned long flags;
  596. struct new_mapping *m = context;
  597. struct pool *pool = m->tc->pool;
  598. m->err = read_err || write_err ? -EIO : 0;
  599. spin_lock_irqsave(&pool->lock, flags);
  600. m->prepared = 1;
  601. __maybe_add_mapping(m);
  602. spin_unlock_irqrestore(&pool->lock, flags);
  603. }
  604. static void overwrite_endio(struct bio *bio, int err)
  605. {
  606. unsigned long flags;
  607. struct new_mapping *m = dm_get_mapinfo(bio)->ptr;
  608. struct pool *pool = m->tc->pool;
  609. m->err = err;
  610. spin_lock_irqsave(&pool->lock, flags);
  611. m->prepared = 1;
  612. __maybe_add_mapping(m);
  613. spin_unlock_irqrestore(&pool->lock, flags);
  614. }
  615. static void shared_read_endio(struct bio *bio, int err)
  616. {
  617. struct list_head mappings;
  618. struct new_mapping *m, *tmp;
  619. struct endio_hook *h = dm_get_mapinfo(bio)->ptr;
  620. unsigned long flags;
  621. struct pool *pool = h->tc->pool;
  622. bio->bi_end_io = h->saved_bi_end_io;
  623. bio_endio(bio, err);
  624. INIT_LIST_HEAD(&mappings);
  625. ds_dec(h->entry, &mappings);
  626. spin_lock_irqsave(&pool->lock, flags);
  627. list_for_each_entry_safe(m, tmp, &mappings, list) {
  628. list_del(&m->list);
  629. INIT_LIST_HEAD(&m->list);
  630. __maybe_add_mapping(m);
  631. }
  632. spin_unlock_irqrestore(&pool->lock, flags);
  633. mempool_free(h, pool->endio_hook_pool);
  634. }
  635. /*----------------------------------------------------------------*/
  636. /*
  637. * Workqueue.
  638. */
  639. /*
  640. * Prepared mapping jobs.
  641. */
  642. /*
  643. * This sends the bios in the cell back to the deferred_bios list.
  644. */
  645. static void cell_defer(struct thin_c *tc, struct cell *cell,
  646. dm_block_t data_block)
  647. {
  648. struct pool *pool = tc->pool;
  649. unsigned long flags;
  650. spin_lock_irqsave(&pool->lock, flags);
  651. cell_release(cell, &pool->deferred_bios);
  652. spin_unlock_irqrestore(&tc->pool->lock, flags);
  653. wake_worker(pool);
  654. }
  655. /*
  656. * Same as cell_defer above, except it omits one particular detainee,
  657. * a write bio that covers the block and has already been processed.
  658. */
  659. static void cell_defer_except(struct thin_c *tc, struct cell *cell,
  660. struct bio *exception)
  661. {
  662. struct bio_list bios;
  663. struct bio *bio;
  664. struct pool *pool = tc->pool;
  665. unsigned long flags;
  666. bio_list_init(&bios);
  667. cell_release(cell, &bios);
  668. spin_lock_irqsave(&pool->lock, flags);
  669. while ((bio = bio_list_pop(&bios)))
  670. if (bio != exception)
  671. bio_list_add(&pool->deferred_bios, bio);
  672. spin_unlock_irqrestore(&pool->lock, flags);
  673. wake_worker(pool);
  674. }
  675. static void process_prepared_mapping(struct new_mapping *m)
  676. {
  677. struct thin_c *tc = m->tc;
  678. struct bio *bio;
  679. int r;
  680. bio = m->bio;
  681. if (bio)
  682. bio->bi_end_io = m->saved_bi_end_io;
  683. if (m->err) {
  684. cell_error(m->cell);
  685. return;
  686. }
  687. /*
  688. * Commit the prepared block into the mapping btree.
  689. * Any I/O for this block arriving after this point will get
  690. * remapped to it directly.
  691. */
  692. r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
  693. if (r) {
  694. DMERR("dm_thin_insert_block() failed");
  695. cell_error(m->cell);
  696. return;
  697. }
  698. /*
  699. * Release any bios held while the block was being provisioned.
  700. * If we are processing a write bio that completely covers the block,
  701. * we already processed it so can ignore it now when processing
  702. * the bios in the cell.
  703. */
  704. if (bio) {
  705. cell_defer_except(tc, m->cell, bio);
  706. bio_endio(bio, 0);
  707. } else
  708. cell_defer(tc, m->cell, m->data_block);
  709. list_del(&m->list);
  710. mempool_free(m, tc->pool->mapping_pool);
  711. }
  712. static void process_prepared_mappings(struct pool *pool)
  713. {
  714. unsigned long flags;
  715. struct list_head maps;
  716. struct new_mapping *m, *tmp;
  717. INIT_LIST_HEAD(&maps);
  718. spin_lock_irqsave(&pool->lock, flags);
  719. list_splice_init(&pool->prepared_mappings, &maps);
  720. spin_unlock_irqrestore(&pool->lock, flags);
  721. list_for_each_entry_safe(m, tmp, &maps, list)
  722. process_prepared_mapping(m);
  723. }
  724. /*
  725. * Deferred bio jobs.
  726. */
  727. static int io_overwrites_block(struct pool *pool, struct bio *bio)
  728. {
  729. return ((bio_data_dir(bio) == WRITE) &&
  730. !(bio->bi_sector & pool->offset_mask)) &&
  731. (bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT));
  732. }
  733. static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
  734. bio_end_io_t *fn)
  735. {
  736. *save = bio->bi_end_io;
  737. bio->bi_end_io = fn;
  738. }
  739. static int ensure_next_mapping(struct pool *pool)
  740. {
  741. if (pool->next_mapping)
  742. return 0;
  743. pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
  744. return pool->next_mapping ? 0 : -ENOMEM;
  745. }
  746. static struct new_mapping *get_next_mapping(struct pool *pool)
  747. {
  748. struct new_mapping *r = pool->next_mapping;
  749. BUG_ON(!pool->next_mapping);
  750. pool->next_mapping = NULL;
  751. return r;
  752. }
  753. static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
  754. dm_block_t data_origin, dm_block_t data_dest,
  755. struct cell *cell, struct bio *bio)
  756. {
  757. int r;
  758. struct pool *pool = tc->pool;
  759. struct new_mapping *m = get_next_mapping(pool);
  760. INIT_LIST_HEAD(&m->list);
  761. m->prepared = 0;
  762. m->tc = tc;
  763. m->virt_block = virt_block;
  764. m->data_block = data_dest;
  765. m->cell = cell;
  766. m->err = 0;
  767. m->bio = NULL;
  768. ds_add_work(&pool->ds, &m->list);
  769. /*
  770. * IO to pool_dev remaps to the pool target's data_dev.
  771. *
  772. * If the whole block of data is being overwritten, we can issue the
  773. * bio immediately. Otherwise we use kcopyd to clone the data first.
  774. */
  775. if (io_overwrites_block(pool, bio)) {
  776. m->bio = bio;
  777. save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
  778. dm_get_mapinfo(bio)->ptr = m;
  779. remap_and_issue(tc, bio, data_dest);
  780. } else {
  781. struct dm_io_region from, to;
  782. from.bdev = tc->pool_dev->bdev;
  783. from.sector = data_origin * pool->sectors_per_block;
  784. from.count = pool->sectors_per_block;
  785. to.bdev = tc->pool_dev->bdev;
  786. to.sector = data_dest * pool->sectors_per_block;
  787. to.count = pool->sectors_per_block;
  788. r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
  789. 0, copy_complete, m);
  790. if (r < 0) {
  791. mempool_free(m, pool->mapping_pool);
  792. DMERR("dm_kcopyd_copy() failed");
  793. cell_error(cell);
  794. }
  795. }
  796. }
  797. static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
  798. dm_block_t data_block, struct cell *cell,
  799. struct bio *bio)
  800. {
  801. struct pool *pool = tc->pool;
  802. struct new_mapping *m = get_next_mapping(pool);
  803. INIT_LIST_HEAD(&m->list);
  804. m->prepared = 0;
  805. m->tc = tc;
  806. m->virt_block = virt_block;
  807. m->data_block = data_block;
  808. m->cell = cell;
  809. m->err = 0;
  810. m->bio = NULL;
  811. /*
  812. * If the whole block of data is being overwritten or we are not
  813. * zeroing pre-existing data, we can issue the bio immediately.
  814. * Otherwise we use kcopyd to zero the data first.
  815. */
  816. if (!pool->zero_new_blocks)
  817. process_prepared_mapping(m);
  818. else if (io_overwrites_block(pool, bio)) {
  819. m->bio = bio;
  820. save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
  821. dm_get_mapinfo(bio)->ptr = m;
  822. remap_and_issue(tc, bio, data_block);
  823. } else {
  824. int r;
  825. struct dm_io_region to;
  826. to.bdev = tc->pool_dev->bdev;
  827. to.sector = data_block * pool->sectors_per_block;
  828. to.count = pool->sectors_per_block;
  829. r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
  830. if (r < 0) {
  831. mempool_free(m, pool->mapping_pool);
  832. DMERR("dm_kcopyd_zero() failed");
  833. cell_error(cell);
  834. }
  835. }
  836. }
  837. static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
  838. {
  839. int r;
  840. dm_block_t free_blocks;
  841. unsigned long flags;
  842. struct pool *pool = tc->pool;
  843. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  844. if (r)
  845. return r;
  846. if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
  847. DMWARN("%s: reached low water mark, sending event.",
  848. dm_device_name(pool->pool_md));
  849. spin_lock_irqsave(&pool->lock, flags);
  850. pool->low_water_triggered = 1;
  851. spin_unlock_irqrestore(&pool->lock, flags);
  852. dm_table_event(pool->ti->table);
  853. }
  854. if (!free_blocks) {
  855. if (pool->no_free_space)
  856. return -ENOSPC;
  857. else {
  858. /*
  859. * Try to commit to see if that will free up some
  860. * more space.
  861. */
  862. r = dm_pool_commit_metadata(pool->pmd);
  863. if (r) {
  864. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  865. __func__, r);
  866. return r;
  867. }
  868. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  869. if (r)
  870. return r;
  871. /*
  872. * If we still have no space we set a flag to avoid
  873. * doing all this checking and return -ENOSPC.
  874. */
  875. if (!free_blocks) {
  876. DMWARN("%s: no free space available.",
  877. dm_device_name(pool->pool_md));
  878. spin_lock_irqsave(&pool->lock, flags);
  879. pool->no_free_space = 1;
  880. spin_unlock_irqrestore(&pool->lock, flags);
  881. return -ENOSPC;
  882. }
  883. }
  884. }
  885. r = dm_pool_alloc_data_block(pool->pmd, result);
  886. if (r)
  887. return r;
  888. return 0;
  889. }
  890. /*
  891. * If we have run out of space, queue bios until the device is
  892. * resumed, presumably after having been reloaded with more space.
  893. */
  894. static void retry_on_resume(struct bio *bio)
  895. {
  896. struct thin_c *tc = dm_get_mapinfo(bio)->ptr;
  897. struct pool *pool = tc->pool;
  898. unsigned long flags;
  899. spin_lock_irqsave(&pool->lock, flags);
  900. bio_list_add(&pool->retry_on_resume_list, bio);
  901. spin_unlock_irqrestore(&pool->lock, flags);
  902. }
  903. static void no_space(struct cell *cell)
  904. {
  905. struct bio *bio;
  906. struct bio_list bios;
  907. bio_list_init(&bios);
  908. cell_release(cell, &bios);
  909. while ((bio = bio_list_pop(&bios)))
  910. retry_on_resume(bio);
  911. }
  912. static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
  913. struct cell_key *key,
  914. struct dm_thin_lookup_result *lookup_result,
  915. struct cell *cell)
  916. {
  917. int r;
  918. dm_block_t data_block;
  919. r = alloc_data_block(tc, &data_block);
  920. switch (r) {
  921. case 0:
  922. schedule_copy(tc, block, lookup_result->block,
  923. data_block, cell, bio);
  924. break;
  925. case -ENOSPC:
  926. no_space(cell);
  927. break;
  928. default:
  929. DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
  930. cell_error(cell);
  931. break;
  932. }
  933. }
  934. static void process_shared_bio(struct thin_c *tc, struct bio *bio,
  935. dm_block_t block,
  936. struct dm_thin_lookup_result *lookup_result)
  937. {
  938. struct cell *cell;
  939. struct pool *pool = tc->pool;
  940. struct cell_key key;
  941. /*
  942. * If cell is already occupied, then sharing is already in the process
  943. * of being broken so we have nothing further to do here.
  944. */
  945. build_data_key(tc->td, lookup_result->block, &key);
  946. if (bio_detain(pool->prison, &key, bio, &cell))
  947. return;
  948. if (bio_data_dir(bio) == WRITE)
  949. break_sharing(tc, bio, block, &key, lookup_result, cell);
  950. else {
  951. struct endio_hook *h;
  952. h = mempool_alloc(pool->endio_hook_pool, GFP_NOIO);
  953. h->tc = tc;
  954. h->entry = ds_inc(&pool->ds);
  955. save_and_set_endio(bio, &h->saved_bi_end_io, shared_read_endio);
  956. dm_get_mapinfo(bio)->ptr = h;
  957. cell_release_singleton(cell, bio);
  958. remap_and_issue(tc, bio, lookup_result->block);
  959. }
  960. }
  961. static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
  962. struct cell *cell)
  963. {
  964. int r;
  965. dm_block_t data_block;
  966. /*
  967. * Remap empty bios (flushes) immediately, without provisioning.
  968. */
  969. if (!bio->bi_size) {
  970. cell_release_singleton(cell, bio);
  971. remap_and_issue(tc, bio, 0);
  972. return;
  973. }
  974. /*
  975. * Fill read bios with zeroes and complete them immediately.
  976. */
  977. if (bio_data_dir(bio) == READ) {
  978. zero_fill_bio(bio);
  979. cell_release_singleton(cell, bio);
  980. bio_endio(bio, 0);
  981. return;
  982. }
  983. r = alloc_data_block(tc, &data_block);
  984. switch (r) {
  985. case 0:
  986. schedule_zero(tc, block, data_block, cell, bio);
  987. break;
  988. case -ENOSPC:
  989. no_space(cell);
  990. break;
  991. default:
  992. DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
  993. cell_error(cell);
  994. break;
  995. }
  996. }
  997. static void process_bio(struct thin_c *tc, struct bio *bio)
  998. {
  999. int r;
  1000. dm_block_t block = get_bio_block(tc, bio);
  1001. struct cell *cell;
  1002. struct cell_key key;
  1003. struct dm_thin_lookup_result lookup_result;
  1004. /*
  1005. * If cell is already occupied, then the block is already
  1006. * being provisioned so we have nothing further to do here.
  1007. */
  1008. build_virtual_key(tc->td, block, &key);
  1009. if (bio_detain(tc->pool->prison, &key, bio, &cell))
  1010. return;
  1011. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1012. switch (r) {
  1013. case 0:
  1014. /*
  1015. * We can release this cell now. This thread is the only
  1016. * one that puts bios into a cell, and we know there were
  1017. * no preceding bios.
  1018. */
  1019. /*
  1020. * TODO: this will probably have to change when discard goes
  1021. * back in.
  1022. */
  1023. cell_release_singleton(cell, bio);
  1024. if (lookup_result.shared)
  1025. process_shared_bio(tc, bio, block, &lookup_result);
  1026. else
  1027. remap_and_issue(tc, bio, lookup_result.block);
  1028. break;
  1029. case -ENODATA:
  1030. provision_block(tc, bio, block, cell);
  1031. break;
  1032. default:
  1033. DMERR("dm_thin_find_block() failed, error = %d", r);
  1034. bio_io_error(bio);
  1035. break;
  1036. }
  1037. }
  1038. static void process_deferred_bios(struct pool *pool)
  1039. {
  1040. unsigned long flags;
  1041. struct bio *bio;
  1042. struct bio_list bios;
  1043. int r;
  1044. bio_list_init(&bios);
  1045. spin_lock_irqsave(&pool->lock, flags);
  1046. bio_list_merge(&bios, &pool->deferred_bios);
  1047. bio_list_init(&pool->deferred_bios);
  1048. spin_unlock_irqrestore(&pool->lock, flags);
  1049. while ((bio = bio_list_pop(&bios))) {
  1050. struct thin_c *tc = dm_get_mapinfo(bio)->ptr;
  1051. /*
  1052. * If we've got no free new_mapping structs, and processing
  1053. * this bio might require one, we pause until there are some
  1054. * prepared mappings to process.
  1055. */
  1056. if (ensure_next_mapping(pool)) {
  1057. spin_lock_irqsave(&pool->lock, flags);
  1058. bio_list_merge(&pool->deferred_bios, &bios);
  1059. spin_unlock_irqrestore(&pool->lock, flags);
  1060. break;
  1061. }
  1062. process_bio(tc, bio);
  1063. }
  1064. /*
  1065. * If there are any deferred flush bios, we must commit
  1066. * the metadata before issuing them.
  1067. */
  1068. bio_list_init(&bios);
  1069. spin_lock_irqsave(&pool->lock, flags);
  1070. bio_list_merge(&bios, &pool->deferred_flush_bios);
  1071. bio_list_init(&pool->deferred_flush_bios);
  1072. spin_unlock_irqrestore(&pool->lock, flags);
  1073. if (bio_list_empty(&bios))
  1074. return;
  1075. r = dm_pool_commit_metadata(pool->pmd);
  1076. if (r) {
  1077. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  1078. __func__, r);
  1079. while ((bio = bio_list_pop(&bios)))
  1080. bio_io_error(bio);
  1081. return;
  1082. }
  1083. while ((bio = bio_list_pop(&bios)))
  1084. generic_make_request(bio);
  1085. }
  1086. static void do_worker(struct work_struct *ws)
  1087. {
  1088. struct pool *pool = container_of(ws, struct pool, worker);
  1089. process_prepared_mappings(pool);
  1090. process_deferred_bios(pool);
  1091. }
  1092. /*----------------------------------------------------------------*/
  1093. /*
  1094. * Mapping functions.
  1095. */
  1096. /*
  1097. * Called only while mapping a thin bio to hand it over to the workqueue.
  1098. */
  1099. static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
  1100. {
  1101. unsigned long flags;
  1102. struct pool *pool = tc->pool;
  1103. spin_lock_irqsave(&pool->lock, flags);
  1104. bio_list_add(&pool->deferred_bios, bio);
  1105. spin_unlock_irqrestore(&pool->lock, flags);
  1106. wake_worker(pool);
  1107. }
  1108. /*
  1109. * Non-blocking function called from the thin target's map function.
  1110. */
  1111. static int thin_bio_map(struct dm_target *ti, struct bio *bio,
  1112. union map_info *map_context)
  1113. {
  1114. int r;
  1115. struct thin_c *tc = ti->private;
  1116. dm_block_t block = get_bio_block(tc, bio);
  1117. struct dm_thin_device *td = tc->td;
  1118. struct dm_thin_lookup_result result;
  1119. /*
  1120. * Save the thin context for easy access from the deferred bio later.
  1121. */
  1122. map_context->ptr = tc;
  1123. if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
  1124. thin_defer_bio(tc, bio);
  1125. return DM_MAPIO_SUBMITTED;
  1126. }
  1127. r = dm_thin_find_block(td, block, 0, &result);
  1128. /*
  1129. * Note that we defer readahead too.
  1130. */
  1131. switch (r) {
  1132. case 0:
  1133. if (unlikely(result.shared)) {
  1134. /*
  1135. * We have a race condition here between the
  1136. * result.shared value returned by the lookup and
  1137. * snapshot creation, which may cause new
  1138. * sharing.
  1139. *
  1140. * To avoid this always quiesce the origin before
  1141. * taking the snap. You want to do this anyway to
  1142. * ensure a consistent application view
  1143. * (i.e. lockfs).
  1144. *
  1145. * More distant ancestors are irrelevant. The
  1146. * shared flag will be set in their case.
  1147. */
  1148. thin_defer_bio(tc, bio);
  1149. r = DM_MAPIO_SUBMITTED;
  1150. } else {
  1151. remap(tc, bio, result.block);
  1152. r = DM_MAPIO_REMAPPED;
  1153. }
  1154. break;
  1155. case -ENODATA:
  1156. /*
  1157. * In future, the failed dm_thin_find_block above could
  1158. * provide the hint to load the metadata into cache.
  1159. */
  1160. case -EWOULDBLOCK:
  1161. thin_defer_bio(tc, bio);
  1162. r = DM_MAPIO_SUBMITTED;
  1163. break;
  1164. }
  1165. return r;
  1166. }
  1167. static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1168. {
  1169. int r;
  1170. unsigned long flags;
  1171. struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
  1172. spin_lock_irqsave(&pt->pool->lock, flags);
  1173. r = !bio_list_empty(&pt->pool->retry_on_resume_list);
  1174. spin_unlock_irqrestore(&pt->pool->lock, flags);
  1175. if (!r) {
  1176. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  1177. r = bdi_congested(&q->backing_dev_info, bdi_bits);
  1178. }
  1179. return r;
  1180. }
  1181. static void __requeue_bios(struct pool *pool)
  1182. {
  1183. bio_list_merge(&pool->deferred_bios, &pool->retry_on_resume_list);
  1184. bio_list_init(&pool->retry_on_resume_list);
  1185. }
  1186. /*----------------------------------------------------------------
  1187. * Binding of control targets to a pool object
  1188. *--------------------------------------------------------------*/
  1189. static int bind_control_target(struct pool *pool, struct dm_target *ti)
  1190. {
  1191. struct pool_c *pt = ti->private;
  1192. pool->ti = ti;
  1193. pool->low_water_blocks = pt->low_water_blocks;
  1194. pool->zero_new_blocks = pt->zero_new_blocks;
  1195. return 0;
  1196. }
  1197. static void unbind_control_target(struct pool *pool, struct dm_target *ti)
  1198. {
  1199. if (pool->ti == ti)
  1200. pool->ti = NULL;
  1201. }
  1202. /*----------------------------------------------------------------
  1203. * Pool creation
  1204. *--------------------------------------------------------------*/
  1205. static void __pool_destroy(struct pool *pool)
  1206. {
  1207. __pool_table_remove(pool);
  1208. if (dm_pool_metadata_close(pool->pmd) < 0)
  1209. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  1210. prison_destroy(pool->prison);
  1211. dm_kcopyd_client_destroy(pool->copier);
  1212. if (pool->wq)
  1213. destroy_workqueue(pool->wq);
  1214. if (pool->next_mapping)
  1215. mempool_free(pool->next_mapping, pool->mapping_pool);
  1216. mempool_destroy(pool->mapping_pool);
  1217. mempool_destroy(pool->endio_hook_pool);
  1218. kfree(pool);
  1219. }
  1220. static struct pool *pool_create(struct mapped_device *pool_md,
  1221. struct block_device *metadata_dev,
  1222. unsigned long block_size, char **error)
  1223. {
  1224. int r;
  1225. void *err_p;
  1226. struct pool *pool;
  1227. struct dm_pool_metadata *pmd;
  1228. pmd = dm_pool_metadata_open(metadata_dev, block_size);
  1229. if (IS_ERR(pmd)) {
  1230. *error = "Error creating metadata object";
  1231. return (struct pool *)pmd;
  1232. }
  1233. pool = kmalloc(sizeof(*pool), GFP_KERNEL);
  1234. if (!pool) {
  1235. *error = "Error allocating memory for pool";
  1236. err_p = ERR_PTR(-ENOMEM);
  1237. goto bad_pool;
  1238. }
  1239. pool->pmd = pmd;
  1240. pool->sectors_per_block = block_size;
  1241. pool->block_shift = ffs(block_size) - 1;
  1242. pool->offset_mask = block_size - 1;
  1243. pool->low_water_blocks = 0;
  1244. pool->zero_new_blocks = 1;
  1245. pool->prison = prison_create(PRISON_CELLS);
  1246. if (!pool->prison) {
  1247. *error = "Error creating pool's bio prison";
  1248. err_p = ERR_PTR(-ENOMEM);
  1249. goto bad_prison;
  1250. }
  1251. pool->copier = dm_kcopyd_client_create();
  1252. if (IS_ERR(pool->copier)) {
  1253. r = PTR_ERR(pool->copier);
  1254. *error = "Error creating pool's kcopyd client";
  1255. err_p = ERR_PTR(r);
  1256. goto bad_kcopyd_client;
  1257. }
  1258. /*
  1259. * Create singlethreaded workqueue that will service all devices
  1260. * that use this metadata.
  1261. */
  1262. pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  1263. if (!pool->wq) {
  1264. *error = "Error creating pool's workqueue";
  1265. err_p = ERR_PTR(-ENOMEM);
  1266. goto bad_wq;
  1267. }
  1268. INIT_WORK(&pool->worker, do_worker);
  1269. spin_lock_init(&pool->lock);
  1270. bio_list_init(&pool->deferred_bios);
  1271. bio_list_init(&pool->deferred_flush_bios);
  1272. INIT_LIST_HEAD(&pool->prepared_mappings);
  1273. pool->low_water_triggered = 0;
  1274. pool->no_free_space = 0;
  1275. bio_list_init(&pool->retry_on_resume_list);
  1276. ds_init(&pool->ds);
  1277. pool->next_mapping = NULL;
  1278. pool->mapping_pool =
  1279. mempool_create_kmalloc_pool(MAPPING_POOL_SIZE, sizeof(struct new_mapping));
  1280. if (!pool->mapping_pool) {
  1281. *error = "Error creating pool's mapping mempool";
  1282. err_p = ERR_PTR(-ENOMEM);
  1283. goto bad_mapping_pool;
  1284. }
  1285. pool->endio_hook_pool =
  1286. mempool_create_kmalloc_pool(ENDIO_HOOK_POOL_SIZE, sizeof(struct endio_hook));
  1287. if (!pool->endio_hook_pool) {
  1288. *error = "Error creating pool's endio_hook mempool";
  1289. err_p = ERR_PTR(-ENOMEM);
  1290. goto bad_endio_hook_pool;
  1291. }
  1292. pool->ref_count = 1;
  1293. pool->pool_md = pool_md;
  1294. pool->md_dev = metadata_dev;
  1295. __pool_table_insert(pool);
  1296. return pool;
  1297. bad_endio_hook_pool:
  1298. mempool_destroy(pool->mapping_pool);
  1299. bad_mapping_pool:
  1300. destroy_workqueue(pool->wq);
  1301. bad_wq:
  1302. dm_kcopyd_client_destroy(pool->copier);
  1303. bad_kcopyd_client:
  1304. prison_destroy(pool->prison);
  1305. bad_prison:
  1306. kfree(pool);
  1307. bad_pool:
  1308. if (dm_pool_metadata_close(pmd))
  1309. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  1310. return err_p;
  1311. }
  1312. static void __pool_inc(struct pool *pool)
  1313. {
  1314. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  1315. pool->ref_count++;
  1316. }
  1317. static void __pool_dec(struct pool *pool)
  1318. {
  1319. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  1320. BUG_ON(!pool->ref_count);
  1321. if (!--pool->ref_count)
  1322. __pool_destroy(pool);
  1323. }
  1324. static struct pool *__pool_find(struct mapped_device *pool_md,
  1325. struct block_device *metadata_dev,
  1326. unsigned long block_size, char **error)
  1327. {
  1328. struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
  1329. if (pool) {
  1330. if (pool->pool_md != pool_md)
  1331. return ERR_PTR(-EBUSY);
  1332. __pool_inc(pool);
  1333. } else {
  1334. pool = __pool_table_lookup(pool_md);
  1335. if (pool) {
  1336. if (pool->md_dev != metadata_dev)
  1337. return ERR_PTR(-EINVAL);
  1338. __pool_inc(pool);
  1339. } else
  1340. pool = pool_create(pool_md, metadata_dev, block_size, error);
  1341. }
  1342. return pool;
  1343. }
  1344. /*----------------------------------------------------------------
  1345. * Pool target methods
  1346. *--------------------------------------------------------------*/
  1347. static void pool_dtr(struct dm_target *ti)
  1348. {
  1349. struct pool_c *pt = ti->private;
  1350. mutex_lock(&dm_thin_pool_table.mutex);
  1351. unbind_control_target(pt->pool, ti);
  1352. __pool_dec(pt->pool);
  1353. dm_put_device(ti, pt->metadata_dev);
  1354. dm_put_device(ti, pt->data_dev);
  1355. kfree(pt);
  1356. mutex_unlock(&dm_thin_pool_table.mutex);
  1357. }
  1358. struct pool_features {
  1359. unsigned zero_new_blocks:1;
  1360. };
  1361. static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
  1362. struct dm_target *ti)
  1363. {
  1364. int r;
  1365. unsigned argc;
  1366. const char *arg_name;
  1367. static struct dm_arg _args[] = {
  1368. {0, 1, "Invalid number of pool feature arguments"},
  1369. };
  1370. /*
  1371. * No feature arguments supplied.
  1372. */
  1373. if (!as->argc)
  1374. return 0;
  1375. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  1376. if (r)
  1377. return -EINVAL;
  1378. while (argc && !r) {
  1379. arg_name = dm_shift_arg(as);
  1380. argc--;
  1381. if (!strcasecmp(arg_name, "skip_block_zeroing")) {
  1382. pf->zero_new_blocks = 0;
  1383. continue;
  1384. }
  1385. ti->error = "Unrecognised pool feature requested";
  1386. r = -EINVAL;
  1387. }
  1388. return r;
  1389. }
  1390. /*
  1391. * thin-pool <metadata dev> <data dev>
  1392. * <data block size (sectors)>
  1393. * <low water mark (blocks)>
  1394. * [<#feature args> [<arg>]*]
  1395. *
  1396. * Optional feature arguments are:
  1397. * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
  1398. */
  1399. static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
  1400. {
  1401. int r;
  1402. struct pool_c *pt;
  1403. struct pool *pool;
  1404. struct pool_features pf;
  1405. struct dm_arg_set as;
  1406. struct dm_dev *data_dev;
  1407. unsigned long block_size;
  1408. dm_block_t low_water_blocks;
  1409. struct dm_dev *metadata_dev;
  1410. sector_t metadata_dev_size;
  1411. /*
  1412. * FIXME Remove validation from scope of lock.
  1413. */
  1414. mutex_lock(&dm_thin_pool_table.mutex);
  1415. if (argc < 4) {
  1416. ti->error = "Invalid argument count";
  1417. r = -EINVAL;
  1418. goto out_unlock;
  1419. }
  1420. as.argc = argc;
  1421. as.argv = argv;
  1422. r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &metadata_dev);
  1423. if (r) {
  1424. ti->error = "Error opening metadata block device";
  1425. goto out_unlock;
  1426. }
  1427. metadata_dev_size = i_size_read(metadata_dev->bdev->bd_inode) >> SECTOR_SHIFT;
  1428. if (metadata_dev_size > METADATA_DEV_MAX_SECTORS) {
  1429. ti->error = "Metadata device is too large";
  1430. r = -EINVAL;
  1431. goto out_metadata;
  1432. }
  1433. r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
  1434. if (r) {
  1435. ti->error = "Error getting data device";
  1436. goto out_metadata;
  1437. }
  1438. if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
  1439. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  1440. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  1441. !is_power_of_2(block_size)) {
  1442. ti->error = "Invalid block size";
  1443. r = -EINVAL;
  1444. goto out;
  1445. }
  1446. if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
  1447. ti->error = "Invalid low water mark";
  1448. r = -EINVAL;
  1449. goto out;
  1450. }
  1451. /*
  1452. * Set default pool features.
  1453. */
  1454. memset(&pf, 0, sizeof(pf));
  1455. pf.zero_new_blocks = 1;
  1456. dm_consume_args(&as, 4);
  1457. r = parse_pool_features(&as, &pf, ti);
  1458. if (r)
  1459. goto out;
  1460. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  1461. if (!pt) {
  1462. r = -ENOMEM;
  1463. goto out;
  1464. }
  1465. pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
  1466. block_size, &ti->error);
  1467. if (IS_ERR(pool)) {
  1468. r = PTR_ERR(pool);
  1469. goto out_free_pt;
  1470. }
  1471. pt->pool = pool;
  1472. pt->ti = ti;
  1473. pt->metadata_dev = metadata_dev;
  1474. pt->data_dev = data_dev;
  1475. pt->low_water_blocks = low_water_blocks;
  1476. pt->zero_new_blocks = pf.zero_new_blocks;
  1477. ti->num_flush_requests = 1;
  1478. ti->num_discard_requests = 0;
  1479. ti->private = pt;
  1480. pt->callbacks.congested_fn = pool_is_congested;
  1481. dm_table_add_target_callbacks(ti->table, &pt->callbacks);
  1482. mutex_unlock(&dm_thin_pool_table.mutex);
  1483. return 0;
  1484. out_free_pt:
  1485. kfree(pt);
  1486. out:
  1487. dm_put_device(ti, data_dev);
  1488. out_metadata:
  1489. dm_put_device(ti, metadata_dev);
  1490. out_unlock:
  1491. mutex_unlock(&dm_thin_pool_table.mutex);
  1492. return r;
  1493. }
  1494. static int pool_map(struct dm_target *ti, struct bio *bio,
  1495. union map_info *map_context)
  1496. {
  1497. int r;
  1498. struct pool_c *pt = ti->private;
  1499. struct pool *pool = pt->pool;
  1500. unsigned long flags;
  1501. /*
  1502. * As this is a singleton target, ti->begin is always zero.
  1503. */
  1504. spin_lock_irqsave(&pool->lock, flags);
  1505. bio->bi_bdev = pt->data_dev->bdev;
  1506. r = DM_MAPIO_REMAPPED;
  1507. spin_unlock_irqrestore(&pool->lock, flags);
  1508. return r;
  1509. }
  1510. /*
  1511. * Retrieves the number of blocks of the data device from
  1512. * the superblock and compares it to the actual device size,
  1513. * thus resizing the data device in case it has grown.
  1514. *
  1515. * This both copes with opening preallocated data devices in the ctr
  1516. * being followed by a resume
  1517. * -and-
  1518. * calling the resume method individually after userspace has
  1519. * grown the data device in reaction to a table event.
  1520. */
  1521. static int pool_preresume(struct dm_target *ti)
  1522. {
  1523. int r;
  1524. struct pool_c *pt = ti->private;
  1525. struct pool *pool = pt->pool;
  1526. dm_block_t data_size, sb_data_size;
  1527. /*
  1528. * Take control of the pool object.
  1529. */
  1530. r = bind_control_target(pool, ti);
  1531. if (r)
  1532. return r;
  1533. data_size = ti->len >> pool->block_shift;
  1534. r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
  1535. if (r) {
  1536. DMERR("failed to retrieve data device size");
  1537. return r;
  1538. }
  1539. if (data_size < sb_data_size) {
  1540. DMERR("pool target too small, is %llu blocks (expected %llu)",
  1541. data_size, sb_data_size);
  1542. return -EINVAL;
  1543. } else if (data_size > sb_data_size) {
  1544. r = dm_pool_resize_data_dev(pool->pmd, data_size);
  1545. if (r) {
  1546. DMERR("failed to resize data device");
  1547. return r;
  1548. }
  1549. r = dm_pool_commit_metadata(pool->pmd);
  1550. if (r) {
  1551. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  1552. __func__, r);
  1553. return r;
  1554. }
  1555. }
  1556. return 0;
  1557. }
  1558. static void pool_resume(struct dm_target *ti)
  1559. {
  1560. struct pool_c *pt = ti->private;
  1561. struct pool *pool = pt->pool;
  1562. unsigned long flags;
  1563. spin_lock_irqsave(&pool->lock, flags);
  1564. pool->low_water_triggered = 0;
  1565. pool->no_free_space = 0;
  1566. __requeue_bios(pool);
  1567. spin_unlock_irqrestore(&pool->lock, flags);
  1568. wake_worker(pool);
  1569. }
  1570. static void pool_postsuspend(struct dm_target *ti)
  1571. {
  1572. int r;
  1573. struct pool_c *pt = ti->private;
  1574. struct pool *pool = pt->pool;
  1575. flush_workqueue(pool->wq);
  1576. r = dm_pool_commit_metadata(pool->pmd);
  1577. if (r < 0) {
  1578. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  1579. __func__, r);
  1580. /* FIXME: invalidate device? error the next FUA or FLUSH bio ?*/
  1581. }
  1582. }
  1583. static int check_arg_count(unsigned argc, unsigned args_required)
  1584. {
  1585. if (argc != args_required) {
  1586. DMWARN("Message received with %u arguments instead of %u.",
  1587. argc, args_required);
  1588. return -EINVAL;
  1589. }
  1590. return 0;
  1591. }
  1592. static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
  1593. {
  1594. if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
  1595. *dev_id <= MAX_DEV_ID)
  1596. return 0;
  1597. if (warning)
  1598. DMWARN("Message received with invalid device id: %s", arg);
  1599. return -EINVAL;
  1600. }
  1601. static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
  1602. {
  1603. dm_thin_id dev_id;
  1604. int r;
  1605. r = check_arg_count(argc, 2);
  1606. if (r)
  1607. return r;
  1608. r = read_dev_id(argv[1], &dev_id, 1);
  1609. if (r)
  1610. return r;
  1611. r = dm_pool_create_thin(pool->pmd, dev_id);
  1612. if (r) {
  1613. DMWARN("Creation of new thinly-provisioned device with id %s failed.",
  1614. argv[1]);
  1615. return r;
  1616. }
  1617. return 0;
  1618. }
  1619. static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  1620. {
  1621. dm_thin_id dev_id;
  1622. dm_thin_id origin_dev_id;
  1623. int r;
  1624. r = check_arg_count(argc, 3);
  1625. if (r)
  1626. return r;
  1627. r = read_dev_id(argv[1], &dev_id, 1);
  1628. if (r)
  1629. return r;
  1630. r = read_dev_id(argv[2], &origin_dev_id, 1);
  1631. if (r)
  1632. return r;
  1633. r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
  1634. if (r) {
  1635. DMWARN("Creation of new snapshot %s of device %s failed.",
  1636. argv[1], argv[2]);
  1637. return r;
  1638. }
  1639. return 0;
  1640. }
  1641. static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
  1642. {
  1643. dm_thin_id dev_id;
  1644. int r;
  1645. r = check_arg_count(argc, 2);
  1646. if (r)
  1647. return r;
  1648. r = read_dev_id(argv[1], &dev_id, 1);
  1649. if (r)
  1650. return r;
  1651. r = dm_pool_delete_thin_device(pool->pmd, dev_id);
  1652. if (r)
  1653. DMWARN("Deletion of thin device %s failed.", argv[1]);
  1654. return r;
  1655. }
  1656. static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
  1657. {
  1658. dm_thin_id old_id, new_id;
  1659. int r;
  1660. r = check_arg_count(argc, 3);
  1661. if (r)
  1662. return r;
  1663. if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
  1664. DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
  1665. return -EINVAL;
  1666. }
  1667. if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
  1668. DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
  1669. return -EINVAL;
  1670. }
  1671. r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
  1672. if (r) {
  1673. DMWARN("Failed to change transaction id from %s to %s.",
  1674. argv[1], argv[2]);
  1675. return r;
  1676. }
  1677. return 0;
  1678. }
  1679. /*
  1680. * Messages supported:
  1681. * create_thin <dev_id>
  1682. * create_snap <dev_id> <origin_id>
  1683. * delete <dev_id>
  1684. * trim <dev_id> <new_size_in_sectors>
  1685. * set_transaction_id <current_trans_id> <new_trans_id>
  1686. */
  1687. static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
  1688. {
  1689. int r = -EINVAL;
  1690. struct pool_c *pt = ti->private;
  1691. struct pool *pool = pt->pool;
  1692. if (!strcasecmp(argv[0], "create_thin"))
  1693. r = process_create_thin_mesg(argc, argv, pool);
  1694. else if (!strcasecmp(argv[0], "create_snap"))
  1695. r = process_create_snap_mesg(argc, argv, pool);
  1696. else if (!strcasecmp(argv[0], "delete"))
  1697. r = process_delete_mesg(argc, argv, pool);
  1698. else if (!strcasecmp(argv[0], "set_transaction_id"))
  1699. r = process_set_transaction_id_mesg(argc, argv, pool);
  1700. else
  1701. DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
  1702. if (!r) {
  1703. r = dm_pool_commit_metadata(pool->pmd);
  1704. if (r)
  1705. DMERR("%s message: dm_pool_commit_metadata() failed, error = %d",
  1706. argv[0], r);
  1707. }
  1708. return r;
  1709. }
  1710. /*
  1711. * Status line is:
  1712. * <transaction id> <used metadata sectors>/<total metadata sectors>
  1713. * <used data sectors>/<total data sectors> <held metadata root>
  1714. */
  1715. static int pool_status(struct dm_target *ti, status_type_t type,
  1716. char *result, unsigned maxlen)
  1717. {
  1718. int r;
  1719. unsigned sz = 0;
  1720. uint64_t transaction_id;
  1721. dm_block_t nr_free_blocks_data;
  1722. dm_block_t nr_free_blocks_metadata;
  1723. dm_block_t nr_blocks_data;
  1724. dm_block_t nr_blocks_metadata;
  1725. dm_block_t held_root;
  1726. char buf[BDEVNAME_SIZE];
  1727. char buf2[BDEVNAME_SIZE];
  1728. struct pool_c *pt = ti->private;
  1729. struct pool *pool = pt->pool;
  1730. switch (type) {
  1731. case STATUSTYPE_INFO:
  1732. r = dm_pool_get_metadata_transaction_id(pool->pmd,
  1733. &transaction_id);
  1734. if (r)
  1735. return r;
  1736. r = dm_pool_get_free_metadata_block_count(pool->pmd,
  1737. &nr_free_blocks_metadata);
  1738. if (r)
  1739. return r;
  1740. r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
  1741. if (r)
  1742. return r;
  1743. r = dm_pool_get_free_block_count(pool->pmd,
  1744. &nr_free_blocks_data);
  1745. if (r)
  1746. return r;
  1747. r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
  1748. if (r)
  1749. return r;
  1750. r = dm_pool_get_held_metadata_root(pool->pmd, &held_root);
  1751. if (r)
  1752. return r;
  1753. DMEMIT("%llu %llu/%llu %llu/%llu ",
  1754. (unsigned long long)transaction_id,
  1755. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  1756. (unsigned long long)nr_blocks_metadata,
  1757. (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
  1758. (unsigned long long)nr_blocks_data);
  1759. if (held_root)
  1760. DMEMIT("%llu", held_root);
  1761. else
  1762. DMEMIT("-");
  1763. break;
  1764. case STATUSTYPE_TABLE:
  1765. DMEMIT("%s %s %lu %llu ",
  1766. format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
  1767. format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
  1768. (unsigned long)pool->sectors_per_block,
  1769. (unsigned long long)pt->low_water_blocks);
  1770. DMEMIT("%u ", !pool->zero_new_blocks);
  1771. if (!pool->zero_new_blocks)
  1772. DMEMIT("skip_block_zeroing ");
  1773. break;
  1774. }
  1775. return 0;
  1776. }
  1777. static int pool_iterate_devices(struct dm_target *ti,
  1778. iterate_devices_callout_fn fn, void *data)
  1779. {
  1780. struct pool_c *pt = ti->private;
  1781. return fn(ti, pt->data_dev, 0, ti->len, data);
  1782. }
  1783. static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  1784. struct bio_vec *biovec, int max_size)
  1785. {
  1786. struct pool_c *pt = ti->private;
  1787. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  1788. if (!q->merge_bvec_fn)
  1789. return max_size;
  1790. bvm->bi_bdev = pt->data_dev->bdev;
  1791. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  1792. }
  1793. static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
  1794. {
  1795. struct pool_c *pt = ti->private;
  1796. struct pool *pool = pt->pool;
  1797. blk_limits_io_min(limits, 0);
  1798. blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
  1799. }
  1800. static struct target_type pool_target = {
  1801. .name = "thin-pool",
  1802. .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
  1803. DM_TARGET_IMMUTABLE,
  1804. .version = {1, 0, 0},
  1805. .module = THIS_MODULE,
  1806. .ctr = pool_ctr,
  1807. .dtr = pool_dtr,
  1808. .map = pool_map,
  1809. .postsuspend = pool_postsuspend,
  1810. .preresume = pool_preresume,
  1811. .resume = pool_resume,
  1812. .message = pool_message,
  1813. .status = pool_status,
  1814. .merge = pool_merge,
  1815. .iterate_devices = pool_iterate_devices,
  1816. .io_hints = pool_io_hints,
  1817. };
  1818. /*----------------------------------------------------------------
  1819. * Thin target methods
  1820. *--------------------------------------------------------------*/
  1821. static void thin_dtr(struct dm_target *ti)
  1822. {
  1823. struct thin_c *tc = ti->private;
  1824. mutex_lock(&dm_thin_pool_table.mutex);
  1825. __pool_dec(tc->pool);
  1826. dm_pool_close_thin_device(tc->td);
  1827. dm_put_device(ti, tc->pool_dev);
  1828. kfree(tc);
  1829. mutex_unlock(&dm_thin_pool_table.mutex);
  1830. }
  1831. /*
  1832. * Thin target parameters:
  1833. *
  1834. * <pool_dev> <dev_id>
  1835. *
  1836. * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
  1837. * dev_id: the internal device identifier
  1838. */
  1839. static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
  1840. {
  1841. int r;
  1842. struct thin_c *tc;
  1843. struct dm_dev *pool_dev;
  1844. struct mapped_device *pool_md;
  1845. mutex_lock(&dm_thin_pool_table.mutex);
  1846. if (argc != 2) {
  1847. ti->error = "Invalid argument count";
  1848. r = -EINVAL;
  1849. goto out_unlock;
  1850. }
  1851. tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
  1852. if (!tc) {
  1853. ti->error = "Out of memory";
  1854. r = -ENOMEM;
  1855. goto out_unlock;
  1856. }
  1857. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
  1858. if (r) {
  1859. ti->error = "Error opening pool device";
  1860. goto bad_pool_dev;
  1861. }
  1862. tc->pool_dev = pool_dev;
  1863. if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
  1864. ti->error = "Invalid device id";
  1865. r = -EINVAL;
  1866. goto bad_common;
  1867. }
  1868. pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
  1869. if (!pool_md) {
  1870. ti->error = "Couldn't get pool mapped device";
  1871. r = -EINVAL;
  1872. goto bad_common;
  1873. }
  1874. tc->pool = __pool_table_lookup(pool_md);
  1875. if (!tc->pool) {
  1876. ti->error = "Couldn't find pool object";
  1877. r = -EINVAL;
  1878. goto bad_pool_lookup;
  1879. }
  1880. __pool_inc(tc->pool);
  1881. r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
  1882. if (r) {
  1883. ti->error = "Couldn't open thin internal device";
  1884. goto bad_thin_open;
  1885. }
  1886. ti->split_io = tc->pool->sectors_per_block;
  1887. ti->num_flush_requests = 1;
  1888. ti->num_discard_requests = 0;
  1889. ti->discards_supported = 0;
  1890. dm_put(pool_md);
  1891. mutex_unlock(&dm_thin_pool_table.mutex);
  1892. return 0;
  1893. bad_thin_open:
  1894. __pool_dec(tc->pool);
  1895. bad_pool_lookup:
  1896. dm_put(pool_md);
  1897. bad_common:
  1898. dm_put_device(ti, tc->pool_dev);
  1899. bad_pool_dev:
  1900. kfree(tc);
  1901. out_unlock:
  1902. mutex_unlock(&dm_thin_pool_table.mutex);
  1903. return r;
  1904. }
  1905. static int thin_map(struct dm_target *ti, struct bio *bio,
  1906. union map_info *map_context)
  1907. {
  1908. bio->bi_sector -= ti->begin;
  1909. return thin_bio_map(ti, bio, map_context);
  1910. }
  1911. static void thin_postsuspend(struct dm_target *ti)
  1912. {
  1913. if (dm_noflush_suspending(ti))
  1914. requeue_io((struct thin_c *)ti->private);
  1915. }
  1916. /*
  1917. * <nr mapped sectors> <highest mapped sector>
  1918. */
  1919. static int thin_status(struct dm_target *ti, status_type_t type,
  1920. char *result, unsigned maxlen)
  1921. {
  1922. int r;
  1923. ssize_t sz = 0;
  1924. dm_block_t mapped, highest;
  1925. char buf[BDEVNAME_SIZE];
  1926. struct thin_c *tc = ti->private;
  1927. if (!tc->td)
  1928. DMEMIT("-");
  1929. else {
  1930. switch (type) {
  1931. case STATUSTYPE_INFO:
  1932. r = dm_thin_get_mapped_count(tc->td, &mapped);
  1933. if (r)
  1934. return r;
  1935. r = dm_thin_get_highest_mapped_block(tc->td, &highest);
  1936. if (r < 0)
  1937. return r;
  1938. DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
  1939. if (r)
  1940. DMEMIT("%llu", ((highest + 1) *
  1941. tc->pool->sectors_per_block) - 1);
  1942. else
  1943. DMEMIT("-");
  1944. break;
  1945. case STATUSTYPE_TABLE:
  1946. DMEMIT("%s %lu",
  1947. format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
  1948. (unsigned long) tc->dev_id);
  1949. break;
  1950. }
  1951. }
  1952. return 0;
  1953. }
  1954. static int thin_iterate_devices(struct dm_target *ti,
  1955. iterate_devices_callout_fn fn, void *data)
  1956. {
  1957. dm_block_t blocks;
  1958. struct thin_c *tc = ti->private;
  1959. /*
  1960. * We can't call dm_pool_get_data_dev_size() since that blocks. So
  1961. * we follow a more convoluted path through to the pool's target.
  1962. */
  1963. if (!tc->pool->ti)
  1964. return 0; /* nothing is bound */
  1965. blocks = tc->pool->ti->len >> tc->pool->block_shift;
  1966. if (blocks)
  1967. return fn(ti, tc->pool_dev, 0, tc->pool->sectors_per_block * blocks, data);
  1968. return 0;
  1969. }
  1970. static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
  1971. {
  1972. struct thin_c *tc = ti->private;
  1973. blk_limits_io_min(limits, 0);
  1974. blk_limits_io_opt(limits, tc->pool->sectors_per_block << SECTOR_SHIFT);
  1975. }
  1976. static struct target_type thin_target = {
  1977. .name = "thin",
  1978. .version = {1, 0, 0},
  1979. .module = THIS_MODULE,
  1980. .ctr = thin_ctr,
  1981. .dtr = thin_dtr,
  1982. .map = thin_map,
  1983. .postsuspend = thin_postsuspend,
  1984. .status = thin_status,
  1985. .iterate_devices = thin_iterate_devices,
  1986. .io_hints = thin_io_hints,
  1987. };
  1988. /*----------------------------------------------------------------*/
  1989. static int __init dm_thin_init(void)
  1990. {
  1991. int r;
  1992. pool_table_init();
  1993. r = dm_register_target(&thin_target);
  1994. if (r)
  1995. return r;
  1996. r = dm_register_target(&pool_target);
  1997. if (r)
  1998. dm_unregister_target(&thin_target);
  1999. return r;
  2000. }
  2001. static void dm_thin_exit(void)
  2002. {
  2003. dm_unregister_target(&thin_target);
  2004. dm_unregister_target(&pool_target);
  2005. }
  2006. module_init(dm_thin_init);
  2007. module_exit(dm_thin_exit);
  2008. MODULE_DESCRIPTION(DM_NAME "device-mapper thin provisioning target");
  2009. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2010. MODULE_LICENSE("GPL");