mballoc.c 147 KB

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