mballoc.c 144 KB

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