mballoc.c 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public Licens
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  17. */
  18. /*
  19. * mballoc.c contains the multiblocks allocation routines
  20. */
  21. #include "mballoc.h"
  22. /*
  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. unlock_buffer(bh[i]);
  740. spin_unlock(sb_bgl_lock(EXT4_SB(sb), first_group + 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. mb_clear_bit_atomic(lock, cur, bm);
  976. cur++;
  977. }
  978. }
  979. static void mb_set_bits(spinlock_t *lock, void *bm, int cur, int len)
  980. {
  981. __u32 *addr;
  982. len = cur + len;
  983. while (cur < len) {
  984. if ((cur & 31) == 0 && (len - cur) >= 32) {
  985. /* fast path: set whole word at once */
  986. addr = bm + (cur >> 3);
  987. *addr = 0xffffffff;
  988. cur += 32;
  989. continue;
  990. }
  991. mb_set_bit_atomic(lock, cur, bm);
  992. cur++;
  993. }
  994. }
  995. static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
  996. int first, int count)
  997. {
  998. int block = 0;
  999. int max = 0;
  1000. int order;
  1001. void *buddy;
  1002. void *buddy2;
  1003. struct super_block *sb = e4b->bd_sb;
  1004. BUG_ON(first + count > (sb->s_blocksize << 3));
  1005. BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
  1006. mb_check_buddy(e4b);
  1007. mb_free_blocks_double(inode, e4b, first, count);
  1008. e4b->bd_info->bb_free += count;
  1009. if (first < e4b->bd_info->bb_first_free)
  1010. e4b->bd_info->bb_first_free = first;
  1011. /* let's maintain fragments counter */
  1012. if (first != 0)
  1013. block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
  1014. if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
  1015. max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
  1016. if (block && max)
  1017. e4b->bd_info->bb_fragments--;
  1018. else if (!block && !max)
  1019. e4b->bd_info->bb_fragments++;
  1020. /* let's maintain buddy itself */
  1021. while (count-- > 0) {
  1022. block = first++;
  1023. order = 0;
  1024. if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
  1025. ext4_fsblk_t blocknr;
  1026. blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
  1027. blocknr += block;
  1028. blocknr +=
  1029. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  1030. ext4_grp_locked_error(sb, e4b->bd_group,
  1031. __func__, "double-free of inode"
  1032. " %lu's block %llu(bit %u in group %u)",
  1033. inode ? inode->i_ino : 0, blocknr, block,
  1034. e4b->bd_group);
  1035. }
  1036. mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
  1037. e4b->bd_info->bb_counters[order]++;
  1038. /* start of the buddy */
  1039. buddy = mb_find_buddy(e4b, order, &max);
  1040. do {
  1041. block &= ~1UL;
  1042. if (mb_test_bit(block, buddy) ||
  1043. mb_test_bit(block + 1, buddy))
  1044. break;
  1045. /* both the buddies are free, try to coalesce them */
  1046. buddy2 = mb_find_buddy(e4b, order + 1, &max);
  1047. if (!buddy2)
  1048. break;
  1049. if (order > 0) {
  1050. /* for special purposes, we don't set
  1051. * free bits in bitmap */
  1052. mb_set_bit(block, buddy);
  1053. mb_set_bit(block + 1, buddy);
  1054. }
  1055. e4b->bd_info->bb_counters[order]--;
  1056. e4b->bd_info->bb_counters[order]--;
  1057. block = block >> 1;
  1058. order++;
  1059. e4b->bd_info->bb_counters[order]++;
  1060. mb_clear_bit(block, buddy2);
  1061. buddy = buddy2;
  1062. } while (1);
  1063. }
  1064. mb_check_buddy(e4b);
  1065. }
  1066. static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
  1067. int needed, struct ext4_free_extent *ex)
  1068. {
  1069. int next = block;
  1070. int max;
  1071. int ord;
  1072. void *buddy;
  1073. BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
  1074. BUG_ON(ex == NULL);
  1075. buddy = mb_find_buddy(e4b, order, &max);
  1076. BUG_ON(buddy == NULL);
  1077. BUG_ON(block >= max);
  1078. if (mb_test_bit(block, buddy)) {
  1079. ex->fe_len = 0;
  1080. ex->fe_start = 0;
  1081. ex->fe_group = 0;
  1082. return 0;
  1083. }
  1084. /* FIXME dorp order completely ? */
  1085. if (likely(order == 0)) {
  1086. /* find actual order */
  1087. order = mb_find_order_for_block(e4b, block);
  1088. block = block >> order;
  1089. }
  1090. ex->fe_len = 1 << order;
  1091. ex->fe_start = block << order;
  1092. ex->fe_group = e4b->bd_group;
  1093. /* calc difference from given start */
  1094. next = next - ex->fe_start;
  1095. ex->fe_len -= next;
  1096. ex->fe_start += next;
  1097. while (needed > ex->fe_len &&
  1098. (buddy = mb_find_buddy(e4b, order, &max))) {
  1099. if (block + 1 >= max)
  1100. break;
  1101. next = (block + 1) * (1 << order);
  1102. if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
  1103. break;
  1104. ord = mb_find_order_for_block(e4b, next);
  1105. order = ord;
  1106. block = next >> order;
  1107. ex->fe_len += 1 << order;
  1108. }
  1109. BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
  1110. return ex->fe_len;
  1111. }
  1112. static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
  1113. {
  1114. int ord;
  1115. int mlen = 0;
  1116. int max = 0;
  1117. int cur;
  1118. int start = ex->fe_start;
  1119. int len = ex->fe_len;
  1120. unsigned ret = 0;
  1121. int len0 = len;
  1122. void *buddy;
  1123. BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
  1124. BUG_ON(e4b->bd_group != ex->fe_group);
  1125. BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
  1126. mb_check_buddy(e4b);
  1127. mb_mark_used_double(e4b, start, len);
  1128. e4b->bd_info->bb_free -= len;
  1129. if (e4b->bd_info->bb_first_free == start)
  1130. e4b->bd_info->bb_first_free += len;
  1131. /* let's maintain fragments counter */
  1132. if (start != 0)
  1133. mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
  1134. if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
  1135. max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
  1136. if (mlen && max)
  1137. e4b->bd_info->bb_fragments++;
  1138. else if (!mlen && !max)
  1139. e4b->bd_info->bb_fragments--;
  1140. /* let's maintain buddy itself */
  1141. while (len) {
  1142. ord = mb_find_order_for_block(e4b, start);
  1143. if (((start >> ord) << ord) == start && len >= (1 << ord)) {
  1144. /* the whole chunk may be allocated at once! */
  1145. mlen = 1 << ord;
  1146. buddy = mb_find_buddy(e4b, ord, &max);
  1147. BUG_ON((start >> ord) >= max);
  1148. mb_set_bit(start >> ord, buddy);
  1149. e4b->bd_info->bb_counters[ord]--;
  1150. start += mlen;
  1151. len -= mlen;
  1152. BUG_ON(len < 0);
  1153. continue;
  1154. }
  1155. /* store for history */
  1156. if (ret == 0)
  1157. ret = len | (ord << 16);
  1158. /* we have to split large buddy */
  1159. BUG_ON(ord <= 0);
  1160. buddy = mb_find_buddy(e4b, ord, &max);
  1161. mb_set_bit(start >> ord, buddy);
  1162. e4b->bd_info->bb_counters[ord]--;
  1163. ord--;
  1164. cur = (start >> ord) & ~1U;
  1165. buddy = mb_find_buddy(e4b, ord, &max);
  1166. mb_clear_bit(cur, buddy);
  1167. mb_clear_bit(cur + 1, buddy);
  1168. e4b->bd_info->bb_counters[ord]++;
  1169. e4b->bd_info->bb_counters[ord]++;
  1170. }
  1171. mb_set_bits(sb_bgl_lock(EXT4_SB(e4b->bd_sb), ex->fe_group),
  1172. EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
  1173. mb_check_buddy(e4b);
  1174. return ret;
  1175. }
  1176. /*
  1177. * Must be called under group lock!
  1178. */
  1179. static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
  1180. struct ext4_buddy *e4b)
  1181. {
  1182. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1183. int ret;
  1184. BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
  1185. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1186. ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
  1187. ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
  1188. ret = mb_mark_used(e4b, &ac->ac_b_ex);
  1189. /* preallocation can change ac_b_ex, thus we store actually
  1190. * allocated blocks for history */
  1191. ac->ac_f_ex = ac->ac_b_ex;
  1192. ac->ac_status = AC_STATUS_FOUND;
  1193. ac->ac_tail = ret & 0xffff;
  1194. ac->ac_buddy = ret >> 16;
  1195. /*
  1196. * take the page reference. We want the page to be pinned
  1197. * so that we don't get a ext4_mb_init_cache_call for this
  1198. * group until we update the bitmap. That would mean we
  1199. * double allocate blocks. The reference is dropped
  1200. * in ext4_mb_release_context
  1201. */
  1202. ac->ac_bitmap_page = e4b->bd_bitmap_page;
  1203. get_page(ac->ac_bitmap_page);
  1204. ac->ac_buddy_page = e4b->bd_buddy_page;
  1205. get_page(ac->ac_buddy_page);
  1206. /* store last allocated for subsequent stream allocation */
  1207. if ((ac->ac_flags & EXT4_MB_HINT_DATA)) {
  1208. spin_lock(&sbi->s_md_lock);
  1209. sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
  1210. sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
  1211. spin_unlock(&sbi->s_md_lock);
  1212. }
  1213. }
  1214. /*
  1215. * regular allocator, for general purposes allocation
  1216. */
  1217. static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
  1218. struct ext4_buddy *e4b,
  1219. int finish_group)
  1220. {
  1221. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1222. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1223. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1224. struct ext4_free_extent ex;
  1225. int max;
  1226. if (ac->ac_status == AC_STATUS_FOUND)
  1227. return;
  1228. /*
  1229. * We don't want to scan for a whole year
  1230. */
  1231. if (ac->ac_found > sbi->s_mb_max_to_scan &&
  1232. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1233. ac->ac_status = AC_STATUS_BREAK;
  1234. return;
  1235. }
  1236. /*
  1237. * Haven't found good chunk so far, let's continue
  1238. */
  1239. if (bex->fe_len < gex->fe_len)
  1240. return;
  1241. if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
  1242. && bex->fe_group == e4b->bd_group) {
  1243. /* recheck chunk's availability - we don't know
  1244. * when it was found (within this lock-unlock
  1245. * period or not) */
  1246. max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
  1247. if (max >= gex->fe_len) {
  1248. ext4_mb_use_best_found(ac, e4b);
  1249. return;
  1250. }
  1251. }
  1252. }
  1253. /*
  1254. * The routine checks whether found extent is good enough. If it is,
  1255. * then the extent gets marked used and flag is set to the context
  1256. * to stop scanning. Otherwise, the extent is compared with the
  1257. * previous found extent and if new one is better, then it's stored
  1258. * in the context. Later, the best found extent will be used, if
  1259. * mballoc can't find good enough extent.
  1260. *
  1261. * FIXME: real allocation policy is to be designed yet!
  1262. */
  1263. static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
  1264. struct ext4_free_extent *ex,
  1265. struct ext4_buddy *e4b)
  1266. {
  1267. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1268. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1269. BUG_ON(ex->fe_len <= 0);
  1270. BUG_ON(ex->fe_len >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1271. BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1272. BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
  1273. ac->ac_found++;
  1274. /*
  1275. * The special case - take what you catch first
  1276. */
  1277. if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1278. *bex = *ex;
  1279. ext4_mb_use_best_found(ac, e4b);
  1280. return;
  1281. }
  1282. /*
  1283. * Let's check whether the chuck is good enough
  1284. */
  1285. if (ex->fe_len == gex->fe_len) {
  1286. *bex = *ex;
  1287. ext4_mb_use_best_found(ac, e4b);
  1288. return;
  1289. }
  1290. /*
  1291. * If this is first found extent, just store it in the context
  1292. */
  1293. if (bex->fe_len == 0) {
  1294. *bex = *ex;
  1295. return;
  1296. }
  1297. /*
  1298. * If new found extent is better, store it in the context
  1299. */
  1300. if (bex->fe_len < gex->fe_len) {
  1301. /* if the request isn't satisfied, any found extent
  1302. * larger than previous best one is better */
  1303. if (ex->fe_len > bex->fe_len)
  1304. *bex = *ex;
  1305. } else if (ex->fe_len > gex->fe_len) {
  1306. /* if the request is satisfied, then we try to find
  1307. * an extent that still satisfy the request, but is
  1308. * smaller than previous one */
  1309. if (ex->fe_len < bex->fe_len)
  1310. *bex = *ex;
  1311. }
  1312. ext4_mb_check_limits(ac, e4b, 0);
  1313. }
  1314. static int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
  1315. struct ext4_buddy *e4b)
  1316. {
  1317. struct ext4_free_extent ex = ac->ac_b_ex;
  1318. ext4_group_t group = ex.fe_group;
  1319. int max;
  1320. int err;
  1321. BUG_ON(ex.fe_len <= 0);
  1322. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1323. if (err)
  1324. return err;
  1325. ext4_lock_group(ac->ac_sb, group);
  1326. max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
  1327. if (max > 0) {
  1328. ac->ac_b_ex = ex;
  1329. ext4_mb_use_best_found(ac, e4b);
  1330. }
  1331. ext4_unlock_group(ac->ac_sb, group);
  1332. ext4_mb_release_desc(e4b);
  1333. return 0;
  1334. }
  1335. static int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
  1336. struct ext4_buddy *e4b)
  1337. {
  1338. ext4_group_t group = ac->ac_g_ex.fe_group;
  1339. int max;
  1340. int err;
  1341. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1342. struct ext4_super_block *es = sbi->s_es;
  1343. struct ext4_free_extent ex;
  1344. if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
  1345. return 0;
  1346. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1347. if (err)
  1348. return err;
  1349. ext4_lock_group(ac->ac_sb, group);
  1350. max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
  1351. ac->ac_g_ex.fe_len, &ex);
  1352. if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
  1353. ext4_fsblk_t start;
  1354. start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) +
  1355. ex.fe_start + le32_to_cpu(es->s_first_data_block);
  1356. /* use do_div to get remainder (would be 64-bit modulo) */
  1357. if (do_div(start, sbi->s_stripe) == 0) {
  1358. ac->ac_found++;
  1359. ac->ac_b_ex = ex;
  1360. ext4_mb_use_best_found(ac, e4b);
  1361. }
  1362. } else if (max >= ac->ac_g_ex.fe_len) {
  1363. BUG_ON(ex.fe_len <= 0);
  1364. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1365. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1366. ac->ac_found++;
  1367. ac->ac_b_ex = ex;
  1368. ext4_mb_use_best_found(ac, e4b);
  1369. } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
  1370. /* Sometimes, caller may want to merge even small
  1371. * number of blocks to an existing extent */
  1372. BUG_ON(ex.fe_len <= 0);
  1373. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1374. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1375. ac->ac_found++;
  1376. ac->ac_b_ex = ex;
  1377. ext4_mb_use_best_found(ac, e4b);
  1378. }
  1379. ext4_unlock_group(ac->ac_sb, group);
  1380. ext4_mb_release_desc(e4b);
  1381. return 0;
  1382. }
  1383. /*
  1384. * The routine scans buddy structures (not bitmap!) from given order
  1385. * to max order and tries to find big enough chunk to satisfy the req
  1386. */
  1387. static void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
  1388. struct ext4_buddy *e4b)
  1389. {
  1390. struct super_block *sb = ac->ac_sb;
  1391. struct ext4_group_info *grp = e4b->bd_info;
  1392. void *buddy;
  1393. int i;
  1394. int k;
  1395. int max;
  1396. BUG_ON(ac->ac_2order <= 0);
  1397. for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
  1398. if (grp->bb_counters[i] == 0)
  1399. continue;
  1400. buddy = mb_find_buddy(e4b, i, &max);
  1401. BUG_ON(buddy == NULL);
  1402. k = mb_find_next_zero_bit(buddy, max, 0);
  1403. BUG_ON(k >= max);
  1404. ac->ac_found++;
  1405. ac->ac_b_ex.fe_len = 1 << i;
  1406. ac->ac_b_ex.fe_start = k << i;
  1407. ac->ac_b_ex.fe_group = e4b->bd_group;
  1408. ext4_mb_use_best_found(ac, e4b);
  1409. BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
  1410. if (EXT4_SB(sb)->s_mb_stats)
  1411. atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
  1412. break;
  1413. }
  1414. }
  1415. /*
  1416. * The routine scans the group and measures all found extents.
  1417. * In order to optimize scanning, caller must pass number of
  1418. * free blocks in the group, so the routine can know upper limit.
  1419. */
  1420. static void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
  1421. struct ext4_buddy *e4b)
  1422. {
  1423. struct super_block *sb = ac->ac_sb;
  1424. void *bitmap = EXT4_MB_BITMAP(e4b);
  1425. struct ext4_free_extent ex;
  1426. int i;
  1427. int free;
  1428. free = e4b->bd_info->bb_free;
  1429. BUG_ON(free <= 0);
  1430. i = e4b->bd_info->bb_first_free;
  1431. while (free && ac->ac_status == AC_STATUS_CONTINUE) {
  1432. i = mb_find_next_zero_bit(bitmap,
  1433. EXT4_BLOCKS_PER_GROUP(sb), i);
  1434. if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
  1435. /*
  1436. * IF we have corrupt bitmap, we won't find any
  1437. * free blocks even though group info says we
  1438. * we have free blocks
  1439. */
  1440. ext4_grp_locked_error(sb, e4b->bd_group,
  1441. __func__, "%d free blocks as per "
  1442. "group info. But bitmap says 0",
  1443. free);
  1444. break;
  1445. }
  1446. mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
  1447. BUG_ON(ex.fe_len <= 0);
  1448. if (free < ex.fe_len) {
  1449. ext4_grp_locked_error(sb, e4b->bd_group,
  1450. __func__, "%d free blocks as per "
  1451. "group info. But got %d blocks",
  1452. free, ex.fe_len);
  1453. /*
  1454. * The number of free blocks differs. This mostly
  1455. * indicate that the bitmap is corrupt. So exit
  1456. * without claiming the space.
  1457. */
  1458. break;
  1459. }
  1460. ext4_mb_measure_extent(ac, &ex, e4b);
  1461. i += ex.fe_len;
  1462. free -= ex.fe_len;
  1463. }
  1464. ext4_mb_check_limits(ac, e4b, 1);
  1465. }
  1466. /*
  1467. * This is a special case for storages like raid5
  1468. * we try to find stripe-aligned chunks for stripe-size requests
  1469. * XXX should do so at least for multiples of stripe size as well
  1470. */
  1471. static void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
  1472. struct ext4_buddy *e4b)
  1473. {
  1474. struct super_block *sb = ac->ac_sb;
  1475. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1476. void *bitmap = EXT4_MB_BITMAP(e4b);
  1477. struct ext4_free_extent ex;
  1478. ext4_fsblk_t first_group_block;
  1479. ext4_fsblk_t a;
  1480. ext4_grpblk_t i;
  1481. int max;
  1482. BUG_ON(sbi->s_stripe == 0);
  1483. /* find first stripe-aligned block in group */
  1484. first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb)
  1485. + le32_to_cpu(sbi->s_es->s_first_data_block);
  1486. a = first_group_block + sbi->s_stripe - 1;
  1487. do_div(a, sbi->s_stripe);
  1488. i = (a * sbi->s_stripe) - first_group_block;
  1489. while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
  1490. if (!mb_test_bit(i, bitmap)) {
  1491. max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
  1492. if (max >= sbi->s_stripe) {
  1493. ac->ac_found++;
  1494. ac->ac_b_ex = ex;
  1495. ext4_mb_use_best_found(ac, e4b);
  1496. break;
  1497. }
  1498. }
  1499. i += sbi->s_stripe;
  1500. }
  1501. }
  1502. static int ext4_mb_good_group(struct ext4_allocation_context *ac,
  1503. ext4_group_t group, int cr)
  1504. {
  1505. unsigned free, fragments;
  1506. unsigned i, bits;
  1507. struct ext4_group_desc *desc;
  1508. struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
  1509. BUG_ON(cr < 0 || cr >= 4);
  1510. BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
  1511. free = grp->bb_free;
  1512. fragments = grp->bb_fragments;
  1513. if (free == 0)
  1514. return 0;
  1515. if (fragments == 0)
  1516. return 0;
  1517. switch (cr) {
  1518. case 0:
  1519. BUG_ON(ac->ac_2order == 0);
  1520. /* If this group is uninitialized, skip it initially */
  1521. desc = ext4_get_group_desc(ac->ac_sb, group, NULL);
  1522. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
  1523. return 0;
  1524. bits = ac->ac_sb->s_blocksize_bits + 1;
  1525. for (i = ac->ac_2order; i <= bits; i++)
  1526. if (grp->bb_counters[i] > 0)
  1527. return 1;
  1528. break;
  1529. case 1:
  1530. if ((free / fragments) >= ac->ac_g_ex.fe_len)
  1531. return 1;
  1532. break;
  1533. case 2:
  1534. if (free >= ac->ac_g_ex.fe_len)
  1535. return 1;
  1536. break;
  1537. case 3:
  1538. return 1;
  1539. default:
  1540. BUG();
  1541. }
  1542. return 0;
  1543. }
  1544. /*
  1545. * lock the group_info alloc_sem of all the groups
  1546. * belonging to the same buddy cache page. This
  1547. * make sure other parallel operation on the buddy
  1548. * cache doesn't happen whild holding the buddy cache
  1549. * lock
  1550. */
  1551. int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
  1552. {
  1553. int i;
  1554. int block, pnum;
  1555. int blocks_per_page;
  1556. int groups_per_page;
  1557. ext4_group_t first_group;
  1558. struct ext4_group_info *grp;
  1559. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1560. /*
  1561. * the buddy cache inode stores the block bitmap
  1562. * and buddy information in consecutive blocks.
  1563. * So for each group we need two blocks.
  1564. */
  1565. block = group * 2;
  1566. pnum = block / blocks_per_page;
  1567. first_group = pnum * blocks_per_page / 2;
  1568. groups_per_page = blocks_per_page >> 1;
  1569. if (groups_per_page == 0)
  1570. groups_per_page = 1;
  1571. /* read all groups the page covers into the cache */
  1572. for (i = 0; i < groups_per_page; i++) {
  1573. if ((first_group + i) >= EXT4_SB(sb)->s_groups_count)
  1574. break;
  1575. grp = ext4_get_group_info(sb, first_group + i);
  1576. /* take all groups write allocation
  1577. * semaphore. This make sure there is
  1578. * no block allocation going on in any
  1579. * of that groups
  1580. */
  1581. down_write_nested(&grp->alloc_sem, i);
  1582. }
  1583. return i;
  1584. }
  1585. void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
  1586. ext4_group_t group, int locked_group)
  1587. {
  1588. int i;
  1589. int block, pnum;
  1590. int blocks_per_page;
  1591. ext4_group_t first_group;
  1592. struct ext4_group_info *grp;
  1593. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1594. /*
  1595. * the buddy cache inode stores the block bitmap
  1596. * and buddy information in consecutive blocks.
  1597. * So for each group we need two blocks.
  1598. */
  1599. block = group * 2;
  1600. pnum = block / blocks_per_page;
  1601. first_group = pnum * blocks_per_page / 2;
  1602. /* release locks on all the groups */
  1603. for (i = 0; i < locked_group; i++) {
  1604. grp = ext4_get_group_info(sb, first_group + i);
  1605. /* take all groups write allocation
  1606. * semaphore. This make sure there is
  1607. * no block allocation going on in any
  1608. * of that groups
  1609. */
  1610. up_write(&grp->alloc_sem);
  1611. }
  1612. }
  1613. static int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
  1614. {
  1615. int ret;
  1616. void *bitmap;
  1617. int blocks_per_page;
  1618. int block, pnum, poff;
  1619. int num_grp_locked = 0;
  1620. struct ext4_group_info *this_grp;
  1621. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1622. struct inode *inode = sbi->s_buddy_cache;
  1623. struct page *page = NULL, *bitmap_page = NULL;
  1624. mb_debug("init group %lu\n", group);
  1625. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1626. this_grp = ext4_get_group_info(sb, group);
  1627. /*
  1628. * This ensures we don't add group
  1629. * to this buddy cache via resize
  1630. */
  1631. num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
  1632. if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
  1633. /*
  1634. * somebody initialized the group
  1635. * return without doing anything
  1636. */
  1637. ret = 0;
  1638. goto err;
  1639. }
  1640. /*
  1641. * the buddy cache inode stores the block bitmap
  1642. * and buddy information in consecutive blocks.
  1643. * So for each group we need two blocks.
  1644. */
  1645. block = group * 2;
  1646. pnum = block / blocks_per_page;
  1647. poff = block % blocks_per_page;
  1648. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1649. if (page) {
  1650. BUG_ON(page->mapping != inode->i_mapping);
  1651. ret = ext4_mb_init_cache(page, NULL);
  1652. if (ret) {
  1653. unlock_page(page);
  1654. goto err;
  1655. }
  1656. unlock_page(page);
  1657. }
  1658. if (page == NULL || !PageUptodate(page)) {
  1659. ret = -EIO;
  1660. goto err;
  1661. }
  1662. mark_page_accessed(page);
  1663. bitmap_page = page;
  1664. bitmap = page_address(page) + (poff * sb->s_blocksize);
  1665. /* init buddy cache */
  1666. block++;
  1667. pnum = block / blocks_per_page;
  1668. poff = block % blocks_per_page;
  1669. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1670. if (page == bitmap_page) {
  1671. /*
  1672. * If both the bitmap and buddy are in
  1673. * the same page we don't need to force
  1674. * init the buddy
  1675. */
  1676. unlock_page(page);
  1677. } else if (page) {
  1678. BUG_ON(page->mapping != inode->i_mapping);
  1679. ret = ext4_mb_init_cache(page, bitmap);
  1680. if (ret) {
  1681. unlock_page(page);
  1682. goto err;
  1683. }
  1684. unlock_page(page);
  1685. }
  1686. if (page == NULL || !PageUptodate(page)) {
  1687. ret = -EIO;
  1688. goto err;
  1689. }
  1690. mark_page_accessed(page);
  1691. err:
  1692. ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
  1693. if (bitmap_page)
  1694. page_cache_release(bitmap_page);
  1695. if (page)
  1696. page_cache_release(page);
  1697. return ret;
  1698. }
  1699. static noinline_for_stack int
  1700. ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
  1701. {
  1702. ext4_group_t group;
  1703. ext4_group_t i;
  1704. int cr;
  1705. int err = 0;
  1706. int bsbits;
  1707. struct ext4_sb_info *sbi;
  1708. struct super_block *sb;
  1709. struct ext4_buddy e4b;
  1710. loff_t size, isize;
  1711. sb = ac->ac_sb;
  1712. sbi = EXT4_SB(sb);
  1713. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1714. /* first, try the goal */
  1715. err = ext4_mb_find_by_goal(ac, &e4b);
  1716. if (err || ac->ac_status == AC_STATUS_FOUND)
  1717. goto out;
  1718. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  1719. goto out;
  1720. /*
  1721. * ac->ac2_order is set only if the fe_len is a power of 2
  1722. * if ac2_order is set we also set criteria to 0 so that we
  1723. * try exact allocation using buddy.
  1724. */
  1725. i = fls(ac->ac_g_ex.fe_len);
  1726. ac->ac_2order = 0;
  1727. /*
  1728. * We search using buddy data only if the order of the request
  1729. * is greater than equal to the sbi_s_mb_order2_reqs
  1730. * You can tune it via /proc/fs/ext4/<partition>/order2_req
  1731. */
  1732. if (i >= sbi->s_mb_order2_reqs) {
  1733. /*
  1734. * This should tell if fe_len is exactly power of 2
  1735. */
  1736. if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
  1737. ac->ac_2order = i - 1;
  1738. }
  1739. bsbits = ac->ac_sb->s_blocksize_bits;
  1740. /* if stream allocation is enabled, use global goal */
  1741. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  1742. isize = i_size_read(ac->ac_inode) >> bsbits;
  1743. if (size < isize)
  1744. size = isize;
  1745. if (size < sbi->s_mb_stream_request &&
  1746. (ac->ac_flags & EXT4_MB_HINT_DATA)) {
  1747. /* TBD: may be hot point */
  1748. spin_lock(&sbi->s_md_lock);
  1749. ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
  1750. ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
  1751. spin_unlock(&sbi->s_md_lock);
  1752. }
  1753. /* Let's just scan groups to find more-less suitable blocks */
  1754. cr = ac->ac_2order ? 0 : 1;
  1755. /*
  1756. * cr == 0 try to get exact allocation,
  1757. * cr == 3 try to get anything
  1758. */
  1759. repeat:
  1760. for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
  1761. ac->ac_criteria = cr;
  1762. /*
  1763. * searching for the right group start
  1764. * from the goal value specified
  1765. */
  1766. group = ac->ac_g_ex.fe_group;
  1767. for (i = 0; i < EXT4_SB(sb)->s_groups_count; group++, i++) {
  1768. struct ext4_group_info *grp;
  1769. struct ext4_group_desc *desc;
  1770. if (group == EXT4_SB(sb)->s_groups_count)
  1771. group = 0;
  1772. /* quick check to skip empty groups */
  1773. grp = ext4_get_group_info(sb, group);
  1774. if (grp->bb_free == 0)
  1775. continue;
  1776. /*
  1777. * if the group is already init we check whether it is
  1778. * a good group and if not we don't load the buddy
  1779. */
  1780. if (EXT4_MB_GRP_NEED_INIT(grp)) {
  1781. /*
  1782. * we need full data about the group
  1783. * to make a good selection
  1784. */
  1785. err = ext4_mb_init_group(sb, group);
  1786. if (err)
  1787. goto out;
  1788. }
  1789. /*
  1790. * If the particular group doesn't satisfy our
  1791. * criteria we continue with the next group
  1792. */
  1793. if (!ext4_mb_good_group(ac, group, cr))
  1794. continue;
  1795. err = ext4_mb_load_buddy(sb, group, &e4b);
  1796. if (err)
  1797. goto out;
  1798. ext4_lock_group(sb, group);
  1799. if (!ext4_mb_good_group(ac, group, cr)) {
  1800. /* someone did allocation from this group */
  1801. ext4_unlock_group(sb, group);
  1802. ext4_mb_release_desc(&e4b);
  1803. continue;
  1804. }
  1805. ac->ac_groups_scanned++;
  1806. desc = ext4_get_group_desc(sb, group, NULL);
  1807. if (cr == 0 || (desc->bg_flags &
  1808. cpu_to_le16(EXT4_BG_BLOCK_UNINIT) &&
  1809. ac->ac_2order != 0))
  1810. ext4_mb_simple_scan_group(ac, &e4b);
  1811. else if (cr == 1 &&
  1812. ac->ac_g_ex.fe_len == sbi->s_stripe)
  1813. ext4_mb_scan_aligned(ac, &e4b);
  1814. else
  1815. ext4_mb_complex_scan_group(ac, &e4b);
  1816. ext4_unlock_group(sb, group);
  1817. ext4_mb_release_desc(&e4b);
  1818. if (ac->ac_status != AC_STATUS_CONTINUE)
  1819. break;
  1820. }
  1821. }
  1822. if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
  1823. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1824. /*
  1825. * We've been searching too long. Let's try to allocate
  1826. * the best chunk we've found so far
  1827. */
  1828. ext4_mb_try_best_found(ac, &e4b);
  1829. if (ac->ac_status != AC_STATUS_FOUND) {
  1830. /*
  1831. * Someone more lucky has already allocated it.
  1832. * The only thing we can do is just take first
  1833. * found block(s)
  1834. printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
  1835. */
  1836. ac->ac_b_ex.fe_group = 0;
  1837. ac->ac_b_ex.fe_start = 0;
  1838. ac->ac_b_ex.fe_len = 0;
  1839. ac->ac_status = AC_STATUS_CONTINUE;
  1840. ac->ac_flags |= EXT4_MB_HINT_FIRST;
  1841. cr = 3;
  1842. atomic_inc(&sbi->s_mb_lost_chunks);
  1843. goto repeat;
  1844. }
  1845. }
  1846. out:
  1847. return err;
  1848. }
  1849. #ifdef EXT4_MB_HISTORY
  1850. struct ext4_mb_proc_session {
  1851. struct ext4_mb_history *history;
  1852. struct super_block *sb;
  1853. int start;
  1854. int max;
  1855. };
  1856. static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session *s,
  1857. struct ext4_mb_history *hs,
  1858. int first)
  1859. {
  1860. if (hs == s->history + s->max)
  1861. hs = s->history;
  1862. if (!first && hs == s->history + s->start)
  1863. return NULL;
  1864. while (hs->orig.fe_len == 0) {
  1865. hs++;
  1866. if (hs == s->history + s->max)
  1867. hs = s->history;
  1868. if (hs == s->history + s->start)
  1869. return NULL;
  1870. }
  1871. return hs;
  1872. }
  1873. static void *ext4_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
  1874. {
  1875. struct ext4_mb_proc_session *s = seq->private;
  1876. struct ext4_mb_history *hs;
  1877. int l = *pos;
  1878. if (l == 0)
  1879. return SEQ_START_TOKEN;
  1880. hs = ext4_mb_history_skip_empty(s, s->history + s->start, 1);
  1881. if (!hs)
  1882. return NULL;
  1883. while (--l && (hs = ext4_mb_history_skip_empty(s, ++hs, 0)) != NULL);
  1884. return hs;
  1885. }
  1886. static void *ext4_mb_seq_history_next(struct seq_file *seq, void *v,
  1887. loff_t *pos)
  1888. {
  1889. struct ext4_mb_proc_session *s = seq->private;
  1890. struct ext4_mb_history *hs = v;
  1891. ++*pos;
  1892. if (v == SEQ_START_TOKEN)
  1893. return ext4_mb_history_skip_empty(s, s->history + s->start, 1);
  1894. else
  1895. return ext4_mb_history_skip_empty(s, ++hs, 0);
  1896. }
  1897. static int ext4_mb_seq_history_show(struct seq_file *seq, void *v)
  1898. {
  1899. char buf[25], buf2[25], buf3[25], *fmt;
  1900. struct ext4_mb_history *hs = v;
  1901. if (v == SEQ_START_TOKEN) {
  1902. seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
  1903. "%-5s %-2s %-5s %-5s %-5s %-6s\n",
  1904. "pid", "inode", "original", "goal", "result", "found",
  1905. "grps", "cr", "flags", "merge", "tail", "broken");
  1906. return 0;
  1907. }
  1908. if (hs->op == EXT4_MB_HISTORY_ALLOC) {
  1909. fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
  1910. "%-5u %-5s %-5u %-6u\n";
  1911. sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
  1912. hs->result.fe_start, hs->result.fe_len,
  1913. hs->result.fe_logical);
  1914. sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
  1915. hs->orig.fe_start, hs->orig.fe_len,
  1916. hs->orig.fe_logical);
  1917. sprintf(buf3, "%u/%d/%u@%u", hs->goal.fe_group,
  1918. hs->goal.fe_start, hs->goal.fe_len,
  1919. hs->goal.fe_logical);
  1920. seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
  1921. hs->found, hs->groups, hs->cr, hs->flags,
  1922. hs->merged ? "M" : "", hs->tail,
  1923. hs->buddy ? 1 << hs->buddy : 0);
  1924. } else if (hs->op == EXT4_MB_HISTORY_PREALLOC) {
  1925. fmt = "%-5u %-8u %-23s %-23s %-23s\n";
  1926. sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
  1927. hs->result.fe_start, hs->result.fe_len,
  1928. hs->result.fe_logical);
  1929. sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
  1930. hs->orig.fe_start, hs->orig.fe_len,
  1931. hs->orig.fe_logical);
  1932. seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
  1933. } else if (hs->op == EXT4_MB_HISTORY_DISCARD) {
  1934. sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
  1935. hs->result.fe_start, hs->result.fe_len);
  1936. seq_printf(seq, "%-5u %-8u %-23s discard\n",
  1937. hs->pid, hs->ino, buf2);
  1938. } else if (hs->op == EXT4_MB_HISTORY_FREE) {
  1939. sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
  1940. hs->result.fe_start, hs->result.fe_len);
  1941. seq_printf(seq, "%-5u %-8u %-23s free\n",
  1942. hs->pid, hs->ino, buf2);
  1943. }
  1944. return 0;
  1945. }
  1946. static void ext4_mb_seq_history_stop(struct seq_file *seq, void *v)
  1947. {
  1948. }
  1949. static struct seq_operations ext4_mb_seq_history_ops = {
  1950. .start = ext4_mb_seq_history_start,
  1951. .next = ext4_mb_seq_history_next,
  1952. .stop = ext4_mb_seq_history_stop,
  1953. .show = ext4_mb_seq_history_show,
  1954. };
  1955. static int ext4_mb_seq_history_open(struct inode *inode, struct file *file)
  1956. {
  1957. struct super_block *sb = PDE(inode)->data;
  1958. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1959. struct ext4_mb_proc_session *s;
  1960. int rc;
  1961. int size;
  1962. if (unlikely(sbi->s_mb_history == NULL))
  1963. return -ENOMEM;
  1964. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1965. if (s == NULL)
  1966. return -ENOMEM;
  1967. s->sb = sb;
  1968. size = sizeof(struct ext4_mb_history) * sbi->s_mb_history_max;
  1969. s->history = kmalloc(size, GFP_KERNEL);
  1970. if (s->history == NULL) {
  1971. kfree(s);
  1972. return -ENOMEM;
  1973. }
  1974. spin_lock(&sbi->s_mb_history_lock);
  1975. memcpy(s->history, sbi->s_mb_history, size);
  1976. s->max = sbi->s_mb_history_max;
  1977. s->start = sbi->s_mb_history_cur % s->max;
  1978. spin_unlock(&sbi->s_mb_history_lock);
  1979. rc = seq_open(file, &ext4_mb_seq_history_ops);
  1980. if (rc == 0) {
  1981. struct seq_file *m = (struct seq_file *)file->private_data;
  1982. m->private = s;
  1983. } else {
  1984. kfree(s->history);
  1985. kfree(s);
  1986. }
  1987. return rc;
  1988. }
  1989. static int ext4_mb_seq_history_release(struct inode *inode, struct file *file)
  1990. {
  1991. struct seq_file *seq = (struct seq_file *)file->private_data;
  1992. struct ext4_mb_proc_session *s = seq->private;
  1993. kfree(s->history);
  1994. kfree(s);
  1995. return seq_release(inode, file);
  1996. }
  1997. static ssize_t ext4_mb_seq_history_write(struct file *file,
  1998. const char __user *buffer,
  1999. size_t count, loff_t *ppos)
  2000. {
  2001. struct seq_file *seq = (struct seq_file *)file->private_data;
  2002. struct ext4_mb_proc_session *s = seq->private;
  2003. struct super_block *sb = s->sb;
  2004. char str[32];
  2005. int value;
  2006. if (count >= sizeof(str)) {
  2007. printk(KERN_ERR "EXT4-fs: %s string too long, max %u bytes\n",
  2008. "mb_history", (int)sizeof(str));
  2009. return -EOVERFLOW;
  2010. }
  2011. if (copy_from_user(str, buffer, count))
  2012. return -EFAULT;
  2013. value = simple_strtol(str, NULL, 0);
  2014. if (value < 0)
  2015. return -ERANGE;
  2016. EXT4_SB(sb)->s_mb_history_filter = value;
  2017. return count;
  2018. }
  2019. static struct file_operations ext4_mb_seq_history_fops = {
  2020. .owner = THIS_MODULE,
  2021. .open = ext4_mb_seq_history_open,
  2022. .read = seq_read,
  2023. .write = ext4_mb_seq_history_write,
  2024. .llseek = seq_lseek,
  2025. .release = ext4_mb_seq_history_release,
  2026. };
  2027. static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
  2028. {
  2029. struct super_block *sb = seq->private;
  2030. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2031. ext4_group_t group;
  2032. if (*pos < 0 || *pos >= sbi->s_groups_count)
  2033. return NULL;
  2034. group = *pos + 1;
  2035. return (void *) ((unsigned long) group);
  2036. }
  2037. static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
  2038. {
  2039. struct super_block *sb = seq->private;
  2040. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2041. ext4_group_t group;
  2042. ++*pos;
  2043. if (*pos < 0 || *pos >= sbi->s_groups_count)
  2044. return NULL;
  2045. group = *pos + 1;
  2046. return (void *) ((unsigned long) group);
  2047. }
  2048. static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
  2049. {
  2050. struct super_block *sb = seq->private;
  2051. ext4_group_t group = (ext4_group_t) ((unsigned long) v);
  2052. int i;
  2053. int err;
  2054. struct ext4_buddy e4b;
  2055. struct sg {
  2056. struct ext4_group_info info;
  2057. unsigned short counters[16];
  2058. } sg;
  2059. group--;
  2060. if (group == 0)
  2061. seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
  2062. "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
  2063. "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
  2064. "group", "free", "frags", "first",
  2065. "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
  2066. "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
  2067. i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
  2068. sizeof(struct ext4_group_info);
  2069. err = ext4_mb_load_buddy(sb, group, &e4b);
  2070. if (err) {
  2071. seq_printf(seq, "#%-5u: I/O error\n", group);
  2072. return 0;
  2073. }
  2074. ext4_lock_group(sb, group);
  2075. memcpy(&sg, ext4_get_group_info(sb, group), i);
  2076. ext4_unlock_group(sb, group);
  2077. ext4_mb_release_desc(&e4b);
  2078. seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
  2079. sg.info.bb_fragments, sg.info.bb_first_free);
  2080. for (i = 0; i <= 13; i++)
  2081. seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
  2082. sg.info.bb_counters[i] : 0);
  2083. seq_printf(seq, " ]\n");
  2084. return 0;
  2085. }
  2086. static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
  2087. {
  2088. }
  2089. static struct seq_operations ext4_mb_seq_groups_ops = {
  2090. .start = ext4_mb_seq_groups_start,
  2091. .next = ext4_mb_seq_groups_next,
  2092. .stop = ext4_mb_seq_groups_stop,
  2093. .show = ext4_mb_seq_groups_show,
  2094. };
  2095. static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
  2096. {
  2097. struct super_block *sb = PDE(inode)->data;
  2098. int rc;
  2099. rc = seq_open(file, &ext4_mb_seq_groups_ops);
  2100. if (rc == 0) {
  2101. struct seq_file *m = (struct seq_file *)file->private_data;
  2102. m->private = sb;
  2103. }
  2104. return rc;
  2105. }
  2106. static struct file_operations ext4_mb_seq_groups_fops = {
  2107. .owner = THIS_MODULE,
  2108. .open = ext4_mb_seq_groups_open,
  2109. .read = seq_read,
  2110. .llseek = seq_lseek,
  2111. .release = seq_release,
  2112. };
  2113. static void ext4_mb_history_release(struct super_block *sb)
  2114. {
  2115. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2116. if (sbi->s_proc != NULL) {
  2117. remove_proc_entry("mb_groups", sbi->s_proc);
  2118. remove_proc_entry("mb_history", sbi->s_proc);
  2119. }
  2120. kfree(sbi->s_mb_history);
  2121. }
  2122. static void ext4_mb_history_init(struct super_block *sb)
  2123. {
  2124. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2125. int i;
  2126. if (sbi->s_proc != NULL) {
  2127. proc_create_data("mb_history", S_IRUGO, sbi->s_proc,
  2128. &ext4_mb_seq_history_fops, sb);
  2129. proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
  2130. &ext4_mb_seq_groups_fops, sb);
  2131. }
  2132. sbi->s_mb_history_max = 1000;
  2133. sbi->s_mb_history_cur = 0;
  2134. spin_lock_init(&sbi->s_mb_history_lock);
  2135. i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history);
  2136. sbi->s_mb_history = kzalloc(i, GFP_KERNEL);
  2137. /* if we can't allocate history, then we simple won't use it */
  2138. }
  2139. static noinline_for_stack void
  2140. ext4_mb_store_history(struct ext4_allocation_context *ac)
  2141. {
  2142. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2143. struct ext4_mb_history h;
  2144. if (unlikely(sbi->s_mb_history == NULL))
  2145. return;
  2146. if (!(ac->ac_op & sbi->s_mb_history_filter))
  2147. return;
  2148. h.op = ac->ac_op;
  2149. h.pid = current->pid;
  2150. h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
  2151. h.orig = ac->ac_o_ex;
  2152. h.result = ac->ac_b_ex;
  2153. h.flags = ac->ac_flags;
  2154. h.found = ac->ac_found;
  2155. h.groups = ac->ac_groups_scanned;
  2156. h.cr = ac->ac_criteria;
  2157. h.tail = ac->ac_tail;
  2158. h.buddy = ac->ac_buddy;
  2159. h.merged = 0;
  2160. if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
  2161. if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
  2162. ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
  2163. h.merged = 1;
  2164. h.goal = ac->ac_g_ex;
  2165. h.result = ac->ac_f_ex;
  2166. }
  2167. spin_lock(&sbi->s_mb_history_lock);
  2168. memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
  2169. if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
  2170. sbi->s_mb_history_cur = 0;
  2171. spin_unlock(&sbi->s_mb_history_lock);
  2172. }
  2173. #else
  2174. #define ext4_mb_history_release(sb)
  2175. #define ext4_mb_history_init(sb)
  2176. #endif
  2177. /* Create and initialize ext4_group_info data for the given group. */
  2178. int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
  2179. struct ext4_group_desc *desc)
  2180. {
  2181. int i, len;
  2182. int metalen = 0;
  2183. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2184. struct ext4_group_info **meta_group_info;
  2185. /*
  2186. * First check if this group is the first of a reserved block.
  2187. * If it's true, we have to allocate a new table of pointers
  2188. * to ext4_group_info structures
  2189. */
  2190. if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
  2191. metalen = sizeof(*meta_group_info) <<
  2192. EXT4_DESC_PER_BLOCK_BITS(sb);
  2193. meta_group_info = kmalloc(metalen, GFP_KERNEL);
  2194. if (meta_group_info == NULL) {
  2195. printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
  2196. "buddy group\n");
  2197. goto exit_meta_group_info;
  2198. }
  2199. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
  2200. meta_group_info;
  2201. }
  2202. /*
  2203. * calculate needed size. if change bb_counters size,
  2204. * don't forget about ext4_mb_generate_buddy()
  2205. */
  2206. len = offsetof(typeof(**meta_group_info),
  2207. bb_counters[sb->s_blocksize_bits + 2]);
  2208. meta_group_info =
  2209. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
  2210. i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
  2211. meta_group_info[i] = kzalloc(len, GFP_KERNEL);
  2212. if (meta_group_info[i] == NULL) {
  2213. printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
  2214. goto exit_group_info;
  2215. }
  2216. set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
  2217. &(meta_group_info[i]->bb_state));
  2218. /*
  2219. * initialize bb_free to be able to skip
  2220. * empty groups without initialization
  2221. */
  2222. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2223. meta_group_info[i]->bb_free =
  2224. ext4_free_blocks_after_init(sb, group, desc);
  2225. } else {
  2226. meta_group_info[i]->bb_free =
  2227. le16_to_cpu(desc->bg_free_blocks_count);
  2228. }
  2229. INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
  2230. init_rwsem(&meta_group_info[i]->alloc_sem);
  2231. meta_group_info[i]->bb_free_root.rb_node = NULL;;
  2232. #ifdef DOUBLE_CHECK
  2233. {
  2234. struct buffer_head *bh;
  2235. meta_group_info[i]->bb_bitmap =
  2236. kmalloc(sb->s_blocksize, GFP_KERNEL);
  2237. BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
  2238. bh = ext4_read_block_bitmap(sb, group);
  2239. BUG_ON(bh == NULL);
  2240. memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
  2241. sb->s_blocksize);
  2242. put_bh(bh);
  2243. }
  2244. #endif
  2245. return 0;
  2246. exit_group_info:
  2247. /* If a meta_group_info table has been allocated, release it now */
  2248. if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
  2249. kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
  2250. exit_meta_group_info:
  2251. return -ENOMEM;
  2252. } /* ext4_mb_add_groupinfo */
  2253. /*
  2254. * Update an existing group.
  2255. * This function is used for online resize
  2256. */
  2257. void ext4_mb_update_group_info(struct ext4_group_info *grp, ext4_grpblk_t add)
  2258. {
  2259. grp->bb_free += add;
  2260. }
  2261. static int ext4_mb_init_backend(struct super_block *sb)
  2262. {
  2263. ext4_group_t i;
  2264. int metalen;
  2265. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2266. struct ext4_super_block *es = sbi->s_es;
  2267. int num_meta_group_infos;
  2268. int num_meta_group_infos_max;
  2269. int array_size;
  2270. struct ext4_group_info **meta_group_info;
  2271. struct ext4_group_desc *desc;
  2272. /* This is the number of blocks used by GDT */
  2273. num_meta_group_infos = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) -
  2274. 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
  2275. /*
  2276. * This is the total number of blocks used by GDT including
  2277. * the number of reserved blocks for GDT.
  2278. * The s_group_info array is allocated with this value
  2279. * to allow a clean online resize without a complex
  2280. * manipulation of pointer.
  2281. * The drawback is the unused memory when no resize
  2282. * occurs but it's very low in terms of pages
  2283. * (see comments below)
  2284. * Need to handle this properly when META_BG resizing is allowed
  2285. */
  2286. num_meta_group_infos_max = num_meta_group_infos +
  2287. le16_to_cpu(es->s_reserved_gdt_blocks);
  2288. /*
  2289. * array_size is the size of s_group_info array. We round it
  2290. * to the next power of two because this approximation is done
  2291. * internally by kmalloc so we can have some more memory
  2292. * for free here (e.g. may be used for META_BG resize).
  2293. */
  2294. array_size = 1;
  2295. while (array_size < sizeof(*sbi->s_group_info) *
  2296. num_meta_group_infos_max)
  2297. array_size = array_size << 1;
  2298. /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
  2299. * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
  2300. * So a two level scheme suffices for now. */
  2301. sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
  2302. if (sbi->s_group_info == NULL) {
  2303. printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
  2304. return -ENOMEM;
  2305. }
  2306. sbi->s_buddy_cache = new_inode(sb);
  2307. if (sbi->s_buddy_cache == NULL) {
  2308. printk(KERN_ERR "EXT4-fs: can't get new inode\n");
  2309. goto err_freesgi;
  2310. }
  2311. EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
  2312. metalen = sizeof(*meta_group_info) << EXT4_DESC_PER_BLOCK_BITS(sb);
  2313. for (i = 0; i < num_meta_group_infos; i++) {
  2314. if ((i + 1) == num_meta_group_infos)
  2315. metalen = sizeof(*meta_group_info) *
  2316. (sbi->s_groups_count -
  2317. (i << EXT4_DESC_PER_BLOCK_BITS(sb)));
  2318. meta_group_info = kmalloc(metalen, GFP_KERNEL);
  2319. if (meta_group_info == NULL) {
  2320. printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
  2321. "buddy group\n");
  2322. goto err_freemeta;
  2323. }
  2324. sbi->s_group_info[i] = meta_group_info;
  2325. }
  2326. for (i = 0; i < sbi->s_groups_count; i++) {
  2327. desc = ext4_get_group_desc(sb, i, NULL);
  2328. if (desc == NULL) {
  2329. printk(KERN_ERR
  2330. "EXT4-fs: can't read descriptor %u\n", i);
  2331. goto err_freebuddy;
  2332. }
  2333. if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
  2334. goto err_freebuddy;
  2335. }
  2336. return 0;
  2337. err_freebuddy:
  2338. while (i-- > 0)
  2339. kfree(ext4_get_group_info(sb, i));
  2340. i = num_meta_group_infos;
  2341. err_freemeta:
  2342. while (i-- > 0)
  2343. kfree(sbi->s_group_info[i]);
  2344. iput(sbi->s_buddy_cache);
  2345. err_freesgi:
  2346. kfree(sbi->s_group_info);
  2347. return -ENOMEM;
  2348. }
  2349. int ext4_mb_init(struct super_block *sb, int needs_recovery)
  2350. {
  2351. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2352. unsigned i, j;
  2353. unsigned offset;
  2354. unsigned max;
  2355. int ret;
  2356. i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
  2357. sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
  2358. if (sbi->s_mb_offsets == NULL) {
  2359. return -ENOMEM;
  2360. }
  2361. i = (sb->s_blocksize_bits + 2) * sizeof(unsigned int);
  2362. sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
  2363. if (sbi->s_mb_maxs == NULL) {
  2364. kfree(sbi->s_mb_maxs);
  2365. return -ENOMEM;
  2366. }
  2367. /* order 0 is regular bitmap */
  2368. sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
  2369. sbi->s_mb_offsets[0] = 0;
  2370. i = 1;
  2371. offset = 0;
  2372. max = sb->s_blocksize << 2;
  2373. do {
  2374. sbi->s_mb_offsets[i] = offset;
  2375. sbi->s_mb_maxs[i] = max;
  2376. offset += 1 << (sb->s_blocksize_bits - i);
  2377. max = max >> 1;
  2378. i++;
  2379. } while (i <= sb->s_blocksize_bits + 1);
  2380. /* init file for buddy data */
  2381. ret = ext4_mb_init_backend(sb);
  2382. if (ret != 0) {
  2383. kfree(sbi->s_mb_offsets);
  2384. kfree(sbi->s_mb_maxs);
  2385. return ret;
  2386. }
  2387. spin_lock_init(&sbi->s_md_lock);
  2388. spin_lock_init(&sbi->s_bal_lock);
  2389. sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
  2390. sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
  2391. sbi->s_mb_stats = MB_DEFAULT_STATS;
  2392. sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
  2393. sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
  2394. sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
  2395. sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
  2396. sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
  2397. if (sbi->s_locality_groups == NULL) {
  2398. kfree(sbi->s_mb_offsets);
  2399. kfree(sbi->s_mb_maxs);
  2400. return -ENOMEM;
  2401. }
  2402. for_each_possible_cpu(i) {
  2403. struct ext4_locality_group *lg;
  2404. lg = per_cpu_ptr(sbi->s_locality_groups, i);
  2405. mutex_init(&lg->lg_mutex);
  2406. for (j = 0; j < PREALLOC_TB_SIZE; j++)
  2407. INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
  2408. spin_lock_init(&lg->lg_prealloc_lock);
  2409. }
  2410. ext4_mb_init_per_dev_proc(sb);
  2411. ext4_mb_history_init(sb);
  2412. if (sbi->s_journal)
  2413. sbi->s_journal->j_commit_callback = release_blocks_on_commit;
  2414. printk(KERN_INFO "EXT4-fs: mballoc enabled\n");
  2415. return 0;
  2416. }
  2417. /* need to called with ext4 group lock (ext4_lock_group) */
  2418. static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
  2419. {
  2420. struct ext4_prealloc_space *pa;
  2421. struct list_head *cur, *tmp;
  2422. int count = 0;
  2423. list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
  2424. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  2425. list_del(&pa->pa_group_list);
  2426. count++;
  2427. kmem_cache_free(ext4_pspace_cachep, pa);
  2428. }
  2429. if (count)
  2430. mb_debug("mballoc: %u PAs left\n", count);
  2431. }
  2432. int ext4_mb_release(struct super_block *sb)
  2433. {
  2434. ext4_group_t i;
  2435. int num_meta_group_infos;
  2436. struct ext4_group_info *grinfo;
  2437. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2438. if (sbi->s_group_info) {
  2439. for (i = 0; i < sbi->s_groups_count; i++) {
  2440. grinfo = ext4_get_group_info(sb, i);
  2441. #ifdef DOUBLE_CHECK
  2442. kfree(grinfo->bb_bitmap);
  2443. #endif
  2444. ext4_lock_group(sb, i);
  2445. ext4_mb_cleanup_pa(grinfo);
  2446. ext4_unlock_group(sb, i);
  2447. kfree(grinfo);
  2448. }
  2449. num_meta_group_infos = (sbi->s_groups_count +
  2450. EXT4_DESC_PER_BLOCK(sb) - 1) >>
  2451. EXT4_DESC_PER_BLOCK_BITS(sb);
  2452. for (i = 0; i < num_meta_group_infos; i++)
  2453. kfree(sbi->s_group_info[i]);
  2454. kfree(sbi->s_group_info);
  2455. }
  2456. kfree(sbi->s_mb_offsets);
  2457. kfree(sbi->s_mb_maxs);
  2458. if (sbi->s_buddy_cache)
  2459. iput(sbi->s_buddy_cache);
  2460. if (sbi->s_mb_stats) {
  2461. printk(KERN_INFO
  2462. "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
  2463. atomic_read(&sbi->s_bal_allocated),
  2464. atomic_read(&sbi->s_bal_reqs),
  2465. atomic_read(&sbi->s_bal_success));
  2466. printk(KERN_INFO
  2467. "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
  2468. "%u 2^N hits, %u breaks, %u lost\n",
  2469. atomic_read(&sbi->s_bal_ex_scanned),
  2470. atomic_read(&sbi->s_bal_goals),
  2471. atomic_read(&sbi->s_bal_2orders),
  2472. atomic_read(&sbi->s_bal_breaks),
  2473. atomic_read(&sbi->s_mb_lost_chunks));
  2474. printk(KERN_INFO
  2475. "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
  2476. sbi->s_mb_buddies_generated++,
  2477. sbi->s_mb_generation_time);
  2478. printk(KERN_INFO
  2479. "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
  2480. atomic_read(&sbi->s_mb_preallocated),
  2481. atomic_read(&sbi->s_mb_discarded));
  2482. }
  2483. free_percpu(sbi->s_locality_groups);
  2484. ext4_mb_history_release(sb);
  2485. ext4_mb_destroy_per_dev_proc(sb);
  2486. return 0;
  2487. }
  2488. /*
  2489. * This function is called by the jbd2 layer once the commit has finished,
  2490. * so we know we can free the blocks that were released with that commit.
  2491. */
  2492. static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
  2493. {
  2494. struct super_block *sb = journal->j_private;
  2495. struct ext4_buddy e4b;
  2496. struct ext4_group_info *db;
  2497. int err, count = 0, count2 = 0;
  2498. struct ext4_free_data *entry;
  2499. ext4_fsblk_t discard_block;
  2500. struct list_head *l, *ltmp;
  2501. list_for_each_safe(l, ltmp, &txn->t_private_list) {
  2502. entry = list_entry(l, struct ext4_free_data, list);
  2503. mb_debug("gonna free %u blocks in group %u (0x%p):",
  2504. entry->count, entry->group, entry);
  2505. err = ext4_mb_load_buddy(sb, entry->group, &e4b);
  2506. /* we expect to find existing buddy because it's pinned */
  2507. BUG_ON(err != 0);
  2508. db = e4b.bd_info;
  2509. /* there are blocks to put in buddy to make them really free */
  2510. count += entry->count;
  2511. count2++;
  2512. ext4_lock_group(sb, entry->group);
  2513. /* Take it out of per group rb tree */
  2514. rb_erase(&entry->node, &(db->bb_free_root));
  2515. mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
  2516. if (!db->bb_free_root.rb_node) {
  2517. /* No more items in the per group rb tree
  2518. * balance refcounts from ext4_mb_free_metadata()
  2519. */
  2520. page_cache_release(e4b.bd_buddy_page);
  2521. page_cache_release(e4b.bd_bitmap_page);
  2522. }
  2523. ext4_unlock_group(sb, entry->group);
  2524. discard_block = (ext4_fsblk_t) entry->group * EXT4_BLOCKS_PER_GROUP(sb)
  2525. + entry->start_blk
  2526. + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  2527. trace_mark(ext4_discard_blocks, "dev %s blk %llu count %u", sb->s_id,
  2528. (unsigned long long) discard_block, entry->count);
  2529. sb_issue_discard(sb, discard_block, entry->count);
  2530. kmem_cache_free(ext4_free_ext_cachep, entry);
  2531. ext4_mb_release_desc(&e4b);
  2532. }
  2533. mb_debug("freed %u blocks in %u structures\n", count, count2);
  2534. }
  2535. #define EXT4_MB_STATS_NAME "stats"
  2536. #define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
  2537. #define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
  2538. #define EXT4_MB_ORDER2_REQ "order2_req"
  2539. #define EXT4_MB_STREAM_REQ "stream_req"
  2540. #define EXT4_MB_GROUP_PREALLOC "group_prealloc"
  2541. static int ext4_mb_init_per_dev_proc(struct super_block *sb)
  2542. {
  2543. #ifdef CONFIG_PROC_FS
  2544. mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
  2545. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2546. struct proc_dir_entry *proc;
  2547. if (sbi->s_proc == NULL)
  2548. return -EINVAL;
  2549. EXT4_PROC_HANDLER(EXT4_MB_STATS_NAME, mb_stats);
  2550. EXT4_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME, mb_max_to_scan);
  2551. EXT4_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME, mb_min_to_scan);
  2552. EXT4_PROC_HANDLER(EXT4_MB_ORDER2_REQ, mb_order2_reqs);
  2553. EXT4_PROC_HANDLER(EXT4_MB_STREAM_REQ, mb_stream_request);
  2554. EXT4_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC, mb_group_prealloc);
  2555. return 0;
  2556. err_out:
  2557. remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
  2558. remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
  2559. remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
  2560. remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
  2561. remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
  2562. remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_proc);
  2563. return -ENOMEM;
  2564. #else
  2565. return 0;
  2566. #endif
  2567. }
  2568. static int ext4_mb_destroy_per_dev_proc(struct super_block *sb)
  2569. {
  2570. #ifdef CONFIG_PROC_FS
  2571. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2572. if (sbi->s_proc == NULL)
  2573. return -EINVAL;
  2574. remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
  2575. remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
  2576. remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
  2577. remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
  2578. remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
  2579. remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_proc);
  2580. #endif
  2581. return 0;
  2582. }
  2583. int __init init_ext4_mballoc(void)
  2584. {
  2585. ext4_pspace_cachep =
  2586. kmem_cache_create("ext4_prealloc_space",
  2587. sizeof(struct ext4_prealloc_space),
  2588. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2589. if (ext4_pspace_cachep == NULL)
  2590. return -ENOMEM;
  2591. ext4_ac_cachep =
  2592. kmem_cache_create("ext4_alloc_context",
  2593. sizeof(struct ext4_allocation_context),
  2594. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2595. if (ext4_ac_cachep == NULL) {
  2596. kmem_cache_destroy(ext4_pspace_cachep);
  2597. return -ENOMEM;
  2598. }
  2599. ext4_free_ext_cachep =
  2600. kmem_cache_create("ext4_free_block_extents",
  2601. sizeof(struct ext4_free_data),
  2602. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2603. if (ext4_free_ext_cachep == NULL) {
  2604. kmem_cache_destroy(ext4_pspace_cachep);
  2605. kmem_cache_destroy(ext4_ac_cachep);
  2606. return -ENOMEM;
  2607. }
  2608. return 0;
  2609. }
  2610. void exit_ext4_mballoc(void)
  2611. {
  2612. /* XXX: synchronize_rcu(); */
  2613. kmem_cache_destroy(ext4_pspace_cachep);
  2614. kmem_cache_destroy(ext4_ac_cachep);
  2615. kmem_cache_destroy(ext4_free_ext_cachep);
  2616. }
  2617. /*
  2618. * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
  2619. * Returns 0 if success or error code
  2620. */
  2621. static noinline_for_stack int
  2622. ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
  2623. handle_t *handle, unsigned int reserv_blks)
  2624. {
  2625. struct buffer_head *bitmap_bh = NULL;
  2626. struct ext4_super_block *es;
  2627. struct ext4_group_desc *gdp;
  2628. struct buffer_head *gdp_bh;
  2629. struct ext4_sb_info *sbi;
  2630. struct super_block *sb;
  2631. ext4_fsblk_t block;
  2632. int err, len;
  2633. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  2634. BUG_ON(ac->ac_b_ex.fe_len <= 0);
  2635. sb = ac->ac_sb;
  2636. sbi = EXT4_SB(sb);
  2637. es = sbi->s_es;
  2638. err = -EIO;
  2639. bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
  2640. if (!bitmap_bh)
  2641. goto out_err;
  2642. err = ext4_journal_get_write_access(handle, bitmap_bh);
  2643. if (err)
  2644. goto out_err;
  2645. err = -EIO;
  2646. gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
  2647. if (!gdp)
  2648. goto out_err;
  2649. ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
  2650. gdp->bg_free_blocks_count);
  2651. err = ext4_journal_get_write_access(handle, gdp_bh);
  2652. if (err)
  2653. goto out_err;
  2654. block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
  2655. + ac->ac_b_ex.fe_start
  2656. + le32_to_cpu(es->s_first_data_block);
  2657. len = ac->ac_b_ex.fe_len;
  2658. if (in_range(ext4_block_bitmap(sb, gdp), block, len) ||
  2659. in_range(ext4_inode_bitmap(sb, gdp), block, len) ||
  2660. in_range(block, ext4_inode_table(sb, gdp),
  2661. EXT4_SB(sb)->s_itb_per_group) ||
  2662. in_range(block + len - 1, ext4_inode_table(sb, gdp),
  2663. EXT4_SB(sb)->s_itb_per_group)) {
  2664. ext4_error(sb, __func__,
  2665. "Allocating block in system zone - block = %llu",
  2666. block);
  2667. /* File system mounted not to panic on error
  2668. * Fix the bitmap and repeat the block allocation
  2669. * We leak some of the blocks here.
  2670. */
  2671. mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group),
  2672. bitmap_bh->b_data, ac->ac_b_ex.fe_start,
  2673. ac->ac_b_ex.fe_len);
  2674. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2675. if (!err)
  2676. err = -EAGAIN;
  2677. goto out_err;
  2678. }
  2679. #ifdef AGGRESSIVE_CHECK
  2680. {
  2681. int i;
  2682. for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
  2683. BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
  2684. bitmap_bh->b_data));
  2685. }
  2686. }
  2687. #endif
  2688. mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group), bitmap_bh->b_data,
  2689. ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
  2690. spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
  2691. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2692. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  2693. gdp->bg_free_blocks_count =
  2694. cpu_to_le16(ext4_free_blocks_after_init(sb,
  2695. ac->ac_b_ex.fe_group,
  2696. gdp));
  2697. }
  2698. le16_add_cpu(&gdp->bg_free_blocks_count, -ac->ac_b_ex.fe_len);
  2699. gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
  2700. spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
  2701. percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
  2702. /*
  2703. * Now reduce the dirty block count also. Should not go negative
  2704. */
  2705. if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
  2706. /* release all the reserved blocks if non delalloc */
  2707. percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
  2708. else
  2709. percpu_counter_sub(&sbi->s_dirtyblocks_counter,
  2710. ac->ac_b_ex.fe_len);
  2711. if (sbi->s_log_groups_per_flex) {
  2712. ext4_group_t flex_group = ext4_flex_group(sbi,
  2713. ac->ac_b_ex.fe_group);
  2714. spin_lock(sb_bgl_lock(sbi, flex_group));
  2715. sbi->s_flex_groups[flex_group].free_blocks -= ac->ac_b_ex.fe_len;
  2716. spin_unlock(sb_bgl_lock(sbi, flex_group));
  2717. }
  2718. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2719. if (err)
  2720. goto out_err;
  2721. err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
  2722. out_err:
  2723. sb->s_dirt = 1;
  2724. brelse(bitmap_bh);
  2725. return err;
  2726. }
  2727. /*
  2728. * here we normalize request for locality group
  2729. * Group request are normalized to s_strip size if we set the same via mount
  2730. * option. If not we set it to s_mb_group_prealloc which can be configured via
  2731. * /proc/fs/ext4/<partition>/group_prealloc
  2732. *
  2733. * XXX: should we try to preallocate more than the group has now?
  2734. */
  2735. static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
  2736. {
  2737. struct super_block *sb = ac->ac_sb;
  2738. struct ext4_locality_group *lg = ac->ac_lg;
  2739. BUG_ON(lg == NULL);
  2740. if (EXT4_SB(sb)->s_stripe)
  2741. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
  2742. else
  2743. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
  2744. mb_debug("#%u: goal %u blocks for locality group\n",
  2745. current->pid, ac->ac_g_ex.fe_len);
  2746. }
  2747. /*
  2748. * Normalization means making request better in terms of
  2749. * size and alignment
  2750. */
  2751. static noinline_for_stack void
  2752. ext4_mb_normalize_request(struct ext4_allocation_context *ac,
  2753. struct ext4_allocation_request *ar)
  2754. {
  2755. int bsbits, max;
  2756. ext4_lblk_t end;
  2757. loff_t size, orig_size, start_off;
  2758. ext4_lblk_t start, orig_start;
  2759. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2760. struct ext4_prealloc_space *pa;
  2761. /* do normalize only data requests, metadata requests
  2762. do not need preallocation */
  2763. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2764. return;
  2765. /* sometime caller may want exact blocks */
  2766. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  2767. return;
  2768. /* caller may indicate that preallocation isn't
  2769. * required (it's a tail, for example) */
  2770. if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
  2771. return;
  2772. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
  2773. ext4_mb_normalize_group_request(ac);
  2774. return ;
  2775. }
  2776. bsbits = ac->ac_sb->s_blocksize_bits;
  2777. /* first, let's learn actual file size
  2778. * given current request is allocated */
  2779. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  2780. size = size << bsbits;
  2781. if (size < i_size_read(ac->ac_inode))
  2782. size = i_size_read(ac->ac_inode);
  2783. /* max size of free chunks */
  2784. max = 2 << bsbits;
  2785. #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
  2786. (req <= (size) || max <= (chunk_size))
  2787. /* first, try to predict filesize */
  2788. /* XXX: should this table be tunable? */
  2789. start_off = 0;
  2790. if (size <= 16 * 1024) {
  2791. size = 16 * 1024;
  2792. } else if (size <= 32 * 1024) {
  2793. size = 32 * 1024;
  2794. } else if (size <= 64 * 1024) {
  2795. size = 64 * 1024;
  2796. } else if (size <= 128 * 1024) {
  2797. size = 128 * 1024;
  2798. } else if (size <= 256 * 1024) {
  2799. size = 256 * 1024;
  2800. } else if (size <= 512 * 1024) {
  2801. size = 512 * 1024;
  2802. } else if (size <= 1024 * 1024) {
  2803. size = 1024 * 1024;
  2804. } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
  2805. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2806. (21 - bsbits)) << 21;
  2807. size = 2 * 1024 * 1024;
  2808. } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
  2809. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2810. (22 - bsbits)) << 22;
  2811. size = 4 * 1024 * 1024;
  2812. } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
  2813. (8<<20)>>bsbits, max, 8 * 1024)) {
  2814. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2815. (23 - bsbits)) << 23;
  2816. size = 8 * 1024 * 1024;
  2817. } else {
  2818. start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
  2819. size = ac->ac_o_ex.fe_len << bsbits;
  2820. }
  2821. orig_size = size = size >> bsbits;
  2822. orig_start = start = start_off >> bsbits;
  2823. /* don't cover already allocated blocks in selected range */
  2824. if (ar->pleft && start <= ar->lleft) {
  2825. size -= ar->lleft + 1 - start;
  2826. start = ar->lleft + 1;
  2827. }
  2828. if (ar->pright && start + size - 1 >= ar->lright)
  2829. size -= start + size - ar->lright;
  2830. end = start + size;
  2831. /* check we don't cross already preallocated blocks */
  2832. rcu_read_lock();
  2833. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2834. ext4_lblk_t pa_end;
  2835. if (pa->pa_deleted)
  2836. continue;
  2837. spin_lock(&pa->pa_lock);
  2838. if (pa->pa_deleted) {
  2839. spin_unlock(&pa->pa_lock);
  2840. continue;
  2841. }
  2842. pa_end = pa->pa_lstart + pa->pa_len;
  2843. /* PA must not overlap original request */
  2844. BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
  2845. ac->ac_o_ex.fe_logical < pa->pa_lstart));
  2846. /* skip PA normalized request doesn't overlap with */
  2847. if (pa->pa_lstart >= end) {
  2848. spin_unlock(&pa->pa_lock);
  2849. continue;
  2850. }
  2851. if (pa_end <= start) {
  2852. spin_unlock(&pa->pa_lock);
  2853. continue;
  2854. }
  2855. BUG_ON(pa->pa_lstart <= start && pa_end >= end);
  2856. if (pa_end <= ac->ac_o_ex.fe_logical) {
  2857. BUG_ON(pa_end < start);
  2858. start = pa_end;
  2859. }
  2860. if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
  2861. BUG_ON(pa->pa_lstart > end);
  2862. end = pa->pa_lstart;
  2863. }
  2864. spin_unlock(&pa->pa_lock);
  2865. }
  2866. rcu_read_unlock();
  2867. size = end - start;
  2868. /* XXX: extra loop to check we really don't overlap preallocations */
  2869. rcu_read_lock();
  2870. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2871. ext4_lblk_t pa_end;
  2872. spin_lock(&pa->pa_lock);
  2873. if (pa->pa_deleted == 0) {
  2874. pa_end = pa->pa_lstart + pa->pa_len;
  2875. BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
  2876. }
  2877. spin_unlock(&pa->pa_lock);
  2878. }
  2879. rcu_read_unlock();
  2880. if (start + size <= ac->ac_o_ex.fe_logical &&
  2881. start > ac->ac_o_ex.fe_logical) {
  2882. printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
  2883. (unsigned long) start, (unsigned long) size,
  2884. (unsigned long) ac->ac_o_ex.fe_logical);
  2885. }
  2886. BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
  2887. start > ac->ac_o_ex.fe_logical);
  2888. BUG_ON(size <= 0 || size >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  2889. /* now prepare goal request */
  2890. /* XXX: is it better to align blocks WRT to logical
  2891. * placement or satisfy big request as is */
  2892. ac->ac_g_ex.fe_logical = start;
  2893. ac->ac_g_ex.fe_len = size;
  2894. /* define goal start in order to merge */
  2895. if (ar->pright && (ar->lright == (start + size))) {
  2896. /* merge to the right */
  2897. ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
  2898. &ac->ac_f_ex.fe_group,
  2899. &ac->ac_f_ex.fe_start);
  2900. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2901. }
  2902. if (ar->pleft && (ar->lleft + 1 == start)) {
  2903. /* merge to the left */
  2904. ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
  2905. &ac->ac_f_ex.fe_group,
  2906. &ac->ac_f_ex.fe_start);
  2907. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2908. }
  2909. mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
  2910. (unsigned) orig_size, (unsigned) start);
  2911. }
  2912. static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
  2913. {
  2914. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2915. if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
  2916. atomic_inc(&sbi->s_bal_reqs);
  2917. atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
  2918. if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
  2919. atomic_inc(&sbi->s_bal_success);
  2920. atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
  2921. if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
  2922. ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
  2923. atomic_inc(&sbi->s_bal_goals);
  2924. if (ac->ac_found > sbi->s_mb_max_to_scan)
  2925. atomic_inc(&sbi->s_bal_breaks);
  2926. }
  2927. ext4_mb_store_history(ac);
  2928. }
  2929. /*
  2930. * use blocks preallocated to inode
  2931. */
  2932. static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
  2933. struct ext4_prealloc_space *pa)
  2934. {
  2935. ext4_fsblk_t start;
  2936. ext4_fsblk_t end;
  2937. int len;
  2938. /* found preallocated blocks, use them */
  2939. start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
  2940. end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
  2941. len = end - start;
  2942. ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
  2943. &ac->ac_b_ex.fe_start);
  2944. ac->ac_b_ex.fe_len = len;
  2945. ac->ac_status = AC_STATUS_FOUND;
  2946. ac->ac_pa = pa;
  2947. BUG_ON(start < pa->pa_pstart);
  2948. BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
  2949. BUG_ON(pa->pa_free < len);
  2950. pa->pa_free -= len;
  2951. mb_debug("use %llu/%u from inode pa %p\n", start, len, pa);
  2952. }
  2953. /*
  2954. * use blocks preallocated to locality group
  2955. */
  2956. static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
  2957. struct ext4_prealloc_space *pa)
  2958. {
  2959. unsigned int len = ac->ac_o_ex.fe_len;
  2960. ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
  2961. &ac->ac_b_ex.fe_group,
  2962. &ac->ac_b_ex.fe_start);
  2963. ac->ac_b_ex.fe_len = len;
  2964. ac->ac_status = AC_STATUS_FOUND;
  2965. ac->ac_pa = pa;
  2966. /* we don't correct pa_pstart or pa_plen here to avoid
  2967. * possible race when the group is being loaded concurrently
  2968. * instead we correct pa later, after blocks are marked
  2969. * in on-disk bitmap -- see ext4_mb_release_context()
  2970. * Other CPUs are prevented from allocating from this pa by lg_mutex
  2971. */
  2972. mb_debug("use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
  2973. }
  2974. /*
  2975. * Return the prealloc space that have minimal distance
  2976. * from the goal block. @cpa is the prealloc
  2977. * space that is having currently known minimal distance
  2978. * from the goal block.
  2979. */
  2980. static struct ext4_prealloc_space *
  2981. ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
  2982. struct ext4_prealloc_space *pa,
  2983. struct ext4_prealloc_space *cpa)
  2984. {
  2985. ext4_fsblk_t cur_distance, new_distance;
  2986. if (cpa == NULL) {
  2987. atomic_inc(&pa->pa_count);
  2988. return pa;
  2989. }
  2990. cur_distance = abs(goal_block - cpa->pa_pstart);
  2991. new_distance = abs(goal_block - pa->pa_pstart);
  2992. if (cur_distance < new_distance)
  2993. return cpa;
  2994. /* drop the previous reference */
  2995. atomic_dec(&cpa->pa_count);
  2996. atomic_inc(&pa->pa_count);
  2997. return pa;
  2998. }
  2999. /*
  3000. * search goal blocks in preallocated space
  3001. */
  3002. static noinline_for_stack int
  3003. ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
  3004. {
  3005. int order, i;
  3006. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  3007. struct ext4_locality_group *lg;
  3008. struct ext4_prealloc_space *pa, *cpa = NULL;
  3009. ext4_fsblk_t goal_block;
  3010. /* only data can be preallocated */
  3011. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  3012. return 0;
  3013. /* first, try per-file preallocation */
  3014. rcu_read_lock();
  3015. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  3016. /* all fields in this condition don't change,
  3017. * so we can skip locking for them */
  3018. if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
  3019. ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
  3020. continue;
  3021. /* found preallocated blocks, use them */
  3022. spin_lock(&pa->pa_lock);
  3023. if (pa->pa_deleted == 0 && pa->pa_free) {
  3024. atomic_inc(&pa->pa_count);
  3025. ext4_mb_use_inode_pa(ac, pa);
  3026. spin_unlock(&pa->pa_lock);
  3027. ac->ac_criteria = 10;
  3028. rcu_read_unlock();
  3029. return 1;
  3030. }
  3031. spin_unlock(&pa->pa_lock);
  3032. }
  3033. rcu_read_unlock();
  3034. /* can we use group allocation? */
  3035. if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
  3036. return 0;
  3037. /* inode may have no locality group for some reason */
  3038. lg = ac->ac_lg;
  3039. if (lg == NULL)
  3040. return 0;
  3041. order = fls(ac->ac_o_ex.fe_len) - 1;
  3042. if (order > PREALLOC_TB_SIZE - 1)
  3043. /* The max size of hash table is PREALLOC_TB_SIZE */
  3044. order = PREALLOC_TB_SIZE - 1;
  3045. goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) +
  3046. ac->ac_g_ex.fe_start +
  3047. le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block);
  3048. /*
  3049. * search for the prealloc space that is having
  3050. * minimal distance from the goal block.
  3051. */
  3052. for (i = order; i < PREALLOC_TB_SIZE; i++) {
  3053. rcu_read_lock();
  3054. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
  3055. pa_inode_list) {
  3056. spin_lock(&pa->pa_lock);
  3057. if (pa->pa_deleted == 0 &&
  3058. pa->pa_free >= ac->ac_o_ex.fe_len) {
  3059. cpa = ext4_mb_check_group_pa(goal_block,
  3060. pa, cpa);
  3061. }
  3062. spin_unlock(&pa->pa_lock);
  3063. }
  3064. rcu_read_unlock();
  3065. }
  3066. if (cpa) {
  3067. ext4_mb_use_group_pa(ac, cpa);
  3068. ac->ac_criteria = 20;
  3069. return 1;
  3070. }
  3071. return 0;
  3072. }
  3073. /*
  3074. * the function goes through all block freed in the group
  3075. * but not yet committed and marks them used in in-core bitmap.
  3076. * buddy must be generated from this bitmap
  3077. * Need to be called with ext4 group lock (ext4_lock_group)
  3078. */
  3079. static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
  3080. ext4_group_t group)
  3081. {
  3082. struct rb_node *n;
  3083. struct ext4_group_info *grp;
  3084. struct ext4_free_data *entry;
  3085. grp = ext4_get_group_info(sb, group);
  3086. n = rb_first(&(grp->bb_free_root));
  3087. while (n) {
  3088. entry = rb_entry(n, struct ext4_free_data, node);
  3089. mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
  3090. bitmap, entry->start_blk,
  3091. entry->count);
  3092. n = rb_next(n);
  3093. }
  3094. return;
  3095. }
  3096. /*
  3097. * the function goes through all preallocation in this group and marks them
  3098. * used in in-core bitmap. buddy must be generated from this bitmap
  3099. * Need to be called with ext4 group lock (ext4_lock_group)
  3100. */
  3101. static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  3102. ext4_group_t group)
  3103. {
  3104. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3105. struct ext4_prealloc_space *pa;
  3106. struct list_head *cur;
  3107. ext4_group_t groupnr;
  3108. ext4_grpblk_t start;
  3109. int preallocated = 0;
  3110. int count = 0;
  3111. int len;
  3112. /* all form of preallocation discards first load group,
  3113. * so the only competing code is preallocation use.
  3114. * we don't need any locking here
  3115. * notice we do NOT ignore preallocations with pa_deleted
  3116. * otherwise we could leave used blocks available for
  3117. * allocation in buddy when concurrent ext4_mb_put_pa()
  3118. * is dropping preallocation
  3119. */
  3120. list_for_each(cur, &grp->bb_prealloc_list) {
  3121. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  3122. spin_lock(&pa->pa_lock);
  3123. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3124. &groupnr, &start);
  3125. len = pa->pa_len;
  3126. spin_unlock(&pa->pa_lock);
  3127. if (unlikely(len == 0))
  3128. continue;
  3129. BUG_ON(groupnr != group);
  3130. mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
  3131. bitmap, start, len);
  3132. preallocated += len;
  3133. count++;
  3134. }
  3135. mb_debug("prellocated %u for group %u\n", preallocated, group);
  3136. }
  3137. static void ext4_mb_pa_callback(struct rcu_head *head)
  3138. {
  3139. struct ext4_prealloc_space *pa;
  3140. pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
  3141. kmem_cache_free(ext4_pspace_cachep, pa);
  3142. }
  3143. /*
  3144. * drops a reference to preallocated space descriptor
  3145. * if this was the last reference and the space is consumed
  3146. */
  3147. static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
  3148. struct super_block *sb, struct ext4_prealloc_space *pa)
  3149. {
  3150. ext4_group_t grp;
  3151. if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
  3152. return;
  3153. /* in this short window concurrent discard can set pa_deleted */
  3154. spin_lock(&pa->pa_lock);
  3155. if (pa->pa_deleted == 1) {
  3156. spin_unlock(&pa->pa_lock);
  3157. return;
  3158. }
  3159. pa->pa_deleted = 1;
  3160. spin_unlock(&pa->pa_lock);
  3161. /* -1 is to protect from crossing allocation group */
  3162. ext4_get_group_no_and_offset(sb, pa->pa_pstart - 1, &grp, NULL);
  3163. /*
  3164. * possible race:
  3165. *
  3166. * P1 (buddy init) P2 (regular allocation)
  3167. * find block B in PA
  3168. * copy on-disk bitmap to buddy
  3169. * mark B in on-disk bitmap
  3170. * drop PA from group
  3171. * mark all PAs in buddy
  3172. *
  3173. * thus, P1 initializes buddy with B available. to prevent this
  3174. * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
  3175. * against that pair
  3176. */
  3177. ext4_lock_group(sb, grp);
  3178. list_del(&pa->pa_group_list);
  3179. ext4_unlock_group(sb, grp);
  3180. spin_lock(pa->pa_obj_lock);
  3181. list_del_rcu(&pa->pa_inode_list);
  3182. spin_unlock(pa->pa_obj_lock);
  3183. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3184. }
  3185. /*
  3186. * creates new preallocated space for given inode
  3187. */
  3188. static noinline_for_stack int
  3189. ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
  3190. {
  3191. struct super_block *sb = ac->ac_sb;
  3192. struct ext4_prealloc_space *pa;
  3193. struct ext4_group_info *grp;
  3194. struct ext4_inode_info *ei;
  3195. /* preallocate only when found space is larger then requested */
  3196. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3197. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3198. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3199. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3200. if (pa == NULL)
  3201. return -ENOMEM;
  3202. if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
  3203. int winl;
  3204. int wins;
  3205. int win;
  3206. int offs;
  3207. /* we can't allocate as much as normalizer wants.
  3208. * so, found space must get proper lstart
  3209. * to cover original request */
  3210. BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
  3211. BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
  3212. /* we're limited by original request in that
  3213. * logical block must be covered any way
  3214. * winl is window we can move our chunk within */
  3215. winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
  3216. /* also, we should cover whole original request */
  3217. wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
  3218. /* the smallest one defines real window */
  3219. win = min(winl, wins);
  3220. offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
  3221. if (offs && offs < win)
  3222. win = offs;
  3223. ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
  3224. BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
  3225. BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
  3226. }
  3227. /* preallocation can change ac_b_ex, thus we store actually
  3228. * allocated blocks for history */
  3229. ac->ac_f_ex = ac->ac_b_ex;
  3230. pa->pa_lstart = ac->ac_b_ex.fe_logical;
  3231. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3232. pa->pa_len = ac->ac_b_ex.fe_len;
  3233. pa->pa_free = pa->pa_len;
  3234. atomic_set(&pa->pa_count, 1);
  3235. spin_lock_init(&pa->pa_lock);
  3236. pa->pa_deleted = 0;
  3237. pa->pa_linear = 0;
  3238. mb_debug("new inode pa %p: %llu/%u for %u\n", pa,
  3239. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3240. ext4_mb_use_inode_pa(ac, pa);
  3241. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3242. ei = EXT4_I(ac->ac_inode);
  3243. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3244. pa->pa_obj_lock = &ei->i_prealloc_lock;
  3245. pa->pa_inode = ac->ac_inode;
  3246. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3247. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3248. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3249. spin_lock(pa->pa_obj_lock);
  3250. list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
  3251. spin_unlock(pa->pa_obj_lock);
  3252. return 0;
  3253. }
  3254. /*
  3255. * creates new preallocated space for locality group inodes belongs to
  3256. */
  3257. static noinline_for_stack int
  3258. ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
  3259. {
  3260. struct super_block *sb = ac->ac_sb;
  3261. struct ext4_locality_group *lg;
  3262. struct ext4_prealloc_space *pa;
  3263. struct ext4_group_info *grp;
  3264. /* preallocate only when found space is larger then requested */
  3265. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3266. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3267. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3268. BUG_ON(ext4_pspace_cachep == NULL);
  3269. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3270. if (pa == NULL)
  3271. return -ENOMEM;
  3272. /* preallocation can change ac_b_ex, thus we store actually
  3273. * allocated blocks for history */
  3274. ac->ac_f_ex = ac->ac_b_ex;
  3275. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3276. pa->pa_lstart = pa->pa_pstart;
  3277. pa->pa_len = ac->ac_b_ex.fe_len;
  3278. pa->pa_free = pa->pa_len;
  3279. atomic_set(&pa->pa_count, 1);
  3280. spin_lock_init(&pa->pa_lock);
  3281. INIT_LIST_HEAD(&pa->pa_inode_list);
  3282. pa->pa_deleted = 0;
  3283. pa->pa_linear = 1;
  3284. mb_debug("new group pa %p: %llu/%u for %u\n", pa,
  3285. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3286. ext4_mb_use_group_pa(ac, pa);
  3287. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3288. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3289. lg = ac->ac_lg;
  3290. BUG_ON(lg == NULL);
  3291. pa->pa_obj_lock = &lg->lg_prealloc_lock;
  3292. pa->pa_inode = NULL;
  3293. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3294. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3295. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3296. /*
  3297. * We will later add the new pa to the right bucket
  3298. * after updating the pa_free in ext4_mb_release_context
  3299. */
  3300. return 0;
  3301. }
  3302. static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
  3303. {
  3304. int err;
  3305. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3306. err = ext4_mb_new_group_pa(ac);
  3307. else
  3308. err = ext4_mb_new_inode_pa(ac);
  3309. return err;
  3310. }
  3311. /*
  3312. * finds all unused blocks in on-disk bitmap, frees them in
  3313. * in-core bitmap and buddy.
  3314. * @pa must be unlinked from inode and group lists, so that
  3315. * nobody else can find/use it.
  3316. * the caller MUST hold group/inode locks.
  3317. * TODO: optimize the case when there are no in-core structures yet
  3318. */
  3319. static noinline_for_stack int
  3320. ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
  3321. struct ext4_prealloc_space *pa,
  3322. struct ext4_allocation_context *ac)
  3323. {
  3324. struct super_block *sb = e4b->bd_sb;
  3325. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3326. unsigned int end;
  3327. unsigned int next;
  3328. ext4_group_t group;
  3329. ext4_grpblk_t bit;
  3330. sector_t start;
  3331. int err = 0;
  3332. int free = 0;
  3333. BUG_ON(pa->pa_deleted == 0);
  3334. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3335. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3336. end = bit + pa->pa_len;
  3337. if (ac) {
  3338. ac->ac_sb = sb;
  3339. ac->ac_inode = pa->pa_inode;
  3340. ac->ac_op = EXT4_MB_HISTORY_DISCARD;
  3341. }
  3342. while (bit < end) {
  3343. bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
  3344. if (bit >= end)
  3345. break;
  3346. next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
  3347. start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
  3348. le32_to_cpu(sbi->s_es->s_first_data_block);
  3349. mb_debug(" free preallocated %u/%u in group %u\n",
  3350. (unsigned) start, (unsigned) next - bit,
  3351. (unsigned) group);
  3352. free += next - bit;
  3353. if (ac) {
  3354. ac->ac_b_ex.fe_group = group;
  3355. ac->ac_b_ex.fe_start = bit;
  3356. ac->ac_b_ex.fe_len = next - bit;
  3357. ac->ac_b_ex.fe_logical = 0;
  3358. ext4_mb_store_history(ac);
  3359. }
  3360. mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
  3361. bit = next + 1;
  3362. }
  3363. if (free != pa->pa_free) {
  3364. printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
  3365. pa, (unsigned long) pa->pa_lstart,
  3366. (unsigned long) pa->pa_pstart,
  3367. (unsigned long) pa->pa_len);
  3368. ext4_grp_locked_error(sb, group,
  3369. __func__, "free %u, pa_free %u",
  3370. free, pa->pa_free);
  3371. /*
  3372. * pa is already deleted so we use the value obtained
  3373. * from the bitmap and continue.
  3374. */
  3375. }
  3376. atomic_add(free, &sbi->s_mb_discarded);
  3377. return err;
  3378. }
  3379. static noinline_for_stack int
  3380. ext4_mb_release_group_pa(struct ext4_buddy *e4b,
  3381. struct ext4_prealloc_space *pa,
  3382. struct ext4_allocation_context *ac)
  3383. {
  3384. struct super_block *sb = e4b->bd_sb;
  3385. ext4_group_t group;
  3386. ext4_grpblk_t bit;
  3387. if (ac)
  3388. ac->ac_op = EXT4_MB_HISTORY_DISCARD;
  3389. BUG_ON(pa->pa_deleted == 0);
  3390. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3391. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3392. mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
  3393. atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
  3394. if (ac) {
  3395. ac->ac_sb = sb;
  3396. ac->ac_inode = NULL;
  3397. ac->ac_b_ex.fe_group = group;
  3398. ac->ac_b_ex.fe_start = bit;
  3399. ac->ac_b_ex.fe_len = pa->pa_len;
  3400. ac->ac_b_ex.fe_logical = 0;
  3401. ext4_mb_store_history(ac);
  3402. }
  3403. return 0;
  3404. }
  3405. /*
  3406. * releases all preallocations in given group
  3407. *
  3408. * first, we need to decide discard policy:
  3409. * - when do we discard
  3410. * 1) ENOSPC
  3411. * - how many do we discard
  3412. * 1) how many requested
  3413. */
  3414. static noinline_for_stack int
  3415. ext4_mb_discard_group_preallocations(struct super_block *sb,
  3416. ext4_group_t group, int needed)
  3417. {
  3418. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3419. struct buffer_head *bitmap_bh = NULL;
  3420. struct ext4_prealloc_space *pa, *tmp;
  3421. struct ext4_allocation_context *ac;
  3422. struct list_head list;
  3423. struct ext4_buddy e4b;
  3424. int err;
  3425. int busy = 0;
  3426. int free = 0;
  3427. mb_debug("discard preallocation for group %u\n", group);
  3428. if (list_empty(&grp->bb_prealloc_list))
  3429. return 0;
  3430. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3431. if (bitmap_bh == NULL) {
  3432. ext4_error(sb, __func__, "Error in reading block "
  3433. "bitmap for %u", group);
  3434. return 0;
  3435. }
  3436. err = ext4_mb_load_buddy(sb, group, &e4b);
  3437. if (err) {
  3438. ext4_error(sb, __func__, "Error in loading buddy "
  3439. "information for %u", group);
  3440. put_bh(bitmap_bh);
  3441. return 0;
  3442. }
  3443. if (needed == 0)
  3444. needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
  3445. INIT_LIST_HEAD(&list);
  3446. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3447. repeat:
  3448. ext4_lock_group(sb, group);
  3449. list_for_each_entry_safe(pa, tmp,
  3450. &grp->bb_prealloc_list, pa_group_list) {
  3451. spin_lock(&pa->pa_lock);
  3452. if (atomic_read(&pa->pa_count)) {
  3453. spin_unlock(&pa->pa_lock);
  3454. busy = 1;
  3455. continue;
  3456. }
  3457. if (pa->pa_deleted) {
  3458. spin_unlock(&pa->pa_lock);
  3459. continue;
  3460. }
  3461. /* seems this one can be freed ... */
  3462. pa->pa_deleted = 1;
  3463. /* we can trust pa_free ... */
  3464. free += pa->pa_free;
  3465. spin_unlock(&pa->pa_lock);
  3466. list_del(&pa->pa_group_list);
  3467. list_add(&pa->u.pa_tmp_list, &list);
  3468. }
  3469. /* if we still need more blocks and some PAs were used, try again */
  3470. if (free < needed && busy) {
  3471. busy = 0;
  3472. ext4_unlock_group(sb, group);
  3473. /*
  3474. * Yield the CPU here so that we don't get soft lockup
  3475. * in non preempt case.
  3476. */
  3477. yield();
  3478. goto repeat;
  3479. }
  3480. /* found anything to free? */
  3481. if (list_empty(&list)) {
  3482. BUG_ON(free != 0);
  3483. goto out;
  3484. }
  3485. /* now free all selected PAs */
  3486. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3487. /* remove from object (inode or locality group) */
  3488. spin_lock(pa->pa_obj_lock);
  3489. list_del_rcu(&pa->pa_inode_list);
  3490. spin_unlock(pa->pa_obj_lock);
  3491. if (pa->pa_linear)
  3492. ext4_mb_release_group_pa(&e4b, pa, ac);
  3493. else
  3494. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3495. list_del(&pa->u.pa_tmp_list);
  3496. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3497. }
  3498. out:
  3499. ext4_unlock_group(sb, group);
  3500. if (ac)
  3501. kmem_cache_free(ext4_ac_cachep, ac);
  3502. ext4_mb_release_desc(&e4b);
  3503. put_bh(bitmap_bh);
  3504. return free;
  3505. }
  3506. /*
  3507. * releases all non-used preallocated blocks for given inode
  3508. *
  3509. * It's important to discard preallocations under i_data_sem
  3510. * We don't want another block to be served from the prealloc
  3511. * space when we are discarding the inode prealloc space.
  3512. *
  3513. * FIXME!! Make sure it is valid at all the call sites
  3514. */
  3515. void ext4_discard_preallocations(struct inode *inode)
  3516. {
  3517. struct ext4_inode_info *ei = EXT4_I(inode);
  3518. struct super_block *sb = inode->i_sb;
  3519. struct buffer_head *bitmap_bh = NULL;
  3520. struct ext4_prealloc_space *pa, *tmp;
  3521. struct ext4_allocation_context *ac;
  3522. ext4_group_t group = 0;
  3523. struct list_head list;
  3524. struct ext4_buddy e4b;
  3525. int err;
  3526. if (!S_ISREG(inode->i_mode)) {
  3527. /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
  3528. return;
  3529. }
  3530. mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
  3531. INIT_LIST_HEAD(&list);
  3532. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3533. repeat:
  3534. /* first, collect all pa's in the inode */
  3535. spin_lock(&ei->i_prealloc_lock);
  3536. while (!list_empty(&ei->i_prealloc_list)) {
  3537. pa = list_entry(ei->i_prealloc_list.next,
  3538. struct ext4_prealloc_space, pa_inode_list);
  3539. BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
  3540. spin_lock(&pa->pa_lock);
  3541. if (atomic_read(&pa->pa_count)) {
  3542. /* this shouldn't happen often - nobody should
  3543. * use preallocation while we're discarding it */
  3544. spin_unlock(&pa->pa_lock);
  3545. spin_unlock(&ei->i_prealloc_lock);
  3546. printk(KERN_ERR "uh-oh! used pa while discarding\n");
  3547. WARN_ON(1);
  3548. schedule_timeout_uninterruptible(HZ);
  3549. goto repeat;
  3550. }
  3551. if (pa->pa_deleted == 0) {
  3552. pa->pa_deleted = 1;
  3553. spin_unlock(&pa->pa_lock);
  3554. list_del_rcu(&pa->pa_inode_list);
  3555. list_add(&pa->u.pa_tmp_list, &list);
  3556. continue;
  3557. }
  3558. /* someone is deleting pa right now */
  3559. spin_unlock(&pa->pa_lock);
  3560. spin_unlock(&ei->i_prealloc_lock);
  3561. /* we have to wait here because pa_deleted
  3562. * doesn't mean pa is already unlinked from
  3563. * the list. as we might be called from
  3564. * ->clear_inode() the inode will get freed
  3565. * and concurrent thread which is unlinking
  3566. * pa from inode's list may access already
  3567. * freed memory, bad-bad-bad */
  3568. /* XXX: if this happens too often, we can
  3569. * add a flag to force wait only in case
  3570. * of ->clear_inode(), but not in case of
  3571. * regular truncate */
  3572. schedule_timeout_uninterruptible(HZ);
  3573. goto repeat;
  3574. }
  3575. spin_unlock(&ei->i_prealloc_lock);
  3576. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3577. BUG_ON(pa->pa_linear != 0);
  3578. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3579. err = ext4_mb_load_buddy(sb, group, &e4b);
  3580. if (err) {
  3581. ext4_error(sb, __func__, "Error in loading buddy "
  3582. "information for %u", group);
  3583. continue;
  3584. }
  3585. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3586. if (bitmap_bh == NULL) {
  3587. ext4_error(sb, __func__, "Error in reading block "
  3588. "bitmap for %u", group);
  3589. ext4_mb_release_desc(&e4b);
  3590. continue;
  3591. }
  3592. ext4_lock_group(sb, group);
  3593. list_del(&pa->pa_group_list);
  3594. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3595. ext4_unlock_group(sb, group);
  3596. ext4_mb_release_desc(&e4b);
  3597. put_bh(bitmap_bh);
  3598. list_del(&pa->u.pa_tmp_list);
  3599. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3600. }
  3601. if (ac)
  3602. kmem_cache_free(ext4_ac_cachep, ac);
  3603. }
  3604. /*
  3605. * finds all preallocated spaces and return blocks being freed to them
  3606. * if preallocated space becomes full (no block is used from the space)
  3607. * then the function frees space in buddy
  3608. * XXX: at the moment, truncate (which is the only way to free blocks)
  3609. * discards all preallocations
  3610. */
  3611. static void ext4_mb_return_to_preallocation(struct inode *inode,
  3612. struct ext4_buddy *e4b,
  3613. sector_t block, int count)
  3614. {
  3615. BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
  3616. }
  3617. #ifdef MB_DEBUG
  3618. static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3619. {
  3620. struct super_block *sb = ac->ac_sb;
  3621. ext4_group_t i;
  3622. printk(KERN_ERR "EXT4-fs: Can't allocate:"
  3623. " Allocation context details:\n");
  3624. printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
  3625. ac->ac_status, ac->ac_flags);
  3626. printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
  3627. "best %lu/%lu/%lu@%lu cr %d\n",
  3628. (unsigned long)ac->ac_o_ex.fe_group,
  3629. (unsigned long)ac->ac_o_ex.fe_start,
  3630. (unsigned long)ac->ac_o_ex.fe_len,
  3631. (unsigned long)ac->ac_o_ex.fe_logical,
  3632. (unsigned long)ac->ac_g_ex.fe_group,
  3633. (unsigned long)ac->ac_g_ex.fe_start,
  3634. (unsigned long)ac->ac_g_ex.fe_len,
  3635. (unsigned long)ac->ac_g_ex.fe_logical,
  3636. (unsigned long)ac->ac_b_ex.fe_group,
  3637. (unsigned long)ac->ac_b_ex.fe_start,
  3638. (unsigned long)ac->ac_b_ex.fe_len,
  3639. (unsigned long)ac->ac_b_ex.fe_logical,
  3640. (int)ac->ac_criteria);
  3641. printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
  3642. ac->ac_found);
  3643. printk(KERN_ERR "EXT4-fs: groups: \n");
  3644. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  3645. struct ext4_group_info *grp = ext4_get_group_info(sb, i);
  3646. struct ext4_prealloc_space *pa;
  3647. ext4_grpblk_t start;
  3648. struct list_head *cur;
  3649. ext4_lock_group(sb, i);
  3650. list_for_each(cur, &grp->bb_prealloc_list) {
  3651. pa = list_entry(cur, struct ext4_prealloc_space,
  3652. pa_group_list);
  3653. spin_lock(&pa->pa_lock);
  3654. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3655. NULL, &start);
  3656. spin_unlock(&pa->pa_lock);
  3657. printk(KERN_ERR "PA:%lu:%d:%u \n", i,
  3658. start, pa->pa_len);
  3659. }
  3660. ext4_unlock_group(sb, i);
  3661. if (grp->bb_free == 0)
  3662. continue;
  3663. printk(KERN_ERR "%lu: %d/%d \n",
  3664. i, grp->bb_free, grp->bb_fragments);
  3665. }
  3666. printk(KERN_ERR "\n");
  3667. }
  3668. #else
  3669. static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3670. {
  3671. return;
  3672. }
  3673. #endif
  3674. /*
  3675. * We use locality group preallocation for small size file. The size of the
  3676. * file is determined by the current size or the resulting size after
  3677. * allocation which ever is larger
  3678. *
  3679. * One can tune this size via /proc/fs/ext4/<partition>/stream_req
  3680. */
  3681. static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
  3682. {
  3683. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  3684. int bsbits = ac->ac_sb->s_blocksize_bits;
  3685. loff_t size, isize;
  3686. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  3687. return;
  3688. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  3689. isize = i_size_read(ac->ac_inode) >> bsbits;
  3690. size = max(size, isize);
  3691. /* don't use group allocation for large files */
  3692. if (size >= sbi->s_mb_stream_request)
  3693. return;
  3694. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  3695. return;
  3696. BUG_ON(ac->ac_lg != NULL);
  3697. /*
  3698. * locality group prealloc space are per cpu. The reason for having
  3699. * per cpu locality group is to reduce the contention between block
  3700. * request from multiple CPUs.
  3701. */
  3702. ac->ac_lg = per_cpu_ptr(sbi->s_locality_groups, raw_smp_processor_id());
  3703. /* we're going to use group allocation */
  3704. ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
  3705. /* serialize all allocations in the group */
  3706. mutex_lock(&ac->ac_lg->lg_mutex);
  3707. }
  3708. static noinline_for_stack int
  3709. ext4_mb_initialize_context(struct ext4_allocation_context *ac,
  3710. struct ext4_allocation_request *ar)
  3711. {
  3712. struct super_block *sb = ar->inode->i_sb;
  3713. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3714. struct ext4_super_block *es = sbi->s_es;
  3715. ext4_group_t group;
  3716. unsigned int len;
  3717. ext4_fsblk_t goal;
  3718. ext4_grpblk_t block;
  3719. /* we can't allocate > group size */
  3720. len = ar->len;
  3721. /* just a dirty hack to filter too big requests */
  3722. if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
  3723. len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
  3724. /* start searching from the goal */
  3725. goal = ar->goal;
  3726. if (goal < le32_to_cpu(es->s_first_data_block) ||
  3727. goal >= ext4_blocks_count(es))
  3728. goal = le32_to_cpu(es->s_first_data_block);
  3729. ext4_get_group_no_and_offset(sb, goal, &group, &block);
  3730. /* set up allocation goals */
  3731. ac->ac_b_ex.fe_logical = ar->logical;
  3732. ac->ac_b_ex.fe_group = 0;
  3733. ac->ac_b_ex.fe_start = 0;
  3734. ac->ac_b_ex.fe_len = 0;
  3735. ac->ac_status = AC_STATUS_CONTINUE;
  3736. ac->ac_groups_scanned = 0;
  3737. ac->ac_ex_scanned = 0;
  3738. ac->ac_found = 0;
  3739. ac->ac_sb = sb;
  3740. ac->ac_inode = ar->inode;
  3741. ac->ac_o_ex.fe_logical = ar->logical;
  3742. ac->ac_o_ex.fe_group = group;
  3743. ac->ac_o_ex.fe_start = block;
  3744. ac->ac_o_ex.fe_len = len;
  3745. ac->ac_g_ex.fe_logical = ar->logical;
  3746. ac->ac_g_ex.fe_group = group;
  3747. ac->ac_g_ex.fe_start = block;
  3748. ac->ac_g_ex.fe_len = len;
  3749. ac->ac_f_ex.fe_len = 0;
  3750. ac->ac_flags = ar->flags;
  3751. ac->ac_2order = 0;
  3752. ac->ac_criteria = 0;
  3753. ac->ac_pa = NULL;
  3754. ac->ac_bitmap_page = NULL;
  3755. ac->ac_buddy_page = NULL;
  3756. ac->ac_lg = NULL;
  3757. /* we have to define context: we'll we work with a file or
  3758. * locality group. this is a policy, actually */
  3759. ext4_mb_group_or_file(ac);
  3760. mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
  3761. "left: %u/%u, right %u/%u to %swritable\n",
  3762. (unsigned) ar->len, (unsigned) ar->logical,
  3763. (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
  3764. (unsigned) ar->lleft, (unsigned) ar->pleft,
  3765. (unsigned) ar->lright, (unsigned) ar->pright,
  3766. atomic_read(&ar->inode->i_writecount) ? "" : "non-");
  3767. return 0;
  3768. }
  3769. static noinline_for_stack void
  3770. ext4_mb_discard_lg_preallocations(struct super_block *sb,
  3771. struct ext4_locality_group *lg,
  3772. int order, int total_entries)
  3773. {
  3774. ext4_group_t group = 0;
  3775. struct ext4_buddy e4b;
  3776. struct list_head discard_list;
  3777. struct ext4_prealloc_space *pa, *tmp;
  3778. struct ext4_allocation_context *ac;
  3779. mb_debug("discard locality group preallocation\n");
  3780. INIT_LIST_HEAD(&discard_list);
  3781. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3782. spin_lock(&lg->lg_prealloc_lock);
  3783. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
  3784. pa_inode_list) {
  3785. spin_lock(&pa->pa_lock);
  3786. if (atomic_read(&pa->pa_count)) {
  3787. /*
  3788. * This is the pa that we just used
  3789. * for block allocation. So don't
  3790. * free that
  3791. */
  3792. spin_unlock(&pa->pa_lock);
  3793. continue;
  3794. }
  3795. if (pa->pa_deleted) {
  3796. spin_unlock(&pa->pa_lock);
  3797. continue;
  3798. }
  3799. /* only lg prealloc space */
  3800. BUG_ON(!pa->pa_linear);
  3801. /* seems this one can be freed ... */
  3802. pa->pa_deleted = 1;
  3803. spin_unlock(&pa->pa_lock);
  3804. list_del_rcu(&pa->pa_inode_list);
  3805. list_add(&pa->u.pa_tmp_list, &discard_list);
  3806. total_entries--;
  3807. if (total_entries <= 5) {
  3808. /*
  3809. * we want to keep only 5 entries
  3810. * allowing it to grow to 8. This
  3811. * mak sure we don't call discard
  3812. * soon for this list.
  3813. */
  3814. break;
  3815. }
  3816. }
  3817. spin_unlock(&lg->lg_prealloc_lock);
  3818. list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
  3819. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3820. if (ext4_mb_load_buddy(sb, group, &e4b)) {
  3821. ext4_error(sb, __func__, "Error in loading buddy "
  3822. "information for %u", group);
  3823. continue;
  3824. }
  3825. ext4_lock_group(sb, group);
  3826. list_del(&pa->pa_group_list);
  3827. ext4_mb_release_group_pa(&e4b, pa, ac);
  3828. ext4_unlock_group(sb, group);
  3829. ext4_mb_release_desc(&e4b);
  3830. list_del(&pa->u.pa_tmp_list);
  3831. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3832. }
  3833. if (ac)
  3834. kmem_cache_free(ext4_ac_cachep, ac);
  3835. }
  3836. /*
  3837. * We have incremented pa_count. So it cannot be freed at this
  3838. * point. Also we hold lg_mutex. So no parallel allocation is
  3839. * possible from this lg. That means pa_free cannot be updated.
  3840. *
  3841. * A parallel ext4_mb_discard_group_preallocations is possible.
  3842. * which can cause the lg_prealloc_list to be updated.
  3843. */
  3844. static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
  3845. {
  3846. int order, added = 0, lg_prealloc_count = 1;
  3847. struct super_block *sb = ac->ac_sb;
  3848. struct ext4_locality_group *lg = ac->ac_lg;
  3849. struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
  3850. order = fls(pa->pa_free) - 1;
  3851. if (order > PREALLOC_TB_SIZE - 1)
  3852. /* The max size of hash table is PREALLOC_TB_SIZE */
  3853. order = PREALLOC_TB_SIZE - 1;
  3854. /* Add the prealloc space to lg */
  3855. rcu_read_lock();
  3856. list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
  3857. pa_inode_list) {
  3858. spin_lock(&tmp_pa->pa_lock);
  3859. if (tmp_pa->pa_deleted) {
  3860. spin_unlock(&pa->pa_lock);
  3861. continue;
  3862. }
  3863. if (!added && pa->pa_free < tmp_pa->pa_free) {
  3864. /* Add to the tail of the previous entry */
  3865. list_add_tail_rcu(&pa->pa_inode_list,
  3866. &tmp_pa->pa_inode_list);
  3867. added = 1;
  3868. /*
  3869. * we want to count the total
  3870. * number of entries in the list
  3871. */
  3872. }
  3873. spin_unlock(&tmp_pa->pa_lock);
  3874. lg_prealloc_count++;
  3875. }
  3876. if (!added)
  3877. list_add_tail_rcu(&pa->pa_inode_list,
  3878. &lg->lg_prealloc_list[order]);
  3879. rcu_read_unlock();
  3880. /* Now trim the list to be not more than 8 elements */
  3881. if (lg_prealloc_count > 8) {
  3882. ext4_mb_discard_lg_preallocations(sb, lg,
  3883. order, lg_prealloc_count);
  3884. return;
  3885. }
  3886. return ;
  3887. }
  3888. /*
  3889. * release all resource we used in allocation
  3890. */
  3891. static int ext4_mb_release_context(struct ext4_allocation_context *ac)
  3892. {
  3893. struct ext4_prealloc_space *pa = ac->ac_pa;
  3894. if (pa) {
  3895. if (pa->pa_linear) {
  3896. /* see comment in ext4_mb_use_group_pa() */
  3897. spin_lock(&pa->pa_lock);
  3898. pa->pa_pstart += ac->ac_b_ex.fe_len;
  3899. pa->pa_lstart += ac->ac_b_ex.fe_len;
  3900. pa->pa_free -= ac->ac_b_ex.fe_len;
  3901. pa->pa_len -= ac->ac_b_ex.fe_len;
  3902. spin_unlock(&pa->pa_lock);
  3903. /*
  3904. * We want to add the pa to the right bucket.
  3905. * Remove it from the list and while adding
  3906. * make sure the list to which we are adding
  3907. * doesn't grow big.
  3908. */
  3909. if (likely(pa->pa_free)) {
  3910. spin_lock(pa->pa_obj_lock);
  3911. list_del_rcu(&pa->pa_inode_list);
  3912. spin_unlock(pa->pa_obj_lock);
  3913. ext4_mb_add_n_trim(ac);
  3914. }
  3915. }
  3916. ext4_mb_put_pa(ac, ac->ac_sb, pa);
  3917. }
  3918. if (ac->ac_bitmap_page)
  3919. page_cache_release(ac->ac_bitmap_page);
  3920. if (ac->ac_buddy_page)
  3921. page_cache_release(ac->ac_buddy_page);
  3922. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3923. mutex_unlock(&ac->ac_lg->lg_mutex);
  3924. ext4_mb_collect_stats(ac);
  3925. return 0;
  3926. }
  3927. static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
  3928. {
  3929. ext4_group_t i;
  3930. int ret;
  3931. int freed = 0;
  3932. for (i = 0; i < EXT4_SB(sb)->s_groups_count && needed > 0; i++) {
  3933. ret = ext4_mb_discard_group_preallocations(sb, i, needed);
  3934. freed += ret;
  3935. needed -= ret;
  3936. }
  3937. return freed;
  3938. }
  3939. /*
  3940. * Main entry point into mballoc to allocate blocks
  3941. * it tries to use preallocation first, then falls back
  3942. * to usual allocation
  3943. */
  3944. ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  3945. struct ext4_allocation_request *ar, int *errp)
  3946. {
  3947. int freed;
  3948. struct ext4_allocation_context *ac = NULL;
  3949. struct ext4_sb_info *sbi;
  3950. struct super_block *sb;
  3951. ext4_fsblk_t block = 0;
  3952. unsigned int inquota;
  3953. unsigned int reserv_blks = 0;
  3954. sb = ar->inode->i_sb;
  3955. sbi = EXT4_SB(sb);
  3956. if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag) {
  3957. /*
  3958. * With delalloc we already reserved the blocks
  3959. */
  3960. while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
  3961. /* let others to free the space */
  3962. yield();
  3963. ar->len = ar->len >> 1;
  3964. }
  3965. if (!ar->len) {
  3966. *errp = -ENOSPC;
  3967. return 0;
  3968. }
  3969. reserv_blks = ar->len;
  3970. }
  3971. while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
  3972. ar->flags |= EXT4_MB_HINT_NOPREALLOC;
  3973. ar->len--;
  3974. }
  3975. if (ar->len == 0) {
  3976. *errp = -EDQUOT;
  3977. return 0;
  3978. }
  3979. inquota = ar->len;
  3980. if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
  3981. ar->flags |= EXT4_MB_DELALLOC_RESERVED;
  3982. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3983. if (!ac) {
  3984. ar->len = 0;
  3985. *errp = -ENOMEM;
  3986. goto out1;
  3987. }
  3988. *errp = ext4_mb_initialize_context(ac, ar);
  3989. if (*errp) {
  3990. ar->len = 0;
  3991. goto out2;
  3992. }
  3993. ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
  3994. if (!ext4_mb_use_preallocated(ac)) {
  3995. ac->ac_op = EXT4_MB_HISTORY_ALLOC;
  3996. ext4_mb_normalize_request(ac, ar);
  3997. repeat:
  3998. /* allocate space in core */
  3999. ext4_mb_regular_allocator(ac);
  4000. /* as we've just preallocated more space than
  4001. * user requested orinally, we store allocated
  4002. * space in a special descriptor */
  4003. if (ac->ac_status == AC_STATUS_FOUND &&
  4004. ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
  4005. ext4_mb_new_preallocation(ac);
  4006. }
  4007. if (likely(ac->ac_status == AC_STATUS_FOUND)) {
  4008. *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
  4009. if (*errp == -EAGAIN) {
  4010. ac->ac_b_ex.fe_group = 0;
  4011. ac->ac_b_ex.fe_start = 0;
  4012. ac->ac_b_ex.fe_len = 0;
  4013. ac->ac_status = AC_STATUS_CONTINUE;
  4014. goto repeat;
  4015. } else if (*errp) {
  4016. ac->ac_b_ex.fe_len = 0;
  4017. ar->len = 0;
  4018. ext4_mb_show_ac(ac);
  4019. } else {
  4020. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  4021. ar->len = ac->ac_b_ex.fe_len;
  4022. }
  4023. } else {
  4024. freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
  4025. if (freed)
  4026. goto repeat;
  4027. *errp = -ENOSPC;
  4028. ac->ac_b_ex.fe_len = 0;
  4029. ar->len = 0;
  4030. ext4_mb_show_ac(ac);
  4031. }
  4032. ext4_mb_release_context(ac);
  4033. out2:
  4034. kmem_cache_free(ext4_ac_cachep, ac);
  4035. out1:
  4036. if (ar->len < inquota)
  4037. DQUOT_FREE_BLOCK(ar->inode, inquota - ar->len);
  4038. return block;
  4039. }
  4040. /*
  4041. * We can merge two free data extents only if the physical blocks
  4042. * are contiguous, AND the extents were freed by the same transaction,
  4043. * AND the blocks are associated with the same group.
  4044. */
  4045. static int can_merge(struct ext4_free_data *entry1,
  4046. struct ext4_free_data *entry2)
  4047. {
  4048. if ((entry1->t_tid == entry2->t_tid) &&
  4049. (entry1->group == entry2->group) &&
  4050. ((entry1->start_blk + entry1->count) == entry2->start_blk))
  4051. return 1;
  4052. return 0;
  4053. }
  4054. static noinline_for_stack int
  4055. ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
  4056. struct ext4_free_data *new_entry)
  4057. {
  4058. ext4_grpblk_t block;
  4059. struct ext4_free_data *entry;
  4060. struct ext4_group_info *db = e4b->bd_info;
  4061. struct super_block *sb = e4b->bd_sb;
  4062. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4063. struct rb_node **n = &db->bb_free_root.rb_node, *node;
  4064. struct rb_node *parent = NULL, *new_node;
  4065. BUG_ON(!ext4_handle_valid(handle));
  4066. BUG_ON(e4b->bd_bitmap_page == NULL);
  4067. BUG_ON(e4b->bd_buddy_page == NULL);
  4068. new_node = &new_entry->node;
  4069. block = new_entry->start_blk;
  4070. if (!*n) {
  4071. /* first free block exent. We need to
  4072. protect buddy cache from being freed,
  4073. * otherwise we'll refresh it from
  4074. * on-disk bitmap and lose not-yet-available
  4075. * blocks */
  4076. page_cache_get(e4b->bd_buddy_page);
  4077. page_cache_get(e4b->bd_bitmap_page);
  4078. }
  4079. while (*n) {
  4080. parent = *n;
  4081. entry = rb_entry(parent, struct ext4_free_data, node);
  4082. if (block < entry->start_blk)
  4083. n = &(*n)->rb_left;
  4084. else if (block >= (entry->start_blk + entry->count))
  4085. n = &(*n)->rb_right;
  4086. else {
  4087. ext4_grp_locked_error(sb, e4b->bd_group, __func__,
  4088. "Double free of blocks %d (%d %d)",
  4089. block, entry->start_blk, entry->count);
  4090. return 0;
  4091. }
  4092. }
  4093. rb_link_node(new_node, parent, n);
  4094. rb_insert_color(new_node, &db->bb_free_root);
  4095. /* Now try to see the extent can be merged to left and right */
  4096. node = rb_prev(new_node);
  4097. if (node) {
  4098. entry = rb_entry(node, struct ext4_free_data, node);
  4099. if (can_merge(entry, new_entry)) {
  4100. new_entry->start_blk = entry->start_blk;
  4101. new_entry->count += entry->count;
  4102. rb_erase(node, &(db->bb_free_root));
  4103. spin_lock(&sbi->s_md_lock);
  4104. list_del(&entry->list);
  4105. spin_unlock(&sbi->s_md_lock);
  4106. kmem_cache_free(ext4_free_ext_cachep, entry);
  4107. }
  4108. }
  4109. node = rb_next(new_node);
  4110. if (node) {
  4111. entry = rb_entry(node, struct ext4_free_data, node);
  4112. if (can_merge(new_entry, entry)) {
  4113. new_entry->count += entry->count;
  4114. rb_erase(node, &(db->bb_free_root));
  4115. spin_lock(&sbi->s_md_lock);
  4116. list_del(&entry->list);
  4117. spin_unlock(&sbi->s_md_lock);
  4118. kmem_cache_free(ext4_free_ext_cachep, entry);
  4119. }
  4120. }
  4121. /* Add the extent to transaction's private list */
  4122. spin_lock(&sbi->s_md_lock);
  4123. list_add(&new_entry->list, &handle->h_transaction->t_private_list);
  4124. spin_unlock(&sbi->s_md_lock);
  4125. return 0;
  4126. }
  4127. /*
  4128. * Main entry point into mballoc to free blocks
  4129. */
  4130. void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
  4131. unsigned long block, unsigned long count,
  4132. int metadata, unsigned long *freed)
  4133. {
  4134. struct buffer_head *bitmap_bh = NULL;
  4135. struct super_block *sb = inode->i_sb;
  4136. struct ext4_allocation_context *ac = NULL;
  4137. struct ext4_group_desc *gdp;
  4138. struct ext4_super_block *es;
  4139. unsigned int overflow;
  4140. ext4_grpblk_t bit;
  4141. struct buffer_head *gd_bh;
  4142. ext4_group_t block_group;
  4143. struct ext4_sb_info *sbi;
  4144. struct ext4_buddy e4b;
  4145. int err = 0;
  4146. int ret;
  4147. *freed = 0;
  4148. sbi = EXT4_SB(sb);
  4149. es = EXT4_SB(sb)->s_es;
  4150. if (block < le32_to_cpu(es->s_first_data_block) ||
  4151. block + count < block ||
  4152. block + count > ext4_blocks_count(es)) {
  4153. ext4_error(sb, __func__,
  4154. "Freeing blocks not in datazone - "
  4155. "block = %lu, count = %lu", block, count);
  4156. goto error_return;
  4157. }
  4158. ext4_debug("freeing block %lu\n", block);
  4159. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  4160. if (ac) {
  4161. ac->ac_op = EXT4_MB_HISTORY_FREE;
  4162. ac->ac_inode = inode;
  4163. ac->ac_sb = sb;
  4164. }
  4165. do_more:
  4166. overflow = 0;
  4167. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  4168. /*
  4169. * Check to see if we are freeing blocks across a group
  4170. * boundary.
  4171. */
  4172. if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  4173. overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
  4174. count -= overflow;
  4175. }
  4176. bitmap_bh = ext4_read_block_bitmap(sb, block_group);
  4177. if (!bitmap_bh) {
  4178. err = -EIO;
  4179. goto error_return;
  4180. }
  4181. gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
  4182. if (!gdp) {
  4183. err = -EIO;
  4184. goto error_return;
  4185. }
  4186. if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
  4187. in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
  4188. in_range(block, ext4_inode_table(sb, gdp),
  4189. EXT4_SB(sb)->s_itb_per_group) ||
  4190. in_range(block + count - 1, ext4_inode_table(sb, gdp),
  4191. EXT4_SB(sb)->s_itb_per_group)) {
  4192. ext4_error(sb, __func__,
  4193. "Freeing blocks in system zone - "
  4194. "Block = %lu, count = %lu", block, count);
  4195. /* err = 0. ext4_std_error should be a no op */
  4196. goto error_return;
  4197. }
  4198. BUFFER_TRACE(bitmap_bh, "getting write access");
  4199. err = ext4_journal_get_write_access(handle, bitmap_bh);
  4200. if (err)
  4201. goto error_return;
  4202. /*
  4203. * We are about to modify some metadata. Call the journal APIs
  4204. * to unshare ->b_data if a currently-committing transaction is
  4205. * using it
  4206. */
  4207. BUFFER_TRACE(gd_bh, "get_write_access");
  4208. err = ext4_journal_get_write_access(handle, gd_bh);
  4209. if (err)
  4210. goto error_return;
  4211. #ifdef AGGRESSIVE_CHECK
  4212. {
  4213. int i;
  4214. for (i = 0; i < count; i++)
  4215. BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
  4216. }
  4217. #endif
  4218. if (ac) {
  4219. ac->ac_b_ex.fe_group = block_group;
  4220. ac->ac_b_ex.fe_start = bit;
  4221. ac->ac_b_ex.fe_len = count;
  4222. ext4_mb_store_history(ac);
  4223. }
  4224. err = ext4_mb_load_buddy(sb, block_group, &e4b);
  4225. if (err)
  4226. goto error_return;
  4227. if (metadata && ext4_handle_valid(handle)) {
  4228. struct ext4_free_data *new_entry;
  4229. /*
  4230. * blocks being freed are metadata. these blocks shouldn't
  4231. * be used until this transaction is committed
  4232. */
  4233. new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
  4234. new_entry->start_blk = bit;
  4235. new_entry->group = block_group;
  4236. new_entry->count = count;
  4237. new_entry->t_tid = handle->h_transaction->t_tid;
  4238. ext4_lock_group(sb, block_group);
  4239. mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
  4240. bit, count);
  4241. ext4_mb_free_metadata(handle, &e4b, new_entry);
  4242. ext4_unlock_group(sb, block_group);
  4243. } else {
  4244. ext4_lock_group(sb, block_group);
  4245. /* need to update group_info->bb_free and bitmap
  4246. * with group lock held. generate_buddy look at
  4247. * them with group lock_held
  4248. */
  4249. mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
  4250. bit, count);
  4251. mb_free_blocks(inode, &e4b, bit, count);
  4252. ext4_mb_return_to_preallocation(inode, &e4b, block, count);
  4253. ext4_unlock_group(sb, block_group);
  4254. }
  4255. spin_lock(sb_bgl_lock(sbi, block_group));
  4256. le16_add_cpu(&gdp->bg_free_blocks_count, count);
  4257. gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
  4258. spin_unlock(sb_bgl_lock(sbi, block_group));
  4259. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  4260. if (sbi->s_log_groups_per_flex) {
  4261. ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
  4262. spin_lock(sb_bgl_lock(sbi, flex_group));
  4263. sbi->s_flex_groups[flex_group].free_blocks += count;
  4264. spin_unlock(sb_bgl_lock(sbi, flex_group));
  4265. }
  4266. ext4_mb_release_desc(&e4b);
  4267. *freed += count;
  4268. /* We dirtied the bitmap block */
  4269. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  4270. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  4271. /* And the group descriptor block */
  4272. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  4273. ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
  4274. if (!err)
  4275. err = ret;
  4276. if (overflow && !err) {
  4277. block += count;
  4278. count = overflow;
  4279. put_bh(bitmap_bh);
  4280. goto do_more;
  4281. }
  4282. sb->s_dirt = 1;
  4283. error_return:
  4284. brelse(bitmap_bh);
  4285. ext4_std_error(sb, err);
  4286. if (ac)
  4287. kmem_cache_free(ext4_ac_cachep, ac);
  4288. return;
  4289. }