mballoc.c 141 KB

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