dm-thin.c 66 KB

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