dm-thin.c 68 KB

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