mballoc.c 125 KB

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