mballoc.c 138 KB

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