mballoc.c 144 KB

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