dm-thin.c 75 KB

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