extents.c 132 KB

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