mballoc.c 138 KB

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