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. __func__, "double-free of inode"
  425. " %lu's block %llu(bit %u in group %u)",
  426. inode ? inode->i_ino : 0, blocknr,
  427. first + i, e4b->bd_group);
  428. }
  429. mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
  430. }
  431. }
  432. static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
  433. {
  434. int i;
  435. if (unlikely(e4b->bd_info->bb_bitmap == NULL))
  436. return;
  437. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  438. for (i = 0; i < count; i++) {
  439. BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
  440. mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
  441. }
  442. }
  443. static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
  444. {
  445. if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
  446. unsigned char *b1, *b2;
  447. int i;
  448. b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
  449. b2 = (unsigned char *) bitmap;
  450. for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
  451. if (b1[i] != b2[i]) {
  452. printk(KERN_ERR "corruption in group %u "
  453. "at byte %u(%u): %x in copy != %x "
  454. "on disk/prealloc\n",
  455. e4b->bd_group, i, i * 8, b1[i], b2[i]);
  456. BUG();
  457. }
  458. }
  459. }
  460. }
  461. #else
  462. static inline void mb_free_blocks_double(struct inode *inode,
  463. struct ext4_buddy *e4b, int first, int count)
  464. {
  465. return;
  466. }
  467. static inline void mb_mark_used_double(struct ext4_buddy *e4b,
  468. int first, int count)
  469. {
  470. return;
  471. }
  472. static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
  473. {
  474. return;
  475. }
  476. #endif
  477. #ifdef AGGRESSIVE_CHECK
  478. #define MB_CHECK_ASSERT(assert) \
  479. do { \
  480. if (!(assert)) { \
  481. printk(KERN_EMERG \
  482. "Assertion failure in %s() at %s:%d: \"%s\"\n", \
  483. function, file, line, # assert); \
  484. BUG(); \
  485. } \
  486. } while (0)
  487. static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
  488. const char *function, int line)
  489. {
  490. struct super_block *sb = e4b->bd_sb;
  491. int order = e4b->bd_blkbits + 1;
  492. int max;
  493. int max2;
  494. int i;
  495. int j;
  496. int k;
  497. int count;
  498. struct ext4_group_info *grp;
  499. int fragments = 0;
  500. int fstart;
  501. struct list_head *cur;
  502. void *buddy;
  503. void *buddy2;
  504. {
  505. static int mb_check_counter;
  506. if (mb_check_counter++ % 100 != 0)
  507. return 0;
  508. }
  509. while (order > 1) {
  510. buddy = mb_find_buddy(e4b, order, &max);
  511. MB_CHECK_ASSERT(buddy);
  512. buddy2 = mb_find_buddy(e4b, order - 1, &max2);
  513. MB_CHECK_ASSERT(buddy2);
  514. MB_CHECK_ASSERT(buddy != buddy2);
  515. MB_CHECK_ASSERT(max * 2 == max2);
  516. count = 0;
  517. for (i = 0; i < max; i++) {
  518. if (mb_test_bit(i, buddy)) {
  519. /* only single bit in buddy2 may be 1 */
  520. if (!mb_test_bit(i << 1, buddy2)) {
  521. MB_CHECK_ASSERT(
  522. mb_test_bit((i<<1)+1, buddy2));
  523. } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
  524. MB_CHECK_ASSERT(
  525. mb_test_bit(i << 1, buddy2));
  526. }
  527. continue;
  528. }
  529. /* both bits in buddy2 must be 0 */
  530. MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
  531. MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
  532. for (j = 0; j < (1 << order); j++) {
  533. k = (i * (1 << order)) + j;
  534. MB_CHECK_ASSERT(
  535. !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
  536. }
  537. count++;
  538. }
  539. MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
  540. order--;
  541. }
  542. fstart = -1;
  543. buddy = mb_find_buddy(e4b, 0, &max);
  544. for (i = 0; i < max; i++) {
  545. if (!mb_test_bit(i, buddy)) {
  546. MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
  547. if (fstart == -1) {
  548. fragments++;
  549. fstart = i;
  550. }
  551. continue;
  552. }
  553. fstart = -1;
  554. /* check used bits only */
  555. for (j = 0; j < e4b->bd_blkbits + 1; j++) {
  556. buddy2 = mb_find_buddy(e4b, j, &max2);
  557. k = i >> j;
  558. MB_CHECK_ASSERT(k < max2);
  559. MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
  560. }
  561. }
  562. MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
  563. MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
  564. grp = ext4_get_group_info(sb, e4b->bd_group);
  565. buddy = mb_find_buddy(e4b, 0, &max);
  566. list_for_each(cur, &grp->bb_prealloc_list) {
  567. ext4_group_t groupnr;
  568. struct ext4_prealloc_space *pa;
  569. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  570. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
  571. MB_CHECK_ASSERT(groupnr == e4b->bd_group);
  572. for (i = 0; i < pa->pa_len; i++)
  573. MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
  574. }
  575. return 0;
  576. }
  577. #undef MB_CHECK_ASSERT
  578. #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
  579. __FILE__, __func__, __LINE__)
  580. #else
  581. #define mb_check_buddy(e4b)
  582. #endif
  583. /* FIXME!! need more doc */
  584. static void ext4_mb_mark_free_simple(struct super_block *sb,
  585. void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
  586. struct ext4_group_info *grp)
  587. {
  588. struct ext4_sb_info *sbi = EXT4_SB(sb);
  589. ext4_grpblk_t min;
  590. ext4_grpblk_t max;
  591. ext4_grpblk_t chunk;
  592. unsigned short border;
  593. BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
  594. border = 2 << sb->s_blocksize_bits;
  595. while (len > 0) {
  596. /* find how many blocks can be covered since this position */
  597. max = ffs(first | border) - 1;
  598. /* find how many blocks of power 2 we need to mark */
  599. min = fls(len) - 1;
  600. if (max < min)
  601. min = max;
  602. chunk = 1 << min;
  603. /* mark multiblock chunks only */
  604. grp->bb_counters[min]++;
  605. if (min > 0)
  606. mb_clear_bit(first >> min,
  607. buddy + sbi->s_mb_offsets[min]);
  608. len -= chunk;
  609. first += chunk;
  610. }
  611. }
  612. /*
  613. * Cache the order of the largest free extent we have available in this block
  614. * group.
  615. */
  616. static void
  617. mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
  618. {
  619. int i;
  620. int bits;
  621. grp->bb_largest_free_order = -1; /* uninit */
  622. bits = sb->s_blocksize_bits + 1;
  623. for (i = bits; i >= 0; i--) {
  624. if (grp->bb_counters[i] > 0) {
  625. grp->bb_largest_free_order = i;
  626. break;
  627. }
  628. }
  629. }
  630. static noinline_for_stack
  631. void ext4_mb_generate_buddy(struct super_block *sb,
  632. void *buddy, void *bitmap, ext4_group_t group)
  633. {
  634. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  635. ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
  636. ext4_grpblk_t i = 0;
  637. ext4_grpblk_t first;
  638. ext4_grpblk_t len;
  639. unsigned free = 0;
  640. unsigned fragments = 0;
  641. unsigned long long period = get_cycles();
  642. /* initialize buddy from bitmap which is aggregation
  643. * of on-disk bitmap and preallocations */
  644. i = mb_find_next_zero_bit(bitmap, max, 0);
  645. grp->bb_first_free = i;
  646. while (i < max) {
  647. fragments++;
  648. first = i;
  649. i = mb_find_next_bit(bitmap, max, i);
  650. len = i - first;
  651. free += len;
  652. if (len > 1)
  653. ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
  654. else
  655. grp->bb_counters[0]++;
  656. if (i < max)
  657. i = mb_find_next_zero_bit(bitmap, max, i);
  658. }
  659. grp->bb_fragments = fragments;
  660. if (free != grp->bb_free) {
  661. ext4_grp_locked_error(sb, group, __func__,
  662. "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
  663. group, free, grp->bb_free);
  664. /*
  665. * If we intent to continue, we consider group descritor
  666. * corrupt and update bb_free using bitmap value
  667. */
  668. grp->bb_free = free;
  669. }
  670. mb_set_largest_free_order(sb, grp);
  671. clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
  672. period = get_cycles() - period;
  673. spin_lock(&EXT4_SB(sb)->s_bal_lock);
  674. EXT4_SB(sb)->s_mb_buddies_generated++;
  675. EXT4_SB(sb)->s_mb_generation_time += period;
  676. spin_unlock(&EXT4_SB(sb)->s_bal_lock);
  677. }
  678. /* The buddy information is attached the buddy cache inode
  679. * for convenience. The information regarding each group
  680. * is loaded via ext4_mb_load_buddy. The information involve
  681. * block bitmap and buddy information. The information are
  682. * stored in the inode as
  683. *
  684. * { page }
  685. * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
  686. *
  687. *
  688. * one block each for bitmap and buddy information.
  689. * So for each group we take up 2 blocks. A page can
  690. * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
  691. * So it can have information regarding groups_per_page which
  692. * is blocks_per_page/2
  693. *
  694. * Locking note: This routine takes the block group lock of all groups
  695. * for this page; do not hold this lock when calling this routine!
  696. */
  697. static int ext4_mb_init_cache(struct page *page, char *incore)
  698. {
  699. ext4_group_t ngroups;
  700. int blocksize;
  701. int blocks_per_page;
  702. int groups_per_page;
  703. int err = 0;
  704. int i;
  705. ext4_group_t first_group;
  706. int first_block;
  707. struct super_block *sb;
  708. struct buffer_head *bhs;
  709. struct buffer_head **bh;
  710. struct inode *inode;
  711. char *data;
  712. char *bitmap;
  713. mb_debug(1, "init page %lu\n", page->index);
  714. inode = page->mapping->host;
  715. sb = inode->i_sb;
  716. ngroups = ext4_get_groups_count(sb);
  717. blocksize = 1 << inode->i_blkbits;
  718. blocks_per_page = PAGE_CACHE_SIZE / blocksize;
  719. groups_per_page = blocks_per_page >> 1;
  720. if (groups_per_page == 0)
  721. groups_per_page = 1;
  722. /* allocate buffer_heads to read bitmaps */
  723. if (groups_per_page > 1) {
  724. err = -ENOMEM;
  725. i = sizeof(struct buffer_head *) * groups_per_page;
  726. bh = kzalloc(i, GFP_NOFS);
  727. if (bh == NULL)
  728. goto out;
  729. } else
  730. bh = &bhs;
  731. first_group = page->index * blocks_per_page / 2;
  732. /* read all groups the page covers into the cache */
  733. for (i = 0; i < groups_per_page; i++) {
  734. struct ext4_group_desc *desc;
  735. if (first_group + i >= ngroups)
  736. break;
  737. err = -EIO;
  738. desc = ext4_get_group_desc(sb, first_group + i, NULL);
  739. if (desc == NULL)
  740. goto out;
  741. err = -ENOMEM;
  742. bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
  743. if (bh[i] == NULL)
  744. goto out;
  745. if (bitmap_uptodate(bh[i]))
  746. continue;
  747. lock_buffer(bh[i]);
  748. if (bitmap_uptodate(bh[i])) {
  749. unlock_buffer(bh[i]);
  750. continue;
  751. }
  752. ext4_lock_group(sb, first_group + i);
  753. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  754. ext4_init_block_bitmap(sb, bh[i],
  755. first_group + i, desc);
  756. set_bitmap_uptodate(bh[i]);
  757. set_buffer_uptodate(bh[i]);
  758. ext4_unlock_group(sb, first_group + i);
  759. unlock_buffer(bh[i]);
  760. continue;
  761. }
  762. ext4_unlock_group(sb, first_group + i);
  763. if (buffer_uptodate(bh[i])) {
  764. /*
  765. * if not uninit if bh is uptodate,
  766. * bitmap is also uptodate
  767. */
  768. set_bitmap_uptodate(bh[i]);
  769. unlock_buffer(bh[i]);
  770. continue;
  771. }
  772. get_bh(bh[i]);
  773. /*
  774. * submit the buffer_head for read. We can
  775. * safely mark the bitmap as uptodate now.
  776. * We do it here so the bitmap uptodate bit
  777. * get set with buffer lock held.
  778. */
  779. set_bitmap_uptodate(bh[i]);
  780. bh[i]->b_end_io = end_buffer_read_sync;
  781. submit_bh(READ, bh[i]);
  782. mb_debug(1, "read bitmap for group %u\n", first_group + i);
  783. }
  784. /* wait for I/O completion */
  785. for (i = 0; i < groups_per_page && bh[i]; i++)
  786. wait_on_buffer(bh[i]);
  787. err = -EIO;
  788. for (i = 0; i < groups_per_page && bh[i]; i++)
  789. if (!buffer_uptodate(bh[i]))
  790. goto out;
  791. err = 0;
  792. first_block = page->index * blocks_per_page;
  793. /* init the page */
  794. memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
  795. for (i = 0; i < blocks_per_page; i++) {
  796. int group;
  797. struct ext4_group_info *grinfo;
  798. group = (first_block + i) >> 1;
  799. if (group >= ngroups)
  800. break;
  801. /*
  802. * data carry information regarding this
  803. * particular group in the format specified
  804. * above
  805. *
  806. */
  807. data = page_address(page) + (i * blocksize);
  808. bitmap = bh[group - first_group]->b_data;
  809. /*
  810. * We place the buddy block and bitmap block
  811. * close together
  812. */
  813. if ((first_block + i) & 1) {
  814. /* this is block of buddy */
  815. BUG_ON(incore == NULL);
  816. mb_debug(1, "put buddy for group %u in page %lu/%x\n",
  817. group, page->index, i * blocksize);
  818. trace_ext4_mb_buddy_bitmap_load(sb, group);
  819. grinfo = ext4_get_group_info(sb, group);
  820. grinfo->bb_fragments = 0;
  821. memset(grinfo->bb_counters, 0,
  822. sizeof(*grinfo->bb_counters) *
  823. (sb->s_blocksize_bits+2));
  824. /*
  825. * incore got set to the group block bitmap below
  826. */
  827. ext4_lock_group(sb, group);
  828. ext4_mb_generate_buddy(sb, data, incore, group);
  829. ext4_unlock_group(sb, group);
  830. incore = NULL;
  831. } else {
  832. /* this is block of bitmap */
  833. BUG_ON(incore != NULL);
  834. mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
  835. group, page->index, i * blocksize);
  836. trace_ext4_mb_bitmap_load(sb, group);
  837. /* see comments in ext4_mb_put_pa() */
  838. ext4_lock_group(sb, group);
  839. memcpy(data, bitmap, blocksize);
  840. /* mark all preallocated blks used in in-core bitmap */
  841. ext4_mb_generate_from_pa(sb, data, group);
  842. ext4_mb_generate_from_freelist(sb, data, group);
  843. ext4_unlock_group(sb, group);
  844. /* set incore so that the buddy information can be
  845. * generated using this
  846. */
  847. incore = data;
  848. }
  849. }
  850. SetPageUptodate(page);
  851. out:
  852. if (bh) {
  853. for (i = 0; i < groups_per_page && bh[i]; i++)
  854. brelse(bh[i]);
  855. if (bh != &bhs)
  856. kfree(bh);
  857. }
  858. return err;
  859. }
  860. /*
  861. * Locking note: This routine calls ext4_mb_init_cache(), which takes the
  862. * block group lock of all groups for this page; do not hold the BG lock when
  863. * calling this routine!
  864. */
  865. static noinline_for_stack
  866. int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
  867. {
  868. int ret = 0;
  869. void *bitmap;
  870. int blocks_per_page;
  871. int block, pnum, poff;
  872. int num_grp_locked = 0;
  873. struct ext4_group_info *this_grp;
  874. struct ext4_sb_info *sbi = EXT4_SB(sb);
  875. struct inode *inode = sbi->s_buddy_cache;
  876. struct page *page = NULL, *bitmap_page = NULL;
  877. mb_debug(1, "init group %u\n", group);
  878. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  879. this_grp = ext4_get_group_info(sb, group);
  880. /*
  881. * This ensures that we don't reinit the buddy cache
  882. * page which map to the group from which we are already
  883. * allocating. If we are looking at the buddy cache we would
  884. * have taken a reference using ext4_mb_load_buddy and that
  885. * would have taken the alloc_sem lock.
  886. */
  887. num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
  888. if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
  889. /*
  890. * somebody initialized the group
  891. * return without doing anything
  892. */
  893. ret = 0;
  894. goto err;
  895. }
  896. /*
  897. * the buddy cache inode stores the block bitmap
  898. * and buddy information in consecutive blocks.
  899. * So for each group we need two blocks.
  900. */
  901. block = group * 2;
  902. pnum = block / blocks_per_page;
  903. poff = block % blocks_per_page;
  904. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  905. if (page) {
  906. BUG_ON(page->mapping != inode->i_mapping);
  907. ret = ext4_mb_init_cache(page, NULL);
  908. if (ret) {
  909. unlock_page(page);
  910. goto err;
  911. }
  912. unlock_page(page);
  913. }
  914. if (page == NULL || !PageUptodate(page)) {
  915. ret = -EIO;
  916. goto err;
  917. }
  918. mark_page_accessed(page);
  919. bitmap_page = page;
  920. bitmap = page_address(page) + (poff * sb->s_blocksize);
  921. /* init buddy cache */
  922. block++;
  923. pnum = block / blocks_per_page;
  924. poff = block % blocks_per_page;
  925. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  926. if (page == bitmap_page) {
  927. /*
  928. * If both the bitmap and buddy are in
  929. * the same page we don't need to force
  930. * init the buddy
  931. */
  932. unlock_page(page);
  933. } else if (page) {
  934. BUG_ON(page->mapping != inode->i_mapping);
  935. ret = ext4_mb_init_cache(page, bitmap);
  936. if (ret) {
  937. unlock_page(page);
  938. goto err;
  939. }
  940. unlock_page(page);
  941. }
  942. if (page == NULL || !PageUptodate(page)) {
  943. ret = -EIO;
  944. goto err;
  945. }
  946. mark_page_accessed(page);
  947. err:
  948. ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
  949. if (bitmap_page)
  950. page_cache_release(bitmap_page);
  951. if (page)
  952. page_cache_release(page);
  953. return ret;
  954. }
  955. /*
  956. * Locking note: This routine calls ext4_mb_init_cache(), which takes the
  957. * block group lock of all groups for this page; do not hold the BG lock when
  958. * calling this routine!
  959. */
  960. static noinline_for_stack int
  961. ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
  962. struct ext4_buddy *e4b)
  963. {
  964. int blocks_per_page;
  965. int block;
  966. int pnum;
  967. int poff;
  968. struct page *page;
  969. int ret;
  970. struct ext4_group_info *grp;
  971. struct ext4_sb_info *sbi = EXT4_SB(sb);
  972. struct inode *inode = sbi->s_buddy_cache;
  973. mb_debug(1, "load group %u\n", group);
  974. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  975. grp = ext4_get_group_info(sb, group);
  976. e4b->bd_blkbits = sb->s_blocksize_bits;
  977. e4b->bd_info = ext4_get_group_info(sb, group);
  978. e4b->bd_sb = sb;
  979. e4b->bd_group = group;
  980. e4b->bd_buddy_page = NULL;
  981. e4b->bd_bitmap_page = NULL;
  982. e4b->alloc_semp = &grp->alloc_sem;
  983. /* Take the read lock on the group alloc
  984. * sem. This would make sure a parallel
  985. * ext4_mb_init_group happening on other
  986. * groups mapped by the page is blocked
  987. * till we are done with allocation
  988. */
  989. repeat_load_buddy:
  990. down_read(e4b->alloc_semp);
  991. if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
  992. /* we need to check for group need init flag
  993. * with alloc_semp held so that we can be sure
  994. * that new blocks didn't get added to the group
  995. * when we are loading the buddy cache
  996. */
  997. up_read(e4b->alloc_semp);
  998. /*
  999. * we need full data about the group
  1000. * to make a good selection
  1001. */
  1002. ret = ext4_mb_init_group(sb, group);
  1003. if (ret)
  1004. return ret;
  1005. goto repeat_load_buddy;
  1006. }
  1007. /*
  1008. * the buddy cache inode stores the block bitmap
  1009. * and buddy information in consecutive blocks.
  1010. * So for each group we need two blocks.
  1011. */
  1012. block = group * 2;
  1013. pnum = block / blocks_per_page;
  1014. poff = block % blocks_per_page;
  1015. /* we could use find_or_create_page(), but it locks page
  1016. * what we'd like to avoid in fast path ... */
  1017. page = find_get_page(inode->i_mapping, pnum);
  1018. if (page == NULL || !PageUptodate(page)) {
  1019. if (page)
  1020. /*
  1021. * drop the page reference and try
  1022. * to get the page with lock. If we
  1023. * are not uptodate that implies
  1024. * somebody just created the page but
  1025. * is yet to initialize the same. So
  1026. * wait for it to initialize.
  1027. */
  1028. page_cache_release(page);
  1029. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1030. if (page) {
  1031. BUG_ON(page->mapping != inode->i_mapping);
  1032. if (!PageUptodate(page)) {
  1033. ret = ext4_mb_init_cache(page, NULL);
  1034. if (ret) {
  1035. unlock_page(page);
  1036. goto err;
  1037. }
  1038. mb_cmp_bitmaps(e4b, page_address(page) +
  1039. (poff * sb->s_blocksize));
  1040. }
  1041. unlock_page(page);
  1042. }
  1043. }
  1044. if (page == NULL || !PageUptodate(page)) {
  1045. ret = -EIO;
  1046. goto err;
  1047. }
  1048. e4b->bd_bitmap_page = page;
  1049. e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
  1050. mark_page_accessed(page);
  1051. block++;
  1052. pnum = block / blocks_per_page;
  1053. poff = block % blocks_per_page;
  1054. page = find_get_page(inode->i_mapping, pnum);
  1055. if (page == NULL || !PageUptodate(page)) {
  1056. if (page)
  1057. page_cache_release(page);
  1058. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1059. if (page) {
  1060. BUG_ON(page->mapping != inode->i_mapping);
  1061. if (!PageUptodate(page)) {
  1062. ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
  1063. if (ret) {
  1064. unlock_page(page);
  1065. goto err;
  1066. }
  1067. }
  1068. unlock_page(page);
  1069. }
  1070. }
  1071. if (page == NULL || !PageUptodate(page)) {
  1072. ret = -EIO;
  1073. goto err;
  1074. }
  1075. e4b->bd_buddy_page = page;
  1076. e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
  1077. mark_page_accessed(page);
  1078. BUG_ON(e4b->bd_bitmap_page == NULL);
  1079. BUG_ON(e4b->bd_buddy_page == NULL);
  1080. return 0;
  1081. err:
  1082. if (e4b->bd_bitmap_page)
  1083. page_cache_release(e4b->bd_bitmap_page);
  1084. if (e4b->bd_buddy_page)
  1085. page_cache_release(e4b->bd_buddy_page);
  1086. e4b->bd_buddy = NULL;
  1087. e4b->bd_bitmap = NULL;
  1088. /* Done with the buddy cache */
  1089. up_read(e4b->alloc_semp);
  1090. return ret;
  1091. }
  1092. static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
  1093. {
  1094. if (e4b->bd_bitmap_page)
  1095. page_cache_release(e4b->bd_bitmap_page);
  1096. if (e4b->bd_buddy_page)
  1097. page_cache_release(e4b->bd_buddy_page);
  1098. /* Done with the buddy cache */
  1099. if (e4b->alloc_semp)
  1100. up_read(e4b->alloc_semp);
  1101. }
  1102. static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
  1103. {
  1104. int order = 1;
  1105. void *bb;
  1106. BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
  1107. BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
  1108. bb = EXT4_MB_BUDDY(e4b);
  1109. while (order <= e4b->bd_blkbits + 1) {
  1110. block = block >> 1;
  1111. if (!mb_test_bit(block, bb)) {
  1112. /* this block is part of buddy of order 'order' */
  1113. return order;
  1114. }
  1115. bb += 1 << (e4b->bd_blkbits - order);
  1116. order++;
  1117. }
  1118. return 0;
  1119. }
  1120. static void mb_clear_bits(void *bm, int cur, int len)
  1121. {
  1122. __u32 *addr;
  1123. len = cur + len;
  1124. while (cur < len) {
  1125. if ((cur & 31) == 0 && (len - cur) >= 32) {
  1126. /* fast path: clear whole word at once */
  1127. addr = bm + (cur >> 3);
  1128. *addr = 0;
  1129. cur += 32;
  1130. continue;
  1131. }
  1132. mb_clear_bit(cur, bm);
  1133. cur++;
  1134. }
  1135. }
  1136. static void mb_set_bits(void *bm, int cur, int len)
  1137. {
  1138. __u32 *addr;
  1139. len = cur + len;
  1140. while (cur < len) {
  1141. if ((cur & 31) == 0 && (len - cur) >= 32) {
  1142. /* fast path: set whole word at once */
  1143. addr = bm + (cur >> 3);
  1144. *addr = 0xffffffff;
  1145. cur += 32;
  1146. continue;
  1147. }
  1148. mb_set_bit(cur, bm);
  1149. cur++;
  1150. }
  1151. }
  1152. static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
  1153. int first, int count)
  1154. {
  1155. int block = 0;
  1156. int max = 0;
  1157. int order;
  1158. void *buddy;
  1159. void *buddy2;
  1160. struct super_block *sb = e4b->bd_sb;
  1161. BUG_ON(first + count > (sb->s_blocksize << 3));
  1162. assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
  1163. mb_check_buddy(e4b);
  1164. mb_free_blocks_double(inode, e4b, first, count);
  1165. e4b->bd_info->bb_free += count;
  1166. if (first < e4b->bd_info->bb_first_free)
  1167. e4b->bd_info->bb_first_free = first;
  1168. /* let's maintain fragments counter */
  1169. if (first != 0)
  1170. block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
  1171. if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
  1172. max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
  1173. if (block && max)
  1174. e4b->bd_info->bb_fragments--;
  1175. else if (!block && !max)
  1176. e4b->bd_info->bb_fragments++;
  1177. /* let's maintain buddy itself */
  1178. while (count-- > 0) {
  1179. block = first++;
  1180. order = 0;
  1181. if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
  1182. ext4_fsblk_t blocknr;
  1183. blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
  1184. blocknr += block;
  1185. ext4_grp_locked_error(sb, e4b->bd_group,
  1186. __func__, "double-free of inode"
  1187. " %lu's block %llu(bit %u in group %u)",
  1188. inode ? inode->i_ino : 0, blocknr, block,
  1189. e4b->bd_group);
  1190. }
  1191. mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
  1192. e4b->bd_info->bb_counters[order]++;
  1193. /* start of the buddy */
  1194. buddy = mb_find_buddy(e4b, order, &max);
  1195. do {
  1196. block &= ~1UL;
  1197. if (mb_test_bit(block, buddy) ||
  1198. mb_test_bit(block + 1, buddy))
  1199. break;
  1200. /* both the buddies are free, try to coalesce them */
  1201. buddy2 = mb_find_buddy(e4b, order + 1, &max);
  1202. if (!buddy2)
  1203. break;
  1204. if (order > 0) {
  1205. /* for special purposes, we don't set
  1206. * free bits in bitmap */
  1207. mb_set_bit(block, buddy);
  1208. mb_set_bit(block + 1, buddy);
  1209. }
  1210. e4b->bd_info->bb_counters[order]--;
  1211. e4b->bd_info->bb_counters[order]--;
  1212. block = block >> 1;
  1213. order++;
  1214. e4b->bd_info->bb_counters[order]++;
  1215. mb_clear_bit(block, buddy2);
  1216. buddy = buddy2;
  1217. } while (1);
  1218. }
  1219. mb_set_largest_free_order(sb, e4b->bd_info);
  1220. mb_check_buddy(e4b);
  1221. }
  1222. static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
  1223. int needed, struct ext4_free_extent *ex)
  1224. {
  1225. int next = block;
  1226. int max;
  1227. int ord;
  1228. void *buddy;
  1229. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  1230. BUG_ON(ex == NULL);
  1231. buddy = mb_find_buddy(e4b, order, &max);
  1232. BUG_ON(buddy == NULL);
  1233. BUG_ON(block >= max);
  1234. if (mb_test_bit(block, buddy)) {
  1235. ex->fe_len = 0;
  1236. ex->fe_start = 0;
  1237. ex->fe_group = 0;
  1238. return 0;
  1239. }
  1240. /* FIXME dorp order completely ? */
  1241. if (likely(order == 0)) {
  1242. /* find actual order */
  1243. order = mb_find_order_for_block(e4b, block);
  1244. block = block >> order;
  1245. }
  1246. ex->fe_len = 1 << order;
  1247. ex->fe_start = block << order;
  1248. ex->fe_group = e4b->bd_group;
  1249. /* calc difference from given start */
  1250. next = next - ex->fe_start;
  1251. ex->fe_len -= next;
  1252. ex->fe_start += next;
  1253. while (needed > ex->fe_len &&
  1254. (buddy = mb_find_buddy(e4b, order, &max))) {
  1255. if (block + 1 >= max)
  1256. break;
  1257. next = (block + 1) * (1 << order);
  1258. if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
  1259. break;
  1260. ord = mb_find_order_for_block(e4b, next);
  1261. order = ord;
  1262. block = next >> order;
  1263. ex->fe_len += 1 << order;
  1264. }
  1265. BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
  1266. return ex->fe_len;
  1267. }
  1268. static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
  1269. {
  1270. int ord;
  1271. int mlen = 0;
  1272. int max = 0;
  1273. int cur;
  1274. int start = ex->fe_start;
  1275. int len = ex->fe_len;
  1276. unsigned ret = 0;
  1277. int len0 = len;
  1278. void *buddy;
  1279. BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
  1280. BUG_ON(e4b->bd_group != ex->fe_group);
  1281. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  1282. mb_check_buddy(e4b);
  1283. mb_mark_used_double(e4b, start, len);
  1284. e4b->bd_info->bb_free -= len;
  1285. if (e4b->bd_info->bb_first_free == start)
  1286. e4b->bd_info->bb_first_free += len;
  1287. /* let's maintain fragments counter */
  1288. if (start != 0)
  1289. mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
  1290. if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
  1291. max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
  1292. if (mlen && max)
  1293. e4b->bd_info->bb_fragments++;
  1294. else if (!mlen && !max)
  1295. e4b->bd_info->bb_fragments--;
  1296. /* let's maintain buddy itself */
  1297. while (len) {
  1298. ord = mb_find_order_for_block(e4b, start);
  1299. if (((start >> ord) << ord) == start && len >= (1 << ord)) {
  1300. /* the whole chunk may be allocated at once! */
  1301. mlen = 1 << ord;
  1302. buddy = mb_find_buddy(e4b, ord, &max);
  1303. BUG_ON((start >> ord) >= max);
  1304. mb_set_bit(start >> ord, buddy);
  1305. e4b->bd_info->bb_counters[ord]--;
  1306. start += mlen;
  1307. len -= mlen;
  1308. BUG_ON(len < 0);
  1309. continue;
  1310. }
  1311. /* store for history */
  1312. if (ret == 0)
  1313. ret = len | (ord << 16);
  1314. /* we have to split large buddy */
  1315. BUG_ON(ord <= 0);
  1316. buddy = mb_find_buddy(e4b, ord, &max);
  1317. mb_set_bit(start >> ord, buddy);
  1318. e4b->bd_info->bb_counters[ord]--;
  1319. ord--;
  1320. cur = (start >> ord) & ~1U;
  1321. buddy = mb_find_buddy(e4b, ord, &max);
  1322. mb_clear_bit(cur, buddy);
  1323. mb_clear_bit(cur + 1, buddy);
  1324. e4b->bd_info->bb_counters[ord]++;
  1325. e4b->bd_info->bb_counters[ord]++;
  1326. }
  1327. mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
  1328. mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
  1329. mb_check_buddy(e4b);
  1330. return ret;
  1331. }
  1332. /*
  1333. * Must be called under group lock!
  1334. */
  1335. static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
  1336. struct ext4_buddy *e4b)
  1337. {
  1338. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1339. int ret;
  1340. BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
  1341. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1342. ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
  1343. ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
  1344. ret = mb_mark_used(e4b, &ac->ac_b_ex);
  1345. /* preallocation can change ac_b_ex, thus we store actually
  1346. * allocated blocks for history */
  1347. ac->ac_f_ex = ac->ac_b_ex;
  1348. ac->ac_status = AC_STATUS_FOUND;
  1349. ac->ac_tail = ret & 0xffff;
  1350. ac->ac_buddy = ret >> 16;
  1351. /*
  1352. * take the page reference. We want the page to be pinned
  1353. * so that we don't get a ext4_mb_init_cache_call for this
  1354. * group until we update the bitmap. That would mean we
  1355. * double allocate blocks. The reference is dropped
  1356. * in ext4_mb_release_context
  1357. */
  1358. ac->ac_bitmap_page = e4b->bd_bitmap_page;
  1359. get_page(ac->ac_bitmap_page);
  1360. ac->ac_buddy_page = e4b->bd_buddy_page;
  1361. get_page(ac->ac_buddy_page);
  1362. /* on allocation we use ac to track the held semaphore */
  1363. ac->alloc_semp = e4b->alloc_semp;
  1364. e4b->alloc_semp = NULL;
  1365. /* store last allocated for subsequent stream allocation */
  1366. if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
  1367. spin_lock(&sbi->s_md_lock);
  1368. sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
  1369. sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
  1370. spin_unlock(&sbi->s_md_lock);
  1371. }
  1372. }
  1373. /*
  1374. * regular allocator, for general purposes allocation
  1375. */
  1376. static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
  1377. struct ext4_buddy *e4b,
  1378. int finish_group)
  1379. {
  1380. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1381. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1382. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1383. struct ext4_free_extent ex;
  1384. int max;
  1385. if (ac->ac_status == AC_STATUS_FOUND)
  1386. return;
  1387. /*
  1388. * We don't want to scan for a whole year
  1389. */
  1390. if (ac->ac_found > sbi->s_mb_max_to_scan &&
  1391. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1392. ac->ac_status = AC_STATUS_BREAK;
  1393. return;
  1394. }
  1395. /*
  1396. * Haven't found good chunk so far, let's continue
  1397. */
  1398. if (bex->fe_len < gex->fe_len)
  1399. return;
  1400. if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
  1401. && bex->fe_group == e4b->bd_group) {
  1402. /* recheck chunk's availability - we don't know
  1403. * when it was found (within this lock-unlock
  1404. * period or not) */
  1405. max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
  1406. if (max >= gex->fe_len) {
  1407. ext4_mb_use_best_found(ac, e4b);
  1408. return;
  1409. }
  1410. }
  1411. }
  1412. /*
  1413. * The routine checks whether found extent is good enough. If it is,
  1414. * then the extent gets marked used and flag is set to the context
  1415. * to stop scanning. Otherwise, the extent is compared with the
  1416. * previous found extent and if new one is better, then it's stored
  1417. * in the context. Later, the best found extent will be used, if
  1418. * mballoc can't find good enough extent.
  1419. *
  1420. * FIXME: real allocation policy is to be designed yet!
  1421. */
  1422. static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
  1423. struct ext4_free_extent *ex,
  1424. struct ext4_buddy *e4b)
  1425. {
  1426. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1427. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1428. BUG_ON(ex->fe_len <= 0);
  1429. BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1430. BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1431. BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
  1432. ac->ac_found++;
  1433. /*
  1434. * The special case - take what you catch first
  1435. */
  1436. if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1437. *bex = *ex;
  1438. ext4_mb_use_best_found(ac, e4b);
  1439. return;
  1440. }
  1441. /*
  1442. * Let's check whether the chuck is good enough
  1443. */
  1444. if (ex->fe_len == gex->fe_len) {
  1445. *bex = *ex;
  1446. ext4_mb_use_best_found(ac, e4b);
  1447. return;
  1448. }
  1449. /*
  1450. * If this is first found extent, just store it in the context
  1451. */
  1452. if (bex->fe_len == 0) {
  1453. *bex = *ex;
  1454. return;
  1455. }
  1456. /*
  1457. * If new found extent is better, store it in the context
  1458. */
  1459. if (bex->fe_len < gex->fe_len) {
  1460. /* if the request isn't satisfied, any found extent
  1461. * larger than previous best one is better */
  1462. if (ex->fe_len > bex->fe_len)
  1463. *bex = *ex;
  1464. } else if (ex->fe_len > gex->fe_len) {
  1465. /* if the request is satisfied, then we try to find
  1466. * an extent that still satisfy the request, but is
  1467. * smaller than previous one */
  1468. if (ex->fe_len < bex->fe_len)
  1469. *bex = *ex;
  1470. }
  1471. ext4_mb_check_limits(ac, e4b, 0);
  1472. }
  1473. static noinline_for_stack
  1474. int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
  1475. struct ext4_buddy *e4b)
  1476. {
  1477. struct ext4_free_extent ex = ac->ac_b_ex;
  1478. ext4_group_t group = ex.fe_group;
  1479. int max;
  1480. int err;
  1481. BUG_ON(ex.fe_len <= 0);
  1482. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1483. if (err)
  1484. return err;
  1485. ext4_lock_group(ac->ac_sb, group);
  1486. max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
  1487. if (max > 0) {
  1488. ac->ac_b_ex = ex;
  1489. ext4_mb_use_best_found(ac, e4b);
  1490. }
  1491. ext4_unlock_group(ac->ac_sb, group);
  1492. ext4_mb_unload_buddy(e4b);
  1493. return 0;
  1494. }
  1495. static noinline_for_stack
  1496. int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
  1497. struct ext4_buddy *e4b)
  1498. {
  1499. ext4_group_t group = ac->ac_g_ex.fe_group;
  1500. int max;
  1501. int err;
  1502. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1503. struct ext4_free_extent ex;
  1504. if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
  1505. return 0;
  1506. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1507. if (err)
  1508. return err;
  1509. ext4_lock_group(ac->ac_sb, group);
  1510. max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
  1511. ac->ac_g_ex.fe_len, &ex);
  1512. if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
  1513. ext4_fsblk_t start;
  1514. start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
  1515. ex.fe_start;
  1516. /* use do_div to get remainder (would be 64-bit modulo) */
  1517. if (do_div(start, sbi->s_stripe) == 0) {
  1518. ac->ac_found++;
  1519. ac->ac_b_ex = ex;
  1520. ext4_mb_use_best_found(ac, e4b);
  1521. }
  1522. } else if (max >= ac->ac_g_ex.fe_len) {
  1523. BUG_ON(ex.fe_len <= 0);
  1524. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1525. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1526. ac->ac_found++;
  1527. ac->ac_b_ex = ex;
  1528. ext4_mb_use_best_found(ac, e4b);
  1529. } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
  1530. /* Sometimes, caller may want to merge even small
  1531. * number of blocks to an existing extent */
  1532. BUG_ON(ex.fe_len <= 0);
  1533. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1534. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1535. ac->ac_found++;
  1536. ac->ac_b_ex = ex;
  1537. ext4_mb_use_best_found(ac, e4b);
  1538. }
  1539. ext4_unlock_group(ac->ac_sb, group);
  1540. ext4_mb_unload_buddy(e4b);
  1541. return 0;
  1542. }
  1543. /*
  1544. * The routine scans buddy structures (not bitmap!) from given order
  1545. * to max order and tries to find big enough chunk to satisfy the req
  1546. */
  1547. static noinline_for_stack
  1548. void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
  1549. struct ext4_buddy *e4b)
  1550. {
  1551. struct super_block *sb = ac->ac_sb;
  1552. struct ext4_group_info *grp = e4b->bd_info;
  1553. void *buddy;
  1554. int i;
  1555. int k;
  1556. int max;
  1557. BUG_ON(ac->ac_2order <= 0);
  1558. for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
  1559. if (grp->bb_counters[i] == 0)
  1560. continue;
  1561. buddy = mb_find_buddy(e4b, i, &max);
  1562. BUG_ON(buddy == NULL);
  1563. k = mb_find_next_zero_bit(buddy, max, 0);
  1564. BUG_ON(k >= max);
  1565. ac->ac_found++;
  1566. ac->ac_b_ex.fe_len = 1 << i;
  1567. ac->ac_b_ex.fe_start = k << i;
  1568. ac->ac_b_ex.fe_group = e4b->bd_group;
  1569. ext4_mb_use_best_found(ac, e4b);
  1570. BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
  1571. if (EXT4_SB(sb)->s_mb_stats)
  1572. atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
  1573. break;
  1574. }
  1575. }
  1576. /*
  1577. * The routine scans the group and measures all found extents.
  1578. * In order to optimize scanning, caller must pass number of
  1579. * free blocks in the group, so the routine can know upper limit.
  1580. */
  1581. static noinline_for_stack
  1582. void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
  1583. struct ext4_buddy *e4b)
  1584. {
  1585. struct super_block *sb = ac->ac_sb;
  1586. void *bitmap = EXT4_MB_BITMAP(e4b);
  1587. struct ext4_free_extent ex;
  1588. int i;
  1589. int free;
  1590. free = e4b->bd_info->bb_free;
  1591. BUG_ON(free <= 0);
  1592. i = e4b->bd_info->bb_first_free;
  1593. while (free && ac->ac_status == AC_STATUS_CONTINUE) {
  1594. i = mb_find_next_zero_bit(bitmap,
  1595. EXT4_BLOCKS_PER_GROUP(sb), i);
  1596. if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
  1597. /*
  1598. * IF we have corrupt bitmap, we won't find any
  1599. * free blocks even though group info says we
  1600. * we have free blocks
  1601. */
  1602. ext4_grp_locked_error(sb, e4b->bd_group,
  1603. __func__, "%d free blocks as per "
  1604. "group info. But bitmap says 0",
  1605. free);
  1606. break;
  1607. }
  1608. mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
  1609. BUG_ON(ex.fe_len <= 0);
  1610. if (free < ex.fe_len) {
  1611. ext4_grp_locked_error(sb, e4b->bd_group,
  1612. __func__, "%d free blocks as per "
  1613. "group info. But got %d blocks",
  1614. free, ex.fe_len);
  1615. /*
  1616. * The number of free blocks differs. This mostly
  1617. * indicate that the bitmap is corrupt. So exit
  1618. * without claiming the space.
  1619. */
  1620. break;
  1621. }
  1622. ext4_mb_measure_extent(ac, &ex, e4b);
  1623. i += ex.fe_len;
  1624. free -= ex.fe_len;
  1625. }
  1626. ext4_mb_check_limits(ac, e4b, 1);
  1627. }
  1628. /*
  1629. * This is a special case for storages like raid5
  1630. * we try to find stripe-aligned chunks for stripe-size requests
  1631. * XXX should do so at least for multiples of stripe size as well
  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. int bsbits;
  1787. struct ext4_sb_info *sbi;
  1788. struct super_block *sb;
  1789. struct ext4_buddy e4b;
  1790. sb = ac->ac_sb;
  1791. sbi = EXT4_SB(sb);
  1792. ngroups = ext4_get_groups_count(sb);
  1793. /* non-extent files are limited to low blocks/groups */
  1794. if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
  1795. ngroups = sbi->s_blockfile_groups;
  1796. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1797. /* first, try the goal */
  1798. err = ext4_mb_find_by_goal(ac, &e4b);
  1799. if (err || ac->ac_status == AC_STATUS_FOUND)
  1800. goto out;
  1801. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  1802. goto out;
  1803. /*
  1804. * ac->ac2_order is set only if the fe_len is a power of 2
  1805. * if ac2_order is set we also set criteria to 0 so that we
  1806. * try exact allocation using buddy.
  1807. */
  1808. i = fls(ac->ac_g_ex.fe_len);
  1809. ac->ac_2order = 0;
  1810. /*
  1811. * We search using buddy data only if the order of the request
  1812. * is greater than equal to the sbi_s_mb_order2_reqs
  1813. * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
  1814. */
  1815. if (i >= sbi->s_mb_order2_reqs) {
  1816. /*
  1817. * This should tell if fe_len is exactly power of 2
  1818. */
  1819. if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
  1820. ac->ac_2order = i - 1;
  1821. }
  1822. bsbits = ac->ac_sb->s_blocksize_bits;
  1823. /* if stream allocation is enabled, use global goal */
  1824. if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
  1825. /* TBD: may be hot point */
  1826. spin_lock(&sbi->s_md_lock);
  1827. ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
  1828. ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
  1829. spin_unlock(&sbi->s_md_lock);
  1830. }
  1831. /* Let's just scan groups to find more-less suitable blocks */
  1832. cr = ac->ac_2order ? 0 : 1;
  1833. /*
  1834. * cr == 0 try to get exact allocation,
  1835. * cr == 3 try to get anything
  1836. */
  1837. repeat:
  1838. for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
  1839. ac->ac_criteria = cr;
  1840. /*
  1841. * searching for the right group start
  1842. * from the goal value specified
  1843. */
  1844. group = ac->ac_g_ex.fe_group;
  1845. for (i = 0; i < ngroups; group++, i++) {
  1846. if (group == ngroups)
  1847. group = 0;
  1848. /* This now checks without needing the buddy page */
  1849. if (!ext4_mb_good_group(ac, group, cr))
  1850. continue;
  1851. err = ext4_mb_load_buddy(sb, group, &e4b);
  1852. if (err)
  1853. goto out;
  1854. ext4_lock_group(sb, group);
  1855. /*
  1856. * We need to check again after locking the
  1857. * block group
  1858. */
  1859. if (!ext4_mb_good_group(ac, group, cr)) {
  1860. ext4_unlock_group(sb, group);
  1861. ext4_mb_unload_buddy(&e4b);
  1862. continue;
  1863. }
  1864. ac->ac_groups_scanned++;
  1865. if (cr == 0)
  1866. ext4_mb_simple_scan_group(ac, &e4b);
  1867. else if (cr == 1 &&
  1868. ac->ac_g_ex.fe_len == sbi->s_stripe)
  1869. ext4_mb_scan_aligned(ac, &e4b);
  1870. else
  1871. ext4_mb_complex_scan_group(ac, &e4b);
  1872. ext4_unlock_group(sb, group);
  1873. ext4_mb_unload_buddy(&e4b);
  1874. if (ac->ac_status != AC_STATUS_CONTINUE)
  1875. break;
  1876. }
  1877. }
  1878. if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
  1879. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1880. /*
  1881. * We've been searching too long. Let's try to allocate
  1882. * the best chunk we've found so far
  1883. */
  1884. ext4_mb_try_best_found(ac, &e4b);
  1885. if (ac->ac_status != AC_STATUS_FOUND) {
  1886. /*
  1887. * Someone more lucky has already allocated it.
  1888. * The only thing we can do is just take first
  1889. * found block(s)
  1890. printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
  1891. */
  1892. ac->ac_b_ex.fe_group = 0;
  1893. ac->ac_b_ex.fe_start = 0;
  1894. ac->ac_b_ex.fe_len = 0;
  1895. ac->ac_status = AC_STATUS_CONTINUE;
  1896. ac->ac_flags |= EXT4_MB_HINT_FIRST;
  1897. cr = 3;
  1898. atomic_inc(&sbi->s_mb_lost_chunks);
  1899. goto repeat;
  1900. }
  1901. }
  1902. out:
  1903. return err;
  1904. }
  1905. static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
  1906. {
  1907. struct super_block *sb = seq->private;
  1908. ext4_group_t group;
  1909. if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
  1910. return NULL;
  1911. group = *pos + 1;
  1912. return (void *) ((unsigned long) group);
  1913. }
  1914. static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
  1915. {
  1916. struct super_block *sb = seq->private;
  1917. ext4_group_t group;
  1918. ++*pos;
  1919. if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
  1920. return NULL;
  1921. group = *pos + 1;
  1922. return (void *) ((unsigned long) group);
  1923. }
  1924. static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
  1925. {
  1926. struct super_block *sb = seq->private;
  1927. ext4_group_t group = (ext4_group_t) ((unsigned long) v);
  1928. int i;
  1929. int err;
  1930. struct ext4_buddy e4b;
  1931. struct sg {
  1932. struct ext4_group_info info;
  1933. ext4_grpblk_t counters[16];
  1934. } sg;
  1935. group--;
  1936. if (group == 0)
  1937. seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
  1938. "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
  1939. "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
  1940. "group", "free", "frags", "first",
  1941. "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
  1942. "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
  1943. i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
  1944. sizeof(struct ext4_group_info);
  1945. err = ext4_mb_load_buddy(sb, group, &e4b);
  1946. if (err) {
  1947. seq_printf(seq, "#%-5u: I/O error\n", group);
  1948. return 0;
  1949. }
  1950. ext4_lock_group(sb, group);
  1951. memcpy(&sg, ext4_get_group_info(sb, group), i);
  1952. ext4_unlock_group(sb, group);
  1953. ext4_mb_unload_buddy(&e4b);
  1954. seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
  1955. sg.info.bb_fragments, sg.info.bb_first_free);
  1956. for (i = 0; i <= 13; i++)
  1957. seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
  1958. sg.info.bb_counters[i] : 0);
  1959. seq_printf(seq, " ]\n");
  1960. return 0;
  1961. }
  1962. static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
  1963. {
  1964. }
  1965. static const struct seq_operations ext4_mb_seq_groups_ops = {
  1966. .start = ext4_mb_seq_groups_start,
  1967. .next = ext4_mb_seq_groups_next,
  1968. .stop = ext4_mb_seq_groups_stop,
  1969. .show = ext4_mb_seq_groups_show,
  1970. };
  1971. static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
  1972. {
  1973. struct super_block *sb = PDE(inode)->data;
  1974. int rc;
  1975. rc = seq_open(file, &ext4_mb_seq_groups_ops);
  1976. if (rc == 0) {
  1977. struct seq_file *m = (struct seq_file *)file->private_data;
  1978. m->private = sb;
  1979. }
  1980. return rc;
  1981. }
  1982. static const struct file_operations ext4_mb_seq_groups_fops = {
  1983. .owner = THIS_MODULE,
  1984. .open = ext4_mb_seq_groups_open,
  1985. .read = seq_read,
  1986. .llseek = seq_lseek,
  1987. .release = seq_release,
  1988. };
  1989. /* Create and initialize ext4_group_info data for the given group. */
  1990. int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
  1991. struct ext4_group_desc *desc)
  1992. {
  1993. int i, len;
  1994. int metalen = 0;
  1995. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1996. struct ext4_group_info **meta_group_info;
  1997. /*
  1998. * First check if this group is the first of a reserved block.
  1999. * If it's true, we have to allocate a new table of pointers
  2000. * to ext4_group_info structures
  2001. */
  2002. if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
  2003. metalen = sizeof(*meta_group_info) <<
  2004. EXT4_DESC_PER_BLOCK_BITS(sb);
  2005. meta_group_info = kmalloc(metalen, GFP_KERNEL);
  2006. if (meta_group_info == NULL) {
  2007. printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
  2008. "buddy group\n");
  2009. goto exit_meta_group_info;
  2010. }
  2011. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
  2012. meta_group_info;
  2013. }
  2014. /*
  2015. * calculate needed size. if change bb_counters size,
  2016. * don't forget about ext4_mb_generate_buddy()
  2017. */
  2018. len = offsetof(typeof(**meta_group_info),
  2019. bb_counters[sb->s_blocksize_bits + 2]);
  2020. meta_group_info =
  2021. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
  2022. i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
  2023. meta_group_info[i] = kzalloc(len, GFP_KERNEL);
  2024. if (meta_group_info[i] == NULL) {
  2025. printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
  2026. goto exit_group_info;
  2027. }
  2028. set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
  2029. &(meta_group_info[i]->bb_state));
  2030. /*
  2031. * initialize bb_free to be able to skip
  2032. * empty groups without initialization
  2033. */
  2034. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2035. meta_group_info[i]->bb_free =
  2036. ext4_free_blocks_after_init(sb, group, desc);
  2037. } else {
  2038. meta_group_info[i]->bb_free =
  2039. ext4_free_blks_count(sb, desc);
  2040. }
  2041. INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
  2042. init_rwsem(&meta_group_info[i]->alloc_sem);
  2043. meta_group_info[i]->bb_free_root = RB_ROOT;
  2044. meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
  2045. #ifdef DOUBLE_CHECK
  2046. {
  2047. struct buffer_head *bh;
  2048. meta_group_info[i]->bb_bitmap =
  2049. kmalloc(sb->s_blocksize, GFP_KERNEL);
  2050. BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
  2051. bh = ext4_read_block_bitmap(sb, group);
  2052. BUG_ON(bh == NULL);
  2053. memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
  2054. sb->s_blocksize);
  2055. put_bh(bh);
  2056. }
  2057. #endif
  2058. return 0;
  2059. exit_group_info:
  2060. /* If a meta_group_info table has been allocated, release it now */
  2061. if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
  2062. kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
  2063. exit_meta_group_info:
  2064. return -ENOMEM;
  2065. } /* ext4_mb_add_groupinfo */
  2066. static int ext4_mb_init_backend(struct super_block *sb)
  2067. {
  2068. ext4_group_t ngroups = ext4_get_groups_count(sb);
  2069. ext4_group_t i;
  2070. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2071. struct ext4_super_block *es = sbi->s_es;
  2072. int num_meta_group_infos;
  2073. int num_meta_group_infos_max;
  2074. int array_size;
  2075. struct ext4_group_desc *desc;
  2076. /* This is the number of blocks used by GDT */
  2077. num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
  2078. 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
  2079. /*
  2080. * This is the total number of blocks used by GDT including
  2081. * the number of reserved blocks for GDT.
  2082. * The s_group_info array is allocated with this value
  2083. * to allow a clean online resize without a complex
  2084. * manipulation of pointer.
  2085. * The drawback is the unused memory when no resize
  2086. * occurs but it's very low in terms of pages
  2087. * (see comments below)
  2088. * Need to handle this properly when META_BG resizing is allowed
  2089. */
  2090. num_meta_group_infos_max = num_meta_group_infos +
  2091. le16_to_cpu(es->s_reserved_gdt_blocks);
  2092. /*
  2093. * array_size is the size of s_group_info array. We round it
  2094. * to the next power of two because this approximation is done
  2095. * internally by kmalloc so we can have some more memory
  2096. * for free here (e.g. may be used for META_BG resize).
  2097. */
  2098. array_size = 1;
  2099. while (array_size < sizeof(*sbi->s_group_info) *
  2100. num_meta_group_infos_max)
  2101. array_size = array_size << 1;
  2102. /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
  2103. * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
  2104. * So a two level scheme suffices for now. */
  2105. sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
  2106. if (sbi->s_group_info == NULL) {
  2107. printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
  2108. return -ENOMEM;
  2109. }
  2110. sbi->s_buddy_cache = new_inode(sb);
  2111. if (sbi->s_buddy_cache == NULL) {
  2112. printk(KERN_ERR "EXT4-fs: can't get new inode\n");
  2113. goto err_freesgi;
  2114. }
  2115. EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
  2116. for (i = 0; i < ngroups; i++) {
  2117. desc = ext4_get_group_desc(sb, i, NULL);
  2118. if (desc == NULL) {
  2119. printk(KERN_ERR
  2120. "EXT4-fs: can't read descriptor %u\n", i);
  2121. goto err_freebuddy;
  2122. }
  2123. if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
  2124. goto err_freebuddy;
  2125. }
  2126. return 0;
  2127. err_freebuddy:
  2128. while (i-- > 0)
  2129. kfree(ext4_get_group_info(sb, i));
  2130. i = num_meta_group_infos;
  2131. while (i-- > 0)
  2132. kfree(sbi->s_group_info[i]);
  2133. iput(sbi->s_buddy_cache);
  2134. err_freesgi:
  2135. kfree(sbi->s_group_info);
  2136. return -ENOMEM;
  2137. }
  2138. int ext4_mb_init(struct super_block *sb, int needs_recovery)
  2139. {
  2140. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2141. unsigned i, j;
  2142. unsigned offset;
  2143. unsigned max;
  2144. int ret;
  2145. i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
  2146. sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
  2147. if (sbi->s_mb_offsets == NULL) {
  2148. return -ENOMEM;
  2149. }
  2150. i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
  2151. sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
  2152. if (sbi->s_mb_maxs == NULL) {
  2153. kfree(sbi->s_mb_offsets);
  2154. return -ENOMEM;
  2155. }
  2156. /* order 0 is regular bitmap */
  2157. sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
  2158. sbi->s_mb_offsets[0] = 0;
  2159. i = 1;
  2160. offset = 0;
  2161. max = sb->s_blocksize << 2;
  2162. do {
  2163. sbi->s_mb_offsets[i] = offset;
  2164. sbi->s_mb_maxs[i] = max;
  2165. offset += 1 << (sb->s_blocksize_bits - i);
  2166. max = max >> 1;
  2167. i++;
  2168. } while (i <= sb->s_blocksize_bits + 1);
  2169. /* init file for buddy data */
  2170. ret = ext4_mb_init_backend(sb);
  2171. if (ret != 0) {
  2172. kfree(sbi->s_mb_offsets);
  2173. kfree(sbi->s_mb_maxs);
  2174. return ret;
  2175. }
  2176. spin_lock_init(&sbi->s_md_lock);
  2177. spin_lock_init(&sbi->s_bal_lock);
  2178. sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
  2179. sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
  2180. sbi->s_mb_stats = MB_DEFAULT_STATS;
  2181. sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
  2182. sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
  2183. sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
  2184. sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
  2185. if (sbi->s_locality_groups == NULL) {
  2186. kfree(sbi->s_mb_offsets);
  2187. kfree(sbi->s_mb_maxs);
  2188. return -ENOMEM;
  2189. }
  2190. for_each_possible_cpu(i) {
  2191. struct ext4_locality_group *lg;
  2192. lg = per_cpu_ptr(sbi->s_locality_groups, i);
  2193. mutex_init(&lg->lg_mutex);
  2194. for (j = 0; j < PREALLOC_TB_SIZE; j++)
  2195. INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
  2196. spin_lock_init(&lg->lg_prealloc_lock);
  2197. }
  2198. if (sbi->s_proc)
  2199. proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
  2200. &ext4_mb_seq_groups_fops, sb);
  2201. if (sbi->s_journal)
  2202. sbi->s_journal->j_commit_callback = release_blocks_on_commit;
  2203. return 0;
  2204. }
  2205. /* need to called with the ext4 group lock held */
  2206. static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
  2207. {
  2208. struct ext4_prealloc_space *pa;
  2209. struct list_head *cur, *tmp;
  2210. int count = 0;
  2211. list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
  2212. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  2213. list_del(&pa->pa_group_list);
  2214. count++;
  2215. kmem_cache_free(ext4_pspace_cachep, pa);
  2216. }
  2217. if (count)
  2218. mb_debug(1, "mballoc: %u PAs left\n", count);
  2219. }
  2220. int ext4_mb_release(struct super_block *sb)
  2221. {
  2222. ext4_group_t ngroups = ext4_get_groups_count(sb);
  2223. ext4_group_t i;
  2224. int num_meta_group_infos;
  2225. struct ext4_group_info *grinfo;
  2226. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2227. if (sbi->s_group_info) {
  2228. for (i = 0; i < ngroups; i++) {
  2229. grinfo = ext4_get_group_info(sb, i);
  2230. #ifdef DOUBLE_CHECK
  2231. kfree(grinfo->bb_bitmap);
  2232. #endif
  2233. ext4_lock_group(sb, i);
  2234. ext4_mb_cleanup_pa(grinfo);
  2235. ext4_unlock_group(sb, i);
  2236. kfree(grinfo);
  2237. }
  2238. num_meta_group_infos = (ngroups +
  2239. EXT4_DESC_PER_BLOCK(sb) - 1) >>
  2240. EXT4_DESC_PER_BLOCK_BITS(sb);
  2241. for (i = 0; i < num_meta_group_infos; i++)
  2242. kfree(sbi->s_group_info[i]);
  2243. kfree(sbi->s_group_info);
  2244. }
  2245. kfree(sbi->s_mb_offsets);
  2246. kfree(sbi->s_mb_maxs);
  2247. if (sbi->s_buddy_cache)
  2248. iput(sbi->s_buddy_cache);
  2249. if (sbi->s_mb_stats) {
  2250. printk(KERN_INFO
  2251. "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
  2252. atomic_read(&sbi->s_bal_allocated),
  2253. atomic_read(&sbi->s_bal_reqs),
  2254. atomic_read(&sbi->s_bal_success));
  2255. printk(KERN_INFO
  2256. "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
  2257. "%u 2^N hits, %u breaks, %u lost\n",
  2258. atomic_read(&sbi->s_bal_ex_scanned),
  2259. atomic_read(&sbi->s_bal_goals),
  2260. atomic_read(&sbi->s_bal_2orders),
  2261. atomic_read(&sbi->s_bal_breaks),
  2262. atomic_read(&sbi->s_mb_lost_chunks));
  2263. printk(KERN_INFO
  2264. "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
  2265. sbi->s_mb_buddies_generated++,
  2266. sbi->s_mb_generation_time);
  2267. printk(KERN_INFO
  2268. "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
  2269. atomic_read(&sbi->s_mb_preallocated),
  2270. atomic_read(&sbi->s_mb_discarded));
  2271. }
  2272. free_percpu(sbi->s_locality_groups);
  2273. if (sbi->s_proc)
  2274. remove_proc_entry("mb_groups", sbi->s_proc);
  2275. return 0;
  2276. }
  2277. /*
  2278. * This function is called by the jbd2 layer once the commit has finished,
  2279. * so we know we can free the blocks that were released with that commit.
  2280. */
  2281. static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
  2282. {
  2283. struct super_block *sb = journal->j_private;
  2284. struct ext4_buddy e4b;
  2285. struct ext4_group_info *db;
  2286. int err, count = 0, count2 = 0;
  2287. struct ext4_free_data *entry;
  2288. struct list_head *l, *ltmp;
  2289. list_for_each_safe(l, ltmp, &txn->t_private_list) {
  2290. entry = list_entry(l, struct ext4_free_data, list);
  2291. mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
  2292. entry->count, entry->group, entry);
  2293. if (test_opt(sb, DISCARD)) {
  2294. int ret;
  2295. ext4_fsblk_t discard_block;
  2296. discard_block = entry->start_blk +
  2297. ext4_group_first_block_no(sb, entry->group);
  2298. trace_ext4_discard_blocks(sb,
  2299. (unsigned long long)discard_block,
  2300. entry->count);
  2301. ret = sb_issue_discard(sb, discard_block, entry->count);
  2302. if (ret == EOPNOTSUPP) {
  2303. ext4_warning(sb,
  2304. "discard not supported, disabling");
  2305. clear_opt(EXT4_SB(sb)->s_mount_opt, DISCARD);
  2306. }
  2307. }
  2308. err = ext4_mb_load_buddy(sb, entry->group, &e4b);
  2309. /* we expect to find existing buddy because it's pinned */
  2310. BUG_ON(err != 0);
  2311. db = e4b.bd_info;
  2312. /* there are blocks to put in buddy to make them really free */
  2313. count += entry->count;
  2314. count2++;
  2315. ext4_lock_group(sb, entry->group);
  2316. /* Take it out of per group rb tree */
  2317. rb_erase(&entry->node, &(db->bb_free_root));
  2318. mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
  2319. if (!db->bb_free_root.rb_node) {
  2320. /* No more items in the per group rb tree
  2321. * balance refcounts from ext4_mb_free_metadata()
  2322. */
  2323. page_cache_release(e4b.bd_buddy_page);
  2324. page_cache_release(e4b.bd_bitmap_page);
  2325. }
  2326. ext4_unlock_group(sb, entry->group);
  2327. kmem_cache_free(ext4_free_ext_cachep, entry);
  2328. ext4_mb_unload_buddy(&e4b);
  2329. }
  2330. mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
  2331. }
  2332. #ifdef CONFIG_EXT4_DEBUG
  2333. u8 mb_enable_debug __read_mostly;
  2334. static struct dentry *debugfs_dir;
  2335. static struct dentry *debugfs_debug;
  2336. static void __init ext4_create_debugfs_entry(void)
  2337. {
  2338. debugfs_dir = debugfs_create_dir("ext4", NULL);
  2339. if (debugfs_dir)
  2340. debugfs_debug = debugfs_create_u8("mballoc-debug",
  2341. S_IRUGO | S_IWUSR,
  2342. debugfs_dir,
  2343. &mb_enable_debug);
  2344. }
  2345. static void ext4_remove_debugfs_entry(void)
  2346. {
  2347. debugfs_remove(debugfs_debug);
  2348. debugfs_remove(debugfs_dir);
  2349. }
  2350. #else
  2351. static void __init ext4_create_debugfs_entry(void)
  2352. {
  2353. }
  2354. static void ext4_remove_debugfs_entry(void)
  2355. {
  2356. }
  2357. #endif
  2358. int __init init_ext4_mballoc(void)
  2359. {
  2360. ext4_pspace_cachep =
  2361. kmem_cache_create("ext4_prealloc_space",
  2362. sizeof(struct ext4_prealloc_space),
  2363. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2364. if (ext4_pspace_cachep == NULL)
  2365. return -ENOMEM;
  2366. ext4_ac_cachep =
  2367. kmem_cache_create("ext4_alloc_context",
  2368. sizeof(struct ext4_allocation_context),
  2369. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2370. if (ext4_ac_cachep == NULL) {
  2371. kmem_cache_destroy(ext4_pspace_cachep);
  2372. return -ENOMEM;
  2373. }
  2374. ext4_free_ext_cachep =
  2375. kmem_cache_create("ext4_free_block_extents",
  2376. sizeof(struct ext4_free_data),
  2377. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2378. if (ext4_free_ext_cachep == NULL) {
  2379. kmem_cache_destroy(ext4_pspace_cachep);
  2380. kmem_cache_destroy(ext4_ac_cachep);
  2381. return -ENOMEM;
  2382. }
  2383. ext4_create_debugfs_entry();
  2384. return 0;
  2385. }
  2386. void exit_ext4_mballoc(void)
  2387. {
  2388. /*
  2389. * Wait for completion of call_rcu()'s on ext4_pspace_cachep
  2390. * before destroying the slab cache.
  2391. */
  2392. rcu_barrier();
  2393. kmem_cache_destroy(ext4_pspace_cachep);
  2394. kmem_cache_destroy(ext4_ac_cachep);
  2395. kmem_cache_destroy(ext4_free_ext_cachep);
  2396. ext4_remove_debugfs_entry();
  2397. }
  2398. /*
  2399. * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
  2400. * Returns 0 if success or error code
  2401. */
  2402. static noinline_for_stack int
  2403. ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
  2404. handle_t *handle, unsigned int reserv_blks)
  2405. {
  2406. struct buffer_head *bitmap_bh = NULL;
  2407. struct ext4_super_block *es;
  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. es = sbi->s_es;
  2419. err = -EIO;
  2420. bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
  2421. if (!bitmap_bh)
  2422. goto out_err;
  2423. err = ext4_journal_get_write_access(handle, bitmap_bh);
  2424. if (err)
  2425. goto out_err;
  2426. err = -EIO;
  2427. gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
  2428. if (!gdp)
  2429. goto out_err;
  2430. ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
  2431. ext4_free_blks_count(sb, gdp));
  2432. err = ext4_journal_get_write_access(handle, gdp_bh);
  2433. if (err)
  2434. goto out_err;
  2435. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  2436. len = ac->ac_b_ex.fe_len;
  2437. if (!ext4_data_block_valid(sbi, block, len)) {
  2438. ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
  2439. "fs metadata\n", block, block+len);
  2440. /* File system mounted not to panic on error
  2441. * Fix the bitmap and repeat the block allocation
  2442. * We leak some of the blocks here.
  2443. */
  2444. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2445. mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
  2446. ac->ac_b_ex.fe_len);
  2447. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2448. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2449. if (!err)
  2450. err = -EAGAIN;
  2451. goto out_err;
  2452. }
  2453. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2454. #ifdef AGGRESSIVE_CHECK
  2455. {
  2456. int i;
  2457. for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
  2458. BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
  2459. bitmap_bh->b_data));
  2460. }
  2461. }
  2462. #endif
  2463. mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,ac->ac_b_ex.fe_len);
  2464. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2465. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  2466. ext4_free_blks_set(sb, gdp,
  2467. ext4_free_blocks_after_init(sb,
  2468. ac->ac_b_ex.fe_group, gdp));
  2469. }
  2470. len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
  2471. ext4_free_blks_set(sb, gdp, len);
  2472. gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
  2473. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2474. percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
  2475. /*
  2476. * Now reduce the dirty block count also. Should not go negative
  2477. */
  2478. if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
  2479. /* release all the reserved blocks if non delalloc */
  2480. percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
  2481. if (sbi->s_log_groups_per_flex) {
  2482. ext4_group_t flex_group = ext4_flex_group(sbi,
  2483. ac->ac_b_ex.fe_group);
  2484. atomic_sub(ac->ac_b_ex.fe_len,
  2485. &sbi->s_flex_groups[flex_group].free_blocks);
  2486. }
  2487. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2488. if (err)
  2489. goto out_err;
  2490. err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
  2491. out_err:
  2492. sb->s_dirt = 1;
  2493. brelse(bitmap_bh);
  2494. return err;
  2495. }
  2496. /*
  2497. * here we normalize request for locality group
  2498. * Group request are normalized to s_strip size if we set the same via mount
  2499. * option. If not we set it to s_mb_group_prealloc which can be configured via
  2500. * /sys/fs/ext4/<partition>/mb_group_prealloc
  2501. *
  2502. * XXX: should we try to preallocate more than the group has now?
  2503. */
  2504. static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
  2505. {
  2506. struct super_block *sb = ac->ac_sb;
  2507. struct ext4_locality_group *lg = ac->ac_lg;
  2508. BUG_ON(lg == NULL);
  2509. if (EXT4_SB(sb)->s_stripe)
  2510. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
  2511. else
  2512. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
  2513. mb_debug(1, "#%u: goal %u blocks for locality group\n",
  2514. current->pid, ac->ac_g_ex.fe_len);
  2515. }
  2516. /*
  2517. * Normalization means making request better in terms of
  2518. * size and alignment
  2519. */
  2520. static noinline_for_stack void
  2521. ext4_mb_normalize_request(struct ext4_allocation_context *ac,
  2522. struct ext4_allocation_request *ar)
  2523. {
  2524. int bsbits, max;
  2525. ext4_lblk_t end;
  2526. loff_t size, orig_size, start_off;
  2527. ext4_lblk_t start, orig_start;
  2528. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2529. struct ext4_prealloc_space *pa;
  2530. /* do normalize only data requests, metadata requests
  2531. do not need preallocation */
  2532. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2533. return;
  2534. /* sometime caller may want exact blocks */
  2535. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  2536. return;
  2537. /* caller may indicate that preallocation isn't
  2538. * required (it's a tail, for example) */
  2539. if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
  2540. return;
  2541. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
  2542. ext4_mb_normalize_group_request(ac);
  2543. return ;
  2544. }
  2545. bsbits = ac->ac_sb->s_blocksize_bits;
  2546. /* first, let's learn actual file size
  2547. * given current request is allocated */
  2548. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  2549. size = size << bsbits;
  2550. if (size < i_size_read(ac->ac_inode))
  2551. size = i_size_read(ac->ac_inode);
  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. orig_size = size = size >> bsbits;
  2591. orig_start = 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. sector_t start;
  3127. int err = 0;
  3128. int free = 0;
  3129. BUG_ON(pa->pa_deleted == 0);
  3130. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3131. grp_blk_start = pa->pa_pstart - bit;
  3132. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3133. end = bit + pa->pa_len;
  3134. if (ac) {
  3135. ac->ac_sb = sb;
  3136. ac->ac_inode = pa->pa_inode;
  3137. }
  3138. while (bit < end) {
  3139. bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
  3140. if (bit >= end)
  3141. break;
  3142. next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
  3143. start = ext4_group_first_block_no(sb, group) + bit;
  3144. mb_debug(1, " free preallocated %u/%u in group %u\n",
  3145. (unsigned) start, (unsigned) next - bit,
  3146. (unsigned) group);
  3147. free += next - bit;
  3148. if (ac) {
  3149. ac->ac_b_ex.fe_group = group;
  3150. ac->ac_b_ex.fe_start = bit;
  3151. ac->ac_b_ex.fe_len = next - bit;
  3152. ac->ac_b_ex.fe_logical = 0;
  3153. trace_ext4_mballoc_discard(ac);
  3154. }
  3155. trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit,
  3156. next - bit);
  3157. mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
  3158. bit = next + 1;
  3159. }
  3160. if (free != pa->pa_free) {
  3161. printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
  3162. pa, (unsigned long) pa->pa_lstart,
  3163. (unsigned long) pa->pa_pstart,
  3164. (unsigned long) pa->pa_len);
  3165. ext4_grp_locked_error(sb, group,
  3166. __func__, "free %u, pa_free %u",
  3167. free, pa->pa_free);
  3168. /*
  3169. * pa is already deleted so we use the value obtained
  3170. * from the bitmap and continue.
  3171. */
  3172. }
  3173. atomic_add(free, &sbi->s_mb_discarded);
  3174. return err;
  3175. }
  3176. static noinline_for_stack int
  3177. ext4_mb_release_group_pa(struct ext4_buddy *e4b,
  3178. struct ext4_prealloc_space *pa,
  3179. struct ext4_allocation_context *ac)
  3180. {
  3181. struct super_block *sb = e4b->bd_sb;
  3182. ext4_group_t group;
  3183. ext4_grpblk_t bit;
  3184. trace_ext4_mb_release_group_pa(ac, pa);
  3185. BUG_ON(pa->pa_deleted == 0);
  3186. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3187. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3188. mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
  3189. atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
  3190. if (ac) {
  3191. ac->ac_sb = sb;
  3192. ac->ac_inode = NULL;
  3193. ac->ac_b_ex.fe_group = group;
  3194. ac->ac_b_ex.fe_start = bit;
  3195. ac->ac_b_ex.fe_len = pa->pa_len;
  3196. ac->ac_b_ex.fe_logical = 0;
  3197. trace_ext4_mballoc_discard(ac);
  3198. }
  3199. return 0;
  3200. }
  3201. /*
  3202. * releases all preallocations in given group
  3203. *
  3204. * first, we need to decide discard policy:
  3205. * - when do we discard
  3206. * 1) ENOSPC
  3207. * - how many do we discard
  3208. * 1) how many requested
  3209. */
  3210. static noinline_for_stack int
  3211. ext4_mb_discard_group_preallocations(struct super_block *sb,
  3212. ext4_group_t group, int needed)
  3213. {
  3214. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3215. struct buffer_head *bitmap_bh = NULL;
  3216. struct ext4_prealloc_space *pa, *tmp;
  3217. struct ext4_allocation_context *ac;
  3218. struct list_head list;
  3219. struct ext4_buddy e4b;
  3220. int err;
  3221. int busy = 0;
  3222. int free = 0;
  3223. mb_debug(1, "discard preallocation for group %u\n", group);
  3224. if (list_empty(&grp->bb_prealloc_list))
  3225. return 0;
  3226. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3227. if (bitmap_bh == NULL) {
  3228. ext4_error(sb, "Error reading block bitmap for %u", group);
  3229. return 0;
  3230. }
  3231. err = ext4_mb_load_buddy(sb, group, &e4b);
  3232. if (err) {
  3233. ext4_error(sb, "Error loading buddy information for %u", group);
  3234. put_bh(bitmap_bh);
  3235. return 0;
  3236. }
  3237. if (needed == 0)
  3238. needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
  3239. INIT_LIST_HEAD(&list);
  3240. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3241. if (ac)
  3242. ac->ac_sb = sb;
  3243. repeat:
  3244. ext4_lock_group(sb, group);
  3245. list_for_each_entry_safe(pa, tmp,
  3246. &grp->bb_prealloc_list, pa_group_list) {
  3247. spin_lock(&pa->pa_lock);
  3248. if (atomic_read(&pa->pa_count)) {
  3249. spin_unlock(&pa->pa_lock);
  3250. busy = 1;
  3251. continue;
  3252. }
  3253. if (pa->pa_deleted) {
  3254. spin_unlock(&pa->pa_lock);
  3255. continue;
  3256. }
  3257. /* seems this one can be freed ... */
  3258. pa->pa_deleted = 1;
  3259. /* we can trust pa_free ... */
  3260. free += pa->pa_free;
  3261. spin_unlock(&pa->pa_lock);
  3262. list_del(&pa->pa_group_list);
  3263. list_add(&pa->u.pa_tmp_list, &list);
  3264. }
  3265. /* if we still need more blocks and some PAs were used, try again */
  3266. if (free < needed && busy) {
  3267. busy = 0;
  3268. ext4_unlock_group(sb, group);
  3269. /*
  3270. * Yield the CPU here so that we don't get soft lockup
  3271. * in non preempt case.
  3272. */
  3273. yield();
  3274. goto repeat;
  3275. }
  3276. /* found anything to free? */
  3277. if (list_empty(&list)) {
  3278. BUG_ON(free != 0);
  3279. goto out;
  3280. }
  3281. /* now free all selected PAs */
  3282. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3283. /* remove from object (inode or locality group) */
  3284. spin_lock(pa->pa_obj_lock);
  3285. list_del_rcu(&pa->pa_inode_list);
  3286. spin_unlock(pa->pa_obj_lock);
  3287. if (pa->pa_type == MB_GROUP_PA)
  3288. ext4_mb_release_group_pa(&e4b, pa, ac);
  3289. else
  3290. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3291. list_del(&pa->u.pa_tmp_list);
  3292. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3293. }
  3294. out:
  3295. ext4_unlock_group(sb, group);
  3296. if (ac)
  3297. kmem_cache_free(ext4_ac_cachep, ac);
  3298. ext4_mb_unload_buddy(&e4b);
  3299. put_bh(bitmap_bh);
  3300. return free;
  3301. }
  3302. /*
  3303. * releases all non-used preallocated blocks for given inode
  3304. *
  3305. * It's important to discard preallocations under i_data_sem
  3306. * We don't want another block to be served from the prealloc
  3307. * space when we are discarding the inode prealloc space.
  3308. *
  3309. * FIXME!! Make sure it is valid at all the call sites
  3310. */
  3311. void ext4_discard_preallocations(struct inode *inode)
  3312. {
  3313. struct ext4_inode_info *ei = EXT4_I(inode);
  3314. struct super_block *sb = inode->i_sb;
  3315. struct buffer_head *bitmap_bh = NULL;
  3316. struct ext4_prealloc_space *pa, *tmp;
  3317. struct ext4_allocation_context *ac;
  3318. ext4_group_t group = 0;
  3319. struct list_head list;
  3320. struct ext4_buddy e4b;
  3321. int err;
  3322. if (!S_ISREG(inode->i_mode)) {
  3323. /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
  3324. return;
  3325. }
  3326. mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
  3327. trace_ext4_discard_preallocations(inode);
  3328. INIT_LIST_HEAD(&list);
  3329. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3330. if (ac) {
  3331. ac->ac_sb = sb;
  3332. ac->ac_inode = inode;
  3333. }
  3334. repeat:
  3335. /* first, collect all pa's in the inode */
  3336. spin_lock(&ei->i_prealloc_lock);
  3337. while (!list_empty(&ei->i_prealloc_list)) {
  3338. pa = list_entry(ei->i_prealloc_list.next,
  3339. struct ext4_prealloc_space, pa_inode_list);
  3340. BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
  3341. spin_lock(&pa->pa_lock);
  3342. if (atomic_read(&pa->pa_count)) {
  3343. /* this shouldn't happen often - nobody should
  3344. * use preallocation while we're discarding it */
  3345. spin_unlock(&pa->pa_lock);
  3346. spin_unlock(&ei->i_prealloc_lock);
  3347. printk(KERN_ERR "uh-oh! used pa while discarding\n");
  3348. WARN_ON(1);
  3349. schedule_timeout_uninterruptible(HZ);
  3350. goto repeat;
  3351. }
  3352. if (pa->pa_deleted == 0) {
  3353. pa->pa_deleted = 1;
  3354. spin_unlock(&pa->pa_lock);
  3355. list_del_rcu(&pa->pa_inode_list);
  3356. list_add(&pa->u.pa_tmp_list, &list);
  3357. continue;
  3358. }
  3359. /* someone is deleting pa right now */
  3360. spin_unlock(&pa->pa_lock);
  3361. spin_unlock(&ei->i_prealloc_lock);
  3362. /* we have to wait here because pa_deleted
  3363. * doesn't mean pa is already unlinked from
  3364. * the list. as we might be called from
  3365. * ->clear_inode() the inode will get freed
  3366. * and concurrent thread which is unlinking
  3367. * pa from inode's list may access already
  3368. * freed memory, bad-bad-bad */
  3369. /* XXX: if this happens too often, we can
  3370. * add a flag to force wait only in case
  3371. * of ->clear_inode(), but not in case of
  3372. * regular truncate */
  3373. schedule_timeout_uninterruptible(HZ);
  3374. goto repeat;
  3375. }
  3376. spin_unlock(&ei->i_prealloc_lock);
  3377. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3378. BUG_ON(pa->pa_type != MB_INODE_PA);
  3379. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3380. err = ext4_mb_load_buddy(sb, group, &e4b);
  3381. if (err) {
  3382. ext4_error(sb, "Error loading buddy information for %u",
  3383. group);
  3384. continue;
  3385. }
  3386. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3387. if (bitmap_bh == NULL) {
  3388. ext4_error(sb, "Error reading block bitmap for %u",
  3389. group);
  3390. ext4_mb_unload_buddy(&e4b);
  3391. continue;
  3392. }
  3393. ext4_lock_group(sb, group);
  3394. list_del(&pa->pa_group_list);
  3395. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3396. ext4_unlock_group(sb, group);
  3397. ext4_mb_unload_buddy(&e4b);
  3398. put_bh(bitmap_bh);
  3399. list_del(&pa->u.pa_tmp_list);
  3400. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3401. }
  3402. if (ac)
  3403. kmem_cache_free(ext4_ac_cachep, ac);
  3404. }
  3405. /*
  3406. * finds all preallocated spaces and return blocks being freed to them
  3407. * if preallocated space becomes full (no block is used from the space)
  3408. * then the function frees space in buddy
  3409. * XXX: at the moment, truncate (which is the only way to free blocks)
  3410. * discards all preallocations
  3411. */
  3412. static void ext4_mb_return_to_preallocation(struct inode *inode,
  3413. struct ext4_buddy *e4b,
  3414. sector_t block, int count)
  3415. {
  3416. BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
  3417. }
  3418. #ifdef CONFIG_EXT4_DEBUG
  3419. static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3420. {
  3421. struct super_block *sb = ac->ac_sb;
  3422. ext4_group_t ngroups, i;
  3423. printk(KERN_ERR "EXT4-fs: Can't allocate:"
  3424. " Allocation context details:\n");
  3425. printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
  3426. ac->ac_status, ac->ac_flags);
  3427. printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
  3428. "best %lu/%lu/%lu@%lu cr %d\n",
  3429. (unsigned long)ac->ac_o_ex.fe_group,
  3430. (unsigned long)ac->ac_o_ex.fe_start,
  3431. (unsigned long)ac->ac_o_ex.fe_len,
  3432. (unsigned long)ac->ac_o_ex.fe_logical,
  3433. (unsigned long)ac->ac_g_ex.fe_group,
  3434. (unsigned long)ac->ac_g_ex.fe_start,
  3435. (unsigned long)ac->ac_g_ex.fe_len,
  3436. (unsigned long)ac->ac_g_ex.fe_logical,
  3437. (unsigned long)ac->ac_b_ex.fe_group,
  3438. (unsigned long)ac->ac_b_ex.fe_start,
  3439. (unsigned long)ac->ac_b_ex.fe_len,
  3440. (unsigned long)ac->ac_b_ex.fe_logical,
  3441. (int)ac->ac_criteria);
  3442. printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
  3443. ac->ac_found);
  3444. printk(KERN_ERR "EXT4-fs: groups: \n");
  3445. ngroups = ext4_get_groups_count(sb);
  3446. for (i = 0; i < ngroups; i++) {
  3447. struct ext4_group_info *grp = ext4_get_group_info(sb, i);
  3448. struct ext4_prealloc_space *pa;
  3449. ext4_grpblk_t start;
  3450. struct list_head *cur;
  3451. ext4_lock_group(sb, i);
  3452. list_for_each(cur, &grp->bb_prealloc_list) {
  3453. pa = list_entry(cur, struct ext4_prealloc_space,
  3454. pa_group_list);
  3455. spin_lock(&pa->pa_lock);
  3456. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3457. NULL, &start);
  3458. spin_unlock(&pa->pa_lock);
  3459. printk(KERN_ERR "PA:%u:%d:%u \n", i,
  3460. start, pa->pa_len);
  3461. }
  3462. ext4_unlock_group(sb, i);
  3463. if (grp->bb_free == 0)
  3464. continue;
  3465. printk(KERN_ERR "%u: %d/%d \n",
  3466. i, grp->bb_free, grp->bb_fragments);
  3467. }
  3468. printk(KERN_ERR "\n");
  3469. }
  3470. #else
  3471. static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3472. {
  3473. return;
  3474. }
  3475. #endif
  3476. /*
  3477. * We use locality group preallocation for small size file. The size of the
  3478. * file is determined by the current size or the resulting size after
  3479. * allocation which ever is larger
  3480. *
  3481. * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
  3482. */
  3483. static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
  3484. {
  3485. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  3486. int bsbits = ac->ac_sb->s_blocksize_bits;
  3487. loff_t size, isize;
  3488. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  3489. return;
  3490. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  3491. return;
  3492. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  3493. isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
  3494. >> bsbits;
  3495. if ((size == isize) &&
  3496. !ext4_fs_is_busy(sbi) &&
  3497. (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
  3498. ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
  3499. return;
  3500. }
  3501. /* don't use group allocation for large files */
  3502. size = max(size, isize);
  3503. if (size > sbi->s_mb_stream_request) {
  3504. ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
  3505. return;
  3506. }
  3507. BUG_ON(ac->ac_lg != NULL);
  3508. /*
  3509. * locality group prealloc space are per cpu. The reason for having
  3510. * per cpu locality group is to reduce the contention between block
  3511. * request from multiple CPUs.
  3512. */
  3513. ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
  3514. /* we're going to use group allocation */
  3515. ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
  3516. /* serialize all allocations in the group */
  3517. mutex_lock(&ac->ac_lg->lg_mutex);
  3518. }
  3519. static noinline_for_stack int
  3520. ext4_mb_initialize_context(struct ext4_allocation_context *ac,
  3521. struct ext4_allocation_request *ar)
  3522. {
  3523. struct super_block *sb = ar->inode->i_sb;
  3524. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3525. struct ext4_super_block *es = sbi->s_es;
  3526. ext4_group_t group;
  3527. unsigned int len;
  3528. ext4_fsblk_t goal;
  3529. ext4_grpblk_t block;
  3530. /* we can't allocate > group size */
  3531. len = ar->len;
  3532. /* just a dirty hack to filter too big requests */
  3533. if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
  3534. len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
  3535. /* start searching from the goal */
  3536. goal = ar->goal;
  3537. if (goal < le32_to_cpu(es->s_first_data_block) ||
  3538. goal >= ext4_blocks_count(es))
  3539. goal = le32_to_cpu(es->s_first_data_block);
  3540. ext4_get_group_no_and_offset(sb, goal, &group, &block);
  3541. /* set up allocation goals */
  3542. memset(ac, 0, sizeof(struct ext4_allocation_context));
  3543. ac->ac_b_ex.fe_logical = ar->logical;
  3544. ac->ac_status = AC_STATUS_CONTINUE;
  3545. ac->ac_sb = sb;
  3546. ac->ac_inode = ar->inode;
  3547. ac->ac_o_ex.fe_logical = ar->logical;
  3548. ac->ac_o_ex.fe_group = group;
  3549. ac->ac_o_ex.fe_start = block;
  3550. ac->ac_o_ex.fe_len = len;
  3551. ac->ac_g_ex.fe_logical = ar->logical;
  3552. ac->ac_g_ex.fe_group = group;
  3553. ac->ac_g_ex.fe_start = block;
  3554. ac->ac_g_ex.fe_len = len;
  3555. ac->ac_flags = ar->flags;
  3556. /* we have to define context: we'll we work with a file or
  3557. * locality group. this is a policy, actually */
  3558. ext4_mb_group_or_file(ac);
  3559. mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
  3560. "left: %u/%u, right %u/%u to %swritable\n",
  3561. (unsigned) ar->len, (unsigned) ar->logical,
  3562. (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
  3563. (unsigned) ar->lleft, (unsigned) ar->pleft,
  3564. (unsigned) ar->lright, (unsigned) ar->pright,
  3565. atomic_read(&ar->inode->i_writecount) ? "" : "non-");
  3566. return 0;
  3567. }
  3568. static noinline_for_stack void
  3569. ext4_mb_discard_lg_preallocations(struct super_block *sb,
  3570. struct ext4_locality_group *lg,
  3571. int order, int total_entries)
  3572. {
  3573. ext4_group_t group = 0;
  3574. struct ext4_buddy e4b;
  3575. struct list_head discard_list;
  3576. struct ext4_prealloc_space *pa, *tmp;
  3577. struct ext4_allocation_context *ac;
  3578. mb_debug(1, "discard locality group preallocation\n");
  3579. INIT_LIST_HEAD(&discard_list);
  3580. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3581. if (ac)
  3582. ac->ac_sb = sb;
  3583. spin_lock(&lg->lg_prealloc_lock);
  3584. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
  3585. pa_inode_list) {
  3586. spin_lock(&pa->pa_lock);
  3587. if (atomic_read(&pa->pa_count)) {
  3588. /*
  3589. * This is the pa that we just used
  3590. * for block allocation. So don't
  3591. * free that
  3592. */
  3593. spin_unlock(&pa->pa_lock);
  3594. continue;
  3595. }
  3596. if (pa->pa_deleted) {
  3597. spin_unlock(&pa->pa_lock);
  3598. continue;
  3599. }
  3600. /* only lg prealloc space */
  3601. BUG_ON(pa->pa_type != MB_GROUP_PA);
  3602. /* seems this one can be freed ... */
  3603. pa->pa_deleted = 1;
  3604. spin_unlock(&pa->pa_lock);
  3605. list_del_rcu(&pa->pa_inode_list);
  3606. list_add(&pa->u.pa_tmp_list, &discard_list);
  3607. total_entries--;
  3608. if (total_entries <= 5) {
  3609. /*
  3610. * we want to keep only 5 entries
  3611. * allowing it to grow to 8. This
  3612. * mak sure we don't call discard
  3613. * soon for this list.
  3614. */
  3615. break;
  3616. }
  3617. }
  3618. spin_unlock(&lg->lg_prealloc_lock);
  3619. list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
  3620. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3621. if (ext4_mb_load_buddy(sb, group, &e4b)) {
  3622. ext4_error(sb, "Error loading buddy information for %u",
  3623. group);
  3624. continue;
  3625. }
  3626. ext4_lock_group(sb, group);
  3627. list_del(&pa->pa_group_list);
  3628. ext4_mb_release_group_pa(&e4b, pa, ac);
  3629. ext4_unlock_group(sb, group);
  3630. ext4_mb_unload_buddy(&e4b);
  3631. list_del(&pa->u.pa_tmp_list);
  3632. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3633. }
  3634. if (ac)
  3635. kmem_cache_free(ext4_ac_cachep, ac);
  3636. }
  3637. /*
  3638. * We have incremented pa_count. So it cannot be freed at this
  3639. * point. Also we hold lg_mutex. So no parallel allocation is
  3640. * possible from this lg. That means pa_free cannot be updated.
  3641. *
  3642. * A parallel ext4_mb_discard_group_preallocations is possible.
  3643. * which can cause the lg_prealloc_list to be updated.
  3644. */
  3645. static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
  3646. {
  3647. int order, added = 0, lg_prealloc_count = 1;
  3648. struct super_block *sb = ac->ac_sb;
  3649. struct ext4_locality_group *lg = ac->ac_lg;
  3650. struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
  3651. order = fls(pa->pa_free) - 1;
  3652. if (order > PREALLOC_TB_SIZE - 1)
  3653. /* The max size of hash table is PREALLOC_TB_SIZE */
  3654. order = PREALLOC_TB_SIZE - 1;
  3655. /* Add the prealloc space to lg */
  3656. rcu_read_lock();
  3657. list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
  3658. pa_inode_list) {
  3659. spin_lock(&tmp_pa->pa_lock);
  3660. if (tmp_pa->pa_deleted) {
  3661. spin_unlock(&tmp_pa->pa_lock);
  3662. continue;
  3663. }
  3664. if (!added && pa->pa_free < tmp_pa->pa_free) {
  3665. /* Add to the tail of the previous entry */
  3666. list_add_tail_rcu(&pa->pa_inode_list,
  3667. &tmp_pa->pa_inode_list);
  3668. added = 1;
  3669. /*
  3670. * we want to count the total
  3671. * number of entries in the list
  3672. */
  3673. }
  3674. spin_unlock(&tmp_pa->pa_lock);
  3675. lg_prealloc_count++;
  3676. }
  3677. if (!added)
  3678. list_add_tail_rcu(&pa->pa_inode_list,
  3679. &lg->lg_prealloc_list[order]);
  3680. rcu_read_unlock();
  3681. /* Now trim the list to be not more than 8 elements */
  3682. if (lg_prealloc_count > 8) {
  3683. ext4_mb_discard_lg_preallocations(sb, lg,
  3684. order, lg_prealloc_count);
  3685. return;
  3686. }
  3687. return ;
  3688. }
  3689. /*
  3690. * release all resource we used in allocation
  3691. */
  3692. static int ext4_mb_release_context(struct ext4_allocation_context *ac)
  3693. {
  3694. struct ext4_prealloc_space *pa = ac->ac_pa;
  3695. if (pa) {
  3696. if (pa->pa_type == MB_GROUP_PA) {
  3697. /* see comment in ext4_mb_use_group_pa() */
  3698. spin_lock(&pa->pa_lock);
  3699. pa->pa_pstart += ac->ac_b_ex.fe_len;
  3700. pa->pa_lstart += ac->ac_b_ex.fe_len;
  3701. pa->pa_free -= ac->ac_b_ex.fe_len;
  3702. pa->pa_len -= ac->ac_b_ex.fe_len;
  3703. spin_unlock(&pa->pa_lock);
  3704. }
  3705. }
  3706. if (ac->alloc_semp)
  3707. up_read(ac->alloc_semp);
  3708. if (pa) {
  3709. /*
  3710. * We want to add the pa to the right bucket.
  3711. * Remove it from the list and while adding
  3712. * make sure the list to which we are adding
  3713. * doesn't grow big. We need to release
  3714. * alloc_semp before calling ext4_mb_add_n_trim()
  3715. */
  3716. if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
  3717. spin_lock(pa->pa_obj_lock);
  3718. list_del_rcu(&pa->pa_inode_list);
  3719. spin_unlock(pa->pa_obj_lock);
  3720. ext4_mb_add_n_trim(ac);
  3721. }
  3722. ext4_mb_put_pa(ac, ac->ac_sb, pa);
  3723. }
  3724. if (ac->ac_bitmap_page)
  3725. page_cache_release(ac->ac_bitmap_page);
  3726. if (ac->ac_buddy_page)
  3727. page_cache_release(ac->ac_buddy_page);
  3728. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3729. mutex_unlock(&ac->ac_lg->lg_mutex);
  3730. ext4_mb_collect_stats(ac);
  3731. return 0;
  3732. }
  3733. static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
  3734. {
  3735. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3736. int ret;
  3737. int freed = 0;
  3738. trace_ext4_mb_discard_preallocations(sb, needed);
  3739. for (i = 0; i < ngroups && needed > 0; i++) {
  3740. ret = ext4_mb_discard_group_preallocations(sb, i, needed);
  3741. freed += ret;
  3742. needed -= ret;
  3743. }
  3744. return freed;
  3745. }
  3746. /*
  3747. * Main entry point into mballoc to allocate blocks
  3748. * it tries to use preallocation first, then falls back
  3749. * to usual allocation
  3750. */
  3751. ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  3752. struct ext4_allocation_request *ar, int *errp)
  3753. {
  3754. int freed;
  3755. struct ext4_allocation_context *ac = NULL;
  3756. struct ext4_sb_info *sbi;
  3757. struct super_block *sb;
  3758. ext4_fsblk_t block = 0;
  3759. unsigned int inquota = 0;
  3760. unsigned int reserv_blks = 0;
  3761. sb = ar->inode->i_sb;
  3762. sbi = EXT4_SB(sb);
  3763. trace_ext4_request_blocks(ar);
  3764. /*
  3765. * For delayed allocation, we could skip the ENOSPC and
  3766. * EDQUOT check, as blocks and quotas have been already
  3767. * reserved when data being copied into pagecache.
  3768. */
  3769. if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
  3770. ar->flags |= EXT4_MB_DELALLOC_RESERVED;
  3771. else {
  3772. /* Without delayed allocation we need to verify
  3773. * there is enough free blocks to do block allocation
  3774. * and verify allocation doesn't exceed the quota limits.
  3775. */
  3776. while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
  3777. /* let others to free the space */
  3778. yield();
  3779. ar->len = ar->len >> 1;
  3780. }
  3781. if (!ar->len) {
  3782. *errp = -ENOSPC;
  3783. return 0;
  3784. }
  3785. reserv_blks = ar->len;
  3786. while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
  3787. ar->flags |= EXT4_MB_HINT_NOPREALLOC;
  3788. ar->len--;
  3789. }
  3790. inquota = ar->len;
  3791. if (ar->len == 0) {
  3792. *errp = -EDQUOT;
  3793. goto out3;
  3794. }
  3795. }
  3796. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3797. if (!ac) {
  3798. ar->len = 0;
  3799. *errp = -ENOMEM;
  3800. goto out1;
  3801. }
  3802. *errp = ext4_mb_initialize_context(ac, ar);
  3803. if (*errp) {
  3804. ar->len = 0;
  3805. goto out2;
  3806. }
  3807. ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
  3808. if (!ext4_mb_use_preallocated(ac)) {
  3809. ac->ac_op = EXT4_MB_HISTORY_ALLOC;
  3810. ext4_mb_normalize_request(ac, ar);
  3811. repeat:
  3812. /* allocate space in core */
  3813. ext4_mb_regular_allocator(ac);
  3814. /* as we've just preallocated more space than
  3815. * user requested orinally, we store allocated
  3816. * space in a special descriptor */
  3817. if (ac->ac_status == AC_STATUS_FOUND &&
  3818. ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
  3819. ext4_mb_new_preallocation(ac);
  3820. }
  3821. if (likely(ac->ac_status == AC_STATUS_FOUND)) {
  3822. *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
  3823. if (*errp == -EAGAIN) {
  3824. /*
  3825. * drop the reference that we took
  3826. * in ext4_mb_use_best_found
  3827. */
  3828. ext4_mb_release_context(ac);
  3829. ac->ac_b_ex.fe_group = 0;
  3830. ac->ac_b_ex.fe_start = 0;
  3831. ac->ac_b_ex.fe_len = 0;
  3832. ac->ac_status = AC_STATUS_CONTINUE;
  3833. goto repeat;
  3834. } else if (*errp) {
  3835. ext4_discard_allocated_blocks(ac);
  3836. ac->ac_b_ex.fe_len = 0;
  3837. ar->len = 0;
  3838. ext4_mb_show_ac(ac);
  3839. } else {
  3840. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3841. ar->len = ac->ac_b_ex.fe_len;
  3842. }
  3843. } else {
  3844. freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
  3845. if (freed)
  3846. goto repeat;
  3847. *errp = -ENOSPC;
  3848. ac->ac_b_ex.fe_len = 0;
  3849. ar->len = 0;
  3850. ext4_mb_show_ac(ac);
  3851. }
  3852. ext4_mb_release_context(ac);
  3853. out2:
  3854. kmem_cache_free(ext4_ac_cachep, ac);
  3855. out1:
  3856. if (inquota && ar->len < inquota)
  3857. dquot_free_block(ar->inode, inquota - ar->len);
  3858. out3:
  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_grpblk_t block;
  3887. struct ext4_free_data *entry;
  3888. struct ext4_group_info *db = e4b->bd_info;
  3889. struct super_block *sb = e4b->bd_sb;
  3890. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3891. struct rb_node **n = &db->bb_free_root.rb_node, *node;
  3892. struct rb_node *parent = NULL, *new_node;
  3893. BUG_ON(!ext4_handle_valid(handle));
  3894. BUG_ON(e4b->bd_bitmap_page == NULL);
  3895. BUG_ON(e4b->bd_buddy_page == NULL);
  3896. new_node = &new_entry->node;
  3897. block = new_entry->start_blk;
  3898. if (!*n) {
  3899. /* first free block exent. We need to
  3900. protect buddy cache from being freed,
  3901. * otherwise we'll refresh it from
  3902. * on-disk bitmap and lose not-yet-available
  3903. * blocks */
  3904. page_cache_get(e4b->bd_buddy_page);
  3905. page_cache_get(e4b->bd_bitmap_page);
  3906. }
  3907. while (*n) {
  3908. parent = *n;
  3909. entry = rb_entry(parent, struct ext4_free_data, node);
  3910. if (block < entry->start_blk)
  3911. n = &(*n)->rb_left;
  3912. else if (block >= (entry->start_blk + entry->count))
  3913. n = &(*n)->rb_right;
  3914. else {
  3915. ext4_grp_locked_error(sb, e4b->bd_group, __func__,
  3916. "Double free of blocks %d (%d %d)",
  3917. block, entry->start_blk, entry->count);
  3918. return 0;
  3919. }
  3920. }
  3921. rb_link_node(new_node, parent, n);
  3922. rb_insert_color(new_node, &db->bb_free_root);
  3923. /* Now try to see the extent can be merged to left and right */
  3924. node = rb_prev(new_node);
  3925. if (node) {
  3926. entry = rb_entry(node, struct ext4_free_data, node);
  3927. if (can_merge(entry, new_entry)) {
  3928. new_entry->start_blk = entry->start_blk;
  3929. new_entry->count += entry->count;
  3930. rb_erase(node, &(db->bb_free_root));
  3931. spin_lock(&sbi->s_md_lock);
  3932. list_del(&entry->list);
  3933. spin_unlock(&sbi->s_md_lock);
  3934. kmem_cache_free(ext4_free_ext_cachep, entry);
  3935. }
  3936. }
  3937. node = rb_next(new_node);
  3938. if (node) {
  3939. entry = rb_entry(node, struct ext4_free_data, node);
  3940. if (can_merge(new_entry, entry)) {
  3941. new_entry->count += entry->count;
  3942. rb_erase(node, &(db->bb_free_root));
  3943. spin_lock(&sbi->s_md_lock);
  3944. list_del(&entry->list);
  3945. spin_unlock(&sbi->s_md_lock);
  3946. kmem_cache_free(ext4_free_ext_cachep, entry);
  3947. }
  3948. }
  3949. /* Add the extent to transaction's private list */
  3950. spin_lock(&sbi->s_md_lock);
  3951. list_add(&new_entry->list, &handle->h_transaction->t_private_list);
  3952. spin_unlock(&sbi->s_md_lock);
  3953. return 0;
  3954. }
  3955. /**
  3956. * ext4_free_blocks() -- Free given blocks and update quota
  3957. * @handle: handle for this transaction
  3958. * @inode: inode
  3959. * @block: start physical block to free
  3960. * @count: number of blocks to count
  3961. * @metadata: Are these metadata blocks
  3962. */
  3963. void ext4_free_blocks(handle_t *handle, struct inode *inode,
  3964. struct buffer_head *bh, ext4_fsblk_t block,
  3965. unsigned long count, int flags)
  3966. {
  3967. struct buffer_head *bitmap_bh = NULL;
  3968. struct super_block *sb = inode->i_sb;
  3969. struct ext4_allocation_context *ac = NULL;
  3970. struct ext4_group_desc *gdp;
  3971. struct ext4_super_block *es;
  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. es = EXT4_SB(sb)->s_es;
  3989. if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
  3990. !ext4_data_block_valid(sbi, block, count)) {
  3991. ext4_error(sb, "Freeing blocks not in datazone - "
  3992. "block = %llu, count = %lu", block, count);
  3993. goto error_return;
  3994. }
  3995. ext4_debug("freeing block %llu\n", block);
  3996. trace_ext4_free_blocks(inode, block, count, flags);
  3997. if (flags & EXT4_FREE_BLOCKS_FORGET) {
  3998. struct buffer_head *tbh = bh;
  3999. int i;
  4000. BUG_ON(bh && (count > 1));
  4001. for (i = 0; i < count; i++) {
  4002. if (!bh)
  4003. tbh = sb_find_get_block(inode->i_sb,
  4004. block + i);
  4005. ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
  4006. inode, tbh, block + i);
  4007. }
  4008. }
  4009. /*
  4010. * We need to make sure we don't reuse the freed block until
  4011. * after the transaction is committed, which we can do by
  4012. * treating the block as metadata, below. We make an
  4013. * exception if the inode is to be written in writeback mode
  4014. * since writeback mode has weak data consistency guarantees.
  4015. */
  4016. if (!ext4_should_writeback_data(inode))
  4017. flags |= EXT4_FREE_BLOCKS_METADATA;
  4018. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  4019. if (ac) {
  4020. ac->ac_inode = inode;
  4021. ac->ac_sb = sb;
  4022. }
  4023. do_more:
  4024. overflow = 0;
  4025. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  4026. /*
  4027. * Check to see if we are freeing blocks across a group
  4028. * boundary.
  4029. */
  4030. if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  4031. overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
  4032. count -= overflow;
  4033. }
  4034. bitmap_bh = ext4_read_block_bitmap(sb, block_group);
  4035. if (!bitmap_bh) {
  4036. err = -EIO;
  4037. goto error_return;
  4038. }
  4039. gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
  4040. if (!gdp) {
  4041. err = -EIO;
  4042. goto error_return;
  4043. }
  4044. if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
  4045. in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
  4046. in_range(block, ext4_inode_table(sb, gdp),
  4047. EXT4_SB(sb)->s_itb_per_group) ||
  4048. in_range(block + count - 1, ext4_inode_table(sb, gdp),
  4049. EXT4_SB(sb)->s_itb_per_group)) {
  4050. ext4_error(sb, "Freeing blocks in system zone - "
  4051. "Block = %llu, count = %lu", block, count);
  4052. /* err = 0. ext4_std_error should be a no op */
  4053. goto error_return;
  4054. }
  4055. BUFFER_TRACE(bitmap_bh, "getting write access");
  4056. err = ext4_journal_get_write_access(handle, bitmap_bh);
  4057. if (err)
  4058. goto error_return;
  4059. /*
  4060. * We are about to modify some metadata. Call the journal APIs
  4061. * to unshare ->b_data if a currently-committing transaction is
  4062. * using it
  4063. */
  4064. BUFFER_TRACE(gd_bh, "get_write_access");
  4065. err = ext4_journal_get_write_access(handle, gd_bh);
  4066. if (err)
  4067. goto error_return;
  4068. #ifdef AGGRESSIVE_CHECK
  4069. {
  4070. int i;
  4071. for (i = 0; i < count; i++)
  4072. BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
  4073. }
  4074. #endif
  4075. if (ac) {
  4076. ac->ac_b_ex.fe_group = block_group;
  4077. ac->ac_b_ex.fe_start = bit;
  4078. ac->ac_b_ex.fe_len = count;
  4079. trace_ext4_mballoc_free(ac);
  4080. }
  4081. err = ext4_mb_load_buddy(sb, block_group, &e4b);
  4082. if (err)
  4083. goto error_return;
  4084. if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
  4085. struct ext4_free_data *new_entry;
  4086. /*
  4087. * blocks being freed are metadata. these blocks shouldn't
  4088. * be used until this transaction is committed
  4089. */
  4090. new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
  4091. new_entry->start_blk = bit;
  4092. new_entry->group = block_group;
  4093. new_entry->count = count;
  4094. new_entry->t_tid = handle->h_transaction->t_tid;
  4095. ext4_lock_group(sb, block_group);
  4096. mb_clear_bits(bitmap_bh->b_data, bit, count);
  4097. ext4_mb_free_metadata(handle, &e4b, new_entry);
  4098. } else {
  4099. /* need to update group_info->bb_free and bitmap
  4100. * with group lock held. generate_buddy look at
  4101. * them with group lock_held
  4102. */
  4103. ext4_lock_group(sb, block_group);
  4104. mb_clear_bits(bitmap_bh->b_data, bit, count);
  4105. mb_free_blocks(inode, &e4b, bit, count);
  4106. ext4_mb_return_to_preallocation(inode, &e4b, block, count);
  4107. }
  4108. ret = ext4_free_blks_count(sb, gdp) + count;
  4109. ext4_free_blks_set(sb, gdp, ret);
  4110. gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
  4111. ext4_unlock_group(sb, block_group);
  4112. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  4113. if (sbi->s_log_groups_per_flex) {
  4114. ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
  4115. atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
  4116. }
  4117. ext4_mb_unload_buddy(&e4b);
  4118. freed += count;
  4119. /* We dirtied the bitmap block */
  4120. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  4121. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  4122. /* And the group descriptor block */
  4123. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  4124. ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
  4125. if (!err)
  4126. err = ret;
  4127. if (overflow && !err) {
  4128. block += count;
  4129. count = overflow;
  4130. put_bh(bitmap_bh);
  4131. goto do_more;
  4132. }
  4133. sb->s_dirt = 1;
  4134. error_return:
  4135. if (freed)
  4136. dquot_free_block(inode, freed);
  4137. brelse(bitmap_bh);
  4138. ext4_std_error(sb, err);
  4139. if (ac)
  4140. kmem_cache_free(ext4_ac_cachep, ac);
  4141. return;
  4142. }