extents.c 135 KB

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