mballoc.c 146 KB

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