mballoc.c 125 KB

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