extent_io.c 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049
  1. #include <linux/bitops.h>
  2. #include <linux/slab.h>
  3. #include <linux/bio.h>
  4. #include <linux/mm.h>
  5. #include <linux/pagemap.h>
  6. #include <linux/page-flags.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/swap.h>
  10. #include <linux/writeback.h>
  11. #include <linux/pagevec.h>
  12. #include <linux/prefetch.h>
  13. #include <linux/cleancache.h>
  14. #include "extent_io.h"
  15. #include "extent_map.h"
  16. #include "ctree.h"
  17. #include "btrfs_inode.h"
  18. #include "volumes.h"
  19. #include "check-integrity.h"
  20. #include "locking.h"
  21. #include "rcu-string.h"
  22. #include "backref.h"
  23. #include "transaction.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. unsigned long bio_flags;
  99. /* tells writepage not to lock the state bits for this range
  100. * it still does the unlocking
  101. */
  102. unsigned int extent_locked:1;
  103. /* tells the submit_bio code to use REQ_SYNC */
  104. unsigned int sync_io:1;
  105. };
  106. static void add_extent_changeset(struct extent_state *state, unsigned bits,
  107. struct extent_changeset *changeset,
  108. int set)
  109. {
  110. int ret;
  111. if (!changeset)
  112. return;
  113. if (set && (state->state & bits) == bits)
  114. return;
  115. if (!set && (state->state & bits) == 0)
  116. return;
  117. changeset->bytes_changed += state->end - state->start + 1;
  118. ret = ulist_add(&changeset->range_changed, state->start, state->end,
  119. GFP_ATOMIC);
  120. /* ENOMEM */
  121. BUG_ON(ret < 0);
  122. }
  123. static noinline void flush_write_bio(void *data);
  124. static inline struct btrfs_fs_info *
  125. tree_fs_info(struct extent_io_tree *tree)
  126. {
  127. if (tree->ops)
  128. return tree->ops->tree_fs_info(tree->private_data);
  129. return NULL;
  130. }
  131. int __init extent_io_init(void)
  132. {
  133. extent_state_cache = kmem_cache_create("btrfs_extent_state",
  134. sizeof(struct extent_state), 0,
  135. SLAB_MEM_SPREAD, NULL);
  136. if (!extent_state_cache)
  137. return -ENOMEM;
  138. extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
  139. sizeof(struct extent_buffer), 0,
  140. SLAB_MEM_SPREAD, NULL);
  141. if (!extent_buffer_cache)
  142. goto free_state_cache;
  143. btrfs_bioset = bioset_create(BIO_POOL_SIZE,
  144. offsetof(struct btrfs_io_bio, bio));
  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(GFP_NOFS, 1);
  1782. if (!bio)
  1783. return -EIO;
  1784. bio->bi_iter.bi_size = 0;
  1785. map_length = length;
  1786. /*
  1787. * Avoid races with device replace and make sure our bbio has devices
  1788. * associated to its stripes that don't go away while we are doing the
  1789. * read repair operation.
  1790. */
  1791. btrfs_bio_counter_inc_blocked(fs_info);
  1792. if (btrfs_is_parity_mirror(fs_info, logical, length, mirror_num)) {
  1793. /*
  1794. * Note that we don't use BTRFS_MAP_WRITE because it's supposed
  1795. * to update all raid stripes, but here we just want to correct
  1796. * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
  1797. * stripe's dev and sector.
  1798. */
  1799. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
  1800. &map_length, &bbio, 0);
  1801. if (ret) {
  1802. btrfs_bio_counter_dec(fs_info);
  1803. bio_put(bio);
  1804. return -EIO;
  1805. }
  1806. ASSERT(bbio->mirror_num == 1);
  1807. } else {
  1808. ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
  1809. &map_length, &bbio, mirror_num);
  1810. if (ret) {
  1811. btrfs_bio_counter_dec(fs_info);
  1812. bio_put(bio);
  1813. return -EIO;
  1814. }
  1815. BUG_ON(mirror_num != bbio->mirror_num);
  1816. }
  1817. sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
  1818. bio->bi_iter.bi_sector = sector;
  1819. dev = bbio->stripes[bbio->mirror_num - 1].dev;
  1820. btrfs_put_bbio(bbio);
  1821. if (!dev || !dev->bdev || !dev->writeable) {
  1822. btrfs_bio_counter_dec(fs_info);
  1823. bio_put(bio);
  1824. return -EIO;
  1825. }
  1826. bio->bi_bdev = dev->bdev;
  1827. bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
  1828. bio_add_page(bio, page, length, pg_offset);
  1829. if (btrfsic_submit_bio_wait(bio)) {
  1830. /* try to remap that extent elsewhere? */
  1831. btrfs_bio_counter_dec(fs_info);
  1832. bio_put(bio);
  1833. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  1834. return -EIO;
  1835. }
  1836. btrfs_info_rl_in_rcu(fs_info,
  1837. "read error corrected: ino %llu off %llu (dev %s sector %llu)",
  1838. ino, start,
  1839. rcu_str_deref(dev->name), sector);
  1840. btrfs_bio_counter_dec(fs_info);
  1841. bio_put(bio);
  1842. return 0;
  1843. }
  1844. int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
  1845. struct extent_buffer *eb, int mirror_num)
  1846. {
  1847. u64 start = eb->start;
  1848. unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
  1849. int ret = 0;
  1850. if (fs_info->sb->s_flags & MS_RDONLY)
  1851. return -EROFS;
  1852. for (i = 0; i < num_pages; i++) {
  1853. struct page *p = eb->pages[i];
  1854. ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
  1855. start - page_offset(p), mirror_num);
  1856. if (ret)
  1857. break;
  1858. start += PAGE_SIZE;
  1859. }
  1860. return ret;
  1861. }
  1862. /*
  1863. * each time an IO finishes, we do a fast check in the IO failure tree
  1864. * to see if we need to process or clean up an io_failure_record
  1865. */
  1866. int clean_io_failure(struct btrfs_fs_info *fs_info,
  1867. struct extent_io_tree *failure_tree,
  1868. struct extent_io_tree *io_tree, u64 start,
  1869. struct page *page, u64 ino, unsigned int pg_offset)
  1870. {
  1871. u64 private;
  1872. struct io_failure_record *failrec;
  1873. struct extent_state *state;
  1874. int num_copies;
  1875. int ret;
  1876. private = 0;
  1877. ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
  1878. EXTENT_DIRTY, 0);
  1879. if (!ret)
  1880. return 0;
  1881. ret = get_state_failrec(failure_tree, start, &failrec);
  1882. if (ret)
  1883. return 0;
  1884. BUG_ON(!failrec->this_mirror);
  1885. if (failrec->in_validation) {
  1886. /* there was no real error, just free the record */
  1887. btrfs_debug(fs_info,
  1888. "clean_io_failure: freeing dummy error at %llu",
  1889. failrec->start);
  1890. goto out;
  1891. }
  1892. if (fs_info->sb->s_flags & MS_RDONLY)
  1893. goto out;
  1894. spin_lock(&io_tree->lock);
  1895. state = find_first_extent_bit_state(io_tree,
  1896. failrec->start,
  1897. EXTENT_LOCKED);
  1898. spin_unlock(&io_tree->lock);
  1899. if (state && state->start <= failrec->start &&
  1900. state->end >= failrec->start + failrec->len - 1) {
  1901. num_copies = btrfs_num_copies(fs_info, failrec->logical,
  1902. failrec->len);
  1903. if (num_copies > 1) {
  1904. repair_io_failure(fs_info, ino, start, failrec->len,
  1905. failrec->logical, page, pg_offset,
  1906. failrec->failed_mirror);
  1907. }
  1908. }
  1909. out:
  1910. free_io_failure(failure_tree, io_tree, failrec);
  1911. return 0;
  1912. }
  1913. /*
  1914. * Can be called when
  1915. * - hold extent lock
  1916. * - under ordered extent
  1917. * - the inode is freeing
  1918. */
  1919. void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
  1920. {
  1921. struct extent_io_tree *failure_tree = &inode->io_failure_tree;
  1922. struct io_failure_record *failrec;
  1923. struct extent_state *state, *next;
  1924. if (RB_EMPTY_ROOT(&failure_tree->state))
  1925. return;
  1926. spin_lock(&failure_tree->lock);
  1927. state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
  1928. while (state) {
  1929. if (state->start > end)
  1930. break;
  1931. ASSERT(state->end <= end);
  1932. next = next_state(state);
  1933. failrec = state->failrec;
  1934. free_extent_state(state);
  1935. kfree(failrec);
  1936. state = next;
  1937. }
  1938. spin_unlock(&failure_tree->lock);
  1939. }
  1940. int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
  1941. struct io_failure_record **failrec_ret)
  1942. {
  1943. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1944. struct io_failure_record *failrec;
  1945. struct extent_map *em;
  1946. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1947. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1948. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1949. int ret;
  1950. u64 logical;
  1951. ret = get_state_failrec(failure_tree, start, &failrec);
  1952. if (ret) {
  1953. failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
  1954. if (!failrec)
  1955. return -ENOMEM;
  1956. failrec->start = start;
  1957. failrec->len = end - start + 1;
  1958. failrec->this_mirror = 0;
  1959. failrec->bio_flags = 0;
  1960. failrec->in_validation = 0;
  1961. read_lock(&em_tree->lock);
  1962. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1963. if (!em) {
  1964. read_unlock(&em_tree->lock);
  1965. kfree(failrec);
  1966. return -EIO;
  1967. }
  1968. if (em->start > start || em->start + em->len <= start) {
  1969. free_extent_map(em);
  1970. em = NULL;
  1971. }
  1972. read_unlock(&em_tree->lock);
  1973. if (!em) {
  1974. kfree(failrec);
  1975. return -EIO;
  1976. }
  1977. logical = start - em->start;
  1978. logical = em->block_start + logical;
  1979. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1980. logical = em->block_start;
  1981. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1982. extent_set_compress_type(&failrec->bio_flags,
  1983. em->compress_type);
  1984. }
  1985. btrfs_debug(fs_info,
  1986. "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
  1987. logical, start, failrec->len);
  1988. failrec->logical = logical;
  1989. free_extent_map(em);
  1990. /* set the bits in the private failure tree */
  1991. ret = set_extent_bits(failure_tree, start, end,
  1992. EXTENT_LOCKED | EXTENT_DIRTY);
  1993. if (ret >= 0)
  1994. ret = set_state_failrec(failure_tree, start, failrec);
  1995. /* set the bits in the inode's tree */
  1996. if (ret >= 0)
  1997. ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
  1998. if (ret < 0) {
  1999. kfree(failrec);
  2000. return ret;
  2001. }
  2002. } else {
  2003. btrfs_debug(fs_info,
  2004. "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
  2005. failrec->logical, failrec->start, failrec->len,
  2006. failrec->in_validation);
  2007. /*
  2008. * when data can be on disk more than twice, add to failrec here
  2009. * (e.g. with a list for failed_mirror) to make
  2010. * clean_io_failure() clean all those errors at once.
  2011. */
  2012. }
  2013. *failrec_ret = failrec;
  2014. return 0;
  2015. }
  2016. int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
  2017. struct io_failure_record *failrec, int failed_mirror)
  2018. {
  2019. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2020. int num_copies;
  2021. num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
  2022. if (num_copies == 1) {
  2023. /*
  2024. * we only have a single copy of the data, so don't bother with
  2025. * all the retry and error correction code that follows. no
  2026. * matter what the error is, it is very likely to persist.
  2027. */
  2028. btrfs_debug(fs_info,
  2029. "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
  2030. num_copies, failrec->this_mirror, failed_mirror);
  2031. return 0;
  2032. }
  2033. /*
  2034. * there are two premises:
  2035. * a) deliver good data to the caller
  2036. * b) correct the bad sectors on disk
  2037. */
  2038. if (failed_bio->bi_vcnt > 1) {
  2039. /*
  2040. * to fulfill b), we need to know the exact failing sectors, as
  2041. * we don't want to rewrite any more than the failed ones. thus,
  2042. * we need separate read requests for the failed bio
  2043. *
  2044. * if the following BUG_ON triggers, our validation request got
  2045. * merged. we need separate requests for our algorithm to work.
  2046. */
  2047. BUG_ON(failrec->in_validation);
  2048. failrec->in_validation = 1;
  2049. failrec->this_mirror = failed_mirror;
  2050. } else {
  2051. /*
  2052. * we're ready to fulfill a) and b) alongside. get a good copy
  2053. * of the failed sector and if we succeed, we have setup
  2054. * everything for repair_io_failure to do the rest for us.
  2055. */
  2056. if (failrec->in_validation) {
  2057. BUG_ON(failrec->this_mirror != failed_mirror);
  2058. failrec->in_validation = 0;
  2059. failrec->this_mirror = 0;
  2060. }
  2061. failrec->failed_mirror = failed_mirror;
  2062. failrec->this_mirror++;
  2063. if (failrec->this_mirror == failed_mirror)
  2064. failrec->this_mirror++;
  2065. }
  2066. if (failrec->this_mirror > num_copies) {
  2067. btrfs_debug(fs_info,
  2068. "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
  2069. num_copies, failrec->this_mirror, failed_mirror);
  2070. return 0;
  2071. }
  2072. return 1;
  2073. }
  2074. struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
  2075. struct io_failure_record *failrec,
  2076. struct page *page, int pg_offset, int icsum,
  2077. bio_end_io_t *endio_func, void *data)
  2078. {
  2079. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2080. struct bio *bio;
  2081. struct btrfs_io_bio *btrfs_failed_bio;
  2082. struct btrfs_io_bio *btrfs_bio;
  2083. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  2084. if (!bio)
  2085. return NULL;
  2086. bio->bi_end_io = endio_func;
  2087. bio->bi_iter.bi_sector = failrec->logical >> 9;
  2088. bio->bi_bdev = fs_info->fs_devices->latest_bdev;
  2089. bio->bi_iter.bi_size = 0;
  2090. bio->bi_private = data;
  2091. btrfs_failed_bio = btrfs_io_bio(failed_bio);
  2092. if (btrfs_failed_bio->csum) {
  2093. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  2094. btrfs_bio = btrfs_io_bio(bio);
  2095. btrfs_bio->csum = btrfs_bio->csum_inline;
  2096. icsum *= csum_size;
  2097. memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
  2098. csum_size);
  2099. }
  2100. bio_add_page(bio, page, failrec->len, pg_offset);
  2101. return bio;
  2102. }
  2103. /*
  2104. * this is a generic handler for readpage errors (default
  2105. * readpage_io_failed_hook). if other copies exist, read those and write back
  2106. * good data to the failed position. does not investigate in remapping the
  2107. * failed extent elsewhere, hoping the device will be smart enough to do this as
  2108. * needed
  2109. */
  2110. static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
  2111. struct page *page, u64 start, u64 end,
  2112. int failed_mirror)
  2113. {
  2114. struct io_failure_record *failrec;
  2115. struct inode *inode = page->mapping->host;
  2116. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  2117. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  2118. struct bio *bio;
  2119. int read_mode = 0;
  2120. int ret;
  2121. BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
  2122. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  2123. if (ret)
  2124. return ret;
  2125. ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
  2126. if (!ret) {
  2127. free_io_failure(failure_tree, tree, failrec);
  2128. return -EIO;
  2129. }
  2130. if (failed_bio->bi_vcnt > 1)
  2131. read_mode |= REQ_FAILFAST_DEV;
  2132. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2133. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  2134. start - page_offset(page),
  2135. (int)phy_offset, failed_bio->bi_end_io,
  2136. NULL);
  2137. if (!bio) {
  2138. free_io_failure(failure_tree, tree, failrec);
  2139. return -EIO;
  2140. }
  2141. bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
  2142. btrfs_debug(btrfs_sb(inode->i_sb),
  2143. "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
  2144. read_mode, failrec->this_mirror, failrec->in_validation);
  2145. ret = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
  2146. failrec->bio_flags, 0);
  2147. if (ret) {
  2148. free_io_failure(failure_tree, tree, failrec);
  2149. bio_put(bio);
  2150. }
  2151. return ret;
  2152. }
  2153. /* lots and lots of room for performance fixes in the end_bio funcs */
  2154. void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
  2155. {
  2156. int uptodate = (err == 0);
  2157. struct extent_io_tree *tree;
  2158. int ret = 0;
  2159. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2160. if (tree->ops && tree->ops->writepage_end_io_hook)
  2161. tree->ops->writepage_end_io_hook(page, start, end, NULL,
  2162. uptodate);
  2163. if (!uptodate) {
  2164. ClearPageUptodate(page);
  2165. SetPageError(page);
  2166. ret = err < 0 ? err : -EIO;
  2167. mapping_set_error(page->mapping, ret);
  2168. }
  2169. }
  2170. /*
  2171. * after a writepage IO is done, we need to:
  2172. * clear the uptodate bits on error
  2173. * clear the writeback bits in the extent tree for this IO
  2174. * end_page_writeback if the page has no more pending IO
  2175. *
  2176. * Scheduling is not allowed, so the extent state tree is expected
  2177. * to have one and only one object corresponding to this IO.
  2178. */
  2179. static void end_bio_extent_writepage(struct bio *bio)
  2180. {
  2181. struct bio_vec *bvec;
  2182. u64 start;
  2183. u64 end;
  2184. int i;
  2185. bio_for_each_segment_all(bvec, bio, i) {
  2186. struct page *page = bvec->bv_page;
  2187. struct inode *inode = page->mapping->host;
  2188. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2189. /* We always issue full-page reads, but if some block
  2190. * in a page fails to read, blk_update_request() will
  2191. * advance bv_offset and adjust bv_len to compensate.
  2192. * Print a warning for nonzero offsets, and an error
  2193. * if they don't add up to a full page. */
  2194. if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
  2195. if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
  2196. btrfs_err(fs_info,
  2197. "partial page write in btrfs with offset %u and length %u",
  2198. bvec->bv_offset, bvec->bv_len);
  2199. else
  2200. btrfs_info(fs_info,
  2201. "incomplete page write in btrfs with offset %u and length %u",
  2202. bvec->bv_offset, bvec->bv_len);
  2203. }
  2204. start = page_offset(page);
  2205. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2206. end_extent_writepage(page, bio->bi_error, start, end);
  2207. end_page_writeback(page);
  2208. }
  2209. bio_put(bio);
  2210. }
  2211. static void
  2212. endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
  2213. int uptodate)
  2214. {
  2215. struct extent_state *cached = NULL;
  2216. u64 end = start + len - 1;
  2217. if (uptodate && tree->track_uptodate)
  2218. set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
  2219. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2220. }
  2221. /*
  2222. * after a readpage IO is done, we need to:
  2223. * clear the uptodate bits on error
  2224. * set the uptodate bits if things worked
  2225. * set the page up to date if all extents in the tree are uptodate
  2226. * clear the lock bit in the extent tree
  2227. * unlock the page if there are no other extents locked for it
  2228. *
  2229. * Scheduling is not allowed, so the extent state tree is expected
  2230. * to have one and only one object corresponding to this IO.
  2231. */
  2232. static void end_bio_extent_readpage(struct bio *bio)
  2233. {
  2234. struct bio_vec *bvec;
  2235. int uptodate = !bio->bi_error;
  2236. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  2237. struct extent_io_tree *tree, *failure_tree;
  2238. u64 offset = 0;
  2239. u64 start;
  2240. u64 end;
  2241. u64 len;
  2242. u64 extent_start = 0;
  2243. u64 extent_len = 0;
  2244. int mirror;
  2245. int ret;
  2246. int i;
  2247. bio_for_each_segment_all(bvec, bio, i) {
  2248. struct page *page = bvec->bv_page;
  2249. struct inode *inode = page->mapping->host;
  2250. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2251. btrfs_debug(fs_info,
  2252. "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
  2253. (u64)bio->bi_iter.bi_sector, bio->bi_error,
  2254. io_bio->mirror_num);
  2255. tree = &BTRFS_I(inode)->io_tree;
  2256. failure_tree = &BTRFS_I(inode)->io_failure_tree;
  2257. /* We always issue full-page reads, but if some block
  2258. * in a page fails to read, blk_update_request() will
  2259. * advance bv_offset and adjust bv_len to compensate.
  2260. * Print a warning for nonzero offsets, and an error
  2261. * if they don't add up to a full page. */
  2262. if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
  2263. if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
  2264. btrfs_err(fs_info,
  2265. "partial page read in btrfs with offset %u and length %u",
  2266. bvec->bv_offset, bvec->bv_len);
  2267. else
  2268. btrfs_info(fs_info,
  2269. "incomplete page read in btrfs with offset %u and length %u",
  2270. bvec->bv_offset, bvec->bv_len);
  2271. }
  2272. start = page_offset(page);
  2273. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2274. len = bvec->bv_len;
  2275. mirror = io_bio->mirror_num;
  2276. if (likely(uptodate && tree->ops)) {
  2277. ret = tree->ops->readpage_end_io_hook(io_bio, offset,
  2278. page, start, end,
  2279. mirror);
  2280. if (ret)
  2281. uptodate = 0;
  2282. else
  2283. clean_io_failure(BTRFS_I(inode)->root->fs_info,
  2284. failure_tree, tree, start,
  2285. page,
  2286. btrfs_ino(BTRFS_I(inode)), 0);
  2287. }
  2288. if (likely(uptodate))
  2289. goto readpage_ok;
  2290. if (tree->ops) {
  2291. ret = tree->ops->readpage_io_failed_hook(page, mirror);
  2292. if (ret == -EAGAIN) {
  2293. /*
  2294. * Data inode's readpage_io_failed_hook() always
  2295. * returns -EAGAIN.
  2296. *
  2297. * The generic bio_readpage_error handles errors
  2298. * the following way: If possible, new read
  2299. * requests are created and submitted and will
  2300. * end up in end_bio_extent_readpage as well (if
  2301. * we're lucky, not in the !uptodate case). In
  2302. * that case it returns 0 and we just go on with
  2303. * the next page in our bio. If it can't handle
  2304. * the error it will return -EIO and we remain
  2305. * responsible for that page.
  2306. */
  2307. ret = bio_readpage_error(bio, offset, page,
  2308. start, end, mirror);
  2309. if (ret == 0) {
  2310. uptodate = !bio->bi_error;
  2311. offset += len;
  2312. continue;
  2313. }
  2314. }
  2315. /*
  2316. * metadata's readpage_io_failed_hook() always returns
  2317. * -EIO and fixes nothing. -EIO is also returned if
  2318. * data inode error could not be fixed.
  2319. */
  2320. ASSERT(ret == -EIO);
  2321. }
  2322. readpage_ok:
  2323. if (likely(uptodate)) {
  2324. loff_t i_size = i_size_read(inode);
  2325. pgoff_t end_index = i_size >> PAGE_SHIFT;
  2326. unsigned off;
  2327. /* Zero out the end if this page straddles i_size */
  2328. off = i_size & (PAGE_SIZE-1);
  2329. if (page->index == end_index && off)
  2330. zero_user_segment(page, off, PAGE_SIZE);
  2331. SetPageUptodate(page);
  2332. } else {
  2333. ClearPageUptodate(page);
  2334. SetPageError(page);
  2335. }
  2336. unlock_page(page);
  2337. offset += len;
  2338. if (unlikely(!uptodate)) {
  2339. if (extent_len) {
  2340. endio_readpage_release_extent(tree,
  2341. extent_start,
  2342. extent_len, 1);
  2343. extent_start = 0;
  2344. extent_len = 0;
  2345. }
  2346. endio_readpage_release_extent(tree, start,
  2347. end - start + 1, 0);
  2348. } else if (!extent_len) {
  2349. extent_start = start;
  2350. extent_len = end + 1 - start;
  2351. } else if (extent_start + extent_len == start) {
  2352. extent_len += end + 1 - start;
  2353. } else {
  2354. endio_readpage_release_extent(tree, extent_start,
  2355. extent_len, uptodate);
  2356. extent_start = start;
  2357. extent_len = end + 1 - start;
  2358. }
  2359. }
  2360. if (extent_len)
  2361. endio_readpage_release_extent(tree, extent_start, extent_len,
  2362. uptodate);
  2363. if (io_bio->end_io)
  2364. io_bio->end_io(io_bio, bio->bi_error);
  2365. bio_put(bio);
  2366. }
  2367. /*
  2368. * this allocates from the btrfs_bioset. We're returning a bio right now
  2369. * but you can call btrfs_io_bio for the appropriate container_of magic
  2370. */
  2371. struct bio *
  2372. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2373. gfp_t gfp_flags)
  2374. {
  2375. struct btrfs_io_bio *btrfs_bio;
  2376. struct bio *bio;
  2377. bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
  2378. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2379. while (!bio && (nr_vecs /= 2)) {
  2380. bio = bio_alloc_bioset(gfp_flags,
  2381. nr_vecs, btrfs_bioset);
  2382. }
  2383. }
  2384. if (bio) {
  2385. bio->bi_bdev = bdev;
  2386. bio->bi_iter.bi_sector = first_sector;
  2387. btrfs_bio = btrfs_io_bio(bio);
  2388. btrfs_bio->csum = NULL;
  2389. btrfs_bio->csum_allocated = NULL;
  2390. btrfs_bio->end_io = NULL;
  2391. }
  2392. return bio;
  2393. }
  2394. struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
  2395. {
  2396. struct btrfs_io_bio *btrfs_bio;
  2397. struct bio *new;
  2398. new = bio_clone_fast(bio, gfp_mask, btrfs_bioset);
  2399. if (new) {
  2400. btrfs_bio = btrfs_io_bio(new);
  2401. btrfs_bio->csum = NULL;
  2402. btrfs_bio->csum_allocated = NULL;
  2403. btrfs_bio->end_io = NULL;
  2404. btrfs_bio->iter = bio->bi_iter;
  2405. }
  2406. return new;
  2407. }
  2408. /* this also allocates from the btrfs_bioset */
  2409. struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
  2410. {
  2411. struct btrfs_io_bio *btrfs_bio;
  2412. struct bio *bio;
  2413. bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
  2414. if (bio) {
  2415. btrfs_bio = btrfs_io_bio(bio);
  2416. btrfs_bio->csum = NULL;
  2417. btrfs_bio->csum_allocated = NULL;
  2418. btrfs_bio->end_io = NULL;
  2419. }
  2420. return bio;
  2421. }
  2422. struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
  2423. {
  2424. struct bio *bio;
  2425. struct btrfs_io_bio *btrfs_bio;
  2426. /* this will never fail when it's backed by a bioset */
  2427. bio = bio_clone_fast(orig, GFP_NOFS, btrfs_bioset);
  2428. ASSERT(bio);
  2429. btrfs_bio = btrfs_io_bio(bio);
  2430. btrfs_bio->csum = NULL;
  2431. btrfs_bio->csum_allocated = NULL;
  2432. btrfs_bio->end_io = NULL;
  2433. bio_trim(bio, offset >> 9, size >> 9);
  2434. btrfs_bio->iter = bio->bi_iter;
  2435. return bio;
  2436. }
  2437. static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
  2438. unsigned long bio_flags)
  2439. {
  2440. int ret = 0;
  2441. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2442. struct page *page = bvec->bv_page;
  2443. struct extent_io_tree *tree = bio->bi_private;
  2444. u64 start;
  2445. start = page_offset(page) + bvec->bv_offset;
  2446. bio->bi_private = NULL;
  2447. bio_get(bio);
  2448. if (tree->ops)
  2449. ret = tree->ops->submit_bio_hook(tree->private_data, bio,
  2450. mirror_num, bio_flags, start);
  2451. else
  2452. btrfsic_submit_bio(bio);
  2453. bio_put(bio);
  2454. return ret;
  2455. }
  2456. static int merge_bio(struct extent_io_tree *tree, struct page *page,
  2457. unsigned long offset, size_t size, struct bio *bio,
  2458. unsigned long bio_flags)
  2459. {
  2460. int ret = 0;
  2461. if (tree->ops)
  2462. ret = tree->ops->merge_bio_hook(page, offset, size, bio,
  2463. bio_flags);
  2464. return ret;
  2465. }
  2466. static int submit_extent_page(int op, int op_flags, struct extent_io_tree *tree,
  2467. struct writeback_control *wbc,
  2468. struct page *page, sector_t sector,
  2469. size_t size, unsigned long offset,
  2470. struct block_device *bdev,
  2471. struct bio **bio_ret,
  2472. bio_end_io_t end_io_func,
  2473. int mirror_num,
  2474. unsigned long prev_bio_flags,
  2475. unsigned long bio_flags,
  2476. bool force_bio_submit)
  2477. {
  2478. int ret = 0;
  2479. struct bio *bio;
  2480. int contig = 0;
  2481. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2482. size_t page_size = min_t(size_t, size, PAGE_SIZE);
  2483. if (bio_ret && *bio_ret) {
  2484. bio = *bio_ret;
  2485. if (old_compressed)
  2486. contig = bio->bi_iter.bi_sector == sector;
  2487. else
  2488. contig = bio_end_sector(bio) == sector;
  2489. if (prev_bio_flags != bio_flags || !contig ||
  2490. force_bio_submit ||
  2491. merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
  2492. bio_add_page(bio, page, page_size, offset) < page_size) {
  2493. ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
  2494. if (ret < 0) {
  2495. *bio_ret = NULL;
  2496. return ret;
  2497. }
  2498. bio = NULL;
  2499. } else {
  2500. if (wbc)
  2501. wbc_account_io(wbc, page, page_size);
  2502. return 0;
  2503. }
  2504. }
  2505. bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES,
  2506. GFP_NOFS | __GFP_HIGH);
  2507. if (!bio)
  2508. return -ENOMEM;
  2509. bio_add_page(bio, page, page_size, offset);
  2510. bio->bi_end_io = end_io_func;
  2511. bio->bi_private = tree;
  2512. bio_set_op_attrs(bio, op, op_flags);
  2513. if (wbc) {
  2514. wbc_init_bio(wbc, bio);
  2515. wbc_account_io(wbc, page, page_size);
  2516. }
  2517. if (bio_ret)
  2518. *bio_ret = bio;
  2519. else
  2520. ret = submit_one_bio(bio, mirror_num, bio_flags);
  2521. return ret;
  2522. }
  2523. static void attach_extent_buffer_page(struct extent_buffer *eb,
  2524. struct page *page)
  2525. {
  2526. if (!PagePrivate(page)) {
  2527. SetPagePrivate(page);
  2528. get_page(page);
  2529. set_page_private(page, (unsigned long)eb);
  2530. } else {
  2531. WARN_ON(page->private != (unsigned long)eb);
  2532. }
  2533. }
  2534. void set_page_extent_mapped(struct page *page)
  2535. {
  2536. if (!PagePrivate(page)) {
  2537. SetPagePrivate(page);
  2538. get_page(page);
  2539. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2540. }
  2541. }
  2542. static struct extent_map *
  2543. __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
  2544. u64 start, u64 len, get_extent_t *get_extent,
  2545. struct extent_map **em_cached)
  2546. {
  2547. struct extent_map *em;
  2548. if (em_cached && *em_cached) {
  2549. em = *em_cached;
  2550. if (extent_map_in_tree(em) && start >= em->start &&
  2551. start < extent_map_end(em)) {
  2552. refcount_inc(&em->refs);
  2553. return em;
  2554. }
  2555. free_extent_map(em);
  2556. *em_cached = NULL;
  2557. }
  2558. em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
  2559. if (em_cached && !IS_ERR_OR_NULL(em)) {
  2560. BUG_ON(*em_cached);
  2561. refcount_inc(&em->refs);
  2562. *em_cached = em;
  2563. }
  2564. return em;
  2565. }
  2566. /*
  2567. * basic readpage implementation. Locked extent state structs are inserted
  2568. * into the tree that are removed when the IO is done (by the end_io
  2569. * handlers)
  2570. * XXX JDM: This needs looking at to ensure proper page locking
  2571. * return 0 on success, otherwise return error
  2572. */
  2573. static int __do_readpage(struct extent_io_tree *tree,
  2574. struct page *page,
  2575. get_extent_t *get_extent,
  2576. struct extent_map **em_cached,
  2577. struct bio **bio, int mirror_num,
  2578. unsigned long *bio_flags, int read_flags,
  2579. u64 *prev_em_start)
  2580. {
  2581. struct inode *inode = page->mapping->host;
  2582. u64 start = page_offset(page);
  2583. u64 page_end = start + PAGE_SIZE - 1;
  2584. u64 end;
  2585. u64 cur = start;
  2586. u64 extent_offset;
  2587. u64 last_byte = i_size_read(inode);
  2588. u64 block_start;
  2589. u64 cur_end;
  2590. sector_t sector;
  2591. struct extent_map *em;
  2592. struct block_device *bdev;
  2593. int ret = 0;
  2594. int nr = 0;
  2595. size_t pg_offset = 0;
  2596. size_t iosize;
  2597. size_t disk_io_size;
  2598. size_t blocksize = inode->i_sb->s_blocksize;
  2599. unsigned long this_bio_flag = 0;
  2600. set_page_extent_mapped(page);
  2601. end = page_end;
  2602. if (!PageUptodate(page)) {
  2603. if (cleancache_get_page(page) == 0) {
  2604. BUG_ON(blocksize != PAGE_SIZE);
  2605. unlock_extent(tree, start, end);
  2606. goto out;
  2607. }
  2608. }
  2609. if (page->index == last_byte >> PAGE_SHIFT) {
  2610. char *userpage;
  2611. size_t zero_offset = last_byte & (PAGE_SIZE - 1);
  2612. if (zero_offset) {
  2613. iosize = PAGE_SIZE - zero_offset;
  2614. userpage = kmap_atomic(page);
  2615. memset(userpage + zero_offset, 0, iosize);
  2616. flush_dcache_page(page);
  2617. kunmap_atomic(userpage);
  2618. }
  2619. }
  2620. while (cur <= end) {
  2621. bool force_bio_submit = false;
  2622. if (cur >= last_byte) {
  2623. char *userpage;
  2624. struct extent_state *cached = NULL;
  2625. iosize = PAGE_SIZE - pg_offset;
  2626. userpage = kmap_atomic(page);
  2627. memset(userpage + pg_offset, 0, iosize);
  2628. flush_dcache_page(page);
  2629. kunmap_atomic(userpage);
  2630. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2631. &cached, GFP_NOFS);
  2632. unlock_extent_cached(tree, cur,
  2633. cur + iosize - 1,
  2634. &cached, GFP_NOFS);
  2635. break;
  2636. }
  2637. em = __get_extent_map(inode, page, pg_offset, cur,
  2638. end - cur + 1, get_extent, em_cached);
  2639. if (IS_ERR_OR_NULL(em)) {
  2640. SetPageError(page);
  2641. unlock_extent(tree, cur, end);
  2642. break;
  2643. }
  2644. extent_offset = cur - em->start;
  2645. BUG_ON(extent_map_end(em) <= cur);
  2646. BUG_ON(end < cur);
  2647. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2648. this_bio_flag |= EXTENT_BIO_COMPRESSED;
  2649. extent_set_compress_type(&this_bio_flag,
  2650. em->compress_type);
  2651. }
  2652. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2653. cur_end = min(extent_map_end(em) - 1, end);
  2654. iosize = ALIGN(iosize, blocksize);
  2655. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2656. disk_io_size = em->block_len;
  2657. sector = em->block_start >> 9;
  2658. } else {
  2659. sector = (em->block_start + extent_offset) >> 9;
  2660. disk_io_size = iosize;
  2661. }
  2662. bdev = em->bdev;
  2663. block_start = em->block_start;
  2664. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2665. block_start = EXTENT_MAP_HOLE;
  2666. /*
  2667. * If we have a file range that points to a compressed extent
  2668. * and it's followed by a consecutive file range that points to
  2669. * to the same compressed extent (possibly with a different
  2670. * offset and/or length, so it either points to the whole extent
  2671. * or only part of it), we must make sure we do not submit a
  2672. * single bio to populate the pages for the 2 ranges because
  2673. * this makes the compressed extent read zero out the pages
  2674. * belonging to the 2nd range. Imagine the following scenario:
  2675. *
  2676. * File layout
  2677. * [0 - 8K] [8K - 24K]
  2678. * | |
  2679. * | |
  2680. * points to extent X, points to extent X,
  2681. * offset 4K, length of 8K offset 0, length 16K
  2682. *
  2683. * [extent X, compressed length = 4K uncompressed length = 16K]
  2684. *
  2685. * If the bio to read the compressed extent covers both ranges,
  2686. * it will decompress extent X into the pages belonging to the
  2687. * first range and then it will stop, zeroing out the remaining
  2688. * pages that belong to the other range that points to extent X.
  2689. * So here we make sure we submit 2 bios, one for the first
  2690. * range and another one for the third range. Both will target
  2691. * the same physical extent from disk, but we can't currently
  2692. * make the compressed bio endio callback populate the pages
  2693. * for both ranges because each compressed bio is tightly
  2694. * coupled with a single extent map, and each range can have
  2695. * an extent map with a different offset value relative to the
  2696. * uncompressed data of our extent and different lengths. This
  2697. * is a corner case so we prioritize correctness over
  2698. * non-optimal behavior (submitting 2 bios for the same extent).
  2699. */
  2700. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
  2701. prev_em_start && *prev_em_start != (u64)-1 &&
  2702. *prev_em_start != em->orig_start)
  2703. force_bio_submit = true;
  2704. if (prev_em_start)
  2705. *prev_em_start = em->orig_start;
  2706. free_extent_map(em);
  2707. em = NULL;
  2708. /* we've found a hole, just zero and go on */
  2709. if (block_start == EXTENT_MAP_HOLE) {
  2710. char *userpage;
  2711. struct extent_state *cached = NULL;
  2712. userpage = kmap_atomic(page);
  2713. memset(userpage + pg_offset, 0, iosize);
  2714. flush_dcache_page(page);
  2715. kunmap_atomic(userpage);
  2716. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2717. &cached, GFP_NOFS);
  2718. unlock_extent_cached(tree, cur,
  2719. cur + iosize - 1,
  2720. &cached, GFP_NOFS);
  2721. cur = cur + iosize;
  2722. pg_offset += iosize;
  2723. continue;
  2724. }
  2725. /* the get_extent function already copied into the page */
  2726. if (test_range_bit(tree, cur, cur_end,
  2727. EXTENT_UPTODATE, 1, NULL)) {
  2728. check_page_uptodate(tree, page);
  2729. unlock_extent(tree, cur, cur + iosize - 1);
  2730. cur = cur + iosize;
  2731. pg_offset += iosize;
  2732. continue;
  2733. }
  2734. /* we have an inline extent but it didn't get marked up
  2735. * to date. Error out
  2736. */
  2737. if (block_start == EXTENT_MAP_INLINE) {
  2738. SetPageError(page);
  2739. unlock_extent(tree, cur, cur + iosize - 1);
  2740. cur = cur + iosize;
  2741. pg_offset += iosize;
  2742. continue;
  2743. }
  2744. ret = submit_extent_page(REQ_OP_READ, read_flags, tree, NULL,
  2745. page, sector, disk_io_size, pg_offset,
  2746. bdev, bio,
  2747. end_bio_extent_readpage, mirror_num,
  2748. *bio_flags,
  2749. this_bio_flag,
  2750. force_bio_submit);
  2751. if (!ret) {
  2752. nr++;
  2753. *bio_flags = this_bio_flag;
  2754. } else {
  2755. SetPageError(page);
  2756. unlock_extent(tree, cur, cur + iosize - 1);
  2757. goto out;
  2758. }
  2759. cur = cur + iosize;
  2760. pg_offset += iosize;
  2761. }
  2762. out:
  2763. if (!nr) {
  2764. if (!PageError(page))
  2765. SetPageUptodate(page);
  2766. unlock_page(page);
  2767. }
  2768. return ret;
  2769. }
  2770. static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
  2771. struct page *pages[], int nr_pages,
  2772. u64 start, u64 end,
  2773. get_extent_t *get_extent,
  2774. struct extent_map **em_cached,
  2775. struct bio **bio, int mirror_num,
  2776. unsigned long *bio_flags,
  2777. u64 *prev_em_start)
  2778. {
  2779. struct inode *inode;
  2780. struct btrfs_ordered_extent *ordered;
  2781. int index;
  2782. inode = pages[0]->mapping->host;
  2783. while (1) {
  2784. lock_extent(tree, start, end);
  2785. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
  2786. end - start + 1);
  2787. if (!ordered)
  2788. break;
  2789. unlock_extent(tree, start, end);
  2790. btrfs_start_ordered_extent(inode, ordered, 1);
  2791. btrfs_put_ordered_extent(ordered);
  2792. }
  2793. for (index = 0; index < nr_pages; index++) {
  2794. __do_readpage(tree, pages[index], get_extent, em_cached, bio,
  2795. mirror_num, bio_flags, 0, prev_em_start);
  2796. put_page(pages[index]);
  2797. }
  2798. }
  2799. static void __extent_readpages(struct extent_io_tree *tree,
  2800. struct page *pages[],
  2801. int nr_pages, get_extent_t *get_extent,
  2802. struct extent_map **em_cached,
  2803. struct bio **bio, int mirror_num,
  2804. unsigned long *bio_flags,
  2805. u64 *prev_em_start)
  2806. {
  2807. u64 start = 0;
  2808. u64 end = 0;
  2809. u64 page_start;
  2810. int index;
  2811. int first_index = 0;
  2812. for (index = 0; index < nr_pages; index++) {
  2813. page_start = page_offset(pages[index]);
  2814. if (!end) {
  2815. start = page_start;
  2816. end = start + PAGE_SIZE - 1;
  2817. first_index = index;
  2818. } else if (end + 1 == page_start) {
  2819. end += PAGE_SIZE;
  2820. } else {
  2821. __do_contiguous_readpages(tree, &pages[first_index],
  2822. index - first_index, start,
  2823. end, get_extent, em_cached,
  2824. bio, mirror_num, bio_flags,
  2825. prev_em_start);
  2826. start = page_start;
  2827. end = start + PAGE_SIZE - 1;
  2828. first_index = index;
  2829. }
  2830. }
  2831. if (end)
  2832. __do_contiguous_readpages(tree, &pages[first_index],
  2833. index - first_index, start,
  2834. end, get_extent, em_cached, bio,
  2835. mirror_num, bio_flags,
  2836. prev_em_start);
  2837. }
  2838. static int __extent_read_full_page(struct extent_io_tree *tree,
  2839. struct page *page,
  2840. get_extent_t *get_extent,
  2841. struct bio **bio, int mirror_num,
  2842. unsigned long *bio_flags, int read_flags)
  2843. {
  2844. struct inode *inode = page->mapping->host;
  2845. struct btrfs_ordered_extent *ordered;
  2846. u64 start = page_offset(page);
  2847. u64 end = start + PAGE_SIZE - 1;
  2848. int ret;
  2849. while (1) {
  2850. lock_extent(tree, start, end);
  2851. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
  2852. PAGE_SIZE);
  2853. if (!ordered)
  2854. break;
  2855. unlock_extent(tree, start, end);
  2856. btrfs_start_ordered_extent(inode, ordered, 1);
  2857. btrfs_put_ordered_extent(ordered);
  2858. }
  2859. ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
  2860. bio_flags, read_flags, NULL);
  2861. return ret;
  2862. }
  2863. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2864. get_extent_t *get_extent, int mirror_num)
  2865. {
  2866. struct bio *bio = NULL;
  2867. unsigned long bio_flags = 0;
  2868. int ret;
  2869. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2870. &bio_flags, 0);
  2871. if (bio)
  2872. ret = submit_one_bio(bio, mirror_num, bio_flags);
  2873. return ret;
  2874. }
  2875. static void update_nr_written(struct writeback_control *wbc,
  2876. unsigned long nr_written)
  2877. {
  2878. wbc->nr_to_write -= nr_written;
  2879. }
  2880. /*
  2881. * helper for __extent_writepage, doing all of the delayed allocation setup.
  2882. *
  2883. * This returns 1 if our fill_delalloc function did all the work required
  2884. * to write the page (copy into inline extent). In this case the IO has
  2885. * been started and the page is already unlocked.
  2886. *
  2887. * This returns 0 if all went well (page still locked)
  2888. * This returns < 0 if there were errors (page still locked)
  2889. */
  2890. static noinline_for_stack int writepage_delalloc(struct inode *inode,
  2891. struct page *page, struct writeback_control *wbc,
  2892. struct extent_page_data *epd,
  2893. u64 delalloc_start,
  2894. unsigned long *nr_written)
  2895. {
  2896. struct extent_io_tree *tree = epd->tree;
  2897. u64 page_end = delalloc_start + PAGE_SIZE - 1;
  2898. u64 nr_delalloc;
  2899. u64 delalloc_to_write = 0;
  2900. u64 delalloc_end = 0;
  2901. int ret;
  2902. int page_started = 0;
  2903. if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
  2904. return 0;
  2905. while (delalloc_end < page_end) {
  2906. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2907. page,
  2908. &delalloc_start,
  2909. &delalloc_end,
  2910. BTRFS_MAX_EXTENT_SIZE);
  2911. if (nr_delalloc == 0) {
  2912. delalloc_start = delalloc_end + 1;
  2913. continue;
  2914. }
  2915. ret = tree->ops->fill_delalloc(inode, page,
  2916. delalloc_start,
  2917. delalloc_end,
  2918. &page_started,
  2919. nr_written);
  2920. /* File system has been set read-only */
  2921. if (ret) {
  2922. SetPageError(page);
  2923. /* fill_delalloc should be return < 0 for error
  2924. * but just in case, we use > 0 here meaning the
  2925. * IO is started, so we don't want to return > 0
  2926. * unless things are going well.
  2927. */
  2928. ret = ret < 0 ? ret : -EIO;
  2929. goto done;
  2930. }
  2931. /*
  2932. * delalloc_end is already one less than the total length, so
  2933. * we don't subtract one from PAGE_SIZE
  2934. */
  2935. delalloc_to_write += (delalloc_end - delalloc_start +
  2936. PAGE_SIZE) >> PAGE_SHIFT;
  2937. delalloc_start = delalloc_end + 1;
  2938. }
  2939. if (wbc->nr_to_write < delalloc_to_write) {
  2940. int thresh = 8192;
  2941. if (delalloc_to_write < thresh * 2)
  2942. thresh = delalloc_to_write;
  2943. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2944. thresh);
  2945. }
  2946. /* did the fill delalloc function already unlock and start
  2947. * the IO?
  2948. */
  2949. if (page_started) {
  2950. /*
  2951. * we've unlocked the page, so we can't update
  2952. * the mapping's writeback index, just update
  2953. * nr_to_write.
  2954. */
  2955. wbc->nr_to_write -= *nr_written;
  2956. return 1;
  2957. }
  2958. ret = 0;
  2959. done:
  2960. return ret;
  2961. }
  2962. /*
  2963. * helper for __extent_writepage. This calls the writepage start hooks,
  2964. * and does the loop to map the page into extents and bios.
  2965. *
  2966. * We return 1 if the IO is started and the page is unlocked,
  2967. * 0 if all went well (page still locked)
  2968. * < 0 if there were errors (page still locked)
  2969. */
  2970. static noinline_for_stack int __extent_writepage_io(struct inode *inode,
  2971. struct page *page,
  2972. struct writeback_control *wbc,
  2973. struct extent_page_data *epd,
  2974. loff_t i_size,
  2975. unsigned long nr_written,
  2976. int write_flags, int *nr_ret)
  2977. {
  2978. struct extent_io_tree *tree = epd->tree;
  2979. u64 start = page_offset(page);
  2980. u64 page_end = start + PAGE_SIZE - 1;
  2981. u64 end;
  2982. u64 cur = start;
  2983. u64 extent_offset;
  2984. u64 block_start;
  2985. u64 iosize;
  2986. sector_t sector;
  2987. struct extent_map *em;
  2988. struct block_device *bdev;
  2989. size_t pg_offset = 0;
  2990. size_t blocksize;
  2991. int ret = 0;
  2992. int nr = 0;
  2993. bool compressed;
  2994. if (tree->ops && tree->ops->writepage_start_hook) {
  2995. ret = tree->ops->writepage_start_hook(page, start,
  2996. page_end);
  2997. if (ret) {
  2998. /* Fixup worker will requeue */
  2999. if (ret == -EBUSY)
  3000. wbc->pages_skipped++;
  3001. else
  3002. redirty_page_for_writepage(wbc, page);
  3003. update_nr_written(wbc, nr_written);
  3004. unlock_page(page);
  3005. return 1;
  3006. }
  3007. }
  3008. /*
  3009. * we don't want to touch the inode after unlocking the page,
  3010. * so we update the mapping writeback index now
  3011. */
  3012. update_nr_written(wbc, nr_written + 1);
  3013. end = page_end;
  3014. if (i_size <= start) {
  3015. if (tree->ops && tree->ops->writepage_end_io_hook)
  3016. tree->ops->writepage_end_io_hook(page, start,
  3017. page_end, NULL, 1);
  3018. goto done;
  3019. }
  3020. blocksize = inode->i_sb->s_blocksize;
  3021. while (cur <= end) {
  3022. u64 em_end;
  3023. if (cur >= i_size) {
  3024. if (tree->ops && tree->ops->writepage_end_io_hook)
  3025. tree->ops->writepage_end_io_hook(page, cur,
  3026. page_end, NULL, 1);
  3027. break;
  3028. }
  3029. em = epd->get_extent(BTRFS_I(inode), page, pg_offset, cur,
  3030. end - cur + 1, 1);
  3031. if (IS_ERR_OR_NULL(em)) {
  3032. SetPageError(page);
  3033. ret = PTR_ERR_OR_ZERO(em);
  3034. break;
  3035. }
  3036. extent_offset = cur - em->start;
  3037. em_end = extent_map_end(em);
  3038. BUG_ON(em_end <= cur);
  3039. BUG_ON(end < cur);
  3040. iosize = min(em_end - cur, end - cur + 1);
  3041. iosize = ALIGN(iosize, blocksize);
  3042. sector = (em->block_start + extent_offset) >> 9;
  3043. bdev = em->bdev;
  3044. block_start = em->block_start;
  3045. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3046. free_extent_map(em);
  3047. em = NULL;
  3048. /*
  3049. * compressed and inline extents are written through other
  3050. * paths in the FS
  3051. */
  3052. if (compressed || block_start == EXTENT_MAP_HOLE ||
  3053. block_start == EXTENT_MAP_INLINE) {
  3054. /*
  3055. * end_io notification does not happen here for
  3056. * compressed extents
  3057. */
  3058. if (!compressed && tree->ops &&
  3059. tree->ops->writepage_end_io_hook)
  3060. tree->ops->writepage_end_io_hook(page, cur,
  3061. cur + iosize - 1,
  3062. NULL, 1);
  3063. else if (compressed) {
  3064. /* we don't want to end_page_writeback on
  3065. * a compressed extent. this happens
  3066. * elsewhere
  3067. */
  3068. nr++;
  3069. }
  3070. cur += iosize;
  3071. pg_offset += iosize;
  3072. continue;
  3073. }
  3074. set_range_writeback(tree, cur, cur + iosize - 1);
  3075. if (!PageWriteback(page)) {
  3076. btrfs_err(BTRFS_I(inode)->root->fs_info,
  3077. "page %lu not writeback, cur %llu end %llu",
  3078. page->index, cur, end);
  3079. }
  3080. ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
  3081. page, sector, iosize, pg_offset,
  3082. bdev, &epd->bio,
  3083. end_bio_extent_writepage,
  3084. 0, 0, 0, false);
  3085. if (ret) {
  3086. SetPageError(page);
  3087. if (PageWriteback(page))
  3088. end_page_writeback(page);
  3089. }
  3090. cur = cur + iosize;
  3091. pg_offset += iosize;
  3092. nr++;
  3093. }
  3094. done:
  3095. *nr_ret = nr;
  3096. return ret;
  3097. }
  3098. /*
  3099. * the writepage semantics are similar to regular writepage. extent
  3100. * records are inserted to lock ranges in the tree, and as dirty areas
  3101. * are found, they are marked writeback. Then the lock bits are removed
  3102. * and the end_io handler clears the writeback ranges
  3103. */
  3104. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  3105. void *data)
  3106. {
  3107. struct inode *inode = page->mapping->host;
  3108. struct extent_page_data *epd = data;
  3109. u64 start = page_offset(page);
  3110. u64 page_end = start + PAGE_SIZE - 1;
  3111. int ret;
  3112. int nr = 0;
  3113. size_t pg_offset = 0;
  3114. loff_t i_size = i_size_read(inode);
  3115. unsigned long end_index = i_size >> PAGE_SHIFT;
  3116. int write_flags = 0;
  3117. unsigned long nr_written = 0;
  3118. if (wbc->sync_mode == WB_SYNC_ALL)
  3119. write_flags = REQ_SYNC;
  3120. trace___extent_writepage(page, inode, wbc);
  3121. WARN_ON(!PageLocked(page));
  3122. ClearPageError(page);
  3123. pg_offset = i_size & (PAGE_SIZE - 1);
  3124. if (page->index > end_index ||
  3125. (page->index == end_index && !pg_offset)) {
  3126. page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
  3127. unlock_page(page);
  3128. return 0;
  3129. }
  3130. if (page->index == end_index) {
  3131. char *userpage;
  3132. userpage = kmap_atomic(page);
  3133. memset(userpage + pg_offset, 0,
  3134. PAGE_SIZE - pg_offset);
  3135. kunmap_atomic(userpage);
  3136. flush_dcache_page(page);
  3137. }
  3138. pg_offset = 0;
  3139. set_page_extent_mapped(page);
  3140. ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
  3141. if (ret == 1)
  3142. goto done_unlocked;
  3143. if (ret)
  3144. goto done;
  3145. ret = __extent_writepage_io(inode, page, wbc, epd,
  3146. i_size, nr_written, write_flags, &nr);
  3147. if (ret == 1)
  3148. goto done_unlocked;
  3149. done:
  3150. if (nr == 0) {
  3151. /* make sure the mapping tag for page dirty gets cleared */
  3152. set_page_writeback(page);
  3153. end_page_writeback(page);
  3154. }
  3155. if (PageError(page)) {
  3156. ret = ret < 0 ? ret : -EIO;
  3157. end_extent_writepage(page, ret, start, page_end);
  3158. }
  3159. unlock_page(page);
  3160. return ret;
  3161. done_unlocked:
  3162. return 0;
  3163. }
  3164. void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
  3165. {
  3166. wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
  3167. TASK_UNINTERRUPTIBLE);
  3168. }
  3169. static noinline_for_stack int
  3170. lock_extent_buffer_for_io(struct extent_buffer *eb,
  3171. struct btrfs_fs_info *fs_info,
  3172. struct extent_page_data *epd)
  3173. {
  3174. unsigned long i, num_pages;
  3175. int flush = 0;
  3176. int ret = 0;
  3177. if (!btrfs_try_tree_write_lock(eb)) {
  3178. flush = 1;
  3179. flush_write_bio(epd);
  3180. btrfs_tree_lock(eb);
  3181. }
  3182. if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  3183. btrfs_tree_unlock(eb);
  3184. if (!epd->sync_io)
  3185. return 0;
  3186. if (!flush) {
  3187. flush_write_bio(epd);
  3188. flush = 1;
  3189. }
  3190. while (1) {
  3191. wait_on_extent_buffer_writeback(eb);
  3192. btrfs_tree_lock(eb);
  3193. if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
  3194. break;
  3195. btrfs_tree_unlock(eb);
  3196. }
  3197. }
  3198. /*
  3199. * We need to do this to prevent races in people who check if the eb is
  3200. * under IO since we can end up having no IO bits set for a short period
  3201. * of time.
  3202. */
  3203. spin_lock(&eb->refs_lock);
  3204. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3205. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3206. spin_unlock(&eb->refs_lock);
  3207. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  3208. __percpu_counter_add(&fs_info->dirty_metadata_bytes,
  3209. -eb->len,
  3210. fs_info->dirty_metadata_batch);
  3211. ret = 1;
  3212. } else {
  3213. spin_unlock(&eb->refs_lock);
  3214. }
  3215. btrfs_tree_unlock(eb);
  3216. if (!ret)
  3217. return ret;
  3218. num_pages = num_extent_pages(eb->start, eb->len);
  3219. for (i = 0; i < num_pages; i++) {
  3220. struct page *p = eb->pages[i];
  3221. if (!trylock_page(p)) {
  3222. if (!flush) {
  3223. flush_write_bio(epd);
  3224. flush = 1;
  3225. }
  3226. lock_page(p);
  3227. }
  3228. }
  3229. return ret;
  3230. }
  3231. static void end_extent_buffer_writeback(struct extent_buffer *eb)
  3232. {
  3233. clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3234. smp_mb__after_atomic();
  3235. wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
  3236. }
  3237. static void set_btree_ioerr(struct page *page)
  3238. {
  3239. struct extent_buffer *eb = (struct extent_buffer *)page->private;
  3240. SetPageError(page);
  3241. if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
  3242. return;
  3243. /*
  3244. * If writeback for a btree extent that doesn't belong to a log tree
  3245. * failed, increment the counter transaction->eb_write_errors.
  3246. * We do this because while the transaction is running and before it's
  3247. * committing (when we call filemap_fdata[write|wait]_range against
  3248. * the btree inode), we might have
  3249. * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
  3250. * returns an error or an error happens during writeback, when we're
  3251. * committing the transaction we wouldn't know about it, since the pages
  3252. * can be no longer dirty nor marked anymore for writeback (if a
  3253. * subsequent modification to the extent buffer didn't happen before the
  3254. * transaction commit), which makes filemap_fdata[write|wait]_range not
  3255. * able to find the pages tagged with SetPageError at transaction
  3256. * commit time. So if this happens we must abort the transaction,
  3257. * otherwise we commit a super block with btree roots that point to
  3258. * btree nodes/leafs whose content on disk is invalid - either garbage
  3259. * or the content of some node/leaf from a past generation that got
  3260. * cowed or deleted and is no longer valid.
  3261. *
  3262. * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
  3263. * not be enough - we need to distinguish between log tree extents vs
  3264. * non-log tree extents, and the next filemap_fdatawait_range() call
  3265. * will catch and clear such errors in the mapping - and that call might
  3266. * be from a log sync and not from a transaction commit. Also, checking
  3267. * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
  3268. * not done and would not be reliable - the eb might have been released
  3269. * from memory and reading it back again means that flag would not be
  3270. * set (since it's a runtime flag, not persisted on disk).
  3271. *
  3272. * Using the flags below in the btree inode also makes us achieve the
  3273. * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
  3274. * writeback for all dirty pages and before filemap_fdatawait_range()
  3275. * is called, the writeback for all dirty pages had already finished
  3276. * with errors - because we were not using AS_EIO/AS_ENOSPC,
  3277. * filemap_fdatawait_range() would return success, as it could not know
  3278. * that writeback errors happened (the pages were no longer tagged for
  3279. * writeback).
  3280. */
  3281. switch (eb->log_index) {
  3282. case -1:
  3283. set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
  3284. break;
  3285. case 0:
  3286. set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
  3287. break;
  3288. case 1:
  3289. set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
  3290. break;
  3291. default:
  3292. BUG(); /* unexpected, logic error */
  3293. }
  3294. }
  3295. static void end_bio_extent_buffer_writepage(struct bio *bio)
  3296. {
  3297. struct bio_vec *bvec;
  3298. struct extent_buffer *eb;
  3299. int i, done;
  3300. bio_for_each_segment_all(bvec, bio, i) {
  3301. struct page *page = bvec->bv_page;
  3302. eb = (struct extent_buffer *)page->private;
  3303. BUG_ON(!eb);
  3304. done = atomic_dec_and_test(&eb->io_pages);
  3305. if (bio->bi_error ||
  3306. test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
  3307. ClearPageUptodate(page);
  3308. set_btree_ioerr(page);
  3309. }
  3310. end_page_writeback(page);
  3311. if (!done)
  3312. continue;
  3313. end_extent_buffer_writeback(eb);
  3314. }
  3315. bio_put(bio);
  3316. }
  3317. static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
  3318. struct btrfs_fs_info *fs_info,
  3319. struct writeback_control *wbc,
  3320. struct extent_page_data *epd)
  3321. {
  3322. struct block_device *bdev = fs_info->fs_devices->latest_bdev;
  3323. struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
  3324. u64 offset = eb->start;
  3325. u32 nritems;
  3326. unsigned long i, num_pages;
  3327. unsigned long bio_flags = 0;
  3328. unsigned long start, end;
  3329. int write_flags = (epd->sync_io ? REQ_SYNC : 0) | REQ_META;
  3330. int ret = 0;
  3331. clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
  3332. num_pages = num_extent_pages(eb->start, eb->len);
  3333. atomic_set(&eb->io_pages, num_pages);
  3334. if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
  3335. bio_flags = EXTENT_BIO_TREE_LOG;
  3336. /* set btree blocks beyond nritems with 0 to avoid stale content. */
  3337. nritems = btrfs_header_nritems(eb);
  3338. if (btrfs_header_level(eb) > 0) {
  3339. end = btrfs_node_key_ptr_offset(nritems);
  3340. memzero_extent_buffer(eb, end, eb->len - end);
  3341. } else {
  3342. /*
  3343. * leaf:
  3344. * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
  3345. */
  3346. start = btrfs_item_nr_offset(nritems);
  3347. end = btrfs_leaf_data(eb) + leaf_data_end(fs_info, eb);
  3348. memzero_extent_buffer(eb, start, end - start);
  3349. }
  3350. for (i = 0; i < num_pages; i++) {
  3351. struct page *p = eb->pages[i];
  3352. clear_page_dirty_for_io(p);
  3353. set_page_writeback(p);
  3354. ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
  3355. p, offset >> 9, PAGE_SIZE, 0, bdev,
  3356. &epd->bio,
  3357. end_bio_extent_buffer_writepage,
  3358. 0, epd->bio_flags, bio_flags, false);
  3359. epd->bio_flags = bio_flags;
  3360. if (ret) {
  3361. set_btree_ioerr(p);
  3362. if (PageWriteback(p))
  3363. end_page_writeback(p);
  3364. if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
  3365. end_extent_buffer_writeback(eb);
  3366. ret = -EIO;
  3367. break;
  3368. }
  3369. offset += PAGE_SIZE;
  3370. update_nr_written(wbc, 1);
  3371. unlock_page(p);
  3372. }
  3373. if (unlikely(ret)) {
  3374. for (; i < num_pages; i++) {
  3375. struct page *p = eb->pages[i];
  3376. clear_page_dirty_for_io(p);
  3377. unlock_page(p);
  3378. }
  3379. }
  3380. return ret;
  3381. }
  3382. int btree_write_cache_pages(struct address_space *mapping,
  3383. struct writeback_control *wbc)
  3384. {
  3385. struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
  3386. struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
  3387. struct extent_buffer *eb, *prev_eb = NULL;
  3388. struct extent_page_data epd = {
  3389. .bio = NULL,
  3390. .tree = tree,
  3391. .extent_locked = 0,
  3392. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3393. .bio_flags = 0,
  3394. };
  3395. int ret = 0;
  3396. int done = 0;
  3397. int nr_to_write_done = 0;
  3398. struct pagevec pvec;
  3399. int nr_pages;
  3400. pgoff_t index;
  3401. pgoff_t end; /* Inclusive */
  3402. int scanned = 0;
  3403. int tag;
  3404. pagevec_init(&pvec, 0);
  3405. if (wbc->range_cyclic) {
  3406. index = mapping->writeback_index; /* Start from prev offset */
  3407. end = -1;
  3408. } else {
  3409. index = wbc->range_start >> PAGE_SHIFT;
  3410. end = wbc->range_end >> PAGE_SHIFT;
  3411. scanned = 1;
  3412. }
  3413. if (wbc->sync_mode == WB_SYNC_ALL)
  3414. tag = PAGECACHE_TAG_TOWRITE;
  3415. else
  3416. tag = PAGECACHE_TAG_DIRTY;
  3417. retry:
  3418. if (wbc->sync_mode == WB_SYNC_ALL)
  3419. tag_pages_for_writeback(mapping, index, end);
  3420. while (!done && !nr_to_write_done && (index <= end) &&
  3421. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3422. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3423. unsigned i;
  3424. scanned = 1;
  3425. for (i = 0; i < nr_pages; i++) {
  3426. struct page *page = pvec.pages[i];
  3427. if (!PagePrivate(page))
  3428. continue;
  3429. if (!wbc->range_cyclic && page->index > end) {
  3430. done = 1;
  3431. break;
  3432. }
  3433. spin_lock(&mapping->private_lock);
  3434. if (!PagePrivate(page)) {
  3435. spin_unlock(&mapping->private_lock);
  3436. continue;
  3437. }
  3438. eb = (struct extent_buffer *)page->private;
  3439. /*
  3440. * Shouldn't happen and normally this would be a BUG_ON
  3441. * but no sense in crashing the users box for something
  3442. * we can survive anyway.
  3443. */
  3444. if (WARN_ON(!eb)) {
  3445. spin_unlock(&mapping->private_lock);
  3446. continue;
  3447. }
  3448. if (eb == prev_eb) {
  3449. spin_unlock(&mapping->private_lock);
  3450. continue;
  3451. }
  3452. ret = atomic_inc_not_zero(&eb->refs);
  3453. spin_unlock(&mapping->private_lock);
  3454. if (!ret)
  3455. continue;
  3456. prev_eb = eb;
  3457. ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
  3458. if (!ret) {
  3459. free_extent_buffer(eb);
  3460. continue;
  3461. }
  3462. ret = write_one_eb(eb, fs_info, wbc, &epd);
  3463. if (ret) {
  3464. done = 1;
  3465. free_extent_buffer(eb);
  3466. break;
  3467. }
  3468. free_extent_buffer(eb);
  3469. /*
  3470. * the filesystem may choose to bump up nr_to_write.
  3471. * We have to make sure to honor the new nr_to_write
  3472. * at any time
  3473. */
  3474. nr_to_write_done = wbc->nr_to_write <= 0;
  3475. }
  3476. pagevec_release(&pvec);
  3477. cond_resched();
  3478. }
  3479. if (!scanned && !done) {
  3480. /*
  3481. * We hit the last page and there is more work to be done: wrap
  3482. * back to the start of the file
  3483. */
  3484. scanned = 1;
  3485. index = 0;
  3486. goto retry;
  3487. }
  3488. flush_write_bio(&epd);
  3489. return ret;
  3490. }
  3491. /**
  3492. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  3493. * @mapping: address space structure to write
  3494. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  3495. * @writepage: function called for each page
  3496. * @data: data passed to writepage function
  3497. *
  3498. * If a page is already under I/O, write_cache_pages() skips it, even
  3499. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  3500. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  3501. * and msync() need to guarantee that all the data which was dirty at the time
  3502. * the call was made get new I/O started against them. If wbc->sync_mode is
  3503. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  3504. * existing IO to complete.
  3505. */
  3506. static int extent_write_cache_pages(struct address_space *mapping,
  3507. struct writeback_control *wbc,
  3508. writepage_t writepage, void *data,
  3509. void (*flush_fn)(void *))
  3510. {
  3511. struct inode *inode = mapping->host;
  3512. int ret = 0;
  3513. int done = 0;
  3514. int nr_to_write_done = 0;
  3515. struct pagevec pvec;
  3516. int nr_pages;
  3517. pgoff_t index;
  3518. pgoff_t end; /* Inclusive */
  3519. pgoff_t done_index;
  3520. int range_whole = 0;
  3521. int scanned = 0;
  3522. int tag;
  3523. /*
  3524. * We have to hold onto the inode so that ordered extents can do their
  3525. * work when the IO finishes. The alternative to this is failing to add
  3526. * an ordered extent if the igrab() fails there and that is a huge pain
  3527. * to deal with, so instead just hold onto the inode throughout the
  3528. * writepages operation. If it fails here we are freeing up the inode
  3529. * anyway and we'd rather not waste our time writing out stuff that is
  3530. * going to be truncated anyway.
  3531. */
  3532. if (!igrab(inode))
  3533. return 0;
  3534. pagevec_init(&pvec, 0);
  3535. if (wbc->range_cyclic) {
  3536. index = mapping->writeback_index; /* Start from prev offset */
  3537. end = -1;
  3538. } else {
  3539. index = wbc->range_start >> PAGE_SHIFT;
  3540. end = wbc->range_end >> PAGE_SHIFT;
  3541. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  3542. range_whole = 1;
  3543. scanned = 1;
  3544. }
  3545. if (wbc->sync_mode == WB_SYNC_ALL)
  3546. tag = PAGECACHE_TAG_TOWRITE;
  3547. else
  3548. tag = PAGECACHE_TAG_DIRTY;
  3549. retry:
  3550. if (wbc->sync_mode == WB_SYNC_ALL)
  3551. tag_pages_for_writeback(mapping, index, end);
  3552. done_index = index;
  3553. while (!done && !nr_to_write_done && (index <= end) &&
  3554. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3555. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3556. unsigned i;
  3557. scanned = 1;
  3558. for (i = 0; i < nr_pages; i++) {
  3559. struct page *page = pvec.pages[i];
  3560. done_index = page->index;
  3561. /*
  3562. * At this point we hold neither mapping->tree_lock nor
  3563. * lock on the page itself: the page may be truncated or
  3564. * invalidated (changing page->mapping to NULL), or even
  3565. * swizzled back from swapper_space to tmpfs file
  3566. * mapping
  3567. */
  3568. if (!trylock_page(page)) {
  3569. flush_fn(data);
  3570. lock_page(page);
  3571. }
  3572. if (unlikely(page->mapping != mapping)) {
  3573. unlock_page(page);
  3574. continue;
  3575. }
  3576. if (!wbc->range_cyclic && page->index > end) {
  3577. done = 1;
  3578. unlock_page(page);
  3579. continue;
  3580. }
  3581. if (wbc->sync_mode != WB_SYNC_NONE) {
  3582. if (PageWriteback(page))
  3583. flush_fn(data);
  3584. wait_on_page_writeback(page);
  3585. }
  3586. if (PageWriteback(page) ||
  3587. !clear_page_dirty_for_io(page)) {
  3588. unlock_page(page);
  3589. continue;
  3590. }
  3591. ret = (*writepage)(page, wbc, data);
  3592. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  3593. unlock_page(page);
  3594. ret = 0;
  3595. }
  3596. if (ret < 0) {
  3597. /*
  3598. * done_index is set past this page,
  3599. * so media errors will not choke
  3600. * background writeout for the entire
  3601. * file. This has consequences for
  3602. * range_cyclic semantics (ie. it may
  3603. * not be suitable for data integrity
  3604. * writeout).
  3605. */
  3606. done_index = page->index + 1;
  3607. done = 1;
  3608. break;
  3609. }
  3610. /*
  3611. * the filesystem may choose to bump up nr_to_write.
  3612. * We have to make sure to honor the new nr_to_write
  3613. * at any time
  3614. */
  3615. nr_to_write_done = wbc->nr_to_write <= 0;
  3616. }
  3617. pagevec_release(&pvec);
  3618. cond_resched();
  3619. }
  3620. if (!scanned && !done) {
  3621. /*
  3622. * We hit the last page and there is more work to be done: wrap
  3623. * back to the start of the file
  3624. */
  3625. scanned = 1;
  3626. index = 0;
  3627. goto retry;
  3628. }
  3629. if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
  3630. mapping->writeback_index = done_index;
  3631. btrfs_add_delayed_iput(inode);
  3632. return ret;
  3633. }
  3634. static void flush_epd_write_bio(struct extent_page_data *epd)
  3635. {
  3636. if (epd->bio) {
  3637. int ret;
  3638. bio_set_op_attrs(epd->bio, REQ_OP_WRITE,
  3639. epd->sync_io ? REQ_SYNC : 0);
  3640. ret = submit_one_bio(epd->bio, 0, epd->bio_flags);
  3641. BUG_ON(ret < 0); /* -ENOMEM */
  3642. epd->bio = NULL;
  3643. }
  3644. }
  3645. static noinline void flush_write_bio(void *data)
  3646. {
  3647. struct extent_page_data *epd = data;
  3648. flush_epd_write_bio(epd);
  3649. }
  3650. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  3651. get_extent_t *get_extent,
  3652. struct writeback_control *wbc)
  3653. {
  3654. int ret;
  3655. struct extent_page_data epd = {
  3656. .bio = NULL,
  3657. .tree = tree,
  3658. .get_extent = get_extent,
  3659. .extent_locked = 0,
  3660. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3661. .bio_flags = 0,
  3662. };
  3663. ret = __extent_writepage(page, wbc, &epd);
  3664. flush_epd_write_bio(&epd);
  3665. return ret;
  3666. }
  3667. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  3668. u64 start, u64 end, get_extent_t *get_extent,
  3669. int mode)
  3670. {
  3671. int ret = 0;
  3672. struct address_space *mapping = inode->i_mapping;
  3673. struct page *page;
  3674. unsigned long nr_pages = (end - start + PAGE_SIZE) >>
  3675. PAGE_SHIFT;
  3676. struct extent_page_data epd = {
  3677. .bio = NULL,
  3678. .tree = tree,
  3679. .get_extent = get_extent,
  3680. .extent_locked = 1,
  3681. .sync_io = mode == WB_SYNC_ALL,
  3682. .bio_flags = 0,
  3683. };
  3684. struct writeback_control wbc_writepages = {
  3685. .sync_mode = mode,
  3686. .nr_to_write = nr_pages * 2,
  3687. .range_start = start,
  3688. .range_end = end + 1,
  3689. };
  3690. while (start <= end) {
  3691. page = find_get_page(mapping, start >> PAGE_SHIFT);
  3692. if (clear_page_dirty_for_io(page))
  3693. ret = __extent_writepage(page, &wbc_writepages, &epd);
  3694. else {
  3695. if (tree->ops && tree->ops->writepage_end_io_hook)
  3696. tree->ops->writepage_end_io_hook(page, start,
  3697. start + PAGE_SIZE - 1,
  3698. NULL, 1);
  3699. unlock_page(page);
  3700. }
  3701. put_page(page);
  3702. start += PAGE_SIZE;
  3703. }
  3704. flush_epd_write_bio(&epd);
  3705. return ret;
  3706. }
  3707. int extent_writepages(struct extent_io_tree *tree,
  3708. struct address_space *mapping,
  3709. get_extent_t *get_extent,
  3710. struct writeback_control *wbc)
  3711. {
  3712. int ret = 0;
  3713. struct extent_page_data epd = {
  3714. .bio = NULL,
  3715. .tree = tree,
  3716. .get_extent = get_extent,
  3717. .extent_locked = 0,
  3718. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3719. .bio_flags = 0,
  3720. };
  3721. ret = extent_write_cache_pages(mapping, wbc, __extent_writepage, &epd,
  3722. flush_write_bio);
  3723. flush_epd_write_bio(&epd);
  3724. return ret;
  3725. }
  3726. int extent_readpages(struct extent_io_tree *tree,
  3727. struct address_space *mapping,
  3728. struct list_head *pages, unsigned nr_pages,
  3729. get_extent_t get_extent)
  3730. {
  3731. struct bio *bio = NULL;
  3732. unsigned page_idx;
  3733. unsigned long bio_flags = 0;
  3734. struct page *pagepool[16];
  3735. struct page *page;
  3736. struct extent_map *em_cached = NULL;
  3737. int nr = 0;
  3738. u64 prev_em_start = (u64)-1;
  3739. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  3740. page = list_entry(pages->prev, struct page, lru);
  3741. prefetchw(&page->flags);
  3742. list_del(&page->lru);
  3743. if (add_to_page_cache_lru(page, mapping,
  3744. page->index,
  3745. readahead_gfp_mask(mapping))) {
  3746. put_page(page);
  3747. continue;
  3748. }
  3749. pagepool[nr++] = page;
  3750. if (nr < ARRAY_SIZE(pagepool))
  3751. continue;
  3752. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3753. &bio, 0, &bio_flags, &prev_em_start);
  3754. nr = 0;
  3755. }
  3756. if (nr)
  3757. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3758. &bio, 0, &bio_flags, &prev_em_start);
  3759. if (em_cached)
  3760. free_extent_map(em_cached);
  3761. BUG_ON(!list_empty(pages));
  3762. if (bio)
  3763. return submit_one_bio(bio, 0, bio_flags);
  3764. return 0;
  3765. }
  3766. /*
  3767. * basic invalidatepage code, this waits on any locked or writeback
  3768. * ranges corresponding to the page, and then deletes any extent state
  3769. * records from the tree
  3770. */
  3771. int extent_invalidatepage(struct extent_io_tree *tree,
  3772. struct page *page, unsigned long offset)
  3773. {
  3774. struct extent_state *cached_state = NULL;
  3775. u64 start = page_offset(page);
  3776. u64 end = start + PAGE_SIZE - 1;
  3777. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  3778. start += ALIGN(offset, blocksize);
  3779. if (start > end)
  3780. return 0;
  3781. lock_extent_bits(tree, start, end, &cached_state);
  3782. wait_on_page_writeback(page);
  3783. clear_extent_bit(tree, start, end,
  3784. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3785. EXTENT_DO_ACCOUNTING,
  3786. 1, 1, &cached_state, GFP_NOFS);
  3787. return 0;
  3788. }
  3789. /*
  3790. * a helper for releasepage, this tests for areas of the page that
  3791. * are locked or under IO and drops the related state bits if it is safe
  3792. * to drop the page.
  3793. */
  3794. static int try_release_extent_state(struct extent_map_tree *map,
  3795. struct extent_io_tree *tree,
  3796. struct page *page, gfp_t mask)
  3797. {
  3798. u64 start = page_offset(page);
  3799. u64 end = start + PAGE_SIZE - 1;
  3800. int ret = 1;
  3801. if (test_range_bit(tree, start, end,
  3802. EXTENT_IOBITS, 0, NULL))
  3803. ret = 0;
  3804. else {
  3805. /*
  3806. * at this point we can safely clear everything except the
  3807. * locked bit and the nodatasum bit
  3808. */
  3809. ret = clear_extent_bit(tree, start, end,
  3810. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  3811. 0, 0, NULL, mask);
  3812. /* if clear_extent_bit failed for enomem reasons,
  3813. * we can't allow the release to continue.
  3814. */
  3815. if (ret < 0)
  3816. ret = 0;
  3817. else
  3818. ret = 1;
  3819. }
  3820. return ret;
  3821. }
  3822. /*
  3823. * a helper for releasepage. As long as there are no locked extents
  3824. * in the range corresponding to the page, both state records and extent
  3825. * map records are removed
  3826. */
  3827. int try_release_extent_mapping(struct extent_map_tree *map,
  3828. struct extent_io_tree *tree, struct page *page,
  3829. gfp_t mask)
  3830. {
  3831. struct extent_map *em;
  3832. u64 start = page_offset(page);
  3833. u64 end = start + PAGE_SIZE - 1;
  3834. if (gfpflags_allow_blocking(mask) &&
  3835. page->mapping->host->i_size > SZ_16M) {
  3836. u64 len;
  3837. while (start <= end) {
  3838. len = end - start + 1;
  3839. write_lock(&map->lock);
  3840. em = lookup_extent_mapping(map, start, len);
  3841. if (!em) {
  3842. write_unlock(&map->lock);
  3843. break;
  3844. }
  3845. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3846. em->start != start) {
  3847. write_unlock(&map->lock);
  3848. free_extent_map(em);
  3849. break;
  3850. }
  3851. if (!test_range_bit(tree, em->start,
  3852. extent_map_end(em) - 1,
  3853. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3854. 0, NULL)) {
  3855. remove_extent_mapping(map, em);
  3856. /* once for the rb tree */
  3857. free_extent_map(em);
  3858. }
  3859. start = extent_map_end(em);
  3860. write_unlock(&map->lock);
  3861. /* once for us */
  3862. free_extent_map(em);
  3863. }
  3864. }
  3865. return try_release_extent_state(map, tree, page, mask);
  3866. }
  3867. /*
  3868. * helper function for fiemap, which doesn't want to see any holes.
  3869. * This maps until we find something past 'last'
  3870. */
  3871. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3872. u64 offset,
  3873. u64 last,
  3874. get_extent_t *get_extent)
  3875. {
  3876. u64 sectorsize = btrfs_inode_sectorsize(inode);
  3877. struct extent_map *em;
  3878. u64 len;
  3879. if (offset >= last)
  3880. return NULL;
  3881. while (1) {
  3882. len = last - offset;
  3883. if (len == 0)
  3884. break;
  3885. len = ALIGN(len, sectorsize);
  3886. em = get_extent(BTRFS_I(inode), NULL, 0, offset, len, 0);
  3887. if (IS_ERR_OR_NULL(em))
  3888. return em;
  3889. /* if this isn't a hole return it */
  3890. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3891. em->block_start != EXTENT_MAP_HOLE) {
  3892. return em;
  3893. }
  3894. /* this is a hole, advance to the next extent */
  3895. offset = extent_map_end(em);
  3896. free_extent_map(em);
  3897. if (offset >= last)
  3898. break;
  3899. }
  3900. return NULL;
  3901. }
  3902. /*
  3903. * To cache previous fiemap extent
  3904. *
  3905. * Will be used for merging fiemap extent
  3906. */
  3907. struct fiemap_cache {
  3908. u64 offset;
  3909. u64 phys;
  3910. u64 len;
  3911. u32 flags;
  3912. bool cached;
  3913. };
  3914. /*
  3915. * Helper to submit fiemap extent.
  3916. *
  3917. * Will try to merge current fiemap extent specified by @offset, @phys,
  3918. * @len and @flags with cached one.
  3919. * And only when we fails to merge, cached one will be submitted as
  3920. * fiemap extent.
  3921. *
  3922. * Return value is the same as fiemap_fill_next_extent().
  3923. */
  3924. static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
  3925. struct fiemap_cache *cache,
  3926. u64 offset, u64 phys, u64 len, u32 flags)
  3927. {
  3928. int ret = 0;
  3929. if (!cache->cached)
  3930. goto assign;
  3931. /*
  3932. * Sanity check, extent_fiemap() should have ensured that new
  3933. * fiemap extent won't overlap with cahced one.
  3934. * Not recoverable.
  3935. *
  3936. * NOTE: Physical address can overlap, due to compression
  3937. */
  3938. if (cache->offset + cache->len > offset) {
  3939. WARN_ON(1);
  3940. return -EINVAL;
  3941. }
  3942. /*
  3943. * Only merges fiemap extents if
  3944. * 1) Their logical addresses are continuous
  3945. *
  3946. * 2) Their physical addresses are continuous
  3947. * So truly compressed (physical size smaller than logical size)
  3948. * extents won't get merged with each other
  3949. *
  3950. * 3) Share same flags except FIEMAP_EXTENT_LAST
  3951. * So regular extent won't get merged with prealloc extent
  3952. */
  3953. if (cache->offset + cache->len == offset &&
  3954. cache->phys + cache->len == phys &&
  3955. (cache->flags & ~FIEMAP_EXTENT_LAST) ==
  3956. (flags & ~FIEMAP_EXTENT_LAST)) {
  3957. cache->len += len;
  3958. cache->flags |= flags;
  3959. goto try_submit_last;
  3960. }
  3961. /* Not mergeable, need to submit cached one */
  3962. ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
  3963. cache->len, cache->flags);
  3964. cache->cached = false;
  3965. if (ret)
  3966. return ret;
  3967. assign:
  3968. cache->cached = true;
  3969. cache->offset = offset;
  3970. cache->phys = phys;
  3971. cache->len = len;
  3972. cache->flags = flags;
  3973. try_submit_last:
  3974. if (cache->flags & FIEMAP_EXTENT_LAST) {
  3975. ret = fiemap_fill_next_extent(fieinfo, cache->offset,
  3976. cache->phys, cache->len, cache->flags);
  3977. cache->cached = false;
  3978. }
  3979. return ret;
  3980. }
  3981. /*
  3982. * Sanity check for fiemap cache
  3983. *
  3984. * All fiemap cache should be submitted by emit_fiemap_extent()
  3985. * Iteration should be terminated either by last fiemap extent or
  3986. * fieinfo->fi_extents_max.
  3987. * So no cached fiemap should exist.
  3988. */
  3989. static int check_fiemap_cache(struct btrfs_fs_info *fs_info,
  3990. struct fiemap_extent_info *fieinfo,
  3991. struct fiemap_cache *cache)
  3992. {
  3993. int ret;
  3994. if (!cache->cached)
  3995. return 0;
  3996. /* Small and recoverbale problem, only to info developer */
  3997. #ifdef CONFIG_BTRFS_DEBUG
  3998. WARN_ON(1);
  3999. #endif
  4000. btrfs_warn(fs_info,
  4001. "unhandled fiemap cache detected: offset=%llu phys=%llu len=%llu flags=0x%x",
  4002. cache->offset, cache->phys, cache->len, cache->flags);
  4003. ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
  4004. cache->len, cache->flags);
  4005. cache->cached = false;
  4006. if (ret > 0)
  4007. ret = 0;
  4008. return ret;
  4009. }
  4010. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4011. __u64 start, __u64 len, get_extent_t *get_extent)
  4012. {
  4013. int ret = 0;
  4014. u64 off = start;
  4015. u64 max = start + len;
  4016. u32 flags = 0;
  4017. u32 found_type;
  4018. u64 last;
  4019. u64 last_for_get_extent = 0;
  4020. u64 disko = 0;
  4021. u64 isize = i_size_read(inode);
  4022. struct btrfs_key found_key;
  4023. struct extent_map *em = NULL;
  4024. struct extent_state *cached_state = NULL;
  4025. struct btrfs_path *path;
  4026. struct btrfs_root *root = BTRFS_I(inode)->root;
  4027. struct fiemap_cache cache = { 0 };
  4028. int end = 0;
  4029. u64 em_start = 0;
  4030. u64 em_len = 0;
  4031. u64 em_end = 0;
  4032. if (len == 0)
  4033. return -EINVAL;
  4034. path = btrfs_alloc_path();
  4035. if (!path)
  4036. return -ENOMEM;
  4037. path->leave_spinning = 1;
  4038. start = round_down(start, btrfs_inode_sectorsize(inode));
  4039. len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
  4040. /*
  4041. * lookup the last file extent. We're not using i_size here
  4042. * because there might be preallocation past i_size
  4043. */
  4044. ret = btrfs_lookup_file_extent(NULL, root, path,
  4045. btrfs_ino(BTRFS_I(inode)), -1, 0);
  4046. if (ret < 0) {
  4047. btrfs_free_path(path);
  4048. return ret;
  4049. } else {
  4050. WARN_ON(!ret);
  4051. if (ret == 1)
  4052. ret = 0;
  4053. }
  4054. path->slots[0]--;
  4055. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  4056. found_type = found_key.type;
  4057. /* No extents, but there might be delalloc bits */
  4058. if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
  4059. found_type != BTRFS_EXTENT_DATA_KEY) {
  4060. /* have to trust i_size as the end */
  4061. last = (u64)-1;
  4062. last_for_get_extent = isize;
  4063. } else {
  4064. /*
  4065. * remember the start of the last extent. There are a
  4066. * bunch of different factors that go into the length of the
  4067. * extent, so its much less complex to remember where it started
  4068. */
  4069. last = found_key.offset;
  4070. last_for_get_extent = last + 1;
  4071. }
  4072. btrfs_release_path(path);
  4073. /*
  4074. * we might have some extents allocated but more delalloc past those
  4075. * extents. so, we trust isize unless the start of the last extent is
  4076. * beyond isize
  4077. */
  4078. if (last < isize) {
  4079. last = (u64)-1;
  4080. last_for_get_extent = isize;
  4081. }
  4082. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4083. &cached_state);
  4084. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  4085. get_extent);
  4086. if (!em)
  4087. goto out;
  4088. if (IS_ERR(em)) {
  4089. ret = PTR_ERR(em);
  4090. goto out;
  4091. }
  4092. while (!end) {
  4093. u64 offset_in_extent = 0;
  4094. /* break if the extent we found is outside the range */
  4095. if (em->start >= max || extent_map_end(em) < off)
  4096. break;
  4097. /*
  4098. * get_extent may return an extent that starts before our
  4099. * requested range. We have to make sure the ranges
  4100. * we return to fiemap always move forward and don't
  4101. * overlap, so adjust the offsets here
  4102. */
  4103. em_start = max(em->start, off);
  4104. /*
  4105. * record the offset from the start of the extent
  4106. * for adjusting the disk offset below. Only do this if the
  4107. * extent isn't compressed since our in ram offset may be past
  4108. * what we have actually allocated on disk.
  4109. */
  4110. if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4111. offset_in_extent = em_start - em->start;
  4112. em_end = extent_map_end(em);
  4113. em_len = em_end - em_start;
  4114. disko = 0;
  4115. flags = 0;
  4116. /*
  4117. * bump off for our next call to get_extent
  4118. */
  4119. off = extent_map_end(em);
  4120. if (off >= max)
  4121. end = 1;
  4122. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  4123. end = 1;
  4124. flags |= FIEMAP_EXTENT_LAST;
  4125. } else if (em->block_start == EXTENT_MAP_INLINE) {
  4126. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  4127. FIEMAP_EXTENT_NOT_ALIGNED);
  4128. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  4129. flags |= (FIEMAP_EXTENT_DELALLOC |
  4130. FIEMAP_EXTENT_UNKNOWN);
  4131. } else if (fieinfo->fi_extents_max) {
  4132. struct btrfs_trans_handle *trans;
  4133. u64 bytenr = em->block_start -
  4134. (em->start - em->orig_start);
  4135. disko = em->block_start + offset_in_extent;
  4136. /*
  4137. * We need a trans handle to get delayed refs
  4138. */
  4139. trans = btrfs_join_transaction(root);
  4140. /*
  4141. * It's OK if we can't start a trans we can still check
  4142. * from commit_root
  4143. */
  4144. if (IS_ERR(trans))
  4145. trans = NULL;
  4146. /*
  4147. * As btrfs supports shared space, this information
  4148. * can be exported to userspace tools via
  4149. * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
  4150. * then we're just getting a count and we can skip the
  4151. * lookup stuff.
  4152. */
  4153. ret = btrfs_check_shared(trans, root->fs_info,
  4154. root->objectid,
  4155. btrfs_ino(BTRFS_I(inode)), bytenr);
  4156. if (trans)
  4157. btrfs_end_transaction(trans);
  4158. if (ret < 0)
  4159. goto out_free;
  4160. if (ret)
  4161. flags |= FIEMAP_EXTENT_SHARED;
  4162. ret = 0;
  4163. }
  4164. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4165. flags |= FIEMAP_EXTENT_ENCODED;
  4166. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4167. flags |= FIEMAP_EXTENT_UNWRITTEN;
  4168. free_extent_map(em);
  4169. em = NULL;
  4170. if ((em_start >= last) || em_len == (u64)-1 ||
  4171. (last == (u64)-1 && isize <= em_end)) {
  4172. flags |= FIEMAP_EXTENT_LAST;
  4173. end = 1;
  4174. }
  4175. /* now scan forward to see if this is really the last extent. */
  4176. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  4177. get_extent);
  4178. if (IS_ERR(em)) {
  4179. ret = PTR_ERR(em);
  4180. goto out;
  4181. }
  4182. if (!em) {
  4183. flags |= FIEMAP_EXTENT_LAST;
  4184. end = 1;
  4185. }
  4186. ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
  4187. em_len, flags);
  4188. if (ret) {
  4189. if (ret == 1)
  4190. ret = 0;
  4191. goto out_free;
  4192. }
  4193. }
  4194. out_free:
  4195. if (!ret)
  4196. ret = check_fiemap_cache(root->fs_info, fieinfo, &cache);
  4197. free_extent_map(em);
  4198. out:
  4199. btrfs_free_path(path);
  4200. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4201. &cached_state, GFP_NOFS);
  4202. return ret;
  4203. }
  4204. static void __free_extent_buffer(struct extent_buffer *eb)
  4205. {
  4206. btrfs_leak_debug_del(&eb->leak_list);
  4207. kmem_cache_free(extent_buffer_cache, eb);
  4208. }
  4209. int extent_buffer_under_io(struct extent_buffer *eb)
  4210. {
  4211. return (atomic_read(&eb->io_pages) ||
  4212. test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  4213. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4214. }
  4215. /*
  4216. * Helper for releasing extent buffer page.
  4217. */
  4218. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
  4219. {
  4220. unsigned long index;
  4221. struct page *page;
  4222. int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4223. BUG_ON(extent_buffer_under_io(eb));
  4224. index = num_extent_pages(eb->start, eb->len);
  4225. if (index == 0)
  4226. return;
  4227. do {
  4228. index--;
  4229. page = eb->pages[index];
  4230. if (!page)
  4231. continue;
  4232. if (mapped)
  4233. spin_lock(&page->mapping->private_lock);
  4234. /*
  4235. * We do this since we'll remove the pages after we've
  4236. * removed the eb from the radix tree, so we could race
  4237. * and have this page now attached to the new eb. So
  4238. * only clear page_private if it's still connected to
  4239. * this eb.
  4240. */
  4241. if (PagePrivate(page) &&
  4242. page->private == (unsigned long)eb) {
  4243. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4244. BUG_ON(PageDirty(page));
  4245. BUG_ON(PageWriteback(page));
  4246. /*
  4247. * We need to make sure we haven't be attached
  4248. * to a new eb.
  4249. */
  4250. ClearPagePrivate(page);
  4251. set_page_private(page, 0);
  4252. /* One for the page private */
  4253. put_page(page);
  4254. }
  4255. if (mapped)
  4256. spin_unlock(&page->mapping->private_lock);
  4257. /* One for when we allocated the page */
  4258. put_page(page);
  4259. } while (index != 0);
  4260. }
  4261. /*
  4262. * Helper for releasing the extent buffer.
  4263. */
  4264. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  4265. {
  4266. btrfs_release_extent_buffer_page(eb);
  4267. __free_extent_buffer(eb);
  4268. }
  4269. static struct extent_buffer *
  4270. __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
  4271. unsigned long len)
  4272. {
  4273. struct extent_buffer *eb = NULL;
  4274. eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
  4275. eb->start = start;
  4276. eb->len = len;
  4277. eb->fs_info = fs_info;
  4278. eb->bflags = 0;
  4279. rwlock_init(&eb->lock);
  4280. atomic_set(&eb->write_locks, 0);
  4281. atomic_set(&eb->read_locks, 0);
  4282. atomic_set(&eb->blocking_readers, 0);
  4283. atomic_set(&eb->blocking_writers, 0);
  4284. atomic_set(&eb->spinning_readers, 0);
  4285. atomic_set(&eb->spinning_writers, 0);
  4286. eb->lock_nested = 0;
  4287. init_waitqueue_head(&eb->write_lock_wq);
  4288. init_waitqueue_head(&eb->read_lock_wq);
  4289. btrfs_leak_debug_add(&eb->leak_list, &buffers);
  4290. spin_lock_init(&eb->refs_lock);
  4291. atomic_set(&eb->refs, 1);
  4292. atomic_set(&eb->io_pages, 0);
  4293. /*
  4294. * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
  4295. */
  4296. BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
  4297. > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4298. BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4299. return eb;
  4300. }
  4301. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
  4302. {
  4303. unsigned long i;
  4304. struct page *p;
  4305. struct extent_buffer *new;
  4306. unsigned long num_pages = num_extent_pages(src->start, src->len);
  4307. new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
  4308. if (new == NULL)
  4309. return NULL;
  4310. for (i = 0; i < num_pages; i++) {
  4311. p = alloc_page(GFP_NOFS);
  4312. if (!p) {
  4313. btrfs_release_extent_buffer(new);
  4314. return NULL;
  4315. }
  4316. attach_extent_buffer_page(new, p);
  4317. WARN_ON(PageDirty(p));
  4318. SetPageUptodate(p);
  4319. new->pages[i] = p;
  4320. copy_page(page_address(p), page_address(src->pages[i]));
  4321. }
  4322. set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
  4323. set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
  4324. return new;
  4325. }
  4326. struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4327. u64 start, unsigned long len)
  4328. {
  4329. struct extent_buffer *eb;
  4330. unsigned long num_pages;
  4331. unsigned long i;
  4332. num_pages = num_extent_pages(start, len);
  4333. eb = __alloc_extent_buffer(fs_info, start, len);
  4334. if (!eb)
  4335. return NULL;
  4336. for (i = 0; i < num_pages; i++) {
  4337. eb->pages[i] = alloc_page(GFP_NOFS);
  4338. if (!eb->pages[i])
  4339. goto err;
  4340. }
  4341. set_extent_buffer_uptodate(eb);
  4342. btrfs_set_header_nritems(eb, 0);
  4343. set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4344. return eb;
  4345. err:
  4346. for (; i > 0; i--)
  4347. __free_page(eb->pages[i - 1]);
  4348. __free_extent_buffer(eb);
  4349. return NULL;
  4350. }
  4351. struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4352. u64 start)
  4353. {
  4354. return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
  4355. }
  4356. static void check_buffer_tree_ref(struct extent_buffer *eb)
  4357. {
  4358. int refs;
  4359. /* the ref bit is tricky. We have to make sure it is set
  4360. * if we have the buffer dirty. Otherwise the
  4361. * code to free a buffer can end up dropping a dirty
  4362. * page
  4363. *
  4364. * Once the ref bit is set, it won't go away while the
  4365. * buffer is dirty or in writeback, and it also won't
  4366. * go away while we have the reference count on the
  4367. * eb bumped.
  4368. *
  4369. * We can't just set the ref bit without bumping the
  4370. * ref on the eb because free_extent_buffer might
  4371. * see the ref bit and try to clear it. If this happens
  4372. * free_extent_buffer might end up dropping our original
  4373. * ref by mistake and freeing the page before we are able
  4374. * to add one more ref.
  4375. *
  4376. * So bump the ref count first, then set the bit. If someone
  4377. * beat us to it, drop the ref we added.
  4378. */
  4379. refs = atomic_read(&eb->refs);
  4380. if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4381. return;
  4382. spin_lock(&eb->refs_lock);
  4383. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4384. atomic_inc(&eb->refs);
  4385. spin_unlock(&eb->refs_lock);
  4386. }
  4387. static void mark_extent_buffer_accessed(struct extent_buffer *eb,
  4388. struct page *accessed)
  4389. {
  4390. unsigned long num_pages, i;
  4391. check_buffer_tree_ref(eb);
  4392. num_pages = num_extent_pages(eb->start, eb->len);
  4393. for (i = 0; i < num_pages; i++) {
  4394. struct page *p = eb->pages[i];
  4395. if (p != accessed)
  4396. mark_page_accessed(p);
  4397. }
  4398. }
  4399. struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
  4400. u64 start)
  4401. {
  4402. struct extent_buffer *eb;
  4403. rcu_read_lock();
  4404. eb = radix_tree_lookup(&fs_info->buffer_radix,
  4405. start >> PAGE_SHIFT);
  4406. if (eb && atomic_inc_not_zero(&eb->refs)) {
  4407. rcu_read_unlock();
  4408. /*
  4409. * Lock our eb's refs_lock to avoid races with
  4410. * free_extent_buffer. When we get our eb it might be flagged
  4411. * with EXTENT_BUFFER_STALE and another task running
  4412. * free_extent_buffer might have seen that flag set,
  4413. * eb->refs == 2, that the buffer isn't under IO (dirty and
  4414. * writeback flags not set) and it's still in the tree (flag
  4415. * EXTENT_BUFFER_TREE_REF set), therefore being in the process
  4416. * of decrementing the extent buffer's reference count twice.
  4417. * So here we could race and increment the eb's reference count,
  4418. * clear its stale flag, mark it as dirty and drop our reference
  4419. * before the other task finishes executing free_extent_buffer,
  4420. * which would later result in an attempt to free an extent
  4421. * buffer that is dirty.
  4422. */
  4423. if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
  4424. spin_lock(&eb->refs_lock);
  4425. spin_unlock(&eb->refs_lock);
  4426. }
  4427. mark_extent_buffer_accessed(eb, NULL);
  4428. return eb;
  4429. }
  4430. rcu_read_unlock();
  4431. return NULL;
  4432. }
  4433. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4434. struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
  4435. u64 start)
  4436. {
  4437. struct extent_buffer *eb, *exists = NULL;
  4438. int ret;
  4439. eb = find_extent_buffer(fs_info, start);
  4440. if (eb)
  4441. return eb;
  4442. eb = alloc_dummy_extent_buffer(fs_info, start);
  4443. if (!eb)
  4444. return NULL;
  4445. eb->fs_info = fs_info;
  4446. again:
  4447. ret = radix_tree_preload(GFP_NOFS);
  4448. if (ret)
  4449. goto free_eb;
  4450. spin_lock(&fs_info->buffer_lock);
  4451. ret = radix_tree_insert(&fs_info->buffer_radix,
  4452. start >> PAGE_SHIFT, eb);
  4453. spin_unlock(&fs_info->buffer_lock);
  4454. radix_tree_preload_end();
  4455. if (ret == -EEXIST) {
  4456. exists = find_extent_buffer(fs_info, start);
  4457. if (exists)
  4458. goto free_eb;
  4459. else
  4460. goto again;
  4461. }
  4462. check_buffer_tree_ref(eb);
  4463. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4464. /*
  4465. * We will free dummy extent buffer's if they come into
  4466. * free_extent_buffer with a ref count of 2, but if we are using this we
  4467. * want the buffers to stay in memory until we're done with them, so
  4468. * bump the ref count again.
  4469. */
  4470. atomic_inc(&eb->refs);
  4471. return eb;
  4472. free_eb:
  4473. btrfs_release_extent_buffer(eb);
  4474. return exists;
  4475. }
  4476. #endif
  4477. struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
  4478. u64 start)
  4479. {
  4480. unsigned long len = fs_info->nodesize;
  4481. unsigned long num_pages = num_extent_pages(start, len);
  4482. unsigned long i;
  4483. unsigned long index = start >> PAGE_SHIFT;
  4484. struct extent_buffer *eb;
  4485. struct extent_buffer *exists = NULL;
  4486. struct page *p;
  4487. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  4488. int uptodate = 1;
  4489. int ret;
  4490. if (!IS_ALIGNED(start, fs_info->sectorsize)) {
  4491. btrfs_err(fs_info, "bad tree block start %llu", start);
  4492. return ERR_PTR(-EINVAL);
  4493. }
  4494. eb = find_extent_buffer(fs_info, start);
  4495. if (eb)
  4496. return eb;
  4497. eb = __alloc_extent_buffer(fs_info, start, len);
  4498. if (!eb)
  4499. return ERR_PTR(-ENOMEM);
  4500. for (i = 0; i < num_pages; i++, index++) {
  4501. p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
  4502. if (!p) {
  4503. exists = ERR_PTR(-ENOMEM);
  4504. goto free_eb;
  4505. }
  4506. spin_lock(&mapping->private_lock);
  4507. if (PagePrivate(p)) {
  4508. /*
  4509. * We could have already allocated an eb for this page
  4510. * and attached one so lets see if we can get a ref on
  4511. * the existing eb, and if we can we know it's good and
  4512. * we can just return that one, else we know we can just
  4513. * overwrite page->private.
  4514. */
  4515. exists = (struct extent_buffer *)p->private;
  4516. if (atomic_inc_not_zero(&exists->refs)) {
  4517. spin_unlock(&mapping->private_lock);
  4518. unlock_page(p);
  4519. put_page(p);
  4520. mark_extent_buffer_accessed(exists, p);
  4521. goto free_eb;
  4522. }
  4523. exists = NULL;
  4524. /*
  4525. * Do this so attach doesn't complain and we need to
  4526. * drop the ref the old guy had.
  4527. */
  4528. ClearPagePrivate(p);
  4529. WARN_ON(PageDirty(p));
  4530. put_page(p);
  4531. }
  4532. attach_extent_buffer_page(eb, p);
  4533. spin_unlock(&mapping->private_lock);
  4534. WARN_ON(PageDirty(p));
  4535. eb->pages[i] = p;
  4536. if (!PageUptodate(p))
  4537. uptodate = 0;
  4538. /*
  4539. * see below about how we avoid a nasty race with release page
  4540. * and why we unlock later
  4541. */
  4542. }
  4543. if (uptodate)
  4544. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4545. again:
  4546. ret = radix_tree_preload(GFP_NOFS);
  4547. if (ret) {
  4548. exists = ERR_PTR(ret);
  4549. goto free_eb;
  4550. }
  4551. spin_lock(&fs_info->buffer_lock);
  4552. ret = radix_tree_insert(&fs_info->buffer_radix,
  4553. start >> PAGE_SHIFT, eb);
  4554. spin_unlock(&fs_info->buffer_lock);
  4555. radix_tree_preload_end();
  4556. if (ret == -EEXIST) {
  4557. exists = find_extent_buffer(fs_info, start);
  4558. if (exists)
  4559. goto free_eb;
  4560. else
  4561. goto again;
  4562. }
  4563. /* add one reference for the tree */
  4564. check_buffer_tree_ref(eb);
  4565. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4566. /*
  4567. * there is a race where release page may have
  4568. * tried to find this extent buffer in the radix
  4569. * but failed. It will tell the VM it is safe to
  4570. * reclaim the, and it will clear the page private bit.
  4571. * We must make sure to set the page private bit properly
  4572. * after the extent buffer is in the radix tree so
  4573. * it doesn't get lost
  4574. */
  4575. SetPageChecked(eb->pages[0]);
  4576. for (i = 1; i < num_pages; i++) {
  4577. p = eb->pages[i];
  4578. ClearPageChecked(p);
  4579. unlock_page(p);
  4580. }
  4581. unlock_page(eb->pages[0]);
  4582. return eb;
  4583. free_eb:
  4584. WARN_ON(!atomic_dec_and_test(&eb->refs));
  4585. for (i = 0; i < num_pages; i++) {
  4586. if (eb->pages[i])
  4587. unlock_page(eb->pages[i]);
  4588. }
  4589. btrfs_release_extent_buffer(eb);
  4590. return exists;
  4591. }
  4592. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  4593. {
  4594. struct extent_buffer *eb =
  4595. container_of(head, struct extent_buffer, rcu_head);
  4596. __free_extent_buffer(eb);
  4597. }
  4598. /* Expects to have eb->eb_lock already held */
  4599. static int release_extent_buffer(struct extent_buffer *eb)
  4600. {
  4601. WARN_ON(atomic_read(&eb->refs) == 0);
  4602. if (atomic_dec_and_test(&eb->refs)) {
  4603. if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
  4604. struct btrfs_fs_info *fs_info = eb->fs_info;
  4605. spin_unlock(&eb->refs_lock);
  4606. spin_lock(&fs_info->buffer_lock);
  4607. radix_tree_delete(&fs_info->buffer_radix,
  4608. eb->start >> PAGE_SHIFT);
  4609. spin_unlock(&fs_info->buffer_lock);
  4610. } else {
  4611. spin_unlock(&eb->refs_lock);
  4612. }
  4613. /* Should be safe to release our pages at this point */
  4614. btrfs_release_extent_buffer_page(eb);
  4615. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4616. if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
  4617. __free_extent_buffer(eb);
  4618. return 1;
  4619. }
  4620. #endif
  4621. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  4622. return 1;
  4623. }
  4624. spin_unlock(&eb->refs_lock);
  4625. return 0;
  4626. }
  4627. void free_extent_buffer(struct extent_buffer *eb)
  4628. {
  4629. int refs;
  4630. int old;
  4631. if (!eb)
  4632. return;
  4633. while (1) {
  4634. refs = atomic_read(&eb->refs);
  4635. if (refs <= 3)
  4636. break;
  4637. old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
  4638. if (old == refs)
  4639. return;
  4640. }
  4641. spin_lock(&eb->refs_lock);
  4642. if (atomic_read(&eb->refs) == 2 &&
  4643. test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
  4644. atomic_dec(&eb->refs);
  4645. if (atomic_read(&eb->refs) == 2 &&
  4646. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  4647. !extent_buffer_under_io(eb) &&
  4648. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4649. atomic_dec(&eb->refs);
  4650. /*
  4651. * I know this is terrible, but it's temporary until we stop tracking
  4652. * the uptodate bits and such for the extent buffers.
  4653. */
  4654. release_extent_buffer(eb);
  4655. }
  4656. void free_extent_buffer_stale(struct extent_buffer *eb)
  4657. {
  4658. if (!eb)
  4659. return;
  4660. spin_lock(&eb->refs_lock);
  4661. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  4662. if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  4663. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4664. atomic_dec(&eb->refs);
  4665. release_extent_buffer(eb);
  4666. }
  4667. void clear_extent_buffer_dirty(struct extent_buffer *eb)
  4668. {
  4669. unsigned long i;
  4670. unsigned long num_pages;
  4671. struct page *page;
  4672. num_pages = num_extent_pages(eb->start, eb->len);
  4673. for (i = 0; i < num_pages; i++) {
  4674. page = eb->pages[i];
  4675. if (!PageDirty(page))
  4676. continue;
  4677. lock_page(page);
  4678. WARN_ON(!PagePrivate(page));
  4679. clear_page_dirty_for_io(page);
  4680. spin_lock_irq(&page->mapping->tree_lock);
  4681. if (!PageDirty(page)) {
  4682. radix_tree_tag_clear(&page->mapping->page_tree,
  4683. page_index(page),
  4684. PAGECACHE_TAG_DIRTY);
  4685. }
  4686. spin_unlock_irq(&page->mapping->tree_lock);
  4687. ClearPageError(page);
  4688. unlock_page(page);
  4689. }
  4690. WARN_ON(atomic_read(&eb->refs) == 0);
  4691. }
  4692. int set_extent_buffer_dirty(struct extent_buffer *eb)
  4693. {
  4694. unsigned long i;
  4695. unsigned long num_pages;
  4696. int was_dirty = 0;
  4697. check_buffer_tree_ref(eb);
  4698. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  4699. num_pages = num_extent_pages(eb->start, eb->len);
  4700. WARN_ON(atomic_read(&eb->refs) == 0);
  4701. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  4702. for (i = 0; i < num_pages; i++)
  4703. set_page_dirty(eb->pages[i]);
  4704. return was_dirty;
  4705. }
  4706. void clear_extent_buffer_uptodate(struct extent_buffer *eb)
  4707. {
  4708. unsigned long i;
  4709. struct page *page;
  4710. unsigned long num_pages;
  4711. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4712. num_pages = num_extent_pages(eb->start, eb->len);
  4713. for (i = 0; i < num_pages; i++) {
  4714. page = eb->pages[i];
  4715. if (page)
  4716. ClearPageUptodate(page);
  4717. }
  4718. }
  4719. void set_extent_buffer_uptodate(struct extent_buffer *eb)
  4720. {
  4721. unsigned long i;
  4722. struct page *page;
  4723. unsigned long num_pages;
  4724. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4725. num_pages = num_extent_pages(eb->start, eb->len);
  4726. for (i = 0; i < num_pages; i++) {
  4727. page = eb->pages[i];
  4728. SetPageUptodate(page);
  4729. }
  4730. }
  4731. int extent_buffer_uptodate(struct extent_buffer *eb)
  4732. {
  4733. return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4734. }
  4735. int read_extent_buffer_pages(struct extent_io_tree *tree,
  4736. struct extent_buffer *eb, int wait,
  4737. get_extent_t *get_extent, int mirror_num)
  4738. {
  4739. unsigned long i;
  4740. struct page *page;
  4741. int err;
  4742. int ret = 0;
  4743. int locked_pages = 0;
  4744. int all_uptodate = 1;
  4745. unsigned long num_pages;
  4746. unsigned long num_reads = 0;
  4747. struct bio *bio = NULL;
  4748. unsigned long bio_flags = 0;
  4749. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  4750. return 0;
  4751. num_pages = num_extent_pages(eb->start, eb->len);
  4752. for (i = 0; i < num_pages; i++) {
  4753. page = eb->pages[i];
  4754. if (wait == WAIT_NONE) {
  4755. if (!trylock_page(page))
  4756. goto unlock_exit;
  4757. } else {
  4758. lock_page(page);
  4759. }
  4760. locked_pages++;
  4761. }
  4762. /*
  4763. * We need to firstly lock all pages to make sure that
  4764. * the uptodate bit of our pages won't be affected by
  4765. * clear_extent_buffer_uptodate().
  4766. */
  4767. for (i = 0; i < num_pages; i++) {
  4768. page = eb->pages[i];
  4769. if (!PageUptodate(page)) {
  4770. num_reads++;
  4771. all_uptodate = 0;
  4772. }
  4773. }
  4774. if (all_uptodate) {
  4775. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4776. goto unlock_exit;
  4777. }
  4778. clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
  4779. eb->read_mirror = 0;
  4780. atomic_set(&eb->io_pages, num_reads);
  4781. for (i = 0; i < num_pages; i++) {
  4782. page = eb->pages[i];
  4783. if (!PageUptodate(page)) {
  4784. if (ret) {
  4785. atomic_dec(&eb->io_pages);
  4786. unlock_page(page);
  4787. continue;
  4788. }
  4789. ClearPageError(page);
  4790. err = __extent_read_full_page(tree, page,
  4791. get_extent, &bio,
  4792. mirror_num, &bio_flags,
  4793. REQ_META);
  4794. if (err) {
  4795. ret = err;
  4796. /*
  4797. * We use &bio in above __extent_read_full_page,
  4798. * so we ensure that if it returns error, the
  4799. * current page fails to add itself to bio and
  4800. * it's been unlocked.
  4801. *
  4802. * We must dec io_pages by ourselves.
  4803. */
  4804. atomic_dec(&eb->io_pages);
  4805. }
  4806. } else {
  4807. unlock_page(page);
  4808. }
  4809. }
  4810. if (bio) {
  4811. err = submit_one_bio(bio, mirror_num, bio_flags);
  4812. if (err)
  4813. return err;
  4814. }
  4815. if (ret || wait != WAIT_COMPLETE)
  4816. return ret;
  4817. for (i = 0; i < num_pages; i++) {
  4818. page = eb->pages[i];
  4819. wait_on_page_locked(page);
  4820. if (!PageUptodate(page))
  4821. ret = -EIO;
  4822. }
  4823. return ret;
  4824. unlock_exit:
  4825. while (locked_pages > 0) {
  4826. locked_pages--;
  4827. page = eb->pages[locked_pages];
  4828. unlock_page(page);
  4829. }
  4830. return ret;
  4831. }
  4832. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  4833. unsigned long start,
  4834. unsigned long len)
  4835. {
  4836. size_t cur;
  4837. size_t offset;
  4838. struct page *page;
  4839. char *kaddr;
  4840. char *dst = (char *)dstv;
  4841. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4842. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4843. WARN_ON(start > eb->len);
  4844. WARN_ON(start + len > eb->start + eb->len);
  4845. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4846. while (len > 0) {
  4847. page = eb->pages[i];
  4848. cur = min(len, (PAGE_SIZE - offset));
  4849. kaddr = page_address(page);
  4850. memcpy(dst, kaddr + offset, cur);
  4851. dst += cur;
  4852. len -= cur;
  4853. offset = 0;
  4854. i++;
  4855. }
  4856. }
  4857. int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
  4858. unsigned long start,
  4859. unsigned long len)
  4860. {
  4861. size_t cur;
  4862. size_t offset;
  4863. struct page *page;
  4864. char *kaddr;
  4865. char __user *dst = (char __user *)dstv;
  4866. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4867. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4868. int ret = 0;
  4869. WARN_ON(start > eb->len);
  4870. WARN_ON(start + len > eb->start + eb->len);
  4871. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4872. while (len > 0) {
  4873. page = eb->pages[i];
  4874. cur = min(len, (PAGE_SIZE - offset));
  4875. kaddr = page_address(page);
  4876. if (copy_to_user(dst, kaddr + offset, cur)) {
  4877. ret = -EFAULT;
  4878. break;
  4879. }
  4880. dst += cur;
  4881. len -= cur;
  4882. offset = 0;
  4883. i++;
  4884. }
  4885. return ret;
  4886. }
  4887. /*
  4888. * return 0 if the item is found within a page.
  4889. * return 1 if the item spans two pages.
  4890. * return -EINVAL otherwise.
  4891. */
  4892. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4893. unsigned long min_len, char **map,
  4894. unsigned long *map_start,
  4895. unsigned long *map_len)
  4896. {
  4897. size_t offset = start & (PAGE_SIZE - 1);
  4898. char *kaddr;
  4899. struct page *p;
  4900. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4901. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4902. unsigned long end_i = (start_offset + start + min_len - 1) >>
  4903. PAGE_SHIFT;
  4904. if (i != end_i)
  4905. return 1;
  4906. if (i == 0) {
  4907. offset = start_offset;
  4908. *map_start = 0;
  4909. } else {
  4910. offset = 0;
  4911. *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
  4912. }
  4913. if (start + min_len > eb->len) {
  4914. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
  4915. eb->start, eb->len, start, min_len);
  4916. return -EINVAL;
  4917. }
  4918. p = eb->pages[i];
  4919. kaddr = page_address(p);
  4920. *map = kaddr + offset;
  4921. *map_len = PAGE_SIZE - offset;
  4922. return 0;
  4923. }
  4924. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  4925. unsigned long start,
  4926. unsigned long len)
  4927. {
  4928. size_t cur;
  4929. size_t offset;
  4930. struct page *page;
  4931. char *kaddr;
  4932. char *ptr = (char *)ptrv;
  4933. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4934. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4935. int ret = 0;
  4936. WARN_ON(start > eb->len);
  4937. WARN_ON(start + len > eb->start + eb->len);
  4938. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4939. while (len > 0) {
  4940. page = eb->pages[i];
  4941. cur = min(len, (PAGE_SIZE - offset));
  4942. kaddr = page_address(page);
  4943. ret = memcmp(ptr, kaddr + offset, cur);
  4944. if (ret)
  4945. break;
  4946. ptr += cur;
  4947. len -= cur;
  4948. offset = 0;
  4949. i++;
  4950. }
  4951. return ret;
  4952. }
  4953. void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
  4954. const void *srcv)
  4955. {
  4956. char *kaddr;
  4957. WARN_ON(!PageUptodate(eb->pages[0]));
  4958. kaddr = page_address(eb->pages[0]);
  4959. memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
  4960. BTRFS_FSID_SIZE);
  4961. }
  4962. void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
  4963. {
  4964. char *kaddr;
  4965. WARN_ON(!PageUptodate(eb->pages[0]));
  4966. kaddr = page_address(eb->pages[0]);
  4967. memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
  4968. BTRFS_FSID_SIZE);
  4969. }
  4970. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  4971. unsigned long start, unsigned long len)
  4972. {
  4973. size_t cur;
  4974. size_t offset;
  4975. struct page *page;
  4976. char *kaddr;
  4977. char *src = (char *)srcv;
  4978. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4979. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4980. WARN_ON(start > eb->len);
  4981. WARN_ON(start + len > eb->start + eb->len);
  4982. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4983. while (len > 0) {
  4984. page = eb->pages[i];
  4985. WARN_ON(!PageUptodate(page));
  4986. cur = min(len, PAGE_SIZE - offset);
  4987. kaddr = page_address(page);
  4988. memcpy(kaddr + offset, src, cur);
  4989. src += cur;
  4990. len -= cur;
  4991. offset = 0;
  4992. i++;
  4993. }
  4994. }
  4995. void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4996. unsigned long len)
  4997. {
  4998. size_t cur;
  4999. size_t offset;
  5000. struct page *page;
  5001. char *kaddr;
  5002. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  5003. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  5004. WARN_ON(start > eb->len);
  5005. WARN_ON(start + len > eb->start + eb->len);
  5006. offset = (start_offset + start) & (PAGE_SIZE - 1);
  5007. while (len > 0) {
  5008. page = eb->pages[i];
  5009. WARN_ON(!PageUptodate(page));
  5010. cur = min(len, PAGE_SIZE - offset);
  5011. kaddr = page_address(page);
  5012. memset(kaddr + offset, 0, cur);
  5013. len -= cur;
  5014. offset = 0;
  5015. i++;
  5016. }
  5017. }
  5018. void copy_extent_buffer_full(struct extent_buffer *dst,
  5019. struct extent_buffer *src)
  5020. {
  5021. int i;
  5022. unsigned num_pages;
  5023. ASSERT(dst->len == src->len);
  5024. num_pages = num_extent_pages(dst->start, dst->len);
  5025. for (i = 0; i < num_pages; i++)
  5026. copy_page(page_address(dst->pages[i]),
  5027. page_address(src->pages[i]));
  5028. }
  5029. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  5030. unsigned long dst_offset, unsigned long src_offset,
  5031. unsigned long len)
  5032. {
  5033. u64 dst_len = dst->len;
  5034. size_t cur;
  5035. size_t offset;
  5036. struct page *page;
  5037. char *kaddr;
  5038. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5039. unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
  5040. WARN_ON(src->len != dst_len);
  5041. offset = (start_offset + dst_offset) &
  5042. (PAGE_SIZE - 1);
  5043. while (len > 0) {
  5044. page = dst->pages[i];
  5045. WARN_ON(!PageUptodate(page));
  5046. cur = min(len, (unsigned long)(PAGE_SIZE - offset));
  5047. kaddr = page_address(page);
  5048. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  5049. src_offset += cur;
  5050. len -= cur;
  5051. offset = 0;
  5052. i++;
  5053. }
  5054. }
  5055. void le_bitmap_set(u8 *map, unsigned int start, int len)
  5056. {
  5057. u8 *p = map + BIT_BYTE(start);
  5058. const unsigned int size = start + len;
  5059. int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
  5060. u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
  5061. while (len - bits_to_set >= 0) {
  5062. *p |= mask_to_set;
  5063. len -= bits_to_set;
  5064. bits_to_set = BITS_PER_BYTE;
  5065. mask_to_set = ~0;
  5066. p++;
  5067. }
  5068. if (len) {
  5069. mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
  5070. *p |= mask_to_set;
  5071. }
  5072. }
  5073. void le_bitmap_clear(u8 *map, unsigned int start, int len)
  5074. {
  5075. u8 *p = map + BIT_BYTE(start);
  5076. const unsigned int size = start + len;
  5077. int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
  5078. u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
  5079. while (len - bits_to_clear >= 0) {
  5080. *p &= ~mask_to_clear;
  5081. len -= bits_to_clear;
  5082. bits_to_clear = BITS_PER_BYTE;
  5083. mask_to_clear = ~0;
  5084. p++;
  5085. }
  5086. if (len) {
  5087. mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
  5088. *p &= ~mask_to_clear;
  5089. }
  5090. }
  5091. /*
  5092. * eb_bitmap_offset() - calculate the page and offset of the byte containing the
  5093. * given bit number
  5094. * @eb: the extent buffer
  5095. * @start: offset of the bitmap item in the extent buffer
  5096. * @nr: bit number
  5097. * @page_index: return index of the page in the extent buffer that contains the
  5098. * given bit number
  5099. * @page_offset: return offset into the page given by page_index
  5100. *
  5101. * This helper hides the ugliness of finding the byte in an extent buffer which
  5102. * contains a given bit.
  5103. */
  5104. static inline void eb_bitmap_offset(struct extent_buffer *eb,
  5105. unsigned long start, unsigned long nr,
  5106. unsigned long *page_index,
  5107. size_t *page_offset)
  5108. {
  5109. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  5110. size_t byte_offset = BIT_BYTE(nr);
  5111. size_t offset;
  5112. /*
  5113. * The byte we want is the offset of the extent buffer + the offset of
  5114. * the bitmap item in the extent buffer + the offset of the byte in the
  5115. * bitmap item.
  5116. */
  5117. offset = start_offset + start + byte_offset;
  5118. *page_index = offset >> PAGE_SHIFT;
  5119. *page_offset = offset & (PAGE_SIZE - 1);
  5120. }
  5121. /**
  5122. * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
  5123. * @eb: the extent buffer
  5124. * @start: offset of the bitmap item in the extent buffer
  5125. * @nr: bit number to test
  5126. */
  5127. int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
  5128. unsigned long nr)
  5129. {
  5130. u8 *kaddr;
  5131. struct page *page;
  5132. unsigned long i;
  5133. size_t offset;
  5134. eb_bitmap_offset(eb, start, nr, &i, &offset);
  5135. page = eb->pages[i];
  5136. WARN_ON(!PageUptodate(page));
  5137. kaddr = page_address(page);
  5138. return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
  5139. }
  5140. /**
  5141. * extent_buffer_bitmap_set - set an area of a bitmap
  5142. * @eb: the extent buffer
  5143. * @start: offset of the bitmap item in the extent buffer
  5144. * @pos: bit number of the first bit
  5145. * @len: number of bits to set
  5146. */
  5147. void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
  5148. unsigned long pos, unsigned long len)
  5149. {
  5150. u8 *kaddr;
  5151. struct page *page;
  5152. unsigned long i;
  5153. size_t offset;
  5154. const unsigned int size = pos + len;
  5155. int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
  5156. u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
  5157. eb_bitmap_offset(eb, start, pos, &i, &offset);
  5158. page = eb->pages[i];
  5159. WARN_ON(!PageUptodate(page));
  5160. kaddr = page_address(page);
  5161. while (len >= bits_to_set) {
  5162. kaddr[offset] |= mask_to_set;
  5163. len -= bits_to_set;
  5164. bits_to_set = BITS_PER_BYTE;
  5165. mask_to_set = ~0;
  5166. if (++offset >= PAGE_SIZE && len > 0) {
  5167. offset = 0;
  5168. page = eb->pages[++i];
  5169. WARN_ON(!PageUptodate(page));
  5170. kaddr = page_address(page);
  5171. }
  5172. }
  5173. if (len) {
  5174. mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
  5175. kaddr[offset] |= mask_to_set;
  5176. }
  5177. }
  5178. /**
  5179. * extent_buffer_bitmap_clear - clear an area of a bitmap
  5180. * @eb: the extent buffer
  5181. * @start: offset of the bitmap item in the extent buffer
  5182. * @pos: bit number of the first bit
  5183. * @len: number of bits to clear
  5184. */
  5185. void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
  5186. unsigned long pos, unsigned long len)
  5187. {
  5188. u8 *kaddr;
  5189. struct page *page;
  5190. unsigned long i;
  5191. size_t offset;
  5192. const unsigned int size = pos + len;
  5193. int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
  5194. u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
  5195. eb_bitmap_offset(eb, start, pos, &i, &offset);
  5196. page = eb->pages[i];
  5197. WARN_ON(!PageUptodate(page));
  5198. kaddr = page_address(page);
  5199. while (len >= bits_to_clear) {
  5200. kaddr[offset] &= ~mask_to_clear;
  5201. len -= bits_to_clear;
  5202. bits_to_clear = BITS_PER_BYTE;
  5203. mask_to_clear = ~0;
  5204. if (++offset >= PAGE_SIZE && len > 0) {
  5205. offset = 0;
  5206. page = eb->pages[++i];
  5207. WARN_ON(!PageUptodate(page));
  5208. kaddr = page_address(page);
  5209. }
  5210. }
  5211. if (len) {
  5212. mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
  5213. kaddr[offset] &= ~mask_to_clear;
  5214. }
  5215. }
  5216. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  5217. {
  5218. unsigned long distance = (src > dst) ? src - dst : dst - src;
  5219. return distance < len;
  5220. }
  5221. static void copy_pages(struct page *dst_page, struct page *src_page,
  5222. unsigned long dst_off, unsigned long src_off,
  5223. unsigned long len)
  5224. {
  5225. char *dst_kaddr = page_address(dst_page);
  5226. char *src_kaddr;
  5227. int must_memmove = 0;
  5228. if (dst_page != src_page) {
  5229. src_kaddr = page_address(src_page);
  5230. } else {
  5231. src_kaddr = dst_kaddr;
  5232. if (areas_overlap(src_off, dst_off, len))
  5233. must_memmove = 1;
  5234. }
  5235. if (must_memmove)
  5236. memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
  5237. else
  5238. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  5239. }
  5240. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  5241. unsigned long src_offset, unsigned long len)
  5242. {
  5243. struct btrfs_fs_info *fs_info = dst->fs_info;
  5244. size_t cur;
  5245. size_t dst_off_in_page;
  5246. size_t src_off_in_page;
  5247. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5248. unsigned long dst_i;
  5249. unsigned long src_i;
  5250. if (src_offset + len > dst->len) {
  5251. btrfs_err(fs_info,
  5252. "memmove bogus src_offset %lu move len %lu dst len %lu",
  5253. src_offset, len, dst->len);
  5254. BUG_ON(1);
  5255. }
  5256. if (dst_offset + len > dst->len) {
  5257. btrfs_err(fs_info,
  5258. "memmove bogus dst_offset %lu move len %lu dst len %lu",
  5259. dst_offset, len, dst->len);
  5260. BUG_ON(1);
  5261. }
  5262. while (len > 0) {
  5263. dst_off_in_page = (start_offset + dst_offset) &
  5264. (PAGE_SIZE - 1);
  5265. src_off_in_page = (start_offset + src_offset) &
  5266. (PAGE_SIZE - 1);
  5267. dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
  5268. src_i = (start_offset + src_offset) >> PAGE_SHIFT;
  5269. cur = min(len, (unsigned long)(PAGE_SIZE -
  5270. src_off_in_page));
  5271. cur = min_t(unsigned long, cur,
  5272. (unsigned long)(PAGE_SIZE - dst_off_in_page));
  5273. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  5274. dst_off_in_page, src_off_in_page, cur);
  5275. src_offset += cur;
  5276. dst_offset += cur;
  5277. len -= cur;
  5278. }
  5279. }
  5280. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  5281. unsigned long src_offset, unsigned long len)
  5282. {
  5283. struct btrfs_fs_info *fs_info = dst->fs_info;
  5284. size_t cur;
  5285. size_t dst_off_in_page;
  5286. size_t src_off_in_page;
  5287. unsigned long dst_end = dst_offset + len - 1;
  5288. unsigned long src_end = src_offset + len - 1;
  5289. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5290. unsigned long dst_i;
  5291. unsigned long src_i;
  5292. if (src_offset + len > dst->len) {
  5293. btrfs_err(fs_info,
  5294. "memmove bogus src_offset %lu move len %lu len %lu",
  5295. src_offset, len, dst->len);
  5296. BUG_ON(1);
  5297. }
  5298. if (dst_offset + len > dst->len) {
  5299. btrfs_err(fs_info,
  5300. "memmove bogus dst_offset %lu move len %lu len %lu",
  5301. dst_offset, len, dst->len);
  5302. BUG_ON(1);
  5303. }
  5304. if (dst_offset < src_offset) {
  5305. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  5306. return;
  5307. }
  5308. while (len > 0) {
  5309. dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
  5310. src_i = (start_offset + src_end) >> PAGE_SHIFT;
  5311. dst_off_in_page = (start_offset + dst_end) &
  5312. (PAGE_SIZE - 1);
  5313. src_off_in_page = (start_offset + src_end) &
  5314. (PAGE_SIZE - 1);
  5315. cur = min_t(unsigned long, len, src_off_in_page + 1);
  5316. cur = min(cur, dst_off_in_page + 1);
  5317. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  5318. dst_off_in_page - cur + 1,
  5319. src_off_in_page - cur + 1, cur);
  5320. dst_end -= cur;
  5321. src_end -= cur;
  5322. len -= cur;
  5323. }
  5324. }
  5325. int try_release_extent_buffer(struct page *page)
  5326. {
  5327. struct extent_buffer *eb;
  5328. /*
  5329. * We need to make sure nobody is attaching this page to an eb right
  5330. * now.
  5331. */
  5332. spin_lock(&page->mapping->private_lock);
  5333. if (!PagePrivate(page)) {
  5334. spin_unlock(&page->mapping->private_lock);
  5335. return 1;
  5336. }
  5337. eb = (struct extent_buffer *)page->private;
  5338. BUG_ON(!eb);
  5339. /*
  5340. * This is a little awful but should be ok, we need to make sure that
  5341. * the eb doesn't disappear out from under us while we're looking at
  5342. * this page.
  5343. */
  5344. spin_lock(&eb->refs_lock);
  5345. if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  5346. spin_unlock(&eb->refs_lock);
  5347. spin_unlock(&page->mapping->private_lock);
  5348. return 0;
  5349. }
  5350. spin_unlock(&page->mapping->private_lock);
  5351. /*
  5352. * If tree ref isn't set then we know the ref on this eb is a real ref,
  5353. * so just return, this page will likely be freed soon anyway.
  5354. */
  5355. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  5356. spin_unlock(&eb->refs_lock);
  5357. return 0;
  5358. }
  5359. return release_extent_buffer(eb);
  5360. }