dm-thin.c 67 KB

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