dm-thin.c 110 KB

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