mballoc.c 133 KB

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