mballoc.c 133 KB

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