mballoc.c 132 KB

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