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. if (!ret)
  1825. sbi->extent_cache_misses++;
  1826. else
  1827. sbi->extent_cache_hits++;
  1828. trace_ext4_ext_in_cache(inode, block, ret);
  1829. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1830. return ret;
  1831. }
  1832. /*
  1833. * ext4_ext_in_cache()
  1834. * Checks to see if the given block is in the cache.
  1835. * If it is, the cached extent is stored in the given
  1836. * extent pointer.
  1837. *
  1838. * @inode: The files inode
  1839. * @block: The block to look for in the cache
  1840. * @ex: Pointer where the cached extent will be stored
  1841. * if it contains block
  1842. *
  1843. * Return 0 if cache is invalid; 1 if the cache is valid
  1844. */
  1845. static int
  1846. ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
  1847. struct ext4_extent *ex)
  1848. {
  1849. struct ext4_ext_cache cex;
  1850. int ret = 0;
  1851. if (ext4_ext_check_cache(inode, block, &cex)) {
  1852. ex->ee_block = cpu_to_le32(cex.ec_block);
  1853. ext4_ext_store_pblock(ex, cex.ec_start);
  1854. ex->ee_len = cpu_to_le16(cex.ec_len);
  1855. ret = 1;
  1856. }
  1857. return ret;
  1858. }
  1859. /*
  1860. * ext4_ext_rm_idx:
  1861. * removes index from the index block.
  1862. */
  1863. static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  1864. struct ext4_ext_path *path)
  1865. {
  1866. int err;
  1867. ext4_fsblk_t leaf;
  1868. /* free index block */
  1869. path--;
  1870. leaf = ext4_idx_pblock(path->p_idx);
  1871. if (unlikely(path->p_hdr->eh_entries == 0)) {
  1872. EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
  1873. return -EIO;
  1874. }
  1875. err = ext4_ext_get_access(handle, inode, path);
  1876. if (err)
  1877. return err;
  1878. if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
  1879. int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
  1880. len *= sizeof(struct ext4_extent_idx);
  1881. memmove(path->p_idx, path->p_idx + 1, len);
  1882. }
  1883. le16_add_cpu(&path->p_hdr->eh_entries, -1);
  1884. err = ext4_ext_dirty(handle, inode, path);
  1885. if (err)
  1886. return err;
  1887. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  1888. trace_ext4_ext_rm_idx(inode, leaf);
  1889. ext4_free_blocks(handle, inode, NULL, leaf, 1,
  1890. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  1891. return err;
  1892. }
  1893. /*
  1894. * ext4_ext_calc_credits_for_single_extent:
  1895. * This routine returns max. credits that needed to insert an extent
  1896. * to the extent tree.
  1897. * When pass the actual path, the caller should calculate credits
  1898. * under i_data_sem.
  1899. */
  1900. int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
  1901. struct ext4_ext_path *path)
  1902. {
  1903. if (path) {
  1904. int depth = ext_depth(inode);
  1905. int ret = 0;
  1906. /* probably there is space in leaf? */
  1907. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  1908. < le16_to_cpu(path[depth].p_hdr->eh_max)) {
  1909. /*
  1910. * There are some space in the leaf tree, no
  1911. * need to account for leaf block credit
  1912. *
  1913. * bitmaps and block group descriptor blocks
  1914. * and other metadata blocks still need to be
  1915. * accounted.
  1916. */
  1917. /* 1 bitmap, 1 block group descriptor */
  1918. ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
  1919. return ret;
  1920. }
  1921. }
  1922. return ext4_chunk_trans_blocks(inode, nrblocks);
  1923. }
  1924. /*
  1925. * How many index/leaf blocks need to change/allocate to modify nrblocks?
  1926. *
  1927. * if nrblocks are fit in a single extent (chunk flag is 1), then
  1928. * in the worse case, each tree level index/leaf need to be changed
  1929. * if the tree split due to insert a new extent, then the old tree
  1930. * index/leaf need to be updated too
  1931. *
  1932. * If the nrblocks are discontiguous, they could cause
  1933. * the whole tree split more than once, but this is really rare.
  1934. */
  1935. int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  1936. {
  1937. int index;
  1938. int depth = ext_depth(inode);
  1939. if (chunk)
  1940. index = depth * 2;
  1941. else
  1942. index = depth * 3;
  1943. return index;
  1944. }
  1945. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  1946. struct ext4_extent *ex,
  1947. ext4_fsblk_t *partial_cluster,
  1948. ext4_lblk_t from, ext4_lblk_t to)
  1949. {
  1950. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1951. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  1952. ext4_fsblk_t pblk;
  1953. int flags = EXT4_FREE_BLOCKS_FORGET;
  1954. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1955. flags |= EXT4_FREE_BLOCKS_METADATA;
  1956. /*
  1957. * For bigalloc file systems, we never free a partial cluster
  1958. * at the beginning of the extent. Instead, we make a note
  1959. * that we tried freeing the cluster, and check to see if we
  1960. * need to free it on a subsequent call to ext4_remove_blocks,
  1961. * or at the end of the ext4_truncate() operation.
  1962. */
  1963. flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
  1964. trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
  1965. /*
  1966. * If we have a partial cluster, and it's different from the
  1967. * cluster of the last block, we need to explicitly free the
  1968. * partial cluster here.
  1969. */
  1970. pblk = ext4_ext_pblock(ex) + ee_len - 1;
  1971. if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
  1972. ext4_free_blocks(handle, inode, NULL,
  1973. EXT4_C2B(sbi, *partial_cluster),
  1974. sbi->s_cluster_ratio, flags);
  1975. *partial_cluster = 0;
  1976. }
  1977. #ifdef EXTENTS_STATS
  1978. {
  1979. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1980. spin_lock(&sbi->s_ext_stats_lock);
  1981. sbi->s_ext_blocks += ee_len;
  1982. sbi->s_ext_extents++;
  1983. if (ee_len < sbi->s_ext_min)
  1984. sbi->s_ext_min = ee_len;
  1985. if (ee_len > sbi->s_ext_max)
  1986. sbi->s_ext_max = ee_len;
  1987. if (ext_depth(inode) > sbi->s_depth_max)
  1988. sbi->s_depth_max = ext_depth(inode);
  1989. spin_unlock(&sbi->s_ext_stats_lock);
  1990. }
  1991. #endif
  1992. if (from >= le32_to_cpu(ex->ee_block)
  1993. && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1994. /* tail removal */
  1995. ext4_lblk_t num;
  1996. num = le32_to_cpu(ex->ee_block) + ee_len - from;
  1997. pblk = ext4_ext_pblock(ex) + ee_len - num;
  1998. ext_debug("free last %u blocks starting %llu\n", num, pblk);
  1999. ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
  2000. /*
  2001. * If the block range to be freed didn't start at the
  2002. * beginning of a cluster, and we removed the entire
  2003. * extent, save the partial cluster here, since we
  2004. * might need to delete if we determine that the
  2005. * truncate operation has removed all of the blocks in
  2006. * the cluster.
  2007. */
  2008. if (pblk & (sbi->s_cluster_ratio - 1) &&
  2009. (ee_len == num))
  2010. *partial_cluster = EXT4_B2C(sbi, pblk);
  2011. else
  2012. *partial_cluster = 0;
  2013. } else if (from == le32_to_cpu(ex->ee_block)
  2014. && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
  2015. /* head removal */
  2016. ext4_lblk_t num;
  2017. ext4_fsblk_t start;
  2018. num = to - from;
  2019. start = ext4_ext_pblock(ex);
  2020. ext_debug("free first %u blocks starting %llu\n", num, start);
  2021. ext4_free_blocks(handle, inode, NULL, start, num, flags);
  2022. } else {
  2023. printk(KERN_INFO "strange request: removal(2) "
  2024. "%u-%u from %u:%u\n",
  2025. from, to, le32_to_cpu(ex->ee_block), ee_len);
  2026. }
  2027. return 0;
  2028. }
  2029. /*
  2030. * ext4_ext_rm_leaf() Removes the extents associated with the
  2031. * blocks appearing between "start" and "end", and splits the extents
  2032. * if "start" and "end" appear in the same extent
  2033. *
  2034. * @handle: The journal handle
  2035. * @inode: The files inode
  2036. * @path: The path to the leaf
  2037. * @start: The first block to remove
  2038. * @end: The last block to remove
  2039. */
  2040. static int
  2041. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  2042. struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
  2043. ext4_lblk_t start, ext4_lblk_t end)
  2044. {
  2045. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2046. int err = 0, correct_index = 0;
  2047. int depth = ext_depth(inode), credits;
  2048. struct ext4_extent_header *eh;
  2049. ext4_lblk_t a, b;
  2050. unsigned num;
  2051. ext4_lblk_t ex_ee_block;
  2052. unsigned short ex_ee_len;
  2053. unsigned uninitialized = 0;
  2054. struct ext4_extent *ex;
  2055. /* the header must be checked already in ext4_ext_remove_space() */
  2056. ext_debug("truncate since %u in leaf to %u\n", start, end);
  2057. if (!path[depth].p_hdr)
  2058. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  2059. eh = path[depth].p_hdr;
  2060. if (unlikely(path[depth].p_hdr == NULL)) {
  2061. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  2062. return -EIO;
  2063. }
  2064. /* find where to start removing */
  2065. ex = EXT_LAST_EXTENT(eh);
  2066. ex_ee_block = le32_to_cpu(ex->ee_block);
  2067. ex_ee_len = ext4_ext_get_actual_len(ex);
  2068. trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
  2069. while (ex >= EXT_FIRST_EXTENT(eh) &&
  2070. ex_ee_block + ex_ee_len > start) {
  2071. if (ext4_ext_is_uninitialized(ex))
  2072. uninitialized = 1;
  2073. else
  2074. uninitialized = 0;
  2075. ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
  2076. uninitialized, ex_ee_len);
  2077. path[depth].p_ext = ex;
  2078. a = ex_ee_block > start ? ex_ee_block : start;
  2079. b = ex_ee_block+ex_ee_len - 1 < end ?
  2080. ex_ee_block+ex_ee_len - 1 : end;
  2081. ext_debug(" border %u:%u\n", a, b);
  2082. /* If this extent is beyond the end of the hole, skip it */
  2083. if (end < ex_ee_block) {
  2084. ex--;
  2085. ex_ee_block = le32_to_cpu(ex->ee_block);
  2086. ex_ee_len = ext4_ext_get_actual_len(ex);
  2087. continue;
  2088. } else if (b != ex_ee_block + ex_ee_len - 1) {
  2089. EXT4_ERROR_INODE(inode,
  2090. "can not handle truncate %u:%u "
  2091. "on extent %u:%u",
  2092. start, end, ex_ee_block,
  2093. ex_ee_block + ex_ee_len - 1);
  2094. err = -EIO;
  2095. goto out;
  2096. } else if (a != ex_ee_block) {
  2097. /* remove tail of the extent */
  2098. num = a - ex_ee_block;
  2099. } else {
  2100. /* remove whole extent: excellent! */
  2101. num = 0;
  2102. }
  2103. /*
  2104. * 3 for leaf, sb, and inode plus 2 (bmap and group
  2105. * descriptor) for each block group; assume two block
  2106. * groups plus ex_ee_len/blocks_per_block_group for
  2107. * the worst case
  2108. */
  2109. credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
  2110. if (ex == EXT_FIRST_EXTENT(eh)) {
  2111. correct_index = 1;
  2112. credits += (ext_depth(inode)) + 1;
  2113. }
  2114. credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
  2115. err = ext4_ext_truncate_extend_restart(handle, inode, credits);
  2116. if (err)
  2117. goto out;
  2118. err = ext4_ext_get_access(handle, inode, path + depth);
  2119. if (err)
  2120. goto out;
  2121. err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
  2122. a, b);
  2123. if (err)
  2124. goto out;
  2125. if (num == 0)
  2126. /* this extent is removed; mark slot entirely unused */
  2127. ext4_ext_store_pblock(ex, 0);
  2128. ex->ee_len = cpu_to_le16(num);
  2129. /*
  2130. * Do not mark uninitialized if all the blocks in the
  2131. * extent have been removed.
  2132. */
  2133. if (uninitialized && num)
  2134. ext4_ext_mark_uninitialized(ex);
  2135. /*
  2136. * If the extent was completely released,
  2137. * we need to remove it from the leaf
  2138. */
  2139. if (num == 0) {
  2140. if (end != EXT_MAX_BLOCKS - 1) {
  2141. /*
  2142. * For hole punching, we need to scoot all the
  2143. * extents up when an extent is removed so that
  2144. * we dont have blank extents in the middle
  2145. */
  2146. memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
  2147. sizeof(struct ext4_extent));
  2148. /* Now get rid of the one at the end */
  2149. memset(EXT_LAST_EXTENT(eh), 0,
  2150. sizeof(struct ext4_extent));
  2151. }
  2152. le16_add_cpu(&eh->eh_entries, -1);
  2153. } else
  2154. *partial_cluster = 0;
  2155. err = ext4_ext_dirty(handle, inode, path + depth);
  2156. if (err)
  2157. goto out;
  2158. ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
  2159. ext4_ext_pblock(ex));
  2160. ex--;
  2161. ex_ee_block = le32_to_cpu(ex->ee_block);
  2162. ex_ee_len = ext4_ext_get_actual_len(ex);
  2163. }
  2164. if (correct_index && eh->eh_entries)
  2165. err = ext4_ext_correct_indexes(handle, inode, path);
  2166. /*
  2167. * If there is still a entry in the leaf node, check to see if
  2168. * it references the partial cluster. This is the only place
  2169. * where it could; if it doesn't, we can free the cluster.
  2170. */
  2171. if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
  2172. (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
  2173. *partial_cluster)) {
  2174. int flags = EXT4_FREE_BLOCKS_FORGET;
  2175. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  2176. flags |= EXT4_FREE_BLOCKS_METADATA;
  2177. ext4_free_blocks(handle, inode, NULL,
  2178. EXT4_C2B(sbi, *partial_cluster),
  2179. sbi->s_cluster_ratio, flags);
  2180. *partial_cluster = 0;
  2181. }
  2182. /* if this leaf is free, then we should
  2183. * remove it from index block above */
  2184. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  2185. err = ext4_ext_rm_idx(handle, inode, path + depth);
  2186. out:
  2187. return err;
  2188. }
  2189. /*
  2190. * ext4_ext_more_to_rm:
  2191. * returns 1 if current index has to be freed (even partial)
  2192. */
  2193. static int
  2194. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  2195. {
  2196. BUG_ON(path->p_idx == NULL);
  2197. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  2198. return 0;
  2199. /*
  2200. * if truncate on deeper level happened, it wasn't partial,
  2201. * so we have to consider current index for truncation
  2202. */
  2203. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  2204. return 0;
  2205. return 1;
  2206. }
  2207. static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
  2208. ext4_lblk_t end)
  2209. {
  2210. struct super_block *sb = inode->i_sb;
  2211. int depth = ext_depth(inode);
  2212. struct ext4_ext_path *path;
  2213. ext4_fsblk_t partial_cluster = 0;
  2214. handle_t *handle;
  2215. int i, err;
  2216. ext_debug("truncate since %u to %u\n", start, end);
  2217. /* probably first extent we're gonna free will be last in block */
  2218. handle = ext4_journal_start(inode, depth + 1);
  2219. if (IS_ERR(handle))
  2220. return PTR_ERR(handle);
  2221. again:
  2222. ext4_ext_invalidate_cache(inode);
  2223. trace_ext4_ext_remove_space(inode, start, depth);
  2224. /*
  2225. * Check if we are removing extents inside the extent tree. If that
  2226. * is the case, we are going to punch a hole inside the extent tree
  2227. * so we have to check whether we need to split the extent covering
  2228. * the last block to remove so we can easily remove the part of it
  2229. * in ext4_ext_rm_leaf().
  2230. */
  2231. if (end < EXT_MAX_BLOCKS - 1) {
  2232. struct ext4_extent *ex;
  2233. ext4_lblk_t ee_block;
  2234. /* find extent for this block */
  2235. path = ext4_ext_find_extent(inode, end, NULL);
  2236. if (IS_ERR(path)) {
  2237. ext4_journal_stop(handle);
  2238. return PTR_ERR(path);
  2239. }
  2240. depth = ext_depth(inode);
  2241. ex = path[depth].p_ext;
  2242. if (!ex)
  2243. goto cont;
  2244. ee_block = le32_to_cpu(ex->ee_block);
  2245. /*
  2246. * See if the last block is inside the extent, if so split
  2247. * the extent at 'end' block so we can easily remove the
  2248. * tail of the first part of the split extent in
  2249. * ext4_ext_rm_leaf().
  2250. */
  2251. if (end >= ee_block &&
  2252. end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
  2253. int split_flag = 0;
  2254. if (ext4_ext_is_uninitialized(ex))
  2255. split_flag = EXT4_EXT_MARK_UNINIT1 |
  2256. EXT4_EXT_MARK_UNINIT2;
  2257. /*
  2258. * Split the extent in two so that 'end' is the last
  2259. * block in the first new extent
  2260. */
  2261. err = ext4_split_extent_at(handle, inode, path,
  2262. end + 1, split_flag,
  2263. EXT4_GET_BLOCKS_PRE_IO |
  2264. EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
  2265. if (err < 0)
  2266. goto out;
  2267. }
  2268. ext4_ext_drop_refs(path);
  2269. kfree(path);
  2270. }
  2271. cont:
  2272. /*
  2273. * We start scanning from right side, freeing all the blocks
  2274. * after i_size and walking into the tree depth-wise.
  2275. */
  2276. depth = ext_depth(inode);
  2277. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
  2278. if (path == NULL) {
  2279. ext4_journal_stop(handle);
  2280. return -ENOMEM;
  2281. }
  2282. path[0].p_depth = depth;
  2283. path[0].p_hdr = ext_inode_hdr(inode);
  2284. if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
  2285. err = -EIO;
  2286. goto out;
  2287. }
  2288. i = err = 0;
  2289. while (i >= 0 && err == 0) {
  2290. if (i == depth) {
  2291. /* this is leaf block */
  2292. err = ext4_ext_rm_leaf(handle, inode, path,
  2293. &partial_cluster, start,
  2294. end);
  2295. /* root level has p_bh == NULL, brelse() eats this */
  2296. brelse(path[i].p_bh);
  2297. path[i].p_bh = NULL;
  2298. i--;
  2299. continue;
  2300. }
  2301. /* this is index block */
  2302. if (!path[i].p_hdr) {
  2303. ext_debug("initialize header\n");
  2304. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  2305. }
  2306. if (!path[i].p_idx) {
  2307. /* this level hasn't been touched yet */
  2308. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  2309. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  2310. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  2311. path[i].p_hdr,
  2312. le16_to_cpu(path[i].p_hdr->eh_entries));
  2313. } else {
  2314. /* we were already here, see at next index */
  2315. path[i].p_idx--;
  2316. }
  2317. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  2318. i, EXT_FIRST_INDEX(path[i].p_hdr),
  2319. path[i].p_idx);
  2320. if (ext4_ext_more_to_rm(path + i)) {
  2321. struct buffer_head *bh;
  2322. /* go to the next level */
  2323. ext_debug("move to level %d (block %llu)\n",
  2324. i + 1, ext4_idx_pblock(path[i].p_idx));
  2325. memset(path + i + 1, 0, sizeof(*path));
  2326. bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
  2327. if (!bh) {
  2328. /* should we reset i_size? */
  2329. err = -EIO;
  2330. break;
  2331. }
  2332. if (WARN_ON(i + 1 > depth)) {
  2333. err = -EIO;
  2334. break;
  2335. }
  2336. if (ext4_ext_check(inode, ext_block_hdr(bh),
  2337. depth - i - 1)) {
  2338. err = -EIO;
  2339. break;
  2340. }
  2341. path[i + 1].p_bh = bh;
  2342. /* save actual number of indexes since this
  2343. * number is changed at the next iteration */
  2344. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  2345. i++;
  2346. } else {
  2347. /* we finished processing this index, go up */
  2348. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  2349. /* index is empty, remove it;
  2350. * handle must be already prepared by the
  2351. * truncatei_leaf() */
  2352. err = ext4_ext_rm_idx(handle, inode, path + i);
  2353. }
  2354. /* root level has p_bh == NULL, brelse() eats this */
  2355. brelse(path[i].p_bh);
  2356. path[i].p_bh = NULL;
  2357. i--;
  2358. ext_debug("return to level %d\n", i);
  2359. }
  2360. }
  2361. trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
  2362. path->p_hdr->eh_entries);
  2363. /* If we still have something in the partial cluster and we have removed
  2364. * even the first extent, then we should free the blocks in the partial
  2365. * cluster as well. */
  2366. if (partial_cluster && path->p_hdr->eh_entries == 0) {
  2367. int flags = EXT4_FREE_BLOCKS_FORGET;
  2368. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  2369. flags |= EXT4_FREE_BLOCKS_METADATA;
  2370. ext4_free_blocks(handle, inode, NULL,
  2371. EXT4_C2B(EXT4_SB(sb), partial_cluster),
  2372. EXT4_SB(sb)->s_cluster_ratio, flags);
  2373. partial_cluster = 0;
  2374. }
  2375. /* TODO: flexible tree reduction should be here */
  2376. if (path->p_hdr->eh_entries == 0) {
  2377. /*
  2378. * truncate to zero freed all the tree,
  2379. * so we need to correct eh_depth
  2380. */
  2381. err = ext4_ext_get_access(handle, inode, path);
  2382. if (err == 0) {
  2383. ext_inode_hdr(inode)->eh_depth = 0;
  2384. ext_inode_hdr(inode)->eh_max =
  2385. cpu_to_le16(ext4_ext_space_root(inode, 0));
  2386. err = ext4_ext_dirty(handle, inode, path);
  2387. }
  2388. }
  2389. out:
  2390. ext4_ext_drop_refs(path);
  2391. kfree(path);
  2392. if (err == -EAGAIN)
  2393. goto again;
  2394. ext4_journal_stop(handle);
  2395. return err;
  2396. }
  2397. /*
  2398. * called at mount time
  2399. */
  2400. void ext4_ext_init(struct super_block *sb)
  2401. {
  2402. /*
  2403. * possible initialization would be here
  2404. */
  2405. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  2406. #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
  2407. printk(KERN_INFO "EXT4-fs: file extents enabled");
  2408. #ifdef AGGRESSIVE_TEST
  2409. printk(", aggressive tests");
  2410. #endif
  2411. #ifdef CHECK_BINSEARCH
  2412. printk(", check binsearch");
  2413. #endif
  2414. #ifdef EXTENTS_STATS
  2415. printk(", stats");
  2416. #endif
  2417. printk("\n");
  2418. #endif
  2419. #ifdef EXTENTS_STATS
  2420. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  2421. EXT4_SB(sb)->s_ext_min = 1 << 30;
  2422. EXT4_SB(sb)->s_ext_max = 0;
  2423. #endif
  2424. }
  2425. }
  2426. /*
  2427. * called at umount time
  2428. */
  2429. void ext4_ext_release(struct super_block *sb)
  2430. {
  2431. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
  2432. return;
  2433. #ifdef EXTENTS_STATS
  2434. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  2435. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2436. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  2437. sbi->s_ext_blocks, sbi->s_ext_extents,
  2438. sbi->s_ext_blocks / sbi->s_ext_extents);
  2439. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  2440. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  2441. }
  2442. #endif
  2443. }
  2444. /* FIXME!! we need to try to merge to left or right after zero-out */
  2445. static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
  2446. {
  2447. ext4_fsblk_t ee_pblock;
  2448. unsigned int ee_len;
  2449. int ret;
  2450. ee_len = ext4_ext_get_actual_len(ex);
  2451. ee_pblock = ext4_ext_pblock(ex);
  2452. ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
  2453. if (ret > 0)
  2454. ret = 0;
  2455. return ret;
  2456. }
  2457. /*
  2458. * ext4_split_extent_at() splits an extent at given block.
  2459. *
  2460. * @handle: the journal handle
  2461. * @inode: the file inode
  2462. * @path: the path to the extent
  2463. * @split: the logical block where the extent is splitted.
  2464. * @split_flags: indicates if the extent could be zeroout if split fails, and
  2465. * the states(init or uninit) of new extents.
  2466. * @flags: flags used to insert new extent to extent tree.
  2467. *
  2468. *
  2469. * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
  2470. * of which are deterimined by split_flag.
  2471. *
  2472. * There are two cases:
  2473. * a> the extent are splitted into two extent.
  2474. * b> split is not needed, and just mark the extent.
  2475. *
  2476. * return 0 on success.
  2477. */
  2478. static int ext4_split_extent_at(handle_t *handle,
  2479. struct inode *inode,
  2480. struct ext4_ext_path *path,
  2481. ext4_lblk_t split,
  2482. int split_flag,
  2483. int flags)
  2484. {
  2485. ext4_fsblk_t newblock;
  2486. ext4_lblk_t ee_block;
  2487. struct ext4_extent *ex, newex, orig_ex;
  2488. struct ext4_extent *ex2 = NULL;
  2489. unsigned int ee_len, depth;
  2490. int err = 0;
  2491. ext_debug("ext4_split_extents_at: inode %lu, logical"
  2492. "block %llu\n", inode->i_ino, (unsigned long long)split);
  2493. ext4_ext_show_leaf(inode, path);
  2494. depth = ext_depth(inode);
  2495. ex = path[depth].p_ext;
  2496. ee_block = le32_to_cpu(ex->ee_block);
  2497. ee_len = ext4_ext_get_actual_len(ex);
  2498. newblock = split - ee_block + ext4_ext_pblock(ex);
  2499. BUG_ON(split < ee_block || split >= (ee_block + ee_len));
  2500. err = ext4_ext_get_access(handle, inode, path + depth);
  2501. if (err)
  2502. goto out;
  2503. if (split == ee_block) {
  2504. /*
  2505. * case b: block @split is the block that the extent begins with
  2506. * then we just change the state of the extent, and splitting
  2507. * is not needed.
  2508. */
  2509. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2510. ext4_ext_mark_uninitialized(ex);
  2511. else
  2512. ext4_ext_mark_initialized(ex);
  2513. if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
  2514. ext4_ext_try_to_merge(inode, path, ex);
  2515. err = ext4_ext_dirty(handle, inode, path + depth);
  2516. goto out;
  2517. }
  2518. /* case a */
  2519. memcpy(&orig_ex, ex, sizeof(orig_ex));
  2520. ex->ee_len = cpu_to_le16(split - ee_block);
  2521. if (split_flag & EXT4_EXT_MARK_UNINIT1)
  2522. ext4_ext_mark_uninitialized(ex);
  2523. /*
  2524. * path may lead to new leaf, not to original leaf any more
  2525. * after ext4_ext_insert_extent() returns,
  2526. */
  2527. err = ext4_ext_dirty(handle, inode, path + depth);
  2528. if (err)
  2529. goto fix_extent_len;
  2530. ex2 = &newex;
  2531. ex2->ee_block = cpu_to_le32(split);
  2532. ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
  2533. ext4_ext_store_pblock(ex2, newblock);
  2534. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2535. ext4_ext_mark_uninitialized(ex2);
  2536. err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
  2537. if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2538. err = ext4_ext_zeroout(inode, &orig_ex);
  2539. if (err)
  2540. goto fix_extent_len;
  2541. /* update the extent length and mark as initialized */
  2542. ex->ee_len = cpu_to_le32(ee_len);
  2543. ext4_ext_try_to_merge(inode, path, ex);
  2544. err = ext4_ext_dirty(handle, inode, path + depth);
  2545. goto out;
  2546. } else if (err)
  2547. goto fix_extent_len;
  2548. out:
  2549. ext4_ext_show_leaf(inode, path);
  2550. return err;
  2551. fix_extent_len:
  2552. ex->ee_len = orig_ex.ee_len;
  2553. ext4_ext_dirty(handle, inode, path + depth);
  2554. return err;
  2555. }
  2556. /*
  2557. * ext4_split_extents() splits an extent and mark extent which is covered
  2558. * by @map as split_flags indicates
  2559. *
  2560. * It may result in splitting the extent into multiple extents (upto three)
  2561. * There are three possibilities:
  2562. * a> There is no split required
  2563. * b> Splits in two extents: Split is happening at either end of the extent
  2564. * c> Splits in three extents: Somone is splitting in middle of the extent
  2565. *
  2566. */
  2567. static int ext4_split_extent(handle_t *handle,
  2568. struct inode *inode,
  2569. struct ext4_ext_path *path,
  2570. struct ext4_map_blocks *map,
  2571. int split_flag,
  2572. int flags)
  2573. {
  2574. ext4_lblk_t ee_block;
  2575. struct ext4_extent *ex;
  2576. unsigned int ee_len, depth;
  2577. int err = 0;
  2578. int uninitialized;
  2579. int split_flag1, flags1;
  2580. depth = ext_depth(inode);
  2581. ex = path[depth].p_ext;
  2582. ee_block = le32_to_cpu(ex->ee_block);
  2583. ee_len = ext4_ext_get_actual_len(ex);
  2584. uninitialized = ext4_ext_is_uninitialized(ex);
  2585. if (map->m_lblk + map->m_len < ee_block + ee_len) {
  2586. split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
  2587. EXT4_EXT_MAY_ZEROOUT : 0;
  2588. flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
  2589. if (uninitialized)
  2590. split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
  2591. EXT4_EXT_MARK_UNINIT2;
  2592. err = ext4_split_extent_at(handle, inode, path,
  2593. map->m_lblk + map->m_len, split_flag1, flags1);
  2594. if (err)
  2595. goto out;
  2596. }
  2597. ext4_ext_drop_refs(path);
  2598. path = ext4_ext_find_extent(inode, map->m_lblk, path);
  2599. if (IS_ERR(path))
  2600. return PTR_ERR(path);
  2601. if (map->m_lblk >= ee_block) {
  2602. split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
  2603. EXT4_EXT_MAY_ZEROOUT : 0;
  2604. if (uninitialized)
  2605. split_flag1 |= EXT4_EXT_MARK_UNINIT1;
  2606. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2607. split_flag1 |= EXT4_EXT_MARK_UNINIT2;
  2608. err = ext4_split_extent_at(handle, inode, path,
  2609. map->m_lblk, split_flag1, flags);
  2610. if (err)
  2611. goto out;
  2612. }
  2613. ext4_ext_show_leaf(inode, path);
  2614. out:
  2615. return err ? err : map->m_len;
  2616. }
  2617. #define EXT4_EXT_ZERO_LEN 7
  2618. /*
  2619. * This function is called by ext4_ext_map_blocks() if someone tries to write
  2620. * to an uninitialized extent. It may result in splitting the uninitialized
  2621. * extent into multiple extents (up to three - one initialized and two
  2622. * uninitialized).
  2623. * There are three possibilities:
  2624. * a> There is no split required: Entire extent should be initialized
  2625. * b> Splits in two extents: Write is happening at either end of the extent
  2626. * c> Splits in three extents: Somone is writing in middle of the extent
  2627. *
  2628. * Pre-conditions:
  2629. * - The extent pointed to by 'path' is uninitialized.
  2630. * - The extent pointed to by 'path' contains a superset
  2631. * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
  2632. *
  2633. * Post-conditions on success:
  2634. * - the returned value is the number of blocks beyond map->l_lblk
  2635. * that are allocated and initialized.
  2636. * It is guaranteed to be >= map->m_len.
  2637. */
  2638. static int ext4_ext_convert_to_initialized(handle_t *handle,
  2639. struct inode *inode,
  2640. struct ext4_map_blocks *map,
  2641. struct ext4_ext_path *path)
  2642. {
  2643. struct ext4_extent_header *eh;
  2644. struct ext4_map_blocks split_map;
  2645. struct ext4_extent zero_ex;
  2646. struct ext4_extent *ex;
  2647. ext4_lblk_t ee_block, eof_block;
  2648. unsigned int ee_len, depth;
  2649. int allocated;
  2650. int err = 0;
  2651. int split_flag = 0;
  2652. ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
  2653. "block %llu, max_blocks %u\n", inode->i_ino,
  2654. (unsigned long long)map->m_lblk, map->m_len);
  2655. eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  2656. inode->i_sb->s_blocksize_bits;
  2657. if (eof_block < map->m_lblk + map->m_len)
  2658. eof_block = map->m_lblk + map->m_len;
  2659. depth = ext_depth(inode);
  2660. eh = path[depth].p_hdr;
  2661. ex = path[depth].p_ext;
  2662. ee_block = le32_to_cpu(ex->ee_block);
  2663. ee_len = ext4_ext_get_actual_len(ex);
  2664. allocated = ee_len - (map->m_lblk - ee_block);
  2665. trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
  2666. /* Pre-conditions */
  2667. BUG_ON(!ext4_ext_is_uninitialized(ex));
  2668. BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
  2669. /*
  2670. * Attempt to transfer newly initialized blocks from the currently
  2671. * uninitialized extent to its left neighbor. This is much cheaper
  2672. * than an insertion followed by a merge as those involve costly
  2673. * memmove() calls. This is the common case in steady state for
  2674. * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
  2675. * writes.
  2676. *
  2677. * Limitations of the current logic:
  2678. * - L1: we only deal with writes at the start of the extent.
  2679. * The approach could be extended to writes at the end
  2680. * of the extent but this scenario was deemed less common.
  2681. * - L2: we do not deal with writes covering the whole extent.
  2682. * This would require removing the extent if the transfer
  2683. * is possible.
  2684. * - L3: we only attempt to merge with an extent stored in the
  2685. * same extent tree node.
  2686. */
  2687. if ((map->m_lblk == ee_block) && /*L1*/
  2688. (map->m_len < ee_len) && /*L2*/
  2689. (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
  2690. struct ext4_extent *prev_ex;
  2691. ext4_lblk_t prev_lblk;
  2692. ext4_fsblk_t prev_pblk, ee_pblk;
  2693. unsigned int prev_len, write_len;
  2694. prev_ex = ex - 1;
  2695. prev_lblk = le32_to_cpu(prev_ex->ee_block);
  2696. prev_len = ext4_ext_get_actual_len(prev_ex);
  2697. prev_pblk = ext4_ext_pblock(prev_ex);
  2698. ee_pblk = ext4_ext_pblock(ex);
  2699. write_len = map->m_len;
  2700. /*
  2701. * A transfer of blocks from 'ex' to 'prev_ex' is allowed
  2702. * upon those conditions:
  2703. * - C1: prev_ex is initialized,
  2704. * - C2: prev_ex is logically abutting ex,
  2705. * - C3: prev_ex is physically abutting ex,
  2706. * - C4: prev_ex can receive the additional blocks without
  2707. * overflowing the (initialized) length limit.
  2708. */
  2709. if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
  2710. ((prev_lblk + prev_len) == ee_block) && /*C2*/
  2711. ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
  2712. (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
  2713. err = ext4_ext_get_access(handle, inode, path + depth);
  2714. if (err)
  2715. goto out;
  2716. trace_ext4_ext_convert_to_initialized_fastpath(inode,
  2717. map, ex, prev_ex);
  2718. /* Shift the start of ex by 'write_len' blocks */
  2719. ex->ee_block = cpu_to_le32(ee_block + write_len);
  2720. ext4_ext_store_pblock(ex, ee_pblk + write_len);
  2721. ex->ee_len = cpu_to_le16(ee_len - write_len);
  2722. ext4_ext_mark_uninitialized(ex); /* Restore the flag */
  2723. /* Extend prev_ex by 'write_len' blocks */
  2724. prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
  2725. /* Mark the block containing both extents as dirty */
  2726. ext4_ext_dirty(handle, inode, path + depth);
  2727. /* Update path to point to the right extent */
  2728. path[depth].p_ext = prev_ex;
  2729. /* Result: number of initialized blocks past m_lblk */
  2730. allocated = write_len;
  2731. goto out;
  2732. }
  2733. }
  2734. WARN_ON(map->m_lblk < ee_block);
  2735. /*
  2736. * It is safe to convert extent to initialized via explicit
  2737. * zeroout only if extent is fully insde i_size or new_size.
  2738. */
  2739. split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
  2740. /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
  2741. if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
  2742. (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2743. err = ext4_ext_zeroout(inode, ex);
  2744. if (err)
  2745. goto out;
  2746. err = ext4_ext_get_access(handle, inode, path + depth);
  2747. if (err)
  2748. goto out;
  2749. ext4_ext_mark_initialized(ex);
  2750. ext4_ext_try_to_merge(inode, path, ex);
  2751. err = ext4_ext_dirty(handle, inode, path + depth);
  2752. goto out;
  2753. }
  2754. /*
  2755. * four cases:
  2756. * 1. split the extent into three extents.
  2757. * 2. split the extent into two extents, zeroout the first half.
  2758. * 3. split the extent into two extents, zeroout the second half.
  2759. * 4. split the extent into two extents with out zeroout.
  2760. */
  2761. split_map.m_lblk = map->m_lblk;
  2762. split_map.m_len = map->m_len;
  2763. if (allocated > map->m_len) {
  2764. if (allocated <= EXT4_EXT_ZERO_LEN &&
  2765. (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2766. /* case 3 */
  2767. zero_ex.ee_block =
  2768. cpu_to_le32(map->m_lblk);
  2769. zero_ex.ee_len = cpu_to_le16(allocated);
  2770. ext4_ext_store_pblock(&zero_ex,
  2771. ext4_ext_pblock(ex) + map->m_lblk - ee_block);
  2772. err = ext4_ext_zeroout(inode, &zero_ex);
  2773. if (err)
  2774. goto out;
  2775. split_map.m_lblk = map->m_lblk;
  2776. split_map.m_len = allocated;
  2777. } else if ((map->m_lblk - ee_block + map->m_len <
  2778. EXT4_EXT_ZERO_LEN) &&
  2779. (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2780. /* case 2 */
  2781. if (map->m_lblk != ee_block) {
  2782. zero_ex.ee_block = ex->ee_block;
  2783. zero_ex.ee_len = cpu_to_le16(map->m_lblk -
  2784. ee_block);
  2785. ext4_ext_store_pblock(&zero_ex,
  2786. ext4_ext_pblock(ex));
  2787. err = ext4_ext_zeroout(inode, &zero_ex);
  2788. if (err)
  2789. goto out;
  2790. }
  2791. split_map.m_lblk = ee_block;
  2792. split_map.m_len = map->m_lblk - ee_block + map->m_len;
  2793. allocated = map->m_len;
  2794. }
  2795. }
  2796. allocated = ext4_split_extent(handle, inode, path,
  2797. &split_map, split_flag, 0);
  2798. if (allocated < 0)
  2799. err = allocated;
  2800. out:
  2801. return err ? err : allocated;
  2802. }
  2803. /*
  2804. * This function is called by ext4_ext_map_blocks() from
  2805. * ext4_get_blocks_dio_write() when DIO to write
  2806. * to an uninitialized extent.
  2807. *
  2808. * Writing to an uninitialized extent may result in splitting the uninitialized
  2809. * extent into multiple /initialized uninitialized extents (up to three)
  2810. * There are three possibilities:
  2811. * a> There is no split required: Entire extent should be uninitialized
  2812. * b> Splits in two extents: Write is happening at either end of the extent
  2813. * c> Splits in three extents: Somone is writing in middle of the extent
  2814. *
  2815. * One of more index blocks maybe needed if the extent tree grow after
  2816. * the uninitialized extent split. To prevent ENOSPC occur at the IO
  2817. * complete, we need to split the uninitialized extent before DIO submit
  2818. * the IO. The uninitialized extent called at this time will be split
  2819. * into three uninitialized extent(at most). After IO complete, the part
  2820. * being filled will be convert to initialized by the end_io callback function
  2821. * via ext4_convert_unwritten_extents().
  2822. *
  2823. * Returns the size of uninitialized extent to be written on success.
  2824. */
  2825. static int ext4_split_unwritten_extents(handle_t *handle,
  2826. struct inode *inode,
  2827. struct ext4_map_blocks *map,
  2828. struct ext4_ext_path *path,
  2829. int flags)
  2830. {
  2831. ext4_lblk_t eof_block;
  2832. ext4_lblk_t ee_block;
  2833. struct ext4_extent *ex;
  2834. unsigned int ee_len;
  2835. int split_flag = 0, depth;
  2836. ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
  2837. "block %llu, max_blocks %u\n", inode->i_ino,
  2838. (unsigned long long)map->m_lblk, map->m_len);
  2839. eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  2840. inode->i_sb->s_blocksize_bits;
  2841. if (eof_block < map->m_lblk + map->m_len)
  2842. eof_block = map->m_lblk + map->m_len;
  2843. /*
  2844. * It is safe to convert extent to initialized via explicit
  2845. * zeroout only if extent is fully insde i_size or new_size.
  2846. */
  2847. depth = ext_depth(inode);
  2848. ex = path[depth].p_ext;
  2849. ee_block = le32_to_cpu(ex->ee_block);
  2850. ee_len = ext4_ext_get_actual_len(ex);
  2851. split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
  2852. split_flag |= EXT4_EXT_MARK_UNINIT2;
  2853. flags |= EXT4_GET_BLOCKS_PRE_IO;
  2854. return ext4_split_extent(handle, inode, path, map, split_flag, flags);
  2855. }
  2856. static int ext4_convert_unwritten_extents_endio(handle_t *handle,
  2857. struct inode *inode,
  2858. struct ext4_ext_path *path)
  2859. {
  2860. struct ext4_extent *ex;
  2861. int depth;
  2862. int err = 0;
  2863. depth = ext_depth(inode);
  2864. ex = path[depth].p_ext;
  2865. ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
  2866. "block %llu, max_blocks %u\n", inode->i_ino,
  2867. (unsigned long long)le32_to_cpu(ex->ee_block),
  2868. ext4_ext_get_actual_len(ex));
  2869. err = ext4_ext_get_access(handle, inode, path + depth);
  2870. if (err)
  2871. goto out;
  2872. /* first mark the extent as initialized */
  2873. ext4_ext_mark_initialized(ex);
  2874. /* note: ext4_ext_correct_indexes() isn't needed here because
  2875. * borders are not changed
  2876. */
  2877. ext4_ext_try_to_merge(inode, path, ex);
  2878. /* Mark modified extent as dirty */
  2879. err = ext4_ext_dirty(handle, inode, path + depth);
  2880. out:
  2881. ext4_ext_show_leaf(inode, path);
  2882. return err;
  2883. }
  2884. static void unmap_underlying_metadata_blocks(struct block_device *bdev,
  2885. sector_t block, int count)
  2886. {
  2887. int i;
  2888. for (i = 0; i < count; i++)
  2889. unmap_underlying_metadata(bdev, block + i);
  2890. }
  2891. /*
  2892. * Handle EOFBLOCKS_FL flag, clearing it if necessary
  2893. */
  2894. static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
  2895. ext4_lblk_t lblk,
  2896. struct ext4_ext_path *path,
  2897. unsigned int len)
  2898. {
  2899. int i, depth;
  2900. struct ext4_extent_header *eh;
  2901. struct ext4_extent *last_ex;
  2902. if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
  2903. return 0;
  2904. depth = ext_depth(inode);
  2905. eh = path[depth].p_hdr;
  2906. if (unlikely(!eh->eh_entries)) {
  2907. EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
  2908. "EOFBLOCKS_FL set");
  2909. return -EIO;
  2910. }
  2911. last_ex = EXT_LAST_EXTENT(eh);
  2912. /*
  2913. * We should clear the EOFBLOCKS_FL flag if we are writing the
  2914. * last block in the last extent in the file. We test this by
  2915. * first checking to see if the caller to
  2916. * ext4_ext_get_blocks() was interested in the last block (or
  2917. * a block beyond the last block) in the current extent. If
  2918. * this turns out to be false, we can bail out from this
  2919. * function immediately.
  2920. */
  2921. if (lblk + len < le32_to_cpu(last_ex->ee_block) +
  2922. ext4_ext_get_actual_len(last_ex))
  2923. return 0;
  2924. /*
  2925. * If the caller does appear to be planning to write at or
  2926. * beyond the end of the current extent, we then test to see
  2927. * if the current extent is the last extent in the file, by
  2928. * checking to make sure it was reached via the rightmost node
  2929. * at each level of the tree.
  2930. */
  2931. for (i = depth-1; i >= 0; i--)
  2932. if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
  2933. return 0;
  2934. ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  2935. return ext4_mark_inode_dirty(handle, inode);
  2936. }
  2937. /**
  2938. * ext4_find_delalloc_range: find delayed allocated block in the given range.
  2939. *
  2940. * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
  2941. * whether there are any buffers marked for delayed allocation. It returns '1'
  2942. * on the first delalloc'ed buffer head found. If no buffer head in the given
  2943. * range is marked for delalloc, it returns 0.
  2944. * lblk_start should always be <= lblk_end.
  2945. * search_hint_reverse is to indicate that searching in reverse from lblk_end to
  2946. * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
  2947. * block sooner). This is useful when blocks are truncated sequentially from
  2948. * lblk_start towards lblk_end.
  2949. */
  2950. static int ext4_find_delalloc_range(struct inode *inode,
  2951. ext4_lblk_t lblk_start,
  2952. ext4_lblk_t lblk_end,
  2953. int search_hint_reverse)
  2954. {
  2955. struct address_space *mapping = inode->i_mapping;
  2956. struct buffer_head *head, *bh = NULL;
  2957. struct page *page;
  2958. ext4_lblk_t i, pg_lblk;
  2959. pgoff_t index;
  2960. if (!test_opt(inode->i_sb, DELALLOC))
  2961. return 0;
  2962. /* reverse search wont work if fs block size is less than page size */
  2963. if (inode->i_blkbits < PAGE_CACHE_SHIFT)
  2964. search_hint_reverse = 0;
  2965. if (search_hint_reverse)
  2966. i = lblk_end;
  2967. else
  2968. i = lblk_start;
  2969. index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  2970. while ((i >= lblk_start) && (i <= lblk_end)) {
  2971. page = find_get_page(mapping, index);
  2972. if (!page)
  2973. goto nextpage;
  2974. if (!page_has_buffers(page))
  2975. goto nextpage;
  2976. head = page_buffers(page);
  2977. if (!head)
  2978. goto nextpage;
  2979. bh = head;
  2980. pg_lblk = index << (PAGE_CACHE_SHIFT -
  2981. inode->i_blkbits);
  2982. do {
  2983. if (unlikely(pg_lblk < lblk_start)) {
  2984. /*
  2985. * This is possible when fs block size is less
  2986. * than page size and our cluster starts/ends in
  2987. * middle of the page. So we need to skip the
  2988. * initial few blocks till we reach the 'lblk'
  2989. */
  2990. pg_lblk++;
  2991. continue;
  2992. }
  2993. /* Check if the buffer is delayed allocated and that it
  2994. * is not yet mapped. (when da-buffers are mapped during
  2995. * their writeout, their da_mapped bit is set.)
  2996. */
  2997. if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
  2998. page_cache_release(page);
  2999. trace_ext4_find_delalloc_range(inode,
  3000. lblk_start, lblk_end,
  3001. search_hint_reverse,
  3002. 1, i);
  3003. return 1;
  3004. }
  3005. if (search_hint_reverse)
  3006. i--;
  3007. else
  3008. i++;
  3009. } while ((i >= lblk_start) && (i <= lblk_end) &&
  3010. ((bh = bh->b_this_page) != head));
  3011. nextpage:
  3012. if (page)
  3013. page_cache_release(page);
  3014. /*
  3015. * Move to next page. 'i' will be the first lblk in the next
  3016. * page.
  3017. */
  3018. if (search_hint_reverse)
  3019. index--;
  3020. else
  3021. index++;
  3022. i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  3023. }
  3024. trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
  3025. search_hint_reverse, 0, 0);
  3026. return 0;
  3027. }
  3028. int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
  3029. int search_hint_reverse)
  3030. {
  3031. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3032. ext4_lblk_t lblk_start, lblk_end;
  3033. lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
  3034. lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
  3035. return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
  3036. search_hint_reverse);
  3037. }
  3038. /**
  3039. * Determines how many complete clusters (out of those specified by the 'map')
  3040. * are under delalloc and were reserved quota for.
  3041. * This function is called when we are writing out the blocks that were
  3042. * originally written with their allocation delayed, but then the space was
  3043. * allocated using fallocate() before the delayed allocation could be resolved.
  3044. * The cases to look for are:
  3045. * ('=' indicated delayed allocated blocks
  3046. * '-' indicates non-delayed allocated blocks)
  3047. * (a) partial clusters towards beginning and/or end outside of allocated range
  3048. * are not delalloc'ed.
  3049. * Ex:
  3050. * |----c---=|====c====|====c====|===-c----|
  3051. * |++++++ allocated ++++++|
  3052. * ==> 4 complete clusters in above example
  3053. *
  3054. * (b) partial cluster (outside of allocated range) towards either end is
  3055. * marked for delayed allocation. In this case, we will exclude that
  3056. * cluster.
  3057. * Ex:
  3058. * |----====c========|========c========|
  3059. * |++++++ allocated ++++++|
  3060. * ==> 1 complete clusters in above example
  3061. *
  3062. * Ex:
  3063. * |================c================|
  3064. * |++++++ allocated ++++++|
  3065. * ==> 0 complete clusters in above example
  3066. *
  3067. * The ext4_da_update_reserve_space will be called only if we
  3068. * determine here that there were some "entire" clusters that span
  3069. * this 'allocated' range.
  3070. * In the non-bigalloc case, this function will just end up returning num_blks
  3071. * without ever calling ext4_find_delalloc_range.
  3072. */
  3073. static unsigned int
  3074. get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
  3075. unsigned int num_blks)
  3076. {
  3077. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3078. ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
  3079. ext4_lblk_t lblk_from, lblk_to, c_offset;
  3080. unsigned int allocated_clusters = 0;
  3081. alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
  3082. alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
  3083. /* max possible clusters for this allocation */
  3084. allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
  3085. trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
  3086. /* Check towards left side */
  3087. c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
  3088. if (c_offset) {
  3089. lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
  3090. lblk_to = lblk_from + c_offset - 1;
  3091. if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
  3092. allocated_clusters--;
  3093. }
  3094. /* Now check towards right. */
  3095. c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
  3096. if (allocated_clusters && c_offset) {
  3097. lblk_from = lblk_start + num_blks;
  3098. lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
  3099. if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
  3100. allocated_clusters--;
  3101. }
  3102. return allocated_clusters;
  3103. }
  3104. static int
  3105. ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
  3106. struct ext4_map_blocks *map,
  3107. struct ext4_ext_path *path, int flags,
  3108. unsigned int allocated, ext4_fsblk_t newblock)
  3109. {
  3110. int ret = 0;
  3111. int err = 0;
  3112. ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
  3113. ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
  3114. "block %llu, max_blocks %u, flags %x, allocated %u\n",
  3115. inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
  3116. flags, allocated);
  3117. ext4_ext_show_leaf(inode, path);
  3118. trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
  3119. newblock);
  3120. /* get_block() before submit the IO, split the extent */
  3121. if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
  3122. ret = ext4_split_unwritten_extents(handle, inode, map,
  3123. path, flags);
  3124. /*
  3125. * Flag the inode(non aio case) or end_io struct (aio case)
  3126. * that this IO needs to conversion to written when IO is
  3127. * completed
  3128. */
  3129. if (io)
  3130. ext4_set_io_unwritten_flag(inode, io);
  3131. else
  3132. ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
  3133. if (ext4_should_dioread_nolock(inode))
  3134. map->m_flags |= EXT4_MAP_UNINIT;
  3135. goto out;
  3136. }
  3137. /* IO end_io complete, convert the filled extent to written */
  3138. if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
  3139. ret = ext4_convert_unwritten_extents_endio(handle, inode,
  3140. path);
  3141. if (ret >= 0) {
  3142. ext4_update_inode_fsync_trans(handle, inode, 1);
  3143. err = check_eofblocks_fl(handle, inode, map->m_lblk,
  3144. path, map->m_len);
  3145. } else
  3146. err = ret;
  3147. goto out2;
  3148. }
  3149. /* buffered IO case */
  3150. /*
  3151. * repeat fallocate creation request
  3152. * we already have an unwritten extent
  3153. */
  3154. if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
  3155. goto map_out;
  3156. /* buffered READ or buffered write_begin() lookup */
  3157. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  3158. /*
  3159. * We have blocks reserved already. We
  3160. * return allocated blocks so that delalloc
  3161. * won't do block reservation for us. But
  3162. * the buffer head will be unmapped so that
  3163. * a read from the block returns 0s.
  3164. */
  3165. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3166. goto out1;
  3167. }
  3168. /* buffered write, writepage time, convert*/
  3169. ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
  3170. if (ret >= 0)
  3171. ext4_update_inode_fsync_trans(handle, inode, 1);
  3172. out:
  3173. if (ret <= 0) {
  3174. err = ret;
  3175. goto out2;
  3176. } else
  3177. allocated = ret;
  3178. map->m_flags |= EXT4_MAP_NEW;
  3179. /*
  3180. * if we allocated more blocks than requested
  3181. * we need to make sure we unmap the extra block
  3182. * allocated. The actual needed block will get
  3183. * unmapped later when we find the buffer_head marked
  3184. * new.
  3185. */
  3186. if (allocated > map->m_len) {
  3187. unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
  3188. newblock + map->m_len,
  3189. allocated - map->m_len);
  3190. allocated = map->m_len;
  3191. }
  3192. /*
  3193. * If we have done fallocate with the offset that is already
  3194. * delayed allocated, we would have block reservation
  3195. * and quota reservation done in the delayed write path.
  3196. * But fallocate would have already updated quota and block
  3197. * count for this offset. So cancel these reservation
  3198. */
  3199. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
  3200. unsigned int reserved_clusters;
  3201. reserved_clusters = get_reserved_cluster_alloc(inode,
  3202. map->m_lblk, map->m_len);
  3203. if (reserved_clusters)
  3204. ext4_da_update_reserve_space(inode,
  3205. reserved_clusters,
  3206. 0);
  3207. }
  3208. map_out:
  3209. map->m_flags |= EXT4_MAP_MAPPED;
  3210. if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
  3211. err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
  3212. map->m_len);
  3213. if (err < 0)
  3214. goto out2;
  3215. }
  3216. out1:
  3217. if (allocated > map->m_len)
  3218. allocated = map->m_len;
  3219. ext4_ext_show_leaf(inode, path);
  3220. map->m_pblk = newblock;
  3221. map->m_len = allocated;
  3222. out2:
  3223. if (path) {
  3224. ext4_ext_drop_refs(path);
  3225. kfree(path);
  3226. }
  3227. return err ? err : allocated;
  3228. }
  3229. /*
  3230. * get_implied_cluster_alloc - check to see if the requested
  3231. * allocation (in the map structure) overlaps with a cluster already
  3232. * allocated in an extent.
  3233. * @sb The filesystem superblock structure
  3234. * @map The requested lblk->pblk mapping
  3235. * @ex The extent structure which might contain an implied
  3236. * cluster allocation
  3237. *
  3238. * This function is called by ext4_ext_map_blocks() after we failed to
  3239. * find blocks that were already in the inode's extent tree. Hence,
  3240. * we know that the beginning of the requested region cannot overlap
  3241. * the extent from the inode's extent tree. There are three cases we
  3242. * want to catch. The first is this case:
  3243. *
  3244. * |--- cluster # N--|
  3245. * |--- extent ---| |---- requested region ---|
  3246. * |==========|
  3247. *
  3248. * The second case that we need to test for is this one:
  3249. *
  3250. * |--------- cluster # N ----------------|
  3251. * |--- requested region --| |------- extent ----|
  3252. * |=======================|
  3253. *
  3254. * The third case is when the requested region lies between two extents
  3255. * within the same cluster:
  3256. * |------------- cluster # N-------------|
  3257. * |----- ex -----| |---- ex_right ----|
  3258. * |------ requested region ------|
  3259. * |================|
  3260. *
  3261. * In each of the above cases, we need to set the map->m_pblk and
  3262. * map->m_len so it corresponds to the return the extent labelled as
  3263. * "|====|" from cluster #N, since it is already in use for data in
  3264. * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
  3265. * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
  3266. * as a new "allocated" block region. Otherwise, we will return 0 and
  3267. * ext4_ext_map_blocks() will then allocate one or more new clusters
  3268. * by calling ext4_mb_new_blocks().
  3269. */
  3270. static int get_implied_cluster_alloc(struct super_block *sb,
  3271. struct ext4_map_blocks *map,
  3272. struct ext4_extent *ex,
  3273. struct ext4_ext_path *path)
  3274. {
  3275. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3276. ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
  3277. ext4_lblk_t ex_cluster_start, ex_cluster_end;
  3278. ext4_lblk_t rr_cluster_start;
  3279. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  3280. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  3281. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  3282. /* The extent passed in that we are trying to match */
  3283. ex_cluster_start = EXT4_B2C(sbi, ee_block);
  3284. ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
  3285. /* The requested region passed into ext4_map_blocks() */
  3286. rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
  3287. if ((rr_cluster_start == ex_cluster_end) ||
  3288. (rr_cluster_start == ex_cluster_start)) {
  3289. if (rr_cluster_start == ex_cluster_end)
  3290. ee_start += ee_len - 1;
  3291. map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
  3292. c_offset;
  3293. map->m_len = min(map->m_len,
  3294. (unsigned) sbi->s_cluster_ratio - c_offset);
  3295. /*
  3296. * Check for and handle this case:
  3297. *
  3298. * |--------- cluster # N-------------|
  3299. * |------- extent ----|
  3300. * |--- requested region ---|
  3301. * |===========|
  3302. */
  3303. if (map->m_lblk < ee_block)
  3304. map->m_len = min(map->m_len, ee_block - map->m_lblk);
  3305. /*
  3306. * Check for the case where there is already another allocated
  3307. * block to the right of 'ex' but before the end of the cluster.
  3308. *
  3309. * |------------- cluster # N-------------|
  3310. * |----- ex -----| |---- ex_right ----|
  3311. * |------ requested region ------|
  3312. * |================|
  3313. */
  3314. if (map->m_lblk > ee_block) {
  3315. ext4_lblk_t next = ext4_ext_next_allocated_block(path);
  3316. map->m_len = min(map->m_len, next - map->m_lblk);
  3317. }
  3318. trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
  3319. return 1;
  3320. }
  3321. trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
  3322. return 0;
  3323. }
  3324. /*
  3325. * Block allocation/map/preallocation routine for extents based files
  3326. *
  3327. *
  3328. * Need to be called with
  3329. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
  3330. * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
  3331. *
  3332. * return > 0, number of of blocks already mapped/allocated
  3333. * if create == 0 and these are pre-allocated blocks
  3334. * buffer head is unmapped
  3335. * otherwise blocks are mapped
  3336. *
  3337. * return = 0, if plain look up failed (blocks have not been allocated)
  3338. * buffer head is unmapped
  3339. *
  3340. * return < 0, error case.
  3341. */
  3342. int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
  3343. struct ext4_map_blocks *map, int flags)
  3344. {
  3345. struct ext4_ext_path *path = NULL;
  3346. struct ext4_extent newex, *ex, *ex2;
  3347. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3348. ext4_fsblk_t newblock = 0;
  3349. int free_on_err = 0, err = 0, depth, ret;
  3350. unsigned int allocated = 0, offset = 0;
  3351. unsigned int allocated_clusters = 0;
  3352. struct ext4_allocation_request ar;
  3353. ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
  3354. ext4_lblk_t cluster_offset;
  3355. ext_debug("blocks %u/%u requested for inode %lu\n",
  3356. map->m_lblk, map->m_len, inode->i_ino);
  3357. trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
  3358. /* check in cache */
  3359. if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
  3360. if (!newex.ee_start_lo && !newex.ee_start_hi) {
  3361. if ((sbi->s_cluster_ratio > 1) &&
  3362. ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
  3363. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  3364. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  3365. /*
  3366. * block isn't allocated yet and
  3367. * user doesn't want to allocate it
  3368. */
  3369. goto out2;
  3370. }
  3371. /* we should allocate requested block */
  3372. } else {
  3373. /* block is already allocated */
  3374. if (sbi->s_cluster_ratio > 1)
  3375. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  3376. newblock = map->m_lblk
  3377. - le32_to_cpu(newex.ee_block)
  3378. + ext4_ext_pblock(&newex);
  3379. /* number of remaining blocks in the extent */
  3380. allocated = ext4_ext_get_actual_len(&newex) -
  3381. (map->m_lblk - le32_to_cpu(newex.ee_block));
  3382. goto out;
  3383. }
  3384. }
  3385. /* find extent for this block */
  3386. path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
  3387. if (IS_ERR(path)) {
  3388. err = PTR_ERR(path);
  3389. path = NULL;
  3390. goto out2;
  3391. }
  3392. depth = ext_depth(inode);
  3393. /*
  3394. * consistent leaf must not be empty;
  3395. * this situation is possible, though, _during_ tree modification;
  3396. * this is why assert can't be put in ext4_ext_find_extent()
  3397. */
  3398. if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
  3399. EXT4_ERROR_INODE(inode, "bad extent address "
  3400. "lblock: %lu, depth: %d pblock %lld",
  3401. (unsigned long) map->m_lblk, depth,
  3402. path[depth].p_block);
  3403. err = -EIO;
  3404. goto out2;
  3405. }
  3406. ex = path[depth].p_ext;
  3407. if (ex) {
  3408. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  3409. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  3410. unsigned short ee_len;
  3411. /*
  3412. * Uninitialized extents are treated as holes, except that
  3413. * we split out initialized portions during a write.
  3414. */
  3415. ee_len = ext4_ext_get_actual_len(ex);
  3416. trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
  3417. /* if found extent covers block, simply return it */
  3418. if (in_range(map->m_lblk, ee_block, ee_len)) {
  3419. newblock = map->m_lblk - ee_block + ee_start;
  3420. /* number of remaining blocks in the extent */
  3421. allocated = ee_len - (map->m_lblk - ee_block);
  3422. ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
  3423. ee_block, ee_len, newblock);
  3424. /*
  3425. * Do not put uninitialized extent
  3426. * in the cache
  3427. */
  3428. if (!ext4_ext_is_uninitialized(ex)) {
  3429. ext4_ext_put_in_cache(inode, ee_block,
  3430. ee_len, ee_start);
  3431. goto out;
  3432. }
  3433. ret = ext4_ext_handle_uninitialized_extents(
  3434. handle, inode, map, path, flags,
  3435. allocated, newblock);
  3436. return ret;
  3437. }
  3438. }
  3439. if ((sbi->s_cluster_ratio > 1) &&
  3440. ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
  3441. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  3442. /*
  3443. * requested block isn't allocated yet;
  3444. * we couldn't try to create block if create flag is zero
  3445. */
  3446. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  3447. /*
  3448. * put just found gap into cache to speed up
  3449. * subsequent requests
  3450. */
  3451. ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
  3452. goto out2;
  3453. }
  3454. /*
  3455. * Okay, we need to do block allocation.
  3456. */
  3457. map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
  3458. newex.ee_block = cpu_to_le32(map->m_lblk);
  3459. cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
  3460. /*
  3461. * If we are doing bigalloc, check to see if the extent returned
  3462. * by ext4_ext_find_extent() implies a cluster we can use.
  3463. */
  3464. if (cluster_offset && ex &&
  3465. get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
  3466. ar.len = allocated = map->m_len;
  3467. newblock = map->m_pblk;
  3468. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  3469. goto got_allocated_blocks;
  3470. }
  3471. /* find neighbour allocated blocks */
  3472. ar.lleft = map->m_lblk;
  3473. err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
  3474. if (err)
  3475. goto out2;
  3476. ar.lright = map->m_lblk;
  3477. ex2 = NULL;
  3478. err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
  3479. if (err)
  3480. goto out2;
  3481. /* Check if the extent after searching to the right implies a
  3482. * cluster we can use. */
  3483. if ((sbi->s_cluster_ratio > 1) && ex2 &&
  3484. get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
  3485. ar.len = allocated = map->m_len;
  3486. newblock = map->m_pblk;
  3487. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  3488. goto got_allocated_blocks;
  3489. }
  3490. /*
  3491. * See if request is beyond maximum number of blocks we can have in
  3492. * a single extent. For an initialized extent this limit is
  3493. * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
  3494. * EXT_UNINIT_MAX_LEN.
  3495. */
  3496. if (map->m_len > EXT_INIT_MAX_LEN &&
  3497. !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
  3498. map->m_len = EXT_INIT_MAX_LEN;
  3499. else if (map->m_len > EXT_UNINIT_MAX_LEN &&
  3500. (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
  3501. map->m_len = EXT_UNINIT_MAX_LEN;
  3502. /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
  3503. newex.ee_len = cpu_to_le16(map->m_len);
  3504. err = ext4_ext_check_overlap(sbi, inode, &newex, path);
  3505. if (err)
  3506. allocated = ext4_ext_get_actual_len(&newex);
  3507. else
  3508. allocated = map->m_len;
  3509. /* allocate new block */
  3510. ar.inode = inode;
  3511. ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
  3512. ar.logical = map->m_lblk;
  3513. /*
  3514. * We calculate the offset from the beginning of the cluster
  3515. * for the logical block number, since when we allocate a
  3516. * physical cluster, the physical block should start at the
  3517. * same offset from the beginning of the cluster. This is
  3518. * needed so that future calls to get_implied_cluster_alloc()
  3519. * work correctly.
  3520. */
  3521. offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
  3522. ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
  3523. ar.goal -= offset;
  3524. ar.logical -= offset;
  3525. if (S_ISREG(inode->i_mode))
  3526. ar.flags = EXT4_MB_HINT_DATA;
  3527. else
  3528. /* disable in-core preallocation for non-regular files */
  3529. ar.flags = 0;
  3530. if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
  3531. ar.flags |= EXT4_MB_HINT_NOPREALLOC;
  3532. newblock = ext4_mb_new_blocks(handle, &ar, &err);
  3533. if (!newblock)
  3534. goto out2;
  3535. ext_debug("allocate new block: goal %llu, found %llu/%u\n",
  3536. ar.goal, newblock, allocated);
  3537. free_on_err = 1;
  3538. allocated_clusters = ar.len;
  3539. ar.len = EXT4_C2B(sbi, ar.len) - offset;
  3540. if (ar.len > allocated)
  3541. ar.len = allocated;
  3542. got_allocated_blocks:
  3543. /* try to insert new extent into found leaf and return */
  3544. ext4_ext_store_pblock(&newex, newblock + offset);
  3545. newex.ee_len = cpu_to_le16(ar.len);
  3546. /* Mark uninitialized */
  3547. if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
  3548. ext4_ext_mark_uninitialized(&newex);
  3549. /*
  3550. * io_end structure was created for every IO write to an
  3551. * uninitialized extent. To avoid unnecessary conversion,
  3552. * here we flag the IO that really needs the conversion.
  3553. * For non asycn direct IO case, flag the inode state
  3554. * that we need to perform conversion when IO is done.
  3555. */
  3556. if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
  3557. if (io)
  3558. ext4_set_io_unwritten_flag(inode, io);
  3559. else
  3560. ext4_set_inode_state(inode,
  3561. EXT4_STATE_DIO_UNWRITTEN);
  3562. }
  3563. if (ext4_should_dioread_nolock(inode))
  3564. map->m_flags |= EXT4_MAP_UNINIT;
  3565. }
  3566. err = 0;
  3567. if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
  3568. err = check_eofblocks_fl(handle, inode, map->m_lblk,
  3569. path, ar.len);
  3570. if (!err)
  3571. err = ext4_ext_insert_extent(handle, inode, path,
  3572. &newex, flags);
  3573. if (err && free_on_err) {
  3574. int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
  3575. EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
  3576. /* free data blocks we just allocated */
  3577. /* not a good idea to call discard here directly,
  3578. * but otherwise we'd need to call it every free() */
  3579. ext4_discard_preallocations(inode);
  3580. ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
  3581. ext4_ext_get_actual_len(&newex), fb_flags);
  3582. goto out2;
  3583. }
  3584. /* previous routine could use block we allocated */
  3585. newblock = ext4_ext_pblock(&newex);
  3586. allocated = ext4_ext_get_actual_len(&newex);
  3587. if (allocated > map->m_len)
  3588. allocated = map->m_len;
  3589. map->m_flags |= EXT4_MAP_NEW;
  3590. /*
  3591. * Update reserved blocks/metadata blocks after successful
  3592. * block allocation which had been deferred till now.
  3593. */
  3594. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
  3595. unsigned int reserved_clusters;
  3596. /*
  3597. * Check how many clusters we had reserved this allocated range
  3598. */
  3599. reserved_clusters = get_reserved_cluster_alloc(inode,
  3600. map->m_lblk, allocated);
  3601. if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
  3602. if (reserved_clusters) {
  3603. /*
  3604. * We have clusters reserved for this range.
  3605. * But since we are not doing actual allocation
  3606. * and are simply using blocks from previously
  3607. * allocated cluster, we should release the
  3608. * reservation and not claim quota.
  3609. */
  3610. ext4_da_update_reserve_space(inode,
  3611. reserved_clusters, 0);
  3612. }
  3613. } else {
  3614. BUG_ON(allocated_clusters < reserved_clusters);
  3615. /* We will claim quota for all newly allocated blocks.*/
  3616. ext4_da_update_reserve_space(inode, allocated_clusters,
  3617. 1);
  3618. if (reserved_clusters < allocated_clusters) {
  3619. struct ext4_inode_info *ei = EXT4_I(inode);
  3620. int reservation = allocated_clusters -
  3621. reserved_clusters;
  3622. /*
  3623. * It seems we claimed few clusters outside of
  3624. * the range of this allocation. We should give
  3625. * it back to the reservation pool. This can
  3626. * happen in the following case:
  3627. *
  3628. * * Suppose s_cluster_ratio is 4 (i.e., each
  3629. * cluster has 4 blocks. Thus, the clusters
  3630. * are [0-3],[4-7],[8-11]...
  3631. * * First comes delayed allocation write for
  3632. * logical blocks 10 & 11. Since there were no
  3633. * previous delayed allocated blocks in the
  3634. * range [8-11], we would reserve 1 cluster
  3635. * for this write.
  3636. * * Next comes write for logical blocks 3 to 8.
  3637. * In this case, we will reserve 2 clusters
  3638. * (for [0-3] and [4-7]; and not for [8-11] as
  3639. * that range has a delayed allocated blocks.
  3640. * Thus total reserved clusters now becomes 3.
  3641. * * Now, during the delayed allocation writeout
  3642. * time, we will first write blocks [3-8] and
  3643. * allocate 3 clusters for writing these
  3644. * blocks. Also, we would claim all these
  3645. * three clusters above.
  3646. * * Now when we come here to writeout the
  3647. * blocks [10-11], we would expect to claim
  3648. * the reservation of 1 cluster we had made
  3649. * (and we would claim it since there are no
  3650. * more delayed allocated blocks in the range
  3651. * [8-11]. But our reserved cluster count had
  3652. * already gone to 0.
  3653. *
  3654. * Thus, at the step 4 above when we determine
  3655. * that there are still some unwritten delayed
  3656. * allocated blocks outside of our current
  3657. * block range, we should increment the
  3658. * reserved clusters count so that when the
  3659. * remaining blocks finally gets written, we
  3660. * could claim them.
  3661. */
  3662. dquot_reserve_block(inode,
  3663. EXT4_C2B(sbi, reservation));
  3664. spin_lock(&ei->i_block_reservation_lock);
  3665. ei->i_reserved_data_blocks += reservation;
  3666. spin_unlock(&ei->i_block_reservation_lock);
  3667. }
  3668. }
  3669. }
  3670. /*
  3671. * Cache the extent and update transaction to commit on fdatasync only
  3672. * when it is _not_ an uninitialized extent.
  3673. */
  3674. if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
  3675. ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
  3676. ext4_update_inode_fsync_trans(handle, inode, 1);
  3677. } else
  3678. ext4_update_inode_fsync_trans(handle, inode, 0);
  3679. out:
  3680. if (allocated > map->m_len)
  3681. allocated = map->m_len;
  3682. ext4_ext_show_leaf(inode, path);
  3683. map->m_flags |= EXT4_MAP_MAPPED;
  3684. map->m_pblk = newblock;
  3685. map->m_len = allocated;
  3686. out2:
  3687. if (path) {
  3688. ext4_ext_drop_refs(path);
  3689. kfree(path);
  3690. }
  3691. trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
  3692. newblock, map->m_len, err ? err : allocated);
  3693. return err ? err : allocated;
  3694. }
  3695. void ext4_ext_truncate(struct inode *inode)
  3696. {
  3697. struct address_space *mapping = inode->i_mapping;
  3698. struct super_block *sb = inode->i_sb;
  3699. ext4_lblk_t last_block;
  3700. handle_t *handle;
  3701. loff_t page_len;
  3702. int err = 0;
  3703. /*
  3704. * finish any pending end_io work so we won't run the risk of
  3705. * converting any truncated blocks to initialized later
  3706. */
  3707. ext4_flush_completed_IO(inode);
  3708. /*
  3709. * probably first extent we're gonna free will be last in block
  3710. */
  3711. err = ext4_writepage_trans_blocks(inode);
  3712. handle = ext4_journal_start(inode, err);
  3713. if (IS_ERR(handle))
  3714. return;
  3715. if (inode->i_size % PAGE_CACHE_SIZE != 0) {
  3716. page_len = PAGE_CACHE_SIZE -
  3717. (inode->i_size & (PAGE_CACHE_SIZE - 1));
  3718. err = ext4_discard_partial_page_buffers(handle,
  3719. mapping, inode->i_size, page_len, 0);
  3720. if (err)
  3721. goto out_stop;
  3722. }
  3723. if (ext4_orphan_add(handle, inode))
  3724. goto out_stop;
  3725. down_write(&EXT4_I(inode)->i_data_sem);
  3726. ext4_ext_invalidate_cache(inode);
  3727. ext4_discard_preallocations(inode);
  3728. /*
  3729. * TODO: optimization is possible here.
  3730. * Probably we need not scan at all,
  3731. * because page truncation is enough.
  3732. */
  3733. /* we have to know where to truncate from in crash case */
  3734. EXT4_I(inode)->i_disksize = inode->i_size;
  3735. ext4_mark_inode_dirty(handle, inode);
  3736. last_block = (inode->i_size + sb->s_blocksize - 1)
  3737. >> EXT4_BLOCK_SIZE_BITS(sb);
  3738. err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
  3739. /* In a multi-transaction truncate, we only make the final
  3740. * transaction synchronous.
  3741. */
  3742. if (IS_SYNC(inode))
  3743. ext4_handle_sync(handle);
  3744. up_write(&EXT4_I(inode)->i_data_sem);
  3745. out_stop:
  3746. /*
  3747. * If this was a simple ftruncate() and the file will remain alive,
  3748. * then we need to clear up the orphan record which we created above.
  3749. * However, if this was a real unlink then we were called by
  3750. * ext4_delete_inode(), and we allow that function to clean up the
  3751. * orphan info for us.
  3752. */
  3753. if (inode->i_nlink)
  3754. ext4_orphan_del(handle, inode);
  3755. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3756. ext4_mark_inode_dirty(handle, inode);
  3757. ext4_journal_stop(handle);
  3758. }
  3759. static void ext4_falloc_update_inode(struct inode *inode,
  3760. int mode, loff_t new_size, int update_ctime)
  3761. {
  3762. struct timespec now;
  3763. if (update_ctime) {
  3764. now = current_fs_time(inode->i_sb);
  3765. if (!timespec_equal(&inode->i_ctime, &now))
  3766. inode->i_ctime = now;
  3767. }
  3768. /*
  3769. * Update only when preallocation was requested beyond
  3770. * the file size.
  3771. */
  3772. if (!(mode & FALLOC_FL_KEEP_SIZE)) {
  3773. if (new_size > i_size_read(inode))
  3774. i_size_write(inode, new_size);
  3775. if (new_size > EXT4_I(inode)->i_disksize)
  3776. ext4_update_i_disksize(inode, new_size);
  3777. } else {
  3778. /*
  3779. * Mark that we allocate beyond EOF so the subsequent truncate
  3780. * can proceed even if the new size is the same as i_size.
  3781. */
  3782. if (new_size > i_size_read(inode))
  3783. ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  3784. }
  3785. }
  3786. /*
  3787. * preallocate space for a file. This implements ext4's fallocate file
  3788. * operation, which gets called from sys_fallocate system call.
  3789. * For block-mapped files, posix_fallocate should fall back to the method
  3790. * of writing zeroes to the required new blocks (the same behavior which is
  3791. * expected for file systems which do not support fallocate() system call).
  3792. */
  3793. long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  3794. {
  3795. struct inode *inode = file->f_path.dentry->d_inode;
  3796. handle_t *handle;
  3797. loff_t new_size;
  3798. unsigned int max_blocks;
  3799. int ret = 0;
  3800. int ret2 = 0;
  3801. int retries = 0;
  3802. int flags;
  3803. struct ext4_map_blocks map;
  3804. unsigned int credits, blkbits = inode->i_blkbits;
  3805. /*
  3806. * currently supporting (pre)allocate mode for extent-based
  3807. * files _only_
  3808. */
  3809. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  3810. return -EOPNOTSUPP;
  3811. /* Return error if mode is not supported */
  3812. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  3813. return -EOPNOTSUPP;
  3814. if (mode & FALLOC_FL_PUNCH_HOLE)
  3815. return ext4_punch_hole(file, offset, len);
  3816. trace_ext4_fallocate_enter(inode, offset, len, mode);
  3817. map.m_lblk = offset >> blkbits;
  3818. /*
  3819. * We can't just convert len to max_blocks because
  3820. * If blocksize = 4096 offset = 3072 and len = 2048
  3821. */
  3822. max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
  3823. - map.m_lblk;
  3824. /*
  3825. * credits to insert 1 extent into extent tree
  3826. */
  3827. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  3828. mutex_lock(&inode->i_mutex);
  3829. ret = inode_newsize_ok(inode, (len + offset));
  3830. if (ret) {
  3831. mutex_unlock(&inode->i_mutex);
  3832. trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
  3833. return ret;
  3834. }
  3835. flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
  3836. if (mode & FALLOC_FL_KEEP_SIZE)
  3837. flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
  3838. /*
  3839. * Don't normalize the request if it can fit in one extent so
  3840. * that it doesn't get unnecessarily split into multiple
  3841. * extents.
  3842. */
  3843. if (len <= EXT_UNINIT_MAX_LEN << blkbits)
  3844. flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
  3845. retry:
  3846. while (ret >= 0 && ret < max_blocks) {
  3847. map.m_lblk = map.m_lblk + ret;
  3848. map.m_len = max_blocks = max_blocks - ret;
  3849. handle = ext4_journal_start(inode, credits);
  3850. if (IS_ERR(handle)) {
  3851. ret = PTR_ERR(handle);
  3852. break;
  3853. }
  3854. ret = ext4_map_blocks(handle, inode, &map, flags);
  3855. if (ret <= 0) {
  3856. #ifdef EXT4FS_DEBUG
  3857. WARN_ON(ret <= 0);
  3858. printk(KERN_ERR "%s: ext4_ext_map_blocks "
  3859. "returned error inode#%lu, block=%u, "
  3860. "max_blocks=%u", __func__,
  3861. inode->i_ino, map.m_lblk, max_blocks);
  3862. #endif
  3863. ext4_mark_inode_dirty(handle, inode);
  3864. ret2 = ext4_journal_stop(handle);
  3865. break;
  3866. }
  3867. if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
  3868. blkbits) >> blkbits))
  3869. new_size = offset + len;
  3870. else
  3871. new_size = ((loff_t) map.m_lblk + ret) << blkbits;
  3872. ext4_falloc_update_inode(inode, mode, new_size,
  3873. (map.m_flags & EXT4_MAP_NEW));
  3874. ext4_mark_inode_dirty(handle, inode);
  3875. ret2 = ext4_journal_stop(handle);
  3876. if (ret2)
  3877. break;
  3878. }
  3879. if (ret == -ENOSPC &&
  3880. ext4_should_retry_alloc(inode->i_sb, &retries)) {
  3881. ret = 0;
  3882. goto retry;
  3883. }
  3884. mutex_unlock(&inode->i_mutex);
  3885. trace_ext4_fallocate_exit(inode, offset, max_blocks,
  3886. ret > 0 ? ret2 : ret);
  3887. return ret > 0 ? ret2 : ret;
  3888. }
  3889. /*
  3890. * This function convert a range of blocks to written extents
  3891. * The caller of this function will pass the start offset and the size.
  3892. * all unwritten extents within this range will be converted to
  3893. * written extents.
  3894. *
  3895. * This function is called from the direct IO end io call back
  3896. * function, to convert the fallocated extents after IO is completed.
  3897. * Returns 0 on success.
  3898. */
  3899. int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
  3900. ssize_t len)
  3901. {
  3902. handle_t *handle;
  3903. unsigned int max_blocks;
  3904. int ret = 0;
  3905. int ret2 = 0;
  3906. struct ext4_map_blocks map;
  3907. unsigned int credits, blkbits = inode->i_blkbits;
  3908. map.m_lblk = offset >> blkbits;
  3909. /*
  3910. * We can't just convert len to max_blocks because
  3911. * If blocksize = 4096 offset = 3072 and len = 2048
  3912. */
  3913. max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
  3914. map.m_lblk);
  3915. /*
  3916. * credits to insert 1 extent into extent tree
  3917. */
  3918. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  3919. while (ret >= 0 && ret < max_blocks) {
  3920. map.m_lblk += ret;
  3921. map.m_len = (max_blocks -= ret);
  3922. handle = ext4_journal_start(inode, credits);
  3923. if (IS_ERR(handle)) {
  3924. ret = PTR_ERR(handle);
  3925. break;
  3926. }
  3927. ret = ext4_map_blocks(handle, inode, &map,
  3928. EXT4_GET_BLOCKS_IO_CONVERT_EXT);
  3929. if (ret <= 0) {
  3930. WARN_ON(ret <= 0);
  3931. printk(KERN_ERR "%s: ext4_ext_map_blocks "
  3932. "returned error inode#%lu, block=%u, "
  3933. "max_blocks=%u", __func__,
  3934. inode->i_ino, map.m_lblk, map.m_len);
  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. }