dm-thin.c 113 KB

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