mballoc.c 132 KB

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