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