mballoc.c 138 KB

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