extent_io.c 151 KB

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