mballoc.c 124 KB

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