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