extents.c 150 KB

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