dm-thin.c 57 KB

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