mballoc.c 125 KB

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