dm-thin.c 114 KB

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