extents.c 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  4. * Written by Alex Tomas <alex@clusterfs.com>
  5. *
  6. * Architecture independence:
  7. * Copyright (c) 2005, Bull S.A.
  8. * Written by Pierre Peiffer <pierre.peiffer@bull.net>
  9. */
  10. /*
  11. * Extents support for EXT4
  12. *
  13. * TODO:
  14. * - ext4*_error() should be used in some situations
  15. * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
  16. * - smart tree reduction
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/time.h>
  20. #include <linux/jbd2.h>
  21. #include <linux/highuid.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/quotaops.h>
  24. #include <linux/string.h>
  25. #include <linux/slab.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/fiemap.h>
  28. #include <linux/iomap.h>
  29. #include <linux/sched/mm.h>
  30. #include "ext4_jbd2.h"
  31. #include "ext4_extents.h"
  32. #include "xattr.h"
  33. #include <trace/events/ext4.h>
  34. /*
  35. * used by extent splitting.
  36. */
  37. #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
  38. due to ENOSPC */
  39. #define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
  40. #define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
  41. #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
  42. #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
  43. static __le32 ext4_extent_block_csum(struct inode *inode,
  44. struct ext4_extent_header *eh)
  45. {
  46. struct ext4_inode_info *ei = EXT4_I(inode);
  47. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  48. __u32 csum;
  49. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
  50. EXT4_EXTENT_TAIL_OFFSET(eh));
  51. return cpu_to_le32(csum);
  52. }
  53. static int ext4_extent_block_csum_verify(struct inode *inode,
  54. struct ext4_extent_header *eh)
  55. {
  56. struct ext4_extent_tail *et;
  57. if (!ext4_has_metadata_csum(inode->i_sb))
  58. return 1;
  59. et = find_ext4_extent_tail(eh);
  60. if (et->et_checksum != ext4_extent_block_csum(inode, eh))
  61. return 0;
  62. return 1;
  63. }
  64. static void ext4_extent_block_csum_set(struct inode *inode,
  65. struct ext4_extent_header *eh)
  66. {
  67. struct ext4_extent_tail *et;
  68. if (!ext4_has_metadata_csum(inode->i_sb))
  69. return;
  70. et = find_ext4_extent_tail(eh);
  71. et->et_checksum = ext4_extent_block_csum(inode, eh);
  72. }
  73. static struct ext4_ext_path *ext4_split_extent_at(handle_t *handle,
  74. struct inode *inode,
  75. struct ext4_ext_path *path,
  76. ext4_lblk_t split,
  77. int split_flag, int flags);
  78. static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped)
  79. {
  80. /*
  81. * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
  82. * moment, get_block can be called only for blocks inside i_size since
  83. * page cache has been already dropped and writes are blocked by
  84. * i_rwsem. So we can safely drop the i_data_sem here.
  85. */
  86. BUG_ON(EXT4_JOURNAL(inode) == NULL);
  87. ext4_discard_preallocations(inode);
  88. up_write(&EXT4_I(inode)->i_data_sem);
  89. *dropped = 1;
  90. return 0;
  91. }
  92. static inline void ext4_ext_path_brelse(struct ext4_ext_path *path)
  93. {
  94. brelse(path->p_bh);
  95. path->p_bh = NULL;
  96. }
  97. static void ext4_ext_drop_refs(struct ext4_ext_path *path)
  98. {
  99. int depth, i;
  100. if (IS_ERR_OR_NULL(path))
  101. return;
  102. depth = path->p_depth;
  103. for (i = 0; i <= depth; i++, path++)
  104. ext4_ext_path_brelse(path);
  105. }
  106. void ext4_free_ext_path(struct ext4_ext_path *path)
  107. {
  108. if (IS_ERR_OR_NULL(path))
  109. return;
  110. ext4_ext_drop_refs(path);
  111. kfree(path);
  112. }
  113. /*
  114. * Make sure 'handle' has at least 'check_cred' credits. If not, restart
  115. * transaction with 'restart_cred' credits. The function drops i_data_sem
  116. * when restarting transaction and gets it after transaction is restarted.
  117. *
  118. * The function returns 0 on success, 1 if transaction had to be restarted,
  119. * and < 0 in case of fatal error.
  120. */
  121. int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
  122. int check_cred, int restart_cred,
  123. int revoke_cred)
  124. {
  125. int ret;
  126. int dropped = 0;
  127. ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred,
  128. revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped));
  129. if (dropped)
  130. down_write(&EXT4_I(inode)->i_data_sem);
  131. return ret;
  132. }
  133. /*
  134. * could return:
  135. * - EROFS
  136. * - ENOMEM
  137. */
  138. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  139. struct ext4_ext_path *path)
  140. {
  141. int err = 0;
  142. if (path->p_bh) {
  143. /* path points to block */
  144. BUFFER_TRACE(path->p_bh, "get_write_access");
  145. err = ext4_journal_get_write_access(handle, inode->i_sb,
  146. path->p_bh, EXT4_JTR_NONE);
  147. /*
  148. * The extent buffer's verified bit will be set again in
  149. * __ext4_ext_dirty(). We could leave an inconsistent
  150. * buffer if the extents updating procudure break off du
  151. * to some error happens, force to check it again.
  152. */
  153. if (!err)
  154. clear_buffer_verified(path->p_bh);
  155. }
  156. /* path points to leaf/index in inode body */
  157. /* we use in-core data, no need to protect them */
  158. return err;
  159. }
  160. /*
  161. * could return:
  162. * - EROFS
  163. * - ENOMEM
  164. * - EIO
  165. */
  166. static int __ext4_ext_dirty(const char *where, unsigned int line,
  167. handle_t *handle, struct inode *inode,
  168. struct ext4_ext_path *path)
  169. {
  170. int err;
  171. WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
  172. if (path->p_bh) {
  173. ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
  174. /* path points to block */
  175. err = __ext4_handle_dirty_metadata(where, line, handle,
  176. inode, path->p_bh);
  177. /* Extents updating done, re-set verified flag */
  178. if (!err)
  179. set_buffer_verified(path->p_bh);
  180. } else {
  181. /* path points to leaf/index in inode body */
  182. err = ext4_mark_inode_dirty(handle, inode);
  183. }
  184. return err;
  185. }
  186. #define ext4_ext_dirty(handle, inode, path) \
  187. __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
  188. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  189. struct ext4_ext_path *path,
  190. ext4_lblk_t block)
  191. {
  192. if (path) {
  193. int depth = path->p_depth;
  194. struct ext4_extent *ex;
  195. /*
  196. * Try to predict block placement assuming that we are
  197. * filling in a file which will eventually be
  198. * non-sparse --- i.e., in the case of libbfd writing
  199. * an ELF object sections out-of-order but in a way
  200. * the eventually results in a contiguous object or
  201. * executable file, or some database extending a table
  202. * space file. However, this is actually somewhat
  203. * non-ideal if we are writing a sparse file such as
  204. * qemu or KVM writing a raw image file that is going
  205. * to stay fairly sparse, since it will end up
  206. * fragmenting the file system's free space. Maybe we
  207. * should have some hueristics or some way to allow
  208. * userspace to pass a hint to file system,
  209. * especially if the latter case turns out to be
  210. * common.
  211. */
  212. ex = path[depth].p_ext;
  213. if (ex) {
  214. ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
  215. ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
  216. if (block > ext_block)
  217. return ext_pblk + (block - ext_block);
  218. else
  219. return ext_pblk - (ext_block - block);
  220. }
  221. /* it looks like index is empty;
  222. * try to find starting block from index itself */
  223. if (path[depth].p_bh)
  224. return path[depth].p_bh->b_blocknr;
  225. }
  226. /* OK. use inode's group */
  227. return ext4_inode_to_goal_block(inode);
  228. }
  229. /*
  230. * Allocation for a meta data block
  231. */
  232. static ext4_fsblk_t
  233. ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
  234. struct ext4_ext_path *path,
  235. struct ext4_extent *ex, int *err, unsigned int flags)
  236. {
  237. ext4_fsblk_t goal, newblock;
  238. goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
  239. newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
  240. NULL, err);
  241. return newblock;
  242. }
  243. static inline int ext4_ext_space_block(struct inode *inode, int check)
  244. {
  245. int size;
  246. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  247. / sizeof(struct ext4_extent);
  248. #ifdef AGGRESSIVE_TEST
  249. if (!check && size > 6)
  250. size = 6;
  251. #endif
  252. return size;
  253. }
  254. static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
  255. {
  256. int size;
  257. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  258. / sizeof(struct ext4_extent_idx);
  259. #ifdef AGGRESSIVE_TEST
  260. if (!check && size > 5)
  261. size = 5;
  262. #endif
  263. return size;
  264. }
  265. static inline int ext4_ext_space_root(struct inode *inode, int check)
  266. {
  267. int size;
  268. size = sizeof(EXT4_I(inode)->i_data);
  269. size -= sizeof(struct ext4_extent_header);
  270. size /= sizeof(struct ext4_extent);
  271. #ifdef AGGRESSIVE_TEST
  272. if (!check && size > 3)
  273. size = 3;
  274. #endif
  275. return size;
  276. }
  277. static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
  278. {
  279. int size;
  280. size = sizeof(EXT4_I(inode)->i_data);
  281. size -= sizeof(struct ext4_extent_header);
  282. size /= sizeof(struct ext4_extent_idx);
  283. #ifdef AGGRESSIVE_TEST
  284. if (!check && size > 4)
  285. size = 4;
  286. #endif
  287. return size;
  288. }
  289. static inline struct ext4_ext_path *
  290. ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
  291. struct ext4_ext_path *path, ext4_lblk_t lblk,
  292. int nofail)
  293. {
  294. int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
  295. int flags = EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO;
  296. if (nofail)
  297. flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL | EXT4_EX_NOFAIL;
  298. return ext4_split_extent_at(handle, inode, path, lblk, unwritten ?
  299. EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
  300. flags);
  301. }
  302. static int
  303. ext4_ext_max_entries(struct inode *inode, int depth)
  304. {
  305. int max;
  306. if (depth == ext_depth(inode)) {
  307. if (depth == 0)
  308. max = ext4_ext_space_root(inode, 1);
  309. else
  310. max = ext4_ext_space_root_idx(inode, 1);
  311. } else {
  312. if (depth == 0)
  313. max = ext4_ext_space_block(inode, 1);
  314. else
  315. max = ext4_ext_space_block_idx(inode, 1);
  316. }
  317. return max;
  318. }
  319. static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
  320. {
  321. ext4_fsblk_t block = ext4_ext_pblock(ext);
  322. int len = ext4_ext_get_actual_len(ext);
  323. ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
  324. /*
  325. * We allow neither:
  326. * - zero length
  327. * - overflow/wrap-around
  328. */
  329. if (lblock + len <= lblock)
  330. return 0;
  331. return ext4_inode_block_valid(inode, block, len);
  332. }
  333. static int ext4_valid_extent_idx(struct inode *inode,
  334. struct ext4_extent_idx *ext_idx)
  335. {
  336. ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
  337. return ext4_inode_block_valid(inode, block, 1);
  338. }
  339. static int ext4_valid_extent_entries(struct inode *inode,
  340. struct ext4_extent_header *eh,
  341. ext4_lblk_t lblk, ext4_fsblk_t *pblk,
  342. int depth)
  343. {
  344. unsigned short entries;
  345. ext4_lblk_t lblock = 0;
  346. ext4_lblk_t cur = 0;
  347. if (eh->eh_entries == 0)
  348. return 1;
  349. entries = le16_to_cpu(eh->eh_entries);
  350. if (depth == 0) {
  351. /* leaf entries */
  352. struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
  353. /*
  354. * The logical block in the first entry should equal to
  355. * the number in the index block.
  356. */
  357. if (depth != ext_depth(inode) &&
  358. lblk != le32_to_cpu(ext->ee_block))
  359. return 0;
  360. while (entries) {
  361. if (!ext4_valid_extent(inode, ext))
  362. return 0;
  363. /* Check for overlapping extents */
  364. lblock = le32_to_cpu(ext->ee_block);
  365. if (lblock < cur) {
  366. *pblk = ext4_ext_pblock(ext);
  367. return 0;
  368. }
  369. cur = lblock + ext4_ext_get_actual_len(ext);
  370. ext++;
  371. entries--;
  372. }
  373. } else {
  374. struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
  375. /*
  376. * The logical block in the first entry should equal to
  377. * the number in the parent index block.
  378. */
  379. if (depth != ext_depth(inode) &&
  380. lblk != le32_to_cpu(ext_idx->ei_block))
  381. return 0;
  382. while (entries) {
  383. if (!ext4_valid_extent_idx(inode, ext_idx))
  384. return 0;
  385. /* Check for overlapping index extents */
  386. lblock = le32_to_cpu(ext_idx->ei_block);
  387. if (lblock < cur) {
  388. *pblk = ext4_idx_pblock(ext_idx);
  389. return 0;
  390. }
  391. ext_idx++;
  392. entries--;
  393. cur = lblock + 1;
  394. }
  395. }
  396. return 1;
  397. }
  398. static int __ext4_ext_check(const char *function, unsigned int line,
  399. struct inode *inode, struct ext4_extent_header *eh,
  400. int depth, ext4_fsblk_t pblk, ext4_lblk_t lblk)
  401. {
  402. const char *error_msg;
  403. int max = 0, err = -EFSCORRUPTED;
  404. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  405. error_msg = "invalid magic";
  406. goto corrupted;
  407. }
  408. if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
  409. error_msg = "unexpected eh_depth";
  410. goto corrupted;
  411. }
  412. if (unlikely(eh->eh_max == 0)) {
  413. error_msg = "invalid eh_max";
  414. goto corrupted;
  415. }
  416. max = ext4_ext_max_entries(inode, depth);
  417. if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
  418. error_msg = "too large eh_max";
  419. goto corrupted;
  420. }
  421. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  422. error_msg = "invalid eh_entries";
  423. goto corrupted;
  424. }
  425. if (unlikely((eh->eh_entries == 0) && (depth > 0))) {
  426. error_msg = "eh_entries is 0 but eh_depth is > 0";
  427. goto corrupted;
  428. }
  429. if (!ext4_valid_extent_entries(inode, eh, lblk, &pblk, depth)) {
  430. error_msg = "invalid extent entries";
  431. goto corrupted;
  432. }
  433. if (unlikely(depth > 32)) {
  434. error_msg = "too large eh_depth";
  435. goto corrupted;
  436. }
  437. /* Verify checksum on non-root extent tree nodes */
  438. if (ext_depth(inode) != depth &&
  439. !ext4_extent_block_csum_verify(inode, eh)) {
  440. error_msg = "extent tree corrupted";
  441. err = -EFSBADCRC;
  442. goto corrupted;
  443. }
  444. return 0;
  445. corrupted:
  446. ext4_error_inode_err(inode, function, line, 0, -err,
  447. "pblk %llu bad header/extent: %s - magic %x, "
  448. "entries %u, max %u(%u), depth %u(%u)",
  449. (unsigned long long) pblk, error_msg,
  450. le16_to_cpu(eh->eh_magic),
  451. le16_to_cpu(eh->eh_entries),
  452. le16_to_cpu(eh->eh_max),
  453. max, le16_to_cpu(eh->eh_depth), depth);
  454. return err;
  455. }
  456. #define ext4_ext_check(inode, eh, depth, pblk) \
  457. __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk), 0)
  458. int ext4_ext_check_inode(struct inode *inode)
  459. {
  460. return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
  461. }
  462. static void ext4_cache_extents(struct inode *inode,
  463. struct ext4_extent_header *eh)
  464. {
  465. struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
  466. ext4_lblk_t prev = 0;
  467. int i;
  468. for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
  469. unsigned int status = EXTENT_STATUS_WRITTEN;
  470. ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
  471. int len = ext4_ext_get_actual_len(ex);
  472. if (prev && (prev != lblk))
  473. ext4_es_cache_extent(inode, prev, lblk - prev, ~0,
  474. EXTENT_STATUS_HOLE);
  475. if (ext4_ext_is_unwritten(ex))
  476. status = EXTENT_STATUS_UNWRITTEN;
  477. ext4_es_cache_extent(inode, lblk, len,
  478. ext4_ext_pblock(ex), status);
  479. prev = lblk + len;
  480. }
  481. }
  482. static struct buffer_head *
  483. __read_extent_tree_block(const char *function, unsigned int line,
  484. struct inode *inode, struct ext4_extent_idx *idx,
  485. int depth, int flags)
  486. {
  487. struct buffer_head *bh;
  488. int err;
  489. gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS;
  490. ext4_fsblk_t pblk;
  491. if (flags & EXT4_EX_NOFAIL)
  492. gfp_flags |= __GFP_NOFAIL;
  493. pblk = ext4_idx_pblock(idx);
  494. bh = sb_getblk_gfp(inode->i_sb, pblk, gfp_flags);
  495. if (unlikely(!bh))
  496. return ERR_PTR(-ENOMEM);
  497. if (!bh_uptodate_or_lock(bh)) {
  498. trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
  499. err = ext4_read_bh(bh, 0, NULL, false);
  500. if (err < 0)
  501. goto errout;
  502. }
  503. if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
  504. return bh;
  505. err = __ext4_ext_check(function, line, inode, ext_block_hdr(bh),
  506. depth, pblk, le32_to_cpu(idx->ei_block));
  507. if (err)
  508. goto errout;
  509. set_buffer_verified(bh);
  510. /*
  511. * If this is a leaf block, cache all of its entries
  512. */
  513. if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
  514. struct ext4_extent_header *eh = ext_block_hdr(bh);
  515. ext4_cache_extents(inode, eh);
  516. }
  517. return bh;
  518. errout:
  519. put_bh(bh);
  520. return ERR_PTR(err);
  521. }
  522. #define read_extent_tree_block(inode, idx, depth, flags) \
  523. __read_extent_tree_block(__func__, __LINE__, (inode), (idx), \
  524. (depth), (flags))
  525. /*
  526. * This function is called to cache a file's extent information in the
  527. * extent status tree
  528. */
  529. int ext4_ext_precache(struct inode *inode)
  530. {
  531. struct ext4_inode_info *ei = EXT4_I(inode);
  532. struct ext4_ext_path *path = NULL;
  533. struct buffer_head *bh;
  534. int i = 0, depth, ret = 0;
  535. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  536. return 0; /* not an extent-mapped inode */
  537. down_read(&ei->i_data_sem);
  538. depth = ext_depth(inode);
  539. /* Don't cache anything if there are no external extent blocks */
  540. if (!depth) {
  541. up_read(&ei->i_data_sem);
  542. return ret;
  543. }
  544. path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
  545. GFP_NOFS);
  546. if (path == NULL) {
  547. up_read(&ei->i_data_sem);
  548. return -ENOMEM;
  549. }
  550. path[0].p_hdr = ext_inode_hdr(inode);
  551. ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
  552. if (ret)
  553. goto out;
  554. path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
  555. while (i >= 0) {
  556. /*
  557. * If this is a leaf block or we've reached the end of
  558. * the index block, go up
  559. */
  560. if ((i == depth) ||
  561. path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
  562. ext4_ext_path_brelse(path + i);
  563. i--;
  564. continue;
  565. }
  566. bh = read_extent_tree_block(inode, path[i].p_idx++,
  567. depth - i - 1,
  568. EXT4_EX_FORCE_CACHE);
  569. if (IS_ERR(bh)) {
  570. ret = PTR_ERR(bh);
  571. break;
  572. }
  573. i++;
  574. path[i].p_bh = bh;
  575. path[i].p_hdr = ext_block_hdr(bh);
  576. path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
  577. }
  578. ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
  579. out:
  580. up_read(&ei->i_data_sem);
  581. ext4_free_ext_path(path);
  582. return ret;
  583. }
  584. #ifdef EXT_DEBUG
  585. static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
  586. {
  587. int k, l = path->p_depth;
  588. ext_debug(inode, "path:");
  589. for (k = 0; k <= l; k++, path++) {
  590. if (path->p_idx) {
  591. ext_debug(inode, " %d->%llu",
  592. le32_to_cpu(path->p_idx->ei_block),
  593. ext4_idx_pblock(path->p_idx));
  594. } else if (path->p_ext) {
  595. ext_debug(inode, " %d:[%d]%d:%llu ",
  596. le32_to_cpu(path->p_ext->ee_block),
  597. ext4_ext_is_unwritten(path->p_ext),
  598. ext4_ext_get_actual_len(path->p_ext),
  599. ext4_ext_pblock(path->p_ext));
  600. } else
  601. ext_debug(inode, " []");
  602. }
  603. ext_debug(inode, "\n");
  604. }
  605. static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
  606. {
  607. int depth = ext_depth(inode);
  608. struct ext4_extent_header *eh;
  609. struct ext4_extent *ex;
  610. int i;
  611. if (IS_ERR_OR_NULL(path))
  612. return;
  613. eh = path[depth].p_hdr;
  614. ex = EXT_FIRST_EXTENT(eh);
  615. ext_debug(inode, "Displaying leaf extents\n");
  616. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
  617. ext_debug(inode, "%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
  618. ext4_ext_is_unwritten(ex),
  619. ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
  620. }
  621. ext_debug(inode, "\n");
  622. }
  623. static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
  624. ext4_fsblk_t newblock, int level)
  625. {
  626. int depth = ext_depth(inode);
  627. struct ext4_extent *ex;
  628. if (depth != level) {
  629. struct ext4_extent_idx *idx;
  630. idx = path[level].p_idx;
  631. while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
  632. ext_debug(inode, "%d: move %d:%llu in new index %llu\n",
  633. level, le32_to_cpu(idx->ei_block),
  634. ext4_idx_pblock(idx), newblock);
  635. idx++;
  636. }
  637. return;
  638. }
  639. ex = path[depth].p_ext;
  640. while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
  641. ext_debug(inode, "move %d:%llu:[%d]%d in new leaf %llu\n",
  642. le32_to_cpu(ex->ee_block),
  643. ext4_ext_pblock(ex),
  644. ext4_ext_is_unwritten(ex),
  645. ext4_ext_get_actual_len(ex),
  646. newblock);
  647. ex++;
  648. }
  649. }
  650. #else
  651. #define ext4_ext_show_path(inode, path)
  652. #define ext4_ext_show_leaf(inode, path)
  653. #define ext4_ext_show_move(inode, path, newblock, level)
  654. #endif
  655. /*
  656. * ext4_ext_binsearch_idx:
  657. * binary search for the closest index of the given block
  658. * the header must be checked before calling this
  659. */
  660. static void
  661. ext4_ext_binsearch_idx(struct inode *inode,
  662. struct ext4_ext_path *path, ext4_lblk_t block)
  663. {
  664. struct ext4_extent_header *eh = path->p_hdr;
  665. struct ext4_extent_idx *r, *l, *m;
  666. ext_debug(inode, "binsearch for %u(idx): ", block);
  667. l = EXT_FIRST_INDEX(eh) + 1;
  668. r = EXT_LAST_INDEX(eh);
  669. while (l <= r) {
  670. m = l + (r - l) / 2;
  671. ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l,
  672. le32_to_cpu(l->ei_block), m, le32_to_cpu(m->ei_block),
  673. r, le32_to_cpu(r->ei_block));
  674. if (block < le32_to_cpu(m->ei_block))
  675. r = m - 1;
  676. else
  677. l = m + 1;
  678. }
  679. path->p_idx = l - 1;
  680. ext_debug(inode, " -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
  681. ext4_idx_pblock(path->p_idx));
  682. #ifdef CHECK_BINSEARCH
  683. {
  684. struct ext4_extent_idx *chix, *ix;
  685. int k;
  686. chix = ix = EXT_FIRST_INDEX(eh);
  687. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
  688. if (k != 0 && le32_to_cpu(ix->ei_block) <=
  689. le32_to_cpu(ix[-1].ei_block)) {
  690. printk(KERN_DEBUG "k=%d, ix=0x%p, "
  691. "first=0x%p\n", k,
  692. ix, EXT_FIRST_INDEX(eh));
  693. printk(KERN_DEBUG "%u <= %u\n",
  694. le32_to_cpu(ix->ei_block),
  695. le32_to_cpu(ix[-1].ei_block));
  696. }
  697. BUG_ON(k && le32_to_cpu(ix->ei_block)
  698. <= le32_to_cpu(ix[-1].ei_block));
  699. if (block < le32_to_cpu(ix->ei_block))
  700. break;
  701. chix = ix;
  702. }
  703. BUG_ON(chix != path->p_idx);
  704. }
  705. #endif
  706. }
  707. /*
  708. * ext4_ext_binsearch:
  709. * binary search for closest extent of the given block
  710. * the header must be checked before calling this
  711. */
  712. static void
  713. ext4_ext_binsearch(struct inode *inode,
  714. struct ext4_ext_path *path, ext4_lblk_t block)
  715. {
  716. struct ext4_extent_header *eh = path->p_hdr;
  717. struct ext4_extent *r, *l, *m;
  718. if (eh->eh_entries == 0) {
  719. /*
  720. * this leaf is empty:
  721. * we get such a leaf in split/add case
  722. */
  723. return;
  724. }
  725. ext_debug(inode, "binsearch for %u: ", block);
  726. l = EXT_FIRST_EXTENT(eh) + 1;
  727. r = EXT_LAST_EXTENT(eh);
  728. while (l <= r) {
  729. m = l + (r - l) / 2;
  730. ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l,
  731. le32_to_cpu(l->ee_block), m, le32_to_cpu(m->ee_block),
  732. r, le32_to_cpu(r->ee_block));
  733. if (block < le32_to_cpu(m->ee_block))
  734. r = m - 1;
  735. else
  736. l = m + 1;
  737. }
  738. path->p_ext = l - 1;
  739. ext_debug(inode, " -> %d:%llu:[%d]%d ",
  740. le32_to_cpu(path->p_ext->ee_block),
  741. ext4_ext_pblock(path->p_ext),
  742. ext4_ext_is_unwritten(path->p_ext),
  743. ext4_ext_get_actual_len(path->p_ext));
  744. #ifdef CHECK_BINSEARCH
  745. {
  746. struct ext4_extent *chex, *ex;
  747. int k;
  748. chex = ex = EXT_FIRST_EXTENT(eh);
  749. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
  750. BUG_ON(k && le32_to_cpu(ex->ee_block)
  751. <= le32_to_cpu(ex[-1].ee_block));
  752. if (block < le32_to_cpu(ex->ee_block))
  753. break;
  754. chex = ex;
  755. }
  756. BUG_ON(chex != path->p_ext);
  757. }
  758. #endif
  759. }
  760. void ext4_ext_tree_init(handle_t *handle, struct inode *inode)
  761. {
  762. struct ext4_extent_header *eh;
  763. eh = ext_inode_hdr(inode);
  764. eh->eh_depth = 0;
  765. eh->eh_entries = 0;
  766. eh->eh_magic = EXT4_EXT_MAGIC;
  767. eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
  768. eh->eh_generation = 0;
  769. ext4_mark_inode_dirty(handle, inode);
  770. }
  771. struct ext4_ext_path *
  772. ext4_find_extent(struct inode *inode, ext4_lblk_t block,
  773. struct ext4_ext_path *path, int flags)
  774. {
  775. struct ext4_extent_header *eh;
  776. struct buffer_head *bh;
  777. short int depth, i, ppos = 0;
  778. int ret;
  779. gfp_t gfp_flags = GFP_NOFS;
  780. if (flags & EXT4_EX_NOFAIL)
  781. gfp_flags |= __GFP_NOFAIL;
  782. eh = ext_inode_hdr(inode);
  783. depth = ext_depth(inode);
  784. if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
  785. EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
  786. depth);
  787. ret = -EFSCORRUPTED;
  788. goto err;
  789. }
  790. if (path) {
  791. ext4_ext_drop_refs(path);
  792. if (depth > path[0].p_maxdepth) {
  793. kfree(path);
  794. path = NULL;
  795. }
  796. }
  797. if (!path) {
  798. /* account possible depth increase */
  799. path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
  800. gfp_flags);
  801. if (unlikely(!path))
  802. return ERR_PTR(-ENOMEM);
  803. path[0].p_maxdepth = depth + 1;
  804. }
  805. path[0].p_hdr = eh;
  806. path[0].p_bh = NULL;
  807. i = depth;
  808. if (!(flags & EXT4_EX_NOCACHE) && depth == 0)
  809. ext4_cache_extents(inode, eh);
  810. /* walk through the tree */
  811. while (i) {
  812. ext_debug(inode, "depth %d: num %d, max %d\n",
  813. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  814. ext4_ext_binsearch_idx(inode, path + ppos, block);
  815. path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
  816. path[ppos].p_depth = i;
  817. path[ppos].p_ext = NULL;
  818. bh = read_extent_tree_block(inode, path[ppos].p_idx, --i, flags);
  819. if (IS_ERR(bh)) {
  820. ret = PTR_ERR(bh);
  821. goto err;
  822. }
  823. eh = ext_block_hdr(bh);
  824. ppos++;
  825. path[ppos].p_bh = bh;
  826. path[ppos].p_hdr = eh;
  827. }
  828. path[ppos].p_depth = i;
  829. path[ppos].p_ext = NULL;
  830. path[ppos].p_idx = NULL;
  831. /* find extent */
  832. ext4_ext_binsearch(inode, path + ppos, block);
  833. /* if not an empty leaf */
  834. if (path[ppos].p_ext)
  835. path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
  836. ext4_ext_show_path(inode, path);
  837. return path;
  838. err:
  839. ext4_free_ext_path(path);
  840. return ERR_PTR(ret);
  841. }
  842. /*
  843. * ext4_ext_insert_index:
  844. * insert new index [@logical;@ptr] into the block at @curp;
  845. * check where to insert: before @curp or after @curp
  846. */
  847. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  848. struct ext4_ext_path *curp,
  849. int logical, ext4_fsblk_t ptr)
  850. {
  851. struct ext4_extent_idx *ix;
  852. int len, err;
  853. err = ext4_ext_get_access(handle, inode, curp);
  854. if (err)
  855. return err;
  856. if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
  857. EXT4_ERROR_INODE(inode,
  858. "logical %d == ei_block %d!",
  859. logical, le32_to_cpu(curp->p_idx->ei_block));
  860. return -EFSCORRUPTED;
  861. }
  862. if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
  863. >= le16_to_cpu(curp->p_hdr->eh_max))) {
  864. EXT4_ERROR_INODE(inode,
  865. "eh_entries %d >= eh_max %d!",
  866. le16_to_cpu(curp->p_hdr->eh_entries),
  867. le16_to_cpu(curp->p_hdr->eh_max));
  868. return -EFSCORRUPTED;
  869. }
  870. if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
  871. /* insert after */
  872. ext_debug(inode, "insert new index %d after: %llu\n",
  873. logical, ptr);
  874. ix = curp->p_idx + 1;
  875. } else {
  876. /* insert before */
  877. ext_debug(inode, "insert new index %d before: %llu\n",
  878. logical, ptr);
  879. ix = curp->p_idx;
  880. }
  881. if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
  882. EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
  883. return -EFSCORRUPTED;
  884. }
  885. len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
  886. BUG_ON(len < 0);
  887. if (len > 0) {
  888. ext_debug(inode, "insert new index %d: "
  889. "move %d indices from 0x%p to 0x%p\n",
  890. logical, len, ix, ix + 1);
  891. memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
  892. }
  893. ix->ei_block = cpu_to_le32(logical);
  894. ext4_idx_store_pblock(ix, ptr);
  895. le16_add_cpu(&curp->p_hdr->eh_entries, 1);
  896. if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
  897. EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
  898. return -EFSCORRUPTED;
  899. }
  900. err = ext4_ext_dirty(handle, inode, curp);
  901. ext4_std_error(inode->i_sb, err);
  902. return err;
  903. }
  904. /*
  905. * ext4_ext_split:
  906. * inserts new subtree into the path, using free index entry
  907. * at depth @at:
  908. * - allocates all needed blocks (new leaf and all intermediate index blocks)
  909. * - makes decision where to split
  910. * - moves remaining extents and index entries (right to the split point)
  911. * into the newly allocated blocks
  912. * - initializes subtree
  913. */
  914. static int ext4_ext_split(handle_t *handle, struct inode *inode,
  915. unsigned int flags,
  916. struct ext4_ext_path *path,
  917. struct ext4_extent *newext, int at)
  918. {
  919. struct buffer_head *bh = NULL;
  920. int depth = ext_depth(inode);
  921. struct ext4_extent_header *neh;
  922. struct ext4_extent_idx *fidx;
  923. int i = at, k, m, a;
  924. ext4_fsblk_t newblock, oldblock;
  925. __le32 border;
  926. ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
  927. gfp_t gfp_flags = GFP_NOFS;
  928. int err = 0;
  929. size_t ext_size = 0;
  930. if (flags & EXT4_EX_NOFAIL)
  931. gfp_flags |= __GFP_NOFAIL;
  932. /* make decision: where to split? */
  933. /* FIXME: now decision is simplest: at current extent */
  934. /* if current leaf will be split, then we should use
  935. * border from split point */
  936. if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
  937. EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
  938. return -EFSCORRUPTED;
  939. }
  940. if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
  941. border = path[depth].p_ext[1].ee_block;
  942. ext_debug(inode, "leaf will be split."
  943. " next leaf starts at %d\n",
  944. le32_to_cpu(border));
  945. } else {
  946. border = newext->ee_block;
  947. ext_debug(inode, "leaf will be added."
  948. " next leaf starts at %d\n",
  949. le32_to_cpu(border));
  950. }
  951. /*
  952. * If error occurs, then we break processing
  953. * and mark filesystem read-only. index won't
  954. * be inserted and tree will be in consistent
  955. * state. Next mount will repair buffers too.
  956. */
  957. /*
  958. * Get array to track all allocated blocks.
  959. * We need this to handle errors and free blocks
  960. * upon them.
  961. */
  962. ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), gfp_flags);
  963. if (!ablocks)
  964. return -ENOMEM;
  965. /* allocate all needed blocks */
  966. ext_debug(inode, "allocate %d blocks for indexes/leaf\n", depth - at);
  967. for (a = 0; a < depth - at; a++) {
  968. newblock = ext4_ext_new_meta_block(handle, inode, path,
  969. newext, &err, flags);
  970. if (newblock == 0)
  971. goto cleanup;
  972. ablocks[a] = newblock;
  973. }
  974. /* initialize new leaf */
  975. newblock = ablocks[--a];
  976. if (unlikely(newblock == 0)) {
  977. EXT4_ERROR_INODE(inode, "newblock == 0!");
  978. err = -EFSCORRUPTED;
  979. goto cleanup;
  980. }
  981. bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
  982. if (unlikely(!bh)) {
  983. err = -ENOMEM;
  984. goto cleanup;
  985. }
  986. lock_buffer(bh);
  987. err = ext4_journal_get_create_access(handle, inode->i_sb, bh,
  988. EXT4_JTR_NONE);
  989. if (err)
  990. goto cleanup;
  991. neh = ext_block_hdr(bh);
  992. neh->eh_entries = 0;
  993. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
  994. neh->eh_magic = EXT4_EXT_MAGIC;
  995. neh->eh_depth = 0;
  996. neh->eh_generation = 0;
  997. /* move remainder of path[depth] to the new leaf */
  998. if (unlikely(path[depth].p_hdr->eh_entries !=
  999. path[depth].p_hdr->eh_max)) {
  1000. EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
  1001. path[depth].p_hdr->eh_entries,
  1002. path[depth].p_hdr->eh_max);
  1003. err = -EFSCORRUPTED;
  1004. goto cleanup;
  1005. }
  1006. /* start copy from next extent */
  1007. m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
  1008. ext4_ext_show_move(inode, path, newblock, depth);
  1009. if (m) {
  1010. struct ext4_extent *ex;
  1011. ex = EXT_FIRST_EXTENT(neh);
  1012. memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
  1013. le16_add_cpu(&neh->eh_entries, m);
  1014. }
  1015. /* zero out unused area in the extent block */
  1016. ext_size = sizeof(struct ext4_extent_header) +
  1017. sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
  1018. memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
  1019. ext4_extent_block_csum_set(inode, neh);
  1020. set_buffer_uptodate(bh);
  1021. unlock_buffer(bh);
  1022. err = ext4_handle_dirty_metadata(handle, inode, bh);
  1023. if (err)
  1024. goto cleanup;
  1025. brelse(bh);
  1026. bh = NULL;
  1027. /* correct old leaf */
  1028. if (m) {
  1029. err = ext4_ext_get_access(handle, inode, path + depth);
  1030. if (err)
  1031. goto cleanup;
  1032. le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
  1033. err = ext4_ext_dirty(handle, inode, path + depth);
  1034. if (err)
  1035. goto cleanup;
  1036. }
  1037. /* create intermediate indexes */
  1038. k = depth - at - 1;
  1039. if (unlikely(k < 0)) {
  1040. EXT4_ERROR_INODE(inode, "k %d < 0!", k);
  1041. err = -EFSCORRUPTED;
  1042. goto cleanup;
  1043. }
  1044. if (k)
  1045. ext_debug(inode, "create %d intermediate indices\n", k);
  1046. /* insert new index into current index block */
  1047. /* current depth stored in i var */
  1048. i = depth - 1;
  1049. while (k--) {
  1050. oldblock = newblock;
  1051. newblock = ablocks[--a];
  1052. bh = sb_getblk(inode->i_sb, newblock);
  1053. if (unlikely(!bh)) {
  1054. err = -ENOMEM;
  1055. goto cleanup;
  1056. }
  1057. lock_buffer(bh);
  1058. err = ext4_journal_get_create_access(handle, inode->i_sb, bh,
  1059. EXT4_JTR_NONE);
  1060. if (err)
  1061. goto cleanup;
  1062. neh = ext_block_hdr(bh);
  1063. neh->eh_entries = cpu_to_le16(1);
  1064. neh->eh_magic = EXT4_EXT_MAGIC;
  1065. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
  1066. neh->eh_depth = cpu_to_le16(depth - i);
  1067. neh->eh_generation = 0;
  1068. fidx = EXT_FIRST_INDEX(neh);
  1069. fidx->ei_block = border;
  1070. ext4_idx_store_pblock(fidx, oldblock);
  1071. ext_debug(inode, "int.index at %d (block %llu): %u -> %llu\n",
  1072. i, newblock, le32_to_cpu(border), oldblock);
  1073. /* move remainder of path[i] to the new index block */
  1074. if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
  1075. EXT_LAST_INDEX(path[i].p_hdr))) {
  1076. EXT4_ERROR_INODE(inode,
  1077. "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
  1078. le32_to_cpu(path[i].p_ext->ee_block));
  1079. err = -EFSCORRUPTED;
  1080. goto cleanup;
  1081. }
  1082. /* start copy indexes */
  1083. m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
  1084. ext_debug(inode, "cur 0x%p, last 0x%p\n", path[i].p_idx,
  1085. EXT_MAX_INDEX(path[i].p_hdr));
  1086. ext4_ext_show_move(inode, path, newblock, i);
  1087. if (m) {
  1088. memmove(++fidx, path[i].p_idx,
  1089. sizeof(struct ext4_extent_idx) * m);
  1090. le16_add_cpu(&neh->eh_entries, m);
  1091. }
  1092. /* zero out unused area in the extent block */
  1093. ext_size = sizeof(struct ext4_extent_header) +
  1094. (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
  1095. memset(bh->b_data + ext_size, 0,
  1096. inode->i_sb->s_blocksize - ext_size);
  1097. ext4_extent_block_csum_set(inode, neh);
  1098. set_buffer_uptodate(bh);
  1099. unlock_buffer(bh);
  1100. err = ext4_handle_dirty_metadata(handle, inode, bh);
  1101. if (err)
  1102. goto cleanup;
  1103. brelse(bh);
  1104. bh = NULL;
  1105. /* correct old index */
  1106. if (m) {
  1107. err = ext4_ext_get_access(handle, inode, path + i);
  1108. if (err)
  1109. goto cleanup;
  1110. le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
  1111. err = ext4_ext_dirty(handle, inode, path + i);
  1112. if (err)
  1113. goto cleanup;
  1114. }
  1115. i--;
  1116. }
  1117. /* insert new index */
  1118. err = ext4_ext_insert_index(handle, inode, path + at,
  1119. le32_to_cpu(border), newblock);
  1120. cleanup:
  1121. if (bh) {
  1122. if (buffer_locked(bh))
  1123. unlock_buffer(bh);
  1124. brelse(bh);
  1125. }
  1126. if (err) {
  1127. /* free all allocated blocks in error case */
  1128. for (i = 0; i < depth; i++) {
  1129. if (!ablocks[i])
  1130. continue;
  1131. ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
  1132. EXT4_FREE_BLOCKS_METADATA);
  1133. }
  1134. }
  1135. kfree(ablocks);
  1136. return err;
  1137. }
  1138. /*
  1139. * ext4_ext_grow_indepth:
  1140. * implements tree growing procedure:
  1141. * - allocates new block
  1142. * - moves top-level data (index block or leaf) into the new block
  1143. * - initializes new top-level, creating index that points to the
  1144. * just created block
  1145. */
  1146. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  1147. unsigned int flags)
  1148. {
  1149. struct ext4_extent_header *neh;
  1150. struct buffer_head *bh;
  1151. ext4_fsblk_t newblock, goal = 0;
  1152. struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
  1153. int err = 0;
  1154. size_t ext_size = 0;
  1155. /* Try to prepend new index to old one */
  1156. if (ext_depth(inode))
  1157. goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
  1158. if (goal > le32_to_cpu(es->s_first_data_block)) {
  1159. flags |= EXT4_MB_HINT_TRY_GOAL;
  1160. goal--;
  1161. } else
  1162. goal = ext4_inode_to_goal_block(inode);
  1163. newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
  1164. NULL, &err);
  1165. if (newblock == 0)
  1166. return err;
  1167. bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
  1168. if (unlikely(!bh))
  1169. return -ENOMEM;
  1170. lock_buffer(bh);
  1171. err = ext4_journal_get_create_access(handle, inode->i_sb, bh,
  1172. EXT4_JTR_NONE);
  1173. if (err) {
  1174. unlock_buffer(bh);
  1175. goto out;
  1176. }
  1177. ext_size = sizeof(EXT4_I(inode)->i_data);
  1178. /* move top-level index/leaf into new block */
  1179. memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
  1180. /* zero out unused area in the extent block */
  1181. memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
  1182. /* set size of new block */
  1183. neh = ext_block_hdr(bh);
  1184. /* old root could have indexes or leaves
  1185. * so calculate e_max right way */
  1186. if (ext_depth(inode))
  1187. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
  1188. else
  1189. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
  1190. neh->eh_magic = EXT4_EXT_MAGIC;
  1191. ext4_extent_block_csum_set(inode, neh);
  1192. set_buffer_uptodate(bh);
  1193. set_buffer_verified(bh);
  1194. unlock_buffer(bh);
  1195. err = ext4_handle_dirty_metadata(handle, inode, bh);
  1196. if (err)
  1197. goto out;
  1198. /* Update top-level index: num,max,pointer */
  1199. neh = ext_inode_hdr(inode);
  1200. neh->eh_entries = cpu_to_le16(1);
  1201. ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
  1202. if (neh->eh_depth == 0) {
  1203. /* Root extent block becomes index block */
  1204. neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
  1205. EXT_FIRST_INDEX(neh)->ei_block =
  1206. EXT_FIRST_EXTENT(neh)->ee_block;
  1207. }
  1208. ext_debug(inode, "new root: num %d(%d), lblock %d, ptr %llu\n",
  1209. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  1210. le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
  1211. ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
  1212. le16_add_cpu(&neh->eh_depth, 1);
  1213. err = ext4_mark_inode_dirty(handle, inode);
  1214. out:
  1215. brelse(bh);
  1216. return err;
  1217. }
  1218. /*
  1219. * ext4_ext_create_new_leaf:
  1220. * finds empty index and adds new leaf.
  1221. * if no free index is found, then it requests in-depth growing.
  1222. */
  1223. static struct ext4_ext_path *
  1224. ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  1225. unsigned int mb_flags, unsigned int gb_flags,
  1226. struct ext4_ext_path *path,
  1227. struct ext4_extent *newext)
  1228. {
  1229. struct ext4_ext_path *curp;
  1230. int depth, i, err = 0;
  1231. ext4_lblk_t ee_block = le32_to_cpu(newext->ee_block);
  1232. repeat:
  1233. i = depth = ext_depth(inode);
  1234. /* walk up to the tree and look for free index entry */
  1235. curp = path + depth;
  1236. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  1237. i--;
  1238. curp--;
  1239. }
  1240. /* we use already allocated block for index block,
  1241. * so subsequent data blocks should be contiguous */
  1242. if (EXT_HAS_FREE_INDEX(curp)) {
  1243. /* if we found index with free entry, then use that
  1244. * entry: create all needed subtree and add new leaf */
  1245. err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
  1246. if (err)
  1247. goto errout;
  1248. /* refill path */
  1249. path = ext4_find_extent(inode, ee_block, path, gb_flags);
  1250. return path;
  1251. }
  1252. /* tree is full, time to grow in depth */
  1253. err = ext4_ext_grow_indepth(handle, inode, mb_flags);
  1254. if (err)
  1255. goto errout;
  1256. /* refill path */
  1257. path = ext4_find_extent(inode, ee_block, path, gb_flags);
  1258. if (IS_ERR(path))
  1259. return path;
  1260. /*
  1261. * only first (depth 0 -> 1) produces free space;
  1262. * in all other cases we have to split the grown tree
  1263. */
  1264. depth = ext_depth(inode);
  1265. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  1266. /* now we need to split */
  1267. goto repeat;
  1268. }
  1269. return path;
  1270. errout:
  1271. ext4_free_ext_path(path);
  1272. return ERR_PTR(err);
  1273. }
  1274. /*
  1275. * search the closest allocated block to the left for *logical
  1276. * and returns it at @logical + it's physical address at @phys
  1277. * if *logical is the smallest allocated block, the function
  1278. * returns 0 at @phys
  1279. * return value contains 0 (success) or error code
  1280. */
  1281. static int ext4_ext_search_left(struct inode *inode,
  1282. struct ext4_ext_path *path,
  1283. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  1284. {
  1285. struct ext4_extent_idx *ix;
  1286. struct ext4_extent *ex;
  1287. int depth, ee_len;
  1288. if (unlikely(path == NULL)) {
  1289. EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
  1290. return -EFSCORRUPTED;
  1291. }
  1292. depth = path->p_depth;
  1293. *phys = 0;
  1294. if (depth == 0 && path->p_ext == NULL)
  1295. return 0;
  1296. /* usually extent in the path covers blocks smaller
  1297. * then *logical, but it can be that extent is the
  1298. * first one in the file */
  1299. ex = path[depth].p_ext;
  1300. ee_len = ext4_ext_get_actual_len(ex);
  1301. if (*logical < le32_to_cpu(ex->ee_block)) {
  1302. if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
  1303. EXT4_ERROR_INODE(inode,
  1304. "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
  1305. *logical, le32_to_cpu(ex->ee_block));
  1306. return -EFSCORRUPTED;
  1307. }
  1308. while (--depth >= 0) {
  1309. ix = path[depth].p_idx;
  1310. if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
  1311. EXT4_ERROR_INODE(inode,
  1312. "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
  1313. ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
  1314. le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block),
  1315. depth);
  1316. return -EFSCORRUPTED;
  1317. }
  1318. }
  1319. return 0;
  1320. }
  1321. if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
  1322. EXT4_ERROR_INODE(inode,
  1323. "logical %d < ee_block %d + ee_len %d!",
  1324. *logical, le32_to_cpu(ex->ee_block), ee_len);
  1325. return -EFSCORRUPTED;
  1326. }
  1327. *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
  1328. *phys = ext4_ext_pblock(ex) + ee_len - 1;
  1329. return 0;
  1330. }
  1331. /*
  1332. * Search the closest allocated block to the right for *logical
  1333. * and returns it at @logical + it's physical address at @phys.
  1334. * If not exists, return 0 and @phys is set to 0. We will return
  1335. * 1 which means we found an allocated block and ret_ex is valid.
  1336. * Or return a (< 0) error code.
  1337. */
  1338. static int ext4_ext_search_right(struct inode *inode,
  1339. struct ext4_ext_path *path,
  1340. ext4_lblk_t *logical, ext4_fsblk_t *phys,
  1341. struct ext4_extent *ret_ex)
  1342. {
  1343. struct buffer_head *bh = NULL;
  1344. struct ext4_extent_header *eh;
  1345. struct ext4_extent_idx *ix;
  1346. struct ext4_extent *ex;
  1347. int depth; /* Note, NOT eh_depth; depth from top of tree */
  1348. int ee_len;
  1349. if (unlikely(path == NULL)) {
  1350. EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
  1351. return -EFSCORRUPTED;
  1352. }
  1353. depth = path->p_depth;
  1354. *phys = 0;
  1355. if (depth == 0 && path->p_ext == NULL)
  1356. return 0;
  1357. /* usually extent in the path covers blocks smaller
  1358. * then *logical, but it can be that extent is the
  1359. * first one in the file */
  1360. ex = path[depth].p_ext;
  1361. ee_len = ext4_ext_get_actual_len(ex);
  1362. if (*logical < le32_to_cpu(ex->ee_block)) {
  1363. if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
  1364. EXT4_ERROR_INODE(inode,
  1365. "first_extent(path[%d].p_hdr) != ex",
  1366. depth);
  1367. return -EFSCORRUPTED;
  1368. }
  1369. while (--depth >= 0) {
  1370. ix = path[depth].p_idx;
  1371. if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
  1372. EXT4_ERROR_INODE(inode,
  1373. "ix != EXT_FIRST_INDEX *logical %d!",
  1374. *logical);
  1375. return -EFSCORRUPTED;
  1376. }
  1377. }
  1378. goto found_extent;
  1379. }
  1380. if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
  1381. EXT4_ERROR_INODE(inode,
  1382. "logical %d < ee_block %d + ee_len %d!",
  1383. *logical, le32_to_cpu(ex->ee_block), ee_len);
  1384. return -EFSCORRUPTED;
  1385. }
  1386. if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
  1387. /* next allocated block in this leaf */
  1388. ex++;
  1389. goto found_extent;
  1390. }
  1391. /* go up and search for index to the right */
  1392. while (--depth >= 0) {
  1393. ix = path[depth].p_idx;
  1394. if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
  1395. goto got_index;
  1396. }
  1397. /* we've gone up to the root and found no index to the right */
  1398. return 0;
  1399. got_index:
  1400. /* we've found index to the right, let's
  1401. * follow it and find the closest allocated
  1402. * block to the right */
  1403. ix++;
  1404. while (++depth < path->p_depth) {
  1405. /* subtract from p_depth to get proper eh_depth */
  1406. bh = read_extent_tree_block(inode, ix, path->p_depth - depth, 0);
  1407. if (IS_ERR(bh))
  1408. return PTR_ERR(bh);
  1409. eh = ext_block_hdr(bh);
  1410. ix = EXT_FIRST_INDEX(eh);
  1411. put_bh(bh);
  1412. }
  1413. bh = read_extent_tree_block(inode, ix, path->p_depth - depth, 0);
  1414. if (IS_ERR(bh))
  1415. return PTR_ERR(bh);
  1416. eh = ext_block_hdr(bh);
  1417. ex = EXT_FIRST_EXTENT(eh);
  1418. found_extent:
  1419. *logical = le32_to_cpu(ex->ee_block);
  1420. *phys = ext4_ext_pblock(ex);
  1421. if (ret_ex)
  1422. *ret_ex = *ex;
  1423. if (bh)
  1424. put_bh(bh);
  1425. return 1;
  1426. }
  1427. /*
  1428. * ext4_ext_next_allocated_block:
  1429. * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
  1430. * NOTE: it considers block number from index entry as
  1431. * allocated block. Thus, index entries have to be consistent
  1432. * with leaves.
  1433. */
  1434. ext4_lblk_t
  1435. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  1436. {
  1437. int depth;
  1438. BUG_ON(path == NULL);
  1439. depth = path->p_depth;
  1440. if (depth == 0 && path->p_ext == NULL)
  1441. return EXT_MAX_BLOCKS;
  1442. while (depth >= 0) {
  1443. struct ext4_ext_path *p = &path[depth];
  1444. if (depth == path->p_depth) {
  1445. /* leaf */
  1446. if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr))
  1447. return le32_to_cpu(p->p_ext[1].ee_block);
  1448. } else {
  1449. /* index */
  1450. if (p->p_idx != EXT_LAST_INDEX(p->p_hdr))
  1451. return le32_to_cpu(p->p_idx[1].ei_block);
  1452. }
  1453. depth--;
  1454. }
  1455. return EXT_MAX_BLOCKS;
  1456. }
  1457. /*
  1458. * ext4_ext_next_leaf_block:
  1459. * returns first allocated block from next leaf or EXT_MAX_BLOCKS
  1460. */
  1461. static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
  1462. {
  1463. int depth;
  1464. BUG_ON(path == NULL);
  1465. depth = path->p_depth;
  1466. /* zero-tree has no leaf blocks at all */
  1467. if (depth == 0)
  1468. return EXT_MAX_BLOCKS;
  1469. /* go to index block */
  1470. depth--;
  1471. while (depth >= 0) {
  1472. if (path[depth].p_idx !=
  1473. EXT_LAST_INDEX(path[depth].p_hdr))
  1474. return (ext4_lblk_t)
  1475. le32_to_cpu(path[depth].p_idx[1].ei_block);
  1476. depth--;
  1477. }
  1478. return EXT_MAX_BLOCKS;
  1479. }
  1480. /*
  1481. * ext4_ext_correct_indexes:
  1482. * if leaf gets modified and modified extent is first in the leaf,
  1483. * then we have to correct all indexes above.
  1484. * TODO: do we need to correct tree in all cases?
  1485. */
  1486. static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  1487. struct ext4_ext_path *path)
  1488. {
  1489. struct ext4_extent_header *eh;
  1490. int depth = ext_depth(inode);
  1491. struct ext4_extent *ex;
  1492. __le32 border;
  1493. int k, err = 0;
  1494. eh = path[depth].p_hdr;
  1495. ex = path[depth].p_ext;
  1496. if (unlikely(ex == NULL || eh == NULL)) {
  1497. EXT4_ERROR_INODE(inode,
  1498. "ex %p == NULL or eh %p == NULL", ex, eh);
  1499. return -EFSCORRUPTED;
  1500. }
  1501. if (depth == 0) {
  1502. /* there is no tree at all */
  1503. return 0;
  1504. }
  1505. if (ex != EXT_FIRST_EXTENT(eh)) {
  1506. /* we correct tree if first leaf got modified only */
  1507. return 0;
  1508. }
  1509. /*
  1510. * TODO: we need correction if border is smaller than current one
  1511. */
  1512. k = depth - 1;
  1513. border = path[depth].p_ext->ee_block;
  1514. err = ext4_ext_get_access(handle, inode, path + k);
  1515. if (err)
  1516. return err;
  1517. path[k].p_idx->ei_block = border;
  1518. err = ext4_ext_dirty(handle, inode, path + k);
  1519. if (err)
  1520. return err;
  1521. while (k--) {
  1522. /* change all left-side indexes */
  1523. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  1524. break;
  1525. err = ext4_ext_get_access(handle, inode, path + k);
  1526. if (err)
  1527. goto clean;
  1528. path[k].p_idx->ei_block = border;
  1529. err = ext4_ext_dirty(handle, inode, path + k);
  1530. if (err)
  1531. goto clean;
  1532. }
  1533. return 0;
  1534. clean:
  1535. /*
  1536. * The path[k].p_bh is either unmodified or with no verified bit
  1537. * set (see ext4_ext_get_access()). So just clear the verified bit
  1538. * of the successfully modified extents buffers, which will force
  1539. * these extents to be checked to avoid using inconsistent data.
  1540. */
  1541. while (++k < depth)
  1542. clear_buffer_verified(path[k].p_bh);
  1543. return err;
  1544. }
  1545. static int ext4_can_extents_be_merged(struct inode *inode,
  1546. struct ext4_extent *ex1,
  1547. struct ext4_extent *ex2)
  1548. {
  1549. unsigned short ext1_ee_len, ext2_ee_len;
  1550. if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
  1551. return 0;
  1552. ext1_ee_len = ext4_ext_get_actual_len(ex1);
  1553. ext2_ee_len = ext4_ext_get_actual_len(ex2);
  1554. if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
  1555. le32_to_cpu(ex2->ee_block))
  1556. return 0;
  1557. if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
  1558. return 0;
  1559. if (ext4_ext_is_unwritten(ex1) &&
  1560. ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)
  1561. return 0;
  1562. #ifdef AGGRESSIVE_TEST
  1563. if (ext1_ee_len >= 4)
  1564. return 0;
  1565. #endif
  1566. if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
  1567. return 1;
  1568. return 0;
  1569. }
  1570. /*
  1571. * This function tries to merge the "ex" extent to the next extent in the tree.
  1572. * It always tries to merge towards right. If you want to merge towards
  1573. * left, pass "ex - 1" as argument instead of "ex".
  1574. * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
  1575. * 1 if they got merged.
  1576. */
  1577. static int ext4_ext_try_to_merge_right(struct inode *inode,
  1578. struct ext4_ext_path *path,
  1579. struct ext4_extent *ex)
  1580. {
  1581. struct ext4_extent_header *eh;
  1582. unsigned int depth, len;
  1583. int merge_done = 0, unwritten;
  1584. depth = ext_depth(inode);
  1585. BUG_ON(path[depth].p_hdr == NULL);
  1586. eh = path[depth].p_hdr;
  1587. while (ex < EXT_LAST_EXTENT(eh)) {
  1588. if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
  1589. break;
  1590. /* merge with next extent! */
  1591. unwritten = ext4_ext_is_unwritten(ex);
  1592. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1593. + ext4_ext_get_actual_len(ex + 1));
  1594. if (unwritten)
  1595. ext4_ext_mark_unwritten(ex);
  1596. if (ex + 1 < EXT_LAST_EXTENT(eh)) {
  1597. len = (EXT_LAST_EXTENT(eh) - ex - 1)
  1598. * sizeof(struct ext4_extent);
  1599. memmove(ex + 1, ex + 2, len);
  1600. }
  1601. le16_add_cpu(&eh->eh_entries, -1);
  1602. merge_done = 1;
  1603. WARN_ON(eh->eh_entries == 0);
  1604. if (!eh->eh_entries)
  1605. EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
  1606. }
  1607. return merge_done;
  1608. }
  1609. /*
  1610. * This function does a very simple check to see if we can collapse
  1611. * an extent tree with a single extent tree leaf block into the inode.
  1612. */
  1613. static void ext4_ext_try_to_merge_up(handle_t *handle,
  1614. struct inode *inode,
  1615. struct ext4_ext_path *path)
  1616. {
  1617. size_t s;
  1618. unsigned max_root = ext4_ext_space_root(inode, 0);
  1619. ext4_fsblk_t blk;
  1620. if ((path[0].p_depth != 1) ||
  1621. (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
  1622. (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
  1623. return;
  1624. /*
  1625. * We need to modify the block allocation bitmap and the block
  1626. * group descriptor to release the extent tree block. If we
  1627. * can't get the journal credits, give up.
  1628. */
  1629. if (ext4_journal_extend(handle, 2,
  1630. ext4_free_metadata_revoke_credits(inode->i_sb, 1)))
  1631. return;
  1632. /*
  1633. * Copy the extent data up to the inode
  1634. */
  1635. blk = ext4_idx_pblock(path[0].p_idx);
  1636. s = le16_to_cpu(path[1].p_hdr->eh_entries) *
  1637. sizeof(struct ext4_extent_idx);
  1638. s += sizeof(struct ext4_extent_header);
  1639. path[1].p_maxdepth = path[0].p_maxdepth;
  1640. memcpy(path[0].p_hdr, path[1].p_hdr, s);
  1641. path[0].p_depth = 0;
  1642. path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
  1643. (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
  1644. path[0].p_hdr->eh_max = cpu_to_le16(max_root);
  1645. ext4_ext_path_brelse(path + 1);
  1646. ext4_free_blocks(handle, inode, NULL, blk, 1,
  1647. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  1648. }
  1649. /*
  1650. * This function tries to merge the @ex extent to neighbours in the tree, then
  1651. * tries to collapse the extent tree into the inode.
  1652. */
  1653. static void ext4_ext_try_to_merge(handle_t *handle,
  1654. struct inode *inode,
  1655. struct ext4_ext_path *path,
  1656. struct ext4_extent *ex)
  1657. {
  1658. struct ext4_extent_header *eh;
  1659. unsigned int depth;
  1660. int merge_done = 0;
  1661. depth = ext_depth(inode);
  1662. BUG_ON(path[depth].p_hdr == NULL);
  1663. eh = path[depth].p_hdr;
  1664. if (ex > EXT_FIRST_EXTENT(eh))
  1665. merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
  1666. if (!merge_done)
  1667. (void) ext4_ext_try_to_merge_right(inode, path, ex);
  1668. ext4_ext_try_to_merge_up(handle, inode, path);
  1669. }
  1670. /*
  1671. * check if a portion of the "newext" extent overlaps with an
  1672. * existing extent.
  1673. *
  1674. * If there is an overlap discovered, it updates the length of the newext
  1675. * such that there will be no overlap, and then returns 1.
  1676. * If there is no overlap found, it returns 0.
  1677. */
  1678. static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
  1679. struct inode *inode,
  1680. struct ext4_extent *newext,
  1681. struct ext4_ext_path *path)
  1682. {
  1683. ext4_lblk_t b1, b2;
  1684. unsigned int depth, len1;
  1685. unsigned int ret = 0;
  1686. b1 = le32_to_cpu(newext->ee_block);
  1687. len1 = ext4_ext_get_actual_len(newext);
  1688. depth = ext_depth(inode);
  1689. if (!path[depth].p_ext)
  1690. goto out;
  1691. b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
  1692. /*
  1693. * get the next allocated block if the extent in the path
  1694. * is before the requested block(s)
  1695. */
  1696. if (b2 < b1) {
  1697. b2 = ext4_ext_next_allocated_block(path);
  1698. if (b2 == EXT_MAX_BLOCKS)
  1699. goto out;
  1700. b2 = EXT4_LBLK_CMASK(sbi, b2);
  1701. }
  1702. /* check for wrap through zero on extent logical start block*/
  1703. if (b1 + len1 < b1) {
  1704. len1 = EXT_MAX_BLOCKS - b1;
  1705. newext->ee_len = cpu_to_le16(len1);
  1706. ret = 1;
  1707. }
  1708. /* check for overlap */
  1709. if (b1 + len1 > b2) {
  1710. newext->ee_len = cpu_to_le16(b2 - b1);
  1711. ret = 1;
  1712. }
  1713. out:
  1714. return ret;
  1715. }
  1716. /*
  1717. * ext4_ext_insert_extent:
  1718. * tries to merge requested extent into the existing extent or
  1719. * inserts requested extent as new one into the tree,
  1720. * creating new leaf in the no-space case.
  1721. */
  1722. struct ext4_ext_path *
  1723. ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  1724. struct ext4_ext_path *path,
  1725. struct ext4_extent *newext, int gb_flags)
  1726. {
  1727. struct ext4_extent_header *eh;
  1728. struct ext4_extent *ex, *fex;
  1729. struct ext4_extent *nearex; /* nearest extent */
  1730. int depth, len, err = 0;
  1731. ext4_lblk_t next;
  1732. int mb_flags = 0, unwritten;
  1733. if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  1734. mb_flags |= EXT4_MB_DELALLOC_RESERVED;
  1735. if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
  1736. EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
  1737. err = -EFSCORRUPTED;
  1738. goto errout;
  1739. }
  1740. depth = ext_depth(inode);
  1741. ex = path[depth].p_ext;
  1742. eh = path[depth].p_hdr;
  1743. if (unlikely(path[depth].p_hdr == NULL)) {
  1744. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  1745. err = -EFSCORRUPTED;
  1746. goto errout;
  1747. }
  1748. /* try to insert block into found extent and return */
  1749. if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
  1750. /*
  1751. * Try to see whether we should rather test the extent on
  1752. * right from ex, or from the left of ex. This is because
  1753. * ext4_find_extent() can return either extent on the
  1754. * left, or on the right from the searched position. This
  1755. * will make merging more effective.
  1756. */
  1757. if (ex < EXT_LAST_EXTENT(eh) &&
  1758. (le32_to_cpu(ex->ee_block) +
  1759. ext4_ext_get_actual_len(ex) <
  1760. le32_to_cpu(newext->ee_block))) {
  1761. ex += 1;
  1762. goto prepend;
  1763. } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
  1764. (le32_to_cpu(newext->ee_block) +
  1765. ext4_ext_get_actual_len(newext) <
  1766. le32_to_cpu(ex->ee_block)))
  1767. ex -= 1;
  1768. /* Try to append newex to the ex */
  1769. if (ext4_can_extents_be_merged(inode, ex, newext)) {
  1770. ext_debug(inode, "append [%d]%d block to %u:[%d]%d"
  1771. "(from %llu)\n",
  1772. ext4_ext_is_unwritten(newext),
  1773. ext4_ext_get_actual_len(newext),
  1774. le32_to_cpu(ex->ee_block),
  1775. ext4_ext_is_unwritten(ex),
  1776. ext4_ext_get_actual_len(ex),
  1777. ext4_ext_pblock(ex));
  1778. err = ext4_ext_get_access(handle, inode,
  1779. path + depth);
  1780. if (err)
  1781. goto errout;
  1782. unwritten = ext4_ext_is_unwritten(ex);
  1783. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1784. + ext4_ext_get_actual_len(newext));
  1785. if (unwritten)
  1786. ext4_ext_mark_unwritten(ex);
  1787. nearex = ex;
  1788. goto merge;
  1789. }
  1790. prepend:
  1791. /* Try to prepend newex to the ex */
  1792. if (ext4_can_extents_be_merged(inode, newext, ex)) {
  1793. ext_debug(inode, "prepend %u[%d]%d block to %u:[%d]%d"
  1794. "(from %llu)\n",
  1795. le32_to_cpu(newext->ee_block),
  1796. ext4_ext_is_unwritten(newext),
  1797. ext4_ext_get_actual_len(newext),
  1798. le32_to_cpu(ex->ee_block),
  1799. ext4_ext_is_unwritten(ex),
  1800. ext4_ext_get_actual_len(ex),
  1801. ext4_ext_pblock(ex));
  1802. err = ext4_ext_get_access(handle, inode,
  1803. path + depth);
  1804. if (err)
  1805. goto errout;
  1806. unwritten = ext4_ext_is_unwritten(ex);
  1807. ex->ee_block = newext->ee_block;
  1808. ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
  1809. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1810. + ext4_ext_get_actual_len(newext));
  1811. if (unwritten)
  1812. ext4_ext_mark_unwritten(ex);
  1813. nearex = ex;
  1814. goto merge;
  1815. }
  1816. }
  1817. depth = ext_depth(inode);
  1818. eh = path[depth].p_hdr;
  1819. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1820. goto has_space;
  1821. /* probably next leaf has space for us? */
  1822. fex = EXT_LAST_EXTENT(eh);
  1823. next = EXT_MAX_BLOCKS;
  1824. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
  1825. next = ext4_ext_next_leaf_block(path);
  1826. if (next != EXT_MAX_BLOCKS) {
  1827. struct ext4_ext_path *npath;
  1828. ext_debug(inode, "next leaf block - %u\n", next);
  1829. npath = ext4_find_extent(inode, next, NULL, gb_flags);
  1830. if (IS_ERR(npath)) {
  1831. err = PTR_ERR(npath);
  1832. goto errout;
  1833. }
  1834. BUG_ON(npath->p_depth != path->p_depth);
  1835. eh = npath[depth].p_hdr;
  1836. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1837. ext_debug(inode, "next leaf isn't full(%d)\n",
  1838. le16_to_cpu(eh->eh_entries));
  1839. ext4_free_ext_path(path);
  1840. path = npath;
  1841. goto has_space;
  1842. }
  1843. ext_debug(inode, "next leaf has no free space(%d,%d)\n",
  1844. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1845. ext4_free_ext_path(npath);
  1846. }
  1847. /*
  1848. * There is no free space in the found leaf.
  1849. * We're gonna add a new leaf in the tree.
  1850. */
  1851. if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
  1852. mb_flags |= EXT4_MB_USE_RESERVED;
  1853. path = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
  1854. path, newext);
  1855. if (IS_ERR(path))
  1856. return path;
  1857. depth = ext_depth(inode);
  1858. eh = path[depth].p_hdr;
  1859. has_space:
  1860. nearex = path[depth].p_ext;
  1861. err = ext4_ext_get_access(handle, inode, path + depth);
  1862. if (err)
  1863. goto errout;
  1864. if (!nearex) {
  1865. /* there is no extent in this leaf, create first one */
  1866. ext_debug(inode, "first extent in the leaf: %u:%llu:[%d]%d\n",
  1867. le32_to_cpu(newext->ee_block),
  1868. ext4_ext_pblock(newext),
  1869. ext4_ext_is_unwritten(newext),
  1870. ext4_ext_get_actual_len(newext));
  1871. nearex = EXT_FIRST_EXTENT(eh);
  1872. } else {
  1873. if (le32_to_cpu(newext->ee_block)
  1874. > le32_to_cpu(nearex->ee_block)) {
  1875. /* Insert after */
  1876. ext_debug(inode, "insert %u:%llu:[%d]%d before: "
  1877. "nearest %p\n",
  1878. le32_to_cpu(newext->ee_block),
  1879. ext4_ext_pblock(newext),
  1880. ext4_ext_is_unwritten(newext),
  1881. ext4_ext_get_actual_len(newext),
  1882. nearex);
  1883. nearex++;
  1884. } else {
  1885. /* Insert before */
  1886. BUG_ON(newext->ee_block == nearex->ee_block);
  1887. ext_debug(inode, "insert %u:%llu:[%d]%d after: "
  1888. "nearest %p\n",
  1889. le32_to_cpu(newext->ee_block),
  1890. ext4_ext_pblock(newext),
  1891. ext4_ext_is_unwritten(newext),
  1892. ext4_ext_get_actual_len(newext),
  1893. nearex);
  1894. }
  1895. len = EXT_LAST_EXTENT(eh) - nearex + 1;
  1896. if (len > 0) {
  1897. ext_debug(inode, "insert %u:%llu:[%d]%d: "
  1898. "move %d extents from 0x%p to 0x%p\n",
  1899. le32_to_cpu(newext->ee_block),
  1900. ext4_ext_pblock(newext),
  1901. ext4_ext_is_unwritten(newext),
  1902. ext4_ext_get_actual_len(newext),
  1903. len, nearex, nearex + 1);
  1904. memmove(nearex + 1, nearex,
  1905. len * sizeof(struct ext4_extent));
  1906. }
  1907. }
  1908. le16_add_cpu(&eh->eh_entries, 1);
  1909. path[depth].p_ext = nearex;
  1910. nearex->ee_block = newext->ee_block;
  1911. ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
  1912. nearex->ee_len = newext->ee_len;
  1913. merge:
  1914. /* try to merge extents */
  1915. if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
  1916. ext4_ext_try_to_merge(handle, inode, path, nearex);
  1917. /* time to correct all indexes above */
  1918. err = ext4_ext_correct_indexes(handle, inode, path);
  1919. if (err)
  1920. goto errout;
  1921. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  1922. if (err)
  1923. goto errout;
  1924. return path;
  1925. errout:
  1926. ext4_free_ext_path(path);
  1927. return ERR_PTR(err);
  1928. }
  1929. static int ext4_fill_es_cache_info(struct inode *inode,
  1930. ext4_lblk_t block, ext4_lblk_t num,
  1931. struct fiemap_extent_info *fieinfo)
  1932. {
  1933. ext4_lblk_t next, end = block + num - 1;
  1934. struct extent_status es;
  1935. unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
  1936. unsigned int flags;
  1937. int err;
  1938. while (block <= end) {
  1939. next = 0;
  1940. flags = 0;
  1941. if (!ext4_es_lookup_extent(inode, block, &next, &es))
  1942. break;
  1943. if (ext4_es_is_unwritten(&es))
  1944. flags |= FIEMAP_EXTENT_UNWRITTEN;
  1945. if (ext4_es_is_delayed(&es))
  1946. flags |= (FIEMAP_EXTENT_DELALLOC |
  1947. FIEMAP_EXTENT_UNKNOWN);
  1948. if (ext4_es_is_hole(&es))
  1949. flags |= EXT4_FIEMAP_EXTENT_HOLE;
  1950. if (next == 0)
  1951. flags |= FIEMAP_EXTENT_LAST;
  1952. if (flags & (FIEMAP_EXTENT_DELALLOC|
  1953. EXT4_FIEMAP_EXTENT_HOLE))
  1954. es.es_pblk = 0;
  1955. else
  1956. es.es_pblk = ext4_es_pblock(&es);
  1957. err = fiemap_fill_next_extent(fieinfo,
  1958. (__u64)es.es_lblk << blksize_bits,
  1959. (__u64)es.es_pblk << blksize_bits,
  1960. (__u64)es.es_len << blksize_bits,
  1961. flags);
  1962. if (next == 0)
  1963. break;
  1964. block = next;
  1965. if (err < 0)
  1966. return err;
  1967. if (err == 1)
  1968. return 0;
  1969. }
  1970. return 0;
  1971. }
  1972. /*
  1973. * ext4_ext_find_hole - find hole around given block according to the given path
  1974. * @inode: inode we lookup in
  1975. * @path: path in extent tree to @lblk
  1976. * @lblk: pointer to logical block around which we want to determine hole
  1977. *
  1978. * Determine hole length (and start if easily possible) around given logical
  1979. * block. We don't try too hard to find the beginning of the hole but @path
  1980. * actually points to extent before @lblk, we provide it.
  1981. *
  1982. * The function returns the length of a hole starting at @lblk. We update @lblk
  1983. * to the beginning of the hole if we managed to find it.
  1984. */
  1985. static ext4_lblk_t ext4_ext_find_hole(struct inode *inode,
  1986. struct ext4_ext_path *path,
  1987. ext4_lblk_t *lblk)
  1988. {
  1989. int depth = ext_depth(inode);
  1990. struct ext4_extent *ex;
  1991. ext4_lblk_t len;
  1992. ex = path[depth].p_ext;
  1993. if (ex == NULL) {
  1994. /* there is no extent yet, so gap is [0;-] */
  1995. *lblk = 0;
  1996. len = EXT_MAX_BLOCKS;
  1997. } else if (*lblk < le32_to_cpu(ex->ee_block)) {
  1998. len = le32_to_cpu(ex->ee_block) - *lblk;
  1999. } else if (*lblk >= le32_to_cpu(ex->ee_block)
  2000. + ext4_ext_get_actual_len(ex)) {
  2001. ext4_lblk_t next;
  2002. *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
  2003. next = ext4_ext_next_allocated_block(path);
  2004. BUG_ON(next == *lblk);
  2005. len = next - *lblk;
  2006. } else {
  2007. BUG();
  2008. }
  2009. return len;
  2010. }
  2011. /*
  2012. * ext4_ext_rm_idx:
  2013. * removes index from the index block.
  2014. */
  2015. static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  2016. struct ext4_ext_path *path, int depth)
  2017. {
  2018. int err;
  2019. ext4_fsblk_t leaf;
  2020. int k = depth - 1;
  2021. /* free index block */
  2022. leaf = ext4_idx_pblock(path[k].p_idx);
  2023. if (unlikely(path[k].p_hdr->eh_entries == 0)) {
  2024. EXT4_ERROR_INODE(inode, "path[%d].p_hdr->eh_entries == 0", k);
  2025. return -EFSCORRUPTED;
  2026. }
  2027. err = ext4_ext_get_access(handle, inode, path + k);
  2028. if (err)
  2029. return err;
  2030. if (path[k].p_idx != EXT_LAST_INDEX(path[k].p_hdr)) {
  2031. int len = EXT_LAST_INDEX(path[k].p_hdr) - path[k].p_idx;
  2032. len *= sizeof(struct ext4_extent_idx);
  2033. memmove(path[k].p_idx, path[k].p_idx + 1, len);
  2034. }
  2035. le16_add_cpu(&path[k].p_hdr->eh_entries, -1);
  2036. err = ext4_ext_dirty(handle, inode, path + k);
  2037. if (err)
  2038. return err;
  2039. ext_debug(inode, "index is empty, remove it, free block %llu\n", leaf);
  2040. trace_ext4_ext_rm_idx(inode, leaf);
  2041. ext4_free_blocks(handle, inode, NULL, leaf, 1,
  2042. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  2043. while (--k >= 0) {
  2044. if (path[k + 1].p_idx != EXT_FIRST_INDEX(path[k + 1].p_hdr))
  2045. break;
  2046. err = ext4_ext_get_access(handle, inode, path + k);
  2047. if (err)
  2048. goto clean;
  2049. path[k].p_idx->ei_block = path[k + 1].p_idx->ei_block;
  2050. err = ext4_ext_dirty(handle, inode, path + k);
  2051. if (err)
  2052. goto clean;
  2053. }
  2054. return 0;
  2055. clean:
  2056. /*
  2057. * The path[k].p_bh is either unmodified or with no verified bit
  2058. * set (see ext4_ext_get_access()). So just clear the verified bit
  2059. * of the successfully modified extents buffers, which will force
  2060. * these extents to be checked to avoid using inconsistent data.
  2061. */
  2062. while (++k < depth)
  2063. clear_buffer_verified(path[k].p_bh);
  2064. return err;
  2065. }
  2066. /*
  2067. * ext4_ext_calc_credits_for_single_extent:
  2068. * This routine returns max. credits that needed to insert an extent
  2069. * to the extent tree.
  2070. * When pass the actual path, the caller should calculate credits
  2071. * under i_data_sem.
  2072. */
  2073. int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
  2074. struct ext4_ext_path *path)
  2075. {
  2076. if (path) {
  2077. int depth = ext_depth(inode);
  2078. int ret = 0;
  2079. /* probably there is space in leaf? */
  2080. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  2081. < le16_to_cpu(path[depth].p_hdr->eh_max)) {
  2082. /*
  2083. * There are some space in the leaf tree, no
  2084. * need to account for leaf block credit
  2085. *
  2086. * bitmaps and block group descriptor blocks
  2087. * and other metadata blocks still need to be
  2088. * accounted.
  2089. */
  2090. /* 1 bitmap, 1 block group descriptor */
  2091. ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
  2092. return ret;
  2093. }
  2094. }
  2095. return ext4_chunk_trans_blocks(inode, nrblocks);
  2096. }
  2097. /*
  2098. * How many index/leaf blocks need to change/allocate to add @extents extents?
  2099. *
  2100. * If we add a single extent, then in the worse case, each tree level
  2101. * index/leaf need to be changed in case of the tree split.
  2102. *
  2103. * If more extents are inserted, they could cause the whole tree split more
  2104. * than once, but this is really rare.
  2105. */
  2106. int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
  2107. {
  2108. int index;
  2109. /* If we are converting the inline data, only one is needed here. */
  2110. if (ext4_has_inline_data(inode))
  2111. return 1;
  2112. /*
  2113. * Extent tree can change between the time we estimate credits and
  2114. * the time we actually modify the tree. Assume the worst case.
  2115. */
  2116. if (extents <= 1)
  2117. index = EXT4_MAX_EXTENT_DEPTH * 2;
  2118. else
  2119. index = EXT4_MAX_EXTENT_DEPTH * 3;
  2120. return index;
  2121. }
  2122. static inline int get_default_free_blocks_flags(struct inode *inode)
  2123. {
  2124. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
  2125. ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
  2126. return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
  2127. else if (ext4_should_journal_data(inode))
  2128. return EXT4_FREE_BLOCKS_FORGET;
  2129. return 0;
  2130. }
  2131. /*
  2132. * ext4_rereserve_cluster - increment the reserved cluster count when
  2133. * freeing a cluster with a pending reservation
  2134. *
  2135. * @inode - file containing the cluster
  2136. * @lblk - logical block in cluster to be reserved
  2137. *
  2138. * Increments the reserved cluster count and adjusts quota in a bigalloc
  2139. * file system when freeing a partial cluster containing at least one
  2140. * delayed and unwritten block. A partial cluster meeting that
  2141. * requirement will have a pending reservation. If so, the
  2142. * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
  2143. * defer reserved and allocated space accounting to a subsequent call
  2144. * to this function.
  2145. */
  2146. static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
  2147. {
  2148. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2149. struct ext4_inode_info *ei = EXT4_I(inode);
  2150. dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
  2151. spin_lock(&ei->i_block_reservation_lock);
  2152. ei->i_reserved_data_blocks++;
  2153. percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
  2154. spin_unlock(&ei->i_block_reservation_lock);
  2155. percpu_counter_add(&sbi->s_freeclusters_counter, 1);
  2156. ext4_remove_pending(inode, lblk);
  2157. }
  2158. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  2159. struct ext4_extent *ex,
  2160. struct partial_cluster *partial,
  2161. ext4_lblk_t from, ext4_lblk_t to)
  2162. {
  2163. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2164. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  2165. ext4_fsblk_t last_pblk, pblk;
  2166. ext4_lblk_t num;
  2167. int flags;
  2168. /* only extent tail removal is allowed */
  2169. if (from < le32_to_cpu(ex->ee_block) ||
  2170. to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
  2171. ext4_error(sbi->s_sb,
  2172. "strange request: removal(2) %u-%u from %u:%u",
  2173. from, to, le32_to_cpu(ex->ee_block), ee_len);
  2174. return 0;
  2175. }
  2176. #ifdef EXTENTS_STATS
  2177. spin_lock(&sbi->s_ext_stats_lock);
  2178. sbi->s_ext_blocks += ee_len;
  2179. sbi->s_ext_extents++;
  2180. if (ee_len < sbi->s_ext_min)
  2181. sbi->s_ext_min = ee_len;
  2182. if (ee_len > sbi->s_ext_max)
  2183. sbi->s_ext_max = ee_len;
  2184. if (ext_depth(inode) > sbi->s_depth_max)
  2185. sbi->s_depth_max = ext_depth(inode);
  2186. spin_unlock(&sbi->s_ext_stats_lock);
  2187. #endif
  2188. trace_ext4_remove_blocks(inode, ex, from, to, partial);
  2189. /*
  2190. * if we have a partial cluster, and it's different from the
  2191. * cluster of the last block in the extent, we free it
  2192. */
  2193. last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
  2194. if (partial->state != initial &&
  2195. partial->pclu != EXT4_B2C(sbi, last_pblk)) {
  2196. if (partial->state == tofree) {
  2197. flags = get_default_free_blocks_flags(inode);
  2198. if (ext4_is_pending(inode, partial->lblk))
  2199. flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
  2200. ext4_free_blocks(handle, inode, NULL,
  2201. EXT4_C2B(sbi, partial->pclu),
  2202. sbi->s_cluster_ratio, flags);
  2203. if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
  2204. ext4_rereserve_cluster(inode, partial->lblk);
  2205. }
  2206. partial->state = initial;
  2207. }
  2208. num = le32_to_cpu(ex->ee_block) + ee_len - from;
  2209. pblk = ext4_ext_pblock(ex) + ee_len - num;
  2210. /*
  2211. * We free the partial cluster at the end of the extent (if any),
  2212. * unless the cluster is used by another extent (partial_cluster
  2213. * state is nofree). If a partial cluster exists here, it must be
  2214. * shared with the last block in the extent.
  2215. */
  2216. flags = get_default_free_blocks_flags(inode);
  2217. /* partial, left end cluster aligned, right end unaligned */
  2218. if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
  2219. (EXT4_LBLK_CMASK(sbi, to) >= from) &&
  2220. (partial->state != nofree)) {
  2221. if (ext4_is_pending(inode, to))
  2222. flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
  2223. ext4_free_blocks(handle, inode, NULL,
  2224. EXT4_PBLK_CMASK(sbi, last_pblk),
  2225. sbi->s_cluster_ratio, flags);
  2226. if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
  2227. ext4_rereserve_cluster(inode, to);
  2228. partial->state = initial;
  2229. flags = get_default_free_blocks_flags(inode);
  2230. }
  2231. flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
  2232. /*
  2233. * For bigalloc file systems, we never free a partial cluster
  2234. * at the beginning of the extent. Instead, we check to see if we
  2235. * need to free it on a subsequent call to ext4_remove_blocks,
  2236. * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
  2237. */
  2238. flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
  2239. ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
  2240. /* reset the partial cluster if we've freed past it */
  2241. if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
  2242. partial->state = initial;
  2243. /*
  2244. * If we've freed the entire extent but the beginning is not left
  2245. * cluster aligned and is not marked as ineligible for freeing we
  2246. * record the partial cluster at the beginning of the extent. It
  2247. * wasn't freed by the preceding ext4_free_blocks() call, and we
  2248. * need to look farther to the left to determine if it's to be freed
  2249. * (not shared with another extent). Else, reset the partial
  2250. * cluster - we're either done freeing or the beginning of the
  2251. * extent is left cluster aligned.
  2252. */
  2253. if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
  2254. if (partial->state == initial) {
  2255. partial->pclu = EXT4_B2C(sbi, pblk);
  2256. partial->lblk = from;
  2257. partial->state = tofree;
  2258. }
  2259. } else {
  2260. partial->state = initial;
  2261. }
  2262. return 0;
  2263. }
  2264. /*
  2265. * ext4_ext_rm_leaf() Removes the extents associated with the
  2266. * blocks appearing between "start" and "end". Both "start"
  2267. * and "end" must appear in the same extent or EIO is returned.
  2268. *
  2269. * @handle: The journal handle
  2270. * @inode: The files inode
  2271. * @path: The path to the leaf
  2272. * @partial_cluster: The cluster which we'll have to free if all extents
  2273. * has been released from it. However, if this value is
  2274. * negative, it's a cluster just to the right of the
  2275. * punched region and it must not be freed.
  2276. * @start: The first block to remove
  2277. * @end: The last block to remove
  2278. */
  2279. static int
  2280. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  2281. struct ext4_ext_path *path,
  2282. struct partial_cluster *partial,
  2283. ext4_lblk_t start, ext4_lblk_t end)
  2284. {
  2285. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2286. int err = 0, correct_index = 0;
  2287. int depth = ext_depth(inode), credits, revoke_credits;
  2288. struct ext4_extent_header *eh;
  2289. ext4_lblk_t a, b;
  2290. unsigned num;
  2291. ext4_lblk_t ex_ee_block;
  2292. unsigned short ex_ee_len;
  2293. unsigned unwritten = 0;
  2294. struct ext4_extent *ex;
  2295. ext4_fsblk_t pblk;
  2296. /* the header must be checked already in ext4_ext_remove_space() */
  2297. ext_debug(inode, "truncate since %u in leaf to %u\n", start, end);
  2298. if (!path[depth].p_hdr)
  2299. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  2300. eh = path[depth].p_hdr;
  2301. if (unlikely(path[depth].p_hdr == NULL)) {
  2302. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  2303. return -EFSCORRUPTED;
  2304. }
  2305. /* find where to start removing */
  2306. ex = path[depth].p_ext;
  2307. if (!ex)
  2308. ex = EXT_LAST_EXTENT(eh);
  2309. ex_ee_block = le32_to_cpu(ex->ee_block);
  2310. ex_ee_len = ext4_ext_get_actual_len(ex);
  2311. trace_ext4_ext_rm_leaf(inode, start, ex, partial);
  2312. while (ex >= EXT_FIRST_EXTENT(eh) &&
  2313. ex_ee_block + ex_ee_len > start) {
  2314. if (ext4_ext_is_unwritten(ex))
  2315. unwritten = 1;
  2316. else
  2317. unwritten = 0;
  2318. ext_debug(inode, "remove ext %u:[%d]%d\n", ex_ee_block,
  2319. unwritten, ex_ee_len);
  2320. path[depth].p_ext = ex;
  2321. a = max(ex_ee_block, start);
  2322. b = min(ex_ee_block + ex_ee_len - 1, end);
  2323. ext_debug(inode, " border %u:%u\n", a, b);
  2324. /* If this extent is beyond the end of the hole, skip it */
  2325. if (end < ex_ee_block) {
  2326. /*
  2327. * We're going to skip this extent and move to another,
  2328. * so note that its first cluster is in use to avoid
  2329. * freeing it when removing blocks. Eventually, the
  2330. * right edge of the truncated/punched region will
  2331. * be just to the left.
  2332. */
  2333. if (sbi->s_cluster_ratio > 1) {
  2334. pblk = ext4_ext_pblock(ex);
  2335. partial->pclu = EXT4_B2C(sbi, pblk);
  2336. partial->state = nofree;
  2337. }
  2338. ex--;
  2339. ex_ee_block = le32_to_cpu(ex->ee_block);
  2340. ex_ee_len = ext4_ext_get_actual_len(ex);
  2341. continue;
  2342. } else if (b != ex_ee_block + ex_ee_len - 1) {
  2343. EXT4_ERROR_INODE(inode,
  2344. "can not handle truncate %u:%u "
  2345. "on extent %u:%u",
  2346. start, end, ex_ee_block,
  2347. ex_ee_block + ex_ee_len - 1);
  2348. err = -EFSCORRUPTED;
  2349. goto out;
  2350. } else if (a != ex_ee_block) {
  2351. /* remove tail of the extent */
  2352. num = a - ex_ee_block;
  2353. } else {
  2354. /* remove whole extent: excellent! */
  2355. num = 0;
  2356. }
  2357. /*
  2358. * 3 for leaf, sb, and inode plus 2 (bmap and group
  2359. * descriptor) for each block group; assume two block
  2360. * groups plus ex_ee_len/blocks_per_block_group for
  2361. * the worst case
  2362. */
  2363. credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
  2364. if (ex == EXT_FIRST_EXTENT(eh)) {
  2365. correct_index = 1;
  2366. credits += (ext_depth(inode)) + 1;
  2367. }
  2368. credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
  2369. /*
  2370. * We may end up freeing some index blocks and data from the
  2371. * punched range. Note that partial clusters are accounted for
  2372. * by ext4_free_data_revoke_credits().
  2373. */
  2374. revoke_credits =
  2375. ext4_free_metadata_revoke_credits(inode->i_sb,
  2376. ext_depth(inode)) +
  2377. ext4_free_data_revoke_credits(inode, b - a + 1);
  2378. err = ext4_datasem_ensure_credits(handle, inode, credits,
  2379. credits, revoke_credits);
  2380. if (err) {
  2381. if (err > 0)
  2382. err = -EAGAIN;
  2383. goto out;
  2384. }
  2385. err = ext4_ext_get_access(handle, inode, path + depth);
  2386. if (err)
  2387. goto out;
  2388. err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
  2389. if (err)
  2390. goto out;
  2391. if (num == 0)
  2392. /* this extent is removed; mark slot entirely unused */
  2393. ext4_ext_store_pblock(ex, 0);
  2394. ex->ee_len = cpu_to_le16(num);
  2395. /*
  2396. * Do not mark unwritten if all the blocks in the
  2397. * extent have been removed.
  2398. */
  2399. if (unwritten && num)
  2400. ext4_ext_mark_unwritten(ex);
  2401. /*
  2402. * If the extent was completely released,
  2403. * we need to remove it from the leaf
  2404. */
  2405. if (num == 0) {
  2406. if (end != EXT_MAX_BLOCKS - 1) {
  2407. /*
  2408. * For hole punching, we need to scoot all the
  2409. * extents up when an extent is removed so that
  2410. * we dont have blank extents in the middle
  2411. */
  2412. memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
  2413. sizeof(struct ext4_extent));
  2414. /* Now get rid of the one at the end */
  2415. memset(EXT_LAST_EXTENT(eh), 0,
  2416. sizeof(struct ext4_extent));
  2417. }
  2418. le16_add_cpu(&eh->eh_entries, -1);
  2419. }
  2420. err = ext4_ext_dirty(handle, inode, path + depth);
  2421. if (err)
  2422. goto out;
  2423. ext_debug(inode, "new extent: %u:%u:%llu\n", ex_ee_block, num,
  2424. ext4_ext_pblock(ex));
  2425. ex--;
  2426. ex_ee_block = le32_to_cpu(ex->ee_block);
  2427. ex_ee_len = ext4_ext_get_actual_len(ex);
  2428. }
  2429. if (correct_index && eh->eh_entries)
  2430. err = ext4_ext_correct_indexes(handle, inode, path);
  2431. /*
  2432. * If there's a partial cluster and at least one extent remains in
  2433. * the leaf, free the partial cluster if it isn't shared with the
  2434. * current extent. If it is shared with the current extent
  2435. * we reset the partial cluster because we've reached the start of the
  2436. * truncated/punched region and we're done removing blocks.
  2437. */
  2438. if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
  2439. pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
  2440. if (partial->pclu != EXT4_B2C(sbi, pblk)) {
  2441. int flags = get_default_free_blocks_flags(inode);
  2442. if (ext4_is_pending(inode, partial->lblk))
  2443. flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
  2444. ext4_free_blocks(handle, inode, NULL,
  2445. EXT4_C2B(sbi, partial->pclu),
  2446. sbi->s_cluster_ratio, flags);
  2447. if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
  2448. ext4_rereserve_cluster(inode, partial->lblk);
  2449. }
  2450. partial->state = initial;
  2451. }
  2452. /* if this leaf is free, then we should
  2453. * remove it from index block above */
  2454. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  2455. err = ext4_ext_rm_idx(handle, inode, path, depth);
  2456. out:
  2457. return err;
  2458. }
  2459. /*
  2460. * ext4_ext_more_to_rm:
  2461. * returns 1 if current index has to be freed (even partial)
  2462. */
  2463. static int
  2464. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  2465. {
  2466. BUG_ON(path->p_idx == NULL);
  2467. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  2468. return 0;
  2469. /*
  2470. * if truncate on deeper level happened, it wasn't partial,
  2471. * so we have to consider current index for truncation
  2472. */
  2473. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  2474. return 0;
  2475. return 1;
  2476. }
  2477. int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
  2478. ext4_lblk_t end)
  2479. {
  2480. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2481. int depth = ext_depth(inode);
  2482. struct ext4_ext_path *path = NULL;
  2483. struct partial_cluster partial;
  2484. handle_t *handle;
  2485. int i = 0, err = 0;
  2486. partial.pclu = 0;
  2487. partial.lblk = 0;
  2488. partial.state = initial;
  2489. ext_debug(inode, "truncate since %u to %u\n", start, end);
  2490. /* probably first extent we're gonna free will be last in block */
  2491. handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE,
  2492. depth + 1,
  2493. ext4_free_metadata_revoke_credits(inode->i_sb, depth));
  2494. if (IS_ERR(handle))
  2495. return PTR_ERR(handle);
  2496. again:
  2497. trace_ext4_ext_remove_space(inode, start, end, depth);
  2498. /*
  2499. * Check if we are removing extents inside the extent tree. If that
  2500. * is the case, we are going to punch a hole inside the extent tree
  2501. * so we have to check whether we need to split the extent covering
  2502. * the last block to remove so we can easily remove the part of it
  2503. * in ext4_ext_rm_leaf().
  2504. */
  2505. if (end < EXT_MAX_BLOCKS - 1) {
  2506. struct ext4_extent *ex;
  2507. ext4_lblk_t ee_block, ex_end, lblk;
  2508. ext4_fsblk_t pblk;
  2509. /* find extent for or closest extent to this block */
  2510. path = ext4_find_extent(inode, end, NULL,
  2511. EXT4_EX_NOCACHE | EXT4_EX_NOFAIL);
  2512. if (IS_ERR(path)) {
  2513. ext4_journal_stop(handle);
  2514. return PTR_ERR(path);
  2515. }
  2516. depth = ext_depth(inode);
  2517. /* Leaf not may not exist only if inode has no blocks at all */
  2518. ex = path[depth].p_ext;
  2519. if (!ex) {
  2520. if (depth) {
  2521. EXT4_ERROR_INODE(inode,
  2522. "path[%d].p_hdr == NULL",
  2523. depth);
  2524. err = -EFSCORRUPTED;
  2525. }
  2526. goto out;
  2527. }
  2528. ee_block = le32_to_cpu(ex->ee_block);
  2529. ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
  2530. /*
  2531. * See if the last block is inside the extent, if so split
  2532. * the extent at 'end' block so we can easily remove the
  2533. * tail of the first part of the split extent in
  2534. * ext4_ext_rm_leaf().
  2535. */
  2536. if (end >= ee_block && end < ex_end) {
  2537. /*
  2538. * If we're going to split the extent, note that
  2539. * the cluster containing the block after 'end' is
  2540. * in use to avoid freeing it when removing blocks.
  2541. */
  2542. if (sbi->s_cluster_ratio > 1) {
  2543. pblk = ext4_ext_pblock(ex) + end - ee_block + 1;
  2544. partial.pclu = EXT4_B2C(sbi, pblk);
  2545. partial.state = nofree;
  2546. }
  2547. /*
  2548. * Split the extent in two so that 'end' is the last
  2549. * block in the first new extent. Also we should not
  2550. * fail removing space due to ENOSPC so try to use
  2551. * reserved block if that happens.
  2552. */
  2553. path = ext4_force_split_extent_at(handle, inode, path,
  2554. end + 1, 1);
  2555. if (IS_ERR(path)) {
  2556. err = PTR_ERR(path);
  2557. goto out;
  2558. }
  2559. } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
  2560. partial.state == initial) {
  2561. /*
  2562. * If we're punching, there's an extent to the right.
  2563. * If the partial cluster hasn't been set, set it to
  2564. * that extent's first cluster and its state to nofree
  2565. * so it won't be freed should it contain blocks to be
  2566. * removed. If it's already set (tofree/nofree), we're
  2567. * retrying and keep the original partial cluster info
  2568. * so a cluster marked tofree as a result of earlier
  2569. * extent removal is not lost.
  2570. */
  2571. lblk = ex_end + 1;
  2572. err = ext4_ext_search_right(inode, path, &lblk, &pblk,
  2573. NULL);
  2574. if (err < 0)
  2575. goto out;
  2576. if (pblk) {
  2577. partial.pclu = EXT4_B2C(sbi, pblk);
  2578. partial.state = nofree;
  2579. }
  2580. }
  2581. }
  2582. /*
  2583. * We start scanning from right side, freeing all the blocks
  2584. * after i_size and walking into the tree depth-wise.
  2585. */
  2586. depth = ext_depth(inode);
  2587. if (path) {
  2588. int k = i = depth;
  2589. while (--k > 0)
  2590. path[k].p_block =
  2591. le16_to_cpu(path[k].p_hdr->eh_entries)+1;
  2592. } else {
  2593. path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
  2594. GFP_NOFS | __GFP_NOFAIL);
  2595. if (path == NULL) {
  2596. ext4_journal_stop(handle);
  2597. return -ENOMEM;
  2598. }
  2599. path[0].p_maxdepth = path[0].p_depth = depth;
  2600. path[0].p_hdr = ext_inode_hdr(inode);
  2601. i = 0;
  2602. if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
  2603. err = -EFSCORRUPTED;
  2604. goto out;
  2605. }
  2606. }
  2607. err = 0;
  2608. while (i >= 0 && err == 0) {
  2609. if (i == depth) {
  2610. /* this is leaf block */
  2611. err = ext4_ext_rm_leaf(handle, inode, path,
  2612. &partial, start, end);
  2613. /* root level has p_bh == NULL, brelse() eats this */
  2614. ext4_ext_path_brelse(path + i);
  2615. i--;
  2616. continue;
  2617. }
  2618. /* this is index block */
  2619. if (!path[i].p_hdr) {
  2620. ext_debug(inode, "initialize header\n");
  2621. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  2622. }
  2623. if (!path[i].p_idx) {
  2624. /* this level hasn't been touched yet */
  2625. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  2626. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  2627. ext_debug(inode, "init index ptr: hdr 0x%p, num %d\n",
  2628. path[i].p_hdr,
  2629. le16_to_cpu(path[i].p_hdr->eh_entries));
  2630. } else {
  2631. /* we were already here, see at next index */
  2632. path[i].p_idx--;
  2633. }
  2634. ext_debug(inode, "level %d - index, first 0x%p, cur 0x%p\n",
  2635. i, EXT_FIRST_INDEX(path[i].p_hdr),
  2636. path[i].p_idx);
  2637. if (ext4_ext_more_to_rm(path + i)) {
  2638. struct buffer_head *bh;
  2639. /* go to the next level */
  2640. ext_debug(inode, "move to level %d (block %llu)\n",
  2641. i + 1, ext4_idx_pblock(path[i].p_idx));
  2642. memset(path + i + 1, 0, sizeof(*path));
  2643. bh = read_extent_tree_block(inode, path[i].p_idx,
  2644. depth - i - 1,
  2645. EXT4_EX_NOCACHE);
  2646. if (IS_ERR(bh)) {
  2647. /* should we reset i_size? */
  2648. err = PTR_ERR(bh);
  2649. break;
  2650. }
  2651. /* Yield here to deal with large extent trees.
  2652. * Should be a no-op if we did IO above. */
  2653. cond_resched();
  2654. if (WARN_ON(i + 1 > depth)) {
  2655. err = -EFSCORRUPTED;
  2656. break;
  2657. }
  2658. path[i + 1].p_bh = bh;
  2659. /* save actual number of indexes since this
  2660. * number is changed at the next iteration */
  2661. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  2662. i++;
  2663. } else {
  2664. /* we finished processing this index, go up */
  2665. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  2666. /* index is empty, remove it;
  2667. * handle must be already prepared by the
  2668. * truncatei_leaf() */
  2669. err = ext4_ext_rm_idx(handle, inode, path, i);
  2670. }
  2671. /* root level has p_bh == NULL, brelse() eats this */
  2672. ext4_ext_path_brelse(path + i);
  2673. i--;
  2674. ext_debug(inode, "return to level %d\n", i);
  2675. }
  2676. }
  2677. trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
  2678. path->p_hdr->eh_entries);
  2679. /*
  2680. * if there's a partial cluster and we have removed the first extent
  2681. * in the file, then we also free the partial cluster, if any
  2682. */
  2683. if (partial.state == tofree && err == 0) {
  2684. int flags = get_default_free_blocks_flags(inode);
  2685. if (ext4_is_pending(inode, partial.lblk))
  2686. flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
  2687. ext4_free_blocks(handle, inode, NULL,
  2688. EXT4_C2B(sbi, partial.pclu),
  2689. sbi->s_cluster_ratio, flags);
  2690. if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
  2691. ext4_rereserve_cluster(inode, partial.lblk);
  2692. partial.state = initial;
  2693. }
  2694. /* TODO: flexible tree reduction should be here */
  2695. if (path->p_hdr->eh_entries == 0) {
  2696. /*
  2697. * truncate to zero freed all the tree,
  2698. * so we need to correct eh_depth
  2699. */
  2700. err = ext4_ext_get_access(handle, inode, path);
  2701. if (err == 0) {
  2702. ext_inode_hdr(inode)->eh_depth = 0;
  2703. ext_inode_hdr(inode)->eh_max =
  2704. cpu_to_le16(ext4_ext_space_root(inode, 0));
  2705. err = ext4_ext_dirty(handle, inode, path);
  2706. }
  2707. }
  2708. out:
  2709. ext4_free_ext_path(path);
  2710. path = NULL;
  2711. if (err == -EAGAIN)
  2712. goto again;
  2713. ext4_journal_stop(handle);
  2714. return err;
  2715. }
  2716. /*
  2717. * called at mount time
  2718. */
  2719. void ext4_ext_init(struct super_block *sb)
  2720. {
  2721. /*
  2722. * possible initialization would be here
  2723. */
  2724. if (ext4_has_feature_extents(sb)) {
  2725. #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
  2726. printk(KERN_INFO "EXT4-fs: file extents enabled"
  2727. #ifdef AGGRESSIVE_TEST
  2728. ", aggressive tests"
  2729. #endif
  2730. #ifdef CHECK_BINSEARCH
  2731. ", check binsearch"
  2732. #endif
  2733. #ifdef EXTENTS_STATS
  2734. ", stats"
  2735. #endif
  2736. "\n");
  2737. #endif
  2738. #ifdef EXTENTS_STATS
  2739. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  2740. EXT4_SB(sb)->s_ext_min = 1 << 30;
  2741. EXT4_SB(sb)->s_ext_max = 0;
  2742. #endif
  2743. }
  2744. }
  2745. /*
  2746. * called at umount time
  2747. */
  2748. void ext4_ext_release(struct super_block *sb)
  2749. {
  2750. if (!ext4_has_feature_extents(sb))
  2751. return;
  2752. #ifdef EXTENTS_STATS
  2753. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  2754. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2755. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  2756. sbi->s_ext_blocks, sbi->s_ext_extents,
  2757. sbi->s_ext_blocks / sbi->s_ext_extents);
  2758. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  2759. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  2760. }
  2761. #endif
  2762. }
  2763. static void ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
  2764. {
  2765. ext4_lblk_t ee_block;
  2766. ext4_fsblk_t ee_pblock;
  2767. unsigned int ee_len;
  2768. ee_block = le32_to_cpu(ex->ee_block);
  2769. ee_len = ext4_ext_get_actual_len(ex);
  2770. ee_pblock = ext4_ext_pblock(ex);
  2771. if (ee_len == 0)
  2772. return;
  2773. ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
  2774. EXTENT_STATUS_WRITTEN, 0);
  2775. }
  2776. /* FIXME!! we need to try to merge to left or right after zero-out */
  2777. static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
  2778. {
  2779. ext4_fsblk_t ee_pblock;
  2780. unsigned int ee_len;
  2781. ee_len = ext4_ext_get_actual_len(ex);
  2782. ee_pblock = ext4_ext_pblock(ex);
  2783. return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
  2784. ee_len);
  2785. }
  2786. /*
  2787. * ext4_split_extent_at() splits an extent at given block.
  2788. *
  2789. * @handle: the journal handle
  2790. * @inode: the file inode
  2791. * @path: the path to the extent
  2792. * @split: the logical block where the extent is splitted.
  2793. * @split_flags: indicates if the extent could be zeroout if split fails, and
  2794. * the states(init or unwritten) of new extents.
  2795. * @flags: flags used to insert new extent to extent tree.
  2796. *
  2797. *
  2798. * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
  2799. * of which are determined by split_flag.
  2800. *
  2801. * There are two cases:
  2802. * a> the extent are splitted into two extent.
  2803. * b> split is not needed, and just mark the extent.
  2804. *
  2805. * Return an extent path pointer on success, or an error pointer on failure.
  2806. */
  2807. static struct ext4_ext_path *ext4_split_extent_at(handle_t *handle,
  2808. struct inode *inode,
  2809. struct ext4_ext_path *path,
  2810. ext4_lblk_t split,
  2811. int split_flag, int flags)
  2812. {
  2813. ext4_fsblk_t newblock;
  2814. ext4_lblk_t ee_block;
  2815. struct ext4_extent *ex, newex, orig_ex, zero_ex;
  2816. struct ext4_extent *ex2 = NULL;
  2817. unsigned int ee_len, depth;
  2818. int err = 0;
  2819. BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
  2820. (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
  2821. ext_debug(inode, "logical block %llu\n", (unsigned long long)split);
  2822. ext4_ext_show_leaf(inode, path);
  2823. depth = ext_depth(inode);
  2824. ex = path[depth].p_ext;
  2825. ee_block = le32_to_cpu(ex->ee_block);
  2826. ee_len = ext4_ext_get_actual_len(ex);
  2827. newblock = split - ee_block + ext4_ext_pblock(ex);
  2828. BUG_ON(split < ee_block || split >= (ee_block + ee_len));
  2829. BUG_ON(!ext4_ext_is_unwritten(ex) &&
  2830. split_flag & (EXT4_EXT_MAY_ZEROOUT |
  2831. EXT4_EXT_MARK_UNWRIT1 |
  2832. EXT4_EXT_MARK_UNWRIT2));
  2833. err = ext4_ext_get_access(handle, inode, path + depth);
  2834. if (err)
  2835. goto out;
  2836. if (split == ee_block) {
  2837. /*
  2838. * case b: block @split is the block that the extent begins with
  2839. * then we just change the state of the extent, and splitting
  2840. * is not needed.
  2841. */
  2842. if (split_flag & EXT4_EXT_MARK_UNWRIT2)
  2843. ext4_ext_mark_unwritten(ex);
  2844. else
  2845. ext4_ext_mark_initialized(ex);
  2846. if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
  2847. ext4_ext_try_to_merge(handle, inode, path, ex);
  2848. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  2849. goto out;
  2850. }
  2851. /* case a */
  2852. memcpy(&orig_ex, ex, sizeof(orig_ex));
  2853. ex->ee_len = cpu_to_le16(split - ee_block);
  2854. if (split_flag & EXT4_EXT_MARK_UNWRIT1)
  2855. ext4_ext_mark_unwritten(ex);
  2856. /*
  2857. * path may lead to new leaf, not to original leaf any more
  2858. * after ext4_ext_insert_extent() returns,
  2859. */
  2860. err = ext4_ext_dirty(handle, inode, path + depth);
  2861. if (err)
  2862. goto fix_extent_len;
  2863. ex2 = &newex;
  2864. ex2->ee_block = cpu_to_le32(split);
  2865. ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
  2866. ext4_ext_store_pblock(ex2, newblock);
  2867. if (split_flag & EXT4_EXT_MARK_UNWRIT2)
  2868. ext4_ext_mark_unwritten(ex2);
  2869. path = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
  2870. if (!IS_ERR(path))
  2871. goto out;
  2872. err = PTR_ERR(path);
  2873. if (err != -ENOSPC && err != -EDQUOT && err != -ENOMEM)
  2874. return path;
  2875. /*
  2876. * Get a new path to try to zeroout or fix the extent length.
  2877. * Using EXT4_EX_NOFAIL guarantees that ext4_find_extent()
  2878. * will not return -ENOMEM, otherwise -ENOMEM will cause a
  2879. * retry in do_writepages(), and a WARN_ON may be triggered
  2880. * in ext4_da_update_reserve_space() due to an incorrect
  2881. * ee_len causing the i_reserved_data_blocks exception.
  2882. */
  2883. path = ext4_find_extent(inode, ee_block, NULL, flags | EXT4_EX_NOFAIL);
  2884. if (IS_ERR(path)) {
  2885. EXT4_ERROR_INODE(inode, "Failed split extent on %u, err %ld",
  2886. split, PTR_ERR(path));
  2887. return path;
  2888. }
  2889. depth = ext_depth(inode);
  2890. ex = path[depth].p_ext;
  2891. if (EXT4_EXT_MAY_ZEROOUT & split_flag) {
  2892. if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
  2893. if (split_flag & EXT4_EXT_DATA_VALID1) {
  2894. err = ext4_ext_zeroout(inode, ex2);
  2895. zero_ex.ee_block = ex2->ee_block;
  2896. zero_ex.ee_len = cpu_to_le16(
  2897. ext4_ext_get_actual_len(ex2));
  2898. ext4_ext_store_pblock(&zero_ex,
  2899. ext4_ext_pblock(ex2));
  2900. } else {
  2901. err = ext4_ext_zeroout(inode, ex);
  2902. zero_ex.ee_block = ex->ee_block;
  2903. zero_ex.ee_len = cpu_to_le16(
  2904. ext4_ext_get_actual_len(ex));
  2905. ext4_ext_store_pblock(&zero_ex,
  2906. ext4_ext_pblock(ex));
  2907. }
  2908. } else {
  2909. err = ext4_ext_zeroout(inode, &orig_ex);
  2910. zero_ex.ee_block = orig_ex.ee_block;
  2911. zero_ex.ee_len = cpu_to_le16(
  2912. ext4_ext_get_actual_len(&orig_ex));
  2913. ext4_ext_store_pblock(&zero_ex,
  2914. ext4_ext_pblock(&orig_ex));
  2915. }
  2916. if (!err) {
  2917. /* update the extent length and mark as initialized */
  2918. ex->ee_len = cpu_to_le16(ee_len);
  2919. ext4_ext_try_to_merge(handle, inode, path, ex);
  2920. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  2921. if (!err)
  2922. /* update extent status tree */
  2923. ext4_zeroout_es(inode, &zero_ex);
  2924. /* If we failed at this point, we don't know in which
  2925. * state the extent tree exactly is so don't try to fix
  2926. * length of the original extent as it may do even more
  2927. * damage.
  2928. */
  2929. goto out;
  2930. }
  2931. }
  2932. fix_extent_len:
  2933. ex->ee_len = orig_ex.ee_len;
  2934. /*
  2935. * Ignore ext4_ext_dirty return value since we are already in error path
  2936. * and err is a non-zero error code.
  2937. */
  2938. ext4_ext_dirty(handle, inode, path + path->p_depth);
  2939. out:
  2940. if (err) {
  2941. ext4_free_ext_path(path);
  2942. path = ERR_PTR(err);
  2943. }
  2944. ext4_ext_show_leaf(inode, path);
  2945. return path;
  2946. }
  2947. /*
  2948. * ext4_split_extent() splits an extent and mark extent which is covered
  2949. * by @map as split_flags indicates
  2950. *
  2951. * It may result in splitting the extent into multiple extents (up to three)
  2952. * There are three possibilities:
  2953. * a> There is no split required
  2954. * b> Splits in two extents: Split is happening at either end of the extent
  2955. * c> Splits in three extents: Somone is splitting in middle of the extent
  2956. *
  2957. */
  2958. static struct ext4_ext_path *ext4_split_extent(handle_t *handle,
  2959. struct inode *inode,
  2960. struct ext4_ext_path *path,
  2961. struct ext4_map_blocks *map,
  2962. int split_flag, int flags,
  2963. unsigned int *allocated)
  2964. {
  2965. ext4_lblk_t ee_block;
  2966. struct ext4_extent *ex;
  2967. unsigned int ee_len, depth;
  2968. int unwritten;
  2969. int split_flag1, flags1;
  2970. depth = ext_depth(inode);
  2971. ex = path[depth].p_ext;
  2972. ee_block = le32_to_cpu(ex->ee_block);
  2973. ee_len = ext4_ext_get_actual_len(ex);
  2974. unwritten = ext4_ext_is_unwritten(ex);
  2975. if (map->m_lblk + map->m_len < ee_block + ee_len) {
  2976. split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
  2977. flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
  2978. if (unwritten)
  2979. split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
  2980. EXT4_EXT_MARK_UNWRIT2;
  2981. if (split_flag & EXT4_EXT_DATA_VALID2)
  2982. split_flag1 |= EXT4_EXT_DATA_VALID1;
  2983. path = ext4_split_extent_at(handle, inode, path,
  2984. map->m_lblk + map->m_len, split_flag1, flags1);
  2985. if (IS_ERR(path))
  2986. return path;
  2987. /*
  2988. * Update path is required because previous ext4_split_extent_at
  2989. * may result in split of original leaf or extent zeroout.
  2990. */
  2991. path = ext4_find_extent(inode, map->m_lblk, path, flags);
  2992. if (IS_ERR(path))
  2993. return path;
  2994. depth = ext_depth(inode);
  2995. ex = path[depth].p_ext;
  2996. if (!ex) {
  2997. EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
  2998. (unsigned long) map->m_lblk);
  2999. ext4_free_ext_path(path);
  3000. return ERR_PTR(-EFSCORRUPTED);
  3001. }
  3002. unwritten = ext4_ext_is_unwritten(ex);
  3003. }
  3004. if (map->m_lblk >= ee_block) {
  3005. split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
  3006. if (unwritten) {
  3007. split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
  3008. split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
  3009. EXT4_EXT_MARK_UNWRIT2);
  3010. }
  3011. path = ext4_split_extent_at(handle, inode, path,
  3012. map->m_lblk, split_flag1, flags);
  3013. if (IS_ERR(path))
  3014. return path;
  3015. }
  3016. if (allocated) {
  3017. if (map->m_lblk + map->m_len > ee_block + ee_len)
  3018. *allocated = ee_len - (map->m_lblk - ee_block);
  3019. else
  3020. *allocated = map->m_len;
  3021. }
  3022. ext4_ext_show_leaf(inode, path);
  3023. return path;
  3024. }
  3025. /*
  3026. * This function is called by ext4_ext_map_blocks() if someone tries to write
  3027. * to an unwritten extent. It may result in splitting the unwritten
  3028. * extent into multiple extents (up to three - one initialized and two
  3029. * unwritten).
  3030. * There are three possibilities:
  3031. * a> There is no split required: Entire extent should be initialized
  3032. * b> Splits in two extents: Write is happening at either end of the extent
  3033. * c> Splits in three extents: Somone is writing in middle of the extent
  3034. *
  3035. * Pre-conditions:
  3036. * - The extent pointed to by 'path' is unwritten.
  3037. * - The extent pointed to by 'path' contains a superset
  3038. * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
  3039. *
  3040. * Post-conditions on success:
  3041. * - the returned value is the number of blocks beyond map->l_lblk
  3042. * that are allocated and initialized.
  3043. * It is guaranteed to be >= map->m_len.
  3044. */
  3045. static struct ext4_ext_path *
  3046. ext4_ext_convert_to_initialized(handle_t *handle, struct inode *inode,
  3047. struct ext4_map_blocks *map, struct ext4_ext_path *path,
  3048. int flags, unsigned int *allocated)
  3049. {
  3050. struct ext4_sb_info *sbi;
  3051. struct ext4_extent_header *eh;
  3052. struct ext4_map_blocks split_map;
  3053. struct ext4_extent zero_ex1, zero_ex2;
  3054. struct ext4_extent *ex, *abut_ex;
  3055. ext4_lblk_t ee_block, eof_block;
  3056. unsigned int ee_len, depth, map_len = map->m_len;
  3057. int err = 0;
  3058. int split_flag = EXT4_EXT_DATA_VALID2;
  3059. unsigned int max_zeroout = 0;
  3060. ext_debug(inode, "logical block %llu, max_blocks %u\n",
  3061. (unsigned long long)map->m_lblk, map_len);
  3062. sbi = EXT4_SB(inode->i_sb);
  3063. eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1)
  3064. >> inode->i_sb->s_blocksize_bits;
  3065. if (eof_block < map->m_lblk + map_len)
  3066. eof_block = map->m_lblk + map_len;
  3067. depth = ext_depth(inode);
  3068. eh = path[depth].p_hdr;
  3069. ex = path[depth].p_ext;
  3070. ee_block = le32_to_cpu(ex->ee_block);
  3071. ee_len = ext4_ext_get_actual_len(ex);
  3072. zero_ex1.ee_len = 0;
  3073. zero_ex2.ee_len = 0;
  3074. trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
  3075. /* Pre-conditions */
  3076. BUG_ON(!ext4_ext_is_unwritten(ex));
  3077. BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
  3078. /*
  3079. * Attempt to transfer newly initialized blocks from the currently
  3080. * unwritten extent to its neighbor. This is much cheaper
  3081. * than an insertion followed by a merge as those involve costly
  3082. * memmove() calls. Transferring to the left is the common case in
  3083. * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
  3084. * followed by append writes.
  3085. *
  3086. * Limitations of the current logic:
  3087. * - L1: we do not deal with writes covering the whole extent.
  3088. * This would require removing the extent if the transfer
  3089. * is possible.
  3090. * - L2: we only attempt to merge with an extent stored in the
  3091. * same extent tree node.
  3092. */
  3093. *allocated = 0;
  3094. if ((map->m_lblk == ee_block) &&
  3095. /* See if we can merge left */
  3096. (map_len < ee_len) && /*L1*/
  3097. (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
  3098. ext4_lblk_t prev_lblk;
  3099. ext4_fsblk_t prev_pblk, ee_pblk;
  3100. unsigned int prev_len;
  3101. abut_ex = ex - 1;
  3102. prev_lblk = le32_to_cpu(abut_ex->ee_block);
  3103. prev_len = ext4_ext_get_actual_len(abut_ex);
  3104. prev_pblk = ext4_ext_pblock(abut_ex);
  3105. ee_pblk = ext4_ext_pblock(ex);
  3106. /*
  3107. * A transfer of blocks from 'ex' to 'abut_ex' is allowed
  3108. * upon those conditions:
  3109. * - C1: abut_ex is initialized,
  3110. * - C2: abut_ex is logically abutting ex,
  3111. * - C3: abut_ex is physically abutting ex,
  3112. * - C4: abut_ex can receive the additional blocks without
  3113. * overflowing the (initialized) length limit.
  3114. */
  3115. if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
  3116. ((prev_lblk + prev_len) == ee_block) && /*C2*/
  3117. ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
  3118. (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
  3119. err = ext4_ext_get_access(handle, inode, path + depth);
  3120. if (err)
  3121. goto errout;
  3122. trace_ext4_ext_convert_to_initialized_fastpath(inode,
  3123. map, ex, abut_ex);
  3124. /* Shift the start of ex by 'map_len' blocks */
  3125. ex->ee_block = cpu_to_le32(ee_block + map_len);
  3126. ext4_ext_store_pblock(ex, ee_pblk + map_len);
  3127. ex->ee_len = cpu_to_le16(ee_len - map_len);
  3128. ext4_ext_mark_unwritten(ex); /* Restore the flag */
  3129. /* Extend abut_ex by 'map_len' blocks */
  3130. abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
  3131. /* Result: number of initialized blocks past m_lblk */
  3132. *allocated = map_len;
  3133. }
  3134. } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
  3135. (map_len < ee_len) && /*L1*/
  3136. ex < EXT_LAST_EXTENT(eh)) { /*L2*/
  3137. /* See if we can merge right */
  3138. ext4_lblk_t next_lblk;
  3139. ext4_fsblk_t next_pblk, ee_pblk;
  3140. unsigned int next_len;
  3141. abut_ex = ex + 1;
  3142. next_lblk = le32_to_cpu(abut_ex->ee_block);
  3143. next_len = ext4_ext_get_actual_len(abut_ex);
  3144. next_pblk = ext4_ext_pblock(abut_ex);
  3145. ee_pblk = ext4_ext_pblock(ex);
  3146. /*
  3147. * A transfer of blocks from 'ex' to 'abut_ex' is allowed
  3148. * upon those conditions:
  3149. * - C1: abut_ex is initialized,
  3150. * - C2: abut_ex is logically abutting ex,
  3151. * - C3: abut_ex is physically abutting ex,
  3152. * - C4: abut_ex can receive the additional blocks without
  3153. * overflowing the (initialized) length limit.
  3154. */
  3155. if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
  3156. ((map->m_lblk + map_len) == next_lblk) && /*C2*/
  3157. ((ee_pblk + ee_len) == next_pblk) && /*C3*/
  3158. (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
  3159. err = ext4_ext_get_access(handle, inode, path + depth);
  3160. if (err)
  3161. goto errout;
  3162. trace_ext4_ext_convert_to_initialized_fastpath(inode,
  3163. map, ex, abut_ex);
  3164. /* Shift the start of abut_ex by 'map_len' blocks */
  3165. abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
  3166. ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
  3167. ex->ee_len = cpu_to_le16(ee_len - map_len);
  3168. ext4_ext_mark_unwritten(ex); /* Restore the flag */
  3169. /* Extend abut_ex by 'map_len' blocks */
  3170. abut_ex->ee_len = cpu_to_le16(next_len + map_len);
  3171. /* Result: number of initialized blocks past m_lblk */
  3172. *allocated = map_len;
  3173. }
  3174. }
  3175. if (*allocated) {
  3176. /* Mark the block containing both extents as dirty */
  3177. err = ext4_ext_dirty(handle, inode, path + depth);
  3178. /* Update path to point to the right extent */
  3179. path[depth].p_ext = abut_ex;
  3180. if (err)
  3181. goto errout;
  3182. goto out;
  3183. } else
  3184. *allocated = ee_len - (map->m_lblk - ee_block);
  3185. WARN_ON(map->m_lblk < ee_block);
  3186. /*
  3187. * It is safe to convert extent to initialized via explicit
  3188. * zeroout only if extent is fully inside i_size or new_size.
  3189. */
  3190. split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
  3191. if (EXT4_EXT_MAY_ZEROOUT & split_flag)
  3192. max_zeroout = sbi->s_extent_max_zeroout_kb >>
  3193. (inode->i_sb->s_blocksize_bits - 10);
  3194. /*
  3195. * five cases:
  3196. * 1. split the extent into three extents.
  3197. * 2. split the extent into two extents, zeroout the head of the first
  3198. * extent.
  3199. * 3. split the extent into two extents, zeroout the tail of the second
  3200. * extent.
  3201. * 4. split the extent into two extents with out zeroout.
  3202. * 5. no splitting needed, just possibly zeroout the head and / or the
  3203. * tail of the extent.
  3204. */
  3205. split_map.m_lblk = map->m_lblk;
  3206. split_map.m_len = map->m_len;
  3207. if (max_zeroout && (*allocated > split_map.m_len)) {
  3208. if (*allocated <= max_zeroout) {
  3209. /* case 3 or 5 */
  3210. zero_ex1.ee_block =
  3211. cpu_to_le32(split_map.m_lblk +
  3212. split_map.m_len);
  3213. zero_ex1.ee_len =
  3214. cpu_to_le16(*allocated - split_map.m_len);
  3215. ext4_ext_store_pblock(&zero_ex1,
  3216. ext4_ext_pblock(ex) + split_map.m_lblk +
  3217. split_map.m_len - ee_block);
  3218. err = ext4_ext_zeroout(inode, &zero_ex1);
  3219. if (err)
  3220. goto fallback;
  3221. split_map.m_len = *allocated;
  3222. }
  3223. if (split_map.m_lblk - ee_block + split_map.m_len <
  3224. max_zeroout) {
  3225. /* case 2 or 5 */
  3226. if (split_map.m_lblk != ee_block) {
  3227. zero_ex2.ee_block = ex->ee_block;
  3228. zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
  3229. ee_block);
  3230. ext4_ext_store_pblock(&zero_ex2,
  3231. ext4_ext_pblock(ex));
  3232. err = ext4_ext_zeroout(inode, &zero_ex2);
  3233. if (err)
  3234. goto fallback;
  3235. }
  3236. split_map.m_len += split_map.m_lblk - ee_block;
  3237. split_map.m_lblk = ee_block;
  3238. *allocated = map->m_len;
  3239. }
  3240. }
  3241. fallback:
  3242. path = ext4_split_extent(handle, inode, path, &split_map, split_flag,
  3243. flags, NULL);
  3244. if (IS_ERR(path))
  3245. return path;
  3246. out:
  3247. /* If we have gotten a failure, don't zero out status tree */
  3248. ext4_zeroout_es(inode, &zero_ex1);
  3249. ext4_zeroout_es(inode, &zero_ex2);
  3250. return path;
  3251. errout:
  3252. ext4_free_ext_path(path);
  3253. return ERR_PTR(err);
  3254. }
  3255. /*
  3256. * This function is called by ext4_ext_map_blocks() from
  3257. * ext4_get_blocks_dio_write() when DIO to write
  3258. * to an unwritten extent.
  3259. *
  3260. * Writing to an unwritten extent may result in splitting the unwritten
  3261. * extent into multiple initialized/unwritten extents (up to three)
  3262. * There are three possibilities:
  3263. * a> There is no split required: Entire extent should be unwritten
  3264. * b> Splits in two extents: Write is happening at either end of the extent
  3265. * c> Splits in three extents: Somone is writing in middle of the extent
  3266. *
  3267. * This works the same way in the case of initialized -> unwritten conversion.
  3268. *
  3269. * One of more index blocks maybe needed if the extent tree grow after
  3270. * the unwritten extent split. To prevent ENOSPC occur at the IO
  3271. * complete, we need to split the unwritten extent before DIO submit
  3272. * the IO. The unwritten extent called at this time will be split
  3273. * into three unwritten extent(at most). After IO complete, the part
  3274. * being filled will be convert to initialized by the end_io callback function
  3275. * via ext4_convert_unwritten_extents().
  3276. *
  3277. * The size of unwritten extent to be written is passed to the caller via the
  3278. * allocated pointer. Return an extent path pointer on success, or an error
  3279. * pointer on failure.
  3280. */
  3281. static struct ext4_ext_path *ext4_split_convert_extents(handle_t *handle,
  3282. struct inode *inode,
  3283. struct ext4_map_blocks *map,
  3284. struct ext4_ext_path *path,
  3285. int flags, unsigned int *allocated)
  3286. {
  3287. ext4_lblk_t eof_block;
  3288. ext4_lblk_t ee_block;
  3289. struct ext4_extent *ex;
  3290. unsigned int ee_len;
  3291. int split_flag = 0, depth;
  3292. ext_debug(inode, "logical block %llu, max_blocks %u\n",
  3293. (unsigned long long)map->m_lblk, map->m_len);
  3294. eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1)
  3295. >> inode->i_sb->s_blocksize_bits;
  3296. if (eof_block < map->m_lblk + map->m_len)
  3297. eof_block = map->m_lblk + map->m_len;
  3298. /*
  3299. * It is safe to convert extent to initialized via explicit
  3300. * zeroout only if extent is fully inside i_size or new_size.
  3301. */
  3302. depth = ext_depth(inode);
  3303. ex = path[depth].p_ext;
  3304. ee_block = le32_to_cpu(ex->ee_block);
  3305. ee_len = ext4_ext_get_actual_len(ex);
  3306. /* Convert to unwritten */
  3307. if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
  3308. split_flag |= EXT4_EXT_DATA_VALID1;
  3309. /* Convert to initialized */
  3310. } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
  3311. split_flag |= ee_block + ee_len <= eof_block ?
  3312. EXT4_EXT_MAY_ZEROOUT : 0;
  3313. split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
  3314. }
  3315. flags |= EXT4_GET_BLOCKS_PRE_IO;
  3316. return ext4_split_extent(handle, inode, path, map, split_flag, flags,
  3317. allocated);
  3318. }
  3319. static struct ext4_ext_path *
  3320. ext4_convert_unwritten_extents_endio(handle_t *handle, struct inode *inode,
  3321. struct ext4_map_blocks *map,
  3322. struct ext4_ext_path *path)
  3323. {
  3324. struct ext4_extent *ex;
  3325. ext4_lblk_t ee_block;
  3326. unsigned int ee_len;
  3327. int depth;
  3328. int err = 0;
  3329. depth = ext_depth(inode);
  3330. ex = path[depth].p_ext;
  3331. ee_block = le32_to_cpu(ex->ee_block);
  3332. ee_len = ext4_ext_get_actual_len(ex);
  3333. ext_debug(inode, "logical block %llu, max_blocks %u\n",
  3334. (unsigned long long)ee_block, ee_len);
  3335. /* If extent is larger than requested it is a clear sign that we still
  3336. * have some extent state machine issues left. So extent_split is still
  3337. * required.
  3338. * TODO: Once all related issues will be fixed this situation should be
  3339. * illegal.
  3340. */
  3341. if (ee_block != map->m_lblk || ee_len > map->m_len) {
  3342. #ifdef CONFIG_EXT4_DEBUG
  3343. ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
  3344. " len %u; IO logical block %llu, len %u",
  3345. inode->i_ino, (unsigned long long)ee_block, ee_len,
  3346. (unsigned long long)map->m_lblk, map->m_len);
  3347. #endif
  3348. path = ext4_split_convert_extents(handle, inode, map, path,
  3349. EXT4_GET_BLOCKS_CONVERT, NULL);
  3350. if (IS_ERR(path))
  3351. return path;
  3352. path = ext4_find_extent(inode, map->m_lblk, path, 0);
  3353. if (IS_ERR(path))
  3354. return path;
  3355. depth = ext_depth(inode);
  3356. ex = path[depth].p_ext;
  3357. }
  3358. err = ext4_ext_get_access(handle, inode, path + depth);
  3359. if (err)
  3360. goto errout;
  3361. /* first mark the extent as initialized */
  3362. ext4_ext_mark_initialized(ex);
  3363. /* note: ext4_ext_correct_indexes() isn't needed here because
  3364. * borders are not changed
  3365. */
  3366. ext4_ext_try_to_merge(handle, inode, path, ex);
  3367. /* Mark modified extent as dirty */
  3368. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  3369. if (err)
  3370. goto errout;
  3371. ext4_ext_show_leaf(inode, path);
  3372. return path;
  3373. errout:
  3374. ext4_free_ext_path(path);
  3375. return ERR_PTR(err);
  3376. }
  3377. static struct ext4_ext_path *
  3378. convert_initialized_extent(handle_t *handle, struct inode *inode,
  3379. struct ext4_map_blocks *map,
  3380. struct ext4_ext_path *path,
  3381. unsigned int *allocated)
  3382. {
  3383. struct ext4_extent *ex;
  3384. ext4_lblk_t ee_block;
  3385. unsigned int ee_len;
  3386. int depth;
  3387. int err = 0;
  3388. /*
  3389. * Make sure that the extent is no bigger than we support with
  3390. * unwritten extent
  3391. */
  3392. if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
  3393. map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
  3394. depth = ext_depth(inode);
  3395. ex = path[depth].p_ext;
  3396. ee_block = le32_to_cpu(ex->ee_block);
  3397. ee_len = ext4_ext_get_actual_len(ex);
  3398. ext_debug(inode, "logical block %llu, max_blocks %u\n",
  3399. (unsigned long long)ee_block, ee_len);
  3400. if (ee_block != map->m_lblk || ee_len > map->m_len) {
  3401. path = ext4_split_convert_extents(handle, inode, map, path,
  3402. EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, NULL);
  3403. if (IS_ERR(path))
  3404. return path;
  3405. path = ext4_find_extent(inode, map->m_lblk, path, 0);
  3406. if (IS_ERR(path))
  3407. return path;
  3408. depth = ext_depth(inode);
  3409. ex = path[depth].p_ext;
  3410. if (!ex) {
  3411. EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
  3412. (unsigned long) map->m_lblk);
  3413. err = -EFSCORRUPTED;
  3414. goto errout;
  3415. }
  3416. }
  3417. err = ext4_ext_get_access(handle, inode, path + depth);
  3418. if (err)
  3419. goto errout;
  3420. /* first mark the extent as unwritten */
  3421. ext4_ext_mark_unwritten(ex);
  3422. /* note: ext4_ext_correct_indexes() isn't needed here because
  3423. * borders are not changed
  3424. */
  3425. ext4_ext_try_to_merge(handle, inode, path, ex);
  3426. /* Mark modified extent as dirty */
  3427. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  3428. if (err)
  3429. goto errout;
  3430. ext4_ext_show_leaf(inode, path);
  3431. ext4_update_inode_fsync_trans(handle, inode, 1);
  3432. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3433. if (*allocated > map->m_len)
  3434. *allocated = map->m_len;
  3435. map->m_len = *allocated;
  3436. return path;
  3437. errout:
  3438. ext4_free_ext_path(path);
  3439. return ERR_PTR(err);
  3440. }
  3441. static struct ext4_ext_path *
  3442. ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
  3443. struct ext4_map_blocks *map,
  3444. struct ext4_ext_path *path, int flags,
  3445. unsigned int *allocated, ext4_fsblk_t newblock)
  3446. {
  3447. int err = 0;
  3448. ext_debug(inode, "logical block %llu, max_blocks %u, flags 0x%x, allocated %u\n",
  3449. (unsigned long long)map->m_lblk, map->m_len, flags,
  3450. *allocated);
  3451. ext4_ext_show_leaf(inode, path);
  3452. /*
  3453. * When writing into unwritten space, we should not fail to
  3454. * allocate metadata blocks for the new extent block if needed.
  3455. */
  3456. flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
  3457. trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
  3458. *allocated, newblock);
  3459. /* get_block() before submitting IO, split the extent */
  3460. if (flags & EXT4_GET_BLOCKS_PRE_IO) {
  3461. path = ext4_split_convert_extents(handle, inode, map, path,
  3462. flags | EXT4_GET_BLOCKS_CONVERT, allocated);
  3463. if (IS_ERR(path))
  3464. return path;
  3465. /*
  3466. * shouldn't get a 0 allocated when splitting an extent unless
  3467. * m_len is 0 (bug) or extent has been corrupted
  3468. */
  3469. if (unlikely(*allocated == 0)) {
  3470. EXT4_ERROR_INODE(inode,
  3471. "unexpected allocated == 0, m_len = %u",
  3472. map->m_len);
  3473. err = -EFSCORRUPTED;
  3474. goto errout;
  3475. }
  3476. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3477. goto out;
  3478. }
  3479. /* IO end_io complete, convert the filled extent to written */
  3480. if (flags & EXT4_GET_BLOCKS_CONVERT) {
  3481. path = ext4_convert_unwritten_extents_endio(handle, inode,
  3482. map, path);
  3483. if (IS_ERR(path))
  3484. return path;
  3485. ext4_update_inode_fsync_trans(handle, inode, 1);
  3486. goto map_out;
  3487. }
  3488. /* buffered IO cases */
  3489. /*
  3490. * repeat fallocate creation request
  3491. * we already have an unwritten extent
  3492. */
  3493. if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
  3494. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3495. goto map_out;
  3496. }
  3497. /* buffered READ or buffered write_begin() lookup */
  3498. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  3499. /*
  3500. * We have blocks reserved already. We
  3501. * return allocated blocks so that delalloc
  3502. * won't do block reservation for us. But
  3503. * the buffer head will be unmapped so that
  3504. * a read from the block returns 0s.
  3505. */
  3506. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3507. goto out1;
  3508. }
  3509. /*
  3510. * Default case when (flags & EXT4_GET_BLOCKS_CREATE) == 1.
  3511. * For buffered writes, at writepage time, etc. Convert a
  3512. * discovered unwritten extent to written.
  3513. */
  3514. path = ext4_ext_convert_to_initialized(handle, inode, map, path,
  3515. flags, allocated);
  3516. if (IS_ERR(path))
  3517. return path;
  3518. ext4_update_inode_fsync_trans(handle, inode, 1);
  3519. /*
  3520. * shouldn't get a 0 allocated when converting an unwritten extent
  3521. * unless m_len is 0 (bug) or extent has been corrupted
  3522. */
  3523. if (unlikely(*allocated == 0)) {
  3524. EXT4_ERROR_INODE(inode, "unexpected allocated == 0, m_len = %u",
  3525. map->m_len);
  3526. err = -EFSCORRUPTED;
  3527. goto errout;
  3528. }
  3529. out:
  3530. map->m_flags |= EXT4_MAP_NEW;
  3531. map_out:
  3532. map->m_flags |= EXT4_MAP_MAPPED;
  3533. out1:
  3534. map->m_pblk = newblock;
  3535. if (*allocated > map->m_len)
  3536. *allocated = map->m_len;
  3537. map->m_len = *allocated;
  3538. ext4_ext_show_leaf(inode, path);
  3539. return path;
  3540. errout:
  3541. ext4_free_ext_path(path);
  3542. return ERR_PTR(err);
  3543. }
  3544. /*
  3545. * get_implied_cluster_alloc - check to see if the requested
  3546. * allocation (in the map structure) overlaps with a cluster already
  3547. * allocated in an extent.
  3548. * @sb The filesystem superblock structure
  3549. * @map The requested lblk->pblk mapping
  3550. * @ex The extent structure which might contain an implied
  3551. * cluster allocation
  3552. *
  3553. * This function is called by ext4_ext_map_blocks() after we failed to
  3554. * find blocks that were already in the inode's extent tree. Hence,
  3555. * we know that the beginning of the requested region cannot overlap
  3556. * the extent from the inode's extent tree. There are three cases we
  3557. * want to catch. The first is this case:
  3558. *
  3559. * |--- cluster # N--|
  3560. * |--- extent ---| |---- requested region ---|
  3561. * |==========|
  3562. *
  3563. * The second case that we need to test for is this one:
  3564. *
  3565. * |--------- cluster # N ----------------|
  3566. * |--- requested region --| |------- extent ----|
  3567. * |=======================|
  3568. *
  3569. * The third case is when the requested region lies between two extents
  3570. * within the same cluster:
  3571. * |------------- cluster # N-------------|
  3572. * |----- ex -----| |---- ex_right ----|
  3573. * |------ requested region ------|
  3574. * |================|
  3575. *
  3576. * In each of the above cases, we need to set the map->m_pblk and
  3577. * map->m_len so it corresponds to the return the extent labelled as
  3578. * "|====|" from cluster #N, since it is already in use for data in
  3579. * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
  3580. * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
  3581. * as a new "allocated" block region. Otherwise, we will return 0 and
  3582. * ext4_ext_map_blocks() will then allocate one or more new clusters
  3583. * by calling ext4_mb_new_blocks().
  3584. */
  3585. static int get_implied_cluster_alloc(struct super_block *sb,
  3586. struct ext4_map_blocks *map,
  3587. struct ext4_extent *ex,
  3588. struct ext4_ext_path *path)
  3589. {
  3590. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3591. ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
  3592. ext4_lblk_t ex_cluster_start, ex_cluster_end;
  3593. ext4_lblk_t rr_cluster_start;
  3594. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  3595. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  3596. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  3597. /* The extent passed in that we are trying to match */
  3598. ex_cluster_start = EXT4_B2C(sbi, ee_block);
  3599. ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
  3600. /* The requested region passed into ext4_map_blocks() */
  3601. rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
  3602. if ((rr_cluster_start == ex_cluster_end) ||
  3603. (rr_cluster_start == ex_cluster_start)) {
  3604. if (rr_cluster_start == ex_cluster_end)
  3605. ee_start += ee_len - 1;
  3606. map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
  3607. map->m_len = min(map->m_len,
  3608. (unsigned) sbi->s_cluster_ratio - c_offset);
  3609. /*
  3610. * Check for and handle this case:
  3611. *
  3612. * |--------- cluster # N-------------|
  3613. * |------- extent ----|
  3614. * |--- requested region ---|
  3615. * |===========|
  3616. */
  3617. if (map->m_lblk < ee_block)
  3618. map->m_len = min(map->m_len, ee_block - map->m_lblk);
  3619. /*
  3620. * Check for the case where there is already another allocated
  3621. * block to the right of 'ex' but before the end of the cluster.
  3622. *
  3623. * |------------- cluster # N-------------|
  3624. * |----- ex -----| |---- ex_right ----|
  3625. * |------ requested region ------|
  3626. * |================|
  3627. */
  3628. if (map->m_lblk > ee_block) {
  3629. ext4_lblk_t next = ext4_ext_next_allocated_block(path);
  3630. map->m_len = min(map->m_len, next - map->m_lblk);
  3631. }
  3632. trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
  3633. return 1;
  3634. }
  3635. trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
  3636. return 0;
  3637. }
  3638. /*
  3639. * Determine hole length around the given logical block, first try to
  3640. * locate and expand the hole from the given @path, and then adjust it
  3641. * if it's partially or completely converted to delayed extents, insert
  3642. * it into the extent cache tree if it's indeed a hole, finally return
  3643. * the length of the determined extent.
  3644. */
  3645. static ext4_lblk_t ext4_ext_determine_insert_hole(struct inode *inode,
  3646. struct ext4_ext_path *path,
  3647. ext4_lblk_t lblk)
  3648. {
  3649. ext4_lblk_t hole_start, len;
  3650. struct extent_status es;
  3651. hole_start = lblk;
  3652. len = ext4_ext_find_hole(inode, path, &hole_start);
  3653. again:
  3654. ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
  3655. hole_start + len - 1, &es);
  3656. if (!es.es_len)
  3657. goto insert_hole;
  3658. /*
  3659. * There's a delalloc extent in the hole, handle it if the delalloc
  3660. * extent is in front of, behind and straddle the queried range.
  3661. */
  3662. if (lblk >= es.es_lblk + es.es_len) {
  3663. /*
  3664. * The delalloc extent is in front of the queried range,
  3665. * find again from the queried start block.
  3666. */
  3667. len -= lblk - hole_start;
  3668. hole_start = lblk;
  3669. goto again;
  3670. } else if (in_range(lblk, es.es_lblk, es.es_len)) {
  3671. /*
  3672. * The delalloc extent containing lblk, it must have been
  3673. * added after ext4_map_blocks() checked the extent status
  3674. * tree so we are not holding i_rwsem and delalloc info is
  3675. * only stabilized by i_data_sem we are going to release
  3676. * soon. Don't modify the extent status tree and report
  3677. * extent as a hole, just adjust the length to the delalloc
  3678. * extent's after lblk.
  3679. */
  3680. len = es.es_lblk + es.es_len - lblk;
  3681. return len;
  3682. } else {
  3683. /*
  3684. * The delalloc extent is partially or completely behind
  3685. * the queried range, update hole length until the
  3686. * beginning of the delalloc extent.
  3687. */
  3688. len = min(es.es_lblk - hole_start, len);
  3689. }
  3690. insert_hole:
  3691. /* Put just found gap into cache to speed up subsequent requests */
  3692. ext_debug(inode, " -> %u:%u\n", hole_start, len);
  3693. ext4_es_insert_extent(inode, hole_start, len, ~0,
  3694. EXTENT_STATUS_HOLE, 0);
  3695. /* Update hole_len to reflect hole size after lblk */
  3696. if (hole_start != lblk)
  3697. len -= lblk - hole_start;
  3698. return len;
  3699. }
  3700. /*
  3701. * Block allocation/map/preallocation routine for extents based files
  3702. *
  3703. *
  3704. * Need to be called with
  3705. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
  3706. * (ie, flags is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
  3707. *
  3708. * return > 0, number of blocks already mapped/allocated
  3709. * if flags doesn't contain EXT4_GET_BLOCKS_CREATE and these are pre-allocated blocks
  3710. * buffer head is unmapped
  3711. * otherwise blocks are mapped
  3712. *
  3713. * return = 0, if plain look up failed (blocks have not been allocated)
  3714. * buffer head is unmapped
  3715. *
  3716. * return < 0, error case.
  3717. */
  3718. int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
  3719. struct ext4_map_blocks *map, int flags)
  3720. {
  3721. struct ext4_ext_path *path = NULL;
  3722. struct ext4_extent newex, *ex, ex2;
  3723. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3724. ext4_fsblk_t newblock = 0, pblk;
  3725. int err = 0, depth;
  3726. unsigned int allocated = 0, offset = 0;
  3727. unsigned int allocated_clusters = 0;
  3728. struct ext4_allocation_request ar;
  3729. ext4_lblk_t cluster_offset;
  3730. ext_debug(inode, "blocks %u/%u requested\n", map->m_lblk, map->m_len);
  3731. trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
  3732. /* find extent for this block */
  3733. path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
  3734. if (IS_ERR(path)) {
  3735. err = PTR_ERR(path);
  3736. goto out;
  3737. }
  3738. depth = ext_depth(inode);
  3739. /*
  3740. * consistent leaf must not be empty;
  3741. * this situation is possible, though, _during_ tree modification;
  3742. * this is why assert can't be put in ext4_find_extent()
  3743. */
  3744. if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
  3745. EXT4_ERROR_INODE(inode, "bad extent address "
  3746. "lblock: %lu, depth: %d pblock %lld",
  3747. (unsigned long) map->m_lblk, depth,
  3748. path[depth].p_block);
  3749. err = -EFSCORRUPTED;
  3750. goto out;
  3751. }
  3752. ex = path[depth].p_ext;
  3753. if (ex) {
  3754. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  3755. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  3756. unsigned short ee_len;
  3757. /*
  3758. * unwritten extents are treated as holes, except that
  3759. * we split out initialized portions during a write.
  3760. */
  3761. ee_len = ext4_ext_get_actual_len(ex);
  3762. trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
  3763. /* if found extent covers block, simply return it */
  3764. if (in_range(map->m_lblk, ee_block, ee_len)) {
  3765. newblock = map->m_lblk - ee_block + ee_start;
  3766. /* number of remaining blocks in the extent */
  3767. allocated = ee_len - (map->m_lblk - ee_block);
  3768. ext_debug(inode, "%u fit into %u:%d -> %llu\n",
  3769. map->m_lblk, ee_block, ee_len, newblock);
  3770. /*
  3771. * If the extent is initialized check whether the
  3772. * caller wants to convert it to unwritten.
  3773. */
  3774. if ((!ext4_ext_is_unwritten(ex)) &&
  3775. (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
  3776. path = convert_initialized_extent(handle,
  3777. inode, map, path, &allocated);
  3778. if (IS_ERR(path))
  3779. err = PTR_ERR(path);
  3780. goto out;
  3781. } else if (!ext4_ext_is_unwritten(ex)) {
  3782. map->m_flags |= EXT4_MAP_MAPPED;
  3783. map->m_pblk = newblock;
  3784. if (allocated > map->m_len)
  3785. allocated = map->m_len;
  3786. map->m_len = allocated;
  3787. ext4_ext_show_leaf(inode, path);
  3788. goto out;
  3789. }
  3790. path = ext4_ext_handle_unwritten_extents(
  3791. handle, inode, map, path, flags,
  3792. &allocated, newblock);
  3793. if (IS_ERR(path))
  3794. err = PTR_ERR(path);
  3795. goto out;
  3796. }
  3797. }
  3798. /*
  3799. * requested block isn't allocated yet;
  3800. * we couldn't try to create block if flags doesn't contain EXT4_GET_BLOCKS_CREATE
  3801. */
  3802. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  3803. ext4_lblk_t len;
  3804. len = ext4_ext_determine_insert_hole(inode, path, map->m_lblk);
  3805. map->m_pblk = 0;
  3806. map->m_len = min_t(unsigned int, map->m_len, len);
  3807. goto out;
  3808. }
  3809. /*
  3810. * Okay, we need to do block allocation.
  3811. */
  3812. newex.ee_block = cpu_to_le32(map->m_lblk);
  3813. cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
  3814. /*
  3815. * If we are doing bigalloc, check to see if the extent returned
  3816. * by ext4_find_extent() implies a cluster we can use.
  3817. */
  3818. if (cluster_offset && ex &&
  3819. get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
  3820. ar.len = allocated = map->m_len;
  3821. newblock = map->m_pblk;
  3822. goto got_allocated_blocks;
  3823. }
  3824. /* find neighbour allocated blocks */
  3825. ar.lleft = map->m_lblk;
  3826. err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
  3827. if (err)
  3828. goto out;
  3829. ar.lright = map->m_lblk;
  3830. err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
  3831. if (err < 0)
  3832. goto out;
  3833. /* Check if the extent after searching to the right implies a
  3834. * cluster we can use. */
  3835. if ((sbi->s_cluster_ratio > 1) && err &&
  3836. get_implied_cluster_alloc(inode->i_sb, map, &ex2, path)) {
  3837. ar.len = allocated = map->m_len;
  3838. newblock = map->m_pblk;
  3839. err = 0;
  3840. goto got_allocated_blocks;
  3841. }
  3842. /*
  3843. * See if request is beyond maximum number of blocks we can have in
  3844. * a single extent. For an initialized extent this limit is
  3845. * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
  3846. * EXT_UNWRITTEN_MAX_LEN.
  3847. */
  3848. if (map->m_len > EXT_INIT_MAX_LEN &&
  3849. !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
  3850. map->m_len = EXT_INIT_MAX_LEN;
  3851. else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
  3852. (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
  3853. map->m_len = EXT_UNWRITTEN_MAX_LEN;
  3854. /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
  3855. newex.ee_len = cpu_to_le16(map->m_len);
  3856. err = ext4_ext_check_overlap(sbi, inode, &newex, path);
  3857. if (err)
  3858. allocated = ext4_ext_get_actual_len(&newex);
  3859. else
  3860. allocated = map->m_len;
  3861. /* allocate new block */
  3862. ar.inode = inode;
  3863. ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
  3864. ar.logical = map->m_lblk;
  3865. /*
  3866. * We calculate the offset from the beginning of the cluster
  3867. * for the logical block number, since when we allocate a
  3868. * physical cluster, the physical block should start at the
  3869. * same offset from the beginning of the cluster. This is
  3870. * needed so that future calls to get_implied_cluster_alloc()
  3871. * work correctly.
  3872. */
  3873. offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
  3874. ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
  3875. ar.goal -= offset;
  3876. ar.logical -= offset;
  3877. if (S_ISREG(inode->i_mode))
  3878. ar.flags = EXT4_MB_HINT_DATA;
  3879. else
  3880. /* disable in-core preallocation for non-regular files */
  3881. ar.flags = 0;
  3882. if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
  3883. ar.flags |= EXT4_MB_HINT_NOPREALLOC;
  3884. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  3885. ar.flags |= EXT4_MB_DELALLOC_RESERVED;
  3886. if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
  3887. ar.flags |= EXT4_MB_USE_RESERVED;
  3888. newblock = ext4_mb_new_blocks(handle, &ar, &err);
  3889. if (!newblock)
  3890. goto out;
  3891. allocated_clusters = ar.len;
  3892. ar.len = EXT4_C2B(sbi, ar.len) - offset;
  3893. ext_debug(inode, "allocate new block: goal %llu, found %llu/%u, requested %u\n",
  3894. ar.goal, newblock, ar.len, allocated);
  3895. if (ar.len > allocated)
  3896. ar.len = allocated;
  3897. got_allocated_blocks:
  3898. /* try to insert new extent into found leaf and return */
  3899. pblk = newblock + offset;
  3900. ext4_ext_store_pblock(&newex, pblk);
  3901. newex.ee_len = cpu_to_le16(ar.len);
  3902. /* Mark unwritten */
  3903. if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
  3904. ext4_ext_mark_unwritten(&newex);
  3905. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3906. }
  3907. path = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
  3908. if (IS_ERR(path)) {
  3909. err = PTR_ERR(path);
  3910. if (allocated_clusters) {
  3911. int fb_flags = 0;
  3912. /*
  3913. * free data blocks we just allocated.
  3914. * not a good idea to call discard here directly,
  3915. * but otherwise we'd need to call it every free().
  3916. */
  3917. ext4_discard_preallocations(inode);
  3918. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  3919. fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE;
  3920. ext4_free_blocks(handle, inode, NULL, newblock,
  3921. EXT4_C2B(sbi, allocated_clusters),
  3922. fb_flags);
  3923. }
  3924. goto out;
  3925. }
  3926. /*
  3927. * Cache the extent and update transaction to commit on fdatasync only
  3928. * when it is _not_ an unwritten extent.
  3929. */
  3930. if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
  3931. ext4_update_inode_fsync_trans(handle, inode, 1);
  3932. else
  3933. ext4_update_inode_fsync_trans(handle, inode, 0);
  3934. map->m_flags |= (EXT4_MAP_NEW | EXT4_MAP_MAPPED);
  3935. map->m_pblk = pblk;
  3936. map->m_len = ar.len;
  3937. allocated = map->m_len;
  3938. ext4_ext_show_leaf(inode, path);
  3939. out:
  3940. ext4_free_ext_path(path);
  3941. trace_ext4_ext_map_blocks_exit(inode, flags, map,
  3942. err ? err : allocated);
  3943. return err ? err : allocated;
  3944. }
  3945. int ext4_ext_truncate(handle_t *handle, struct inode *inode)
  3946. {
  3947. struct super_block *sb = inode->i_sb;
  3948. ext4_lblk_t last_block;
  3949. int err = 0;
  3950. /*
  3951. * TODO: optimization is possible here.
  3952. * Probably we need not scan at all,
  3953. * because page truncation is enough.
  3954. */
  3955. /* we have to know where to truncate from in crash case */
  3956. EXT4_I(inode)->i_disksize = inode->i_size;
  3957. err = ext4_mark_inode_dirty(handle, inode);
  3958. if (err)
  3959. return err;
  3960. last_block = (inode->i_size + sb->s_blocksize - 1)
  3961. >> EXT4_BLOCK_SIZE_BITS(sb);
  3962. ext4_es_remove_extent(inode, last_block, EXT_MAX_BLOCKS - last_block);
  3963. retry_remove_space:
  3964. err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
  3965. if (err == -ENOMEM) {
  3966. memalloc_retry_wait(GFP_ATOMIC);
  3967. goto retry_remove_space;
  3968. }
  3969. return err;
  3970. }
  3971. static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
  3972. ext4_lblk_t len, loff_t new_size,
  3973. int flags)
  3974. {
  3975. struct inode *inode = file_inode(file);
  3976. handle_t *handle;
  3977. int ret = 0, ret2 = 0, ret3 = 0;
  3978. int retries = 0;
  3979. int depth = 0;
  3980. struct ext4_map_blocks map;
  3981. unsigned int credits;
  3982. loff_t epos, old_size = i_size_read(inode);
  3983. BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
  3984. map.m_lblk = offset;
  3985. map.m_len = len;
  3986. /*
  3987. * Don't normalize the request if it can fit in one extent so
  3988. * that it doesn't get unnecessarily split into multiple
  3989. * extents.
  3990. */
  3991. if (len <= EXT_UNWRITTEN_MAX_LEN)
  3992. flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
  3993. /*
  3994. * credits to insert 1 extent into extent tree
  3995. */
  3996. credits = ext4_chunk_trans_blocks(inode, len);
  3997. depth = ext_depth(inode);
  3998. retry:
  3999. while (len) {
  4000. /*
  4001. * Recalculate credits when extent tree depth changes.
  4002. */
  4003. if (depth != ext_depth(inode)) {
  4004. credits = ext4_chunk_trans_blocks(inode, len);
  4005. depth = ext_depth(inode);
  4006. }
  4007. handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
  4008. credits);
  4009. if (IS_ERR(handle)) {
  4010. ret = PTR_ERR(handle);
  4011. break;
  4012. }
  4013. ret = ext4_map_blocks(handle, inode, &map, flags);
  4014. if (ret <= 0) {
  4015. ext4_debug("inode #%lu: block %u: len %u: "
  4016. "ext4_ext_map_blocks returned %d",
  4017. inode->i_ino, map.m_lblk,
  4018. map.m_len, ret);
  4019. ext4_mark_inode_dirty(handle, inode);
  4020. ext4_journal_stop(handle);
  4021. break;
  4022. }
  4023. /*
  4024. * allow a full retry cycle for any remaining allocations
  4025. */
  4026. retries = 0;
  4027. map.m_lblk += ret;
  4028. map.m_len = len = len - ret;
  4029. epos = (loff_t)map.m_lblk << inode->i_blkbits;
  4030. inode_set_ctime_current(inode);
  4031. if (new_size) {
  4032. if (epos > new_size)
  4033. epos = new_size;
  4034. if (ext4_update_inode_size(inode, epos) & 0x1)
  4035. inode_set_mtime_to_ts(inode,
  4036. inode_get_ctime(inode));
  4037. if (epos > old_size) {
  4038. pagecache_isize_extended(inode, old_size, epos);
  4039. ext4_zero_partial_blocks(handle, inode,
  4040. old_size, epos - old_size);
  4041. }
  4042. }
  4043. ret2 = ext4_mark_inode_dirty(handle, inode);
  4044. ext4_update_inode_fsync_trans(handle, inode, 1);
  4045. ret3 = ext4_journal_stop(handle);
  4046. ret2 = ret3 ? ret3 : ret2;
  4047. if (unlikely(ret2))
  4048. break;
  4049. }
  4050. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  4051. goto retry;
  4052. return ret > 0 ? ret2 : ret;
  4053. }
  4054. static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len);
  4055. static int ext4_insert_range(struct file *file, loff_t offset, loff_t len);
  4056. static long ext4_zero_range(struct file *file, loff_t offset,
  4057. loff_t len, int mode)
  4058. {
  4059. struct inode *inode = file_inode(file);
  4060. handle_t *handle = NULL;
  4061. loff_t new_size = 0;
  4062. loff_t end = offset + len;
  4063. ext4_lblk_t start_lblk, end_lblk;
  4064. unsigned int blocksize = i_blocksize(inode);
  4065. unsigned int blkbits = inode->i_blkbits;
  4066. int ret, flags, credits;
  4067. trace_ext4_zero_range(inode, offset, len, mode);
  4068. WARN_ON_ONCE(!inode_is_locked(inode));
  4069. /* Indirect files do not support unwritten extents */
  4070. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  4071. return -EOPNOTSUPP;
  4072. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  4073. (end > inode->i_size || end > EXT4_I(inode)->i_disksize)) {
  4074. new_size = end;
  4075. ret = inode_newsize_ok(inode, new_size);
  4076. if (ret)
  4077. return ret;
  4078. }
  4079. flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
  4080. /* Preallocate the range including the unaligned edges */
  4081. if (!IS_ALIGNED(offset | end, blocksize)) {
  4082. ext4_lblk_t alloc_lblk = offset >> blkbits;
  4083. ext4_lblk_t len_lblk = EXT4_MAX_BLOCKS(len, offset, blkbits);
  4084. ret = ext4_alloc_file_blocks(file, alloc_lblk, len_lblk,
  4085. new_size, flags);
  4086. if (ret)
  4087. return ret;
  4088. }
  4089. ret = ext4_update_disksize_before_punch(inode, offset, len);
  4090. if (ret)
  4091. return ret;
  4092. /* Now release the pages and zero block aligned part of pages */
  4093. ret = ext4_truncate_page_cache_block_range(inode, offset, end);
  4094. if (ret)
  4095. return ret;
  4096. /* Zero range excluding the unaligned edges */
  4097. start_lblk = EXT4_B_TO_LBLK(inode, offset);
  4098. end_lblk = end >> blkbits;
  4099. if (end_lblk > start_lblk) {
  4100. ext4_lblk_t zero_blks = end_lblk - start_lblk;
  4101. flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | EXT4_EX_NOCACHE);
  4102. ret = ext4_alloc_file_blocks(file, start_lblk, zero_blks,
  4103. new_size, flags);
  4104. if (ret)
  4105. return ret;
  4106. }
  4107. /* Finish zeroing out if it doesn't contain partial block */
  4108. if (IS_ALIGNED(offset | end, blocksize))
  4109. return ret;
  4110. /*
  4111. * In worst case we have to writeout two nonadjacent unwritten
  4112. * blocks and update the inode
  4113. */
  4114. credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
  4115. if (ext4_should_journal_data(inode))
  4116. credits += 2;
  4117. handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
  4118. if (IS_ERR(handle)) {
  4119. ret = PTR_ERR(handle);
  4120. ext4_std_error(inode->i_sb, ret);
  4121. return ret;
  4122. }
  4123. /* Zero out partial block at the edges of the range */
  4124. ret = ext4_zero_partial_blocks(handle, inode, offset, len);
  4125. if (ret)
  4126. goto out_handle;
  4127. if (new_size)
  4128. ext4_update_inode_size(inode, new_size);
  4129. ret = ext4_mark_inode_dirty(handle, inode);
  4130. if (unlikely(ret))
  4131. goto out_handle;
  4132. ext4_update_inode_fsync_trans(handle, inode, 1);
  4133. if (file->f_flags & O_SYNC)
  4134. ext4_handle_sync(handle);
  4135. out_handle:
  4136. ext4_journal_stop(handle);
  4137. return ret;
  4138. }
  4139. static long ext4_do_fallocate(struct file *file, loff_t offset,
  4140. loff_t len, int mode)
  4141. {
  4142. struct inode *inode = file_inode(file);
  4143. loff_t end = offset + len;
  4144. loff_t new_size = 0;
  4145. ext4_lblk_t start_lblk, len_lblk;
  4146. int ret;
  4147. trace_ext4_fallocate_enter(inode, offset, len, mode);
  4148. WARN_ON_ONCE(!inode_is_locked(inode));
  4149. start_lblk = offset >> inode->i_blkbits;
  4150. len_lblk = EXT4_MAX_BLOCKS(len, offset, inode->i_blkbits);
  4151. /* We only support preallocation for extent-based files only. */
  4152. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  4153. ret = -EOPNOTSUPP;
  4154. goto out;
  4155. }
  4156. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  4157. (end > inode->i_size || end > EXT4_I(inode)->i_disksize)) {
  4158. new_size = end;
  4159. ret = inode_newsize_ok(inode, new_size);
  4160. if (ret)
  4161. goto out;
  4162. }
  4163. ret = ext4_alloc_file_blocks(file, start_lblk, len_lblk, new_size,
  4164. EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT);
  4165. if (ret)
  4166. goto out;
  4167. if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
  4168. ret = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal,
  4169. EXT4_I(inode)->i_sync_tid);
  4170. }
  4171. out:
  4172. trace_ext4_fallocate_exit(inode, offset, len_lblk, ret);
  4173. return ret;
  4174. }
  4175. /*
  4176. * preallocate space for a file. This implements ext4's fallocate file
  4177. * operation, which gets called from sys_fallocate system call.
  4178. * For block-mapped files, posix_fallocate should fall back to the method
  4179. * of writing zeroes to the required new blocks (the same behavior which is
  4180. * expected for file systems which do not support fallocate() system call).
  4181. */
  4182. long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  4183. {
  4184. struct inode *inode = file_inode(file);
  4185. struct address_space *mapping = file->f_mapping;
  4186. int ret;
  4187. /*
  4188. * Encrypted inodes can't handle collapse range or insert
  4189. * range since we would need to re-encrypt blocks with a
  4190. * different IV or XTS tweak (which are based on the logical
  4191. * block number).
  4192. */
  4193. if (IS_ENCRYPTED(inode) &&
  4194. (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
  4195. return -EOPNOTSUPP;
  4196. /* Return error if mode is not supported */
  4197. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
  4198. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
  4199. FALLOC_FL_INSERT_RANGE))
  4200. return -EOPNOTSUPP;
  4201. inode_lock(inode);
  4202. ret = ext4_convert_inline_data(inode);
  4203. if (ret)
  4204. goto out_inode_lock;
  4205. /* Wait all existing dio workers, newcomers will block on i_rwsem */
  4206. inode_dio_wait(inode);
  4207. ret = file_modified(file);
  4208. if (ret)
  4209. goto out_inode_lock;
  4210. if ((mode & FALLOC_FL_MODE_MASK) == FALLOC_FL_ALLOCATE_RANGE) {
  4211. ret = ext4_do_fallocate(file, offset, len, mode);
  4212. goto out_inode_lock;
  4213. }
  4214. /*
  4215. * Follow-up operations will drop page cache, hold invalidate lock
  4216. * to prevent page faults from reinstantiating pages we have
  4217. * released from page cache.
  4218. */
  4219. filemap_invalidate_lock(mapping);
  4220. ret = ext4_break_layouts(inode);
  4221. if (ret)
  4222. goto out_invalidate_lock;
  4223. if (mode & FALLOC_FL_PUNCH_HOLE)
  4224. ret = ext4_punch_hole(file, offset, len);
  4225. else if (mode & FALLOC_FL_COLLAPSE_RANGE)
  4226. ret = ext4_collapse_range(file, offset, len);
  4227. else if (mode & FALLOC_FL_INSERT_RANGE)
  4228. ret = ext4_insert_range(file, offset, len);
  4229. else if (mode & FALLOC_FL_ZERO_RANGE)
  4230. ret = ext4_zero_range(file, offset, len, mode);
  4231. else
  4232. ret = -EOPNOTSUPP;
  4233. out_invalidate_lock:
  4234. filemap_invalidate_unlock(mapping);
  4235. out_inode_lock:
  4236. inode_unlock(inode);
  4237. return ret;
  4238. }
  4239. /*
  4240. * This function convert a range of blocks to written extents
  4241. * The caller of this function will pass the start offset and the size.
  4242. * all unwritten extents within this range will be converted to
  4243. * written extents.
  4244. *
  4245. * This function is called from the direct IO end io call back
  4246. * function, to convert the fallocated extents after IO is completed.
  4247. * Returns 0 on success.
  4248. */
  4249. int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
  4250. loff_t offset, ssize_t len)
  4251. {
  4252. unsigned int max_blocks;
  4253. int ret = 0, ret2 = 0, ret3 = 0;
  4254. struct ext4_map_blocks map;
  4255. unsigned int blkbits = inode->i_blkbits;
  4256. unsigned int credits = 0;
  4257. map.m_lblk = offset >> blkbits;
  4258. max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
  4259. if (!handle) {
  4260. /*
  4261. * credits to insert 1 extent into extent tree
  4262. */
  4263. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  4264. }
  4265. while (ret >= 0 && ret < max_blocks) {
  4266. map.m_lblk += ret;
  4267. map.m_len = (max_blocks -= ret);
  4268. if (credits) {
  4269. handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
  4270. credits);
  4271. if (IS_ERR(handle)) {
  4272. ret = PTR_ERR(handle);
  4273. break;
  4274. }
  4275. }
  4276. ret = ext4_map_blocks(handle, inode, &map,
  4277. EXT4_GET_BLOCKS_IO_CONVERT_EXT);
  4278. if (ret <= 0)
  4279. ext4_warning(inode->i_sb,
  4280. "inode #%lu: block %u: len %u: "
  4281. "ext4_ext_map_blocks returned %d",
  4282. inode->i_ino, map.m_lblk,
  4283. map.m_len, ret);
  4284. ret2 = ext4_mark_inode_dirty(handle, inode);
  4285. if (credits) {
  4286. ret3 = ext4_journal_stop(handle);
  4287. if (unlikely(ret3))
  4288. ret2 = ret3;
  4289. }
  4290. if (ret <= 0 || ret2)
  4291. break;
  4292. }
  4293. return ret > 0 ? ret2 : ret;
  4294. }
  4295. int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
  4296. {
  4297. int ret = 0, err = 0;
  4298. struct ext4_io_end_vec *io_end_vec;
  4299. /*
  4300. * This is somewhat ugly but the idea is clear: When transaction is
  4301. * reserved, everything goes into it. Otherwise we rather start several
  4302. * smaller transactions for conversion of each extent separately.
  4303. */
  4304. if (handle) {
  4305. handle = ext4_journal_start_reserved(handle,
  4306. EXT4_HT_EXT_CONVERT);
  4307. if (IS_ERR(handle))
  4308. return PTR_ERR(handle);
  4309. }
  4310. list_for_each_entry(io_end_vec, &io_end->list_vec, list) {
  4311. ret = ext4_convert_unwritten_extents(handle, io_end->inode,
  4312. io_end_vec->offset,
  4313. io_end_vec->size);
  4314. if (ret)
  4315. break;
  4316. }
  4317. if (handle)
  4318. err = ext4_journal_stop(handle);
  4319. return ret < 0 ? ret : err;
  4320. }
  4321. static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap)
  4322. {
  4323. __u64 physical = 0;
  4324. __u64 length = 0;
  4325. int blockbits = inode->i_sb->s_blocksize_bits;
  4326. int error = 0;
  4327. u16 iomap_type;
  4328. /* in-inode? */
  4329. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  4330. struct ext4_iloc iloc;
  4331. int offset; /* offset of xattr in inode */
  4332. error = ext4_get_inode_loc(inode, &iloc);
  4333. if (error)
  4334. return error;
  4335. physical = (__u64)iloc.bh->b_blocknr << blockbits;
  4336. offset = EXT4_GOOD_OLD_INODE_SIZE +
  4337. EXT4_I(inode)->i_extra_isize;
  4338. physical += offset;
  4339. length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
  4340. brelse(iloc.bh);
  4341. iomap_type = IOMAP_INLINE;
  4342. } else if (EXT4_I(inode)->i_file_acl) { /* external block */
  4343. physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
  4344. length = inode->i_sb->s_blocksize;
  4345. iomap_type = IOMAP_MAPPED;
  4346. } else {
  4347. /* no in-inode or external block for xattr, so return -ENOENT */
  4348. error = -ENOENT;
  4349. goto out;
  4350. }
  4351. iomap->addr = physical;
  4352. iomap->offset = 0;
  4353. iomap->length = length;
  4354. iomap->type = iomap_type;
  4355. iomap->flags = 0;
  4356. out:
  4357. return error;
  4358. }
  4359. static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset,
  4360. loff_t length, unsigned flags,
  4361. struct iomap *iomap, struct iomap *srcmap)
  4362. {
  4363. int error;
  4364. error = ext4_iomap_xattr_fiemap(inode, iomap);
  4365. if (error == 0 && (offset >= iomap->length))
  4366. error = -ENOENT;
  4367. return error;
  4368. }
  4369. static const struct iomap_ops ext4_iomap_xattr_ops = {
  4370. .iomap_begin = ext4_iomap_xattr_begin,
  4371. };
  4372. static int ext4_fiemap_check_ranges(struct inode *inode, u64 start, u64 *len)
  4373. {
  4374. u64 maxbytes = ext4_get_maxbytes(inode);
  4375. if (*len == 0)
  4376. return -EINVAL;
  4377. if (start > maxbytes)
  4378. return -EFBIG;
  4379. /*
  4380. * Shrink request scope to what the fs can actually handle.
  4381. */
  4382. if (*len > maxbytes || (maxbytes - *len) < start)
  4383. *len = maxbytes - start;
  4384. return 0;
  4385. }
  4386. int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4387. u64 start, u64 len)
  4388. {
  4389. int error = 0;
  4390. if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
  4391. error = ext4_ext_precache(inode);
  4392. if (error)
  4393. return error;
  4394. fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
  4395. }
  4396. /*
  4397. * For bitmap files the maximum size limit could be smaller than
  4398. * s_maxbytes, so check len here manually instead of just relying on the
  4399. * generic check.
  4400. */
  4401. error = ext4_fiemap_check_ranges(inode, start, &len);
  4402. if (error)
  4403. return error;
  4404. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  4405. fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
  4406. return iomap_fiemap(inode, fieinfo, start, len,
  4407. &ext4_iomap_xattr_ops);
  4408. }
  4409. return iomap_fiemap(inode, fieinfo, start, len, &ext4_iomap_report_ops);
  4410. }
  4411. int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4412. __u64 start, __u64 len)
  4413. {
  4414. ext4_lblk_t start_blk, len_blks;
  4415. __u64 last_blk;
  4416. int error = 0;
  4417. if (ext4_has_inline_data(inode)) {
  4418. int has_inline;
  4419. down_read(&EXT4_I(inode)->xattr_sem);
  4420. has_inline = ext4_has_inline_data(inode);
  4421. up_read(&EXT4_I(inode)->xattr_sem);
  4422. if (has_inline)
  4423. return 0;
  4424. }
  4425. if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
  4426. error = ext4_ext_precache(inode);
  4427. if (error)
  4428. return error;
  4429. fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
  4430. }
  4431. error = fiemap_prep(inode, fieinfo, start, &len, 0);
  4432. if (error)
  4433. return error;
  4434. error = ext4_fiemap_check_ranges(inode, start, &len);
  4435. if (error)
  4436. return error;
  4437. start_blk = start >> inode->i_sb->s_blocksize_bits;
  4438. last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
  4439. if (last_blk >= EXT_MAX_BLOCKS)
  4440. last_blk = EXT_MAX_BLOCKS-1;
  4441. len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
  4442. /*
  4443. * Walk the extent tree gathering extent information
  4444. * and pushing extents back to the user.
  4445. */
  4446. return ext4_fill_es_cache_info(inode, start_blk, len_blks, fieinfo);
  4447. }
  4448. /*
  4449. * ext4_ext_shift_path_extents:
  4450. * Shift the extents of a path structure lying between path[depth].p_ext
  4451. * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
  4452. * if it is right shift or left shift operation.
  4453. */
  4454. static int
  4455. ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
  4456. struct inode *inode, handle_t *handle,
  4457. enum SHIFT_DIRECTION SHIFT)
  4458. {
  4459. int depth, err = 0;
  4460. struct ext4_extent *ex_start, *ex_last;
  4461. bool update = false;
  4462. int credits, restart_credits;
  4463. depth = path->p_depth;
  4464. while (depth >= 0) {
  4465. if (depth == path->p_depth) {
  4466. ex_start = path[depth].p_ext;
  4467. if (!ex_start)
  4468. return -EFSCORRUPTED;
  4469. ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
  4470. /* leaf + sb + inode */
  4471. credits = 3;
  4472. if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr)) {
  4473. update = true;
  4474. /* extent tree + sb + inode */
  4475. credits = depth + 2;
  4476. }
  4477. restart_credits = ext4_writepage_trans_blocks(inode);
  4478. err = ext4_datasem_ensure_credits(handle, inode, credits,
  4479. restart_credits, 0);
  4480. if (err) {
  4481. if (err > 0)
  4482. err = -EAGAIN;
  4483. goto out;
  4484. }
  4485. err = ext4_ext_get_access(handle, inode, path + depth);
  4486. if (err)
  4487. goto out;
  4488. while (ex_start <= ex_last) {
  4489. if (SHIFT == SHIFT_LEFT) {
  4490. le32_add_cpu(&ex_start->ee_block,
  4491. -shift);
  4492. /* Try to merge to the left. */
  4493. if ((ex_start >
  4494. EXT_FIRST_EXTENT(path[depth].p_hdr))
  4495. &&
  4496. ext4_ext_try_to_merge_right(inode,
  4497. path, ex_start - 1))
  4498. ex_last--;
  4499. else
  4500. ex_start++;
  4501. } else {
  4502. le32_add_cpu(&ex_last->ee_block, shift);
  4503. ext4_ext_try_to_merge_right(inode, path,
  4504. ex_last);
  4505. ex_last--;
  4506. }
  4507. }
  4508. err = ext4_ext_dirty(handle, inode, path + depth);
  4509. if (err)
  4510. goto out;
  4511. if (--depth < 0 || !update)
  4512. break;
  4513. }
  4514. /* Update index too */
  4515. err = ext4_ext_get_access(handle, inode, path + depth);
  4516. if (err)
  4517. goto out;
  4518. if (SHIFT == SHIFT_LEFT)
  4519. le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
  4520. else
  4521. le32_add_cpu(&path[depth].p_idx->ei_block, shift);
  4522. err = ext4_ext_dirty(handle, inode, path + depth);
  4523. if (err)
  4524. goto out;
  4525. /* we are done if current index is not a starting index */
  4526. if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
  4527. break;
  4528. depth--;
  4529. }
  4530. out:
  4531. return err;
  4532. }
  4533. /*
  4534. * ext4_ext_shift_extents:
  4535. * All the extents which lies in the range from @start to the last allocated
  4536. * block for the @inode are shifted either towards left or right (depending
  4537. * upon @SHIFT) by @shift blocks.
  4538. * On success, 0 is returned, error otherwise.
  4539. */
  4540. static int
  4541. ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
  4542. ext4_lblk_t start, ext4_lblk_t shift,
  4543. enum SHIFT_DIRECTION SHIFT)
  4544. {
  4545. struct ext4_ext_path *path;
  4546. int ret = 0, depth;
  4547. struct ext4_extent *extent;
  4548. ext4_lblk_t stop, *iterator, ex_start, ex_end;
  4549. ext4_lblk_t tmp = EXT_MAX_BLOCKS;
  4550. /* Let path point to the last extent */
  4551. path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
  4552. EXT4_EX_NOCACHE);
  4553. if (IS_ERR(path))
  4554. return PTR_ERR(path);
  4555. depth = path->p_depth;
  4556. extent = path[depth].p_ext;
  4557. if (!extent)
  4558. goto out;
  4559. stop = le32_to_cpu(extent->ee_block);
  4560. /*
  4561. * For left shifts, make sure the hole on the left is big enough to
  4562. * accommodate the shift. For right shifts, make sure the last extent
  4563. * won't be shifted beyond EXT_MAX_BLOCKS.
  4564. */
  4565. if (SHIFT == SHIFT_LEFT) {
  4566. path = ext4_find_extent(inode, start - 1, path,
  4567. EXT4_EX_NOCACHE);
  4568. if (IS_ERR(path))
  4569. return PTR_ERR(path);
  4570. depth = path->p_depth;
  4571. extent = path[depth].p_ext;
  4572. if (extent) {
  4573. ex_start = le32_to_cpu(extent->ee_block);
  4574. ex_end = le32_to_cpu(extent->ee_block) +
  4575. ext4_ext_get_actual_len(extent);
  4576. } else {
  4577. ex_start = 0;
  4578. ex_end = 0;
  4579. }
  4580. if ((start == ex_start && shift > ex_start) ||
  4581. (shift > start - ex_end)) {
  4582. ret = -EINVAL;
  4583. goto out;
  4584. }
  4585. } else {
  4586. if (shift > EXT_MAX_BLOCKS -
  4587. (stop + ext4_ext_get_actual_len(extent))) {
  4588. ret = -EINVAL;
  4589. goto out;
  4590. }
  4591. }
  4592. /*
  4593. * In case of left shift, iterator points to start and it is increased
  4594. * till we reach stop. In case of right shift, iterator points to stop
  4595. * and it is decreased till we reach start.
  4596. */
  4597. again:
  4598. ret = 0;
  4599. if (SHIFT == SHIFT_LEFT)
  4600. iterator = &start;
  4601. else
  4602. iterator = &stop;
  4603. if (tmp != EXT_MAX_BLOCKS)
  4604. *iterator = tmp;
  4605. /*
  4606. * Its safe to start updating extents. Start and stop are unsigned, so
  4607. * in case of right shift if extent with 0 block is reached, iterator
  4608. * becomes NULL to indicate the end of the loop.
  4609. */
  4610. while (iterator && start <= stop) {
  4611. path = ext4_find_extent(inode, *iterator, path,
  4612. EXT4_EX_NOCACHE);
  4613. if (IS_ERR(path))
  4614. return PTR_ERR(path);
  4615. depth = path->p_depth;
  4616. extent = path[depth].p_ext;
  4617. if (!extent) {
  4618. EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
  4619. (unsigned long) *iterator);
  4620. return -EFSCORRUPTED;
  4621. }
  4622. if (SHIFT == SHIFT_LEFT && *iterator >
  4623. le32_to_cpu(extent->ee_block)) {
  4624. /* Hole, move to the next extent */
  4625. if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
  4626. path[depth].p_ext++;
  4627. } else {
  4628. *iterator = ext4_ext_next_allocated_block(path);
  4629. continue;
  4630. }
  4631. }
  4632. tmp = *iterator;
  4633. if (SHIFT == SHIFT_LEFT) {
  4634. extent = EXT_LAST_EXTENT(path[depth].p_hdr);
  4635. *iterator = le32_to_cpu(extent->ee_block) +
  4636. ext4_ext_get_actual_len(extent);
  4637. } else {
  4638. extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
  4639. if (le32_to_cpu(extent->ee_block) > start)
  4640. *iterator = le32_to_cpu(extent->ee_block) - 1;
  4641. else if (le32_to_cpu(extent->ee_block) == start)
  4642. iterator = NULL;
  4643. else {
  4644. extent = EXT_LAST_EXTENT(path[depth].p_hdr);
  4645. while (le32_to_cpu(extent->ee_block) >= start)
  4646. extent--;
  4647. if (extent == EXT_LAST_EXTENT(path[depth].p_hdr))
  4648. break;
  4649. extent++;
  4650. iterator = NULL;
  4651. }
  4652. path[depth].p_ext = extent;
  4653. }
  4654. ret = ext4_ext_shift_path_extents(path, shift, inode,
  4655. handle, SHIFT);
  4656. /* iterator can be NULL which means we should break */
  4657. if (ret == -EAGAIN)
  4658. goto again;
  4659. if (ret)
  4660. break;
  4661. }
  4662. out:
  4663. ext4_free_ext_path(path);
  4664. return ret;
  4665. }
  4666. /*
  4667. * ext4_collapse_range:
  4668. * This implements the fallocate's collapse range functionality for ext4
  4669. * Returns: 0 and non-zero on error.
  4670. */
  4671. static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
  4672. {
  4673. struct inode *inode = file_inode(file);
  4674. struct super_block *sb = inode->i_sb;
  4675. struct address_space *mapping = inode->i_mapping;
  4676. loff_t end = offset + len;
  4677. ext4_lblk_t start_lblk, end_lblk;
  4678. handle_t *handle;
  4679. unsigned int credits;
  4680. loff_t start, new_size;
  4681. int ret;
  4682. trace_ext4_collapse_range(inode, offset, len);
  4683. WARN_ON_ONCE(!inode_is_locked(inode));
  4684. /* Currently just for extent based files */
  4685. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  4686. return -EOPNOTSUPP;
  4687. /* Collapse range works only on fs cluster size aligned regions. */
  4688. if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
  4689. return -EINVAL;
  4690. /*
  4691. * There is no need to overlap collapse range with EOF, in which case
  4692. * it is effectively a truncate operation
  4693. */
  4694. if (end >= inode->i_size)
  4695. return -EINVAL;
  4696. /*
  4697. * Write tail of the last page before removed range and data that
  4698. * will be shifted since they will get removed from the page cache
  4699. * below. We are also protected from pages becoming dirty by
  4700. * i_rwsem and invalidate_lock.
  4701. * Need to round down offset to be aligned with page size boundary
  4702. * for page size > block size.
  4703. */
  4704. start = round_down(offset, PAGE_SIZE);
  4705. ret = filemap_write_and_wait_range(mapping, start, offset);
  4706. if (!ret)
  4707. ret = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
  4708. if (ret)
  4709. return ret;
  4710. truncate_pagecache(inode, start);
  4711. credits = ext4_writepage_trans_blocks(inode);
  4712. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  4713. if (IS_ERR(handle))
  4714. return PTR_ERR(handle);
  4715. ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle);
  4716. start_lblk = offset >> inode->i_blkbits;
  4717. end_lblk = (offset + len) >> inode->i_blkbits;
  4718. down_write(&EXT4_I(inode)->i_data_sem);
  4719. ext4_discard_preallocations(inode);
  4720. ext4_es_remove_extent(inode, start_lblk, EXT_MAX_BLOCKS - start_lblk);
  4721. ret = ext4_ext_remove_space(inode, start_lblk, end_lblk - 1);
  4722. if (ret) {
  4723. up_write(&EXT4_I(inode)->i_data_sem);
  4724. goto out_handle;
  4725. }
  4726. ext4_discard_preallocations(inode);
  4727. ret = ext4_ext_shift_extents(inode, handle, end_lblk,
  4728. end_lblk - start_lblk, SHIFT_LEFT);
  4729. if (ret) {
  4730. up_write(&EXT4_I(inode)->i_data_sem);
  4731. goto out_handle;
  4732. }
  4733. new_size = inode->i_size - len;
  4734. i_size_write(inode, new_size);
  4735. EXT4_I(inode)->i_disksize = new_size;
  4736. up_write(&EXT4_I(inode)->i_data_sem);
  4737. ret = ext4_mark_inode_dirty(handle, inode);
  4738. if (ret)
  4739. goto out_handle;
  4740. ext4_update_inode_fsync_trans(handle, inode, 1);
  4741. if (IS_SYNC(inode))
  4742. ext4_handle_sync(handle);
  4743. out_handle:
  4744. ext4_journal_stop(handle);
  4745. return ret;
  4746. }
  4747. /*
  4748. * ext4_insert_range:
  4749. * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
  4750. * The data blocks starting from @offset to the EOF are shifted by @len
  4751. * towards right to create a hole in the @inode. Inode size is increased
  4752. * by len bytes.
  4753. * Returns 0 on success, error otherwise.
  4754. */
  4755. static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
  4756. {
  4757. struct inode *inode = file_inode(file);
  4758. struct super_block *sb = inode->i_sb;
  4759. struct address_space *mapping = inode->i_mapping;
  4760. handle_t *handle;
  4761. struct ext4_ext_path *path;
  4762. struct ext4_extent *extent;
  4763. ext4_lblk_t start_lblk, len_lblk, ee_start_lblk = 0;
  4764. unsigned int credits, ee_len;
  4765. int ret, depth, split_flag = 0;
  4766. loff_t start;
  4767. trace_ext4_insert_range(inode, offset, len);
  4768. WARN_ON_ONCE(!inode_is_locked(inode));
  4769. /* Currently just for extent based files */
  4770. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  4771. return -EOPNOTSUPP;
  4772. /* Insert range works only on fs cluster size aligned regions. */
  4773. if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb)))
  4774. return -EINVAL;
  4775. /* Offset must be less than i_size */
  4776. if (offset >= inode->i_size)
  4777. return -EINVAL;
  4778. /* Check whether the maximum file size would be exceeded */
  4779. if (len > inode->i_sb->s_maxbytes - inode->i_size)
  4780. return -EFBIG;
  4781. /*
  4782. * Write out all dirty pages. Need to round down to align start offset
  4783. * to page size boundary for page size > block size.
  4784. */
  4785. start = round_down(offset, PAGE_SIZE);
  4786. ret = filemap_write_and_wait_range(mapping, start, LLONG_MAX);
  4787. if (ret)
  4788. return ret;
  4789. truncate_pagecache(inode, start);
  4790. credits = ext4_writepage_trans_blocks(inode);
  4791. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  4792. if (IS_ERR(handle))
  4793. return PTR_ERR(handle);
  4794. ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle);
  4795. /* Expand file to avoid data loss if there is error while shifting */
  4796. inode->i_size += len;
  4797. EXT4_I(inode)->i_disksize += len;
  4798. ret = ext4_mark_inode_dirty(handle, inode);
  4799. if (ret)
  4800. goto out_handle;
  4801. start_lblk = offset >> inode->i_blkbits;
  4802. len_lblk = len >> inode->i_blkbits;
  4803. down_write(&EXT4_I(inode)->i_data_sem);
  4804. ext4_discard_preallocations(inode);
  4805. path = ext4_find_extent(inode, start_lblk, NULL, 0);
  4806. if (IS_ERR(path)) {
  4807. up_write(&EXT4_I(inode)->i_data_sem);
  4808. ret = PTR_ERR(path);
  4809. goto out_handle;
  4810. }
  4811. depth = ext_depth(inode);
  4812. extent = path[depth].p_ext;
  4813. if (extent) {
  4814. ee_start_lblk = le32_to_cpu(extent->ee_block);
  4815. ee_len = ext4_ext_get_actual_len(extent);
  4816. /*
  4817. * If start_lblk is not the starting block of extent, split
  4818. * the extent @start_lblk
  4819. */
  4820. if ((start_lblk > ee_start_lblk) &&
  4821. (start_lblk < (ee_start_lblk + ee_len))) {
  4822. if (ext4_ext_is_unwritten(extent))
  4823. split_flag = EXT4_EXT_MARK_UNWRIT1 |
  4824. EXT4_EXT_MARK_UNWRIT2;
  4825. path = ext4_split_extent_at(handle, inode, path,
  4826. start_lblk, split_flag,
  4827. EXT4_EX_NOCACHE |
  4828. EXT4_GET_BLOCKS_PRE_IO |
  4829. EXT4_GET_BLOCKS_METADATA_NOFAIL);
  4830. }
  4831. if (IS_ERR(path)) {
  4832. up_write(&EXT4_I(inode)->i_data_sem);
  4833. ret = PTR_ERR(path);
  4834. goto out_handle;
  4835. }
  4836. }
  4837. ext4_free_ext_path(path);
  4838. ext4_es_remove_extent(inode, start_lblk, EXT_MAX_BLOCKS - start_lblk);
  4839. /*
  4840. * if start_lblk lies in a hole which is at start of file, use
  4841. * ee_start_lblk to shift extents
  4842. */
  4843. ret = ext4_ext_shift_extents(inode, handle,
  4844. max(ee_start_lblk, start_lblk), len_lblk, SHIFT_RIGHT);
  4845. up_write(&EXT4_I(inode)->i_data_sem);
  4846. if (ret)
  4847. goto out_handle;
  4848. ext4_update_inode_fsync_trans(handle, inode, 1);
  4849. if (IS_SYNC(inode))
  4850. ext4_handle_sync(handle);
  4851. out_handle:
  4852. ext4_journal_stop(handle);
  4853. return ret;
  4854. }
  4855. /**
  4856. * ext4_swap_extents() - Swap extents between two inodes
  4857. * @handle: handle for this transaction
  4858. * @inode1: First inode
  4859. * @inode2: Second inode
  4860. * @lblk1: Start block for first inode
  4861. * @lblk2: Start block for second inode
  4862. * @count: Number of blocks to swap
  4863. * @unwritten: Mark second inode's extents as unwritten after swap
  4864. * @erp: Pointer to save error value
  4865. *
  4866. * This helper routine does exactly what is promise "swap extents". All other
  4867. * stuff such as page-cache locking consistency, bh mapping consistency or
  4868. * extent's data copying must be performed by caller.
  4869. * Locking:
  4870. * i_rwsem is held for both inodes
  4871. * i_data_sem is locked for write for both inodes
  4872. * Assumptions:
  4873. * All pages from requested range are locked for both inodes
  4874. */
  4875. int
  4876. ext4_swap_extents(handle_t *handle, struct inode *inode1,
  4877. struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
  4878. ext4_lblk_t count, int unwritten, int *erp)
  4879. {
  4880. struct ext4_ext_path *path1 = NULL;
  4881. struct ext4_ext_path *path2 = NULL;
  4882. int replaced_count = 0;
  4883. BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
  4884. BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
  4885. BUG_ON(!inode_is_locked(inode1));
  4886. BUG_ON(!inode_is_locked(inode2));
  4887. ext4_es_remove_extent(inode1, lblk1, count);
  4888. ext4_es_remove_extent(inode2, lblk2, count);
  4889. while (count) {
  4890. struct ext4_extent *ex1, *ex2, tmp_ex;
  4891. ext4_lblk_t e1_blk, e2_blk;
  4892. int e1_len, e2_len, len;
  4893. int split = 0;
  4894. path1 = ext4_find_extent(inode1, lblk1, path1, EXT4_EX_NOCACHE);
  4895. if (IS_ERR(path1)) {
  4896. *erp = PTR_ERR(path1);
  4897. goto errout;
  4898. }
  4899. path2 = ext4_find_extent(inode2, lblk2, path2, EXT4_EX_NOCACHE);
  4900. if (IS_ERR(path2)) {
  4901. *erp = PTR_ERR(path2);
  4902. goto errout;
  4903. }
  4904. ex1 = path1[path1->p_depth].p_ext;
  4905. ex2 = path2[path2->p_depth].p_ext;
  4906. /* Do we have something to swap ? */
  4907. if (unlikely(!ex2 || !ex1))
  4908. goto errout;
  4909. e1_blk = le32_to_cpu(ex1->ee_block);
  4910. e2_blk = le32_to_cpu(ex2->ee_block);
  4911. e1_len = ext4_ext_get_actual_len(ex1);
  4912. e2_len = ext4_ext_get_actual_len(ex2);
  4913. /* Hole handling */
  4914. if (!in_range(lblk1, e1_blk, e1_len) ||
  4915. !in_range(lblk2, e2_blk, e2_len)) {
  4916. ext4_lblk_t next1, next2;
  4917. /* if hole after extent, then go to next extent */
  4918. next1 = ext4_ext_next_allocated_block(path1);
  4919. next2 = ext4_ext_next_allocated_block(path2);
  4920. /* If hole before extent, then shift to that extent */
  4921. if (e1_blk > lblk1)
  4922. next1 = e1_blk;
  4923. if (e2_blk > lblk2)
  4924. next2 = e2_blk;
  4925. /* Do we have something to swap */
  4926. if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
  4927. goto errout;
  4928. /* Move to the rightest boundary */
  4929. len = next1 - lblk1;
  4930. if (len < next2 - lblk2)
  4931. len = next2 - lblk2;
  4932. if (len > count)
  4933. len = count;
  4934. lblk1 += len;
  4935. lblk2 += len;
  4936. count -= len;
  4937. continue;
  4938. }
  4939. /* Prepare left boundary */
  4940. if (e1_blk < lblk1) {
  4941. split = 1;
  4942. path1 = ext4_force_split_extent_at(handle, inode1,
  4943. path1, lblk1, 0);
  4944. if (IS_ERR(path1)) {
  4945. *erp = PTR_ERR(path1);
  4946. goto errout;
  4947. }
  4948. }
  4949. if (e2_blk < lblk2) {
  4950. split = 1;
  4951. path2 = ext4_force_split_extent_at(handle, inode2,
  4952. path2, lblk2, 0);
  4953. if (IS_ERR(path2)) {
  4954. *erp = PTR_ERR(path2);
  4955. goto errout;
  4956. }
  4957. }
  4958. /* ext4_split_extent_at() may result in leaf extent split,
  4959. * path must to be revalidated. */
  4960. if (split)
  4961. continue;
  4962. /* Prepare right boundary */
  4963. len = count;
  4964. if (len > e1_blk + e1_len - lblk1)
  4965. len = e1_blk + e1_len - lblk1;
  4966. if (len > e2_blk + e2_len - lblk2)
  4967. len = e2_blk + e2_len - lblk2;
  4968. if (len != e1_len) {
  4969. split = 1;
  4970. path1 = ext4_force_split_extent_at(handle, inode1,
  4971. path1, lblk1 + len, 0);
  4972. if (IS_ERR(path1)) {
  4973. *erp = PTR_ERR(path1);
  4974. goto errout;
  4975. }
  4976. }
  4977. if (len != e2_len) {
  4978. split = 1;
  4979. path2 = ext4_force_split_extent_at(handle, inode2,
  4980. path2, lblk2 + len, 0);
  4981. if (IS_ERR(path2)) {
  4982. *erp = PTR_ERR(path2);
  4983. goto errout;
  4984. }
  4985. }
  4986. /* ext4_split_extent_at() may result in leaf extent split,
  4987. * path must to be revalidated. */
  4988. if (split)
  4989. continue;
  4990. BUG_ON(e2_len != e1_len);
  4991. *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
  4992. if (unlikely(*erp))
  4993. goto errout;
  4994. *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
  4995. if (unlikely(*erp))
  4996. goto errout;
  4997. /* Both extents are fully inside boundaries. Swap it now */
  4998. tmp_ex = *ex1;
  4999. ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
  5000. ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
  5001. ex1->ee_len = cpu_to_le16(e2_len);
  5002. ex2->ee_len = cpu_to_le16(e1_len);
  5003. if (unwritten)
  5004. ext4_ext_mark_unwritten(ex2);
  5005. if (ext4_ext_is_unwritten(&tmp_ex))
  5006. ext4_ext_mark_unwritten(ex1);
  5007. ext4_ext_try_to_merge(handle, inode2, path2, ex2);
  5008. ext4_ext_try_to_merge(handle, inode1, path1, ex1);
  5009. *erp = ext4_ext_dirty(handle, inode2, path2 +
  5010. path2->p_depth);
  5011. if (unlikely(*erp))
  5012. goto errout;
  5013. *erp = ext4_ext_dirty(handle, inode1, path1 +
  5014. path1->p_depth);
  5015. /*
  5016. * Looks scarry ah..? second inode already points to new blocks,
  5017. * and it was successfully dirtied. But luckily error may happen
  5018. * only due to journal error, so full transaction will be
  5019. * aborted anyway.
  5020. */
  5021. if (unlikely(*erp))
  5022. goto errout;
  5023. lblk1 += len;
  5024. lblk2 += len;
  5025. replaced_count += len;
  5026. count -= len;
  5027. }
  5028. errout:
  5029. ext4_free_ext_path(path1);
  5030. ext4_free_ext_path(path2);
  5031. return replaced_count;
  5032. }
  5033. /*
  5034. * ext4_clu_mapped - determine whether any block in a logical cluster has
  5035. * been mapped to a physical cluster
  5036. *
  5037. * @inode - file containing the logical cluster
  5038. * @lclu - logical cluster of interest
  5039. *
  5040. * Returns 1 if any block in the logical cluster is mapped, signifying
  5041. * that a physical cluster has been allocated for it. Otherwise,
  5042. * returns 0. Can also return negative error codes. Derived from
  5043. * ext4_ext_map_blocks().
  5044. */
  5045. int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
  5046. {
  5047. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  5048. struct ext4_ext_path *path;
  5049. int depth, mapped = 0, err = 0;
  5050. struct ext4_extent *extent;
  5051. ext4_lblk_t first_lblk, first_lclu, last_lclu;
  5052. /*
  5053. * if data can be stored inline, the logical cluster isn't
  5054. * mapped - no physical clusters have been allocated, and the
  5055. * file has no extents
  5056. */
  5057. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) ||
  5058. ext4_has_inline_data(inode))
  5059. return 0;
  5060. /* search for the extent closest to the first block in the cluster */
  5061. path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
  5062. if (IS_ERR(path))
  5063. return PTR_ERR(path);
  5064. depth = ext_depth(inode);
  5065. /*
  5066. * A consistent leaf must not be empty. This situation is possible,
  5067. * though, _during_ tree modification, and it's why an assert can't
  5068. * be put in ext4_find_extent().
  5069. */
  5070. if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
  5071. EXT4_ERROR_INODE(inode,
  5072. "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
  5073. (unsigned long) EXT4_C2B(sbi, lclu),
  5074. depth, path[depth].p_block);
  5075. err = -EFSCORRUPTED;
  5076. goto out;
  5077. }
  5078. extent = path[depth].p_ext;
  5079. /* can't be mapped if the extent tree is empty */
  5080. if (extent == NULL)
  5081. goto out;
  5082. first_lblk = le32_to_cpu(extent->ee_block);
  5083. first_lclu = EXT4_B2C(sbi, first_lblk);
  5084. /*
  5085. * Three possible outcomes at this point - found extent spanning
  5086. * the target cluster, to the left of the target cluster, or to the
  5087. * right of the target cluster. The first two cases are handled here.
  5088. * The last case indicates the target cluster is not mapped.
  5089. */
  5090. if (lclu >= first_lclu) {
  5091. last_lclu = EXT4_B2C(sbi, first_lblk +
  5092. ext4_ext_get_actual_len(extent) - 1);
  5093. if (lclu <= last_lclu) {
  5094. mapped = 1;
  5095. } else {
  5096. first_lblk = ext4_ext_next_allocated_block(path);
  5097. first_lclu = EXT4_B2C(sbi, first_lblk);
  5098. if (lclu == first_lclu)
  5099. mapped = 1;
  5100. }
  5101. }
  5102. out:
  5103. ext4_free_ext_path(path);
  5104. return err ? err : mapped;
  5105. }
  5106. /*
  5107. * Updates physical block address and unwritten status of extent
  5108. * starting at lblk start and of len. If such an extent doesn't exist,
  5109. * this function splits the extent tree appropriately to create an
  5110. * extent like this. This function is called in the fast commit
  5111. * replay path. Returns 0 on success and error on failure.
  5112. */
  5113. int ext4_ext_replay_update_ex(struct inode *inode, ext4_lblk_t start,
  5114. int len, int unwritten, ext4_fsblk_t pblk)
  5115. {
  5116. struct ext4_ext_path *path;
  5117. struct ext4_extent *ex;
  5118. int ret;
  5119. path = ext4_find_extent(inode, start, NULL, 0);
  5120. if (IS_ERR(path))
  5121. return PTR_ERR(path);
  5122. ex = path[path->p_depth].p_ext;
  5123. if (!ex) {
  5124. ret = -EFSCORRUPTED;
  5125. goto out;
  5126. }
  5127. if (le32_to_cpu(ex->ee_block) != start ||
  5128. ext4_ext_get_actual_len(ex) != len) {
  5129. /* We need to split this extent to match our extent first */
  5130. down_write(&EXT4_I(inode)->i_data_sem);
  5131. path = ext4_force_split_extent_at(NULL, inode, path, start, 1);
  5132. up_write(&EXT4_I(inode)->i_data_sem);
  5133. if (IS_ERR(path)) {
  5134. ret = PTR_ERR(path);
  5135. goto out;
  5136. }
  5137. path = ext4_find_extent(inode, start, path, 0);
  5138. if (IS_ERR(path))
  5139. return PTR_ERR(path);
  5140. ex = path[path->p_depth].p_ext;
  5141. WARN_ON(le32_to_cpu(ex->ee_block) != start);
  5142. if (ext4_ext_get_actual_len(ex) != len) {
  5143. down_write(&EXT4_I(inode)->i_data_sem);
  5144. path = ext4_force_split_extent_at(NULL, inode, path,
  5145. start + len, 1);
  5146. up_write(&EXT4_I(inode)->i_data_sem);
  5147. if (IS_ERR(path)) {
  5148. ret = PTR_ERR(path);
  5149. goto out;
  5150. }
  5151. path = ext4_find_extent(inode, start, path, 0);
  5152. if (IS_ERR(path))
  5153. return PTR_ERR(path);
  5154. ex = path[path->p_depth].p_ext;
  5155. }
  5156. }
  5157. if (unwritten)
  5158. ext4_ext_mark_unwritten(ex);
  5159. else
  5160. ext4_ext_mark_initialized(ex);
  5161. ext4_ext_store_pblock(ex, pblk);
  5162. down_write(&EXT4_I(inode)->i_data_sem);
  5163. ret = ext4_ext_dirty(NULL, inode, &path[path->p_depth]);
  5164. up_write(&EXT4_I(inode)->i_data_sem);
  5165. out:
  5166. ext4_free_ext_path(path);
  5167. ext4_mark_inode_dirty(NULL, inode);
  5168. return ret;
  5169. }
  5170. /* Try to shrink the extent tree */
  5171. void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end)
  5172. {
  5173. struct ext4_ext_path *path = NULL;
  5174. struct ext4_extent *ex;
  5175. ext4_lblk_t old_cur, cur = 0;
  5176. while (cur < end) {
  5177. path = ext4_find_extent(inode, cur, NULL, 0);
  5178. if (IS_ERR(path))
  5179. return;
  5180. ex = path[path->p_depth].p_ext;
  5181. if (!ex) {
  5182. ext4_free_ext_path(path);
  5183. ext4_mark_inode_dirty(NULL, inode);
  5184. return;
  5185. }
  5186. old_cur = cur;
  5187. cur = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
  5188. if (cur <= old_cur)
  5189. cur = old_cur + 1;
  5190. ext4_ext_try_to_merge(NULL, inode, path, ex);
  5191. down_write(&EXT4_I(inode)->i_data_sem);
  5192. ext4_ext_dirty(NULL, inode, &path[path->p_depth]);
  5193. up_write(&EXT4_I(inode)->i_data_sem);
  5194. ext4_mark_inode_dirty(NULL, inode);
  5195. ext4_free_ext_path(path);
  5196. }
  5197. }
  5198. /* Check if *cur is a hole and if it is, skip it */
  5199. static int skip_hole(struct inode *inode, ext4_lblk_t *cur)
  5200. {
  5201. int ret;
  5202. struct ext4_map_blocks map;
  5203. map.m_lblk = *cur;
  5204. map.m_len = ((inode->i_size) >> inode->i_sb->s_blocksize_bits) - *cur;
  5205. ret = ext4_map_blocks(NULL, inode, &map, 0);
  5206. if (ret < 0)
  5207. return ret;
  5208. if (ret != 0)
  5209. return 0;
  5210. *cur = *cur + map.m_len;
  5211. return 0;
  5212. }
  5213. /* Count number of blocks used by this inode and update i_blocks */
  5214. int ext4_ext_replay_set_iblocks(struct inode *inode)
  5215. {
  5216. struct ext4_ext_path *path = NULL, *path2 = NULL;
  5217. struct ext4_extent *ex;
  5218. ext4_lblk_t cur = 0, end;
  5219. int numblks = 0, i, ret = 0;
  5220. ext4_fsblk_t cmp1, cmp2;
  5221. struct ext4_map_blocks map;
  5222. /* Determin the size of the file first */
  5223. path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
  5224. EXT4_EX_NOCACHE);
  5225. if (IS_ERR(path))
  5226. return PTR_ERR(path);
  5227. ex = path[path->p_depth].p_ext;
  5228. if (!ex)
  5229. goto out;
  5230. end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
  5231. /* Count the number of data blocks */
  5232. cur = 0;
  5233. while (cur < end) {
  5234. map.m_lblk = cur;
  5235. map.m_len = end - cur;
  5236. ret = ext4_map_blocks(NULL, inode, &map, 0);
  5237. if (ret < 0)
  5238. break;
  5239. if (ret > 0)
  5240. numblks += ret;
  5241. cur = cur + map.m_len;
  5242. }
  5243. /*
  5244. * Count the number of extent tree blocks. We do it by looking up
  5245. * two successive extents and determining the difference between
  5246. * their paths. When path is different for 2 successive extents
  5247. * we compare the blocks in the path at each level and increment
  5248. * iblocks by total number of differences found.
  5249. */
  5250. cur = 0;
  5251. ret = skip_hole(inode, &cur);
  5252. if (ret < 0)
  5253. goto out;
  5254. path = ext4_find_extent(inode, cur, path, 0);
  5255. if (IS_ERR(path))
  5256. goto out;
  5257. numblks += path->p_depth;
  5258. while (cur < end) {
  5259. path = ext4_find_extent(inode, cur, path, 0);
  5260. if (IS_ERR(path))
  5261. break;
  5262. ex = path[path->p_depth].p_ext;
  5263. if (!ex)
  5264. goto cleanup;
  5265. cur = max(cur + 1, le32_to_cpu(ex->ee_block) +
  5266. ext4_ext_get_actual_len(ex));
  5267. ret = skip_hole(inode, &cur);
  5268. if (ret < 0)
  5269. break;
  5270. path2 = ext4_find_extent(inode, cur, path2, 0);
  5271. if (IS_ERR(path2))
  5272. break;
  5273. for (i = 0; i <= max(path->p_depth, path2->p_depth); i++) {
  5274. cmp1 = cmp2 = 0;
  5275. if (i <= path->p_depth)
  5276. cmp1 = path[i].p_bh ?
  5277. path[i].p_bh->b_blocknr : 0;
  5278. if (i <= path2->p_depth)
  5279. cmp2 = path2[i].p_bh ?
  5280. path2[i].p_bh->b_blocknr : 0;
  5281. if (cmp1 != cmp2 && cmp2 != 0)
  5282. numblks++;
  5283. }
  5284. }
  5285. out:
  5286. inode->i_blocks = numblks << (inode->i_sb->s_blocksize_bits - 9);
  5287. ext4_mark_inode_dirty(NULL, inode);
  5288. cleanup:
  5289. ext4_free_ext_path(path);
  5290. ext4_free_ext_path(path2);
  5291. return 0;
  5292. }
  5293. int ext4_ext_clear_bb(struct inode *inode)
  5294. {
  5295. struct ext4_ext_path *path = NULL;
  5296. struct ext4_extent *ex;
  5297. ext4_lblk_t cur = 0, end;
  5298. int j, ret = 0;
  5299. struct ext4_map_blocks map;
  5300. if (ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA))
  5301. return 0;
  5302. /* Determin the size of the file first */
  5303. path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
  5304. EXT4_EX_NOCACHE);
  5305. if (IS_ERR(path))
  5306. return PTR_ERR(path);
  5307. ex = path[path->p_depth].p_ext;
  5308. if (!ex)
  5309. goto out;
  5310. end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
  5311. cur = 0;
  5312. while (cur < end) {
  5313. map.m_lblk = cur;
  5314. map.m_len = end - cur;
  5315. ret = ext4_map_blocks(NULL, inode, &map, 0);
  5316. if (ret < 0)
  5317. break;
  5318. if (ret > 0) {
  5319. path = ext4_find_extent(inode, map.m_lblk, path, 0);
  5320. if (!IS_ERR(path)) {
  5321. for (j = 0; j < path->p_depth; j++) {
  5322. ext4_mb_mark_bb(inode->i_sb,
  5323. path[j].p_block, 1, false);
  5324. ext4_fc_record_regions(inode->i_sb, inode->i_ino,
  5325. 0, path[j].p_block, 1, 1);
  5326. }
  5327. } else {
  5328. path = NULL;
  5329. }
  5330. ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, false);
  5331. ext4_fc_record_regions(inode->i_sb, inode->i_ino,
  5332. map.m_lblk, map.m_pblk, map.m_len, 1);
  5333. }
  5334. cur = cur + map.m_len;
  5335. }
  5336. out:
  5337. ext4_free_ext_path(path);
  5338. return 0;
  5339. }