dm-thin.c 69 KB

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