mballoc.c 146 KB

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