mballoc.c 139 KB

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