dm-thin.c 102 KB

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