mballoc.c 143 KB

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