mballoc.c 126 KB

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