dm-thin.c 76 KB

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