mballoc.c 133 KB

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