mballoc.c 127 KB

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