extent_io.c 149 KB

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