extents.c 131 KB

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