mballoc.c 147 KB

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