extent_io.c 152 KB

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