dm-thin.c 58 KB

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