mballoc.c 138 KB

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