mballoc.c 144 KB

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