mballoc.c 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public Licens
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  17. */
  18. /*
  19. * mballoc.c contains the multiblocks allocation routines
  20. */
  21. #include "mballoc.h"
  22. #include <trace/events/ext4.h>
  23. /*
  24. * MUSTDO:
  25. * - test ext4_ext_search_left() and ext4_ext_search_right()
  26. * - search for metadata in few groups
  27. *
  28. * TODO v4:
  29. * - normalization should take into account whether file is still open
  30. * - discard preallocations if no free space left (policy?)
  31. * - don't normalize tails
  32. * - quota
  33. * - reservation for superuser
  34. *
  35. * TODO v3:
  36. * - bitmap read-ahead (proposed by Oleg Drokin aka green)
  37. * - track min/max extents in each group for better group selection
  38. * - mb_mark_used() may allocate chunk right after splitting buddy
  39. * - tree of groups sorted by number of free blocks
  40. * - error handling
  41. */
  42. /*
  43. * The allocation request involve request for multiple number of blocks
  44. * near to the goal(block) value specified.
  45. *
  46. * During initialization phase of the allocator we decide to use the
  47. * group preallocation or inode preallocation depending on the size of
  48. * the file. The size of the file could be the resulting file size we
  49. * would have after allocation, or the current file size, which ever
  50. * is larger. If the size is less than sbi->s_mb_stream_request we
  51. * select to use the group preallocation. The default value of
  52. * s_mb_stream_request is 16 blocks. This can also be tuned via
  53. * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
  54. * terms of number of blocks.
  55. *
  56. * The main motivation for having small file use group preallocation is to
  57. * ensure that we have small files closer together on the disk.
  58. *
  59. * First stage the allocator looks at the inode prealloc list,
  60. * ext4_inode_info->i_prealloc_list, which contains list of prealloc
  61. * spaces for this particular inode. The inode prealloc space is
  62. * represented as:
  63. *
  64. * pa_lstart -> the logical start block for this prealloc space
  65. * pa_pstart -> the physical start block for this prealloc space
  66. * pa_len -> lenght for this prealloc space
  67. * pa_free -> free space available in this prealloc space
  68. *
  69. * The inode preallocation space is used looking at the _logical_ start
  70. * block. If only the logical file block falls within the range of prealloc
  71. * space we will consume the particular prealloc space. This make sure that
  72. * that the we have contiguous physical blocks representing the file blocks
  73. *
  74. * The important thing to be noted in case of inode prealloc space is that
  75. * we don't modify the values associated to inode prealloc space except
  76. * pa_free.
  77. *
  78. * If we are not able to find blocks in the inode prealloc space and if we
  79. * have the group allocation flag set then we look at the locality group
  80. * prealloc space. These are per CPU prealloc list repreasented as
  81. *
  82. * ext4_sb_info.s_locality_groups[smp_processor_id()]
  83. *
  84. * The reason for having a per cpu locality group is to reduce the contention
  85. * between CPUs. It is possible to get scheduled at this point.
  86. *
  87. * The locality group prealloc space is used looking at whether we have
  88. * enough free space (pa_free) withing the prealloc space.
  89. *
  90. * If we can't allocate blocks via inode prealloc or/and locality group
  91. * prealloc then we look at the buddy cache. The buddy cache is represented
  92. * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
  93. * mapped to the buddy and bitmap information regarding different
  94. * groups. The buddy information is attached to buddy cache inode so that
  95. * we can access them through the page cache. The information regarding
  96. * each group is loaded via ext4_mb_load_buddy. The information involve
  97. * block bitmap and buddy information. The information are stored in the
  98. * inode as:
  99. *
  100. * { page }
  101. * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
  102. *
  103. *
  104. * one block each for bitmap and buddy information. So for each group we
  105. * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
  106. * blocksize) blocks. So it can have information regarding groups_per_page
  107. * which is blocks_per_page/2
  108. *
  109. * The buddy cache inode is not stored on disk. The inode is thrown
  110. * away when the filesystem is unmounted.
  111. *
  112. * We look for count number of blocks in the buddy cache. If we were able
  113. * to locate that many free blocks we return with additional information
  114. * regarding rest of the contiguous physical block available
  115. *
  116. * Before allocating blocks via buddy cache we normalize the request
  117. * blocks. This ensure we ask for more blocks that we needed. The extra
  118. * blocks that we get after allocation is added to the respective prealloc
  119. * list. In case of inode preallocation we follow a list of heuristics
  120. * based on file size. This can be found in ext4_mb_normalize_request. If
  121. * we are doing a group prealloc we try to normalize the request to
  122. * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
  123. * 512 blocks. This can be tuned via
  124. * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
  125. * terms of number of blocks. If we have mounted the file system with -O
  126. * stripe=<value> option the group prealloc request is normalized to the
  127. * stripe value (sbi->s_stripe)
  128. *
  129. * The regular allocator(using the buddy cache) supports few tunables.
  130. *
  131. * /sys/fs/ext4/<partition>/mb_min_to_scan
  132. * /sys/fs/ext4/<partition>/mb_max_to_scan
  133. * /sys/fs/ext4/<partition>/mb_order2_req
  134. *
  135. * The regular allocator uses buddy scan only if the request len is power of
  136. * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
  137. * value of s_mb_order2_reqs can be tuned via
  138. * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
  139. * stripe size (sbi->s_stripe), we try to search for contigous block in
  140. * stripe size. This should result in better allocation on RAID setups. If
  141. * not, we search in the specific group using bitmap for best extents. The
  142. * tunable min_to_scan and max_to_scan control the behaviour here.
  143. * min_to_scan indicate how long the mballoc __must__ look for a best
  144. * extent and max_to_scan indicates how long the mballoc __can__ look for a
  145. * best extent in the found extents. Searching for the blocks starts with
  146. * the group specified as the goal value in allocation context via
  147. * ac_g_ex. Each group is first checked based on the criteria whether it
  148. * can used for allocation. ext4_mb_good_group explains how the groups are
  149. * checked.
  150. *
  151. * Both the prealloc space are getting populated as above. So for the first
  152. * request we will hit the buddy cache which will result in this prealloc
  153. * space getting filled. The prealloc space is then later used for the
  154. * subsequent request.
  155. */
  156. /*
  157. * mballoc operates on the following data:
  158. * - on-disk bitmap
  159. * - in-core buddy (actually includes buddy and bitmap)
  160. * - preallocation descriptors (PAs)
  161. *
  162. * there are two types of preallocations:
  163. * - inode
  164. * assiged to specific inode and can be used for this inode only.
  165. * it describes part of inode's space preallocated to specific
  166. * physical blocks. any block from that preallocated can be used
  167. * independent. the descriptor just tracks number of blocks left
  168. * unused. so, before taking some block from descriptor, one must
  169. * make sure corresponded logical block isn't allocated yet. this
  170. * also means that freeing any block within descriptor's range
  171. * must discard all preallocated blocks.
  172. * - locality group
  173. * assigned to specific locality group which does not translate to
  174. * permanent set of inodes: inode can join and leave group. space
  175. * from this type of preallocation can be used for any inode. thus
  176. * it's consumed from the beginning to the end.
  177. *
  178. * relation between them can be expressed as:
  179. * in-core buddy = on-disk bitmap + preallocation descriptors
  180. *
  181. * this mean blocks mballoc considers used are:
  182. * - allocated blocks (persistent)
  183. * - preallocated blocks (non-persistent)
  184. *
  185. * consistency in mballoc world means that at any time a block is either
  186. * free or used in ALL structures. notice: "any time" should not be read
  187. * literally -- time is discrete and delimited by locks.
  188. *
  189. * to keep it simple, we don't use block numbers, instead we count number of
  190. * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
  191. *
  192. * all operations can be expressed as:
  193. * - init buddy: buddy = on-disk + PAs
  194. * - new PA: buddy += N; PA = N
  195. * - use inode PA: on-disk += N; PA -= N
  196. * - discard inode PA buddy -= on-disk - PA; PA = 0
  197. * - use locality group PA on-disk += N; PA -= N
  198. * - discard locality group PA buddy -= PA; PA = 0
  199. * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
  200. * is used in real operation because we can't know actual used
  201. * bits from PA, only from on-disk bitmap
  202. *
  203. * if we follow this strict logic, then all operations above should be atomic.
  204. * given some of them can block, we'd have to use something like semaphores
  205. * killing performance on high-end SMP hardware. let's try to relax it using
  206. * the following knowledge:
  207. * 1) if buddy is referenced, it's already initialized
  208. * 2) while block is used in buddy and the buddy is referenced,
  209. * nobody can re-allocate that block
  210. * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
  211. * bit set and PA claims same block, it's OK. IOW, one can set bit in
  212. * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
  213. * block
  214. *
  215. * so, now we're building a concurrency table:
  216. * - init buddy vs.
  217. * - new PA
  218. * blocks for PA are allocated in the buddy, buddy must be referenced
  219. * until PA is linked to allocation group to avoid concurrent buddy init
  220. * - use inode PA
  221. * we need to make sure that either on-disk bitmap or PA has uptodate data
  222. * given (3) we care that PA-=N operation doesn't interfere with init
  223. * - discard inode PA
  224. * the simplest way would be to have buddy initialized by the discard
  225. * - use locality group PA
  226. * again PA-=N must be serialized with init
  227. * - discard locality group PA
  228. * the simplest way would be to have buddy initialized by the discard
  229. * - new PA vs.
  230. * - use inode PA
  231. * i_data_sem serializes them
  232. * - discard inode PA
  233. * discard process must wait until PA isn't used by another process
  234. * - use locality group PA
  235. * some mutex should serialize them
  236. * - discard locality group PA
  237. * discard process must wait until PA isn't used by another process
  238. * - use inode PA
  239. * - use inode PA
  240. * i_data_sem or another mutex should serializes them
  241. * - discard inode PA
  242. * discard process must wait until PA isn't used by another process
  243. * - use locality group PA
  244. * nothing wrong here -- they're different PAs covering different blocks
  245. * - discard locality group PA
  246. * discard process must wait until PA isn't used by another process
  247. *
  248. * now we're ready to make few consequences:
  249. * - PA is referenced and while it is no discard is possible
  250. * - PA is referenced until block isn't marked in on-disk bitmap
  251. * - PA changes only after on-disk bitmap
  252. * - discard must not compete with init. either init is done before
  253. * any discard or they're serialized somehow
  254. * - buddy init as sum of on-disk bitmap and PAs is done atomically
  255. *
  256. * a special case when we've used PA to emptiness. no need to modify buddy
  257. * in this case, but we should care about concurrent init
  258. *
  259. */
  260. /*
  261. * Logic in few words:
  262. *
  263. * - allocation:
  264. * load group
  265. * find blocks
  266. * mark bits in on-disk bitmap
  267. * release group
  268. *
  269. * - use preallocation:
  270. * find proper PA (per-inode or group)
  271. * load group
  272. * mark bits in on-disk bitmap
  273. * release group
  274. * release PA
  275. *
  276. * - free:
  277. * load group
  278. * mark bits in on-disk bitmap
  279. * release group
  280. *
  281. * - discard preallocations in group:
  282. * mark PAs deleted
  283. * move them onto local list
  284. * load on-disk bitmap
  285. * load group
  286. * remove PA from object (inode or locality group)
  287. * mark free blocks in-core
  288. *
  289. * - discard inode's preallocations:
  290. */
  291. /*
  292. * Locking rules
  293. *
  294. * Locks:
  295. * - bitlock on a group (group)
  296. * - object (inode/locality) (object)
  297. * - per-pa lock (pa)
  298. *
  299. * Paths:
  300. * - new pa
  301. * object
  302. * group
  303. *
  304. * - find and use pa:
  305. * pa
  306. *
  307. * - release consumed pa:
  308. * pa
  309. * group
  310. * object
  311. *
  312. * - generate in-core bitmap:
  313. * group
  314. * pa
  315. *
  316. * - discard all for given object (inode, locality group):
  317. * object
  318. * pa
  319. * group
  320. *
  321. * - discard all for given group:
  322. * group
  323. * pa
  324. * group
  325. * object
  326. *
  327. */
  328. static struct kmem_cache *ext4_pspace_cachep;
  329. static struct kmem_cache *ext4_ac_cachep;
  330. static struct kmem_cache *ext4_free_ext_cachep;
  331. static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  332. ext4_group_t group);
  333. static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
  334. ext4_group_t group);
  335. static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
  336. static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
  337. {
  338. #if BITS_PER_LONG == 64
  339. *bit += ((unsigned long) addr & 7UL) << 3;
  340. addr = (void *) ((unsigned long) addr & ~7UL);
  341. #elif BITS_PER_LONG == 32
  342. *bit += ((unsigned long) addr & 3UL) << 3;
  343. addr = (void *) ((unsigned long) addr & ~3UL);
  344. #else
  345. #error "how many bits you are?!"
  346. #endif
  347. return addr;
  348. }
  349. static inline int mb_test_bit(int bit, void *addr)
  350. {
  351. /*
  352. * ext4_test_bit on architecture like powerpc
  353. * needs unsigned long aligned address
  354. */
  355. addr = mb_correct_addr_and_bit(&bit, addr);
  356. return ext4_test_bit(bit, addr);
  357. }
  358. static inline void mb_set_bit(int bit, void *addr)
  359. {
  360. addr = mb_correct_addr_and_bit(&bit, addr);
  361. ext4_set_bit(bit, addr);
  362. }
  363. static inline void mb_clear_bit(int bit, void *addr)
  364. {
  365. addr = mb_correct_addr_and_bit(&bit, addr);
  366. ext4_clear_bit(bit, addr);
  367. }
  368. static inline int mb_find_next_zero_bit(void *addr, int max, int start)
  369. {
  370. int fix = 0, ret, tmpmax;
  371. addr = mb_correct_addr_and_bit(&fix, addr);
  372. tmpmax = max + fix;
  373. start += fix;
  374. ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
  375. if (ret > max)
  376. return max;
  377. return ret;
  378. }
  379. static inline int mb_find_next_bit(void *addr, int max, int start)
  380. {
  381. int fix = 0, ret, tmpmax;
  382. addr = mb_correct_addr_and_bit(&fix, addr);
  383. tmpmax = max + fix;
  384. start += fix;
  385. ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
  386. if (ret > max)
  387. return max;
  388. return ret;
  389. }
  390. static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
  391. {
  392. char *bb;
  393. BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
  394. BUG_ON(max == NULL);
  395. if (order > e4b->bd_blkbits + 1) {
  396. *max = 0;
  397. return NULL;
  398. }
  399. /* at order 0 we see each particular block */
  400. *max = 1 << (e4b->bd_blkbits + 3);
  401. if (order == 0)
  402. return EXT4_MB_BITMAP(e4b);
  403. bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
  404. *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
  405. return bb;
  406. }
  407. #ifdef DOUBLE_CHECK
  408. static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
  409. int first, int count)
  410. {
  411. int i;
  412. struct super_block *sb = e4b->bd_sb;
  413. if (unlikely(e4b->bd_info->bb_bitmap == NULL))
  414. return;
  415. assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
  416. for (i = 0; i < count; i++) {
  417. if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
  418. ext4_fsblk_t blocknr;
  419. blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
  420. blocknr += first + i;
  421. blocknr +=
  422. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  423. ext4_grp_locked_error(sb, e4b->bd_group,
  424. __func__, "double-free of inode"
  425. " %lu's block %llu(bit %u in group %u)",
  426. inode ? inode->i_ino : 0, blocknr,
  427. first + i, e4b->bd_group);
  428. }
  429. mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
  430. }
  431. }
  432. static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
  433. {
  434. int i;
  435. if (unlikely(e4b->bd_info->bb_bitmap == NULL))
  436. return;
  437. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  438. for (i = 0; i < count; i++) {
  439. BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
  440. mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
  441. }
  442. }
  443. static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
  444. {
  445. if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
  446. unsigned char *b1, *b2;
  447. int i;
  448. b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
  449. b2 = (unsigned char *) bitmap;
  450. for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
  451. if (b1[i] != b2[i]) {
  452. printk(KERN_ERR "corruption in group %u "
  453. "at byte %u(%u): %x in copy != %x "
  454. "on disk/prealloc\n",
  455. e4b->bd_group, i, i * 8, b1[i], b2[i]);
  456. BUG();
  457. }
  458. }
  459. }
  460. }
  461. #else
  462. static inline void mb_free_blocks_double(struct inode *inode,
  463. struct ext4_buddy *e4b, int first, int count)
  464. {
  465. return;
  466. }
  467. static inline void mb_mark_used_double(struct ext4_buddy *e4b,
  468. int first, int count)
  469. {
  470. return;
  471. }
  472. static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
  473. {
  474. return;
  475. }
  476. #endif
  477. #ifdef AGGRESSIVE_CHECK
  478. #define MB_CHECK_ASSERT(assert) \
  479. do { \
  480. if (!(assert)) { \
  481. printk(KERN_EMERG \
  482. "Assertion failure in %s() at %s:%d: \"%s\"\n", \
  483. function, file, line, # assert); \
  484. BUG(); \
  485. } \
  486. } while (0)
  487. static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
  488. const char *function, int line)
  489. {
  490. struct super_block *sb = e4b->bd_sb;
  491. int order = e4b->bd_blkbits + 1;
  492. int max;
  493. int max2;
  494. int i;
  495. int j;
  496. int k;
  497. int count;
  498. struct ext4_group_info *grp;
  499. int fragments = 0;
  500. int fstart;
  501. struct list_head *cur;
  502. void *buddy;
  503. void *buddy2;
  504. {
  505. static int mb_check_counter;
  506. if (mb_check_counter++ % 100 != 0)
  507. return 0;
  508. }
  509. while (order > 1) {
  510. buddy = mb_find_buddy(e4b, order, &max);
  511. MB_CHECK_ASSERT(buddy);
  512. buddy2 = mb_find_buddy(e4b, order - 1, &max2);
  513. MB_CHECK_ASSERT(buddy2);
  514. MB_CHECK_ASSERT(buddy != buddy2);
  515. MB_CHECK_ASSERT(max * 2 == max2);
  516. count = 0;
  517. for (i = 0; i < max; i++) {
  518. if (mb_test_bit(i, buddy)) {
  519. /* only single bit in buddy2 may be 1 */
  520. if (!mb_test_bit(i << 1, buddy2)) {
  521. MB_CHECK_ASSERT(
  522. mb_test_bit((i<<1)+1, buddy2));
  523. } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
  524. MB_CHECK_ASSERT(
  525. mb_test_bit(i << 1, buddy2));
  526. }
  527. continue;
  528. }
  529. /* both bits in buddy2 must be 0 */
  530. MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
  531. MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
  532. for (j = 0; j < (1 << order); j++) {
  533. k = (i * (1 << order)) + j;
  534. MB_CHECK_ASSERT(
  535. !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
  536. }
  537. count++;
  538. }
  539. MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
  540. order--;
  541. }
  542. fstart = -1;
  543. buddy = mb_find_buddy(e4b, 0, &max);
  544. for (i = 0; i < max; i++) {
  545. if (!mb_test_bit(i, buddy)) {
  546. MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
  547. if (fstart == -1) {
  548. fragments++;
  549. fstart = i;
  550. }
  551. continue;
  552. }
  553. fstart = -1;
  554. /* check used bits only */
  555. for (j = 0; j < e4b->bd_blkbits + 1; j++) {
  556. buddy2 = mb_find_buddy(e4b, j, &max2);
  557. k = i >> j;
  558. MB_CHECK_ASSERT(k < max2);
  559. MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
  560. }
  561. }
  562. MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
  563. MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
  564. grp = ext4_get_group_info(sb, e4b->bd_group);
  565. buddy = mb_find_buddy(e4b, 0, &max);
  566. list_for_each(cur, &grp->bb_prealloc_list) {
  567. ext4_group_t groupnr;
  568. struct ext4_prealloc_space *pa;
  569. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  570. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
  571. MB_CHECK_ASSERT(groupnr == e4b->bd_group);
  572. for (i = 0; i < pa->pa_len; i++)
  573. MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
  574. }
  575. return 0;
  576. }
  577. #undef MB_CHECK_ASSERT
  578. #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
  579. __FILE__, __func__, __LINE__)
  580. #else
  581. #define mb_check_buddy(e4b)
  582. #endif
  583. /* FIXME!! need more doc */
  584. static void ext4_mb_mark_free_simple(struct super_block *sb,
  585. void *buddy, unsigned first, int len,
  586. struct ext4_group_info *grp)
  587. {
  588. struct ext4_sb_info *sbi = EXT4_SB(sb);
  589. unsigned short min;
  590. unsigned short max;
  591. unsigned short chunk;
  592. unsigned short border;
  593. BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
  594. border = 2 << sb->s_blocksize_bits;
  595. while (len > 0) {
  596. /* find how many blocks can be covered since this position */
  597. max = ffs(first | border) - 1;
  598. /* find how many blocks of power 2 we need to mark */
  599. min = fls(len) - 1;
  600. if (max < min)
  601. min = max;
  602. chunk = 1 << min;
  603. /* mark multiblock chunks only */
  604. grp->bb_counters[min]++;
  605. if (min > 0)
  606. mb_clear_bit(first >> min,
  607. buddy + sbi->s_mb_offsets[min]);
  608. len -= chunk;
  609. first += chunk;
  610. }
  611. }
  612. static noinline_for_stack
  613. void ext4_mb_generate_buddy(struct super_block *sb,
  614. void *buddy, void *bitmap, ext4_group_t group)
  615. {
  616. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  617. unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
  618. unsigned short i = 0;
  619. unsigned short first;
  620. unsigned short len;
  621. unsigned free = 0;
  622. unsigned fragments = 0;
  623. unsigned long long period = get_cycles();
  624. /* initialize buddy from bitmap which is aggregation
  625. * of on-disk bitmap and preallocations */
  626. i = mb_find_next_zero_bit(bitmap, max, 0);
  627. grp->bb_first_free = i;
  628. while (i < max) {
  629. fragments++;
  630. first = i;
  631. i = mb_find_next_bit(bitmap, max, i);
  632. len = i - first;
  633. free += len;
  634. if (len > 1)
  635. ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
  636. else
  637. grp->bb_counters[0]++;
  638. if (i < max)
  639. i = mb_find_next_zero_bit(bitmap, max, i);
  640. }
  641. grp->bb_fragments = fragments;
  642. if (free != grp->bb_free) {
  643. ext4_grp_locked_error(sb, group, __func__,
  644. "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
  645. group, free, grp->bb_free);
  646. /*
  647. * If we intent to continue, we consider group descritor
  648. * corrupt and update bb_free using bitmap value
  649. */
  650. grp->bb_free = free;
  651. }
  652. clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
  653. period = get_cycles() - period;
  654. spin_lock(&EXT4_SB(sb)->s_bal_lock);
  655. EXT4_SB(sb)->s_mb_buddies_generated++;
  656. EXT4_SB(sb)->s_mb_generation_time += period;
  657. spin_unlock(&EXT4_SB(sb)->s_bal_lock);
  658. }
  659. /* The buddy information is attached the buddy cache inode
  660. * for convenience. The information regarding each group
  661. * is loaded via ext4_mb_load_buddy. The information involve
  662. * block bitmap and buddy information. The information are
  663. * stored in the inode as
  664. *
  665. * { page }
  666. * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
  667. *
  668. *
  669. * one block each for bitmap and buddy information.
  670. * So for each group we take up 2 blocks. A page can
  671. * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
  672. * So it can have information regarding groups_per_page which
  673. * is blocks_per_page/2
  674. */
  675. static int ext4_mb_init_cache(struct page *page, char *incore)
  676. {
  677. ext4_group_t ngroups;
  678. int blocksize;
  679. int blocks_per_page;
  680. int groups_per_page;
  681. int err = 0;
  682. int i;
  683. ext4_group_t first_group;
  684. int first_block;
  685. struct super_block *sb;
  686. struct buffer_head *bhs;
  687. struct buffer_head **bh;
  688. struct inode *inode;
  689. char *data;
  690. char *bitmap;
  691. mb_debug("init page %lu\n", page->index);
  692. inode = page->mapping->host;
  693. sb = inode->i_sb;
  694. ngroups = ext4_get_groups_count(sb);
  695. blocksize = 1 << inode->i_blkbits;
  696. blocks_per_page = PAGE_CACHE_SIZE / blocksize;
  697. groups_per_page = blocks_per_page >> 1;
  698. if (groups_per_page == 0)
  699. groups_per_page = 1;
  700. /* allocate buffer_heads to read bitmaps */
  701. if (groups_per_page > 1) {
  702. err = -ENOMEM;
  703. i = sizeof(struct buffer_head *) * groups_per_page;
  704. bh = kzalloc(i, GFP_NOFS);
  705. if (bh == NULL)
  706. goto out;
  707. } else
  708. bh = &bhs;
  709. first_group = page->index * blocks_per_page / 2;
  710. /* read all groups the page covers into the cache */
  711. for (i = 0; i < groups_per_page; i++) {
  712. struct ext4_group_desc *desc;
  713. if (first_group + i >= ngroups)
  714. break;
  715. err = -EIO;
  716. desc = ext4_get_group_desc(sb, first_group + i, NULL);
  717. if (desc == NULL)
  718. goto out;
  719. err = -ENOMEM;
  720. bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
  721. if (bh[i] == NULL)
  722. goto out;
  723. if (bitmap_uptodate(bh[i]))
  724. continue;
  725. lock_buffer(bh[i]);
  726. if (bitmap_uptodate(bh[i])) {
  727. unlock_buffer(bh[i]);
  728. continue;
  729. }
  730. ext4_lock_group(sb, first_group + i);
  731. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  732. ext4_init_block_bitmap(sb, bh[i],
  733. first_group + i, desc);
  734. set_bitmap_uptodate(bh[i]);
  735. set_buffer_uptodate(bh[i]);
  736. ext4_unlock_group(sb, first_group + i);
  737. unlock_buffer(bh[i]);
  738. continue;
  739. }
  740. ext4_unlock_group(sb, first_group + i);
  741. if (buffer_uptodate(bh[i])) {
  742. /*
  743. * if not uninit if bh is uptodate,
  744. * bitmap is also uptodate
  745. */
  746. set_bitmap_uptodate(bh[i]);
  747. unlock_buffer(bh[i]);
  748. continue;
  749. }
  750. get_bh(bh[i]);
  751. /*
  752. * submit the buffer_head for read. We can
  753. * safely mark the bitmap as uptodate now.
  754. * We do it here so the bitmap uptodate bit
  755. * get set with buffer lock held.
  756. */
  757. set_bitmap_uptodate(bh[i]);
  758. bh[i]->b_end_io = end_buffer_read_sync;
  759. submit_bh(READ, bh[i]);
  760. mb_debug("read bitmap for group %u\n", first_group + i);
  761. }
  762. /* wait for I/O completion */
  763. for (i = 0; i < groups_per_page && bh[i]; i++)
  764. wait_on_buffer(bh[i]);
  765. err = -EIO;
  766. for (i = 0; i < groups_per_page && bh[i]; i++)
  767. if (!buffer_uptodate(bh[i]))
  768. goto out;
  769. err = 0;
  770. first_block = page->index * blocks_per_page;
  771. /* init the page */
  772. memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
  773. for (i = 0; i < blocks_per_page; i++) {
  774. int group;
  775. struct ext4_group_info *grinfo;
  776. group = (first_block + i) >> 1;
  777. if (group >= ngroups)
  778. break;
  779. /*
  780. * data carry information regarding this
  781. * particular group in the format specified
  782. * above
  783. *
  784. */
  785. data = page_address(page) + (i * blocksize);
  786. bitmap = bh[group - first_group]->b_data;
  787. /*
  788. * We place the buddy block and bitmap block
  789. * close together
  790. */
  791. if ((first_block + i) & 1) {
  792. /* this is block of buddy */
  793. BUG_ON(incore == NULL);
  794. mb_debug("put buddy for group %u in page %lu/%x\n",
  795. group, page->index, i * blocksize);
  796. grinfo = ext4_get_group_info(sb, group);
  797. grinfo->bb_fragments = 0;
  798. memset(grinfo->bb_counters, 0,
  799. sizeof(unsigned short)*(sb->s_blocksize_bits+2));
  800. /*
  801. * incore got set to the group block bitmap below
  802. */
  803. ext4_lock_group(sb, group);
  804. ext4_mb_generate_buddy(sb, data, incore, group);
  805. ext4_unlock_group(sb, group);
  806. incore = NULL;
  807. } else {
  808. /* this is block of bitmap */
  809. BUG_ON(incore != NULL);
  810. mb_debug("put bitmap for group %u in page %lu/%x\n",
  811. group, page->index, i * blocksize);
  812. /* see comments in ext4_mb_put_pa() */
  813. ext4_lock_group(sb, group);
  814. memcpy(data, bitmap, blocksize);
  815. /* mark all preallocated blks used in in-core bitmap */
  816. ext4_mb_generate_from_pa(sb, data, group);
  817. ext4_mb_generate_from_freelist(sb, data, group);
  818. ext4_unlock_group(sb, group);
  819. /* set incore so that the buddy information can be
  820. * generated using this
  821. */
  822. incore = data;
  823. }
  824. }
  825. SetPageUptodate(page);
  826. out:
  827. if (bh) {
  828. for (i = 0; i < groups_per_page && bh[i]; i++)
  829. brelse(bh[i]);
  830. if (bh != &bhs)
  831. kfree(bh);
  832. }
  833. return err;
  834. }
  835. static noinline_for_stack int
  836. ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
  837. struct ext4_buddy *e4b)
  838. {
  839. int blocks_per_page;
  840. int block;
  841. int pnum;
  842. int poff;
  843. struct page *page;
  844. int ret;
  845. struct ext4_group_info *grp;
  846. struct ext4_sb_info *sbi = EXT4_SB(sb);
  847. struct inode *inode = sbi->s_buddy_cache;
  848. mb_debug("load group %u\n", group);
  849. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  850. grp = ext4_get_group_info(sb, group);
  851. e4b->bd_blkbits = sb->s_blocksize_bits;
  852. e4b->bd_info = ext4_get_group_info(sb, group);
  853. e4b->bd_sb = sb;
  854. e4b->bd_group = group;
  855. e4b->bd_buddy_page = NULL;
  856. e4b->bd_bitmap_page = NULL;
  857. e4b->alloc_semp = &grp->alloc_sem;
  858. /* Take the read lock on the group alloc
  859. * sem. This would make sure a parallel
  860. * ext4_mb_init_group happening on other
  861. * groups mapped by the page is blocked
  862. * till we are done with allocation
  863. */
  864. down_read(e4b->alloc_semp);
  865. /*
  866. * the buddy cache inode stores the block bitmap
  867. * and buddy information in consecutive blocks.
  868. * So for each group we need two blocks.
  869. */
  870. block = group * 2;
  871. pnum = block / blocks_per_page;
  872. poff = block % blocks_per_page;
  873. /* we could use find_or_create_page(), but it locks page
  874. * what we'd like to avoid in fast path ... */
  875. page = find_get_page(inode->i_mapping, pnum);
  876. if (page == NULL || !PageUptodate(page)) {
  877. if (page)
  878. /*
  879. * drop the page reference and try
  880. * to get the page with lock. If we
  881. * are not uptodate that implies
  882. * somebody just created the page but
  883. * is yet to initialize the same. So
  884. * wait for it to initialize.
  885. */
  886. page_cache_release(page);
  887. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  888. if (page) {
  889. BUG_ON(page->mapping != inode->i_mapping);
  890. if (!PageUptodate(page)) {
  891. ret = ext4_mb_init_cache(page, NULL);
  892. if (ret) {
  893. unlock_page(page);
  894. goto err;
  895. }
  896. mb_cmp_bitmaps(e4b, page_address(page) +
  897. (poff * sb->s_blocksize));
  898. }
  899. unlock_page(page);
  900. }
  901. }
  902. if (page == NULL || !PageUptodate(page)) {
  903. ret = -EIO;
  904. goto err;
  905. }
  906. e4b->bd_bitmap_page = page;
  907. e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
  908. mark_page_accessed(page);
  909. block++;
  910. pnum = block / blocks_per_page;
  911. poff = block % blocks_per_page;
  912. page = find_get_page(inode->i_mapping, pnum);
  913. if (page == NULL || !PageUptodate(page)) {
  914. if (page)
  915. page_cache_release(page);
  916. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  917. if (page) {
  918. BUG_ON(page->mapping != inode->i_mapping);
  919. if (!PageUptodate(page)) {
  920. ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
  921. if (ret) {
  922. unlock_page(page);
  923. goto err;
  924. }
  925. }
  926. unlock_page(page);
  927. }
  928. }
  929. if (page == NULL || !PageUptodate(page)) {
  930. ret = -EIO;
  931. goto err;
  932. }
  933. e4b->bd_buddy_page = page;
  934. e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
  935. mark_page_accessed(page);
  936. BUG_ON(e4b->bd_bitmap_page == NULL);
  937. BUG_ON(e4b->bd_buddy_page == NULL);
  938. return 0;
  939. err:
  940. if (e4b->bd_bitmap_page)
  941. page_cache_release(e4b->bd_bitmap_page);
  942. if (e4b->bd_buddy_page)
  943. page_cache_release(e4b->bd_buddy_page);
  944. e4b->bd_buddy = NULL;
  945. e4b->bd_bitmap = NULL;
  946. /* Done with the buddy cache */
  947. up_read(e4b->alloc_semp);
  948. return ret;
  949. }
  950. static void ext4_mb_release_desc(struct ext4_buddy *e4b)
  951. {
  952. if (e4b->bd_bitmap_page)
  953. page_cache_release(e4b->bd_bitmap_page);
  954. if (e4b->bd_buddy_page)
  955. page_cache_release(e4b->bd_buddy_page);
  956. /* Done with the buddy cache */
  957. if (e4b->alloc_semp)
  958. up_read(e4b->alloc_semp);
  959. }
  960. static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
  961. {
  962. int order = 1;
  963. void *bb;
  964. BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
  965. BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
  966. bb = EXT4_MB_BUDDY(e4b);
  967. while (order <= e4b->bd_blkbits + 1) {
  968. block = block >> 1;
  969. if (!mb_test_bit(block, bb)) {
  970. /* this block is part of buddy of order 'order' */
  971. return order;
  972. }
  973. bb += 1 << (e4b->bd_blkbits - order);
  974. order++;
  975. }
  976. return 0;
  977. }
  978. static void mb_clear_bits(void *bm, int cur, int len)
  979. {
  980. __u32 *addr;
  981. len = cur + len;
  982. while (cur < len) {
  983. if ((cur & 31) == 0 && (len - cur) >= 32) {
  984. /* fast path: clear whole word at once */
  985. addr = bm + (cur >> 3);
  986. *addr = 0;
  987. cur += 32;
  988. continue;
  989. }
  990. mb_clear_bit(cur, bm);
  991. cur++;
  992. }
  993. }
  994. static void mb_set_bits(void *bm, int cur, int len)
  995. {
  996. __u32 *addr;
  997. len = cur + len;
  998. while (cur < len) {
  999. if ((cur & 31) == 0 && (len - cur) >= 32) {
  1000. /* fast path: set whole word at once */
  1001. addr = bm + (cur >> 3);
  1002. *addr = 0xffffffff;
  1003. cur += 32;
  1004. continue;
  1005. }
  1006. mb_set_bit(cur, bm);
  1007. cur++;
  1008. }
  1009. }
  1010. static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
  1011. int first, int count)
  1012. {
  1013. int block = 0;
  1014. int max = 0;
  1015. int order;
  1016. void *buddy;
  1017. void *buddy2;
  1018. struct super_block *sb = e4b->bd_sb;
  1019. BUG_ON(first + count > (sb->s_blocksize << 3));
  1020. assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
  1021. mb_check_buddy(e4b);
  1022. mb_free_blocks_double(inode, e4b, first, count);
  1023. e4b->bd_info->bb_free += count;
  1024. if (first < e4b->bd_info->bb_first_free)
  1025. e4b->bd_info->bb_first_free = first;
  1026. /* let's maintain fragments counter */
  1027. if (first != 0)
  1028. block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
  1029. if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
  1030. max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
  1031. if (block && max)
  1032. e4b->bd_info->bb_fragments--;
  1033. else if (!block && !max)
  1034. e4b->bd_info->bb_fragments++;
  1035. /* let's maintain buddy itself */
  1036. while (count-- > 0) {
  1037. block = first++;
  1038. order = 0;
  1039. if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
  1040. ext4_fsblk_t blocknr;
  1041. blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
  1042. blocknr += block;
  1043. blocknr +=
  1044. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  1045. ext4_grp_locked_error(sb, e4b->bd_group,
  1046. __func__, "double-free of inode"
  1047. " %lu's block %llu(bit %u in group %u)",
  1048. inode ? inode->i_ino : 0, blocknr, block,
  1049. e4b->bd_group);
  1050. }
  1051. mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
  1052. e4b->bd_info->bb_counters[order]++;
  1053. /* start of the buddy */
  1054. buddy = mb_find_buddy(e4b, order, &max);
  1055. do {
  1056. block &= ~1UL;
  1057. if (mb_test_bit(block, buddy) ||
  1058. mb_test_bit(block + 1, buddy))
  1059. break;
  1060. /* both the buddies are free, try to coalesce them */
  1061. buddy2 = mb_find_buddy(e4b, order + 1, &max);
  1062. if (!buddy2)
  1063. break;
  1064. if (order > 0) {
  1065. /* for special purposes, we don't set
  1066. * free bits in bitmap */
  1067. mb_set_bit(block, buddy);
  1068. mb_set_bit(block + 1, buddy);
  1069. }
  1070. e4b->bd_info->bb_counters[order]--;
  1071. e4b->bd_info->bb_counters[order]--;
  1072. block = block >> 1;
  1073. order++;
  1074. e4b->bd_info->bb_counters[order]++;
  1075. mb_clear_bit(block, buddy2);
  1076. buddy = buddy2;
  1077. } while (1);
  1078. }
  1079. mb_check_buddy(e4b);
  1080. }
  1081. static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
  1082. int needed, struct ext4_free_extent *ex)
  1083. {
  1084. int next = block;
  1085. int max;
  1086. int ord;
  1087. void *buddy;
  1088. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  1089. BUG_ON(ex == NULL);
  1090. buddy = mb_find_buddy(e4b, order, &max);
  1091. BUG_ON(buddy == NULL);
  1092. BUG_ON(block >= max);
  1093. if (mb_test_bit(block, buddy)) {
  1094. ex->fe_len = 0;
  1095. ex->fe_start = 0;
  1096. ex->fe_group = 0;
  1097. return 0;
  1098. }
  1099. /* FIXME dorp order completely ? */
  1100. if (likely(order == 0)) {
  1101. /* find actual order */
  1102. order = mb_find_order_for_block(e4b, block);
  1103. block = block >> order;
  1104. }
  1105. ex->fe_len = 1 << order;
  1106. ex->fe_start = block << order;
  1107. ex->fe_group = e4b->bd_group;
  1108. /* calc difference from given start */
  1109. next = next - ex->fe_start;
  1110. ex->fe_len -= next;
  1111. ex->fe_start += next;
  1112. while (needed > ex->fe_len &&
  1113. (buddy = mb_find_buddy(e4b, order, &max))) {
  1114. if (block + 1 >= max)
  1115. break;
  1116. next = (block + 1) * (1 << order);
  1117. if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
  1118. break;
  1119. ord = mb_find_order_for_block(e4b, next);
  1120. order = ord;
  1121. block = next >> order;
  1122. ex->fe_len += 1 << order;
  1123. }
  1124. BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
  1125. return ex->fe_len;
  1126. }
  1127. static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
  1128. {
  1129. int ord;
  1130. int mlen = 0;
  1131. int max = 0;
  1132. int cur;
  1133. int start = ex->fe_start;
  1134. int len = ex->fe_len;
  1135. unsigned ret = 0;
  1136. int len0 = len;
  1137. void *buddy;
  1138. BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
  1139. BUG_ON(e4b->bd_group != ex->fe_group);
  1140. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  1141. mb_check_buddy(e4b);
  1142. mb_mark_used_double(e4b, start, len);
  1143. e4b->bd_info->bb_free -= len;
  1144. if (e4b->bd_info->bb_first_free == start)
  1145. e4b->bd_info->bb_first_free += len;
  1146. /* let's maintain fragments counter */
  1147. if (start != 0)
  1148. mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
  1149. if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
  1150. max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
  1151. if (mlen && max)
  1152. e4b->bd_info->bb_fragments++;
  1153. else if (!mlen && !max)
  1154. e4b->bd_info->bb_fragments--;
  1155. /* let's maintain buddy itself */
  1156. while (len) {
  1157. ord = mb_find_order_for_block(e4b, start);
  1158. if (((start >> ord) << ord) == start && len >= (1 << ord)) {
  1159. /* the whole chunk may be allocated at once! */
  1160. mlen = 1 << ord;
  1161. buddy = mb_find_buddy(e4b, ord, &max);
  1162. BUG_ON((start >> ord) >= max);
  1163. mb_set_bit(start >> ord, buddy);
  1164. e4b->bd_info->bb_counters[ord]--;
  1165. start += mlen;
  1166. len -= mlen;
  1167. BUG_ON(len < 0);
  1168. continue;
  1169. }
  1170. /* store for history */
  1171. if (ret == 0)
  1172. ret = len | (ord << 16);
  1173. /* we have to split large buddy */
  1174. BUG_ON(ord <= 0);
  1175. buddy = mb_find_buddy(e4b, ord, &max);
  1176. mb_set_bit(start >> ord, buddy);
  1177. e4b->bd_info->bb_counters[ord]--;
  1178. ord--;
  1179. cur = (start >> ord) & ~1U;
  1180. buddy = mb_find_buddy(e4b, ord, &max);
  1181. mb_clear_bit(cur, buddy);
  1182. mb_clear_bit(cur + 1, buddy);
  1183. e4b->bd_info->bb_counters[ord]++;
  1184. e4b->bd_info->bb_counters[ord]++;
  1185. }
  1186. mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
  1187. mb_check_buddy(e4b);
  1188. return ret;
  1189. }
  1190. /*
  1191. * Must be called under group lock!
  1192. */
  1193. static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
  1194. struct ext4_buddy *e4b)
  1195. {
  1196. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1197. int ret;
  1198. BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
  1199. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1200. ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
  1201. ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
  1202. ret = mb_mark_used(e4b, &ac->ac_b_ex);
  1203. /* preallocation can change ac_b_ex, thus we store actually
  1204. * allocated blocks for history */
  1205. ac->ac_f_ex = ac->ac_b_ex;
  1206. ac->ac_status = AC_STATUS_FOUND;
  1207. ac->ac_tail = ret & 0xffff;
  1208. ac->ac_buddy = ret >> 16;
  1209. /*
  1210. * take the page reference. We want the page to be pinned
  1211. * so that we don't get a ext4_mb_init_cache_call for this
  1212. * group until we update the bitmap. That would mean we
  1213. * double allocate blocks. The reference is dropped
  1214. * in ext4_mb_release_context
  1215. */
  1216. ac->ac_bitmap_page = e4b->bd_bitmap_page;
  1217. get_page(ac->ac_bitmap_page);
  1218. ac->ac_buddy_page = e4b->bd_buddy_page;
  1219. get_page(ac->ac_buddy_page);
  1220. /* on allocation we use ac to track the held semaphore */
  1221. ac->alloc_semp = e4b->alloc_semp;
  1222. e4b->alloc_semp = NULL;
  1223. /* store last allocated for subsequent stream allocation */
  1224. if ((ac->ac_flags & EXT4_MB_HINT_DATA)) {
  1225. spin_lock(&sbi->s_md_lock);
  1226. sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
  1227. sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
  1228. spin_unlock(&sbi->s_md_lock);
  1229. }
  1230. }
  1231. /*
  1232. * regular allocator, for general purposes allocation
  1233. */
  1234. static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
  1235. struct ext4_buddy *e4b,
  1236. int finish_group)
  1237. {
  1238. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1239. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1240. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1241. struct ext4_free_extent ex;
  1242. int max;
  1243. if (ac->ac_status == AC_STATUS_FOUND)
  1244. return;
  1245. /*
  1246. * We don't want to scan for a whole year
  1247. */
  1248. if (ac->ac_found > sbi->s_mb_max_to_scan &&
  1249. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1250. ac->ac_status = AC_STATUS_BREAK;
  1251. return;
  1252. }
  1253. /*
  1254. * Haven't found good chunk so far, let's continue
  1255. */
  1256. if (bex->fe_len < gex->fe_len)
  1257. return;
  1258. if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
  1259. && bex->fe_group == e4b->bd_group) {
  1260. /* recheck chunk's availability - we don't know
  1261. * when it was found (within this lock-unlock
  1262. * period or not) */
  1263. max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
  1264. if (max >= gex->fe_len) {
  1265. ext4_mb_use_best_found(ac, e4b);
  1266. return;
  1267. }
  1268. }
  1269. }
  1270. /*
  1271. * The routine checks whether found extent is good enough. If it is,
  1272. * then the extent gets marked used and flag is set to the context
  1273. * to stop scanning. Otherwise, the extent is compared with the
  1274. * previous found extent and if new one is better, then it's stored
  1275. * in the context. Later, the best found extent will be used, if
  1276. * mballoc can't find good enough extent.
  1277. *
  1278. * FIXME: real allocation policy is to be designed yet!
  1279. */
  1280. static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
  1281. struct ext4_free_extent *ex,
  1282. struct ext4_buddy *e4b)
  1283. {
  1284. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1285. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1286. BUG_ON(ex->fe_len <= 0);
  1287. BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1288. BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1289. BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
  1290. ac->ac_found++;
  1291. /*
  1292. * The special case - take what you catch first
  1293. */
  1294. if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1295. *bex = *ex;
  1296. ext4_mb_use_best_found(ac, e4b);
  1297. return;
  1298. }
  1299. /*
  1300. * Let's check whether the chuck is good enough
  1301. */
  1302. if (ex->fe_len == gex->fe_len) {
  1303. *bex = *ex;
  1304. ext4_mb_use_best_found(ac, e4b);
  1305. return;
  1306. }
  1307. /*
  1308. * If this is first found extent, just store it in the context
  1309. */
  1310. if (bex->fe_len == 0) {
  1311. *bex = *ex;
  1312. return;
  1313. }
  1314. /*
  1315. * If new found extent is better, store it in the context
  1316. */
  1317. if (bex->fe_len < gex->fe_len) {
  1318. /* if the request isn't satisfied, any found extent
  1319. * larger than previous best one is better */
  1320. if (ex->fe_len > bex->fe_len)
  1321. *bex = *ex;
  1322. } else if (ex->fe_len > gex->fe_len) {
  1323. /* if the request is satisfied, then we try to find
  1324. * an extent that still satisfy the request, but is
  1325. * smaller than previous one */
  1326. if (ex->fe_len < bex->fe_len)
  1327. *bex = *ex;
  1328. }
  1329. ext4_mb_check_limits(ac, e4b, 0);
  1330. }
  1331. static noinline_for_stack
  1332. int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
  1333. struct ext4_buddy *e4b)
  1334. {
  1335. struct ext4_free_extent ex = ac->ac_b_ex;
  1336. ext4_group_t group = ex.fe_group;
  1337. int max;
  1338. int err;
  1339. BUG_ON(ex.fe_len <= 0);
  1340. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1341. if (err)
  1342. return err;
  1343. ext4_lock_group(ac->ac_sb, group);
  1344. max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
  1345. if (max > 0) {
  1346. ac->ac_b_ex = ex;
  1347. ext4_mb_use_best_found(ac, e4b);
  1348. }
  1349. ext4_unlock_group(ac->ac_sb, group);
  1350. ext4_mb_release_desc(e4b);
  1351. return 0;
  1352. }
  1353. static noinline_for_stack
  1354. int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
  1355. struct ext4_buddy *e4b)
  1356. {
  1357. ext4_group_t group = ac->ac_g_ex.fe_group;
  1358. int max;
  1359. int err;
  1360. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1361. struct ext4_super_block *es = sbi->s_es;
  1362. struct ext4_free_extent ex;
  1363. if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
  1364. return 0;
  1365. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1366. if (err)
  1367. return err;
  1368. ext4_lock_group(ac->ac_sb, group);
  1369. max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
  1370. ac->ac_g_ex.fe_len, &ex);
  1371. if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
  1372. ext4_fsblk_t start;
  1373. start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) +
  1374. ex.fe_start + le32_to_cpu(es->s_first_data_block);
  1375. /* use do_div to get remainder (would be 64-bit modulo) */
  1376. if (do_div(start, sbi->s_stripe) == 0) {
  1377. ac->ac_found++;
  1378. ac->ac_b_ex = ex;
  1379. ext4_mb_use_best_found(ac, e4b);
  1380. }
  1381. } else if (max >= ac->ac_g_ex.fe_len) {
  1382. BUG_ON(ex.fe_len <= 0);
  1383. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1384. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1385. ac->ac_found++;
  1386. ac->ac_b_ex = ex;
  1387. ext4_mb_use_best_found(ac, e4b);
  1388. } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
  1389. /* Sometimes, caller may want to merge even small
  1390. * number of blocks to an existing extent */
  1391. BUG_ON(ex.fe_len <= 0);
  1392. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1393. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1394. ac->ac_found++;
  1395. ac->ac_b_ex = ex;
  1396. ext4_mb_use_best_found(ac, e4b);
  1397. }
  1398. ext4_unlock_group(ac->ac_sb, group);
  1399. ext4_mb_release_desc(e4b);
  1400. return 0;
  1401. }
  1402. /*
  1403. * The routine scans buddy structures (not bitmap!) from given order
  1404. * to max order and tries to find big enough chunk to satisfy the req
  1405. */
  1406. static noinline_for_stack
  1407. void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
  1408. struct ext4_buddy *e4b)
  1409. {
  1410. struct super_block *sb = ac->ac_sb;
  1411. struct ext4_group_info *grp = e4b->bd_info;
  1412. void *buddy;
  1413. int i;
  1414. int k;
  1415. int max;
  1416. BUG_ON(ac->ac_2order <= 0);
  1417. for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
  1418. if (grp->bb_counters[i] == 0)
  1419. continue;
  1420. buddy = mb_find_buddy(e4b, i, &max);
  1421. BUG_ON(buddy == NULL);
  1422. k = mb_find_next_zero_bit(buddy, max, 0);
  1423. BUG_ON(k >= max);
  1424. ac->ac_found++;
  1425. ac->ac_b_ex.fe_len = 1 << i;
  1426. ac->ac_b_ex.fe_start = k << i;
  1427. ac->ac_b_ex.fe_group = e4b->bd_group;
  1428. ext4_mb_use_best_found(ac, e4b);
  1429. BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
  1430. if (EXT4_SB(sb)->s_mb_stats)
  1431. atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
  1432. break;
  1433. }
  1434. }
  1435. /*
  1436. * The routine scans the group and measures all found extents.
  1437. * In order to optimize scanning, caller must pass number of
  1438. * free blocks in the group, so the routine can know upper limit.
  1439. */
  1440. static noinline_for_stack
  1441. void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
  1442. struct ext4_buddy *e4b)
  1443. {
  1444. struct super_block *sb = ac->ac_sb;
  1445. void *bitmap = EXT4_MB_BITMAP(e4b);
  1446. struct ext4_free_extent ex;
  1447. int i;
  1448. int free;
  1449. free = e4b->bd_info->bb_free;
  1450. BUG_ON(free <= 0);
  1451. i = e4b->bd_info->bb_first_free;
  1452. while (free && ac->ac_status == AC_STATUS_CONTINUE) {
  1453. i = mb_find_next_zero_bit(bitmap,
  1454. EXT4_BLOCKS_PER_GROUP(sb), i);
  1455. if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
  1456. /*
  1457. * IF we have corrupt bitmap, we won't find any
  1458. * free blocks even though group info says we
  1459. * we have free blocks
  1460. */
  1461. ext4_grp_locked_error(sb, e4b->bd_group,
  1462. __func__, "%d free blocks as per "
  1463. "group info. But bitmap says 0",
  1464. free);
  1465. break;
  1466. }
  1467. mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
  1468. BUG_ON(ex.fe_len <= 0);
  1469. if (free < ex.fe_len) {
  1470. ext4_grp_locked_error(sb, e4b->bd_group,
  1471. __func__, "%d free blocks as per "
  1472. "group info. But got %d blocks",
  1473. free, ex.fe_len);
  1474. /*
  1475. * The number of free blocks differs. This mostly
  1476. * indicate that the bitmap is corrupt. So exit
  1477. * without claiming the space.
  1478. */
  1479. break;
  1480. }
  1481. ext4_mb_measure_extent(ac, &ex, e4b);
  1482. i += ex.fe_len;
  1483. free -= ex.fe_len;
  1484. }
  1485. ext4_mb_check_limits(ac, e4b, 1);
  1486. }
  1487. /*
  1488. * This is a special case for storages like raid5
  1489. * we try to find stripe-aligned chunks for stripe-size requests
  1490. * XXX should do so at least for multiples of stripe size as well
  1491. */
  1492. static noinline_for_stack
  1493. void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
  1494. struct ext4_buddy *e4b)
  1495. {
  1496. struct super_block *sb = ac->ac_sb;
  1497. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1498. void *bitmap = EXT4_MB_BITMAP(e4b);
  1499. struct ext4_free_extent ex;
  1500. ext4_fsblk_t first_group_block;
  1501. ext4_fsblk_t a;
  1502. ext4_grpblk_t i;
  1503. int max;
  1504. BUG_ON(sbi->s_stripe == 0);
  1505. /* find first stripe-aligned block in group */
  1506. first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb)
  1507. + le32_to_cpu(sbi->s_es->s_first_data_block);
  1508. a = first_group_block + sbi->s_stripe - 1;
  1509. do_div(a, sbi->s_stripe);
  1510. i = (a * sbi->s_stripe) - first_group_block;
  1511. while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
  1512. if (!mb_test_bit(i, bitmap)) {
  1513. max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
  1514. if (max >= sbi->s_stripe) {
  1515. ac->ac_found++;
  1516. ac->ac_b_ex = ex;
  1517. ext4_mb_use_best_found(ac, e4b);
  1518. break;
  1519. }
  1520. }
  1521. i += sbi->s_stripe;
  1522. }
  1523. }
  1524. static int ext4_mb_good_group(struct ext4_allocation_context *ac,
  1525. ext4_group_t group, int cr)
  1526. {
  1527. unsigned free, fragments;
  1528. unsigned i, bits;
  1529. int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
  1530. struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
  1531. BUG_ON(cr < 0 || cr >= 4);
  1532. BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
  1533. free = grp->bb_free;
  1534. fragments = grp->bb_fragments;
  1535. if (free == 0)
  1536. return 0;
  1537. if (fragments == 0)
  1538. return 0;
  1539. switch (cr) {
  1540. case 0:
  1541. BUG_ON(ac->ac_2order == 0);
  1542. /* Avoid using the first bg of a flexgroup for data files */
  1543. if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
  1544. (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
  1545. ((group % flex_size) == 0))
  1546. return 0;
  1547. bits = ac->ac_sb->s_blocksize_bits + 1;
  1548. for (i = ac->ac_2order; i <= bits; i++)
  1549. if (grp->bb_counters[i] > 0)
  1550. return 1;
  1551. break;
  1552. case 1:
  1553. if ((free / fragments) >= ac->ac_g_ex.fe_len)
  1554. return 1;
  1555. break;
  1556. case 2:
  1557. if (free >= ac->ac_g_ex.fe_len)
  1558. return 1;
  1559. break;
  1560. case 3:
  1561. return 1;
  1562. default:
  1563. BUG();
  1564. }
  1565. return 0;
  1566. }
  1567. /*
  1568. * lock the group_info alloc_sem of all the groups
  1569. * belonging to the same buddy cache page. This
  1570. * make sure other parallel operation on the buddy
  1571. * cache doesn't happen whild holding the buddy cache
  1572. * lock
  1573. */
  1574. int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
  1575. {
  1576. int i;
  1577. int block, pnum;
  1578. int blocks_per_page;
  1579. int groups_per_page;
  1580. ext4_group_t ngroups = ext4_get_groups_count(sb);
  1581. ext4_group_t first_group;
  1582. struct ext4_group_info *grp;
  1583. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1584. /*
  1585. * the buddy cache inode stores the block bitmap
  1586. * and buddy information in consecutive blocks.
  1587. * So for each group we need two blocks.
  1588. */
  1589. block = group * 2;
  1590. pnum = block / blocks_per_page;
  1591. first_group = pnum * blocks_per_page / 2;
  1592. groups_per_page = blocks_per_page >> 1;
  1593. if (groups_per_page == 0)
  1594. groups_per_page = 1;
  1595. /* read all groups the page covers into the cache */
  1596. for (i = 0; i < groups_per_page; i++) {
  1597. if ((first_group + i) >= ngroups)
  1598. break;
  1599. grp = ext4_get_group_info(sb, first_group + i);
  1600. /* take all groups write allocation
  1601. * semaphore. This make sure there is
  1602. * no block allocation going on in any
  1603. * of that groups
  1604. */
  1605. down_write_nested(&grp->alloc_sem, i);
  1606. }
  1607. return i;
  1608. }
  1609. void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
  1610. ext4_group_t group, int locked_group)
  1611. {
  1612. int i;
  1613. int block, pnum;
  1614. int blocks_per_page;
  1615. ext4_group_t first_group;
  1616. struct ext4_group_info *grp;
  1617. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1618. /*
  1619. * the buddy cache inode stores the block bitmap
  1620. * and buddy information in consecutive blocks.
  1621. * So for each group we need two blocks.
  1622. */
  1623. block = group * 2;
  1624. pnum = block / blocks_per_page;
  1625. first_group = pnum * blocks_per_page / 2;
  1626. /* release locks on all the groups */
  1627. for (i = 0; i < locked_group; i++) {
  1628. grp = ext4_get_group_info(sb, first_group + i);
  1629. /* take all groups write allocation
  1630. * semaphore. This make sure there is
  1631. * no block allocation going on in any
  1632. * of that groups
  1633. */
  1634. up_write(&grp->alloc_sem);
  1635. }
  1636. }
  1637. static noinline_for_stack
  1638. int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
  1639. {
  1640. int ret;
  1641. void *bitmap;
  1642. int blocks_per_page;
  1643. int block, pnum, poff;
  1644. int num_grp_locked = 0;
  1645. struct ext4_group_info *this_grp;
  1646. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1647. struct inode *inode = sbi->s_buddy_cache;
  1648. struct page *page = NULL, *bitmap_page = NULL;
  1649. mb_debug("init group %lu\n", group);
  1650. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1651. this_grp = ext4_get_group_info(sb, group);
  1652. /*
  1653. * This ensures we don't add group
  1654. * to this buddy cache via resize
  1655. */
  1656. num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
  1657. if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
  1658. /*
  1659. * somebody initialized the group
  1660. * return without doing anything
  1661. */
  1662. ret = 0;
  1663. goto err;
  1664. }
  1665. /*
  1666. * the buddy cache inode stores the block bitmap
  1667. * and buddy information in consecutive blocks.
  1668. * So for each group we need two blocks.
  1669. */
  1670. block = group * 2;
  1671. pnum = block / blocks_per_page;
  1672. poff = block % blocks_per_page;
  1673. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1674. if (page) {
  1675. BUG_ON(page->mapping != inode->i_mapping);
  1676. ret = ext4_mb_init_cache(page, NULL);
  1677. if (ret) {
  1678. unlock_page(page);
  1679. goto err;
  1680. }
  1681. unlock_page(page);
  1682. }
  1683. if (page == NULL || !PageUptodate(page)) {
  1684. ret = -EIO;
  1685. goto err;
  1686. }
  1687. mark_page_accessed(page);
  1688. bitmap_page = page;
  1689. bitmap = page_address(page) + (poff * sb->s_blocksize);
  1690. /* init buddy cache */
  1691. block++;
  1692. pnum = block / blocks_per_page;
  1693. poff = block % blocks_per_page;
  1694. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1695. if (page == bitmap_page) {
  1696. /*
  1697. * If both the bitmap and buddy are in
  1698. * the same page we don't need to force
  1699. * init the buddy
  1700. */
  1701. unlock_page(page);
  1702. } else if (page) {
  1703. BUG_ON(page->mapping != inode->i_mapping);
  1704. ret = ext4_mb_init_cache(page, bitmap);
  1705. if (ret) {
  1706. unlock_page(page);
  1707. goto err;
  1708. }
  1709. unlock_page(page);
  1710. }
  1711. if (page == NULL || !PageUptodate(page)) {
  1712. ret = -EIO;
  1713. goto err;
  1714. }
  1715. mark_page_accessed(page);
  1716. err:
  1717. ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
  1718. if (bitmap_page)
  1719. page_cache_release(bitmap_page);
  1720. if (page)
  1721. page_cache_release(page);
  1722. return ret;
  1723. }
  1724. static noinline_for_stack int
  1725. ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
  1726. {
  1727. ext4_group_t ngroups, group, i;
  1728. int cr;
  1729. int err = 0;
  1730. int bsbits;
  1731. struct ext4_sb_info *sbi;
  1732. struct super_block *sb;
  1733. struct ext4_buddy e4b;
  1734. loff_t size, isize;
  1735. sb = ac->ac_sb;
  1736. sbi = EXT4_SB(sb);
  1737. ngroups = ext4_get_groups_count(sb);
  1738. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1739. /* first, try the goal */
  1740. err = ext4_mb_find_by_goal(ac, &e4b);
  1741. if (err || ac->ac_status == AC_STATUS_FOUND)
  1742. goto out;
  1743. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  1744. goto out;
  1745. /*
  1746. * ac->ac2_order is set only if the fe_len is a power of 2
  1747. * if ac2_order is set we also set criteria to 0 so that we
  1748. * try exact allocation using buddy.
  1749. */
  1750. i = fls(ac->ac_g_ex.fe_len);
  1751. ac->ac_2order = 0;
  1752. /*
  1753. * We search using buddy data only if the order of the request
  1754. * is greater than equal to the sbi_s_mb_order2_reqs
  1755. * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
  1756. */
  1757. if (i >= sbi->s_mb_order2_reqs) {
  1758. /*
  1759. * This should tell if fe_len is exactly power of 2
  1760. */
  1761. if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
  1762. ac->ac_2order = i - 1;
  1763. }
  1764. bsbits = ac->ac_sb->s_blocksize_bits;
  1765. /* if stream allocation is enabled, use global goal */
  1766. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  1767. isize = i_size_read(ac->ac_inode) >> bsbits;
  1768. if (size < isize)
  1769. size = isize;
  1770. if (size < sbi->s_mb_stream_request &&
  1771. (ac->ac_flags & EXT4_MB_HINT_DATA)) {
  1772. /* TBD: may be hot point */
  1773. spin_lock(&sbi->s_md_lock);
  1774. ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
  1775. ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
  1776. spin_unlock(&sbi->s_md_lock);
  1777. }
  1778. /* Let's just scan groups to find more-less suitable blocks */
  1779. cr = ac->ac_2order ? 0 : 1;
  1780. /*
  1781. * cr == 0 try to get exact allocation,
  1782. * cr == 3 try to get anything
  1783. */
  1784. repeat:
  1785. for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
  1786. ac->ac_criteria = cr;
  1787. /*
  1788. * searching for the right group start
  1789. * from the goal value specified
  1790. */
  1791. group = ac->ac_g_ex.fe_group;
  1792. for (i = 0; i < ngroups; group++, i++) {
  1793. struct ext4_group_info *grp;
  1794. struct ext4_group_desc *desc;
  1795. if (group == ngroups)
  1796. group = 0;
  1797. /* quick check to skip empty groups */
  1798. grp = ext4_get_group_info(sb, group);
  1799. if (grp->bb_free == 0)
  1800. continue;
  1801. /*
  1802. * if the group is already init we check whether it is
  1803. * a good group and if not we don't load the buddy
  1804. */
  1805. if (EXT4_MB_GRP_NEED_INIT(grp)) {
  1806. /*
  1807. * we need full data about the group
  1808. * to make a good selection
  1809. */
  1810. err = ext4_mb_init_group(sb, group);
  1811. if (err)
  1812. goto out;
  1813. }
  1814. /*
  1815. * If the particular group doesn't satisfy our
  1816. * criteria we continue with the next group
  1817. */
  1818. if (!ext4_mb_good_group(ac, group, cr))
  1819. continue;
  1820. err = ext4_mb_load_buddy(sb, group, &e4b);
  1821. if (err)
  1822. goto out;
  1823. ext4_lock_group(sb, group);
  1824. if (!ext4_mb_good_group(ac, group, cr)) {
  1825. /* someone did allocation from this group */
  1826. ext4_unlock_group(sb, group);
  1827. ext4_mb_release_desc(&e4b);
  1828. continue;
  1829. }
  1830. ac->ac_groups_scanned++;
  1831. desc = ext4_get_group_desc(sb, group, NULL);
  1832. if (cr == 0)
  1833. ext4_mb_simple_scan_group(ac, &e4b);
  1834. else if (cr == 1 &&
  1835. ac->ac_g_ex.fe_len == sbi->s_stripe)
  1836. ext4_mb_scan_aligned(ac, &e4b);
  1837. else
  1838. ext4_mb_complex_scan_group(ac, &e4b);
  1839. ext4_unlock_group(sb, group);
  1840. ext4_mb_release_desc(&e4b);
  1841. if (ac->ac_status != AC_STATUS_CONTINUE)
  1842. break;
  1843. }
  1844. }
  1845. if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
  1846. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1847. /*
  1848. * We've been searching too long. Let's try to allocate
  1849. * the best chunk we've found so far
  1850. */
  1851. ext4_mb_try_best_found(ac, &e4b);
  1852. if (ac->ac_status != AC_STATUS_FOUND) {
  1853. /*
  1854. * Someone more lucky has already allocated it.
  1855. * The only thing we can do is just take first
  1856. * found block(s)
  1857. printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
  1858. */
  1859. ac->ac_b_ex.fe_group = 0;
  1860. ac->ac_b_ex.fe_start = 0;
  1861. ac->ac_b_ex.fe_len = 0;
  1862. ac->ac_status = AC_STATUS_CONTINUE;
  1863. ac->ac_flags |= EXT4_MB_HINT_FIRST;
  1864. cr = 3;
  1865. atomic_inc(&sbi->s_mb_lost_chunks);
  1866. goto repeat;
  1867. }
  1868. }
  1869. out:
  1870. return err;
  1871. }
  1872. #ifdef EXT4_MB_HISTORY
  1873. struct ext4_mb_proc_session {
  1874. struct ext4_mb_history *history;
  1875. struct super_block *sb;
  1876. int start;
  1877. int max;
  1878. };
  1879. static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session *s,
  1880. struct ext4_mb_history *hs,
  1881. int first)
  1882. {
  1883. if (hs == s->history + s->max)
  1884. hs = s->history;
  1885. if (!first && hs == s->history + s->start)
  1886. return NULL;
  1887. while (hs->orig.fe_len == 0) {
  1888. hs++;
  1889. if (hs == s->history + s->max)
  1890. hs = s->history;
  1891. if (hs == s->history + s->start)
  1892. return NULL;
  1893. }
  1894. return hs;
  1895. }
  1896. static void *ext4_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
  1897. {
  1898. struct ext4_mb_proc_session *s = seq->private;
  1899. struct ext4_mb_history *hs;
  1900. int l = *pos;
  1901. if (l == 0)
  1902. return SEQ_START_TOKEN;
  1903. hs = ext4_mb_history_skip_empty(s, s->history + s->start, 1);
  1904. if (!hs)
  1905. return NULL;
  1906. while (--l && (hs = ext4_mb_history_skip_empty(s, ++hs, 0)) != NULL);
  1907. return hs;
  1908. }
  1909. static void *ext4_mb_seq_history_next(struct seq_file *seq, void *v,
  1910. loff_t *pos)
  1911. {
  1912. struct ext4_mb_proc_session *s = seq->private;
  1913. struct ext4_mb_history *hs = v;
  1914. ++*pos;
  1915. if (v == SEQ_START_TOKEN)
  1916. return ext4_mb_history_skip_empty(s, s->history + s->start, 1);
  1917. else
  1918. return ext4_mb_history_skip_empty(s, ++hs, 0);
  1919. }
  1920. static int ext4_mb_seq_history_show(struct seq_file *seq, void *v)
  1921. {
  1922. char buf[25], buf2[25], buf3[25], *fmt;
  1923. struct ext4_mb_history *hs = v;
  1924. if (v == SEQ_START_TOKEN) {
  1925. seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
  1926. "%-5s %-2s %-5s %-5s %-5s %-6s\n",
  1927. "pid", "inode", "original", "goal", "result", "found",
  1928. "grps", "cr", "flags", "merge", "tail", "broken");
  1929. return 0;
  1930. }
  1931. if (hs->op == EXT4_MB_HISTORY_ALLOC) {
  1932. fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
  1933. "%-5u %-5s %-5u %-6u\n";
  1934. sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
  1935. hs->result.fe_start, hs->result.fe_len,
  1936. hs->result.fe_logical);
  1937. sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
  1938. hs->orig.fe_start, hs->orig.fe_len,
  1939. hs->orig.fe_logical);
  1940. sprintf(buf3, "%u/%d/%u@%u", hs->goal.fe_group,
  1941. hs->goal.fe_start, hs->goal.fe_len,
  1942. hs->goal.fe_logical);
  1943. seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
  1944. hs->found, hs->groups, hs->cr, hs->flags,
  1945. hs->merged ? "M" : "", hs->tail,
  1946. hs->buddy ? 1 << hs->buddy : 0);
  1947. } else if (hs->op == EXT4_MB_HISTORY_PREALLOC) {
  1948. fmt = "%-5u %-8u %-23s %-23s %-23s\n";
  1949. sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
  1950. hs->result.fe_start, hs->result.fe_len,
  1951. hs->result.fe_logical);
  1952. sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
  1953. hs->orig.fe_start, hs->orig.fe_len,
  1954. hs->orig.fe_logical);
  1955. seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
  1956. } else if (hs->op == EXT4_MB_HISTORY_DISCARD) {
  1957. sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
  1958. hs->result.fe_start, hs->result.fe_len);
  1959. seq_printf(seq, "%-5u %-8u %-23s discard\n",
  1960. hs->pid, hs->ino, buf2);
  1961. } else if (hs->op == EXT4_MB_HISTORY_FREE) {
  1962. sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
  1963. hs->result.fe_start, hs->result.fe_len);
  1964. seq_printf(seq, "%-5u %-8u %-23s free\n",
  1965. hs->pid, hs->ino, buf2);
  1966. }
  1967. return 0;
  1968. }
  1969. static void ext4_mb_seq_history_stop(struct seq_file *seq, void *v)
  1970. {
  1971. }
  1972. static struct seq_operations ext4_mb_seq_history_ops = {
  1973. .start = ext4_mb_seq_history_start,
  1974. .next = ext4_mb_seq_history_next,
  1975. .stop = ext4_mb_seq_history_stop,
  1976. .show = ext4_mb_seq_history_show,
  1977. };
  1978. static int ext4_mb_seq_history_open(struct inode *inode, struct file *file)
  1979. {
  1980. struct super_block *sb = PDE(inode)->data;
  1981. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1982. struct ext4_mb_proc_session *s;
  1983. int rc;
  1984. int size;
  1985. if (unlikely(sbi->s_mb_history == NULL))
  1986. return -ENOMEM;
  1987. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1988. if (s == NULL)
  1989. return -ENOMEM;
  1990. s->sb = sb;
  1991. size = sizeof(struct ext4_mb_history) * sbi->s_mb_history_max;
  1992. s->history = kmalloc(size, GFP_KERNEL);
  1993. if (s->history == NULL) {
  1994. kfree(s);
  1995. return -ENOMEM;
  1996. }
  1997. spin_lock(&sbi->s_mb_history_lock);
  1998. memcpy(s->history, sbi->s_mb_history, size);
  1999. s->max = sbi->s_mb_history_max;
  2000. s->start = sbi->s_mb_history_cur % s->max;
  2001. spin_unlock(&sbi->s_mb_history_lock);
  2002. rc = seq_open(file, &ext4_mb_seq_history_ops);
  2003. if (rc == 0) {
  2004. struct seq_file *m = (struct seq_file *)file->private_data;
  2005. m->private = s;
  2006. } else {
  2007. kfree(s->history);
  2008. kfree(s);
  2009. }
  2010. return rc;
  2011. }
  2012. static int ext4_mb_seq_history_release(struct inode *inode, struct file *file)
  2013. {
  2014. struct seq_file *seq = (struct seq_file *)file->private_data;
  2015. struct ext4_mb_proc_session *s = seq->private;
  2016. kfree(s->history);
  2017. kfree(s);
  2018. return seq_release(inode, file);
  2019. }
  2020. static ssize_t ext4_mb_seq_history_write(struct file *file,
  2021. const char __user *buffer,
  2022. size_t count, loff_t *ppos)
  2023. {
  2024. struct seq_file *seq = (struct seq_file *)file->private_data;
  2025. struct ext4_mb_proc_session *s = seq->private;
  2026. struct super_block *sb = s->sb;
  2027. char str[32];
  2028. int value;
  2029. if (count >= sizeof(str)) {
  2030. printk(KERN_ERR "EXT4-fs: %s string too long, max %u bytes\n",
  2031. "mb_history", (int)sizeof(str));
  2032. return -EOVERFLOW;
  2033. }
  2034. if (copy_from_user(str, buffer, count))
  2035. return -EFAULT;
  2036. value = simple_strtol(str, NULL, 0);
  2037. if (value < 0)
  2038. return -ERANGE;
  2039. EXT4_SB(sb)->s_mb_history_filter = value;
  2040. return count;
  2041. }
  2042. static struct file_operations ext4_mb_seq_history_fops = {
  2043. .owner = THIS_MODULE,
  2044. .open = ext4_mb_seq_history_open,
  2045. .read = seq_read,
  2046. .write = ext4_mb_seq_history_write,
  2047. .llseek = seq_lseek,
  2048. .release = ext4_mb_seq_history_release,
  2049. };
  2050. static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
  2051. {
  2052. struct super_block *sb = seq->private;
  2053. ext4_group_t group;
  2054. if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
  2055. return NULL;
  2056. group = *pos + 1;
  2057. return (void *) ((unsigned long) group);
  2058. }
  2059. static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
  2060. {
  2061. struct super_block *sb = seq->private;
  2062. ext4_group_t group;
  2063. ++*pos;
  2064. if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
  2065. return NULL;
  2066. group = *pos + 1;
  2067. return (void *) ((unsigned long) group);
  2068. }
  2069. static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
  2070. {
  2071. struct super_block *sb = seq->private;
  2072. ext4_group_t group = (ext4_group_t) ((unsigned long) v);
  2073. int i;
  2074. int err;
  2075. struct ext4_buddy e4b;
  2076. struct sg {
  2077. struct ext4_group_info info;
  2078. unsigned short counters[16];
  2079. } sg;
  2080. group--;
  2081. if (group == 0)
  2082. seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
  2083. "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
  2084. "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
  2085. "group", "free", "frags", "first",
  2086. "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
  2087. "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
  2088. i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
  2089. sizeof(struct ext4_group_info);
  2090. err = ext4_mb_load_buddy(sb, group, &e4b);
  2091. if (err) {
  2092. seq_printf(seq, "#%-5u: I/O error\n", group);
  2093. return 0;
  2094. }
  2095. ext4_lock_group(sb, group);
  2096. memcpy(&sg, ext4_get_group_info(sb, group), i);
  2097. ext4_unlock_group(sb, group);
  2098. ext4_mb_release_desc(&e4b);
  2099. seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
  2100. sg.info.bb_fragments, sg.info.bb_first_free);
  2101. for (i = 0; i <= 13; i++)
  2102. seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
  2103. sg.info.bb_counters[i] : 0);
  2104. seq_printf(seq, " ]\n");
  2105. return 0;
  2106. }
  2107. static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
  2108. {
  2109. }
  2110. static struct seq_operations ext4_mb_seq_groups_ops = {
  2111. .start = ext4_mb_seq_groups_start,
  2112. .next = ext4_mb_seq_groups_next,
  2113. .stop = ext4_mb_seq_groups_stop,
  2114. .show = ext4_mb_seq_groups_show,
  2115. };
  2116. static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
  2117. {
  2118. struct super_block *sb = PDE(inode)->data;
  2119. int rc;
  2120. rc = seq_open(file, &ext4_mb_seq_groups_ops);
  2121. if (rc == 0) {
  2122. struct seq_file *m = (struct seq_file *)file->private_data;
  2123. m->private = sb;
  2124. }
  2125. return rc;
  2126. }
  2127. static struct file_operations ext4_mb_seq_groups_fops = {
  2128. .owner = THIS_MODULE,
  2129. .open = ext4_mb_seq_groups_open,
  2130. .read = seq_read,
  2131. .llseek = seq_lseek,
  2132. .release = seq_release,
  2133. };
  2134. static void ext4_mb_history_release(struct super_block *sb)
  2135. {
  2136. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2137. if (sbi->s_proc != NULL) {
  2138. remove_proc_entry("mb_groups", sbi->s_proc);
  2139. if (sbi->s_mb_history_max)
  2140. remove_proc_entry("mb_history", sbi->s_proc);
  2141. }
  2142. kfree(sbi->s_mb_history);
  2143. }
  2144. static void ext4_mb_history_init(struct super_block *sb)
  2145. {
  2146. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2147. int i;
  2148. if (sbi->s_proc != NULL) {
  2149. if (sbi->s_mb_history_max)
  2150. proc_create_data("mb_history", S_IRUGO, sbi->s_proc,
  2151. &ext4_mb_seq_history_fops, sb);
  2152. proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
  2153. &ext4_mb_seq_groups_fops, sb);
  2154. }
  2155. sbi->s_mb_history_cur = 0;
  2156. spin_lock_init(&sbi->s_mb_history_lock);
  2157. i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history);
  2158. sbi->s_mb_history = i ? kzalloc(i, GFP_KERNEL) : NULL;
  2159. /* if we can't allocate history, then we simple won't use it */
  2160. }
  2161. static noinline_for_stack void
  2162. ext4_mb_store_history(struct ext4_allocation_context *ac)
  2163. {
  2164. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2165. struct ext4_mb_history h;
  2166. if (sbi->s_mb_history == NULL)
  2167. return;
  2168. if (!(ac->ac_op & sbi->s_mb_history_filter))
  2169. return;
  2170. h.op = ac->ac_op;
  2171. h.pid = current->pid;
  2172. h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
  2173. h.orig = ac->ac_o_ex;
  2174. h.result = ac->ac_b_ex;
  2175. h.flags = ac->ac_flags;
  2176. h.found = ac->ac_found;
  2177. h.groups = ac->ac_groups_scanned;
  2178. h.cr = ac->ac_criteria;
  2179. h.tail = ac->ac_tail;
  2180. h.buddy = ac->ac_buddy;
  2181. h.merged = 0;
  2182. if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
  2183. if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
  2184. ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
  2185. h.merged = 1;
  2186. h.goal = ac->ac_g_ex;
  2187. h.result = ac->ac_f_ex;
  2188. }
  2189. spin_lock(&sbi->s_mb_history_lock);
  2190. memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
  2191. if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
  2192. sbi->s_mb_history_cur = 0;
  2193. spin_unlock(&sbi->s_mb_history_lock);
  2194. }
  2195. #else
  2196. #define ext4_mb_history_release(sb)
  2197. #define ext4_mb_history_init(sb)
  2198. #endif
  2199. /* Create and initialize ext4_group_info data for the given group. */
  2200. int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
  2201. struct ext4_group_desc *desc)
  2202. {
  2203. int i, len;
  2204. int metalen = 0;
  2205. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2206. struct ext4_group_info **meta_group_info;
  2207. /*
  2208. * First check if this group is the first of a reserved block.
  2209. * If it's true, we have to allocate a new table of pointers
  2210. * to ext4_group_info structures
  2211. */
  2212. if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
  2213. metalen = sizeof(*meta_group_info) <<
  2214. EXT4_DESC_PER_BLOCK_BITS(sb);
  2215. meta_group_info = kmalloc(metalen, GFP_KERNEL);
  2216. if (meta_group_info == NULL) {
  2217. printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
  2218. "buddy group\n");
  2219. goto exit_meta_group_info;
  2220. }
  2221. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
  2222. meta_group_info;
  2223. }
  2224. /*
  2225. * calculate needed size. if change bb_counters size,
  2226. * don't forget about ext4_mb_generate_buddy()
  2227. */
  2228. len = offsetof(typeof(**meta_group_info),
  2229. bb_counters[sb->s_blocksize_bits + 2]);
  2230. meta_group_info =
  2231. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
  2232. i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
  2233. meta_group_info[i] = kzalloc(len, GFP_KERNEL);
  2234. if (meta_group_info[i] == NULL) {
  2235. printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
  2236. goto exit_group_info;
  2237. }
  2238. set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
  2239. &(meta_group_info[i]->bb_state));
  2240. /*
  2241. * initialize bb_free to be able to skip
  2242. * empty groups without initialization
  2243. */
  2244. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2245. meta_group_info[i]->bb_free =
  2246. ext4_free_blocks_after_init(sb, group, desc);
  2247. } else {
  2248. meta_group_info[i]->bb_free =
  2249. ext4_free_blks_count(sb, desc);
  2250. }
  2251. INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
  2252. init_rwsem(&meta_group_info[i]->alloc_sem);
  2253. meta_group_info[i]->bb_free_root.rb_node = NULL;;
  2254. #ifdef DOUBLE_CHECK
  2255. {
  2256. struct buffer_head *bh;
  2257. meta_group_info[i]->bb_bitmap =
  2258. kmalloc(sb->s_blocksize, GFP_KERNEL);
  2259. BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
  2260. bh = ext4_read_block_bitmap(sb, group);
  2261. BUG_ON(bh == NULL);
  2262. memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
  2263. sb->s_blocksize);
  2264. put_bh(bh);
  2265. }
  2266. #endif
  2267. return 0;
  2268. exit_group_info:
  2269. /* If a meta_group_info table has been allocated, release it now */
  2270. if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
  2271. kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
  2272. exit_meta_group_info:
  2273. return -ENOMEM;
  2274. } /* ext4_mb_add_groupinfo */
  2275. /*
  2276. * Update an existing group.
  2277. * This function is used for online resize
  2278. */
  2279. void ext4_mb_update_group_info(struct ext4_group_info *grp, ext4_grpblk_t add)
  2280. {
  2281. grp->bb_free += add;
  2282. }
  2283. static int ext4_mb_init_backend(struct super_block *sb)
  2284. {
  2285. ext4_group_t ngroups = ext4_get_groups_count(sb);
  2286. ext4_group_t i;
  2287. int metalen;
  2288. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2289. struct ext4_super_block *es = sbi->s_es;
  2290. int num_meta_group_infos;
  2291. int num_meta_group_infos_max;
  2292. int array_size;
  2293. struct ext4_group_info **meta_group_info;
  2294. struct ext4_group_desc *desc;
  2295. /* This is the number of blocks used by GDT */
  2296. num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
  2297. 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
  2298. /*
  2299. * This is the total number of blocks used by GDT including
  2300. * the number of reserved blocks for GDT.
  2301. * The s_group_info array is allocated with this value
  2302. * to allow a clean online resize without a complex
  2303. * manipulation of pointer.
  2304. * The drawback is the unused memory when no resize
  2305. * occurs but it's very low in terms of pages
  2306. * (see comments below)
  2307. * Need to handle this properly when META_BG resizing is allowed
  2308. */
  2309. num_meta_group_infos_max = num_meta_group_infos +
  2310. le16_to_cpu(es->s_reserved_gdt_blocks);
  2311. /*
  2312. * array_size is the size of s_group_info array. We round it
  2313. * to the next power of two because this approximation is done
  2314. * internally by kmalloc so we can have some more memory
  2315. * for free here (e.g. may be used for META_BG resize).
  2316. */
  2317. array_size = 1;
  2318. while (array_size < sizeof(*sbi->s_group_info) *
  2319. num_meta_group_infos_max)
  2320. array_size = array_size << 1;
  2321. /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
  2322. * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
  2323. * So a two level scheme suffices for now. */
  2324. sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
  2325. if (sbi->s_group_info == NULL) {
  2326. printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
  2327. return -ENOMEM;
  2328. }
  2329. sbi->s_buddy_cache = new_inode(sb);
  2330. if (sbi->s_buddy_cache == NULL) {
  2331. printk(KERN_ERR "EXT4-fs: can't get new inode\n");
  2332. goto err_freesgi;
  2333. }
  2334. EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
  2335. metalen = sizeof(*meta_group_info) << EXT4_DESC_PER_BLOCK_BITS(sb);
  2336. for (i = 0; i < num_meta_group_infos; i++) {
  2337. if ((i + 1) == num_meta_group_infos)
  2338. metalen = sizeof(*meta_group_info) *
  2339. (ngroups -
  2340. (i << EXT4_DESC_PER_BLOCK_BITS(sb)));
  2341. meta_group_info = kmalloc(metalen, GFP_KERNEL);
  2342. if (meta_group_info == NULL) {
  2343. printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
  2344. "buddy group\n");
  2345. goto err_freemeta;
  2346. }
  2347. sbi->s_group_info[i] = meta_group_info;
  2348. }
  2349. for (i = 0; i < ngroups; i++) {
  2350. desc = ext4_get_group_desc(sb, i, NULL);
  2351. if (desc == NULL) {
  2352. printk(KERN_ERR
  2353. "EXT4-fs: can't read descriptor %u\n", i);
  2354. goto err_freebuddy;
  2355. }
  2356. if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
  2357. goto err_freebuddy;
  2358. }
  2359. return 0;
  2360. err_freebuddy:
  2361. while (i-- > 0)
  2362. kfree(ext4_get_group_info(sb, i));
  2363. i = num_meta_group_infos;
  2364. err_freemeta:
  2365. while (i-- > 0)
  2366. kfree(sbi->s_group_info[i]);
  2367. iput(sbi->s_buddy_cache);
  2368. err_freesgi:
  2369. kfree(sbi->s_group_info);
  2370. return -ENOMEM;
  2371. }
  2372. int ext4_mb_init(struct super_block *sb, int needs_recovery)
  2373. {
  2374. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2375. unsigned i, j;
  2376. unsigned offset;
  2377. unsigned max;
  2378. int ret;
  2379. i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
  2380. sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
  2381. if (sbi->s_mb_offsets == NULL) {
  2382. return -ENOMEM;
  2383. }
  2384. i = (sb->s_blocksize_bits + 2) * sizeof(unsigned int);
  2385. sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
  2386. if (sbi->s_mb_maxs == NULL) {
  2387. kfree(sbi->s_mb_offsets);
  2388. return -ENOMEM;
  2389. }
  2390. /* order 0 is regular bitmap */
  2391. sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
  2392. sbi->s_mb_offsets[0] = 0;
  2393. i = 1;
  2394. offset = 0;
  2395. max = sb->s_blocksize << 2;
  2396. do {
  2397. sbi->s_mb_offsets[i] = offset;
  2398. sbi->s_mb_maxs[i] = max;
  2399. offset += 1 << (sb->s_blocksize_bits - i);
  2400. max = max >> 1;
  2401. i++;
  2402. } while (i <= sb->s_blocksize_bits + 1);
  2403. /* init file for buddy data */
  2404. ret = ext4_mb_init_backend(sb);
  2405. if (ret != 0) {
  2406. kfree(sbi->s_mb_offsets);
  2407. kfree(sbi->s_mb_maxs);
  2408. return ret;
  2409. }
  2410. spin_lock_init(&sbi->s_md_lock);
  2411. spin_lock_init(&sbi->s_bal_lock);
  2412. sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
  2413. sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
  2414. sbi->s_mb_stats = MB_DEFAULT_STATS;
  2415. sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
  2416. sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
  2417. sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
  2418. sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
  2419. sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
  2420. if (sbi->s_locality_groups == NULL) {
  2421. kfree(sbi->s_mb_offsets);
  2422. kfree(sbi->s_mb_maxs);
  2423. return -ENOMEM;
  2424. }
  2425. for_each_possible_cpu(i) {
  2426. struct ext4_locality_group *lg;
  2427. lg = per_cpu_ptr(sbi->s_locality_groups, i);
  2428. mutex_init(&lg->lg_mutex);
  2429. for (j = 0; j < PREALLOC_TB_SIZE; j++)
  2430. INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
  2431. spin_lock_init(&lg->lg_prealloc_lock);
  2432. }
  2433. ext4_mb_history_init(sb);
  2434. if (sbi->s_journal)
  2435. sbi->s_journal->j_commit_callback = release_blocks_on_commit;
  2436. printk(KERN_INFO "EXT4-fs: mballoc enabled\n");
  2437. return 0;
  2438. }
  2439. /* need to called with the ext4 group lock held */
  2440. static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
  2441. {
  2442. struct ext4_prealloc_space *pa;
  2443. struct list_head *cur, *tmp;
  2444. int count = 0;
  2445. list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
  2446. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  2447. list_del(&pa->pa_group_list);
  2448. count++;
  2449. kmem_cache_free(ext4_pspace_cachep, pa);
  2450. }
  2451. if (count)
  2452. mb_debug("mballoc: %u PAs left\n", count);
  2453. }
  2454. int ext4_mb_release(struct super_block *sb)
  2455. {
  2456. ext4_group_t ngroups = ext4_get_groups_count(sb);
  2457. ext4_group_t i;
  2458. int num_meta_group_infos;
  2459. struct ext4_group_info *grinfo;
  2460. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2461. if (sbi->s_group_info) {
  2462. for (i = 0; i < ngroups; i++) {
  2463. grinfo = ext4_get_group_info(sb, i);
  2464. #ifdef DOUBLE_CHECK
  2465. kfree(grinfo->bb_bitmap);
  2466. #endif
  2467. ext4_lock_group(sb, i);
  2468. ext4_mb_cleanup_pa(grinfo);
  2469. ext4_unlock_group(sb, i);
  2470. kfree(grinfo);
  2471. }
  2472. num_meta_group_infos = (ngroups +
  2473. EXT4_DESC_PER_BLOCK(sb) - 1) >>
  2474. EXT4_DESC_PER_BLOCK_BITS(sb);
  2475. for (i = 0; i < num_meta_group_infos; i++)
  2476. kfree(sbi->s_group_info[i]);
  2477. kfree(sbi->s_group_info);
  2478. }
  2479. kfree(sbi->s_mb_offsets);
  2480. kfree(sbi->s_mb_maxs);
  2481. if (sbi->s_buddy_cache)
  2482. iput(sbi->s_buddy_cache);
  2483. if (sbi->s_mb_stats) {
  2484. printk(KERN_INFO
  2485. "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
  2486. atomic_read(&sbi->s_bal_allocated),
  2487. atomic_read(&sbi->s_bal_reqs),
  2488. atomic_read(&sbi->s_bal_success));
  2489. printk(KERN_INFO
  2490. "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
  2491. "%u 2^N hits, %u breaks, %u lost\n",
  2492. atomic_read(&sbi->s_bal_ex_scanned),
  2493. atomic_read(&sbi->s_bal_goals),
  2494. atomic_read(&sbi->s_bal_2orders),
  2495. atomic_read(&sbi->s_bal_breaks),
  2496. atomic_read(&sbi->s_mb_lost_chunks));
  2497. printk(KERN_INFO
  2498. "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
  2499. sbi->s_mb_buddies_generated++,
  2500. sbi->s_mb_generation_time);
  2501. printk(KERN_INFO
  2502. "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
  2503. atomic_read(&sbi->s_mb_preallocated),
  2504. atomic_read(&sbi->s_mb_discarded));
  2505. }
  2506. free_percpu(sbi->s_locality_groups);
  2507. ext4_mb_history_release(sb);
  2508. return 0;
  2509. }
  2510. /*
  2511. * This function is called by the jbd2 layer once the commit has finished,
  2512. * so we know we can free the blocks that were released with that commit.
  2513. */
  2514. static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
  2515. {
  2516. struct super_block *sb = journal->j_private;
  2517. struct ext4_buddy e4b;
  2518. struct ext4_group_info *db;
  2519. int err, count = 0, count2 = 0;
  2520. struct ext4_free_data *entry;
  2521. ext4_fsblk_t discard_block;
  2522. struct list_head *l, *ltmp;
  2523. list_for_each_safe(l, ltmp, &txn->t_private_list) {
  2524. entry = list_entry(l, struct ext4_free_data, list);
  2525. mb_debug("gonna free %u blocks in group %u (0x%p):",
  2526. entry->count, entry->group, entry);
  2527. err = ext4_mb_load_buddy(sb, entry->group, &e4b);
  2528. /* we expect to find existing buddy because it's pinned */
  2529. BUG_ON(err != 0);
  2530. db = e4b.bd_info;
  2531. /* there are blocks to put in buddy to make them really free */
  2532. count += entry->count;
  2533. count2++;
  2534. ext4_lock_group(sb, entry->group);
  2535. /* Take it out of per group rb tree */
  2536. rb_erase(&entry->node, &(db->bb_free_root));
  2537. mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
  2538. if (!db->bb_free_root.rb_node) {
  2539. /* No more items in the per group rb tree
  2540. * balance refcounts from ext4_mb_free_metadata()
  2541. */
  2542. page_cache_release(e4b.bd_buddy_page);
  2543. page_cache_release(e4b.bd_bitmap_page);
  2544. }
  2545. ext4_unlock_group(sb, entry->group);
  2546. discard_block = (ext4_fsblk_t) entry->group * EXT4_BLOCKS_PER_GROUP(sb)
  2547. + entry->start_blk
  2548. + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  2549. trace_ext4_discard_blocks(sb, (unsigned long long)discard_block,
  2550. entry->count);
  2551. sb_issue_discard(sb, discard_block, entry->count);
  2552. kmem_cache_free(ext4_free_ext_cachep, entry);
  2553. ext4_mb_release_desc(&e4b);
  2554. }
  2555. mb_debug("freed %u blocks in %u structures\n", count, count2);
  2556. }
  2557. int __init init_ext4_mballoc(void)
  2558. {
  2559. ext4_pspace_cachep =
  2560. kmem_cache_create("ext4_prealloc_space",
  2561. sizeof(struct ext4_prealloc_space),
  2562. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2563. if (ext4_pspace_cachep == NULL)
  2564. return -ENOMEM;
  2565. ext4_ac_cachep =
  2566. kmem_cache_create("ext4_alloc_context",
  2567. sizeof(struct ext4_allocation_context),
  2568. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2569. if (ext4_ac_cachep == NULL) {
  2570. kmem_cache_destroy(ext4_pspace_cachep);
  2571. return -ENOMEM;
  2572. }
  2573. ext4_free_ext_cachep =
  2574. kmem_cache_create("ext4_free_block_extents",
  2575. sizeof(struct ext4_free_data),
  2576. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2577. if (ext4_free_ext_cachep == NULL) {
  2578. kmem_cache_destroy(ext4_pspace_cachep);
  2579. kmem_cache_destroy(ext4_ac_cachep);
  2580. return -ENOMEM;
  2581. }
  2582. return 0;
  2583. }
  2584. void exit_ext4_mballoc(void)
  2585. {
  2586. /*
  2587. * Wait for completion of call_rcu()'s on ext4_pspace_cachep
  2588. * before destroying the slab cache.
  2589. */
  2590. rcu_barrier();
  2591. kmem_cache_destroy(ext4_pspace_cachep);
  2592. kmem_cache_destroy(ext4_ac_cachep);
  2593. kmem_cache_destroy(ext4_free_ext_cachep);
  2594. }
  2595. /*
  2596. * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
  2597. * Returns 0 if success or error code
  2598. */
  2599. static noinline_for_stack int
  2600. ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
  2601. handle_t *handle, unsigned int reserv_blks)
  2602. {
  2603. struct buffer_head *bitmap_bh = NULL;
  2604. struct ext4_super_block *es;
  2605. struct ext4_group_desc *gdp;
  2606. struct buffer_head *gdp_bh;
  2607. struct ext4_sb_info *sbi;
  2608. struct super_block *sb;
  2609. ext4_fsblk_t block;
  2610. int err, len;
  2611. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  2612. BUG_ON(ac->ac_b_ex.fe_len <= 0);
  2613. sb = ac->ac_sb;
  2614. sbi = EXT4_SB(sb);
  2615. es = sbi->s_es;
  2616. err = -EIO;
  2617. bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
  2618. if (!bitmap_bh)
  2619. goto out_err;
  2620. err = ext4_journal_get_write_access(handle, bitmap_bh);
  2621. if (err)
  2622. goto out_err;
  2623. err = -EIO;
  2624. gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
  2625. if (!gdp)
  2626. goto out_err;
  2627. ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
  2628. ext4_free_blks_count(sb, gdp));
  2629. err = ext4_journal_get_write_access(handle, gdp_bh);
  2630. if (err)
  2631. goto out_err;
  2632. block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
  2633. + ac->ac_b_ex.fe_start
  2634. + le32_to_cpu(es->s_first_data_block);
  2635. len = ac->ac_b_ex.fe_len;
  2636. if (!ext4_data_block_valid(sbi, block, len)) {
  2637. ext4_error(sb, __func__,
  2638. "Allocating blocks %llu-%llu which overlap "
  2639. "fs metadata\n", block, block+len);
  2640. /* File system mounted not to panic on error
  2641. * Fix the bitmap and repeat the block allocation
  2642. * We leak some of the blocks here.
  2643. */
  2644. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2645. mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
  2646. ac->ac_b_ex.fe_len);
  2647. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2648. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2649. if (!err)
  2650. err = -EAGAIN;
  2651. goto out_err;
  2652. }
  2653. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2654. #ifdef AGGRESSIVE_CHECK
  2655. {
  2656. int i;
  2657. for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
  2658. BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
  2659. bitmap_bh->b_data));
  2660. }
  2661. }
  2662. #endif
  2663. mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,ac->ac_b_ex.fe_len);
  2664. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2665. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  2666. ext4_free_blks_set(sb, gdp,
  2667. ext4_free_blocks_after_init(sb,
  2668. ac->ac_b_ex.fe_group, gdp));
  2669. }
  2670. len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
  2671. ext4_free_blks_set(sb, gdp, len);
  2672. gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
  2673. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2674. percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
  2675. /*
  2676. * Now reduce the dirty block count also. Should not go negative
  2677. */
  2678. if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
  2679. /* release all the reserved blocks if non delalloc */
  2680. percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
  2681. else {
  2682. percpu_counter_sub(&sbi->s_dirtyblocks_counter,
  2683. ac->ac_b_ex.fe_len);
  2684. /* convert reserved quota blocks to real quota blocks */
  2685. vfs_dq_claim_block(ac->ac_inode, ac->ac_b_ex.fe_len);
  2686. }
  2687. if (sbi->s_log_groups_per_flex) {
  2688. ext4_group_t flex_group = ext4_flex_group(sbi,
  2689. ac->ac_b_ex.fe_group);
  2690. atomic_sub(ac->ac_b_ex.fe_len,
  2691. &sbi->s_flex_groups[flex_group].free_blocks);
  2692. }
  2693. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2694. if (err)
  2695. goto out_err;
  2696. err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
  2697. out_err:
  2698. sb->s_dirt = 1;
  2699. brelse(bitmap_bh);
  2700. return err;
  2701. }
  2702. /*
  2703. * here we normalize request for locality group
  2704. * Group request are normalized to s_strip size if we set the same via mount
  2705. * option. If not we set it to s_mb_group_prealloc which can be configured via
  2706. * /sys/fs/ext4/<partition>/mb_group_prealloc
  2707. *
  2708. * XXX: should we try to preallocate more than the group has now?
  2709. */
  2710. static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
  2711. {
  2712. struct super_block *sb = ac->ac_sb;
  2713. struct ext4_locality_group *lg = ac->ac_lg;
  2714. BUG_ON(lg == NULL);
  2715. if (EXT4_SB(sb)->s_stripe)
  2716. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
  2717. else
  2718. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
  2719. mb_debug("#%u: goal %u blocks for locality group\n",
  2720. current->pid, ac->ac_g_ex.fe_len);
  2721. }
  2722. /*
  2723. * Normalization means making request better in terms of
  2724. * size and alignment
  2725. */
  2726. static noinline_for_stack void
  2727. ext4_mb_normalize_request(struct ext4_allocation_context *ac,
  2728. struct ext4_allocation_request *ar)
  2729. {
  2730. int bsbits, max;
  2731. ext4_lblk_t end;
  2732. loff_t size, orig_size, start_off;
  2733. ext4_lblk_t start, orig_start;
  2734. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2735. struct ext4_prealloc_space *pa;
  2736. /* do normalize only data requests, metadata requests
  2737. do not need preallocation */
  2738. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2739. return;
  2740. /* sometime caller may want exact blocks */
  2741. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  2742. return;
  2743. /* caller may indicate that preallocation isn't
  2744. * required (it's a tail, for example) */
  2745. if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
  2746. return;
  2747. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
  2748. ext4_mb_normalize_group_request(ac);
  2749. return ;
  2750. }
  2751. bsbits = ac->ac_sb->s_blocksize_bits;
  2752. /* first, let's learn actual file size
  2753. * given current request is allocated */
  2754. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  2755. size = size << bsbits;
  2756. if (size < i_size_read(ac->ac_inode))
  2757. size = i_size_read(ac->ac_inode);
  2758. /* max size of free chunks */
  2759. max = 2 << bsbits;
  2760. #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
  2761. (req <= (size) || max <= (chunk_size))
  2762. /* first, try to predict filesize */
  2763. /* XXX: should this table be tunable? */
  2764. start_off = 0;
  2765. if (size <= 16 * 1024) {
  2766. size = 16 * 1024;
  2767. } else if (size <= 32 * 1024) {
  2768. size = 32 * 1024;
  2769. } else if (size <= 64 * 1024) {
  2770. size = 64 * 1024;
  2771. } else if (size <= 128 * 1024) {
  2772. size = 128 * 1024;
  2773. } else if (size <= 256 * 1024) {
  2774. size = 256 * 1024;
  2775. } else if (size <= 512 * 1024) {
  2776. size = 512 * 1024;
  2777. } else if (size <= 1024 * 1024) {
  2778. size = 1024 * 1024;
  2779. } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
  2780. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2781. (21 - bsbits)) << 21;
  2782. size = 2 * 1024 * 1024;
  2783. } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
  2784. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2785. (22 - bsbits)) << 22;
  2786. size = 4 * 1024 * 1024;
  2787. } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
  2788. (8<<20)>>bsbits, max, 8 * 1024)) {
  2789. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2790. (23 - bsbits)) << 23;
  2791. size = 8 * 1024 * 1024;
  2792. } else {
  2793. start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
  2794. size = ac->ac_o_ex.fe_len << bsbits;
  2795. }
  2796. orig_size = size = size >> bsbits;
  2797. orig_start = start = start_off >> bsbits;
  2798. /* don't cover already allocated blocks in selected range */
  2799. if (ar->pleft && start <= ar->lleft) {
  2800. size -= ar->lleft + 1 - start;
  2801. start = ar->lleft + 1;
  2802. }
  2803. if (ar->pright && start + size - 1 >= ar->lright)
  2804. size -= start + size - ar->lright;
  2805. end = start + size;
  2806. /* check we don't cross already preallocated blocks */
  2807. rcu_read_lock();
  2808. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2809. ext4_lblk_t pa_end;
  2810. if (pa->pa_deleted)
  2811. continue;
  2812. spin_lock(&pa->pa_lock);
  2813. if (pa->pa_deleted) {
  2814. spin_unlock(&pa->pa_lock);
  2815. continue;
  2816. }
  2817. pa_end = pa->pa_lstart + pa->pa_len;
  2818. /* PA must not overlap original request */
  2819. BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
  2820. ac->ac_o_ex.fe_logical < pa->pa_lstart));
  2821. /* skip PA normalized request doesn't overlap with */
  2822. if (pa->pa_lstart >= end) {
  2823. spin_unlock(&pa->pa_lock);
  2824. continue;
  2825. }
  2826. if (pa_end <= start) {
  2827. spin_unlock(&pa->pa_lock);
  2828. continue;
  2829. }
  2830. BUG_ON(pa->pa_lstart <= start && pa_end >= end);
  2831. if (pa_end <= ac->ac_o_ex.fe_logical) {
  2832. BUG_ON(pa_end < start);
  2833. start = pa_end;
  2834. }
  2835. if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
  2836. BUG_ON(pa->pa_lstart > end);
  2837. end = pa->pa_lstart;
  2838. }
  2839. spin_unlock(&pa->pa_lock);
  2840. }
  2841. rcu_read_unlock();
  2842. size = end - start;
  2843. /* XXX: extra loop to check we really don't overlap preallocations */
  2844. rcu_read_lock();
  2845. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2846. ext4_lblk_t pa_end;
  2847. spin_lock(&pa->pa_lock);
  2848. if (pa->pa_deleted == 0) {
  2849. pa_end = pa->pa_lstart + pa->pa_len;
  2850. BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
  2851. }
  2852. spin_unlock(&pa->pa_lock);
  2853. }
  2854. rcu_read_unlock();
  2855. if (start + size <= ac->ac_o_ex.fe_logical &&
  2856. start > ac->ac_o_ex.fe_logical) {
  2857. printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
  2858. (unsigned long) start, (unsigned long) size,
  2859. (unsigned long) ac->ac_o_ex.fe_logical);
  2860. }
  2861. BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
  2862. start > ac->ac_o_ex.fe_logical);
  2863. BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  2864. /* now prepare goal request */
  2865. /* XXX: is it better to align blocks WRT to logical
  2866. * placement or satisfy big request as is */
  2867. ac->ac_g_ex.fe_logical = start;
  2868. ac->ac_g_ex.fe_len = size;
  2869. /* define goal start in order to merge */
  2870. if (ar->pright && (ar->lright == (start + size))) {
  2871. /* merge to the right */
  2872. ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
  2873. &ac->ac_f_ex.fe_group,
  2874. &ac->ac_f_ex.fe_start);
  2875. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2876. }
  2877. if (ar->pleft && (ar->lleft + 1 == start)) {
  2878. /* merge to the left */
  2879. ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
  2880. &ac->ac_f_ex.fe_group,
  2881. &ac->ac_f_ex.fe_start);
  2882. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2883. }
  2884. mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
  2885. (unsigned) orig_size, (unsigned) start);
  2886. }
  2887. static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
  2888. {
  2889. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2890. if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
  2891. atomic_inc(&sbi->s_bal_reqs);
  2892. atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
  2893. if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
  2894. atomic_inc(&sbi->s_bal_success);
  2895. atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
  2896. if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
  2897. ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
  2898. atomic_inc(&sbi->s_bal_goals);
  2899. if (ac->ac_found > sbi->s_mb_max_to_scan)
  2900. atomic_inc(&sbi->s_bal_breaks);
  2901. }
  2902. ext4_mb_store_history(ac);
  2903. }
  2904. /*
  2905. * use blocks preallocated to inode
  2906. */
  2907. static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
  2908. struct ext4_prealloc_space *pa)
  2909. {
  2910. ext4_fsblk_t start;
  2911. ext4_fsblk_t end;
  2912. int len;
  2913. /* found preallocated blocks, use them */
  2914. start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
  2915. end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
  2916. len = end - start;
  2917. ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
  2918. &ac->ac_b_ex.fe_start);
  2919. ac->ac_b_ex.fe_len = len;
  2920. ac->ac_status = AC_STATUS_FOUND;
  2921. ac->ac_pa = pa;
  2922. BUG_ON(start < pa->pa_pstart);
  2923. BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
  2924. BUG_ON(pa->pa_free < len);
  2925. pa->pa_free -= len;
  2926. mb_debug("use %llu/%u from inode pa %p\n", start, len, pa);
  2927. }
  2928. /*
  2929. * use blocks preallocated to locality group
  2930. */
  2931. static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
  2932. struct ext4_prealloc_space *pa)
  2933. {
  2934. unsigned int len = ac->ac_o_ex.fe_len;
  2935. ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
  2936. &ac->ac_b_ex.fe_group,
  2937. &ac->ac_b_ex.fe_start);
  2938. ac->ac_b_ex.fe_len = len;
  2939. ac->ac_status = AC_STATUS_FOUND;
  2940. ac->ac_pa = pa;
  2941. /* we don't correct pa_pstart or pa_plen here to avoid
  2942. * possible race when the group is being loaded concurrently
  2943. * instead we correct pa later, after blocks are marked
  2944. * in on-disk bitmap -- see ext4_mb_release_context()
  2945. * Other CPUs are prevented from allocating from this pa by lg_mutex
  2946. */
  2947. mb_debug("use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
  2948. }
  2949. /*
  2950. * Return the prealloc space that have minimal distance
  2951. * from the goal block. @cpa is the prealloc
  2952. * space that is having currently known minimal distance
  2953. * from the goal block.
  2954. */
  2955. static struct ext4_prealloc_space *
  2956. ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
  2957. struct ext4_prealloc_space *pa,
  2958. struct ext4_prealloc_space *cpa)
  2959. {
  2960. ext4_fsblk_t cur_distance, new_distance;
  2961. if (cpa == NULL) {
  2962. atomic_inc(&pa->pa_count);
  2963. return pa;
  2964. }
  2965. cur_distance = abs(goal_block - cpa->pa_pstart);
  2966. new_distance = abs(goal_block - pa->pa_pstart);
  2967. if (cur_distance < new_distance)
  2968. return cpa;
  2969. /* drop the previous reference */
  2970. atomic_dec(&cpa->pa_count);
  2971. atomic_inc(&pa->pa_count);
  2972. return pa;
  2973. }
  2974. /*
  2975. * search goal blocks in preallocated space
  2976. */
  2977. static noinline_for_stack int
  2978. ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
  2979. {
  2980. int order, i;
  2981. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2982. struct ext4_locality_group *lg;
  2983. struct ext4_prealloc_space *pa, *cpa = NULL;
  2984. ext4_fsblk_t goal_block;
  2985. /* only data can be preallocated */
  2986. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2987. return 0;
  2988. /* first, try per-file preallocation */
  2989. rcu_read_lock();
  2990. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2991. /* all fields in this condition don't change,
  2992. * so we can skip locking for them */
  2993. if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
  2994. ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
  2995. continue;
  2996. /* found preallocated blocks, use them */
  2997. spin_lock(&pa->pa_lock);
  2998. if (pa->pa_deleted == 0 && pa->pa_free) {
  2999. atomic_inc(&pa->pa_count);
  3000. ext4_mb_use_inode_pa(ac, pa);
  3001. spin_unlock(&pa->pa_lock);
  3002. ac->ac_criteria = 10;
  3003. rcu_read_unlock();
  3004. return 1;
  3005. }
  3006. spin_unlock(&pa->pa_lock);
  3007. }
  3008. rcu_read_unlock();
  3009. /* can we use group allocation? */
  3010. if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
  3011. return 0;
  3012. /* inode may have no locality group for some reason */
  3013. lg = ac->ac_lg;
  3014. if (lg == NULL)
  3015. return 0;
  3016. order = fls(ac->ac_o_ex.fe_len) - 1;
  3017. if (order > PREALLOC_TB_SIZE - 1)
  3018. /* The max size of hash table is PREALLOC_TB_SIZE */
  3019. order = PREALLOC_TB_SIZE - 1;
  3020. goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) +
  3021. ac->ac_g_ex.fe_start +
  3022. le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block);
  3023. /*
  3024. * search for the prealloc space that is having
  3025. * minimal distance from the goal block.
  3026. */
  3027. for (i = order; i < PREALLOC_TB_SIZE; i++) {
  3028. rcu_read_lock();
  3029. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
  3030. pa_inode_list) {
  3031. spin_lock(&pa->pa_lock);
  3032. if (pa->pa_deleted == 0 &&
  3033. pa->pa_free >= ac->ac_o_ex.fe_len) {
  3034. cpa = ext4_mb_check_group_pa(goal_block,
  3035. pa, cpa);
  3036. }
  3037. spin_unlock(&pa->pa_lock);
  3038. }
  3039. rcu_read_unlock();
  3040. }
  3041. if (cpa) {
  3042. ext4_mb_use_group_pa(ac, cpa);
  3043. ac->ac_criteria = 20;
  3044. return 1;
  3045. }
  3046. return 0;
  3047. }
  3048. /*
  3049. * the function goes through all block freed in the group
  3050. * but not yet committed and marks them used in in-core bitmap.
  3051. * buddy must be generated from this bitmap
  3052. * Need to be called with the ext4 group lock held
  3053. */
  3054. static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
  3055. ext4_group_t group)
  3056. {
  3057. struct rb_node *n;
  3058. struct ext4_group_info *grp;
  3059. struct ext4_free_data *entry;
  3060. grp = ext4_get_group_info(sb, group);
  3061. n = rb_first(&(grp->bb_free_root));
  3062. while (n) {
  3063. entry = rb_entry(n, struct ext4_free_data, node);
  3064. mb_set_bits(bitmap, entry->start_blk, entry->count);
  3065. n = rb_next(n);
  3066. }
  3067. return;
  3068. }
  3069. /*
  3070. * the function goes through all preallocation in this group and marks them
  3071. * used in in-core bitmap. buddy must be generated from this bitmap
  3072. * Need to be called with ext4 group lock held
  3073. */
  3074. static noinline_for_stack
  3075. void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  3076. ext4_group_t group)
  3077. {
  3078. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3079. struct ext4_prealloc_space *pa;
  3080. struct list_head *cur;
  3081. ext4_group_t groupnr;
  3082. ext4_grpblk_t start;
  3083. int preallocated = 0;
  3084. int count = 0;
  3085. int len;
  3086. /* all form of preallocation discards first load group,
  3087. * so the only competing code is preallocation use.
  3088. * we don't need any locking here
  3089. * notice we do NOT ignore preallocations with pa_deleted
  3090. * otherwise we could leave used blocks available for
  3091. * allocation in buddy when concurrent ext4_mb_put_pa()
  3092. * is dropping preallocation
  3093. */
  3094. list_for_each(cur, &grp->bb_prealloc_list) {
  3095. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  3096. spin_lock(&pa->pa_lock);
  3097. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3098. &groupnr, &start);
  3099. len = pa->pa_len;
  3100. spin_unlock(&pa->pa_lock);
  3101. if (unlikely(len == 0))
  3102. continue;
  3103. BUG_ON(groupnr != group);
  3104. mb_set_bits(bitmap, start, len);
  3105. preallocated += len;
  3106. count++;
  3107. }
  3108. mb_debug("prellocated %u for group %u\n", preallocated, group);
  3109. }
  3110. static void ext4_mb_pa_callback(struct rcu_head *head)
  3111. {
  3112. struct ext4_prealloc_space *pa;
  3113. pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
  3114. kmem_cache_free(ext4_pspace_cachep, pa);
  3115. }
  3116. /*
  3117. * drops a reference to preallocated space descriptor
  3118. * if this was the last reference and the space is consumed
  3119. */
  3120. static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
  3121. struct super_block *sb, struct ext4_prealloc_space *pa)
  3122. {
  3123. ext4_group_t grp;
  3124. ext4_fsblk_t grp_blk;
  3125. if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
  3126. return;
  3127. /* in this short window concurrent discard can set pa_deleted */
  3128. spin_lock(&pa->pa_lock);
  3129. if (pa->pa_deleted == 1) {
  3130. spin_unlock(&pa->pa_lock);
  3131. return;
  3132. }
  3133. pa->pa_deleted = 1;
  3134. spin_unlock(&pa->pa_lock);
  3135. grp_blk = pa->pa_pstart;
  3136. /*
  3137. * If doing group-based preallocation, pa_pstart may be in the
  3138. * next group when pa is used up
  3139. */
  3140. if (pa->pa_type == MB_GROUP_PA)
  3141. grp_blk--;
  3142. ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
  3143. /*
  3144. * possible race:
  3145. *
  3146. * P1 (buddy init) P2 (regular allocation)
  3147. * find block B in PA
  3148. * copy on-disk bitmap to buddy
  3149. * mark B in on-disk bitmap
  3150. * drop PA from group
  3151. * mark all PAs in buddy
  3152. *
  3153. * thus, P1 initializes buddy with B available. to prevent this
  3154. * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
  3155. * against that pair
  3156. */
  3157. ext4_lock_group(sb, grp);
  3158. list_del(&pa->pa_group_list);
  3159. ext4_unlock_group(sb, grp);
  3160. spin_lock(pa->pa_obj_lock);
  3161. list_del_rcu(&pa->pa_inode_list);
  3162. spin_unlock(pa->pa_obj_lock);
  3163. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3164. }
  3165. /*
  3166. * creates new preallocated space for given inode
  3167. */
  3168. static noinline_for_stack int
  3169. ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
  3170. {
  3171. struct super_block *sb = ac->ac_sb;
  3172. struct ext4_prealloc_space *pa;
  3173. struct ext4_group_info *grp;
  3174. struct ext4_inode_info *ei;
  3175. /* preallocate only when found space is larger then requested */
  3176. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3177. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3178. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3179. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3180. if (pa == NULL)
  3181. return -ENOMEM;
  3182. if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
  3183. int winl;
  3184. int wins;
  3185. int win;
  3186. int offs;
  3187. /* we can't allocate as much as normalizer wants.
  3188. * so, found space must get proper lstart
  3189. * to cover original request */
  3190. BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
  3191. BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
  3192. /* we're limited by original request in that
  3193. * logical block must be covered any way
  3194. * winl is window we can move our chunk within */
  3195. winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
  3196. /* also, we should cover whole original request */
  3197. wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
  3198. /* the smallest one defines real window */
  3199. win = min(winl, wins);
  3200. offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
  3201. if (offs && offs < win)
  3202. win = offs;
  3203. ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
  3204. BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
  3205. BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
  3206. }
  3207. /* preallocation can change ac_b_ex, thus we store actually
  3208. * allocated blocks for history */
  3209. ac->ac_f_ex = ac->ac_b_ex;
  3210. pa->pa_lstart = ac->ac_b_ex.fe_logical;
  3211. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3212. pa->pa_len = ac->ac_b_ex.fe_len;
  3213. pa->pa_free = pa->pa_len;
  3214. atomic_set(&pa->pa_count, 1);
  3215. spin_lock_init(&pa->pa_lock);
  3216. INIT_LIST_HEAD(&pa->pa_inode_list);
  3217. INIT_LIST_HEAD(&pa->pa_group_list);
  3218. pa->pa_deleted = 0;
  3219. pa->pa_type = MB_INODE_PA;
  3220. mb_debug("new inode pa %p: %llu/%u for %u\n", pa,
  3221. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3222. trace_ext4_mb_new_inode_pa(ac, pa);
  3223. ext4_mb_use_inode_pa(ac, pa);
  3224. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3225. ei = EXT4_I(ac->ac_inode);
  3226. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3227. pa->pa_obj_lock = &ei->i_prealloc_lock;
  3228. pa->pa_inode = ac->ac_inode;
  3229. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3230. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3231. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3232. spin_lock(pa->pa_obj_lock);
  3233. list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
  3234. spin_unlock(pa->pa_obj_lock);
  3235. return 0;
  3236. }
  3237. /*
  3238. * creates new preallocated space for locality group inodes belongs to
  3239. */
  3240. static noinline_for_stack int
  3241. ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
  3242. {
  3243. struct super_block *sb = ac->ac_sb;
  3244. struct ext4_locality_group *lg;
  3245. struct ext4_prealloc_space *pa;
  3246. struct ext4_group_info *grp;
  3247. /* preallocate only when found space is larger then requested */
  3248. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3249. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3250. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3251. BUG_ON(ext4_pspace_cachep == NULL);
  3252. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3253. if (pa == NULL)
  3254. return -ENOMEM;
  3255. /* preallocation can change ac_b_ex, thus we store actually
  3256. * allocated blocks for history */
  3257. ac->ac_f_ex = ac->ac_b_ex;
  3258. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3259. pa->pa_lstart = pa->pa_pstart;
  3260. pa->pa_len = ac->ac_b_ex.fe_len;
  3261. pa->pa_free = pa->pa_len;
  3262. atomic_set(&pa->pa_count, 1);
  3263. spin_lock_init(&pa->pa_lock);
  3264. INIT_LIST_HEAD(&pa->pa_inode_list);
  3265. INIT_LIST_HEAD(&pa->pa_group_list);
  3266. pa->pa_deleted = 0;
  3267. pa->pa_type = MB_GROUP_PA;
  3268. mb_debug("new group pa %p: %llu/%u for %u\n", pa,
  3269. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3270. trace_ext4_mb_new_group_pa(ac, pa);
  3271. ext4_mb_use_group_pa(ac, pa);
  3272. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3273. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3274. lg = ac->ac_lg;
  3275. BUG_ON(lg == NULL);
  3276. pa->pa_obj_lock = &lg->lg_prealloc_lock;
  3277. pa->pa_inode = NULL;
  3278. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3279. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3280. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3281. /*
  3282. * We will later add the new pa to the right bucket
  3283. * after updating the pa_free in ext4_mb_release_context
  3284. */
  3285. return 0;
  3286. }
  3287. static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
  3288. {
  3289. int err;
  3290. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3291. err = ext4_mb_new_group_pa(ac);
  3292. else
  3293. err = ext4_mb_new_inode_pa(ac);
  3294. return err;
  3295. }
  3296. /*
  3297. * finds all unused blocks in on-disk bitmap, frees them in
  3298. * in-core bitmap and buddy.
  3299. * @pa must be unlinked from inode and group lists, so that
  3300. * nobody else can find/use it.
  3301. * the caller MUST hold group/inode locks.
  3302. * TODO: optimize the case when there are no in-core structures yet
  3303. */
  3304. static noinline_for_stack int
  3305. ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
  3306. struct ext4_prealloc_space *pa,
  3307. struct ext4_allocation_context *ac)
  3308. {
  3309. struct super_block *sb = e4b->bd_sb;
  3310. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3311. unsigned int end;
  3312. unsigned int next;
  3313. ext4_group_t group;
  3314. ext4_grpblk_t bit;
  3315. unsigned long long grp_blk_start;
  3316. sector_t start;
  3317. int err = 0;
  3318. int free = 0;
  3319. BUG_ON(pa->pa_deleted == 0);
  3320. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3321. grp_blk_start = pa->pa_pstart - bit;
  3322. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3323. end = bit + pa->pa_len;
  3324. if (ac) {
  3325. ac->ac_sb = sb;
  3326. ac->ac_inode = pa->pa_inode;
  3327. ac->ac_op = EXT4_MB_HISTORY_DISCARD;
  3328. }
  3329. while (bit < end) {
  3330. bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
  3331. if (bit >= end)
  3332. break;
  3333. next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
  3334. start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
  3335. le32_to_cpu(sbi->s_es->s_first_data_block);
  3336. mb_debug(" free preallocated %u/%u in group %u\n",
  3337. (unsigned) start, (unsigned) next - bit,
  3338. (unsigned) group);
  3339. free += next - bit;
  3340. if (ac) {
  3341. ac->ac_b_ex.fe_group = group;
  3342. ac->ac_b_ex.fe_start = bit;
  3343. ac->ac_b_ex.fe_len = next - bit;
  3344. ac->ac_b_ex.fe_logical = 0;
  3345. ext4_mb_store_history(ac);
  3346. }
  3347. trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit,
  3348. next - bit);
  3349. mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
  3350. bit = next + 1;
  3351. }
  3352. if (free != pa->pa_free) {
  3353. printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
  3354. pa, (unsigned long) pa->pa_lstart,
  3355. (unsigned long) pa->pa_pstart,
  3356. (unsigned long) pa->pa_len);
  3357. ext4_grp_locked_error(sb, group,
  3358. __func__, "free %u, pa_free %u",
  3359. free, pa->pa_free);
  3360. /*
  3361. * pa is already deleted so we use the value obtained
  3362. * from the bitmap and continue.
  3363. */
  3364. }
  3365. atomic_add(free, &sbi->s_mb_discarded);
  3366. return err;
  3367. }
  3368. static noinline_for_stack int
  3369. ext4_mb_release_group_pa(struct ext4_buddy *e4b,
  3370. struct ext4_prealloc_space *pa,
  3371. struct ext4_allocation_context *ac)
  3372. {
  3373. struct super_block *sb = e4b->bd_sb;
  3374. ext4_group_t group;
  3375. ext4_grpblk_t bit;
  3376. if (ac)
  3377. ac->ac_op = EXT4_MB_HISTORY_DISCARD;
  3378. trace_ext4_mb_release_group_pa(ac, pa);
  3379. BUG_ON(pa->pa_deleted == 0);
  3380. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3381. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3382. mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
  3383. atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
  3384. if (ac) {
  3385. ac->ac_sb = sb;
  3386. ac->ac_inode = NULL;
  3387. ac->ac_b_ex.fe_group = group;
  3388. ac->ac_b_ex.fe_start = bit;
  3389. ac->ac_b_ex.fe_len = pa->pa_len;
  3390. ac->ac_b_ex.fe_logical = 0;
  3391. ext4_mb_store_history(ac);
  3392. }
  3393. return 0;
  3394. }
  3395. /*
  3396. * releases all preallocations in given group
  3397. *
  3398. * first, we need to decide discard policy:
  3399. * - when do we discard
  3400. * 1) ENOSPC
  3401. * - how many do we discard
  3402. * 1) how many requested
  3403. */
  3404. static noinline_for_stack int
  3405. ext4_mb_discard_group_preallocations(struct super_block *sb,
  3406. ext4_group_t group, int needed)
  3407. {
  3408. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3409. struct buffer_head *bitmap_bh = NULL;
  3410. struct ext4_prealloc_space *pa, *tmp;
  3411. struct ext4_allocation_context *ac;
  3412. struct list_head list;
  3413. struct ext4_buddy e4b;
  3414. int err;
  3415. int busy = 0;
  3416. int free = 0;
  3417. mb_debug("discard preallocation for group %u\n", group);
  3418. if (list_empty(&grp->bb_prealloc_list))
  3419. return 0;
  3420. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3421. if (bitmap_bh == NULL) {
  3422. ext4_error(sb, __func__, "Error in reading block "
  3423. "bitmap for %u", group);
  3424. return 0;
  3425. }
  3426. err = ext4_mb_load_buddy(sb, group, &e4b);
  3427. if (err) {
  3428. ext4_error(sb, __func__, "Error in loading buddy "
  3429. "information for %u", group);
  3430. put_bh(bitmap_bh);
  3431. return 0;
  3432. }
  3433. if (needed == 0)
  3434. needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
  3435. INIT_LIST_HEAD(&list);
  3436. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3437. if (ac)
  3438. ac->ac_sb = sb;
  3439. repeat:
  3440. ext4_lock_group(sb, group);
  3441. list_for_each_entry_safe(pa, tmp,
  3442. &grp->bb_prealloc_list, pa_group_list) {
  3443. spin_lock(&pa->pa_lock);
  3444. if (atomic_read(&pa->pa_count)) {
  3445. spin_unlock(&pa->pa_lock);
  3446. busy = 1;
  3447. continue;
  3448. }
  3449. if (pa->pa_deleted) {
  3450. spin_unlock(&pa->pa_lock);
  3451. continue;
  3452. }
  3453. /* seems this one can be freed ... */
  3454. pa->pa_deleted = 1;
  3455. /* we can trust pa_free ... */
  3456. free += pa->pa_free;
  3457. spin_unlock(&pa->pa_lock);
  3458. list_del(&pa->pa_group_list);
  3459. list_add(&pa->u.pa_tmp_list, &list);
  3460. }
  3461. /* if we still need more blocks and some PAs were used, try again */
  3462. if (free < needed && busy) {
  3463. busy = 0;
  3464. ext4_unlock_group(sb, group);
  3465. /*
  3466. * Yield the CPU here so that we don't get soft lockup
  3467. * in non preempt case.
  3468. */
  3469. yield();
  3470. goto repeat;
  3471. }
  3472. /* found anything to free? */
  3473. if (list_empty(&list)) {
  3474. BUG_ON(free != 0);
  3475. goto out;
  3476. }
  3477. /* now free all selected PAs */
  3478. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3479. /* remove from object (inode or locality group) */
  3480. spin_lock(pa->pa_obj_lock);
  3481. list_del_rcu(&pa->pa_inode_list);
  3482. spin_unlock(pa->pa_obj_lock);
  3483. if (pa->pa_type == MB_GROUP_PA)
  3484. ext4_mb_release_group_pa(&e4b, pa, ac);
  3485. else
  3486. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3487. list_del(&pa->u.pa_tmp_list);
  3488. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3489. }
  3490. out:
  3491. ext4_unlock_group(sb, group);
  3492. if (ac)
  3493. kmem_cache_free(ext4_ac_cachep, ac);
  3494. ext4_mb_release_desc(&e4b);
  3495. put_bh(bitmap_bh);
  3496. return free;
  3497. }
  3498. /*
  3499. * releases all non-used preallocated blocks for given inode
  3500. *
  3501. * It's important to discard preallocations under i_data_sem
  3502. * We don't want another block to be served from the prealloc
  3503. * space when we are discarding the inode prealloc space.
  3504. *
  3505. * FIXME!! Make sure it is valid at all the call sites
  3506. */
  3507. void ext4_discard_preallocations(struct inode *inode)
  3508. {
  3509. struct ext4_inode_info *ei = EXT4_I(inode);
  3510. struct super_block *sb = inode->i_sb;
  3511. struct buffer_head *bitmap_bh = NULL;
  3512. struct ext4_prealloc_space *pa, *tmp;
  3513. struct ext4_allocation_context *ac;
  3514. ext4_group_t group = 0;
  3515. struct list_head list;
  3516. struct ext4_buddy e4b;
  3517. int err;
  3518. if (!S_ISREG(inode->i_mode)) {
  3519. /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
  3520. return;
  3521. }
  3522. mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
  3523. trace_ext4_discard_preallocations(inode);
  3524. INIT_LIST_HEAD(&list);
  3525. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3526. if (ac) {
  3527. ac->ac_sb = sb;
  3528. ac->ac_inode = inode;
  3529. }
  3530. repeat:
  3531. /* first, collect all pa's in the inode */
  3532. spin_lock(&ei->i_prealloc_lock);
  3533. while (!list_empty(&ei->i_prealloc_list)) {
  3534. pa = list_entry(ei->i_prealloc_list.next,
  3535. struct ext4_prealloc_space, pa_inode_list);
  3536. BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
  3537. spin_lock(&pa->pa_lock);
  3538. if (atomic_read(&pa->pa_count)) {
  3539. /* this shouldn't happen often - nobody should
  3540. * use preallocation while we're discarding it */
  3541. spin_unlock(&pa->pa_lock);
  3542. spin_unlock(&ei->i_prealloc_lock);
  3543. printk(KERN_ERR "uh-oh! used pa while discarding\n");
  3544. WARN_ON(1);
  3545. schedule_timeout_uninterruptible(HZ);
  3546. goto repeat;
  3547. }
  3548. if (pa->pa_deleted == 0) {
  3549. pa->pa_deleted = 1;
  3550. spin_unlock(&pa->pa_lock);
  3551. list_del_rcu(&pa->pa_inode_list);
  3552. list_add(&pa->u.pa_tmp_list, &list);
  3553. continue;
  3554. }
  3555. /* someone is deleting pa right now */
  3556. spin_unlock(&pa->pa_lock);
  3557. spin_unlock(&ei->i_prealloc_lock);
  3558. /* we have to wait here because pa_deleted
  3559. * doesn't mean pa is already unlinked from
  3560. * the list. as we might be called from
  3561. * ->clear_inode() the inode will get freed
  3562. * and concurrent thread which is unlinking
  3563. * pa from inode's list may access already
  3564. * freed memory, bad-bad-bad */
  3565. /* XXX: if this happens too often, we can
  3566. * add a flag to force wait only in case
  3567. * of ->clear_inode(), but not in case of
  3568. * regular truncate */
  3569. schedule_timeout_uninterruptible(HZ);
  3570. goto repeat;
  3571. }
  3572. spin_unlock(&ei->i_prealloc_lock);
  3573. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3574. BUG_ON(pa->pa_type != MB_INODE_PA);
  3575. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3576. err = ext4_mb_load_buddy(sb, group, &e4b);
  3577. if (err) {
  3578. ext4_error(sb, __func__, "Error in loading buddy "
  3579. "information for %u", group);
  3580. continue;
  3581. }
  3582. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3583. if (bitmap_bh == NULL) {
  3584. ext4_error(sb, __func__, "Error in reading block "
  3585. "bitmap for %u", group);
  3586. ext4_mb_release_desc(&e4b);
  3587. continue;
  3588. }
  3589. ext4_lock_group(sb, group);
  3590. list_del(&pa->pa_group_list);
  3591. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3592. ext4_unlock_group(sb, group);
  3593. ext4_mb_release_desc(&e4b);
  3594. put_bh(bitmap_bh);
  3595. list_del(&pa->u.pa_tmp_list);
  3596. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3597. }
  3598. if (ac)
  3599. kmem_cache_free(ext4_ac_cachep, ac);
  3600. }
  3601. /*
  3602. * finds all preallocated spaces and return blocks being freed to them
  3603. * if preallocated space becomes full (no block is used from the space)
  3604. * then the function frees space in buddy
  3605. * XXX: at the moment, truncate (which is the only way to free blocks)
  3606. * discards all preallocations
  3607. */
  3608. static void ext4_mb_return_to_preallocation(struct inode *inode,
  3609. struct ext4_buddy *e4b,
  3610. sector_t block, int count)
  3611. {
  3612. BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
  3613. }
  3614. #ifdef MB_DEBUG
  3615. static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3616. {
  3617. struct super_block *sb = ac->ac_sb;
  3618. ext4_group_t ngroups, i;
  3619. printk(KERN_ERR "EXT4-fs: Can't allocate:"
  3620. " Allocation context details:\n");
  3621. printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
  3622. ac->ac_status, ac->ac_flags);
  3623. printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
  3624. "best %lu/%lu/%lu@%lu cr %d\n",
  3625. (unsigned long)ac->ac_o_ex.fe_group,
  3626. (unsigned long)ac->ac_o_ex.fe_start,
  3627. (unsigned long)ac->ac_o_ex.fe_len,
  3628. (unsigned long)ac->ac_o_ex.fe_logical,
  3629. (unsigned long)ac->ac_g_ex.fe_group,
  3630. (unsigned long)ac->ac_g_ex.fe_start,
  3631. (unsigned long)ac->ac_g_ex.fe_len,
  3632. (unsigned long)ac->ac_g_ex.fe_logical,
  3633. (unsigned long)ac->ac_b_ex.fe_group,
  3634. (unsigned long)ac->ac_b_ex.fe_start,
  3635. (unsigned long)ac->ac_b_ex.fe_len,
  3636. (unsigned long)ac->ac_b_ex.fe_logical,
  3637. (int)ac->ac_criteria);
  3638. printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
  3639. ac->ac_found);
  3640. printk(KERN_ERR "EXT4-fs: groups: \n");
  3641. ngroups = ext4_get_groups_count(sb);
  3642. for (i = 0; i < ngroups; i++) {
  3643. struct ext4_group_info *grp = ext4_get_group_info(sb, i);
  3644. struct ext4_prealloc_space *pa;
  3645. ext4_grpblk_t start;
  3646. struct list_head *cur;
  3647. ext4_lock_group(sb, i);
  3648. list_for_each(cur, &grp->bb_prealloc_list) {
  3649. pa = list_entry(cur, struct ext4_prealloc_space,
  3650. pa_group_list);
  3651. spin_lock(&pa->pa_lock);
  3652. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3653. NULL, &start);
  3654. spin_unlock(&pa->pa_lock);
  3655. printk(KERN_ERR "PA:%lu:%d:%u \n", i,
  3656. start, pa->pa_len);
  3657. }
  3658. ext4_unlock_group(sb, i);
  3659. if (grp->bb_free == 0)
  3660. continue;
  3661. printk(KERN_ERR "%lu: %d/%d \n",
  3662. i, grp->bb_free, grp->bb_fragments);
  3663. }
  3664. printk(KERN_ERR "\n");
  3665. }
  3666. #else
  3667. static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3668. {
  3669. return;
  3670. }
  3671. #endif
  3672. /*
  3673. * We use locality group preallocation for small size file. The size of the
  3674. * file is determined by the current size or the resulting size after
  3675. * allocation which ever is larger
  3676. *
  3677. * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
  3678. */
  3679. static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
  3680. {
  3681. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  3682. int bsbits = ac->ac_sb->s_blocksize_bits;
  3683. loff_t size, isize;
  3684. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  3685. return;
  3686. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  3687. isize = i_size_read(ac->ac_inode) >> bsbits;
  3688. size = max(size, isize);
  3689. /* don't use group allocation for large files */
  3690. if (size >= sbi->s_mb_stream_request)
  3691. return;
  3692. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  3693. return;
  3694. BUG_ON(ac->ac_lg != NULL);
  3695. /*
  3696. * locality group prealloc space are per cpu. The reason for having
  3697. * per cpu locality group is to reduce the contention between block
  3698. * request from multiple CPUs.
  3699. */
  3700. ac->ac_lg = per_cpu_ptr(sbi->s_locality_groups, raw_smp_processor_id());
  3701. /* we're going to use group allocation */
  3702. ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
  3703. /* serialize all allocations in the group */
  3704. mutex_lock(&ac->ac_lg->lg_mutex);
  3705. }
  3706. static noinline_for_stack int
  3707. ext4_mb_initialize_context(struct ext4_allocation_context *ac,
  3708. struct ext4_allocation_request *ar)
  3709. {
  3710. struct super_block *sb = ar->inode->i_sb;
  3711. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3712. struct ext4_super_block *es = sbi->s_es;
  3713. ext4_group_t group;
  3714. unsigned int len;
  3715. ext4_fsblk_t goal;
  3716. ext4_grpblk_t block;
  3717. /* we can't allocate > group size */
  3718. len = ar->len;
  3719. /* just a dirty hack to filter too big requests */
  3720. if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
  3721. len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
  3722. /* start searching from the goal */
  3723. goal = ar->goal;
  3724. if (goal < le32_to_cpu(es->s_first_data_block) ||
  3725. goal >= ext4_blocks_count(es))
  3726. goal = le32_to_cpu(es->s_first_data_block);
  3727. ext4_get_group_no_and_offset(sb, goal, &group, &block);
  3728. /* set up allocation goals */
  3729. memset(ac, 0, sizeof(struct ext4_allocation_context));
  3730. ac->ac_b_ex.fe_logical = ar->logical;
  3731. ac->ac_status = AC_STATUS_CONTINUE;
  3732. ac->ac_sb = sb;
  3733. ac->ac_inode = ar->inode;
  3734. ac->ac_o_ex.fe_logical = ar->logical;
  3735. ac->ac_o_ex.fe_group = group;
  3736. ac->ac_o_ex.fe_start = block;
  3737. ac->ac_o_ex.fe_len = len;
  3738. ac->ac_g_ex.fe_logical = ar->logical;
  3739. ac->ac_g_ex.fe_group = group;
  3740. ac->ac_g_ex.fe_start = block;
  3741. ac->ac_g_ex.fe_len = len;
  3742. ac->ac_flags = ar->flags;
  3743. /* we have to define context: we'll we work with a file or
  3744. * locality group. this is a policy, actually */
  3745. ext4_mb_group_or_file(ac);
  3746. mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
  3747. "left: %u/%u, right %u/%u to %swritable\n",
  3748. (unsigned) ar->len, (unsigned) ar->logical,
  3749. (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
  3750. (unsigned) ar->lleft, (unsigned) ar->pleft,
  3751. (unsigned) ar->lright, (unsigned) ar->pright,
  3752. atomic_read(&ar->inode->i_writecount) ? "" : "non-");
  3753. return 0;
  3754. }
  3755. static noinline_for_stack void
  3756. ext4_mb_discard_lg_preallocations(struct super_block *sb,
  3757. struct ext4_locality_group *lg,
  3758. int order, int total_entries)
  3759. {
  3760. ext4_group_t group = 0;
  3761. struct ext4_buddy e4b;
  3762. struct list_head discard_list;
  3763. struct ext4_prealloc_space *pa, *tmp;
  3764. struct ext4_allocation_context *ac;
  3765. mb_debug("discard locality group preallocation\n");
  3766. INIT_LIST_HEAD(&discard_list);
  3767. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3768. if (ac)
  3769. ac->ac_sb = sb;
  3770. spin_lock(&lg->lg_prealloc_lock);
  3771. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
  3772. pa_inode_list) {
  3773. spin_lock(&pa->pa_lock);
  3774. if (atomic_read(&pa->pa_count)) {
  3775. /*
  3776. * This is the pa that we just used
  3777. * for block allocation. So don't
  3778. * free that
  3779. */
  3780. spin_unlock(&pa->pa_lock);
  3781. continue;
  3782. }
  3783. if (pa->pa_deleted) {
  3784. spin_unlock(&pa->pa_lock);
  3785. continue;
  3786. }
  3787. /* only lg prealloc space */
  3788. BUG_ON(pa->pa_type != MB_GROUP_PA);
  3789. /* seems this one can be freed ... */
  3790. pa->pa_deleted = 1;
  3791. spin_unlock(&pa->pa_lock);
  3792. list_del_rcu(&pa->pa_inode_list);
  3793. list_add(&pa->u.pa_tmp_list, &discard_list);
  3794. total_entries--;
  3795. if (total_entries <= 5) {
  3796. /*
  3797. * we want to keep only 5 entries
  3798. * allowing it to grow to 8. This
  3799. * mak sure we don't call discard
  3800. * soon for this list.
  3801. */
  3802. break;
  3803. }
  3804. }
  3805. spin_unlock(&lg->lg_prealloc_lock);
  3806. list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
  3807. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3808. if (ext4_mb_load_buddy(sb, group, &e4b)) {
  3809. ext4_error(sb, __func__, "Error in loading buddy "
  3810. "information for %u", group);
  3811. continue;
  3812. }
  3813. ext4_lock_group(sb, group);
  3814. list_del(&pa->pa_group_list);
  3815. ext4_mb_release_group_pa(&e4b, pa, ac);
  3816. ext4_unlock_group(sb, group);
  3817. ext4_mb_release_desc(&e4b);
  3818. list_del(&pa->u.pa_tmp_list);
  3819. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3820. }
  3821. if (ac)
  3822. kmem_cache_free(ext4_ac_cachep, ac);
  3823. }
  3824. /*
  3825. * We have incremented pa_count. So it cannot be freed at this
  3826. * point. Also we hold lg_mutex. So no parallel allocation is
  3827. * possible from this lg. That means pa_free cannot be updated.
  3828. *
  3829. * A parallel ext4_mb_discard_group_preallocations is possible.
  3830. * which can cause the lg_prealloc_list to be updated.
  3831. */
  3832. static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
  3833. {
  3834. int order, added = 0, lg_prealloc_count = 1;
  3835. struct super_block *sb = ac->ac_sb;
  3836. struct ext4_locality_group *lg = ac->ac_lg;
  3837. struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
  3838. order = fls(pa->pa_free) - 1;
  3839. if (order > PREALLOC_TB_SIZE - 1)
  3840. /* The max size of hash table is PREALLOC_TB_SIZE */
  3841. order = PREALLOC_TB_SIZE - 1;
  3842. /* Add the prealloc space to lg */
  3843. rcu_read_lock();
  3844. list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
  3845. pa_inode_list) {
  3846. spin_lock(&tmp_pa->pa_lock);
  3847. if (tmp_pa->pa_deleted) {
  3848. spin_unlock(&tmp_pa->pa_lock);
  3849. continue;
  3850. }
  3851. if (!added && pa->pa_free < tmp_pa->pa_free) {
  3852. /* Add to the tail of the previous entry */
  3853. list_add_tail_rcu(&pa->pa_inode_list,
  3854. &tmp_pa->pa_inode_list);
  3855. added = 1;
  3856. /*
  3857. * we want to count the total
  3858. * number of entries in the list
  3859. */
  3860. }
  3861. spin_unlock(&tmp_pa->pa_lock);
  3862. lg_prealloc_count++;
  3863. }
  3864. if (!added)
  3865. list_add_tail_rcu(&pa->pa_inode_list,
  3866. &lg->lg_prealloc_list[order]);
  3867. rcu_read_unlock();
  3868. /* Now trim the list to be not more than 8 elements */
  3869. if (lg_prealloc_count > 8) {
  3870. ext4_mb_discard_lg_preallocations(sb, lg,
  3871. order, lg_prealloc_count);
  3872. return;
  3873. }
  3874. return ;
  3875. }
  3876. /*
  3877. * release all resource we used in allocation
  3878. */
  3879. static int ext4_mb_release_context(struct ext4_allocation_context *ac)
  3880. {
  3881. struct ext4_prealloc_space *pa = ac->ac_pa;
  3882. if (pa) {
  3883. if (pa->pa_type == MB_GROUP_PA) {
  3884. /* see comment in ext4_mb_use_group_pa() */
  3885. spin_lock(&pa->pa_lock);
  3886. pa->pa_pstart += ac->ac_b_ex.fe_len;
  3887. pa->pa_lstart += ac->ac_b_ex.fe_len;
  3888. pa->pa_free -= ac->ac_b_ex.fe_len;
  3889. pa->pa_len -= ac->ac_b_ex.fe_len;
  3890. spin_unlock(&pa->pa_lock);
  3891. }
  3892. }
  3893. if (ac->alloc_semp)
  3894. up_read(ac->alloc_semp);
  3895. if (pa) {
  3896. /*
  3897. * We want to add the pa to the right bucket.
  3898. * Remove it from the list and while adding
  3899. * make sure the list to which we are adding
  3900. * doesn't grow big. We need to release
  3901. * alloc_semp before calling ext4_mb_add_n_trim()
  3902. */
  3903. if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
  3904. spin_lock(pa->pa_obj_lock);
  3905. list_del_rcu(&pa->pa_inode_list);
  3906. spin_unlock(pa->pa_obj_lock);
  3907. ext4_mb_add_n_trim(ac);
  3908. }
  3909. ext4_mb_put_pa(ac, ac->ac_sb, pa);
  3910. }
  3911. if (ac->ac_bitmap_page)
  3912. page_cache_release(ac->ac_bitmap_page);
  3913. if (ac->ac_buddy_page)
  3914. page_cache_release(ac->ac_buddy_page);
  3915. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3916. mutex_unlock(&ac->ac_lg->lg_mutex);
  3917. ext4_mb_collect_stats(ac);
  3918. return 0;
  3919. }
  3920. static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
  3921. {
  3922. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3923. int ret;
  3924. int freed = 0;
  3925. trace_ext4_mb_discard_preallocations(sb, needed);
  3926. for (i = 0; i < ngroups && needed > 0; i++) {
  3927. ret = ext4_mb_discard_group_preallocations(sb, i, needed);
  3928. freed += ret;
  3929. needed -= ret;
  3930. }
  3931. return freed;
  3932. }
  3933. /*
  3934. * Main entry point into mballoc to allocate blocks
  3935. * it tries to use preallocation first, then falls back
  3936. * to usual allocation
  3937. */
  3938. ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  3939. struct ext4_allocation_request *ar, int *errp)
  3940. {
  3941. int freed;
  3942. struct ext4_allocation_context *ac = NULL;
  3943. struct ext4_sb_info *sbi;
  3944. struct super_block *sb;
  3945. ext4_fsblk_t block = 0;
  3946. unsigned int inquota = 0;
  3947. unsigned int reserv_blks = 0;
  3948. sb = ar->inode->i_sb;
  3949. sbi = EXT4_SB(sb);
  3950. trace_ext4_request_blocks(ar);
  3951. /*
  3952. * For delayed allocation, we could skip the ENOSPC and
  3953. * EDQUOT check, as blocks and quotas have been already
  3954. * reserved when data being copied into pagecache.
  3955. */
  3956. if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
  3957. ar->flags |= EXT4_MB_DELALLOC_RESERVED;
  3958. else {
  3959. /* Without delayed allocation we need to verify
  3960. * there is enough free blocks to do block allocation
  3961. * and verify allocation doesn't exceed the quota limits.
  3962. */
  3963. while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
  3964. /* let others to free the space */
  3965. yield();
  3966. ar->len = ar->len >> 1;
  3967. }
  3968. if (!ar->len) {
  3969. *errp = -ENOSPC;
  3970. return 0;
  3971. }
  3972. reserv_blks = ar->len;
  3973. while (ar->len && vfs_dq_alloc_block(ar->inode, ar->len)) {
  3974. ar->flags |= EXT4_MB_HINT_NOPREALLOC;
  3975. ar->len--;
  3976. }
  3977. inquota = ar->len;
  3978. if (ar->len == 0) {
  3979. *errp = -EDQUOT;
  3980. goto out3;
  3981. }
  3982. }
  3983. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3984. if (!ac) {
  3985. ar->len = 0;
  3986. *errp = -ENOMEM;
  3987. goto out1;
  3988. }
  3989. *errp = ext4_mb_initialize_context(ac, ar);
  3990. if (*errp) {
  3991. ar->len = 0;
  3992. goto out2;
  3993. }
  3994. ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
  3995. if (!ext4_mb_use_preallocated(ac)) {
  3996. ac->ac_op = EXT4_MB_HISTORY_ALLOC;
  3997. ext4_mb_normalize_request(ac, ar);
  3998. repeat:
  3999. /* allocate space in core */
  4000. ext4_mb_regular_allocator(ac);
  4001. /* as we've just preallocated more space than
  4002. * user requested orinally, we store allocated
  4003. * space in a special descriptor */
  4004. if (ac->ac_status == AC_STATUS_FOUND &&
  4005. ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
  4006. ext4_mb_new_preallocation(ac);
  4007. }
  4008. if (likely(ac->ac_status == AC_STATUS_FOUND)) {
  4009. *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
  4010. if (*errp == -EAGAIN) {
  4011. /*
  4012. * drop the reference that we took
  4013. * in ext4_mb_use_best_found
  4014. */
  4015. ext4_mb_release_context(ac);
  4016. ac->ac_b_ex.fe_group = 0;
  4017. ac->ac_b_ex.fe_start = 0;
  4018. ac->ac_b_ex.fe_len = 0;
  4019. ac->ac_status = AC_STATUS_CONTINUE;
  4020. goto repeat;
  4021. } else if (*errp) {
  4022. ac->ac_b_ex.fe_len = 0;
  4023. ar->len = 0;
  4024. ext4_mb_show_ac(ac);
  4025. } else {
  4026. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  4027. ar->len = ac->ac_b_ex.fe_len;
  4028. }
  4029. } else {
  4030. freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
  4031. if (freed)
  4032. goto repeat;
  4033. *errp = -ENOSPC;
  4034. ac->ac_b_ex.fe_len = 0;
  4035. ar->len = 0;
  4036. ext4_mb_show_ac(ac);
  4037. }
  4038. ext4_mb_release_context(ac);
  4039. out2:
  4040. kmem_cache_free(ext4_ac_cachep, ac);
  4041. out1:
  4042. if (inquota && ar->len < inquota)
  4043. vfs_dq_free_block(ar->inode, inquota - ar->len);
  4044. out3:
  4045. if (!ar->len) {
  4046. if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
  4047. /* release all the reserved blocks if non delalloc */
  4048. percpu_counter_sub(&sbi->s_dirtyblocks_counter,
  4049. reserv_blks);
  4050. }
  4051. trace_ext4_allocate_blocks(ar, (unsigned long long)block);
  4052. return block;
  4053. }
  4054. /*
  4055. * We can merge two free data extents only if the physical blocks
  4056. * are contiguous, AND the extents were freed by the same transaction,
  4057. * AND the blocks are associated with the same group.
  4058. */
  4059. static int can_merge(struct ext4_free_data *entry1,
  4060. struct ext4_free_data *entry2)
  4061. {
  4062. if ((entry1->t_tid == entry2->t_tid) &&
  4063. (entry1->group == entry2->group) &&
  4064. ((entry1->start_blk + entry1->count) == entry2->start_blk))
  4065. return 1;
  4066. return 0;
  4067. }
  4068. static noinline_for_stack int
  4069. ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
  4070. struct ext4_free_data *new_entry)
  4071. {
  4072. ext4_grpblk_t block;
  4073. struct ext4_free_data *entry;
  4074. struct ext4_group_info *db = e4b->bd_info;
  4075. struct super_block *sb = e4b->bd_sb;
  4076. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4077. struct rb_node **n = &db->bb_free_root.rb_node, *node;
  4078. struct rb_node *parent = NULL, *new_node;
  4079. BUG_ON(!ext4_handle_valid(handle));
  4080. BUG_ON(e4b->bd_bitmap_page == NULL);
  4081. BUG_ON(e4b->bd_buddy_page == NULL);
  4082. new_node = &new_entry->node;
  4083. block = new_entry->start_blk;
  4084. if (!*n) {
  4085. /* first free block exent. We need to
  4086. protect buddy cache from being freed,
  4087. * otherwise we'll refresh it from
  4088. * on-disk bitmap and lose not-yet-available
  4089. * blocks */
  4090. page_cache_get(e4b->bd_buddy_page);
  4091. page_cache_get(e4b->bd_bitmap_page);
  4092. }
  4093. while (*n) {
  4094. parent = *n;
  4095. entry = rb_entry(parent, struct ext4_free_data, node);
  4096. if (block < entry->start_blk)
  4097. n = &(*n)->rb_left;
  4098. else if (block >= (entry->start_blk + entry->count))
  4099. n = &(*n)->rb_right;
  4100. else {
  4101. ext4_grp_locked_error(sb, e4b->bd_group, __func__,
  4102. "Double free of blocks %d (%d %d)",
  4103. block, entry->start_blk, entry->count);
  4104. return 0;
  4105. }
  4106. }
  4107. rb_link_node(new_node, parent, n);
  4108. rb_insert_color(new_node, &db->bb_free_root);
  4109. /* Now try to see the extent can be merged to left and right */
  4110. node = rb_prev(new_node);
  4111. if (node) {
  4112. entry = rb_entry(node, struct ext4_free_data, node);
  4113. if (can_merge(entry, new_entry)) {
  4114. new_entry->start_blk = entry->start_blk;
  4115. new_entry->count += entry->count;
  4116. rb_erase(node, &(db->bb_free_root));
  4117. spin_lock(&sbi->s_md_lock);
  4118. list_del(&entry->list);
  4119. spin_unlock(&sbi->s_md_lock);
  4120. kmem_cache_free(ext4_free_ext_cachep, entry);
  4121. }
  4122. }
  4123. node = rb_next(new_node);
  4124. if (node) {
  4125. entry = rb_entry(node, struct ext4_free_data, node);
  4126. if (can_merge(new_entry, entry)) {
  4127. new_entry->count += entry->count;
  4128. rb_erase(node, &(db->bb_free_root));
  4129. spin_lock(&sbi->s_md_lock);
  4130. list_del(&entry->list);
  4131. spin_unlock(&sbi->s_md_lock);
  4132. kmem_cache_free(ext4_free_ext_cachep, entry);
  4133. }
  4134. }
  4135. /* Add the extent to transaction's private list */
  4136. spin_lock(&sbi->s_md_lock);
  4137. list_add(&new_entry->list, &handle->h_transaction->t_private_list);
  4138. spin_unlock(&sbi->s_md_lock);
  4139. return 0;
  4140. }
  4141. /*
  4142. * Main entry point into mballoc to free blocks
  4143. */
  4144. void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
  4145. ext4_fsblk_t block, unsigned long count,
  4146. int metadata, unsigned long *freed)
  4147. {
  4148. struct buffer_head *bitmap_bh = NULL;
  4149. struct super_block *sb = inode->i_sb;
  4150. struct ext4_allocation_context *ac = NULL;
  4151. struct ext4_group_desc *gdp;
  4152. struct ext4_super_block *es;
  4153. unsigned int overflow;
  4154. ext4_grpblk_t bit;
  4155. struct buffer_head *gd_bh;
  4156. ext4_group_t block_group;
  4157. struct ext4_sb_info *sbi;
  4158. struct ext4_buddy e4b;
  4159. int err = 0;
  4160. int ret;
  4161. *freed = 0;
  4162. sbi = EXT4_SB(sb);
  4163. es = EXT4_SB(sb)->s_es;
  4164. if (block < le32_to_cpu(es->s_first_data_block) ||
  4165. block + count < block ||
  4166. block + count > ext4_blocks_count(es)) {
  4167. ext4_error(sb, __func__,
  4168. "Freeing blocks not in datazone - "
  4169. "block = %llu, count = %lu", block, count);
  4170. goto error_return;
  4171. }
  4172. ext4_debug("freeing block %llu\n", block);
  4173. trace_ext4_free_blocks(inode, block, count, metadata);
  4174. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  4175. if (ac) {
  4176. ac->ac_op = EXT4_MB_HISTORY_FREE;
  4177. ac->ac_inode = inode;
  4178. ac->ac_sb = sb;
  4179. }
  4180. do_more:
  4181. overflow = 0;
  4182. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  4183. /*
  4184. * Check to see if we are freeing blocks across a group
  4185. * boundary.
  4186. */
  4187. if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  4188. overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
  4189. count -= overflow;
  4190. }
  4191. bitmap_bh = ext4_read_block_bitmap(sb, block_group);
  4192. if (!bitmap_bh) {
  4193. err = -EIO;
  4194. goto error_return;
  4195. }
  4196. gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
  4197. if (!gdp) {
  4198. err = -EIO;
  4199. goto error_return;
  4200. }
  4201. if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
  4202. in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
  4203. in_range(block, ext4_inode_table(sb, gdp),
  4204. EXT4_SB(sb)->s_itb_per_group) ||
  4205. in_range(block + count - 1, ext4_inode_table(sb, gdp),
  4206. EXT4_SB(sb)->s_itb_per_group)) {
  4207. ext4_error(sb, __func__,
  4208. "Freeing blocks in system zone - "
  4209. "Block = %llu, count = %lu", block, count);
  4210. /* err = 0. ext4_std_error should be a no op */
  4211. goto error_return;
  4212. }
  4213. BUFFER_TRACE(bitmap_bh, "getting write access");
  4214. err = ext4_journal_get_write_access(handle, bitmap_bh);
  4215. if (err)
  4216. goto error_return;
  4217. /*
  4218. * We are about to modify some metadata. Call the journal APIs
  4219. * to unshare ->b_data if a currently-committing transaction is
  4220. * using it
  4221. */
  4222. BUFFER_TRACE(gd_bh, "get_write_access");
  4223. err = ext4_journal_get_write_access(handle, gd_bh);
  4224. if (err)
  4225. goto error_return;
  4226. #ifdef AGGRESSIVE_CHECK
  4227. {
  4228. int i;
  4229. for (i = 0; i < count; i++)
  4230. BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
  4231. }
  4232. #endif
  4233. if (ac) {
  4234. ac->ac_b_ex.fe_group = block_group;
  4235. ac->ac_b_ex.fe_start = bit;
  4236. ac->ac_b_ex.fe_len = count;
  4237. ext4_mb_store_history(ac);
  4238. }
  4239. err = ext4_mb_load_buddy(sb, block_group, &e4b);
  4240. if (err)
  4241. goto error_return;
  4242. if (metadata && ext4_handle_valid(handle)) {
  4243. struct ext4_free_data *new_entry;
  4244. /*
  4245. * blocks being freed are metadata. these blocks shouldn't
  4246. * be used until this transaction is committed
  4247. */
  4248. new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
  4249. new_entry->start_blk = bit;
  4250. new_entry->group = block_group;
  4251. new_entry->count = count;
  4252. new_entry->t_tid = handle->h_transaction->t_tid;
  4253. ext4_lock_group(sb, block_group);
  4254. mb_clear_bits(bitmap_bh->b_data, bit, count);
  4255. ext4_mb_free_metadata(handle, &e4b, new_entry);
  4256. } else {
  4257. /* need to update group_info->bb_free and bitmap
  4258. * with group lock held. generate_buddy look at
  4259. * them with group lock_held
  4260. */
  4261. ext4_lock_group(sb, block_group);
  4262. mb_clear_bits(bitmap_bh->b_data, bit, count);
  4263. mb_free_blocks(inode, &e4b, bit, count);
  4264. ext4_mb_return_to_preallocation(inode, &e4b, block, count);
  4265. }
  4266. ret = ext4_free_blks_count(sb, gdp) + count;
  4267. ext4_free_blks_set(sb, gdp, ret);
  4268. gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
  4269. ext4_unlock_group(sb, block_group);
  4270. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  4271. if (sbi->s_log_groups_per_flex) {
  4272. ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
  4273. atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
  4274. }
  4275. ext4_mb_release_desc(&e4b);
  4276. *freed += count;
  4277. /* We dirtied the bitmap block */
  4278. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  4279. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  4280. /* And the group descriptor block */
  4281. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  4282. ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
  4283. if (!err)
  4284. err = ret;
  4285. if (overflow && !err) {
  4286. block += count;
  4287. count = overflow;
  4288. put_bh(bitmap_bh);
  4289. goto do_more;
  4290. }
  4291. sb->s_dirt = 1;
  4292. error_return:
  4293. brelse(bitmap_bh);
  4294. ext4_std_error(sb, err);
  4295. if (ac)
  4296. kmem_cache_free(ext4_ac_cachep, ac);
  4297. return;
  4298. }