extents.c 133 KB

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