mballoc.c 132 KB

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