extents.c 131 KB

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