extents.c 131 KB

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