dm-thin.c 75 KB

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