dm-thin.c 110 KB

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