dm-thin.c 109 KB

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