mballoc.c 143 KB

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