mballoc.c 136 KB

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