mballoc.c 146 KB

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