extents.c 133 KB

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