extent_io.c 151 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bitops.h>
  3. #include <linux/slab.h>
  4. #include <linux/bio.h>
  5. #include <linux/mm.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/page-flags.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/swap.h>
  11. #include <linux/writeback.h>
  12. #include <linux/pagevec.h>
  13. #include <linux/prefetch.h>
  14. #include <linux/cleancache.h>
  15. #include "extent_io.h"
  16. #include "extent_map.h"
  17. #include "ctree.h"
  18. #include "btrfs_inode.h"
  19. #include "volumes.h"
  20. #include "check-integrity.h"
  21. #include "locking.h"
  22. #include "rcu-string.h"
  23. #include "backref.h"
  24. static struct kmem_cache *extent_state_cache;
  25. static struct kmem_cache *extent_buffer_cache;
  26. static struct bio_set *btrfs_bioset;
  27. static inline bool extent_state_in_tree(const struct extent_state *state)
  28. {
  29. return !RB_EMPTY_NODE(&state->rb_node);
  30. }
  31. #ifdef CONFIG_BTRFS_DEBUG
  32. static LIST_HEAD(buffers);
  33. static LIST_HEAD(states);
  34. static DEFINE_SPINLOCK(leak_lock);
  35. static inline
  36. void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
  37. {
  38. unsigned long flags;
  39. spin_lock_irqsave(&leak_lock, flags);
  40. list_add(new, head);
  41. spin_unlock_irqrestore(&leak_lock, flags);
  42. }
  43. static inline
  44. void btrfs_leak_debug_del(struct list_head *entry)
  45. {
  46. unsigned long flags;
  47. spin_lock_irqsave(&leak_lock, flags);
  48. list_del(entry);
  49. spin_unlock_irqrestore(&leak_lock, flags);
  50. }
  51. static inline
  52. void btrfs_leak_debug_check(void)
  53. {
  54. struct extent_state *state;
  55. struct extent_buffer *eb;
  56. while (!list_empty(&states)) {
  57. state = list_entry(states.next, struct extent_state, leak_list);
  58. pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
  59. state->start, state->end, state->state,
  60. extent_state_in_tree(state),
  61. refcount_read(&state->refs));
  62. list_del(&state->leak_list);
  63. kmem_cache_free(extent_state_cache, state);
  64. }
  65. while (!list_empty(&buffers)) {
  66. eb = list_entry(buffers.next, struct extent_buffer, leak_list);
  67. pr_err("BTRFS: buffer leak start %llu len %lu refs %d\n",
  68. eb->start, eb->len, atomic_read(&eb->refs));
  69. list_del(&eb->leak_list);
  70. kmem_cache_free(extent_buffer_cache, eb);
  71. }
  72. }
  73. #define btrfs_debug_check_extent_io_range(tree, start, end) \
  74. __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
  75. static inline void __btrfs_debug_check_extent_io_range(const char *caller,
  76. struct extent_io_tree *tree, u64 start, u64 end)
  77. {
  78. if (tree->ops && tree->ops->check_extent_io_range)
  79. tree->ops->check_extent_io_range(tree->private_data, caller,
  80. start, end);
  81. }
  82. #else
  83. #define btrfs_leak_debug_add(new, head) do {} while (0)
  84. #define btrfs_leak_debug_del(entry) do {} while (0)
  85. #define btrfs_leak_debug_check() do {} while (0)
  86. #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
  87. #endif
  88. #define BUFFER_LRU_MAX 64
  89. struct tree_entry {
  90. u64 start;
  91. u64 end;
  92. struct rb_node rb_node;
  93. };
  94. struct extent_page_data {
  95. struct bio *bio;
  96. struct extent_io_tree *tree;
  97. get_extent_t *get_extent;
  98. /* tells writepage not to lock the state bits for this range
  99. * it still does the unlocking
  100. */
  101. unsigned int extent_locked:1;
  102. /* tells the submit_bio code to use REQ_SYNC */
  103. unsigned int sync_io:1;
  104. };
  105. static void add_extent_changeset(struct extent_state *state, unsigned bits,
  106. struct extent_changeset *changeset,
  107. int set)
  108. {
  109. int ret;
  110. if (!changeset)
  111. return;
  112. if (set && (state->state & bits) == bits)
  113. return;
  114. if (!set && (state->state & bits) == 0)
  115. return;
  116. changeset->bytes_changed += state->end - state->start + 1;
  117. ret = ulist_add(&changeset->range_changed, state->start, state->end,
  118. GFP_ATOMIC);
  119. /* ENOMEM */
  120. BUG_ON(ret < 0);
  121. }
  122. static noinline void flush_write_bio(void *data);
  123. static inline struct btrfs_fs_info *
  124. tree_fs_info(struct extent_io_tree *tree)
  125. {
  126. if (tree->ops)
  127. return tree->ops->tree_fs_info(tree->private_data);
  128. return NULL;
  129. }
  130. int __init extent_io_init(void)
  131. {
  132. extent_state_cache = kmem_cache_create("btrfs_extent_state",
  133. sizeof(struct extent_state), 0,
  134. SLAB_MEM_SPREAD, NULL);
  135. if (!extent_state_cache)
  136. return -ENOMEM;
  137. extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
  138. sizeof(struct extent_buffer), 0,
  139. SLAB_MEM_SPREAD, NULL);
  140. if (!extent_buffer_cache)
  141. goto free_state_cache;
  142. btrfs_bioset = bioset_create(BIO_POOL_SIZE,
  143. offsetof(struct btrfs_io_bio, bio),
  144. BIOSET_NEED_BVECS);
  145. if (!btrfs_bioset)
  146. goto free_buffer_cache;
  147. if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE))
  148. goto free_bioset;
  149. return 0;
  150. free_bioset:
  151. bioset_free(btrfs_bioset);
  152. btrfs_bioset = NULL;
  153. free_buffer_cache:
  154. kmem_cache_destroy(extent_buffer_cache);
  155. extent_buffer_cache = NULL;
  156. free_state_cache:
  157. kmem_cache_destroy(extent_state_cache);
  158. extent_state_cache = NULL;
  159. return -ENOMEM;
  160. }
  161. void extent_io_exit(void)
  162. {
  163. btrfs_leak_debug_check();
  164. /*
  165. * Make sure all delayed rcu free are flushed before we
  166. * destroy caches.
  167. */
  168. rcu_barrier();
  169. kmem_cache_destroy(extent_state_cache);
  170. kmem_cache_destroy(extent_buffer_cache);
  171. if (btrfs_bioset)
  172. bioset_free(btrfs_bioset);
  173. }
  174. void extent_io_tree_init(struct extent_io_tree *tree,
  175. void *private_data)
  176. {
  177. tree->state = RB_ROOT;
  178. tree->ops = NULL;
  179. tree->dirty_bytes = 0;
  180. spin_lock_init(&tree->lock);
  181. tree->private_data = private_data;
  182. }
  183. static struct extent_state *alloc_extent_state(gfp_t mask)
  184. {
  185. struct extent_state *state;
  186. /*
  187. * The given mask might be not appropriate for the slab allocator,
  188. * drop the unsupported bits
  189. */
  190. mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
  191. state = kmem_cache_alloc(extent_state_cache, mask);
  192. if (!state)
  193. return state;
  194. state->state = 0;
  195. state->failrec = NULL;
  196. RB_CLEAR_NODE(&state->rb_node);
  197. btrfs_leak_debug_add(&state->leak_list, &states);
  198. refcount_set(&state->refs, 1);
  199. init_waitqueue_head(&state->wq);
  200. trace_alloc_extent_state(state, mask, _RET_IP_);
  201. return state;
  202. }
  203. void free_extent_state(struct extent_state *state)
  204. {
  205. if (!state)
  206. return;
  207. if (refcount_dec_and_test(&state->refs)) {
  208. WARN_ON(extent_state_in_tree(state));
  209. btrfs_leak_debug_del(&state->leak_list);
  210. trace_free_extent_state(state, _RET_IP_);
  211. kmem_cache_free(extent_state_cache, state);
  212. }
  213. }
  214. static struct rb_node *tree_insert(struct rb_root *root,
  215. struct rb_node *search_start,
  216. u64 offset,
  217. struct rb_node *node,
  218. struct rb_node ***p_in,
  219. struct rb_node **parent_in)
  220. {
  221. struct rb_node **p;
  222. struct rb_node *parent = NULL;
  223. struct tree_entry *entry;
  224. if (p_in && parent_in) {
  225. p = *p_in;
  226. parent = *parent_in;
  227. goto do_insert;
  228. }
  229. p = search_start ? &search_start : &root->rb_node;
  230. while (*p) {
  231. parent = *p;
  232. entry = rb_entry(parent, struct tree_entry, rb_node);
  233. if (offset < entry->start)
  234. p = &(*p)->rb_left;
  235. else if (offset > entry->end)
  236. p = &(*p)->rb_right;
  237. else
  238. return parent;
  239. }
  240. do_insert:
  241. rb_link_node(node, parent, p);
  242. rb_insert_color(node, root);
  243. return NULL;
  244. }
  245. static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
  246. struct rb_node **prev_ret,
  247. struct rb_node **next_ret,
  248. struct rb_node ***p_ret,
  249. struct rb_node **parent_ret)
  250. {
  251. struct rb_root *root = &tree->state;
  252. struct rb_node **n = &root->rb_node;
  253. struct rb_node *prev = NULL;
  254. struct rb_node *orig_prev = NULL;
  255. struct tree_entry *entry;
  256. struct tree_entry *prev_entry = NULL;
  257. while (*n) {
  258. prev = *n;
  259. entry = rb_entry(prev, struct tree_entry, rb_node);
  260. prev_entry = entry;
  261. if (offset < entry->start)
  262. n = &(*n)->rb_left;
  263. else if (offset > entry->end)
  264. n = &(*n)->rb_right;
  265. else
  266. return *n;
  267. }
  268. if (p_ret)
  269. *p_ret = n;
  270. if (parent_ret)
  271. *parent_ret = prev;
  272. if (prev_ret) {
  273. orig_prev = prev;
  274. while (prev && offset > prev_entry->end) {
  275. prev = rb_next(prev);
  276. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  277. }
  278. *prev_ret = prev;
  279. prev = orig_prev;
  280. }
  281. if (next_ret) {
  282. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  283. while (prev && offset < prev_entry->start) {
  284. prev = rb_prev(prev);
  285. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  286. }
  287. *next_ret = prev;
  288. }
  289. return NULL;
  290. }
  291. static inline struct rb_node *
  292. tree_search_for_insert(struct extent_io_tree *tree,
  293. u64 offset,
  294. struct rb_node ***p_ret,
  295. struct rb_node **parent_ret)
  296. {
  297. struct rb_node *prev = NULL;
  298. struct rb_node *ret;
  299. ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
  300. if (!ret)
  301. return prev;
  302. return ret;
  303. }
  304. static inline struct rb_node *tree_search(struct extent_io_tree *tree,
  305. u64 offset)
  306. {
  307. return tree_search_for_insert(tree, offset, NULL, NULL);
  308. }
  309. static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
  310. struct extent_state *other)
  311. {
  312. if (tree->ops && tree->ops->merge_extent_hook)
  313. tree->ops->merge_extent_hook(tree->private_data, new, other);
  314. }
  315. /*
  316. * utility function to look for merge candidates inside a given range.
  317. * Any extents with matching state are merged together into a single
  318. * extent in the tree. Extents with EXTENT_IO in their state field
  319. * are not merged because the end_io handlers need to be able to do
  320. * operations on them without sleeping (or doing allocations/splits).
  321. *
  322. * This should be called with the tree lock held.
  323. */
  324. static void merge_state(struct extent_io_tree *tree,
  325. struct extent_state *state)
  326. {
  327. struct extent_state *other;
  328. struct rb_node *other_node;
  329. if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
  330. return;
  331. other_node = rb_prev(&state->rb_node);
  332. if (other_node) {
  333. other = rb_entry(other_node, struct extent_state, rb_node);
  334. if (other->end == state->start - 1 &&
  335. other->state == state->state) {
  336. merge_cb(tree, state, other);
  337. state->start = other->start;
  338. rb_erase(&other->rb_node, &tree->state);
  339. RB_CLEAR_NODE(&other->rb_node);
  340. free_extent_state(other);
  341. }
  342. }
  343. other_node = rb_next(&state->rb_node);
  344. if (other_node) {
  345. other = rb_entry(other_node, struct extent_state, rb_node);
  346. if (other->start == state->end + 1 &&
  347. other->state == state->state) {
  348. merge_cb(tree, state, other);
  349. state->end = other->end;
  350. rb_erase(&other->rb_node, &tree->state);
  351. RB_CLEAR_NODE(&other->rb_node);
  352. free_extent_state(other);
  353. }
  354. }
  355. }
  356. static void set_state_cb(struct extent_io_tree *tree,
  357. struct extent_state *state, unsigned *bits)
  358. {
  359. if (tree->ops && tree->ops->set_bit_hook)
  360. tree->ops->set_bit_hook(tree->private_data, state, bits);
  361. }
  362. static void clear_state_cb(struct extent_io_tree *tree,
  363. struct extent_state *state, unsigned *bits)
  364. {
  365. if (tree->ops && tree->ops->clear_bit_hook)
  366. tree->ops->clear_bit_hook(tree->private_data, state, bits);
  367. }
  368. static void set_state_bits(struct extent_io_tree *tree,
  369. struct extent_state *state, unsigned *bits,
  370. struct extent_changeset *changeset);
  371. /*
  372. * insert an extent_state struct into the tree. 'bits' are set on the
  373. * struct before it is inserted.
  374. *
  375. * This may return -EEXIST if the extent is already there, in which case the
  376. * state struct is freed.
  377. *
  378. * The tree lock is not taken internally. This is a utility function and
  379. * probably isn't what you want to call (see set/clear_extent_bit).
  380. */
  381. static int insert_state(struct extent_io_tree *tree,
  382. struct extent_state *state, u64 start, u64 end,
  383. struct rb_node ***p,
  384. struct rb_node **parent,
  385. unsigned *bits, struct extent_changeset *changeset)
  386. {
  387. struct rb_node *node;
  388. if (end < start)
  389. WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
  390. end, start);
  391. state->start = start;
  392. state->end = end;
  393. set_state_bits(tree, state, bits, changeset);
  394. node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
  395. if (node) {
  396. struct extent_state *found;
  397. found = rb_entry(node, struct extent_state, rb_node);
  398. pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
  399. found->start, found->end, start, end);
  400. return -EEXIST;
  401. }
  402. merge_state(tree, state);
  403. return 0;
  404. }
  405. static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
  406. u64 split)
  407. {
  408. if (tree->ops && tree->ops->split_extent_hook)
  409. tree->ops->split_extent_hook(tree->private_data, orig, split);
  410. }
  411. /*
  412. * split a given extent state struct in two, inserting the preallocated
  413. * struct 'prealloc' as the newly created second half. 'split' indicates an
  414. * offset inside 'orig' where it should be split.
  415. *
  416. * Before calling,
  417. * the tree has 'orig' at [orig->start, orig->end]. After calling, there
  418. * are two extent state structs in the tree:
  419. * prealloc: [orig->start, split - 1]
  420. * orig: [ split, orig->end ]
  421. *
  422. * The tree locks are not taken by this function. They need to be held
  423. * by the caller.
  424. */
  425. static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
  426. struct extent_state *prealloc, u64 split)
  427. {
  428. struct rb_node *node;
  429. split_cb(tree, orig, split);
  430. prealloc->start = orig->start;
  431. prealloc->end = split - 1;
  432. prealloc->state = orig->state;
  433. orig->start = split;
  434. node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
  435. &prealloc->rb_node, NULL, NULL);
  436. if (node) {
  437. free_extent_state(prealloc);
  438. return -EEXIST;
  439. }
  440. return 0;
  441. }
  442. static struct extent_state *next_state(struct extent_state *state)
  443. {
  444. struct rb_node *next = rb_next(&state->rb_node);
  445. if (next)
  446. return rb_entry(next, struct extent_state, rb_node);
  447. else
  448. return NULL;
  449. }
  450. /*
  451. * utility function to clear some bits in an extent state struct.
  452. * it will optionally wake up any one waiting on this state (wake == 1).
  453. *
  454. * If no bits are set on the state struct after clearing things, the
  455. * struct is freed and removed from the tree
  456. */
  457. static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
  458. struct extent_state *state,
  459. unsigned *bits, int wake,
  460. struct extent_changeset *changeset)
  461. {
  462. struct extent_state *next;
  463. unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
  464. if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
  465. u64 range = state->end - state->start + 1;
  466. WARN_ON(range > tree->dirty_bytes);
  467. tree->dirty_bytes -= range;
  468. }
  469. clear_state_cb(tree, state, bits);
  470. add_extent_changeset(state, bits_to_clear, changeset, 0);
  471. state->state &= ~bits_to_clear;
  472. if (wake)
  473. wake_up(&state->wq);
  474. if (state->state == 0) {
  475. next = next_state(state);
  476. if (extent_state_in_tree(state)) {
  477. rb_erase(&state->rb_node, &tree->state);
  478. RB_CLEAR_NODE(&state->rb_node);
  479. free_extent_state(state);
  480. } else {
  481. WARN_ON(1);
  482. }
  483. } else {
  484. merge_state(tree, state);
  485. next = next_state(state);
  486. }
  487. return next;
  488. }
  489. static struct extent_state *
  490. alloc_extent_state_atomic(struct extent_state *prealloc)
  491. {
  492. if (!prealloc)
  493. prealloc = alloc_extent_state(GFP_ATOMIC);
  494. return prealloc;
  495. }
  496. static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
  497. {
  498. btrfs_panic(tree_fs_info(tree), err,
  499. "Locking error: Extent tree was modified by another thread while locked.");
  500. }
  501. /*
  502. * clear some bits on a range in the tree. This may require splitting
  503. * or inserting elements in the tree, so the gfp mask is used to
  504. * indicate which allocations or sleeping are allowed.
  505. *
  506. * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
  507. * the given range from the tree regardless of state (ie for truncate).
  508. *
  509. * the range [start, end] is inclusive.
  510. *
  511. * This takes the tree lock, and returns 0 on success and < 0 on error.
  512. */
  513. static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  514. unsigned bits, int wake, int delete,
  515. struct extent_state **cached_state,
  516. gfp_t mask, struct extent_changeset *changeset)
  517. {
  518. struct extent_state *state;
  519. struct extent_state *cached;
  520. struct extent_state *prealloc = NULL;
  521. struct rb_node *node;
  522. u64 last_end;
  523. int err;
  524. int clear = 0;
  525. btrfs_debug_check_extent_io_range(tree, start, end);
  526. if (bits & EXTENT_DELALLOC)
  527. bits |= EXTENT_NORESERVE;
  528. if (delete)
  529. bits |= ~EXTENT_CTLBITS;
  530. bits |= EXTENT_FIRST_DELALLOC;
  531. if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
  532. clear = 1;
  533. again:
  534. if (!prealloc && gfpflags_allow_blocking(mask)) {
  535. /*
  536. * Don't care for allocation failure here because we might end
  537. * up not needing the pre-allocated extent state at all, which
  538. * is the case if we only have in the tree extent states that
  539. * cover our input range and don't cover too any other range.
  540. * If we end up needing a new extent state we allocate it later.
  541. */
  542. prealloc = alloc_extent_state(mask);
  543. }
  544. spin_lock(&tree->lock);
  545. if (cached_state) {
  546. cached = *cached_state;
  547. if (clear) {
  548. *cached_state = NULL;
  549. cached_state = NULL;
  550. }
  551. if (cached && extent_state_in_tree(cached) &&
  552. cached->start <= start && cached->end > start) {
  553. if (clear)
  554. refcount_dec(&cached->refs);
  555. state = cached;
  556. goto hit_next;
  557. }
  558. if (clear)
  559. free_extent_state(cached);
  560. }
  561. /*
  562. * this search will find the extents that end after
  563. * our range starts
  564. */
  565. node = tree_search(tree, start);
  566. if (!node)
  567. goto out;
  568. state = rb_entry(node, struct extent_state, rb_node);
  569. hit_next:
  570. if (state->start > end)
  571. goto out;
  572. WARN_ON(state->end < start);
  573. last_end = state->end;
  574. /* the state doesn't have the wanted bits, go ahead */
  575. if (!(state->state & bits)) {
  576. state = next_state(state);
  577. goto next;
  578. }
  579. /*
  580. * | ---- desired range ---- |
  581. * | state | or
  582. * | ------------- state -------------- |
  583. *
  584. * We need to split the extent we found, and may flip
  585. * bits on second half.
  586. *
  587. * If the extent we found extends past our range, we
  588. * just split and search again. It'll get split again
  589. * the next time though.
  590. *
  591. * If the extent we found is inside our range, we clear
  592. * the desired bit on it.
  593. */
  594. if (state->start < start) {
  595. prealloc = alloc_extent_state_atomic(prealloc);
  596. BUG_ON(!prealloc);
  597. err = split_state(tree, state, prealloc, start);
  598. if (err)
  599. extent_io_tree_panic(tree, err);
  600. prealloc = NULL;
  601. if (err)
  602. goto out;
  603. if (state->end <= end) {
  604. state = clear_state_bit(tree, state, &bits, wake,
  605. changeset);
  606. goto next;
  607. }
  608. goto search_again;
  609. }
  610. /*
  611. * | ---- desired range ---- |
  612. * | state |
  613. * We need to split the extent, and clear the bit
  614. * on the first half
  615. */
  616. if (state->start <= end && state->end > end) {
  617. prealloc = alloc_extent_state_atomic(prealloc);
  618. BUG_ON(!prealloc);
  619. err = split_state(tree, state, prealloc, end + 1);
  620. if (err)
  621. extent_io_tree_panic(tree, err);
  622. if (wake)
  623. wake_up(&state->wq);
  624. clear_state_bit(tree, prealloc, &bits, wake, changeset);
  625. prealloc = NULL;
  626. goto out;
  627. }
  628. state = clear_state_bit(tree, state, &bits, wake, changeset);
  629. next:
  630. if (last_end == (u64)-1)
  631. goto out;
  632. start = last_end + 1;
  633. if (start <= end && state && !need_resched())
  634. goto hit_next;
  635. search_again:
  636. if (start > end)
  637. goto out;
  638. spin_unlock(&tree->lock);
  639. if (gfpflags_allow_blocking(mask))
  640. cond_resched();
  641. goto again;
  642. out:
  643. spin_unlock(&tree->lock);
  644. if (prealloc)
  645. free_extent_state(prealloc);
  646. return 0;
  647. }
  648. static void wait_on_state(struct extent_io_tree *tree,
  649. struct extent_state *state)
  650. __releases(tree->lock)
  651. __acquires(tree->lock)
  652. {
  653. DEFINE_WAIT(wait);
  654. prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
  655. spin_unlock(&tree->lock);
  656. schedule();
  657. spin_lock(&tree->lock);
  658. finish_wait(&state->wq, &wait);
  659. }
  660. /*
  661. * waits for one or more bits to clear on a range in the state tree.
  662. * The range [start, end] is inclusive.
  663. * The tree lock is taken by this function
  664. */
  665. static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  666. unsigned long bits)
  667. {
  668. struct extent_state *state;
  669. struct rb_node *node;
  670. btrfs_debug_check_extent_io_range(tree, start, end);
  671. spin_lock(&tree->lock);
  672. again:
  673. while (1) {
  674. /*
  675. * this search will find all the extents that end after
  676. * our range starts
  677. */
  678. node = tree_search(tree, start);
  679. process_node:
  680. if (!node)
  681. break;
  682. state = rb_entry(node, struct extent_state, rb_node);
  683. if (state->start > end)
  684. goto out;
  685. if (state->state & bits) {
  686. start = state->start;
  687. refcount_inc(&state->refs);
  688. wait_on_state(tree, state);
  689. free_extent_state(state);
  690. goto again;
  691. }
  692. start = state->end + 1;
  693. if (start > end)
  694. break;
  695. if (!cond_resched_lock(&tree->lock)) {
  696. node = rb_next(node);
  697. goto process_node;
  698. }
  699. }
  700. out:
  701. spin_unlock(&tree->lock);
  702. }
  703. static void set_state_bits(struct extent_io_tree *tree,
  704. struct extent_state *state,
  705. unsigned *bits, struct extent_changeset *changeset)
  706. {
  707. unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
  708. set_state_cb(tree, state, bits);
  709. if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
  710. u64 range = state->end - state->start + 1;
  711. tree->dirty_bytes += range;
  712. }
  713. add_extent_changeset(state, bits_to_set, changeset, 1);
  714. state->state |= bits_to_set;
  715. }
  716. static void cache_state_if_flags(struct extent_state *state,
  717. struct extent_state **cached_ptr,
  718. unsigned flags)
  719. {
  720. if (cached_ptr && !(*cached_ptr)) {
  721. if (!flags || (state->state & flags)) {
  722. *cached_ptr = state;
  723. refcount_inc(&state->refs);
  724. }
  725. }
  726. }
  727. static void cache_state(struct extent_state *state,
  728. struct extent_state **cached_ptr)
  729. {
  730. return cache_state_if_flags(state, cached_ptr,
  731. EXTENT_IOBITS | EXTENT_BOUNDARY);
  732. }
  733. /*
  734. * set some bits on a range in the tree. This may require allocations or
  735. * sleeping, so the gfp mask is used to indicate what is allowed.
  736. *
  737. * If any of the exclusive bits are set, this will fail with -EEXIST if some
  738. * part of the range already has the desired bits set. The start of the
  739. * existing range is returned in failed_start in this case.
  740. *
  741. * [start, end] is inclusive This takes the tree lock.
  742. */
  743. static int __must_check
  744. __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  745. unsigned bits, unsigned exclusive_bits,
  746. u64 *failed_start, struct extent_state **cached_state,
  747. gfp_t mask, struct extent_changeset *changeset)
  748. {
  749. struct extent_state *state;
  750. struct extent_state *prealloc = NULL;
  751. struct rb_node *node;
  752. struct rb_node **p;
  753. struct rb_node *parent;
  754. int err = 0;
  755. u64 last_start;
  756. u64 last_end;
  757. btrfs_debug_check_extent_io_range(tree, start, end);
  758. bits |= EXTENT_FIRST_DELALLOC;
  759. again:
  760. if (!prealloc && gfpflags_allow_blocking(mask)) {
  761. /*
  762. * Don't care for allocation failure here because we might end
  763. * up not needing the pre-allocated extent state at all, which
  764. * is the case if we only have in the tree extent states that
  765. * cover our input range and don't cover too any other range.
  766. * If we end up needing a new extent state we allocate it later.
  767. */
  768. prealloc = alloc_extent_state(mask);
  769. }
  770. spin_lock(&tree->lock);
  771. if (cached_state && *cached_state) {
  772. state = *cached_state;
  773. if (state->start <= start && state->end > start &&
  774. extent_state_in_tree(state)) {
  775. node = &state->rb_node;
  776. goto hit_next;
  777. }
  778. }
  779. /*
  780. * this search will find all the extents that end after
  781. * our range starts.
  782. */
  783. node = tree_search_for_insert(tree, start, &p, &parent);
  784. if (!node) {
  785. prealloc = alloc_extent_state_atomic(prealloc);
  786. BUG_ON(!prealloc);
  787. err = insert_state(tree, prealloc, start, end,
  788. &p, &parent, &bits, changeset);
  789. if (err)
  790. extent_io_tree_panic(tree, err);
  791. cache_state(prealloc, cached_state);
  792. prealloc = NULL;
  793. goto out;
  794. }
  795. state = rb_entry(node, struct extent_state, rb_node);
  796. hit_next:
  797. last_start = state->start;
  798. last_end = state->end;
  799. /*
  800. * | ---- desired range ---- |
  801. * | state |
  802. *
  803. * Just lock what we found and keep going
  804. */
  805. if (state->start == start && state->end <= end) {
  806. if (state->state & exclusive_bits) {
  807. *failed_start = state->start;
  808. err = -EEXIST;
  809. goto out;
  810. }
  811. set_state_bits(tree, state, &bits, changeset);
  812. cache_state(state, cached_state);
  813. merge_state(tree, state);
  814. if (last_end == (u64)-1)
  815. goto out;
  816. start = last_end + 1;
  817. state = next_state(state);
  818. if (start < end && state && state->start == start &&
  819. !need_resched())
  820. goto hit_next;
  821. goto search_again;
  822. }
  823. /*
  824. * | ---- desired range ---- |
  825. * | state |
  826. * or
  827. * | ------------- state -------------- |
  828. *
  829. * We need to split the extent we found, and may flip bits on
  830. * second half.
  831. *
  832. * If the extent we found extends past our
  833. * range, we just split and search again. It'll get split
  834. * again the next time though.
  835. *
  836. * If the extent we found is inside our range, we set the
  837. * desired bit on it.
  838. */
  839. if (state->start < start) {
  840. if (state->state & exclusive_bits) {
  841. *failed_start = start;
  842. err = -EEXIST;
  843. goto out;
  844. }
  845. prealloc = alloc_extent_state_atomic(prealloc);
  846. BUG_ON(!prealloc);
  847. err = split_state(tree, state, prealloc, start);
  848. if (err)
  849. extent_io_tree_panic(tree, err);
  850. prealloc = NULL;
  851. if (err)
  852. goto out;
  853. if (state->end <= end) {
  854. set_state_bits(tree, state, &bits, changeset);
  855. cache_state(state, cached_state);
  856. merge_state(tree, state);
  857. if (last_end == (u64)-1)
  858. goto out;
  859. start = last_end + 1;
  860. state = next_state(state);
  861. if (start < end && state && state->start == start &&
  862. !need_resched())
  863. goto hit_next;
  864. }
  865. goto search_again;
  866. }
  867. /*
  868. * | ---- desired range ---- |
  869. * | state | or | state |
  870. *
  871. * There's a hole, we need to insert something in it and
  872. * ignore the extent we found.
  873. */
  874. if (state->start > start) {
  875. u64 this_end;
  876. if (end < last_start)
  877. this_end = end;
  878. else
  879. this_end = last_start - 1;
  880. prealloc = alloc_extent_state_atomic(prealloc);
  881. BUG_ON(!prealloc);
  882. /*
  883. * Avoid to free 'prealloc' if it can be merged with
  884. * the later extent.
  885. */
  886. err = insert_state(tree, prealloc, start, this_end,
  887. NULL, NULL, &bits, changeset);
  888. if (err)
  889. extent_io_tree_panic(tree, err);
  890. cache_state(prealloc, cached_state);
  891. prealloc = NULL;
  892. start = this_end + 1;
  893. goto search_again;
  894. }
  895. /*
  896. * | ---- desired range ---- |
  897. * | state |
  898. * We need to split the extent, and set the bit
  899. * on the first half
  900. */
  901. if (state->start <= end && state->end > end) {
  902. if (state->state & exclusive_bits) {
  903. *failed_start = start;
  904. err = -EEXIST;
  905. goto out;
  906. }
  907. prealloc = alloc_extent_state_atomic(prealloc);
  908. BUG_ON(!prealloc);
  909. err = split_state(tree, state, prealloc, end + 1);
  910. if (err)
  911. extent_io_tree_panic(tree, err);
  912. set_state_bits(tree, prealloc, &bits, changeset);
  913. cache_state(prealloc, cached_state);
  914. merge_state(tree, prealloc);
  915. prealloc = NULL;
  916. goto out;
  917. }
  918. search_again:
  919. if (start > end)
  920. goto out;
  921. spin_unlock(&tree->lock);
  922. if (gfpflags_allow_blocking(mask))
  923. cond_resched();
  924. goto again;
  925. out:
  926. spin_unlock(&tree->lock);
  927. if (prealloc)
  928. free_extent_state(prealloc);
  929. return err;
  930. }
  931. int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  932. unsigned bits, u64 * failed_start,
  933. struct extent_state **cached_state, gfp_t mask)
  934. {
  935. return __set_extent_bit(tree, start, end, bits, 0, failed_start,
  936. cached_state, mask, NULL);
  937. }
  938. /**
  939. * convert_extent_bit - convert all bits in a given range from one bit to
  940. * another
  941. * @tree: the io tree to search
  942. * @start: the start offset in bytes
  943. * @end: the end offset in bytes (inclusive)
  944. * @bits: the bits to set in this range
  945. * @clear_bits: the bits to clear in this range
  946. * @cached_state: state that we're going to cache
  947. *
  948. * This will go through and set bits for the given range. If any states exist
  949. * already in this range they are set with the given bit and cleared of the
  950. * clear_bits. This is only meant to be used by things that are mergeable, ie
  951. * converting from say DELALLOC to DIRTY. This is not meant to be used with
  952. * boundary bits like LOCK.
  953. *
  954. * All allocations are done with GFP_NOFS.
  955. */
  956. int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  957. unsigned bits, unsigned clear_bits,
  958. struct extent_state **cached_state)
  959. {
  960. struct extent_state *state;
  961. struct extent_state *prealloc = NULL;
  962. struct rb_node *node;
  963. struct rb_node **p;
  964. struct rb_node *parent;
  965. int err = 0;
  966. u64 last_start;
  967. u64 last_end;
  968. bool first_iteration = true;
  969. btrfs_debug_check_extent_io_range(tree, start, end);
  970. again:
  971. if (!prealloc) {
  972. /*
  973. * Best effort, don't worry if extent state allocation fails
  974. * here for the first iteration. We might have a cached state
  975. * that matches exactly the target range, in which case no
  976. * extent state allocations are needed. We'll only know this
  977. * after locking the tree.
  978. */
  979. prealloc = alloc_extent_state(GFP_NOFS);
  980. if (!prealloc && !first_iteration)
  981. return -ENOMEM;
  982. }
  983. spin_lock(&tree->lock);
  984. if (cached_state && *cached_state) {
  985. state = *cached_state;
  986. if (state->start <= start && state->end > start &&
  987. extent_state_in_tree(state)) {
  988. node = &state->rb_node;
  989. goto hit_next;
  990. }
  991. }
  992. /*
  993. * this search will find all the extents that end after
  994. * our range starts.
  995. */
  996. node = tree_search_for_insert(tree, start, &p, &parent);
  997. if (!node) {
  998. prealloc = alloc_extent_state_atomic(prealloc);
  999. if (!prealloc) {
  1000. err = -ENOMEM;
  1001. goto out;
  1002. }
  1003. err = insert_state(tree, prealloc, start, end,
  1004. &p, &parent, &bits, NULL);
  1005. if (err)
  1006. extent_io_tree_panic(tree, err);
  1007. cache_state(prealloc, cached_state);
  1008. prealloc = NULL;
  1009. goto out;
  1010. }
  1011. state = rb_entry(node, struct extent_state, rb_node);
  1012. hit_next:
  1013. last_start = state->start;
  1014. last_end = state->end;
  1015. /*
  1016. * | ---- desired range ---- |
  1017. * | state |
  1018. *
  1019. * Just lock what we found and keep going
  1020. */
  1021. if (state->start == start && state->end <= end) {
  1022. set_state_bits(tree, state, &bits, NULL);
  1023. cache_state(state, cached_state);
  1024. state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
  1025. if (last_end == (u64)-1)
  1026. goto out;
  1027. start = last_end + 1;
  1028. if (start < end && state && state->start == start &&
  1029. !need_resched())
  1030. goto hit_next;
  1031. goto search_again;
  1032. }
  1033. /*
  1034. * | ---- desired range ---- |
  1035. * | state |
  1036. * or
  1037. * | ------------- state -------------- |
  1038. *
  1039. * We need to split the extent we found, and may flip bits on
  1040. * second half.
  1041. *
  1042. * If the extent we found extends past our
  1043. * range, we just split and search again. It'll get split
  1044. * again the next time though.
  1045. *
  1046. * If the extent we found is inside our range, we set the
  1047. * desired bit on it.
  1048. */
  1049. if (state->start < start) {
  1050. prealloc = alloc_extent_state_atomic(prealloc);
  1051. if (!prealloc) {
  1052. err = -ENOMEM;
  1053. goto out;
  1054. }
  1055. err = split_state(tree, state, prealloc, start);
  1056. if (err)
  1057. extent_io_tree_panic(tree, err);
  1058. prealloc = NULL;
  1059. if (err)
  1060. goto out;
  1061. if (state->end <= end) {
  1062. set_state_bits(tree, state, &bits, NULL);
  1063. cache_state(state, cached_state);
  1064. state = clear_state_bit(tree, state, &clear_bits, 0,
  1065. NULL);
  1066. if (last_end == (u64)-1)
  1067. goto out;
  1068. start = last_end + 1;
  1069. if (start < end && state && state->start == start &&
  1070. !need_resched())
  1071. goto hit_next;
  1072. }
  1073. goto search_again;
  1074. }
  1075. /*
  1076. * | ---- desired range ---- |
  1077. * | state | or | state |
  1078. *
  1079. * There's a hole, we need to insert something in it and
  1080. * ignore the extent we found.
  1081. */
  1082. if (state->start > start) {
  1083. u64 this_end;
  1084. if (end < last_start)
  1085. this_end = end;
  1086. else
  1087. this_end = last_start - 1;
  1088. prealloc = alloc_extent_state_atomic(prealloc);
  1089. if (!prealloc) {
  1090. err = -ENOMEM;
  1091. goto out;
  1092. }
  1093. /*
  1094. * Avoid to free 'prealloc' if it can be merged with
  1095. * the later extent.
  1096. */
  1097. err = insert_state(tree, prealloc, start, this_end,
  1098. NULL, NULL, &bits, NULL);
  1099. if (err)
  1100. extent_io_tree_panic(tree, err);
  1101. cache_state(prealloc, cached_state);
  1102. prealloc = NULL;
  1103. start = this_end + 1;
  1104. goto search_again;
  1105. }
  1106. /*
  1107. * | ---- desired range ---- |
  1108. * | state |
  1109. * We need to split the extent, and set the bit
  1110. * on the first half
  1111. */
  1112. if (state->start <= end && state->end > end) {
  1113. prealloc = alloc_extent_state_atomic(prealloc);
  1114. if (!prealloc) {
  1115. err = -ENOMEM;
  1116. goto out;
  1117. }
  1118. err = split_state(tree, state, prealloc, end + 1);
  1119. if (err)
  1120. extent_io_tree_panic(tree, err);
  1121. set_state_bits(tree, prealloc, &bits, NULL);
  1122. cache_state(prealloc, cached_state);
  1123. clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
  1124. prealloc = NULL;
  1125. goto out;
  1126. }
  1127. search_again:
  1128. if (start > end)
  1129. goto out;
  1130. spin_unlock(&tree->lock);
  1131. cond_resched();
  1132. first_iteration = false;
  1133. goto again;
  1134. out:
  1135. spin_unlock(&tree->lock);
  1136. if (prealloc)
  1137. free_extent_state(prealloc);
  1138. return err;
  1139. }
  1140. /* wrappers around set/clear extent bit */
  1141. int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1142. unsigned bits, struct extent_changeset *changeset)
  1143. {
  1144. /*
  1145. * We don't support EXTENT_LOCKED yet, as current changeset will
  1146. * record any bits changed, so for EXTENT_LOCKED case, it will
  1147. * either fail with -EEXIST or changeset will record the whole
  1148. * range.
  1149. */
  1150. BUG_ON(bits & EXTENT_LOCKED);
  1151. return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
  1152. changeset);
  1153. }
  1154. int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  1155. unsigned bits, int wake, int delete,
  1156. struct extent_state **cached, gfp_t mask)
  1157. {
  1158. return __clear_extent_bit(tree, start, end, bits, wake, delete,
  1159. cached, mask, NULL);
  1160. }
  1161. int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1162. unsigned bits, struct extent_changeset *changeset)
  1163. {
  1164. /*
  1165. * Don't support EXTENT_LOCKED case, same reason as
  1166. * set_record_extent_bits().
  1167. */
  1168. BUG_ON(bits & EXTENT_LOCKED);
  1169. return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
  1170. changeset);
  1171. }
  1172. /*
  1173. * either insert or lock state struct between start and end use mask to tell
  1174. * us if waiting is desired.
  1175. */
  1176. int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1177. struct extent_state **cached_state)
  1178. {
  1179. int err;
  1180. u64 failed_start;
  1181. while (1) {
  1182. err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
  1183. EXTENT_LOCKED, &failed_start,
  1184. cached_state, GFP_NOFS, NULL);
  1185. if (err == -EEXIST) {
  1186. wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
  1187. start = failed_start;
  1188. } else
  1189. break;
  1190. WARN_ON(start > end);
  1191. }
  1192. return err;
  1193. }
  1194. int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
  1195. {
  1196. int err;
  1197. u64 failed_start;
  1198. err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
  1199. &failed_start, NULL, GFP_NOFS, NULL);
  1200. if (err == -EEXIST) {
  1201. if (failed_start > start)
  1202. clear_extent_bit(tree, start, failed_start - 1,
  1203. EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
  1204. return 0;
  1205. }
  1206. return 1;
  1207. }
  1208. void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
  1209. {
  1210. unsigned long index = start >> PAGE_SHIFT;
  1211. unsigned long end_index = end >> PAGE_SHIFT;
  1212. struct page *page;
  1213. while (index <= end_index) {
  1214. page = find_get_page(inode->i_mapping, index);
  1215. BUG_ON(!page); /* Pages should be in the extent_io_tree */
  1216. clear_page_dirty_for_io(page);
  1217. put_page(page);
  1218. index++;
  1219. }
  1220. }
  1221. void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
  1222. {
  1223. unsigned long index = start >> PAGE_SHIFT;
  1224. unsigned long end_index = end >> PAGE_SHIFT;
  1225. struct page *page;
  1226. while (index <= end_index) {
  1227. page = find_get_page(inode->i_mapping, index);
  1228. BUG_ON(!page); /* Pages should be in the extent_io_tree */
  1229. __set_page_dirty_nobuffers(page);
  1230. account_page_redirty(page);
  1231. put_page(page);
  1232. index++;
  1233. }
  1234. }
  1235. /*
  1236. * helper function to set both pages and extents in the tree writeback
  1237. */
  1238. static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
  1239. {
  1240. tree->ops->set_range_writeback(tree->private_data, start, end);
  1241. }
  1242. /* find the first state struct with 'bits' set after 'start', and
  1243. * return it. tree->lock must be held. NULL will returned if
  1244. * nothing was found after 'start'
  1245. */
  1246. static struct extent_state *
  1247. find_first_extent_bit_state(struct extent_io_tree *tree,
  1248. u64 start, unsigned bits)
  1249. {
  1250. struct rb_node *node;
  1251. struct extent_state *state;
  1252. /*
  1253. * this search will find all the extents that end after
  1254. * our range starts.
  1255. */
  1256. node = tree_search(tree, start);
  1257. if (!node)
  1258. goto out;
  1259. while (1) {
  1260. state = rb_entry(node, struct extent_state, rb_node);
  1261. if (state->end >= start && (state->state & bits))
  1262. return state;
  1263. node = rb_next(node);
  1264. if (!node)
  1265. break;
  1266. }
  1267. out:
  1268. return NULL;
  1269. }
  1270. /*
  1271. * find the first offset in the io tree with 'bits' set. zero is
  1272. * returned if we find something, and *start_ret and *end_ret are
  1273. * set to reflect the state struct that was found.
  1274. *
  1275. * If nothing was found, 1 is returned. If found something, return 0.
  1276. */
  1277. int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
  1278. u64 *start_ret, u64 *end_ret, unsigned bits,
  1279. struct extent_state **cached_state)
  1280. {
  1281. struct extent_state *state;
  1282. struct rb_node *n;
  1283. int ret = 1;
  1284. spin_lock(&tree->lock);
  1285. if (cached_state && *cached_state) {
  1286. state = *cached_state;
  1287. if (state->end == start - 1 && extent_state_in_tree(state)) {
  1288. n = rb_next(&state->rb_node);
  1289. while (n) {
  1290. state = rb_entry(n, struct extent_state,
  1291. rb_node);
  1292. if (state->state & bits)
  1293. goto got_it;
  1294. n = rb_next(n);
  1295. }
  1296. free_extent_state(*cached_state);
  1297. *cached_state = NULL;
  1298. goto out;
  1299. }
  1300. free_extent_state(*cached_state);
  1301. *cached_state = NULL;
  1302. }
  1303. state = find_first_extent_bit_state(tree, start, bits);
  1304. got_it:
  1305. if (state) {
  1306. cache_state_if_flags(state, cached_state, 0);
  1307. *start_ret = state->start;
  1308. *end_ret = state->end;
  1309. ret = 0;
  1310. }
  1311. out:
  1312. spin_unlock(&tree->lock);
  1313. return ret;
  1314. }
  1315. /*
  1316. * find a contiguous range of bytes in the file marked as delalloc, not
  1317. * more than 'max_bytes'. start and end are used to return the range,
  1318. *
  1319. * 1 is returned if we find something, 0 if nothing was in the tree
  1320. */
  1321. static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
  1322. u64 *start, u64 *end, u64 max_bytes,
  1323. struct extent_state **cached_state)
  1324. {
  1325. struct rb_node *node;
  1326. struct extent_state *state;
  1327. u64 cur_start = *start;
  1328. u64 found = 0;
  1329. u64 total_bytes = 0;
  1330. spin_lock(&tree->lock);
  1331. /*
  1332. * this search will find all the extents that end after
  1333. * our range starts.
  1334. */
  1335. node = tree_search(tree, cur_start);
  1336. if (!node) {
  1337. if (!found)
  1338. *end = (u64)-1;
  1339. goto out;
  1340. }
  1341. while (1) {
  1342. state = rb_entry(node, struct extent_state, rb_node);
  1343. if (found && (state->start != cur_start ||
  1344. (state->state & EXTENT_BOUNDARY))) {
  1345. goto out;
  1346. }
  1347. if (!(state->state & EXTENT_DELALLOC)) {
  1348. if (!found)
  1349. *end = state->end;
  1350. goto out;
  1351. }
  1352. if (!found) {
  1353. *start = state->start;
  1354. *cached_state = state;
  1355. refcount_inc(&state->refs);
  1356. }
  1357. found++;
  1358. *end = state->end;
  1359. cur_start = state->end + 1;
  1360. node = rb_next(node);
  1361. total_bytes += state->end - state->start + 1;
  1362. if (total_bytes >= max_bytes)
  1363. break;
  1364. if (!node)
  1365. break;
  1366. }
  1367. out:
  1368. spin_unlock(&tree->lock);
  1369. return found;
  1370. }
  1371. static int __process_pages_contig(struct address_space *mapping,
  1372. struct page *locked_page,
  1373. pgoff_t start_index, pgoff_t end_index,
  1374. unsigned long page_ops, pgoff_t *index_ret);
  1375. static noinline void __unlock_for_delalloc(struct inode *inode,
  1376. struct page *locked_page,
  1377. u64 start, u64 end)
  1378. {
  1379. unsigned long index = start >> PAGE_SHIFT;
  1380. unsigned long end_index = end >> PAGE_SHIFT;
  1381. ASSERT(locked_page);
  1382. if (index == locked_page->index && end_index == index)
  1383. return;
  1384. __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
  1385. PAGE_UNLOCK, NULL);
  1386. }
  1387. static noinline int lock_delalloc_pages(struct inode *inode,
  1388. struct page *locked_page,
  1389. u64 delalloc_start,
  1390. u64 delalloc_end)
  1391. {
  1392. unsigned long index = delalloc_start >> PAGE_SHIFT;
  1393. unsigned long index_ret = index;
  1394. unsigned long end_index = delalloc_end >> PAGE_SHIFT;
  1395. int ret;
  1396. ASSERT(locked_page);
  1397. if (index == locked_page->index && index == end_index)
  1398. return 0;
  1399. ret = __process_pages_contig(inode->i_mapping, locked_page, index,
  1400. end_index, PAGE_LOCK, &index_ret);
  1401. if (ret == -EAGAIN)
  1402. __unlock_for_delalloc(inode, locked_page, delalloc_start,
  1403. (u64)index_ret << PAGE_SHIFT);
  1404. return ret;
  1405. }
  1406. /*
  1407. * find a contiguous range of bytes in the file marked as delalloc, not
  1408. * more than 'max_bytes'. start and end are used to return the range,
  1409. *
  1410. * 1 is returned if we find something, 0 if nothing was in the tree
  1411. */
  1412. STATIC u64 find_lock_delalloc_range(struct inode *inode,
  1413. struct extent_io_tree *tree,
  1414. struct page *locked_page, u64 *start,
  1415. u64 *end, u64 max_bytes)
  1416. {
  1417. u64 delalloc_start;
  1418. u64 delalloc_end;
  1419. u64 found;
  1420. struct extent_state *cached_state = NULL;
  1421. int ret;
  1422. int loops = 0;
  1423. again:
  1424. /* step one, find a bunch of delalloc bytes starting at start */
  1425. delalloc_start = *start;
  1426. delalloc_end = 0;
  1427. found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
  1428. max_bytes, &cached_state);
  1429. if (!found || delalloc_end <= *start) {
  1430. *start = delalloc_start;
  1431. *end = delalloc_end;
  1432. free_extent_state(cached_state);
  1433. return 0;
  1434. }
  1435. /*
  1436. * start comes from the offset of locked_page. We have to lock
  1437. * pages in order, so we can't process delalloc bytes before
  1438. * locked_page
  1439. */
  1440. if (delalloc_start < *start)
  1441. delalloc_start = *start;
  1442. /*
  1443. * make sure to limit the number of pages we try to lock down
  1444. */
  1445. if (delalloc_end + 1 - delalloc_start > max_bytes)
  1446. delalloc_end = delalloc_start + max_bytes - 1;
  1447. /* step two, lock all the pages after the page that has start */
  1448. ret = lock_delalloc_pages(inode, locked_page,
  1449. delalloc_start, delalloc_end);
  1450. if (ret == -EAGAIN) {
  1451. /* some of the pages are gone, lets avoid looping by
  1452. * shortening the size of the delalloc range we're searching
  1453. */
  1454. free_extent_state(cached_state);
  1455. cached_state = NULL;
  1456. if (!loops) {
  1457. max_bytes = PAGE_SIZE;
  1458. loops = 1;
  1459. goto again;
  1460. } else {
  1461. found = 0;
  1462. goto out_failed;
  1463. }
  1464. }
  1465. BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
  1466. /* step three, lock the state bits for the whole range */
  1467. lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
  1468. /* then test to make sure it is all still delalloc */
  1469. ret = test_range_bit(tree, delalloc_start, delalloc_end,
  1470. EXTENT_DELALLOC, 1, cached_state);
  1471. if (!ret) {
  1472. unlock_extent_cached(tree, delalloc_start, delalloc_end,
  1473. &cached_state, GFP_NOFS);
  1474. __unlock_for_delalloc(inode, locked_page,
  1475. delalloc_start, delalloc_end);
  1476. cond_resched();
  1477. goto again;
  1478. }
  1479. free_extent_state(cached_state);
  1480. *start = delalloc_start;
  1481. *end = delalloc_end;
  1482. out_failed:
  1483. return found;
  1484. }
  1485. static int __process_pages_contig(struct address_space *mapping,
  1486. struct page *locked_page,
  1487. pgoff_t start_index, pgoff_t end_index,
  1488. unsigned long page_ops, pgoff_t *index_ret)
  1489. {
  1490. unsigned long nr_pages = end_index - start_index + 1;
  1491. unsigned long pages_locked = 0;
  1492. pgoff_t index = start_index;
  1493. struct page *pages[16];
  1494. unsigned ret;
  1495. int err = 0;
  1496. int i;
  1497. if (page_ops & PAGE_LOCK) {
  1498. ASSERT(page_ops == PAGE_LOCK);
  1499. ASSERT(index_ret && *index_ret == start_index);
  1500. }
  1501. if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
  1502. mapping_set_error(mapping, -EIO);
  1503. while (nr_pages > 0) {
  1504. ret = find_get_pages_contig(mapping, index,
  1505. min_t(unsigned long,
  1506. nr_pages, ARRAY_SIZE(pages)), pages);
  1507. if (ret == 0) {
  1508. /*
  1509. * Only if we're going to lock these pages,
  1510. * can we find nothing at @index.
  1511. */
  1512. ASSERT(page_ops & PAGE_LOCK);
  1513. err = -EAGAIN;
  1514. goto out;
  1515. }
  1516. for (i = 0; i < ret; i++) {
  1517. if (page_ops & PAGE_SET_PRIVATE2)
  1518. SetPagePrivate2(pages[i]);
  1519. if (pages[i] == locked_page) {
  1520. put_page(pages[i]);
  1521. pages_locked++;
  1522. continue;
  1523. }
  1524. if (page_ops & PAGE_CLEAR_DIRTY)
  1525. clear_page_dirty_for_io(pages[i]);
  1526. if (page_ops & PAGE_SET_WRITEBACK)
  1527. set_page_writeback(pages[i]);
  1528. if (page_ops & PAGE_SET_ERROR)
  1529. SetPageError(pages[i]);
  1530. if (page_ops & PAGE_END_WRITEBACK)
  1531. end_page_writeback(pages[i]);
  1532. if (page_ops & PAGE_UNLOCK)
  1533. unlock_page(pages[i]);
  1534. if (page_ops & PAGE_LOCK) {
  1535. lock_page(pages[i]);
  1536. if (!PageDirty(pages[i]) ||
  1537. pages[i]->mapping != mapping) {
  1538. unlock_page(pages[i]);
  1539. put_page(pages[i]);
  1540. err = -EAGAIN;
  1541. goto out;
  1542. }
  1543. }
  1544. put_page(pages[i]);
  1545. pages_locked++;
  1546. }
  1547. nr_pages -= ret;
  1548. index += ret;
  1549. cond_resched();
  1550. }
  1551. out:
  1552. if (err && index_ret)
  1553. *index_ret = start_index + pages_locked - 1;
  1554. return err;
  1555. }
  1556. void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
  1557. u64 delalloc_end, struct page *locked_page,
  1558. unsigned clear_bits,
  1559. unsigned long page_ops)
  1560. {
  1561. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
  1562. NULL, GFP_NOFS);
  1563. __process_pages_contig(inode->i_mapping, locked_page,
  1564. start >> PAGE_SHIFT, end >> PAGE_SHIFT,
  1565. page_ops, NULL);
  1566. }
  1567. /*
  1568. * count the number of bytes in the tree that have a given bit(s)
  1569. * set. This can be fairly slow, except for EXTENT_DIRTY which is
  1570. * cached. The total number found is returned.
  1571. */
  1572. u64 count_range_bits(struct extent_io_tree *tree,
  1573. u64 *start, u64 search_end, u64 max_bytes,
  1574. unsigned bits, int contig)
  1575. {
  1576. struct rb_node *node;
  1577. struct extent_state *state;
  1578. u64 cur_start = *start;
  1579. u64 total_bytes = 0;
  1580. u64 last = 0;
  1581. int found = 0;
  1582. if (WARN_ON(search_end <= cur_start))
  1583. return 0;
  1584. spin_lock(&tree->lock);
  1585. if (cur_start == 0 && bits == EXTENT_DIRTY) {
  1586. total_bytes = tree->dirty_bytes;
  1587. goto out;
  1588. }
  1589. /*
  1590. * this search will find all the extents that end after
  1591. * our range starts.
  1592. */
  1593. node = tree_search(tree, cur_start);
  1594. if (!node)
  1595. goto out;
  1596. while (1) {
  1597. state = rb_entry(node, struct extent_state, rb_node);
  1598. if (state->start > search_end)
  1599. break;
  1600. if (contig && found && state->start > last + 1)
  1601. break;
  1602. if (state->end >= cur_start && (state->state & bits) == bits) {
  1603. total_bytes += min(search_end, state->end) + 1 -
  1604. max(cur_start, state->start);
  1605. if (total_bytes >= max_bytes)
  1606. break;
  1607. if (!found) {
  1608. *start = max(cur_start, state->start);
  1609. found = 1;
  1610. }
  1611. last = state->end;
  1612. } else if (contig && found) {
  1613. break;
  1614. }
  1615. node = rb_next(node);
  1616. if (!node)
  1617. break;
  1618. }
  1619. out:
  1620. spin_unlock(&tree->lock);
  1621. return total_bytes;
  1622. }
  1623. /*
  1624. * set the private field for a given byte offset in the tree. If there isn't
  1625. * an extent_state there already, this does nothing.
  1626. */
  1627. static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
  1628. struct io_failure_record *failrec)
  1629. {
  1630. struct rb_node *node;
  1631. struct extent_state *state;
  1632. int ret = 0;
  1633. spin_lock(&tree->lock);
  1634. /*
  1635. * this search will find all the extents that end after
  1636. * our range starts.
  1637. */
  1638. node = tree_search(tree, start);
  1639. if (!node) {
  1640. ret = -ENOENT;
  1641. goto out;
  1642. }
  1643. state = rb_entry(node, struct extent_state, rb_node);
  1644. if (state->start != start) {
  1645. ret = -ENOENT;
  1646. goto out;
  1647. }
  1648. state->failrec = failrec;
  1649. out:
  1650. spin_unlock(&tree->lock);
  1651. return ret;
  1652. }
  1653. static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
  1654. struct io_failure_record **failrec)
  1655. {
  1656. struct rb_node *node;
  1657. struct extent_state *state;
  1658. int ret = 0;
  1659. spin_lock(&tree->lock);
  1660. /*
  1661. * this search will find all the extents that end after
  1662. * our range starts.
  1663. */
  1664. node = tree_search(tree, start);
  1665. if (!node) {
  1666. ret = -ENOENT;
  1667. goto out;
  1668. }
  1669. state = rb_entry(node, struct extent_state, rb_node);
  1670. if (state->start != start) {
  1671. ret = -ENOENT;
  1672. goto out;
  1673. }
  1674. *failrec = state->failrec;
  1675. out:
  1676. spin_unlock(&tree->lock);
  1677. return ret;
  1678. }
  1679. /*
  1680. * searches a range in the state tree for a given mask.
  1681. * If 'filled' == 1, this returns 1 only if every extent in the tree
  1682. * has the bits set. Otherwise, 1 is returned if any bit in the
  1683. * range is found set.
  1684. */
  1685. int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
  1686. unsigned bits, int filled, struct extent_state *cached)
  1687. {
  1688. struct extent_state *state = NULL;
  1689. struct rb_node *node;
  1690. int bitset = 0;
  1691. spin_lock(&tree->lock);
  1692. if (cached && extent_state_in_tree(cached) && cached->start <= start &&
  1693. cached->end > start)
  1694. node = &cached->rb_node;
  1695. else
  1696. node = tree_search(tree, start);
  1697. while (node && start <= end) {
  1698. state = rb_entry(node, struct extent_state, rb_node);
  1699. if (filled && state->start > start) {
  1700. bitset = 0;
  1701. break;
  1702. }
  1703. if (state->start > end)
  1704. break;
  1705. if (state->state & bits) {
  1706. bitset = 1;
  1707. if (!filled)
  1708. break;
  1709. } else if (filled) {
  1710. bitset = 0;
  1711. break;
  1712. }
  1713. if (state->end == (u64)-1)
  1714. break;
  1715. start = state->end + 1;
  1716. if (start > end)
  1717. break;
  1718. node = rb_next(node);
  1719. if (!node) {
  1720. if (filled)
  1721. bitset = 0;
  1722. break;
  1723. }
  1724. }
  1725. spin_unlock(&tree->lock);
  1726. return bitset;
  1727. }
  1728. /*
  1729. * helper function to set a given page up to date if all the
  1730. * extents in the tree for that page are up to date
  1731. */
  1732. static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
  1733. {
  1734. u64 start = page_offset(page);
  1735. u64 end = start + PAGE_SIZE - 1;
  1736. if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
  1737. SetPageUptodate(page);
  1738. }
  1739. int free_io_failure(struct extent_io_tree *failure_tree,
  1740. struct extent_io_tree *io_tree,
  1741. struct io_failure_record *rec)
  1742. {
  1743. int ret;
  1744. int err = 0;
  1745. set_state_failrec(failure_tree, rec->start, NULL);
  1746. ret = clear_extent_bits(failure_tree, rec->start,
  1747. rec->start + rec->len - 1,
  1748. EXTENT_LOCKED | EXTENT_DIRTY);
  1749. if (ret)
  1750. err = ret;
  1751. ret = clear_extent_bits(io_tree, rec->start,
  1752. rec->start + rec->len - 1,
  1753. EXTENT_DAMAGED);
  1754. if (ret && !err)
  1755. err = ret;
  1756. kfree(rec);
  1757. return err;
  1758. }
  1759. /*
  1760. * this bypasses the standard btrfs submit functions deliberately, as
  1761. * the standard behavior is to write all copies in a raid setup. here we only
  1762. * want to write the one bad copy. so we do the mapping for ourselves and issue
  1763. * submit_bio directly.
  1764. * to avoid any synchronization issues, wait for the data after writing, which
  1765. * actually prevents the read that triggered the error from finishing.
  1766. * currently, there can be no more than two copies of every data bit. thus,
  1767. * exactly one rewrite is required.
  1768. */
  1769. int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
  1770. u64 length, u64 logical, struct page *page,
  1771. unsigned int pg_offset, int mirror_num)
  1772. {
  1773. struct bio *bio;
  1774. struct btrfs_device *dev;
  1775. u64 map_length = 0;
  1776. u64 sector;
  1777. struct btrfs_bio *bbio = NULL;
  1778. int ret;
  1779. ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
  1780. BUG_ON(!mirror_num);
  1781. bio = btrfs_io_bio_alloc(1);
  1782. bio->bi_iter.bi_size = 0;
  1783. map_length = length;
  1784. /*
  1785. * Avoid races with device replace and make sure our bbio has devices
  1786. * associated to its stripes that don't go away while we are doing the
  1787. * read repair operation.
  1788. */
  1789. btrfs_bio_counter_inc_blocked(fs_info);
  1790. if (btrfs_is_parity_mirror(fs_info, logical, length)) {
  1791. /*
  1792. * Note that we don't use BTRFS_MAP_WRITE because it's supposed
  1793. * to update all raid stripes, but here we just want to correct
  1794. * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
  1795. * stripe's dev and sector.
  1796. */
  1797. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
  1798. &map_length, &bbio, 0);
  1799. if (ret) {
  1800. btrfs_bio_counter_dec(fs_info);
  1801. bio_put(bio);
  1802. return -EIO;
  1803. }
  1804. ASSERT(bbio->mirror_num == 1);
  1805. } else {
  1806. ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
  1807. &map_length, &bbio, mirror_num);
  1808. if (ret) {
  1809. btrfs_bio_counter_dec(fs_info);
  1810. bio_put(bio);
  1811. return -EIO;
  1812. }
  1813. BUG_ON(mirror_num != bbio->mirror_num);
  1814. }
  1815. sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
  1816. bio->bi_iter.bi_sector = sector;
  1817. dev = bbio->stripes[bbio->mirror_num - 1].dev;
  1818. btrfs_put_bbio(bbio);
  1819. if (!dev || !dev->bdev || !dev->writeable) {
  1820. btrfs_bio_counter_dec(fs_info);
  1821. bio_put(bio);
  1822. return -EIO;
  1823. }
  1824. bio_set_dev(bio, dev->bdev);
  1825. bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
  1826. bio_add_page(bio, page, length, pg_offset);
  1827. if (btrfsic_submit_bio_wait(bio)) {
  1828. /* try to remap that extent elsewhere? */
  1829. btrfs_bio_counter_dec(fs_info);
  1830. bio_put(bio);
  1831. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  1832. return -EIO;
  1833. }
  1834. btrfs_info_rl_in_rcu(fs_info,
  1835. "read error corrected: ino %llu off %llu (dev %s sector %llu)",
  1836. ino, start,
  1837. rcu_str_deref(dev->name), sector);
  1838. btrfs_bio_counter_dec(fs_info);
  1839. bio_put(bio);
  1840. return 0;
  1841. }
  1842. int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
  1843. struct extent_buffer *eb, int mirror_num)
  1844. {
  1845. u64 start = eb->start;
  1846. unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
  1847. int ret = 0;
  1848. if (sb_rdonly(fs_info->sb))
  1849. return -EROFS;
  1850. for (i = 0; i < num_pages; i++) {
  1851. struct page *p = eb->pages[i];
  1852. ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
  1853. start - page_offset(p), mirror_num);
  1854. if (ret)
  1855. break;
  1856. start += PAGE_SIZE;
  1857. }
  1858. return ret;
  1859. }
  1860. /*
  1861. * each time an IO finishes, we do a fast check in the IO failure tree
  1862. * to see if we need to process or clean up an io_failure_record
  1863. */
  1864. int clean_io_failure(struct btrfs_fs_info *fs_info,
  1865. struct extent_io_tree *failure_tree,
  1866. struct extent_io_tree *io_tree, u64 start,
  1867. struct page *page, u64 ino, unsigned int pg_offset)
  1868. {
  1869. u64 private;
  1870. struct io_failure_record *failrec;
  1871. struct extent_state *state;
  1872. int num_copies;
  1873. int ret;
  1874. private = 0;
  1875. ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
  1876. EXTENT_DIRTY, 0);
  1877. if (!ret)
  1878. return 0;
  1879. ret = get_state_failrec(failure_tree, start, &failrec);
  1880. if (ret)
  1881. return 0;
  1882. BUG_ON(!failrec->this_mirror);
  1883. if (failrec->in_validation) {
  1884. /* there was no real error, just free the record */
  1885. btrfs_debug(fs_info,
  1886. "clean_io_failure: freeing dummy error at %llu",
  1887. failrec->start);
  1888. goto out;
  1889. }
  1890. if (sb_rdonly(fs_info->sb))
  1891. goto out;
  1892. spin_lock(&io_tree->lock);
  1893. state = find_first_extent_bit_state(io_tree,
  1894. failrec->start,
  1895. EXTENT_LOCKED);
  1896. spin_unlock(&io_tree->lock);
  1897. if (state && state->start <= failrec->start &&
  1898. state->end >= failrec->start + failrec->len - 1) {
  1899. num_copies = btrfs_num_copies(fs_info, failrec->logical,
  1900. failrec->len);
  1901. if (num_copies > 1) {
  1902. repair_io_failure(fs_info, ino, start, failrec->len,
  1903. failrec->logical, page, pg_offset,
  1904. failrec->failed_mirror);
  1905. }
  1906. }
  1907. out:
  1908. free_io_failure(failure_tree, io_tree, failrec);
  1909. return 0;
  1910. }
  1911. /*
  1912. * Can be called when
  1913. * - hold extent lock
  1914. * - under ordered extent
  1915. * - the inode is freeing
  1916. */
  1917. void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
  1918. {
  1919. struct extent_io_tree *failure_tree = &inode->io_failure_tree;
  1920. struct io_failure_record *failrec;
  1921. struct extent_state *state, *next;
  1922. if (RB_EMPTY_ROOT(&failure_tree->state))
  1923. return;
  1924. spin_lock(&failure_tree->lock);
  1925. state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
  1926. while (state) {
  1927. if (state->start > end)
  1928. break;
  1929. ASSERT(state->end <= end);
  1930. next = next_state(state);
  1931. failrec = state->failrec;
  1932. free_extent_state(state);
  1933. kfree(failrec);
  1934. state = next;
  1935. }
  1936. spin_unlock(&failure_tree->lock);
  1937. }
  1938. int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
  1939. struct io_failure_record **failrec_ret)
  1940. {
  1941. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1942. struct io_failure_record *failrec;
  1943. struct extent_map *em;
  1944. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1945. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1946. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1947. int ret;
  1948. u64 logical;
  1949. ret = get_state_failrec(failure_tree, start, &failrec);
  1950. if (ret) {
  1951. failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
  1952. if (!failrec)
  1953. return -ENOMEM;
  1954. failrec->start = start;
  1955. failrec->len = end - start + 1;
  1956. failrec->this_mirror = 0;
  1957. failrec->bio_flags = 0;
  1958. failrec->in_validation = 0;
  1959. read_lock(&em_tree->lock);
  1960. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1961. if (!em) {
  1962. read_unlock(&em_tree->lock);
  1963. kfree(failrec);
  1964. return -EIO;
  1965. }
  1966. if (em->start > start || em->start + em->len <= start) {
  1967. free_extent_map(em);
  1968. em = NULL;
  1969. }
  1970. read_unlock(&em_tree->lock);
  1971. if (!em) {
  1972. kfree(failrec);
  1973. return -EIO;
  1974. }
  1975. logical = start - em->start;
  1976. logical = em->block_start + logical;
  1977. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1978. logical = em->block_start;
  1979. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1980. extent_set_compress_type(&failrec->bio_flags,
  1981. em->compress_type);
  1982. }
  1983. btrfs_debug(fs_info,
  1984. "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
  1985. logical, start, failrec->len);
  1986. failrec->logical = logical;
  1987. free_extent_map(em);
  1988. /* set the bits in the private failure tree */
  1989. ret = set_extent_bits(failure_tree, start, end,
  1990. EXTENT_LOCKED | EXTENT_DIRTY);
  1991. if (ret >= 0)
  1992. ret = set_state_failrec(failure_tree, start, failrec);
  1993. /* set the bits in the inode's tree */
  1994. if (ret >= 0)
  1995. ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
  1996. if (ret < 0) {
  1997. kfree(failrec);
  1998. return ret;
  1999. }
  2000. } else {
  2001. btrfs_debug(fs_info,
  2002. "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
  2003. failrec->logical, failrec->start, failrec->len,
  2004. failrec->in_validation);
  2005. /*
  2006. * when data can be on disk more than twice, add to failrec here
  2007. * (e.g. with a list for failed_mirror) to make
  2008. * clean_io_failure() clean all those errors at once.
  2009. */
  2010. }
  2011. *failrec_ret = failrec;
  2012. return 0;
  2013. }
  2014. bool btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
  2015. struct io_failure_record *failrec, int failed_mirror)
  2016. {
  2017. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2018. int num_copies;
  2019. num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
  2020. if (num_copies == 1) {
  2021. /*
  2022. * we only have a single copy of the data, so don't bother with
  2023. * all the retry and error correction code that follows. no
  2024. * matter what the error is, it is very likely to persist.
  2025. */
  2026. btrfs_debug(fs_info,
  2027. "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
  2028. num_copies, failrec->this_mirror, failed_mirror);
  2029. return false;
  2030. }
  2031. /*
  2032. * there are two premises:
  2033. * a) deliver good data to the caller
  2034. * b) correct the bad sectors on disk
  2035. */
  2036. if (failed_bio->bi_vcnt > 1) {
  2037. /*
  2038. * to fulfill b), we need to know the exact failing sectors, as
  2039. * we don't want to rewrite any more than the failed ones. thus,
  2040. * we need separate read requests for the failed bio
  2041. *
  2042. * if the following BUG_ON triggers, our validation request got
  2043. * merged. we need separate requests for our algorithm to work.
  2044. */
  2045. BUG_ON(failrec->in_validation);
  2046. failrec->in_validation = 1;
  2047. failrec->this_mirror = failed_mirror;
  2048. } else {
  2049. /*
  2050. * we're ready to fulfill a) and b) alongside. get a good copy
  2051. * of the failed sector and if we succeed, we have setup
  2052. * everything for repair_io_failure to do the rest for us.
  2053. */
  2054. if (failrec->in_validation) {
  2055. BUG_ON(failrec->this_mirror != failed_mirror);
  2056. failrec->in_validation = 0;
  2057. failrec->this_mirror = 0;
  2058. }
  2059. failrec->failed_mirror = failed_mirror;
  2060. failrec->this_mirror++;
  2061. if (failrec->this_mirror == failed_mirror)
  2062. failrec->this_mirror++;
  2063. }
  2064. if (failrec->this_mirror > num_copies) {
  2065. btrfs_debug(fs_info,
  2066. "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
  2067. num_copies, failrec->this_mirror, failed_mirror);
  2068. return false;
  2069. }
  2070. return true;
  2071. }
  2072. struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
  2073. struct io_failure_record *failrec,
  2074. struct page *page, int pg_offset, int icsum,
  2075. bio_end_io_t *endio_func, void *data)
  2076. {
  2077. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2078. struct bio *bio;
  2079. struct btrfs_io_bio *btrfs_failed_bio;
  2080. struct btrfs_io_bio *btrfs_bio;
  2081. bio = btrfs_io_bio_alloc(1);
  2082. bio->bi_end_io = endio_func;
  2083. bio->bi_iter.bi_sector = failrec->logical >> 9;
  2084. bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
  2085. bio->bi_iter.bi_size = 0;
  2086. bio->bi_private = data;
  2087. btrfs_failed_bio = btrfs_io_bio(failed_bio);
  2088. if (btrfs_failed_bio->csum) {
  2089. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  2090. btrfs_bio = btrfs_io_bio(bio);
  2091. btrfs_bio->csum = btrfs_bio->csum_inline;
  2092. icsum *= csum_size;
  2093. memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
  2094. csum_size);
  2095. }
  2096. bio_add_page(bio, page, failrec->len, pg_offset);
  2097. return bio;
  2098. }
  2099. /*
  2100. * this is a generic handler for readpage errors (default
  2101. * readpage_io_failed_hook). if other copies exist, read those and write back
  2102. * good data to the failed position. does not investigate in remapping the
  2103. * failed extent elsewhere, hoping the device will be smart enough to do this as
  2104. * needed
  2105. */
  2106. static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
  2107. struct page *page, u64 start, u64 end,
  2108. int failed_mirror)
  2109. {
  2110. struct io_failure_record *failrec;
  2111. struct inode *inode = page->mapping->host;
  2112. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  2113. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  2114. struct bio *bio;
  2115. int read_mode = 0;
  2116. blk_status_t status;
  2117. int ret;
  2118. BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
  2119. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  2120. if (ret)
  2121. return ret;
  2122. if (!btrfs_check_repairable(inode, failed_bio, failrec,
  2123. failed_mirror)) {
  2124. free_io_failure(failure_tree, tree, failrec);
  2125. return -EIO;
  2126. }
  2127. if (failed_bio->bi_vcnt > 1)
  2128. read_mode |= REQ_FAILFAST_DEV;
  2129. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2130. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  2131. start - page_offset(page),
  2132. (int)phy_offset, failed_bio->bi_end_io,
  2133. NULL);
  2134. bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
  2135. btrfs_debug(btrfs_sb(inode->i_sb),
  2136. "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
  2137. read_mode, failrec->this_mirror, failrec->in_validation);
  2138. status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
  2139. failrec->bio_flags, 0);
  2140. if (status) {
  2141. free_io_failure(failure_tree, tree, failrec);
  2142. bio_put(bio);
  2143. ret = blk_status_to_errno(status);
  2144. }
  2145. return ret;
  2146. }
  2147. /* lots and lots of room for performance fixes in the end_bio funcs */
  2148. void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
  2149. {
  2150. int uptodate = (err == 0);
  2151. struct extent_io_tree *tree;
  2152. int ret = 0;
  2153. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2154. if (tree->ops && tree->ops->writepage_end_io_hook)
  2155. tree->ops->writepage_end_io_hook(page, start, end, NULL,
  2156. uptodate);
  2157. if (!uptodate) {
  2158. ClearPageUptodate(page);
  2159. SetPageError(page);
  2160. ret = err < 0 ? err : -EIO;
  2161. mapping_set_error(page->mapping, ret);
  2162. }
  2163. }
  2164. /*
  2165. * after a writepage IO is done, we need to:
  2166. * clear the uptodate bits on error
  2167. * clear the writeback bits in the extent tree for this IO
  2168. * end_page_writeback if the page has no more pending IO
  2169. *
  2170. * Scheduling is not allowed, so the extent state tree is expected
  2171. * to have one and only one object corresponding to this IO.
  2172. */
  2173. static void end_bio_extent_writepage(struct bio *bio)
  2174. {
  2175. int error = blk_status_to_errno(bio->bi_status);
  2176. struct bio_vec *bvec;
  2177. u64 start;
  2178. u64 end;
  2179. int i;
  2180. ASSERT(!bio_flagged(bio, BIO_CLONED));
  2181. bio_for_each_segment_all(bvec, bio, i) {
  2182. struct page *page = bvec->bv_page;
  2183. struct inode *inode = page->mapping->host;
  2184. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2185. /* We always issue full-page reads, but if some block
  2186. * in a page fails to read, blk_update_request() will
  2187. * advance bv_offset and adjust bv_len to compensate.
  2188. * Print a warning for nonzero offsets, and an error
  2189. * if they don't add up to a full page. */
  2190. if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
  2191. if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
  2192. btrfs_err(fs_info,
  2193. "partial page write in btrfs with offset %u and length %u",
  2194. bvec->bv_offset, bvec->bv_len);
  2195. else
  2196. btrfs_info(fs_info,
  2197. "incomplete page write in btrfs with offset %u and length %u",
  2198. bvec->bv_offset, bvec->bv_len);
  2199. }
  2200. start = page_offset(page);
  2201. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2202. end_extent_writepage(page, error, start, end);
  2203. end_page_writeback(page);
  2204. }
  2205. bio_put(bio);
  2206. }
  2207. static void
  2208. endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
  2209. int uptodate)
  2210. {
  2211. struct extent_state *cached = NULL;
  2212. u64 end = start + len - 1;
  2213. if (uptodate && tree->track_uptodate)
  2214. set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
  2215. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2216. }
  2217. /*
  2218. * after a readpage IO is done, we need to:
  2219. * clear the uptodate bits on error
  2220. * set the uptodate bits if things worked
  2221. * set the page up to date if all extents in the tree are uptodate
  2222. * clear the lock bit in the extent tree
  2223. * unlock the page if there are no other extents locked for it
  2224. *
  2225. * Scheduling is not allowed, so the extent state tree is expected
  2226. * to have one and only one object corresponding to this IO.
  2227. */
  2228. static void end_bio_extent_readpage(struct bio *bio)
  2229. {
  2230. struct bio_vec *bvec;
  2231. int uptodate = !bio->bi_status;
  2232. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  2233. struct extent_io_tree *tree, *failure_tree;
  2234. u64 offset = 0;
  2235. u64 start;
  2236. u64 end;
  2237. u64 len;
  2238. u64 extent_start = 0;
  2239. u64 extent_len = 0;
  2240. int mirror;
  2241. int ret;
  2242. int i;
  2243. ASSERT(!bio_flagged(bio, BIO_CLONED));
  2244. bio_for_each_segment_all(bvec, bio, i) {
  2245. struct page *page = bvec->bv_page;
  2246. struct inode *inode = page->mapping->host;
  2247. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2248. btrfs_debug(fs_info,
  2249. "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
  2250. (u64)bio->bi_iter.bi_sector, bio->bi_status,
  2251. io_bio->mirror_num);
  2252. tree = &BTRFS_I(inode)->io_tree;
  2253. failure_tree = &BTRFS_I(inode)->io_failure_tree;
  2254. /* We always issue full-page reads, but if some block
  2255. * in a page fails to read, blk_update_request() will
  2256. * advance bv_offset and adjust bv_len to compensate.
  2257. * Print a warning for nonzero offsets, and an error
  2258. * if they don't add up to a full page. */
  2259. if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
  2260. if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
  2261. btrfs_err(fs_info,
  2262. "partial page read in btrfs with offset %u and length %u",
  2263. bvec->bv_offset, bvec->bv_len);
  2264. else
  2265. btrfs_info(fs_info,
  2266. "incomplete page read in btrfs with offset %u and length %u",
  2267. bvec->bv_offset, bvec->bv_len);
  2268. }
  2269. start = page_offset(page);
  2270. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2271. len = bvec->bv_len;
  2272. mirror = io_bio->mirror_num;
  2273. if (likely(uptodate && tree->ops)) {
  2274. ret = tree->ops->readpage_end_io_hook(io_bio, offset,
  2275. page, start, end,
  2276. mirror);
  2277. if (ret)
  2278. uptodate = 0;
  2279. else
  2280. clean_io_failure(BTRFS_I(inode)->root->fs_info,
  2281. failure_tree, tree, start,
  2282. page,
  2283. btrfs_ino(BTRFS_I(inode)), 0);
  2284. }
  2285. if (likely(uptodate))
  2286. goto readpage_ok;
  2287. if (tree->ops) {
  2288. ret = tree->ops->readpage_io_failed_hook(page, mirror);
  2289. if (ret == -EAGAIN) {
  2290. /*
  2291. * Data inode's readpage_io_failed_hook() always
  2292. * returns -EAGAIN.
  2293. *
  2294. * The generic bio_readpage_error handles errors
  2295. * the following way: If possible, new read
  2296. * requests are created and submitted and will
  2297. * end up in end_bio_extent_readpage as well (if
  2298. * we're lucky, not in the !uptodate case). In
  2299. * that case it returns 0 and we just go on with
  2300. * the next page in our bio. If it can't handle
  2301. * the error it will return -EIO and we remain
  2302. * responsible for that page.
  2303. */
  2304. ret = bio_readpage_error(bio, offset, page,
  2305. start, end, mirror);
  2306. if (ret == 0) {
  2307. uptodate = !bio->bi_status;
  2308. offset += len;
  2309. continue;
  2310. }
  2311. }
  2312. /*
  2313. * metadata's readpage_io_failed_hook() always returns
  2314. * -EIO and fixes nothing. -EIO is also returned if
  2315. * data inode error could not be fixed.
  2316. */
  2317. ASSERT(ret == -EIO);
  2318. }
  2319. readpage_ok:
  2320. if (likely(uptodate)) {
  2321. loff_t i_size = i_size_read(inode);
  2322. pgoff_t end_index = i_size >> PAGE_SHIFT;
  2323. unsigned off;
  2324. /* Zero out the end if this page straddles i_size */
  2325. off = i_size & (PAGE_SIZE-1);
  2326. if (page->index == end_index && off)
  2327. zero_user_segment(page, off, PAGE_SIZE);
  2328. SetPageUptodate(page);
  2329. } else {
  2330. ClearPageUptodate(page);
  2331. SetPageError(page);
  2332. }
  2333. unlock_page(page);
  2334. offset += len;
  2335. if (unlikely(!uptodate)) {
  2336. if (extent_len) {
  2337. endio_readpage_release_extent(tree,
  2338. extent_start,
  2339. extent_len, 1);
  2340. extent_start = 0;
  2341. extent_len = 0;
  2342. }
  2343. endio_readpage_release_extent(tree, start,
  2344. end - start + 1, 0);
  2345. } else if (!extent_len) {
  2346. extent_start = start;
  2347. extent_len = end + 1 - start;
  2348. } else if (extent_start + extent_len == start) {
  2349. extent_len += end + 1 - start;
  2350. } else {
  2351. endio_readpage_release_extent(tree, extent_start,
  2352. extent_len, uptodate);
  2353. extent_start = start;
  2354. extent_len = end + 1 - start;
  2355. }
  2356. }
  2357. if (extent_len)
  2358. endio_readpage_release_extent(tree, extent_start, extent_len,
  2359. uptodate);
  2360. if (io_bio->end_io)
  2361. io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status));
  2362. bio_put(bio);
  2363. }
  2364. /*
  2365. * Initialize the members up to but not including 'bio'. Use after allocating a
  2366. * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
  2367. * 'bio' because use of __GFP_ZERO is not supported.
  2368. */
  2369. static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
  2370. {
  2371. memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
  2372. }
  2373. /*
  2374. * The following helpers allocate a bio. As it's backed by a bioset, it'll
  2375. * never fail. We're returning a bio right now but you can call btrfs_io_bio
  2376. * for the appropriate container_of magic
  2377. */
  2378. struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
  2379. {
  2380. struct bio *bio;
  2381. bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, btrfs_bioset);
  2382. bio_set_dev(bio, bdev);
  2383. bio->bi_iter.bi_sector = first_byte >> 9;
  2384. btrfs_io_bio_init(btrfs_io_bio(bio));
  2385. return bio;
  2386. }
  2387. struct bio *btrfs_bio_clone(struct bio *bio)
  2388. {
  2389. struct btrfs_io_bio *btrfs_bio;
  2390. struct bio *new;
  2391. /* Bio allocation backed by a bioset does not fail */
  2392. new = bio_clone_fast(bio, GFP_NOFS, btrfs_bioset);
  2393. btrfs_bio = btrfs_io_bio(new);
  2394. btrfs_io_bio_init(btrfs_bio);
  2395. btrfs_bio->iter = bio->bi_iter;
  2396. return new;
  2397. }
  2398. struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
  2399. {
  2400. struct bio *bio;
  2401. /* Bio allocation backed by a bioset does not fail */
  2402. bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, btrfs_bioset);
  2403. btrfs_io_bio_init(btrfs_io_bio(bio));
  2404. return bio;
  2405. }
  2406. struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
  2407. {
  2408. struct bio *bio;
  2409. struct btrfs_io_bio *btrfs_bio;
  2410. /* this will never fail when it's backed by a bioset */
  2411. bio = bio_clone_fast(orig, GFP_NOFS, btrfs_bioset);
  2412. ASSERT(bio);
  2413. btrfs_bio = btrfs_io_bio(bio);
  2414. btrfs_io_bio_init(btrfs_bio);
  2415. bio_trim(bio, offset >> 9, size >> 9);
  2416. btrfs_bio->iter = bio->bi_iter;
  2417. return bio;
  2418. }
  2419. static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
  2420. unsigned long bio_flags)
  2421. {
  2422. blk_status_t ret = 0;
  2423. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2424. struct page *page = bvec->bv_page;
  2425. struct extent_io_tree *tree = bio->bi_private;
  2426. u64 start;
  2427. start = page_offset(page) + bvec->bv_offset;
  2428. bio->bi_private = NULL;
  2429. bio_get(bio);
  2430. if (tree->ops)
  2431. ret = tree->ops->submit_bio_hook(tree->private_data, bio,
  2432. mirror_num, bio_flags, start);
  2433. else
  2434. btrfsic_submit_bio(bio);
  2435. bio_put(bio);
  2436. return blk_status_to_errno(ret);
  2437. }
  2438. static int merge_bio(struct extent_io_tree *tree, struct page *page,
  2439. unsigned long offset, size_t size, struct bio *bio,
  2440. unsigned long bio_flags)
  2441. {
  2442. int ret = 0;
  2443. if (tree->ops)
  2444. ret = tree->ops->merge_bio_hook(page, offset, size, bio,
  2445. bio_flags);
  2446. return ret;
  2447. }
  2448. /*
  2449. * @opf: bio REQ_OP_* and REQ_* flags as one value
  2450. */
  2451. static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
  2452. struct writeback_control *wbc,
  2453. struct page *page, u64 offset,
  2454. size_t size, unsigned long pg_offset,
  2455. struct block_device *bdev,
  2456. struct bio **bio_ret,
  2457. bio_end_io_t end_io_func,
  2458. int mirror_num,
  2459. unsigned long prev_bio_flags,
  2460. unsigned long bio_flags,
  2461. bool force_bio_submit)
  2462. {
  2463. int ret = 0;
  2464. struct bio *bio;
  2465. int contig = 0;
  2466. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2467. size_t page_size = min_t(size_t, size, PAGE_SIZE);
  2468. sector_t sector = offset >> 9;
  2469. if (bio_ret && *bio_ret) {
  2470. bio = *bio_ret;
  2471. if (old_compressed)
  2472. contig = bio->bi_iter.bi_sector == sector;
  2473. else
  2474. contig = bio_end_sector(bio) == sector;
  2475. if (prev_bio_flags != bio_flags || !contig ||
  2476. force_bio_submit ||
  2477. merge_bio(tree, page, pg_offset, page_size, bio, bio_flags) ||
  2478. bio_add_page(bio, page, page_size, pg_offset) < page_size) {
  2479. ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
  2480. if (ret < 0) {
  2481. *bio_ret = NULL;
  2482. return ret;
  2483. }
  2484. bio = NULL;
  2485. } else {
  2486. if (wbc)
  2487. wbc_account_io(wbc, page, page_size);
  2488. return 0;
  2489. }
  2490. }
  2491. bio = btrfs_bio_alloc(bdev, offset);
  2492. bio_add_page(bio, page, page_size, pg_offset);
  2493. bio->bi_end_io = end_io_func;
  2494. bio->bi_private = tree;
  2495. bio->bi_write_hint = page->mapping->host->i_write_hint;
  2496. bio->bi_opf = opf;
  2497. if (wbc) {
  2498. wbc_init_bio(wbc, bio);
  2499. wbc_account_io(wbc, page, page_size);
  2500. }
  2501. if (bio_ret)
  2502. *bio_ret = bio;
  2503. else
  2504. ret = submit_one_bio(bio, mirror_num, bio_flags);
  2505. return ret;
  2506. }
  2507. static void attach_extent_buffer_page(struct extent_buffer *eb,
  2508. struct page *page)
  2509. {
  2510. if (!PagePrivate(page)) {
  2511. SetPagePrivate(page);
  2512. get_page(page);
  2513. set_page_private(page, (unsigned long)eb);
  2514. } else {
  2515. WARN_ON(page->private != (unsigned long)eb);
  2516. }
  2517. }
  2518. void set_page_extent_mapped(struct page *page)
  2519. {
  2520. if (!PagePrivate(page)) {
  2521. SetPagePrivate(page);
  2522. get_page(page);
  2523. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2524. }
  2525. }
  2526. static struct extent_map *
  2527. __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
  2528. u64 start, u64 len, get_extent_t *get_extent,
  2529. struct extent_map **em_cached)
  2530. {
  2531. struct extent_map *em;
  2532. if (em_cached && *em_cached) {
  2533. em = *em_cached;
  2534. if (extent_map_in_tree(em) && start >= em->start &&
  2535. start < extent_map_end(em)) {
  2536. refcount_inc(&em->refs);
  2537. return em;
  2538. }
  2539. free_extent_map(em);
  2540. *em_cached = NULL;
  2541. }
  2542. em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
  2543. if (em_cached && !IS_ERR_OR_NULL(em)) {
  2544. BUG_ON(*em_cached);
  2545. refcount_inc(&em->refs);
  2546. *em_cached = em;
  2547. }
  2548. return em;
  2549. }
  2550. /*
  2551. * basic readpage implementation. Locked extent state structs are inserted
  2552. * into the tree that are removed when the IO is done (by the end_io
  2553. * handlers)
  2554. * XXX JDM: This needs looking at to ensure proper page locking
  2555. * return 0 on success, otherwise return error
  2556. */
  2557. static int __do_readpage(struct extent_io_tree *tree,
  2558. struct page *page,
  2559. get_extent_t *get_extent,
  2560. struct extent_map **em_cached,
  2561. struct bio **bio, int mirror_num,
  2562. unsigned long *bio_flags, unsigned int read_flags,
  2563. u64 *prev_em_start)
  2564. {
  2565. struct inode *inode = page->mapping->host;
  2566. u64 start = page_offset(page);
  2567. u64 page_end = start + PAGE_SIZE - 1;
  2568. u64 end;
  2569. u64 cur = start;
  2570. u64 extent_offset;
  2571. u64 last_byte = i_size_read(inode);
  2572. u64 block_start;
  2573. u64 cur_end;
  2574. struct extent_map *em;
  2575. struct block_device *bdev;
  2576. int ret = 0;
  2577. int nr = 0;
  2578. size_t pg_offset = 0;
  2579. size_t iosize;
  2580. size_t disk_io_size;
  2581. size_t blocksize = inode->i_sb->s_blocksize;
  2582. unsigned long this_bio_flag = 0;
  2583. set_page_extent_mapped(page);
  2584. end = page_end;
  2585. if (!PageUptodate(page)) {
  2586. if (cleancache_get_page(page) == 0) {
  2587. BUG_ON(blocksize != PAGE_SIZE);
  2588. unlock_extent(tree, start, end);
  2589. goto out;
  2590. }
  2591. }
  2592. if (page->index == last_byte >> PAGE_SHIFT) {
  2593. char *userpage;
  2594. size_t zero_offset = last_byte & (PAGE_SIZE - 1);
  2595. if (zero_offset) {
  2596. iosize = PAGE_SIZE - zero_offset;
  2597. userpage = kmap_atomic(page);
  2598. memset(userpage + zero_offset, 0, iosize);
  2599. flush_dcache_page(page);
  2600. kunmap_atomic(userpage);
  2601. }
  2602. }
  2603. while (cur <= end) {
  2604. bool force_bio_submit = false;
  2605. u64 offset;
  2606. if (cur >= last_byte) {
  2607. char *userpage;
  2608. struct extent_state *cached = NULL;
  2609. iosize = PAGE_SIZE - pg_offset;
  2610. userpage = kmap_atomic(page);
  2611. memset(userpage + pg_offset, 0, iosize);
  2612. flush_dcache_page(page);
  2613. kunmap_atomic(userpage);
  2614. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2615. &cached, GFP_NOFS);
  2616. unlock_extent_cached(tree, cur,
  2617. cur + iosize - 1,
  2618. &cached, GFP_NOFS);
  2619. break;
  2620. }
  2621. em = __get_extent_map(inode, page, pg_offset, cur,
  2622. end - cur + 1, get_extent, em_cached);
  2623. if (IS_ERR_OR_NULL(em)) {
  2624. SetPageError(page);
  2625. unlock_extent(tree, cur, end);
  2626. break;
  2627. }
  2628. extent_offset = cur - em->start;
  2629. BUG_ON(extent_map_end(em) <= cur);
  2630. BUG_ON(end < cur);
  2631. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2632. this_bio_flag |= EXTENT_BIO_COMPRESSED;
  2633. extent_set_compress_type(&this_bio_flag,
  2634. em->compress_type);
  2635. }
  2636. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2637. cur_end = min(extent_map_end(em) - 1, end);
  2638. iosize = ALIGN(iosize, blocksize);
  2639. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2640. disk_io_size = em->block_len;
  2641. offset = em->block_start;
  2642. } else {
  2643. offset = em->block_start + extent_offset;
  2644. disk_io_size = iosize;
  2645. }
  2646. bdev = em->bdev;
  2647. block_start = em->block_start;
  2648. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2649. block_start = EXTENT_MAP_HOLE;
  2650. /*
  2651. * If we have a file range that points to a compressed extent
  2652. * and it's followed by a consecutive file range that points to
  2653. * to the same compressed extent (possibly with a different
  2654. * offset and/or length, so it either points to the whole extent
  2655. * or only part of it), we must make sure we do not submit a
  2656. * single bio to populate the pages for the 2 ranges because
  2657. * this makes the compressed extent read zero out the pages
  2658. * belonging to the 2nd range. Imagine the following scenario:
  2659. *
  2660. * File layout
  2661. * [0 - 8K] [8K - 24K]
  2662. * | |
  2663. * | |
  2664. * points to extent X, points to extent X,
  2665. * offset 4K, length of 8K offset 0, length 16K
  2666. *
  2667. * [extent X, compressed length = 4K uncompressed length = 16K]
  2668. *
  2669. * If the bio to read the compressed extent covers both ranges,
  2670. * it will decompress extent X into the pages belonging to the
  2671. * first range and then it will stop, zeroing out the remaining
  2672. * pages that belong to the other range that points to extent X.
  2673. * So here we make sure we submit 2 bios, one for the first
  2674. * range and another one for the third range. Both will target
  2675. * the same physical extent from disk, but we can't currently
  2676. * make the compressed bio endio callback populate the pages
  2677. * for both ranges because each compressed bio is tightly
  2678. * coupled with a single extent map, and each range can have
  2679. * an extent map with a different offset value relative to the
  2680. * uncompressed data of our extent and different lengths. This
  2681. * is a corner case so we prioritize correctness over
  2682. * non-optimal behavior (submitting 2 bios for the same extent).
  2683. */
  2684. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
  2685. prev_em_start && *prev_em_start != (u64)-1 &&
  2686. *prev_em_start != em->orig_start)
  2687. force_bio_submit = true;
  2688. if (prev_em_start)
  2689. *prev_em_start = em->orig_start;
  2690. free_extent_map(em);
  2691. em = NULL;
  2692. /* we've found a hole, just zero and go on */
  2693. if (block_start == EXTENT_MAP_HOLE) {
  2694. char *userpage;
  2695. struct extent_state *cached = NULL;
  2696. userpage = kmap_atomic(page);
  2697. memset(userpage + pg_offset, 0, iosize);
  2698. flush_dcache_page(page);
  2699. kunmap_atomic(userpage);
  2700. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2701. &cached, GFP_NOFS);
  2702. unlock_extent_cached(tree, cur,
  2703. cur + iosize - 1,
  2704. &cached, GFP_NOFS);
  2705. cur = cur + iosize;
  2706. pg_offset += iosize;
  2707. continue;
  2708. }
  2709. /* the get_extent function already copied into the page */
  2710. if (test_range_bit(tree, cur, cur_end,
  2711. EXTENT_UPTODATE, 1, NULL)) {
  2712. check_page_uptodate(tree, page);
  2713. unlock_extent(tree, cur, cur + iosize - 1);
  2714. cur = cur + iosize;
  2715. pg_offset += iosize;
  2716. continue;
  2717. }
  2718. /* we have an inline extent but it didn't get marked up
  2719. * to date. Error out
  2720. */
  2721. if (block_start == EXTENT_MAP_INLINE) {
  2722. SetPageError(page);
  2723. unlock_extent(tree, cur, cur + iosize - 1);
  2724. cur = cur + iosize;
  2725. pg_offset += iosize;
  2726. continue;
  2727. }
  2728. ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
  2729. page, offset, disk_io_size,
  2730. pg_offset, bdev, bio,
  2731. end_bio_extent_readpage, mirror_num,
  2732. *bio_flags,
  2733. this_bio_flag,
  2734. force_bio_submit);
  2735. if (!ret) {
  2736. nr++;
  2737. *bio_flags = this_bio_flag;
  2738. } else {
  2739. SetPageError(page);
  2740. unlock_extent(tree, cur, cur + iosize - 1);
  2741. goto out;
  2742. }
  2743. cur = cur + iosize;
  2744. pg_offset += iosize;
  2745. }
  2746. out:
  2747. if (!nr) {
  2748. if (!PageError(page))
  2749. SetPageUptodate(page);
  2750. unlock_page(page);
  2751. }
  2752. return ret;
  2753. }
  2754. static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
  2755. struct page *pages[], int nr_pages,
  2756. u64 start, u64 end,
  2757. get_extent_t *get_extent,
  2758. struct extent_map **em_cached,
  2759. struct bio **bio, int mirror_num,
  2760. unsigned long *bio_flags,
  2761. u64 *prev_em_start)
  2762. {
  2763. struct inode *inode;
  2764. struct btrfs_ordered_extent *ordered;
  2765. int index;
  2766. inode = pages[0]->mapping->host;
  2767. while (1) {
  2768. lock_extent(tree, start, end);
  2769. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
  2770. end - start + 1);
  2771. if (!ordered)
  2772. break;
  2773. unlock_extent(tree, start, end);
  2774. btrfs_start_ordered_extent(inode, ordered, 1);
  2775. btrfs_put_ordered_extent(ordered);
  2776. }
  2777. for (index = 0; index < nr_pages; index++) {
  2778. __do_readpage(tree, pages[index], get_extent, em_cached, bio,
  2779. mirror_num, bio_flags, 0, prev_em_start);
  2780. put_page(pages[index]);
  2781. }
  2782. }
  2783. static void __extent_readpages(struct extent_io_tree *tree,
  2784. struct page *pages[],
  2785. int nr_pages, get_extent_t *get_extent,
  2786. struct extent_map **em_cached,
  2787. struct bio **bio, int mirror_num,
  2788. unsigned long *bio_flags,
  2789. u64 *prev_em_start)
  2790. {
  2791. u64 start = 0;
  2792. u64 end = 0;
  2793. u64 page_start;
  2794. int index;
  2795. int first_index = 0;
  2796. for (index = 0; index < nr_pages; index++) {
  2797. page_start = page_offset(pages[index]);
  2798. if (!end) {
  2799. start = page_start;
  2800. end = start + PAGE_SIZE - 1;
  2801. first_index = index;
  2802. } else if (end + 1 == page_start) {
  2803. end += PAGE_SIZE;
  2804. } else {
  2805. __do_contiguous_readpages(tree, &pages[first_index],
  2806. index - first_index, start,
  2807. end, get_extent, em_cached,
  2808. bio, mirror_num, bio_flags,
  2809. prev_em_start);
  2810. start = page_start;
  2811. end = start + PAGE_SIZE - 1;
  2812. first_index = index;
  2813. }
  2814. }
  2815. if (end)
  2816. __do_contiguous_readpages(tree, &pages[first_index],
  2817. index - first_index, start,
  2818. end, get_extent, em_cached, bio,
  2819. mirror_num, bio_flags,
  2820. prev_em_start);
  2821. }
  2822. static int __extent_read_full_page(struct extent_io_tree *tree,
  2823. struct page *page,
  2824. get_extent_t *get_extent,
  2825. struct bio **bio, int mirror_num,
  2826. unsigned long *bio_flags,
  2827. unsigned int read_flags)
  2828. {
  2829. struct inode *inode = page->mapping->host;
  2830. struct btrfs_ordered_extent *ordered;
  2831. u64 start = page_offset(page);
  2832. u64 end = start + PAGE_SIZE - 1;
  2833. int ret;
  2834. while (1) {
  2835. lock_extent(tree, start, end);
  2836. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
  2837. PAGE_SIZE);
  2838. if (!ordered)
  2839. break;
  2840. unlock_extent(tree, start, end);
  2841. btrfs_start_ordered_extent(inode, ordered, 1);
  2842. btrfs_put_ordered_extent(ordered);
  2843. }
  2844. ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
  2845. bio_flags, read_flags, NULL);
  2846. return ret;
  2847. }
  2848. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2849. get_extent_t *get_extent, int mirror_num)
  2850. {
  2851. struct bio *bio = NULL;
  2852. unsigned long bio_flags = 0;
  2853. int ret;
  2854. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2855. &bio_flags, 0);
  2856. if (bio)
  2857. ret = submit_one_bio(bio, mirror_num, bio_flags);
  2858. return ret;
  2859. }
  2860. static void update_nr_written(struct writeback_control *wbc,
  2861. unsigned long nr_written)
  2862. {
  2863. wbc->nr_to_write -= nr_written;
  2864. }
  2865. /*
  2866. * helper for __extent_writepage, doing all of the delayed allocation setup.
  2867. *
  2868. * This returns 1 if our fill_delalloc function did all the work required
  2869. * to write the page (copy into inline extent). In this case the IO has
  2870. * been started and the page is already unlocked.
  2871. *
  2872. * This returns 0 if all went well (page still locked)
  2873. * This returns < 0 if there were errors (page still locked)
  2874. */
  2875. static noinline_for_stack int writepage_delalloc(struct inode *inode,
  2876. struct page *page, struct writeback_control *wbc,
  2877. struct extent_page_data *epd,
  2878. u64 delalloc_start,
  2879. unsigned long *nr_written)
  2880. {
  2881. struct extent_io_tree *tree = epd->tree;
  2882. u64 page_end = delalloc_start + PAGE_SIZE - 1;
  2883. u64 nr_delalloc;
  2884. u64 delalloc_to_write = 0;
  2885. u64 delalloc_end = 0;
  2886. int ret;
  2887. int page_started = 0;
  2888. if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
  2889. return 0;
  2890. while (delalloc_end < page_end) {
  2891. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2892. page,
  2893. &delalloc_start,
  2894. &delalloc_end,
  2895. BTRFS_MAX_EXTENT_SIZE);
  2896. if (nr_delalloc == 0) {
  2897. delalloc_start = delalloc_end + 1;
  2898. continue;
  2899. }
  2900. ret = tree->ops->fill_delalloc(inode, page,
  2901. delalloc_start,
  2902. delalloc_end,
  2903. &page_started,
  2904. nr_written);
  2905. /* File system has been set read-only */
  2906. if (ret) {
  2907. SetPageError(page);
  2908. /* fill_delalloc should be return < 0 for error
  2909. * but just in case, we use > 0 here meaning the
  2910. * IO is started, so we don't want to return > 0
  2911. * unless things are going well.
  2912. */
  2913. ret = ret < 0 ? ret : -EIO;
  2914. goto done;
  2915. }
  2916. /*
  2917. * delalloc_end is already one less than the total length, so
  2918. * we don't subtract one from PAGE_SIZE
  2919. */
  2920. delalloc_to_write += (delalloc_end - delalloc_start +
  2921. PAGE_SIZE) >> PAGE_SHIFT;
  2922. delalloc_start = delalloc_end + 1;
  2923. }
  2924. if (wbc->nr_to_write < delalloc_to_write) {
  2925. int thresh = 8192;
  2926. if (delalloc_to_write < thresh * 2)
  2927. thresh = delalloc_to_write;
  2928. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2929. thresh);
  2930. }
  2931. /* did the fill delalloc function already unlock and start
  2932. * the IO?
  2933. */
  2934. if (page_started) {
  2935. /*
  2936. * we've unlocked the page, so we can't update
  2937. * the mapping's writeback index, just update
  2938. * nr_to_write.
  2939. */
  2940. wbc->nr_to_write -= *nr_written;
  2941. return 1;
  2942. }
  2943. ret = 0;
  2944. done:
  2945. return ret;
  2946. }
  2947. /*
  2948. * helper for __extent_writepage. This calls the writepage start hooks,
  2949. * and does the loop to map the page into extents and bios.
  2950. *
  2951. * We return 1 if the IO is started and the page is unlocked,
  2952. * 0 if all went well (page still locked)
  2953. * < 0 if there were errors (page still locked)
  2954. */
  2955. static noinline_for_stack int __extent_writepage_io(struct inode *inode,
  2956. struct page *page,
  2957. struct writeback_control *wbc,
  2958. struct extent_page_data *epd,
  2959. loff_t i_size,
  2960. unsigned long nr_written,
  2961. unsigned int write_flags, int *nr_ret)
  2962. {
  2963. struct extent_io_tree *tree = epd->tree;
  2964. u64 start = page_offset(page);
  2965. u64 page_end = start + PAGE_SIZE - 1;
  2966. u64 end;
  2967. u64 cur = start;
  2968. u64 extent_offset;
  2969. u64 block_start;
  2970. u64 iosize;
  2971. struct extent_map *em;
  2972. struct block_device *bdev;
  2973. size_t pg_offset = 0;
  2974. size_t blocksize;
  2975. int ret = 0;
  2976. int nr = 0;
  2977. bool compressed;
  2978. if (tree->ops && tree->ops->writepage_start_hook) {
  2979. ret = tree->ops->writepage_start_hook(page, start,
  2980. page_end);
  2981. if (ret) {
  2982. /* Fixup worker will requeue */
  2983. if (ret == -EBUSY)
  2984. wbc->pages_skipped++;
  2985. else
  2986. redirty_page_for_writepage(wbc, page);
  2987. update_nr_written(wbc, nr_written);
  2988. unlock_page(page);
  2989. return 1;
  2990. }
  2991. }
  2992. /*
  2993. * we don't want to touch the inode after unlocking the page,
  2994. * so we update the mapping writeback index now
  2995. */
  2996. update_nr_written(wbc, nr_written + 1);
  2997. end = page_end;
  2998. if (i_size <= start) {
  2999. if (tree->ops && tree->ops->writepage_end_io_hook)
  3000. tree->ops->writepage_end_io_hook(page, start,
  3001. page_end, NULL, 1);
  3002. goto done;
  3003. }
  3004. blocksize = inode->i_sb->s_blocksize;
  3005. while (cur <= end) {
  3006. u64 em_end;
  3007. u64 offset;
  3008. if (cur >= i_size) {
  3009. if (tree->ops && tree->ops->writepage_end_io_hook)
  3010. tree->ops->writepage_end_io_hook(page, cur,
  3011. page_end, NULL, 1);
  3012. break;
  3013. }
  3014. em = epd->get_extent(BTRFS_I(inode), page, pg_offset, cur,
  3015. end - cur + 1, 1);
  3016. if (IS_ERR_OR_NULL(em)) {
  3017. SetPageError(page);
  3018. ret = PTR_ERR_OR_ZERO(em);
  3019. break;
  3020. }
  3021. extent_offset = cur - em->start;
  3022. em_end = extent_map_end(em);
  3023. BUG_ON(em_end <= cur);
  3024. BUG_ON(end < cur);
  3025. iosize = min(em_end - cur, end - cur + 1);
  3026. iosize = ALIGN(iosize, blocksize);
  3027. offset = em->block_start + extent_offset;
  3028. bdev = em->bdev;
  3029. block_start = em->block_start;
  3030. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3031. free_extent_map(em);
  3032. em = NULL;
  3033. /*
  3034. * compressed and inline extents are written through other
  3035. * paths in the FS
  3036. */
  3037. if (compressed || block_start == EXTENT_MAP_HOLE ||
  3038. block_start == EXTENT_MAP_INLINE) {
  3039. /*
  3040. * end_io notification does not happen here for
  3041. * compressed extents
  3042. */
  3043. if (!compressed && tree->ops &&
  3044. tree->ops->writepage_end_io_hook)
  3045. tree->ops->writepage_end_io_hook(page, cur,
  3046. cur + iosize - 1,
  3047. NULL, 1);
  3048. else if (compressed) {
  3049. /* we don't want to end_page_writeback on
  3050. * a compressed extent. this happens
  3051. * elsewhere
  3052. */
  3053. nr++;
  3054. }
  3055. cur += iosize;
  3056. pg_offset += iosize;
  3057. continue;
  3058. }
  3059. set_range_writeback(tree, cur, cur + iosize - 1);
  3060. if (!PageWriteback(page)) {
  3061. btrfs_err(BTRFS_I(inode)->root->fs_info,
  3062. "page %lu not writeback, cur %llu end %llu",
  3063. page->index, cur, end);
  3064. }
  3065. ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
  3066. page, offset, iosize, pg_offset,
  3067. bdev, &epd->bio,
  3068. end_bio_extent_writepage,
  3069. 0, 0, 0, false);
  3070. if (ret) {
  3071. SetPageError(page);
  3072. if (PageWriteback(page))
  3073. end_page_writeback(page);
  3074. }
  3075. cur = cur + iosize;
  3076. pg_offset += iosize;
  3077. nr++;
  3078. }
  3079. done:
  3080. *nr_ret = nr;
  3081. return ret;
  3082. }
  3083. /*
  3084. * the writepage semantics are similar to regular writepage. extent
  3085. * records are inserted to lock ranges in the tree, and as dirty areas
  3086. * are found, they are marked writeback. Then the lock bits are removed
  3087. * and the end_io handler clears the writeback ranges
  3088. */
  3089. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  3090. void *data)
  3091. {
  3092. struct inode *inode = page->mapping->host;
  3093. struct extent_page_data *epd = data;
  3094. u64 start = page_offset(page);
  3095. u64 page_end = start + PAGE_SIZE - 1;
  3096. int ret;
  3097. int nr = 0;
  3098. size_t pg_offset = 0;
  3099. loff_t i_size = i_size_read(inode);
  3100. unsigned long end_index = i_size >> PAGE_SHIFT;
  3101. unsigned int write_flags = 0;
  3102. unsigned long nr_written = 0;
  3103. write_flags = wbc_to_write_flags(wbc);
  3104. trace___extent_writepage(page, inode, wbc);
  3105. WARN_ON(!PageLocked(page));
  3106. ClearPageError(page);
  3107. pg_offset = i_size & (PAGE_SIZE - 1);
  3108. if (page->index > end_index ||
  3109. (page->index == end_index && !pg_offset)) {
  3110. page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
  3111. unlock_page(page);
  3112. return 0;
  3113. }
  3114. if (page->index == end_index) {
  3115. char *userpage;
  3116. userpage = kmap_atomic(page);
  3117. memset(userpage + pg_offset, 0,
  3118. PAGE_SIZE - pg_offset);
  3119. kunmap_atomic(userpage);
  3120. flush_dcache_page(page);
  3121. }
  3122. pg_offset = 0;
  3123. set_page_extent_mapped(page);
  3124. ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
  3125. if (ret == 1)
  3126. goto done_unlocked;
  3127. if (ret)
  3128. goto done;
  3129. ret = __extent_writepage_io(inode, page, wbc, epd,
  3130. i_size, nr_written, write_flags, &nr);
  3131. if (ret == 1)
  3132. goto done_unlocked;
  3133. done:
  3134. if (nr == 0) {
  3135. /* make sure the mapping tag for page dirty gets cleared */
  3136. set_page_writeback(page);
  3137. end_page_writeback(page);
  3138. }
  3139. if (PageError(page)) {
  3140. ret = ret < 0 ? ret : -EIO;
  3141. end_extent_writepage(page, ret, start, page_end);
  3142. }
  3143. unlock_page(page);
  3144. return ret;
  3145. done_unlocked:
  3146. return 0;
  3147. }
  3148. void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
  3149. {
  3150. wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
  3151. TASK_UNINTERRUPTIBLE);
  3152. }
  3153. static noinline_for_stack int
  3154. lock_extent_buffer_for_io(struct extent_buffer *eb,
  3155. struct btrfs_fs_info *fs_info,
  3156. struct extent_page_data *epd)
  3157. {
  3158. unsigned long i, num_pages;
  3159. int flush = 0;
  3160. int ret = 0;
  3161. if (!btrfs_try_tree_write_lock(eb)) {
  3162. flush = 1;
  3163. flush_write_bio(epd);
  3164. btrfs_tree_lock(eb);
  3165. }
  3166. if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  3167. btrfs_tree_unlock(eb);
  3168. if (!epd->sync_io)
  3169. return 0;
  3170. if (!flush) {
  3171. flush_write_bio(epd);
  3172. flush = 1;
  3173. }
  3174. while (1) {
  3175. wait_on_extent_buffer_writeback(eb);
  3176. btrfs_tree_lock(eb);
  3177. if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
  3178. break;
  3179. btrfs_tree_unlock(eb);
  3180. }
  3181. }
  3182. /*
  3183. * We need to do this to prevent races in people who check if the eb is
  3184. * under IO since we can end up having no IO bits set for a short period
  3185. * of time.
  3186. */
  3187. spin_lock(&eb->refs_lock);
  3188. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3189. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3190. spin_unlock(&eb->refs_lock);
  3191. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  3192. percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
  3193. -eb->len,
  3194. fs_info->dirty_metadata_batch);
  3195. ret = 1;
  3196. } else {
  3197. spin_unlock(&eb->refs_lock);
  3198. }
  3199. btrfs_tree_unlock(eb);
  3200. if (!ret)
  3201. return ret;
  3202. num_pages = num_extent_pages(eb->start, eb->len);
  3203. for (i = 0; i < num_pages; i++) {
  3204. struct page *p = eb->pages[i];
  3205. if (!trylock_page(p)) {
  3206. if (!flush) {
  3207. flush_write_bio(epd);
  3208. flush = 1;
  3209. }
  3210. lock_page(p);
  3211. }
  3212. }
  3213. return ret;
  3214. }
  3215. static void end_extent_buffer_writeback(struct extent_buffer *eb)
  3216. {
  3217. clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3218. smp_mb__after_atomic();
  3219. wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
  3220. }
  3221. static void set_btree_ioerr(struct page *page)
  3222. {
  3223. struct extent_buffer *eb = (struct extent_buffer *)page->private;
  3224. SetPageError(page);
  3225. if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
  3226. return;
  3227. /*
  3228. * If writeback for a btree extent that doesn't belong to a log tree
  3229. * failed, increment the counter transaction->eb_write_errors.
  3230. * We do this because while the transaction is running and before it's
  3231. * committing (when we call filemap_fdata[write|wait]_range against
  3232. * the btree inode), we might have
  3233. * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
  3234. * returns an error or an error happens during writeback, when we're
  3235. * committing the transaction we wouldn't know about it, since the pages
  3236. * can be no longer dirty nor marked anymore for writeback (if a
  3237. * subsequent modification to the extent buffer didn't happen before the
  3238. * transaction commit), which makes filemap_fdata[write|wait]_range not
  3239. * able to find the pages tagged with SetPageError at transaction
  3240. * commit time. So if this happens we must abort the transaction,
  3241. * otherwise we commit a super block with btree roots that point to
  3242. * btree nodes/leafs whose content on disk is invalid - either garbage
  3243. * or the content of some node/leaf from a past generation that got
  3244. * cowed or deleted and is no longer valid.
  3245. *
  3246. * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
  3247. * not be enough - we need to distinguish between log tree extents vs
  3248. * non-log tree extents, and the next filemap_fdatawait_range() call
  3249. * will catch and clear such errors in the mapping - and that call might
  3250. * be from a log sync and not from a transaction commit. Also, checking
  3251. * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
  3252. * not done and would not be reliable - the eb might have been released
  3253. * from memory and reading it back again means that flag would not be
  3254. * set (since it's a runtime flag, not persisted on disk).
  3255. *
  3256. * Using the flags below in the btree inode also makes us achieve the
  3257. * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
  3258. * writeback for all dirty pages and before filemap_fdatawait_range()
  3259. * is called, the writeback for all dirty pages had already finished
  3260. * with errors - because we were not using AS_EIO/AS_ENOSPC,
  3261. * filemap_fdatawait_range() would return success, as it could not know
  3262. * that writeback errors happened (the pages were no longer tagged for
  3263. * writeback).
  3264. */
  3265. switch (eb->log_index) {
  3266. case -1:
  3267. set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
  3268. break;
  3269. case 0:
  3270. set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
  3271. break;
  3272. case 1:
  3273. set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
  3274. break;
  3275. default:
  3276. BUG(); /* unexpected, logic error */
  3277. }
  3278. }
  3279. static void end_bio_extent_buffer_writepage(struct bio *bio)
  3280. {
  3281. struct bio_vec *bvec;
  3282. struct extent_buffer *eb;
  3283. int i, done;
  3284. ASSERT(!bio_flagged(bio, BIO_CLONED));
  3285. bio_for_each_segment_all(bvec, bio, i) {
  3286. struct page *page = bvec->bv_page;
  3287. eb = (struct extent_buffer *)page->private;
  3288. BUG_ON(!eb);
  3289. done = atomic_dec_and_test(&eb->io_pages);
  3290. if (bio->bi_status ||
  3291. test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
  3292. ClearPageUptodate(page);
  3293. set_btree_ioerr(page);
  3294. }
  3295. end_page_writeback(page);
  3296. if (!done)
  3297. continue;
  3298. end_extent_buffer_writeback(eb);
  3299. }
  3300. bio_put(bio);
  3301. }
  3302. static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
  3303. struct btrfs_fs_info *fs_info,
  3304. struct writeback_control *wbc,
  3305. struct extent_page_data *epd)
  3306. {
  3307. struct block_device *bdev = fs_info->fs_devices->latest_bdev;
  3308. struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
  3309. u64 offset = eb->start;
  3310. u32 nritems;
  3311. unsigned long i, num_pages;
  3312. unsigned long start, end;
  3313. unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
  3314. int ret = 0;
  3315. clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
  3316. num_pages = num_extent_pages(eb->start, eb->len);
  3317. atomic_set(&eb->io_pages, num_pages);
  3318. /* set btree blocks beyond nritems with 0 to avoid stale content. */
  3319. nritems = btrfs_header_nritems(eb);
  3320. if (btrfs_header_level(eb) > 0) {
  3321. end = btrfs_node_key_ptr_offset(nritems);
  3322. memzero_extent_buffer(eb, end, eb->len - end);
  3323. } else {
  3324. /*
  3325. * leaf:
  3326. * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
  3327. */
  3328. start = btrfs_item_nr_offset(nritems);
  3329. end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
  3330. memzero_extent_buffer(eb, start, end - start);
  3331. }
  3332. for (i = 0; i < num_pages; i++) {
  3333. struct page *p = eb->pages[i];
  3334. clear_page_dirty_for_io(p);
  3335. set_page_writeback(p);
  3336. ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
  3337. p, offset, PAGE_SIZE, 0, bdev,
  3338. &epd->bio,
  3339. end_bio_extent_buffer_writepage,
  3340. 0, 0, 0, false);
  3341. if (ret) {
  3342. set_btree_ioerr(p);
  3343. if (PageWriteback(p))
  3344. end_page_writeback(p);
  3345. if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
  3346. end_extent_buffer_writeback(eb);
  3347. ret = -EIO;
  3348. break;
  3349. }
  3350. offset += PAGE_SIZE;
  3351. update_nr_written(wbc, 1);
  3352. unlock_page(p);
  3353. }
  3354. if (unlikely(ret)) {
  3355. for (; i < num_pages; i++) {
  3356. struct page *p = eb->pages[i];
  3357. clear_page_dirty_for_io(p);
  3358. unlock_page(p);
  3359. }
  3360. }
  3361. return ret;
  3362. }
  3363. int btree_write_cache_pages(struct address_space *mapping,
  3364. struct writeback_control *wbc)
  3365. {
  3366. struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
  3367. struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
  3368. struct extent_buffer *eb, *prev_eb = NULL;
  3369. struct extent_page_data epd = {
  3370. .bio = NULL,
  3371. .tree = tree,
  3372. .extent_locked = 0,
  3373. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3374. };
  3375. int ret = 0;
  3376. int done = 0;
  3377. int nr_to_write_done = 0;
  3378. struct pagevec pvec;
  3379. int nr_pages;
  3380. pgoff_t index;
  3381. pgoff_t end; /* Inclusive */
  3382. int scanned = 0;
  3383. int tag;
  3384. pagevec_init(&pvec);
  3385. if (wbc->range_cyclic) {
  3386. index = mapping->writeback_index; /* Start from prev offset */
  3387. end = -1;
  3388. } else {
  3389. index = wbc->range_start >> PAGE_SHIFT;
  3390. end = wbc->range_end >> PAGE_SHIFT;
  3391. scanned = 1;
  3392. }
  3393. if (wbc->sync_mode == WB_SYNC_ALL)
  3394. tag = PAGECACHE_TAG_TOWRITE;
  3395. else
  3396. tag = PAGECACHE_TAG_DIRTY;
  3397. retry:
  3398. if (wbc->sync_mode == WB_SYNC_ALL)
  3399. tag_pages_for_writeback(mapping, index, end);
  3400. while (!done && !nr_to_write_done && (index <= end) &&
  3401. (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
  3402. tag))) {
  3403. unsigned i;
  3404. scanned = 1;
  3405. for (i = 0; i < nr_pages; i++) {
  3406. struct page *page = pvec.pages[i];
  3407. if (!PagePrivate(page))
  3408. continue;
  3409. spin_lock(&mapping->private_lock);
  3410. if (!PagePrivate(page)) {
  3411. spin_unlock(&mapping->private_lock);
  3412. continue;
  3413. }
  3414. eb = (struct extent_buffer *)page->private;
  3415. /*
  3416. * Shouldn't happen and normally this would be a BUG_ON
  3417. * but no sense in crashing the users box for something
  3418. * we can survive anyway.
  3419. */
  3420. if (WARN_ON(!eb)) {
  3421. spin_unlock(&mapping->private_lock);
  3422. continue;
  3423. }
  3424. if (eb == prev_eb) {
  3425. spin_unlock(&mapping->private_lock);
  3426. continue;
  3427. }
  3428. ret = atomic_inc_not_zero(&eb->refs);
  3429. spin_unlock(&mapping->private_lock);
  3430. if (!ret)
  3431. continue;
  3432. prev_eb = eb;
  3433. ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
  3434. if (!ret) {
  3435. free_extent_buffer(eb);
  3436. continue;
  3437. }
  3438. ret = write_one_eb(eb, fs_info, wbc, &epd);
  3439. if (ret) {
  3440. done = 1;
  3441. free_extent_buffer(eb);
  3442. break;
  3443. }
  3444. free_extent_buffer(eb);
  3445. /*
  3446. * the filesystem may choose to bump up nr_to_write.
  3447. * We have to make sure to honor the new nr_to_write
  3448. * at any time
  3449. */
  3450. nr_to_write_done = wbc->nr_to_write <= 0;
  3451. }
  3452. pagevec_release(&pvec);
  3453. cond_resched();
  3454. }
  3455. if (!scanned && !done) {
  3456. /*
  3457. * We hit the last page and there is more work to be done: wrap
  3458. * back to the start of the file
  3459. */
  3460. scanned = 1;
  3461. index = 0;
  3462. goto retry;
  3463. }
  3464. flush_write_bio(&epd);
  3465. return ret;
  3466. }
  3467. /**
  3468. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  3469. * @mapping: address space structure to write
  3470. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  3471. * @writepage: function called for each page
  3472. * @data: data passed to writepage function
  3473. *
  3474. * If a page is already under I/O, write_cache_pages() skips it, even
  3475. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  3476. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  3477. * and msync() need to guarantee that all the data which was dirty at the time
  3478. * the call was made get new I/O started against them. If wbc->sync_mode is
  3479. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  3480. * existing IO to complete.
  3481. */
  3482. static int extent_write_cache_pages(struct address_space *mapping,
  3483. struct writeback_control *wbc,
  3484. writepage_t writepage, void *data,
  3485. void (*flush_fn)(void *))
  3486. {
  3487. struct inode *inode = mapping->host;
  3488. int ret = 0;
  3489. int done = 0;
  3490. int nr_to_write_done = 0;
  3491. struct pagevec pvec;
  3492. int nr_pages;
  3493. pgoff_t index;
  3494. pgoff_t end; /* Inclusive */
  3495. pgoff_t done_index;
  3496. int range_whole = 0;
  3497. int scanned = 0;
  3498. int tag;
  3499. /*
  3500. * We have to hold onto the inode so that ordered extents can do their
  3501. * work when the IO finishes. The alternative to this is failing to add
  3502. * an ordered extent if the igrab() fails there and that is a huge pain
  3503. * to deal with, so instead just hold onto the inode throughout the
  3504. * writepages operation. If it fails here we are freeing up the inode
  3505. * anyway and we'd rather not waste our time writing out stuff that is
  3506. * going to be truncated anyway.
  3507. */
  3508. if (!igrab(inode))
  3509. return 0;
  3510. pagevec_init(&pvec);
  3511. if (wbc->range_cyclic) {
  3512. index = mapping->writeback_index; /* Start from prev offset */
  3513. end = -1;
  3514. } else {
  3515. index = wbc->range_start >> PAGE_SHIFT;
  3516. end = wbc->range_end >> PAGE_SHIFT;
  3517. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  3518. range_whole = 1;
  3519. scanned = 1;
  3520. }
  3521. if (wbc->sync_mode == WB_SYNC_ALL)
  3522. tag = PAGECACHE_TAG_TOWRITE;
  3523. else
  3524. tag = PAGECACHE_TAG_DIRTY;
  3525. retry:
  3526. if (wbc->sync_mode == WB_SYNC_ALL)
  3527. tag_pages_for_writeback(mapping, index, end);
  3528. done_index = index;
  3529. while (!done && !nr_to_write_done && (index <= end) &&
  3530. (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
  3531. &index, end, tag))) {
  3532. unsigned i;
  3533. scanned = 1;
  3534. for (i = 0; i < nr_pages; i++) {
  3535. struct page *page = pvec.pages[i];
  3536. done_index = page->index;
  3537. /*
  3538. * At this point we hold neither mapping->tree_lock nor
  3539. * lock on the page itself: the page may be truncated or
  3540. * invalidated (changing page->mapping to NULL), or even
  3541. * swizzled back from swapper_space to tmpfs file
  3542. * mapping
  3543. */
  3544. if (!trylock_page(page)) {
  3545. flush_fn(data);
  3546. lock_page(page);
  3547. }
  3548. if (unlikely(page->mapping != mapping)) {
  3549. unlock_page(page);
  3550. continue;
  3551. }
  3552. if (wbc->sync_mode != WB_SYNC_NONE) {
  3553. if (PageWriteback(page))
  3554. flush_fn(data);
  3555. wait_on_page_writeback(page);
  3556. }
  3557. if (PageWriteback(page) ||
  3558. !clear_page_dirty_for_io(page)) {
  3559. unlock_page(page);
  3560. continue;
  3561. }
  3562. ret = (*writepage)(page, wbc, data);
  3563. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  3564. unlock_page(page);
  3565. ret = 0;
  3566. }
  3567. if (ret < 0) {
  3568. /*
  3569. * done_index is set past this page,
  3570. * so media errors will not choke
  3571. * background writeout for the entire
  3572. * file. This has consequences for
  3573. * range_cyclic semantics (ie. it may
  3574. * not be suitable for data integrity
  3575. * writeout).
  3576. */
  3577. done_index = page->index + 1;
  3578. done = 1;
  3579. break;
  3580. }
  3581. /*
  3582. * the filesystem may choose to bump up nr_to_write.
  3583. * We have to make sure to honor the new nr_to_write
  3584. * at any time
  3585. */
  3586. nr_to_write_done = wbc->nr_to_write <= 0;
  3587. }
  3588. pagevec_release(&pvec);
  3589. cond_resched();
  3590. }
  3591. if (!scanned && !done) {
  3592. /*
  3593. * We hit the last page and there is more work to be done: wrap
  3594. * back to the start of the file
  3595. */
  3596. scanned = 1;
  3597. index = 0;
  3598. goto retry;
  3599. }
  3600. if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
  3601. mapping->writeback_index = done_index;
  3602. btrfs_add_delayed_iput(inode);
  3603. return ret;
  3604. }
  3605. static void flush_epd_write_bio(struct extent_page_data *epd)
  3606. {
  3607. if (epd->bio) {
  3608. int ret;
  3609. ret = submit_one_bio(epd->bio, 0, 0);
  3610. BUG_ON(ret < 0); /* -ENOMEM */
  3611. epd->bio = NULL;
  3612. }
  3613. }
  3614. static noinline void flush_write_bio(void *data)
  3615. {
  3616. struct extent_page_data *epd = data;
  3617. flush_epd_write_bio(epd);
  3618. }
  3619. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  3620. get_extent_t *get_extent,
  3621. struct writeback_control *wbc)
  3622. {
  3623. int ret;
  3624. struct extent_page_data epd = {
  3625. .bio = NULL,
  3626. .tree = tree,
  3627. .get_extent = get_extent,
  3628. .extent_locked = 0,
  3629. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3630. };
  3631. ret = __extent_writepage(page, wbc, &epd);
  3632. flush_epd_write_bio(&epd);
  3633. return ret;
  3634. }
  3635. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  3636. u64 start, u64 end, get_extent_t *get_extent,
  3637. int mode)
  3638. {
  3639. int ret = 0;
  3640. struct address_space *mapping = inode->i_mapping;
  3641. struct page *page;
  3642. unsigned long nr_pages = (end - start + PAGE_SIZE) >>
  3643. PAGE_SHIFT;
  3644. struct extent_page_data epd = {
  3645. .bio = NULL,
  3646. .tree = tree,
  3647. .get_extent = get_extent,
  3648. .extent_locked = 1,
  3649. .sync_io = mode == WB_SYNC_ALL,
  3650. };
  3651. struct writeback_control wbc_writepages = {
  3652. .sync_mode = mode,
  3653. .nr_to_write = nr_pages * 2,
  3654. .range_start = start,
  3655. .range_end = end + 1,
  3656. };
  3657. while (start <= end) {
  3658. page = find_get_page(mapping, start >> PAGE_SHIFT);
  3659. if (clear_page_dirty_for_io(page))
  3660. ret = __extent_writepage(page, &wbc_writepages, &epd);
  3661. else {
  3662. if (tree->ops && tree->ops->writepage_end_io_hook)
  3663. tree->ops->writepage_end_io_hook(page, start,
  3664. start + PAGE_SIZE - 1,
  3665. NULL, 1);
  3666. unlock_page(page);
  3667. }
  3668. put_page(page);
  3669. start += PAGE_SIZE;
  3670. }
  3671. flush_epd_write_bio(&epd);
  3672. return ret;
  3673. }
  3674. int extent_writepages(struct extent_io_tree *tree,
  3675. struct address_space *mapping,
  3676. get_extent_t *get_extent,
  3677. struct writeback_control *wbc)
  3678. {
  3679. int ret = 0;
  3680. struct extent_page_data epd = {
  3681. .bio = NULL,
  3682. .tree = tree,
  3683. .get_extent = get_extent,
  3684. .extent_locked = 0,
  3685. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3686. };
  3687. ret = extent_write_cache_pages(mapping, wbc, __extent_writepage, &epd,
  3688. flush_write_bio);
  3689. flush_epd_write_bio(&epd);
  3690. return ret;
  3691. }
  3692. int extent_readpages(struct extent_io_tree *tree,
  3693. struct address_space *mapping,
  3694. struct list_head *pages, unsigned nr_pages,
  3695. get_extent_t get_extent)
  3696. {
  3697. struct bio *bio = NULL;
  3698. unsigned page_idx;
  3699. unsigned long bio_flags = 0;
  3700. struct page *pagepool[16];
  3701. struct page *page;
  3702. struct extent_map *em_cached = NULL;
  3703. int nr = 0;
  3704. u64 prev_em_start = (u64)-1;
  3705. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  3706. page = list_entry(pages->prev, struct page, lru);
  3707. prefetchw(&page->flags);
  3708. list_del(&page->lru);
  3709. if (add_to_page_cache_lru(page, mapping,
  3710. page->index,
  3711. readahead_gfp_mask(mapping))) {
  3712. put_page(page);
  3713. continue;
  3714. }
  3715. pagepool[nr++] = page;
  3716. if (nr < ARRAY_SIZE(pagepool))
  3717. continue;
  3718. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3719. &bio, 0, &bio_flags, &prev_em_start);
  3720. nr = 0;
  3721. }
  3722. if (nr)
  3723. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3724. &bio, 0, &bio_flags, &prev_em_start);
  3725. if (em_cached)
  3726. free_extent_map(em_cached);
  3727. BUG_ON(!list_empty(pages));
  3728. if (bio)
  3729. return submit_one_bio(bio, 0, bio_flags);
  3730. return 0;
  3731. }
  3732. /*
  3733. * basic invalidatepage code, this waits on any locked or writeback
  3734. * ranges corresponding to the page, and then deletes any extent state
  3735. * records from the tree
  3736. */
  3737. int extent_invalidatepage(struct extent_io_tree *tree,
  3738. struct page *page, unsigned long offset)
  3739. {
  3740. struct extent_state *cached_state = NULL;
  3741. u64 start = page_offset(page);
  3742. u64 end = start + PAGE_SIZE - 1;
  3743. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  3744. start += ALIGN(offset, blocksize);
  3745. if (start > end)
  3746. return 0;
  3747. lock_extent_bits(tree, start, end, &cached_state);
  3748. wait_on_page_writeback(page);
  3749. clear_extent_bit(tree, start, end,
  3750. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3751. EXTENT_DO_ACCOUNTING,
  3752. 1, 1, &cached_state, GFP_NOFS);
  3753. return 0;
  3754. }
  3755. /*
  3756. * a helper for releasepage, this tests for areas of the page that
  3757. * are locked or under IO and drops the related state bits if it is safe
  3758. * to drop the page.
  3759. */
  3760. static int try_release_extent_state(struct extent_map_tree *map,
  3761. struct extent_io_tree *tree,
  3762. struct page *page, gfp_t mask)
  3763. {
  3764. u64 start = page_offset(page);
  3765. u64 end = start + PAGE_SIZE - 1;
  3766. int ret = 1;
  3767. if (test_range_bit(tree, start, end,
  3768. EXTENT_IOBITS, 0, NULL))
  3769. ret = 0;
  3770. else {
  3771. /*
  3772. * at this point we can safely clear everything except the
  3773. * locked bit and the nodatasum bit
  3774. */
  3775. ret = clear_extent_bit(tree, start, end,
  3776. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  3777. 0, 0, NULL, mask);
  3778. /* if clear_extent_bit failed for enomem reasons,
  3779. * we can't allow the release to continue.
  3780. */
  3781. if (ret < 0)
  3782. ret = 0;
  3783. else
  3784. ret = 1;
  3785. }
  3786. return ret;
  3787. }
  3788. /*
  3789. * a helper for releasepage. As long as there are no locked extents
  3790. * in the range corresponding to the page, both state records and extent
  3791. * map records are removed
  3792. */
  3793. int try_release_extent_mapping(struct extent_map_tree *map,
  3794. struct extent_io_tree *tree, struct page *page,
  3795. gfp_t mask)
  3796. {
  3797. struct extent_map *em;
  3798. u64 start = page_offset(page);
  3799. u64 end = start + PAGE_SIZE - 1;
  3800. if (gfpflags_allow_blocking(mask) &&
  3801. page->mapping->host->i_size > SZ_16M) {
  3802. u64 len;
  3803. while (start <= end) {
  3804. len = end - start + 1;
  3805. write_lock(&map->lock);
  3806. em = lookup_extent_mapping(map, start, len);
  3807. if (!em) {
  3808. write_unlock(&map->lock);
  3809. break;
  3810. }
  3811. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3812. em->start != start) {
  3813. write_unlock(&map->lock);
  3814. free_extent_map(em);
  3815. break;
  3816. }
  3817. if (!test_range_bit(tree, em->start,
  3818. extent_map_end(em) - 1,
  3819. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3820. 0, NULL)) {
  3821. remove_extent_mapping(map, em);
  3822. /* once for the rb tree */
  3823. free_extent_map(em);
  3824. }
  3825. start = extent_map_end(em);
  3826. write_unlock(&map->lock);
  3827. /* once for us */
  3828. free_extent_map(em);
  3829. }
  3830. }
  3831. return try_release_extent_state(map, tree, page, mask);
  3832. }
  3833. /*
  3834. * helper function for fiemap, which doesn't want to see any holes.
  3835. * This maps until we find something past 'last'
  3836. */
  3837. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3838. u64 offset,
  3839. u64 last,
  3840. get_extent_t *get_extent)
  3841. {
  3842. u64 sectorsize = btrfs_inode_sectorsize(inode);
  3843. struct extent_map *em;
  3844. u64 len;
  3845. if (offset >= last)
  3846. return NULL;
  3847. while (1) {
  3848. len = last - offset;
  3849. if (len == 0)
  3850. break;
  3851. len = ALIGN(len, sectorsize);
  3852. em = get_extent(BTRFS_I(inode), NULL, 0, offset, len, 0);
  3853. if (IS_ERR_OR_NULL(em))
  3854. return em;
  3855. /* if this isn't a hole return it */
  3856. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3857. em->block_start != EXTENT_MAP_HOLE) {
  3858. return em;
  3859. }
  3860. /* this is a hole, advance to the next extent */
  3861. offset = extent_map_end(em);
  3862. free_extent_map(em);
  3863. if (offset >= last)
  3864. break;
  3865. }
  3866. return NULL;
  3867. }
  3868. /*
  3869. * To cache previous fiemap extent
  3870. *
  3871. * Will be used for merging fiemap extent
  3872. */
  3873. struct fiemap_cache {
  3874. u64 offset;
  3875. u64 phys;
  3876. u64 len;
  3877. u32 flags;
  3878. bool cached;
  3879. };
  3880. /*
  3881. * Helper to submit fiemap extent.
  3882. *
  3883. * Will try to merge current fiemap extent specified by @offset, @phys,
  3884. * @len and @flags with cached one.
  3885. * And only when we fails to merge, cached one will be submitted as
  3886. * fiemap extent.
  3887. *
  3888. * Return value is the same as fiemap_fill_next_extent().
  3889. */
  3890. static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
  3891. struct fiemap_cache *cache,
  3892. u64 offset, u64 phys, u64 len, u32 flags)
  3893. {
  3894. int ret = 0;
  3895. if (!cache->cached)
  3896. goto assign;
  3897. /*
  3898. * Sanity check, extent_fiemap() should have ensured that new
  3899. * fiemap extent won't overlap with cahced one.
  3900. * Not recoverable.
  3901. *
  3902. * NOTE: Physical address can overlap, due to compression
  3903. */
  3904. if (cache->offset + cache->len > offset) {
  3905. WARN_ON(1);
  3906. return -EINVAL;
  3907. }
  3908. /*
  3909. * Only merges fiemap extents if
  3910. * 1) Their logical addresses are continuous
  3911. *
  3912. * 2) Their physical addresses are continuous
  3913. * So truly compressed (physical size smaller than logical size)
  3914. * extents won't get merged with each other
  3915. *
  3916. * 3) Share same flags except FIEMAP_EXTENT_LAST
  3917. * So regular extent won't get merged with prealloc extent
  3918. */
  3919. if (cache->offset + cache->len == offset &&
  3920. cache->phys + cache->len == phys &&
  3921. (cache->flags & ~FIEMAP_EXTENT_LAST) ==
  3922. (flags & ~FIEMAP_EXTENT_LAST)) {
  3923. cache->len += len;
  3924. cache->flags |= flags;
  3925. goto try_submit_last;
  3926. }
  3927. /* Not mergeable, need to submit cached one */
  3928. ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
  3929. cache->len, cache->flags);
  3930. cache->cached = false;
  3931. if (ret)
  3932. return ret;
  3933. assign:
  3934. cache->cached = true;
  3935. cache->offset = offset;
  3936. cache->phys = phys;
  3937. cache->len = len;
  3938. cache->flags = flags;
  3939. try_submit_last:
  3940. if (cache->flags & FIEMAP_EXTENT_LAST) {
  3941. ret = fiemap_fill_next_extent(fieinfo, cache->offset,
  3942. cache->phys, cache->len, cache->flags);
  3943. cache->cached = false;
  3944. }
  3945. return ret;
  3946. }
  3947. /*
  3948. * Emit last fiemap cache
  3949. *
  3950. * The last fiemap cache may still be cached in the following case:
  3951. * 0 4k 8k
  3952. * |<- Fiemap range ->|
  3953. * |<------------ First extent ----------->|
  3954. *
  3955. * In this case, the first extent range will be cached but not emitted.
  3956. * So we must emit it before ending extent_fiemap().
  3957. */
  3958. static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
  3959. struct fiemap_extent_info *fieinfo,
  3960. struct fiemap_cache *cache)
  3961. {
  3962. int ret;
  3963. if (!cache->cached)
  3964. return 0;
  3965. ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
  3966. cache->len, cache->flags);
  3967. cache->cached = false;
  3968. if (ret > 0)
  3969. ret = 0;
  3970. return ret;
  3971. }
  3972. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3973. __u64 start, __u64 len, get_extent_t *get_extent)
  3974. {
  3975. int ret = 0;
  3976. u64 off = start;
  3977. u64 max = start + len;
  3978. u32 flags = 0;
  3979. u32 found_type;
  3980. u64 last;
  3981. u64 last_for_get_extent = 0;
  3982. u64 disko = 0;
  3983. u64 isize = i_size_read(inode);
  3984. struct btrfs_key found_key;
  3985. struct extent_map *em = NULL;
  3986. struct extent_state *cached_state = NULL;
  3987. struct btrfs_path *path;
  3988. struct btrfs_root *root = BTRFS_I(inode)->root;
  3989. struct fiemap_cache cache = { 0 };
  3990. int end = 0;
  3991. u64 em_start = 0;
  3992. u64 em_len = 0;
  3993. u64 em_end = 0;
  3994. if (len == 0)
  3995. return -EINVAL;
  3996. path = btrfs_alloc_path();
  3997. if (!path)
  3998. return -ENOMEM;
  3999. path->leave_spinning = 1;
  4000. start = round_down(start, btrfs_inode_sectorsize(inode));
  4001. len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
  4002. /*
  4003. * lookup the last file extent. We're not using i_size here
  4004. * because there might be preallocation past i_size
  4005. */
  4006. ret = btrfs_lookup_file_extent(NULL, root, path,
  4007. btrfs_ino(BTRFS_I(inode)), -1, 0);
  4008. if (ret < 0) {
  4009. btrfs_free_path(path);
  4010. return ret;
  4011. } else {
  4012. WARN_ON(!ret);
  4013. if (ret == 1)
  4014. ret = 0;
  4015. }
  4016. path->slots[0]--;
  4017. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  4018. found_type = found_key.type;
  4019. /* No extents, but there might be delalloc bits */
  4020. if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
  4021. found_type != BTRFS_EXTENT_DATA_KEY) {
  4022. /* have to trust i_size as the end */
  4023. last = (u64)-1;
  4024. last_for_get_extent = isize;
  4025. } else {
  4026. /*
  4027. * remember the start of the last extent. There are a
  4028. * bunch of different factors that go into the length of the
  4029. * extent, so its much less complex to remember where it started
  4030. */
  4031. last = found_key.offset;
  4032. last_for_get_extent = last + 1;
  4033. }
  4034. btrfs_release_path(path);
  4035. /*
  4036. * we might have some extents allocated but more delalloc past those
  4037. * extents. so, we trust isize unless the start of the last extent is
  4038. * beyond isize
  4039. */
  4040. if (last < isize) {
  4041. last = (u64)-1;
  4042. last_for_get_extent = isize;
  4043. }
  4044. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4045. &cached_state);
  4046. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  4047. get_extent);
  4048. if (!em)
  4049. goto out;
  4050. if (IS_ERR(em)) {
  4051. ret = PTR_ERR(em);
  4052. goto out;
  4053. }
  4054. while (!end) {
  4055. u64 offset_in_extent = 0;
  4056. /* break if the extent we found is outside the range */
  4057. if (em->start >= max || extent_map_end(em) < off)
  4058. break;
  4059. /*
  4060. * get_extent may return an extent that starts before our
  4061. * requested range. We have to make sure the ranges
  4062. * we return to fiemap always move forward and don't
  4063. * overlap, so adjust the offsets here
  4064. */
  4065. em_start = max(em->start, off);
  4066. /*
  4067. * record the offset from the start of the extent
  4068. * for adjusting the disk offset below. Only do this if the
  4069. * extent isn't compressed since our in ram offset may be past
  4070. * what we have actually allocated on disk.
  4071. */
  4072. if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4073. offset_in_extent = em_start - em->start;
  4074. em_end = extent_map_end(em);
  4075. em_len = em_end - em_start;
  4076. disko = 0;
  4077. flags = 0;
  4078. /*
  4079. * bump off for our next call to get_extent
  4080. */
  4081. off = extent_map_end(em);
  4082. if (off >= max)
  4083. end = 1;
  4084. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  4085. end = 1;
  4086. flags |= FIEMAP_EXTENT_LAST;
  4087. } else if (em->block_start == EXTENT_MAP_INLINE) {
  4088. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  4089. FIEMAP_EXTENT_NOT_ALIGNED);
  4090. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  4091. flags |= (FIEMAP_EXTENT_DELALLOC |
  4092. FIEMAP_EXTENT_UNKNOWN);
  4093. } else if (fieinfo->fi_extents_max) {
  4094. u64 bytenr = em->block_start -
  4095. (em->start - em->orig_start);
  4096. disko = em->block_start + offset_in_extent;
  4097. /*
  4098. * As btrfs supports shared space, this information
  4099. * can be exported to userspace tools via
  4100. * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
  4101. * then we're just getting a count and we can skip the
  4102. * lookup stuff.
  4103. */
  4104. ret = btrfs_check_shared(root,
  4105. btrfs_ino(BTRFS_I(inode)),
  4106. bytenr);
  4107. if (ret < 0)
  4108. goto out_free;
  4109. if (ret)
  4110. flags |= FIEMAP_EXTENT_SHARED;
  4111. ret = 0;
  4112. }
  4113. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4114. flags |= FIEMAP_EXTENT_ENCODED;
  4115. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4116. flags |= FIEMAP_EXTENT_UNWRITTEN;
  4117. free_extent_map(em);
  4118. em = NULL;
  4119. if ((em_start >= last) || em_len == (u64)-1 ||
  4120. (last == (u64)-1 && isize <= em_end)) {
  4121. flags |= FIEMAP_EXTENT_LAST;
  4122. end = 1;
  4123. }
  4124. /* now scan forward to see if this is really the last extent. */
  4125. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  4126. get_extent);
  4127. if (IS_ERR(em)) {
  4128. ret = PTR_ERR(em);
  4129. goto out;
  4130. }
  4131. if (!em) {
  4132. flags |= FIEMAP_EXTENT_LAST;
  4133. end = 1;
  4134. }
  4135. ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
  4136. em_len, flags);
  4137. if (ret) {
  4138. if (ret == 1)
  4139. ret = 0;
  4140. goto out_free;
  4141. }
  4142. }
  4143. out_free:
  4144. if (!ret)
  4145. ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
  4146. free_extent_map(em);
  4147. out:
  4148. btrfs_free_path(path);
  4149. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4150. &cached_state, GFP_NOFS);
  4151. return ret;
  4152. }
  4153. static void __free_extent_buffer(struct extent_buffer *eb)
  4154. {
  4155. btrfs_leak_debug_del(&eb->leak_list);
  4156. kmem_cache_free(extent_buffer_cache, eb);
  4157. }
  4158. int extent_buffer_under_io(struct extent_buffer *eb)
  4159. {
  4160. return (atomic_read(&eb->io_pages) ||
  4161. test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  4162. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4163. }
  4164. /*
  4165. * Helper for releasing extent buffer page.
  4166. */
  4167. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
  4168. {
  4169. unsigned long index;
  4170. struct page *page;
  4171. int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4172. BUG_ON(extent_buffer_under_io(eb));
  4173. index = num_extent_pages(eb->start, eb->len);
  4174. if (index == 0)
  4175. return;
  4176. do {
  4177. index--;
  4178. page = eb->pages[index];
  4179. if (!page)
  4180. continue;
  4181. if (mapped)
  4182. spin_lock(&page->mapping->private_lock);
  4183. /*
  4184. * We do this since we'll remove the pages after we've
  4185. * removed the eb from the radix tree, so we could race
  4186. * and have this page now attached to the new eb. So
  4187. * only clear page_private if it's still connected to
  4188. * this eb.
  4189. */
  4190. if (PagePrivate(page) &&
  4191. page->private == (unsigned long)eb) {
  4192. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4193. BUG_ON(PageDirty(page));
  4194. BUG_ON(PageWriteback(page));
  4195. /*
  4196. * We need to make sure we haven't be attached
  4197. * to a new eb.
  4198. */
  4199. ClearPagePrivate(page);
  4200. set_page_private(page, 0);
  4201. /* One for the page private */
  4202. put_page(page);
  4203. }
  4204. if (mapped)
  4205. spin_unlock(&page->mapping->private_lock);
  4206. /* One for when we allocated the page */
  4207. put_page(page);
  4208. } while (index != 0);
  4209. }
  4210. /*
  4211. * Helper for releasing the extent buffer.
  4212. */
  4213. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  4214. {
  4215. btrfs_release_extent_buffer_page(eb);
  4216. __free_extent_buffer(eb);
  4217. }
  4218. static struct extent_buffer *
  4219. __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
  4220. unsigned long len)
  4221. {
  4222. struct extent_buffer *eb = NULL;
  4223. eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
  4224. eb->start = start;
  4225. eb->len = len;
  4226. eb->fs_info = fs_info;
  4227. eb->bflags = 0;
  4228. rwlock_init(&eb->lock);
  4229. atomic_set(&eb->write_locks, 0);
  4230. atomic_set(&eb->read_locks, 0);
  4231. atomic_set(&eb->blocking_readers, 0);
  4232. atomic_set(&eb->blocking_writers, 0);
  4233. atomic_set(&eb->spinning_readers, 0);
  4234. atomic_set(&eb->spinning_writers, 0);
  4235. eb->lock_nested = 0;
  4236. init_waitqueue_head(&eb->write_lock_wq);
  4237. init_waitqueue_head(&eb->read_lock_wq);
  4238. btrfs_leak_debug_add(&eb->leak_list, &buffers);
  4239. spin_lock_init(&eb->refs_lock);
  4240. atomic_set(&eb->refs, 1);
  4241. atomic_set(&eb->io_pages, 0);
  4242. /*
  4243. * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
  4244. */
  4245. BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
  4246. > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4247. BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4248. return eb;
  4249. }
  4250. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
  4251. {
  4252. unsigned long i;
  4253. struct page *p;
  4254. struct extent_buffer *new;
  4255. unsigned long num_pages = num_extent_pages(src->start, src->len);
  4256. new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
  4257. if (new == NULL)
  4258. return NULL;
  4259. for (i = 0; i < num_pages; i++) {
  4260. p = alloc_page(GFP_NOFS);
  4261. if (!p) {
  4262. btrfs_release_extent_buffer(new);
  4263. return NULL;
  4264. }
  4265. attach_extent_buffer_page(new, p);
  4266. WARN_ON(PageDirty(p));
  4267. SetPageUptodate(p);
  4268. new->pages[i] = p;
  4269. copy_page(page_address(p), page_address(src->pages[i]));
  4270. }
  4271. set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
  4272. set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
  4273. return new;
  4274. }
  4275. struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4276. u64 start, unsigned long len)
  4277. {
  4278. struct extent_buffer *eb;
  4279. unsigned long num_pages;
  4280. unsigned long i;
  4281. num_pages = num_extent_pages(start, len);
  4282. eb = __alloc_extent_buffer(fs_info, start, len);
  4283. if (!eb)
  4284. return NULL;
  4285. for (i = 0; i < num_pages; i++) {
  4286. eb->pages[i] = alloc_page(GFP_NOFS);
  4287. if (!eb->pages[i])
  4288. goto err;
  4289. }
  4290. set_extent_buffer_uptodate(eb);
  4291. btrfs_set_header_nritems(eb, 0);
  4292. set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4293. return eb;
  4294. err:
  4295. for (; i > 0; i--)
  4296. __free_page(eb->pages[i - 1]);
  4297. __free_extent_buffer(eb);
  4298. return NULL;
  4299. }
  4300. struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4301. u64 start)
  4302. {
  4303. return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
  4304. }
  4305. static void check_buffer_tree_ref(struct extent_buffer *eb)
  4306. {
  4307. int refs;
  4308. /* the ref bit is tricky. We have to make sure it is set
  4309. * if we have the buffer dirty. Otherwise the
  4310. * code to free a buffer can end up dropping a dirty
  4311. * page
  4312. *
  4313. * Once the ref bit is set, it won't go away while the
  4314. * buffer is dirty or in writeback, and it also won't
  4315. * go away while we have the reference count on the
  4316. * eb bumped.
  4317. *
  4318. * We can't just set the ref bit without bumping the
  4319. * ref on the eb because free_extent_buffer might
  4320. * see the ref bit and try to clear it. If this happens
  4321. * free_extent_buffer might end up dropping our original
  4322. * ref by mistake and freeing the page before we are able
  4323. * to add one more ref.
  4324. *
  4325. * So bump the ref count first, then set the bit. If someone
  4326. * beat us to it, drop the ref we added.
  4327. */
  4328. refs = atomic_read(&eb->refs);
  4329. if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4330. return;
  4331. spin_lock(&eb->refs_lock);
  4332. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4333. atomic_inc(&eb->refs);
  4334. spin_unlock(&eb->refs_lock);
  4335. }
  4336. static void mark_extent_buffer_accessed(struct extent_buffer *eb,
  4337. struct page *accessed)
  4338. {
  4339. unsigned long num_pages, i;
  4340. check_buffer_tree_ref(eb);
  4341. num_pages = num_extent_pages(eb->start, eb->len);
  4342. for (i = 0; i < num_pages; i++) {
  4343. struct page *p = eb->pages[i];
  4344. if (p != accessed)
  4345. mark_page_accessed(p);
  4346. }
  4347. }
  4348. struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
  4349. u64 start)
  4350. {
  4351. struct extent_buffer *eb;
  4352. rcu_read_lock();
  4353. eb = radix_tree_lookup(&fs_info->buffer_radix,
  4354. start >> PAGE_SHIFT);
  4355. if (eb && atomic_inc_not_zero(&eb->refs)) {
  4356. rcu_read_unlock();
  4357. /*
  4358. * Lock our eb's refs_lock to avoid races with
  4359. * free_extent_buffer. When we get our eb it might be flagged
  4360. * with EXTENT_BUFFER_STALE and another task running
  4361. * free_extent_buffer might have seen that flag set,
  4362. * eb->refs == 2, that the buffer isn't under IO (dirty and
  4363. * writeback flags not set) and it's still in the tree (flag
  4364. * EXTENT_BUFFER_TREE_REF set), therefore being in the process
  4365. * of decrementing the extent buffer's reference count twice.
  4366. * So here we could race and increment the eb's reference count,
  4367. * clear its stale flag, mark it as dirty and drop our reference
  4368. * before the other task finishes executing free_extent_buffer,
  4369. * which would later result in an attempt to free an extent
  4370. * buffer that is dirty.
  4371. */
  4372. if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
  4373. spin_lock(&eb->refs_lock);
  4374. spin_unlock(&eb->refs_lock);
  4375. }
  4376. mark_extent_buffer_accessed(eb, NULL);
  4377. return eb;
  4378. }
  4379. rcu_read_unlock();
  4380. return NULL;
  4381. }
  4382. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4383. struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
  4384. u64 start)
  4385. {
  4386. struct extent_buffer *eb, *exists = NULL;
  4387. int ret;
  4388. eb = find_extent_buffer(fs_info, start);
  4389. if (eb)
  4390. return eb;
  4391. eb = alloc_dummy_extent_buffer(fs_info, start);
  4392. if (!eb)
  4393. return NULL;
  4394. eb->fs_info = fs_info;
  4395. again:
  4396. ret = radix_tree_preload(GFP_NOFS);
  4397. if (ret)
  4398. goto free_eb;
  4399. spin_lock(&fs_info->buffer_lock);
  4400. ret = radix_tree_insert(&fs_info->buffer_radix,
  4401. start >> PAGE_SHIFT, eb);
  4402. spin_unlock(&fs_info->buffer_lock);
  4403. radix_tree_preload_end();
  4404. if (ret == -EEXIST) {
  4405. exists = find_extent_buffer(fs_info, start);
  4406. if (exists)
  4407. goto free_eb;
  4408. else
  4409. goto again;
  4410. }
  4411. check_buffer_tree_ref(eb);
  4412. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4413. /*
  4414. * We will free dummy extent buffer's if they come into
  4415. * free_extent_buffer with a ref count of 2, but if we are using this we
  4416. * want the buffers to stay in memory until we're done with them, so
  4417. * bump the ref count again.
  4418. */
  4419. atomic_inc(&eb->refs);
  4420. return eb;
  4421. free_eb:
  4422. btrfs_release_extent_buffer(eb);
  4423. return exists;
  4424. }
  4425. #endif
  4426. struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
  4427. u64 start)
  4428. {
  4429. unsigned long len = fs_info->nodesize;
  4430. unsigned long num_pages = num_extent_pages(start, len);
  4431. unsigned long i;
  4432. unsigned long index = start >> PAGE_SHIFT;
  4433. struct extent_buffer *eb;
  4434. struct extent_buffer *exists = NULL;
  4435. struct page *p;
  4436. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  4437. int uptodate = 1;
  4438. int ret;
  4439. if (!IS_ALIGNED(start, fs_info->sectorsize)) {
  4440. btrfs_err(fs_info, "bad tree block start %llu", start);
  4441. return ERR_PTR(-EINVAL);
  4442. }
  4443. eb = find_extent_buffer(fs_info, start);
  4444. if (eb)
  4445. return eb;
  4446. eb = __alloc_extent_buffer(fs_info, start, len);
  4447. if (!eb)
  4448. return ERR_PTR(-ENOMEM);
  4449. for (i = 0; i < num_pages; i++, index++) {
  4450. p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
  4451. if (!p) {
  4452. exists = ERR_PTR(-ENOMEM);
  4453. goto free_eb;
  4454. }
  4455. spin_lock(&mapping->private_lock);
  4456. if (PagePrivate(p)) {
  4457. /*
  4458. * We could have already allocated an eb for this page
  4459. * and attached one so lets see if we can get a ref on
  4460. * the existing eb, and if we can we know it's good and
  4461. * we can just return that one, else we know we can just
  4462. * overwrite page->private.
  4463. */
  4464. exists = (struct extent_buffer *)p->private;
  4465. if (atomic_inc_not_zero(&exists->refs)) {
  4466. spin_unlock(&mapping->private_lock);
  4467. unlock_page(p);
  4468. put_page(p);
  4469. mark_extent_buffer_accessed(exists, p);
  4470. goto free_eb;
  4471. }
  4472. exists = NULL;
  4473. /*
  4474. * Do this so attach doesn't complain and we need to
  4475. * drop the ref the old guy had.
  4476. */
  4477. ClearPagePrivate(p);
  4478. WARN_ON(PageDirty(p));
  4479. put_page(p);
  4480. }
  4481. attach_extent_buffer_page(eb, p);
  4482. spin_unlock(&mapping->private_lock);
  4483. WARN_ON(PageDirty(p));
  4484. eb->pages[i] = p;
  4485. if (!PageUptodate(p))
  4486. uptodate = 0;
  4487. /*
  4488. * see below about how we avoid a nasty race with release page
  4489. * and why we unlock later
  4490. */
  4491. }
  4492. if (uptodate)
  4493. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4494. again:
  4495. ret = radix_tree_preload(GFP_NOFS);
  4496. if (ret) {
  4497. exists = ERR_PTR(ret);
  4498. goto free_eb;
  4499. }
  4500. spin_lock(&fs_info->buffer_lock);
  4501. ret = radix_tree_insert(&fs_info->buffer_radix,
  4502. start >> PAGE_SHIFT, eb);
  4503. spin_unlock(&fs_info->buffer_lock);
  4504. radix_tree_preload_end();
  4505. if (ret == -EEXIST) {
  4506. exists = find_extent_buffer(fs_info, start);
  4507. if (exists)
  4508. goto free_eb;
  4509. else
  4510. goto again;
  4511. }
  4512. /* add one reference for the tree */
  4513. check_buffer_tree_ref(eb);
  4514. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4515. /*
  4516. * there is a race where release page may have
  4517. * tried to find this extent buffer in the radix
  4518. * but failed. It will tell the VM it is safe to
  4519. * reclaim the, and it will clear the page private bit.
  4520. * We must make sure to set the page private bit properly
  4521. * after the extent buffer is in the radix tree so
  4522. * it doesn't get lost
  4523. */
  4524. SetPageChecked(eb->pages[0]);
  4525. for (i = 1; i < num_pages; i++) {
  4526. p = eb->pages[i];
  4527. ClearPageChecked(p);
  4528. unlock_page(p);
  4529. }
  4530. unlock_page(eb->pages[0]);
  4531. return eb;
  4532. free_eb:
  4533. WARN_ON(!atomic_dec_and_test(&eb->refs));
  4534. for (i = 0; i < num_pages; i++) {
  4535. if (eb->pages[i])
  4536. unlock_page(eb->pages[i]);
  4537. }
  4538. btrfs_release_extent_buffer(eb);
  4539. return exists;
  4540. }
  4541. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  4542. {
  4543. struct extent_buffer *eb =
  4544. container_of(head, struct extent_buffer, rcu_head);
  4545. __free_extent_buffer(eb);
  4546. }
  4547. /* Expects to have eb->eb_lock already held */
  4548. static int release_extent_buffer(struct extent_buffer *eb)
  4549. {
  4550. WARN_ON(atomic_read(&eb->refs) == 0);
  4551. if (atomic_dec_and_test(&eb->refs)) {
  4552. if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
  4553. struct btrfs_fs_info *fs_info = eb->fs_info;
  4554. spin_unlock(&eb->refs_lock);
  4555. spin_lock(&fs_info->buffer_lock);
  4556. radix_tree_delete(&fs_info->buffer_radix,
  4557. eb->start >> PAGE_SHIFT);
  4558. spin_unlock(&fs_info->buffer_lock);
  4559. } else {
  4560. spin_unlock(&eb->refs_lock);
  4561. }
  4562. /* Should be safe to release our pages at this point */
  4563. btrfs_release_extent_buffer_page(eb);
  4564. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4565. if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
  4566. __free_extent_buffer(eb);
  4567. return 1;
  4568. }
  4569. #endif
  4570. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  4571. return 1;
  4572. }
  4573. spin_unlock(&eb->refs_lock);
  4574. return 0;
  4575. }
  4576. void free_extent_buffer(struct extent_buffer *eb)
  4577. {
  4578. int refs;
  4579. int old;
  4580. if (!eb)
  4581. return;
  4582. while (1) {
  4583. refs = atomic_read(&eb->refs);
  4584. if (refs <= 3)
  4585. break;
  4586. old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
  4587. if (old == refs)
  4588. return;
  4589. }
  4590. spin_lock(&eb->refs_lock);
  4591. if (atomic_read(&eb->refs) == 2 &&
  4592. test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
  4593. atomic_dec(&eb->refs);
  4594. if (atomic_read(&eb->refs) == 2 &&
  4595. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  4596. !extent_buffer_under_io(eb) &&
  4597. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4598. atomic_dec(&eb->refs);
  4599. /*
  4600. * I know this is terrible, but it's temporary until we stop tracking
  4601. * the uptodate bits and such for the extent buffers.
  4602. */
  4603. release_extent_buffer(eb);
  4604. }
  4605. void free_extent_buffer_stale(struct extent_buffer *eb)
  4606. {
  4607. if (!eb)
  4608. return;
  4609. spin_lock(&eb->refs_lock);
  4610. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  4611. if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  4612. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4613. atomic_dec(&eb->refs);
  4614. release_extent_buffer(eb);
  4615. }
  4616. void clear_extent_buffer_dirty(struct extent_buffer *eb)
  4617. {
  4618. unsigned long i;
  4619. unsigned long num_pages;
  4620. struct page *page;
  4621. num_pages = num_extent_pages(eb->start, eb->len);
  4622. for (i = 0; i < num_pages; i++) {
  4623. page = eb->pages[i];
  4624. if (!PageDirty(page))
  4625. continue;
  4626. lock_page(page);
  4627. WARN_ON(!PagePrivate(page));
  4628. clear_page_dirty_for_io(page);
  4629. spin_lock_irq(&page->mapping->tree_lock);
  4630. if (!PageDirty(page)) {
  4631. radix_tree_tag_clear(&page->mapping->page_tree,
  4632. page_index(page),
  4633. PAGECACHE_TAG_DIRTY);
  4634. }
  4635. spin_unlock_irq(&page->mapping->tree_lock);
  4636. ClearPageError(page);
  4637. unlock_page(page);
  4638. }
  4639. WARN_ON(atomic_read(&eb->refs) == 0);
  4640. }
  4641. int set_extent_buffer_dirty(struct extent_buffer *eb)
  4642. {
  4643. unsigned long i;
  4644. unsigned long num_pages;
  4645. int was_dirty = 0;
  4646. check_buffer_tree_ref(eb);
  4647. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  4648. num_pages = num_extent_pages(eb->start, eb->len);
  4649. WARN_ON(atomic_read(&eb->refs) == 0);
  4650. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  4651. for (i = 0; i < num_pages; i++)
  4652. set_page_dirty(eb->pages[i]);
  4653. return was_dirty;
  4654. }
  4655. void clear_extent_buffer_uptodate(struct extent_buffer *eb)
  4656. {
  4657. unsigned long i;
  4658. struct page *page;
  4659. unsigned long num_pages;
  4660. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4661. num_pages = num_extent_pages(eb->start, eb->len);
  4662. for (i = 0; i < num_pages; i++) {
  4663. page = eb->pages[i];
  4664. if (page)
  4665. ClearPageUptodate(page);
  4666. }
  4667. }
  4668. void set_extent_buffer_uptodate(struct extent_buffer *eb)
  4669. {
  4670. unsigned long i;
  4671. struct page *page;
  4672. unsigned long num_pages;
  4673. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4674. num_pages = num_extent_pages(eb->start, eb->len);
  4675. for (i = 0; i < num_pages; i++) {
  4676. page = eb->pages[i];
  4677. SetPageUptodate(page);
  4678. }
  4679. }
  4680. int extent_buffer_uptodate(struct extent_buffer *eb)
  4681. {
  4682. return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4683. }
  4684. int read_extent_buffer_pages(struct extent_io_tree *tree,
  4685. struct extent_buffer *eb, int wait,
  4686. get_extent_t *get_extent, int mirror_num)
  4687. {
  4688. unsigned long i;
  4689. struct page *page;
  4690. int err;
  4691. int ret = 0;
  4692. int locked_pages = 0;
  4693. int all_uptodate = 1;
  4694. unsigned long num_pages;
  4695. unsigned long num_reads = 0;
  4696. struct bio *bio = NULL;
  4697. unsigned long bio_flags = 0;
  4698. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  4699. return 0;
  4700. num_pages = num_extent_pages(eb->start, eb->len);
  4701. for (i = 0; i < num_pages; i++) {
  4702. page = eb->pages[i];
  4703. if (wait == WAIT_NONE) {
  4704. if (!trylock_page(page))
  4705. goto unlock_exit;
  4706. } else {
  4707. lock_page(page);
  4708. }
  4709. locked_pages++;
  4710. }
  4711. /*
  4712. * We need to firstly lock all pages to make sure that
  4713. * the uptodate bit of our pages won't be affected by
  4714. * clear_extent_buffer_uptodate().
  4715. */
  4716. for (i = 0; i < num_pages; i++) {
  4717. page = eb->pages[i];
  4718. if (!PageUptodate(page)) {
  4719. num_reads++;
  4720. all_uptodate = 0;
  4721. }
  4722. }
  4723. if (all_uptodate) {
  4724. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4725. goto unlock_exit;
  4726. }
  4727. clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
  4728. eb->read_mirror = 0;
  4729. atomic_set(&eb->io_pages, num_reads);
  4730. for (i = 0; i < num_pages; i++) {
  4731. page = eb->pages[i];
  4732. if (!PageUptodate(page)) {
  4733. if (ret) {
  4734. atomic_dec(&eb->io_pages);
  4735. unlock_page(page);
  4736. continue;
  4737. }
  4738. ClearPageError(page);
  4739. err = __extent_read_full_page(tree, page,
  4740. get_extent, &bio,
  4741. mirror_num, &bio_flags,
  4742. REQ_META);
  4743. if (err) {
  4744. ret = err;
  4745. /*
  4746. * We use &bio in above __extent_read_full_page,
  4747. * so we ensure that if it returns error, the
  4748. * current page fails to add itself to bio and
  4749. * it's been unlocked.
  4750. *
  4751. * We must dec io_pages by ourselves.
  4752. */
  4753. atomic_dec(&eb->io_pages);
  4754. }
  4755. } else {
  4756. unlock_page(page);
  4757. }
  4758. }
  4759. if (bio) {
  4760. err = submit_one_bio(bio, mirror_num, bio_flags);
  4761. if (err)
  4762. return err;
  4763. }
  4764. if (ret || wait != WAIT_COMPLETE)
  4765. return ret;
  4766. for (i = 0; i < num_pages; i++) {
  4767. page = eb->pages[i];
  4768. wait_on_page_locked(page);
  4769. if (!PageUptodate(page))
  4770. ret = -EIO;
  4771. }
  4772. return ret;
  4773. unlock_exit:
  4774. while (locked_pages > 0) {
  4775. locked_pages--;
  4776. page = eb->pages[locked_pages];
  4777. unlock_page(page);
  4778. }
  4779. return ret;
  4780. }
  4781. void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
  4782. unsigned long start, unsigned long len)
  4783. {
  4784. size_t cur;
  4785. size_t offset;
  4786. struct page *page;
  4787. char *kaddr;
  4788. char *dst = (char *)dstv;
  4789. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4790. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4791. if (start + len > eb->len) {
  4792. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
  4793. eb->start, eb->len, start, len);
  4794. memset(dst, 0, len);
  4795. return;
  4796. }
  4797. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4798. while (len > 0) {
  4799. page = eb->pages[i];
  4800. cur = min(len, (PAGE_SIZE - offset));
  4801. kaddr = page_address(page);
  4802. memcpy(dst, kaddr + offset, cur);
  4803. dst += cur;
  4804. len -= cur;
  4805. offset = 0;
  4806. i++;
  4807. }
  4808. }
  4809. int read_extent_buffer_to_user(const struct extent_buffer *eb,
  4810. void __user *dstv,
  4811. unsigned long start, unsigned long len)
  4812. {
  4813. size_t cur;
  4814. size_t offset;
  4815. struct page *page;
  4816. char *kaddr;
  4817. char __user *dst = (char __user *)dstv;
  4818. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4819. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4820. int ret = 0;
  4821. WARN_ON(start > eb->len);
  4822. WARN_ON(start + len > eb->start + eb->len);
  4823. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4824. while (len > 0) {
  4825. page = eb->pages[i];
  4826. cur = min(len, (PAGE_SIZE - offset));
  4827. kaddr = page_address(page);
  4828. if (copy_to_user(dst, kaddr + offset, cur)) {
  4829. ret = -EFAULT;
  4830. break;
  4831. }
  4832. dst += cur;
  4833. len -= cur;
  4834. offset = 0;
  4835. i++;
  4836. }
  4837. return ret;
  4838. }
  4839. /*
  4840. * return 0 if the item is found within a page.
  4841. * return 1 if the item spans two pages.
  4842. * return -EINVAL otherwise.
  4843. */
  4844. int map_private_extent_buffer(const struct extent_buffer *eb,
  4845. unsigned long start, unsigned long min_len,
  4846. char **map, unsigned long *map_start,
  4847. unsigned long *map_len)
  4848. {
  4849. size_t offset = start & (PAGE_SIZE - 1);
  4850. char *kaddr;
  4851. struct page *p;
  4852. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4853. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4854. unsigned long end_i = (start_offset + start + min_len - 1) >>
  4855. PAGE_SHIFT;
  4856. if (start + min_len > eb->len) {
  4857. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
  4858. eb->start, eb->len, start, min_len);
  4859. return -EINVAL;
  4860. }
  4861. if (i != end_i)
  4862. return 1;
  4863. if (i == 0) {
  4864. offset = start_offset;
  4865. *map_start = 0;
  4866. } else {
  4867. offset = 0;
  4868. *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
  4869. }
  4870. p = eb->pages[i];
  4871. kaddr = page_address(p);
  4872. *map = kaddr + offset;
  4873. *map_len = PAGE_SIZE - offset;
  4874. return 0;
  4875. }
  4876. int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
  4877. unsigned long start, unsigned long len)
  4878. {
  4879. size_t cur;
  4880. size_t offset;
  4881. struct page *page;
  4882. char *kaddr;
  4883. char *ptr = (char *)ptrv;
  4884. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4885. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4886. int ret = 0;
  4887. WARN_ON(start > eb->len);
  4888. WARN_ON(start + len > eb->start + eb->len);
  4889. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4890. while (len > 0) {
  4891. page = eb->pages[i];
  4892. cur = min(len, (PAGE_SIZE - offset));
  4893. kaddr = page_address(page);
  4894. ret = memcmp(ptr, kaddr + offset, cur);
  4895. if (ret)
  4896. break;
  4897. ptr += cur;
  4898. len -= cur;
  4899. offset = 0;
  4900. i++;
  4901. }
  4902. return ret;
  4903. }
  4904. void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
  4905. const void *srcv)
  4906. {
  4907. char *kaddr;
  4908. WARN_ON(!PageUptodate(eb->pages[0]));
  4909. kaddr = page_address(eb->pages[0]);
  4910. memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
  4911. BTRFS_FSID_SIZE);
  4912. }
  4913. void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
  4914. {
  4915. char *kaddr;
  4916. WARN_ON(!PageUptodate(eb->pages[0]));
  4917. kaddr = page_address(eb->pages[0]);
  4918. memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
  4919. BTRFS_FSID_SIZE);
  4920. }
  4921. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  4922. unsigned long start, unsigned long len)
  4923. {
  4924. size_t cur;
  4925. size_t offset;
  4926. struct page *page;
  4927. char *kaddr;
  4928. char *src = (char *)srcv;
  4929. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4930. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4931. WARN_ON(start > eb->len);
  4932. WARN_ON(start + len > eb->start + eb->len);
  4933. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4934. while (len > 0) {
  4935. page = eb->pages[i];
  4936. WARN_ON(!PageUptodate(page));
  4937. cur = min(len, PAGE_SIZE - offset);
  4938. kaddr = page_address(page);
  4939. memcpy(kaddr + offset, src, cur);
  4940. src += cur;
  4941. len -= cur;
  4942. offset = 0;
  4943. i++;
  4944. }
  4945. }
  4946. void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4947. unsigned long len)
  4948. {
  4949. size_t cur;
  4950. size_t offset;
  4951. struct page *page;
  4952. char *kaddr;
  4953. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4954. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4955. WARN_ON(start > eb->len);
  4956. WARN_ON(start + len > eb->start + eb->len);
  4957. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4958. while (len > 0) {
  4959. page = eb->pages[i];
  4960. WARN_ON(!PageUptodate(page));
  4961. cur = min(len, PAGE_SIZE - offset);
  4962. kaddr = page_address(page);
  4963. memset(kaddr + offset, 0, cur);
  4964. len -= cur;
  4965. offset = 0;
  4966. i++;
  4967. }
  4968. }
  4969. void copy_extent_buffer_full(struct extent_buffer *dst,
  4970. struct extent_buffer *src)
  4971. {
  4972. int i;
  4973. unsigned num_pages;
  4974. ASSERT(dst->len == src->len);
  4975. num_pages = num_extent_pages(dst->start, dst->len);
  4976. for (i = 0; i < num_pages; i++)
  4977. copy_page(page_address(dst->pages[i]),
  4978. page_address(src->pages[i]));
  4979. }
  4980. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  4981. unsigned long dst_offset, unsigned long src_offset,
  4982. unsigned long len)
  4983. {
  4984. u64 dst_len = dst->len;
  4985. size_t cur;
  4986. size_t offset;
  4987. struct page *page;
  4988. char *kaddr;
  4989. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  4990. unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
  4991. WARN_ON(src->len != dst_len);
  4992. offset = (start_offset + dst_offset) &
  4993. (PAGE_SIZE - 1);
  4994. while (len > 0) {
  4995. page = dst->pages[i];
  4996. WARN_ON(!PageUptodate(page));
  4997. cur = min(len, (unsigned long)(PAGE_SIZE - offset));
  4998. kaddr = page_address(page);
  4999. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  5000. src_offset += cur;
  5001. len -= cur;
  5002. offset = 0;
  5003. i++;
  5004. }
  5005. }
  5006. void le_bitmap_set(u8 *map, unsigned int start, int len)
  5007. {
  5008. u8 *p = map + BIT_BYTE(start);
  5009. const unsigned int size = start + len;
  5010. int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
  5011. u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
  5012. while (len - bits_to_set >= 0) {
  5013. *p |= mask_to_set;
  5014. len -= bits_to_set;
  5015. bits_to_set = BITS_PER_BYTE;
  5016. mask_to_set = ~0;
  5017. p++;
  5018. }
  5019. if (len) {
  5020. mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
  5021. *p |= mask_to_set;
  5022. }
  5023. }
  5024. void le_bitmap_clear(u8 *map, unsigned int start, int len)
  5025. {
  5026. u8 *p = map + BIT_BYTE(start);
  5027. const unsigned int size = start + len;
  5028. int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
  5029. u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
  5030. while (len - bits_to_clear >= 0) {
  5031. *p &= ~mask_to_clear;
  5032. len -= bits_to_clear;
  5033. bits_to_clear = BITS_PER_BYTE;
  5034. mask_to_clear = ~0;
  5035. p++;
  5036. }
  5037. if (len) {
  5038. mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
  5039. *p &= ~mask_to_clear;
  5040. }
  5041. }
  5042. /*
  5043. * eb_bitmap_offset() - calculate the page and offset of the byte containing the
  5044. * given bit number
  5045. * @eb: the extent buffer
  5046. * @start: offset of the bitmap item in the extent buffer
  5047. * @nr: bit number
  5048. * @page_index: return index of the page in the extent buffer that contains the
  5049. * given bit number
  5050. * @page_offset: return offset into the page given by page_index
  5051. *
  5052. * This helper hides the ugliness of finding the byte in an extent buffer which
  5053. * contains a given bit.
  5054. */
  5055. static inline void eb_bitmap_offset(struct extent_buffer *eb,
  5056. unsigned long start, unsigned long nr,
  5057. unsigned long *page_index,
  5058. size_t *page_offset)
  5059. {
  5060. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  5061. size_t byte_offset = BIT_BYTE(nr);
  5062. size_t offset;
  5063. /*
  5064. * The byte we want is the offset of the extent buffer + the offset of
  5065. * the bitmap item in the extent buffer + the offset of the byte in the
  5066. * bitmap item.
  5067. */
  5068. offset = start_offset + start + byte_offset;
  5069. *page_index = offset >> PAGE_SHIFT;
  5070. *page_offset = offset & (PAGE_SIZE - 1);
  5071. }
  5072. /**
  5073. * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
  5074. * @eb: the extent buffer
  5075. * @start: offset of the bitmap item in the extent buffer
  5076. * @nr: bit number to test
  5077. */
  5078. int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
  5079. unsigned long nr)
  5080. {
  5081. u8 *kaddr;
  5082. struct page *page;
  5083. unsigned long i;
  5084. size_t offset;
  5085. eb_bitmap_offset(eb, start, nr, &i, &offset);
  5086. page = eb->pages[i];
  5087. WARN_ON(!PageUptodate(page));
  5088. kaddr = page_address(page);
  5089. return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
  5090. }
  5091. /**
  5092. * extent_buffer_bitmap_set - set an area of a bitmap
  5093. * @eb: the extent buffer
  5094. * @start: offset of the bitmap item in the extent buffer
  5095. * @pos: bit number of the first bit
  5096. * @len: number of bits to set
  5097. */
  5098. void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
  5099. unsigned long pos, unsigned long len)
  5100. {
  5101. u8 *kaddr;
  5102. struct page *page;
  5103. unsigned long i;
  5104. size_t offset;
  5105. const unsigned int size = pos + len;
  5106. int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
  5107. u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
  5108. eb_bitmap_offset(eb, start, pos, &i, &offset);
  5109. page = eb->pages[i];
  5110. WARN_ON(!PageUptodate(page));
  5111. kaddr = page_address(page);
  5112. while (len >= bits_to_set) {
  5113. kaddr[offset] |= mask_to_set;
  5114. len -= bits_to_set;
  5115. bits_to_set = BITS_PER_BYTE;
  5116. mask_to_set = ~0;
  5117. if (++offset >= PAGE_SIZE && len > 0) {
  5118. offset = 0;
  5119. page = eb->pages[++i];
  5120. WARN_ON(!PageUptodate(page));
  5121. kaddr = page_address(page);
  5122. }
  5123. }
  5124. if (len) {
  5125. mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
  5126. kaddr[offset] |= mask_to_set;
  5127. }
  5128. }
  5129. /**
  5130. * extent_buffer_bitmap_clear - clear an area of a bitmap
  5131. * @eb: the extent buffer
  5132. * @start: offset of the bitmap item in the extent buffer
  5133. * @pos: bit number of the first bit
  5134. * @len: number of bits to clear
  5135. */
  5136. void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
  5137. unsigned long pos, unsigned long len)
  5138. {
  5139. u8 *kaddr;
  5140. struct page *page;
  5141. unsigned long i;
  5142. size_t offset;
  5143. const unsigned int size = pos + len;
  5144. int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
  5145. u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
  5146. eb_bitmap_offset(eb, start, pos, &i, &offset);
  5147. page = eb->pages[i];
  5148. WARN_ON(!PageUptodate(page));
  5149. kaddr = page_address(page);
  5150. while (len >= bits_to_clear) {
  5151. kaddr[offset] &= ~mask_to_clear;
  5152. len -= bits_to_clear;
  5153. bits_to_clear = BITS_PER_BYTE;
  5154. mask_to_clear = ~0;
  5155. if (++offset >= PAGE_SIZE && len > 0) {
  5156. offset = 0;
  5157. page = eb->pages[++i];
  5158. WARN_ON(!PageUptodate(page));
  5159. kaddr = page_address(page);
  5160. }
  5161. }
  5162. if (len) {
  5163. mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
  5164. kaddr[offset] &= ~mask_to_clear;
  5165. }
  5166. }
  5167. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  5168. {
  5169. unsigned long distance = (src > dst) ? src - dst : dst - src;
  5170. return distance < len;
  5171. }
  5172. static void copy_pages(struct page *dst_page, struct page *src_page,
  5173. unsigned long dst_off, unsigned long src_off,
  5174. unsigned long len)
  5175. {
  5176. char *dst_kaddr = page_address(dst_page);
  5177. char *src_kaddr;
  5178. int must_memmove = 0;
  5179. if (dst_page != src_page) {
  5180. src_kaddr = page_address(src_page);
  5181. } else {
  5182. src_kaddr = dst_kaddr;
  5183. if (areas_overlap(src_off, dst_off, len))
  5184. must_memmove = 1;
  5185. }
  5186. if (must_memmove)
  5187. memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
  5188. else
  5189. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  5190. }
  5191. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  5192. unsigned long src_offset, unsigned long len)
  5193. {
  5194. struct btrfs_fs_info *fs_info = dst->fs_info;
  5195. size_t cur;
  5196. size_t dst_off_in_page;
  5197. size_t src_off_in_page;
  5198. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5199. unsigned long dst_i;
  5200. unsigned long src_i;
  5201. if (src_offset + len > dst->len) {
  5202. btrfs_err(fs_info,
  5203. "memmove bogus src_offset %lu move len %lu dst len %lu",
  5204. src_offset, len, dst->len);
  5205. BUG_ON(1);
  5206. }
  5207. if (dst_offset + len > dst->len) {
  5208. btrfs_err(fs_info,
  5209. "memmove bogus dst_offset %lu move len %lu dst len %lu",
  5210. dst_offset, len, dst->len);
  5211. BUG_ON(1);
  5212. }
  5213. while (len > 0) {
  5214. dst_off_in_page = (start_offset + dst_offset) &
  5215. (PAGE_SIZE - 1);
  5216. src_off_in_page = (start_offset + src_offset) &
  5217. (PAGE_SIZE - 1);
  5218. dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
  5219. src_i = (start_offset + src_offset) >> PAGE_SHIFT;
  5220. cur = min(len, (unsigned long)(PAGE_SIZE -
  5221. src_off_in_page));
  5222. cur = min_t(unsigned long, cur,
  5223. (unsigned long)(PAGE_SIZE - dst_off_in_page));
  5224. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  5225. dst_off_in_page, src_off_in_page, cur);
  5226. src_offset += cur;
  5227. dst_offset += cur;
  5228. len -= cur;
  5229. }
  5230. }
  5231. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  5232. unsigned long src_offset, unsigned long len)
  5233. {
  5234. struct btrfs_fs_info *fs_info = dst->fs_info;
  5235. size_t cur;
  5236. size_t dst_off_in_page;
  5237. size_t src_off_in_page;
  5238. unsigned long dst_end = dst_offset + len - 1;
  5239. unsigned long src_end = src_offset + len - 1;
  5240. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5241. unsigned long dst_i;
  5242. unsigned long src_i;
  5243. if (src_offset + len > dst->len) {
  5244. btrfs_err(fs_info,
  5245. "memmove bogus src_offset %lu move len %lu len %lu",
  5246. src_offset, len, dst->len);
  5247. BUG_ON(1);
  5248. }
  5249. if (dst_offset + len > dst->len) {
  5250. btrfs_err(fs_info,
  5251. "memmove bogus dst_offset %lu move len %lu len %lu",
  5252. dst_offset, len, dst->len);
  5253. BUG_ON(1);
  5254. }
  5255. if (dst_offset < src_offset) {
  5256. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  5257. return;
  5258. }
  5259. while (len > 0) {
  5260. dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
  5261. src_i = (start_offset + src_end) >> PAGE_SHIFT;
  5262. dst_off_in_page = (start_offset + dst_end) &
  5263. (PAGE_SIZE - 1);
  5264. src_off_in_page = (start_offset + src_end) &
  5265. (PAGE_SIZE - 1);
  5266. cur = min_t(unsigned long, len, src_off_in_page + 1);
  5267. cur = min(cur, dst_off_in_page + 1);
  5268. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  5269. dst_off_in_page - cur + 1,
  5270. src_off_in_page - cur + 1, cur);
  5271. dst_end -= cur;
  5272. src_end -= cur;
  5273. len -= cur;
  5274. }
  5275. }
  5276. int try_release_extent_buffer(struct page *page)
  5277. {
  5278. struct extent_buffer *eb;
  5279. /*
  5280. * We need to make sure nobody is attaching this page to an eb right
  5281. * now.
  5282. */
  5283. spin_lock(&page->mapping->private_lock);
  5284. if (!PagePrivate(page)) {
  5285. spin_unlock(&page->mapping->private_lock);
  5286. return 1;
  5287. }
  5288. eb = (struct extent_buffer *)page->private;
  5289. BUG_ON(!eb);
  5290. /*
  5291. * This is a little awful but should be ok, we need to make sure that
  5292. * the eb doesn't disappear out from under us while we're looking at
  5293. * this page.
  5294. */
  5295. spin_lock(&eb->refs_lock);
  5296. if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  5297. spin_unlock(&eb->refs_lock);
  5298. spin_unlock(&page->mapping->private_lock);
  5299. return 0;
  5300. }
  5301. spin_unlock(&page->mapping->private_lock);
  5302. /*
  5303. * If tree ref isn't set then we know the ref on this eb is a real ref,
  5304. * so just return, this page will likely be freed soon anyway.
  5305. */
  5306. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  5307. spin_unlock(&eb->refs_lock);
  5308. return 0;
  5309. }
  5310. return release_extent_buffer(eb);
  5311. }