dm-thin.c 82 KB

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