mballoc.c 127 KB

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