extents.c 163 KB

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