extents.c 162 KB

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