mballoc.c 138 KB

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