mballoc.c 138 KB

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