mballoc.c 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747
  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. * Locking note: This routine calls ext4_mb_init_cache(), which takes the
  869. * block group lock of all groups for this page; do not hold the BG lock when
  870. * calling this routine!
  871. */
  872. static noinline_for_stack
  873. int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
  874. {
  875. int ret = 0;
  876. void *bitmap;
  877. int blocks_per_page;
  878. int block, pnum, poff;
  879. int num_grp_locked = 0;
  880. struct ext4_group_info *this_grp;
  881. struct ext4_sb_info *sbi = EXT4_SB(sb);
  882. struct inode *inode = sbi->s_buddy_cache;
  883. struct page *page = NULL, *bitmap_page = NULL;
  884. mb_debug(1, "init group %u\n", group);
  885. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  886. this_grp = ext4_get_group_info(sb, group);
  887. /*
  888. * This ensures that we don't reinit the buddy cache
  889. * page which map to the group from which we are already
  890. * allocating. If we are looking at the buddy cache we would
  891. * have taken a reference using ext4_mb_load_buddy and that
  892. * would have taken the alloc_sem lock.
  893. */
  894. num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
  895. if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
  896. /*
  897. * somebody initialized the group
  898. * return without doing anything
  899. */
  900. ret = 0;
  901. goto err;
  902. }
  903. /*
  904. * the buddy cache inode stores the block bitmap
  905. * and buddy information in consecutive blocks.
  906. * So for each group we need two blocks.
  907. */
  908. block = group * 2;
  909. pnum = block / blocks_per_page;
  910. poff = block % blocks_per_page;
  911. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  912. if (page) {
  913. BUG_ON(page->mapping != inode->i_mapping);
  914. ret = ext4_mb_init_cache(page, NULL);
  915. if (ret) {
  916. unlock_page(page);
  917. goto err;
  918. }
  919. unlock_page(page);
  920. }
  921. if (page == NULL || !PageUptodate(page)) {
  922. ret = -EIO;
  923. goto err;
  924. }
  925. mark_page_accessed(page);
  926. bitmap_page = page;
  927. bitmap = page_address(page) + (poff * sb->s_blocksize);
  928. /* init buddy cache */
  929. block++;
  930. pnum = block / blocks_per_page;
  931. poff = block % blocks_per_page;
  932. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  933. if (page == bitmap_page) {
  934. /*
  935. * If both the bitmap and buddy are in
  936. * the same page we don't need to force
  937. * init the buddy
  938. */
  939. unlock_page(page);
  940. } else if (page) {
  941. BUG_ON(page->mapping != inode->i_mapping);
  942. ret = ext4_mb_init_cache(page, bitmap);
  943. if (ret) {
  944. unlock_page(page);
  945. goto err;
  946. }
  947. unlock_page(page);
  948. }
  949. if (page == NULL || !PageUptodate(page)) {
  950. ret = -EIO;
  951. goto err;
  952. }
  953. mark_page_accessed(page);
  954. err:
  955. ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
  956. if (bitmap_page)
  957. page_cache_release(bitmap_page);
  958. if (page)
  959. page_cache_release(page);
  960. return ret;
  961. }
  962. /*
  963. * Locking note: This routine calls ext4_mb_init_cache(), which takes the
  964. * block group lock of all groups for this page; do not hold the BG lock when
  965. * calling this routine!
  966. */
  967. static noinline_for_stack int
  968. ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
  969. struct ext4_buddy *e4b)
  970. {
  971. int blocks_per_page;
  972. int block;
  973. int pnum;
  974. int poff;
  975. struct page *page;
  976. int ret;
  977. struct ext4_group_info *grp;
  978. struct ext4_sb_info *sbi = EXT4_SB(sb);
  979. struct inode *inode = sbi->s_buddy_cache;
  980. mb_debug(1, "load group %u\n", group);
  981. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  982. grp = ext4_get_group_info(sb, group);
  983. e4b->bd_blkbits = sb->s_blocksize_bits;
  984. e4b->bd_info = ext4_get_group_info(sb, group);
  985. e4b->bd_sb = sb;
  986. e4b->bd_group = group;
  987. e4b->bd_buddy_page = NULL;
  988. e4b->bd_bitmap_page = NULL;
  989. e4b->alloc_semp = &grp->alloc_sem;
  990. /* Take the read lock on the group alloc
  991. * sem. This would make sure a parallel
  992. * ext4_mb_init_group happening on other
  993. * groups mapped by the page is blocked
  994. * till we are done with allocation
  995. */
  996. repeat_load_buddy:
  997. down_read(e4b->alloc_semp);
  998. if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
  999. /* we need to check for group need init flag
  1000. * with alloc_semp held so that we can be sure
  1001. * that new blocks didn't get added to the group
  1002. * when we are loading the buddy cache
  1003. */
  1004. up_read(e4b->alloc_semp);
  1005. /*
  1006. * we need full data about the group
  1007. * to make a good selection
  1008. */
  1009. ret = ext4_mb_init_group(sb, group);
  1010. if (ret)
  1011. return ret;
  1012. goto repeat_load_buddy;
  1013. }
  1014. /*
  1015. * the buddy cache inode stores the block bitmap
  1016. * and buddy information in consecutive blocks.
  1017. * So for each group we need two blocks.
  1018. */
  1019. block = group * 2;
  1020. pnum = block / blocks_per_page;
  1021. poff = block % blocks_per_page;
  1022. /* we could use find_or_create_page(), but it locks page
  1023. * what we'd like to avoid in fast path ... */
  1024. page = find_get_page(inode->i_mapping, pnum);
  1025. if (page == NULL || !PageUptodate(page)) {
  1026. if (page)
  1027. /*
  1028. * drop the page reference and try
  1029. * to get the page with lock. If we
  1030. * are not uptodate that implies
  1031. * somebody just created the page but
  1032. * is yet to initialize the same. So
  1033. * wait for it to initialize.
  1034. */
  1035. page_cache_release(page);
  1036. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1037. if (page) {
  1038. BUG_ON(page->mapping != inode->i_mapping);
  1039. if (!PageUptodate(page)) {
  1040. ret = ext4_mb_init_cache(page, NULL);
  1041. if (ret) {
  1042. unlock_page(page);
  1043. goto err;
  1044. }
  1045. mb_cmp_bitmaps(e4b, page_address(page) +
  1046. (poff * sb->s_blocksize));
  1047. }
  1048. unlock_page(page);
  1049. }
  1050. }
  1051. if (page == NULL || !PageUptodate(page)) {
  1052. ret = -EIO;
  1053. goto err;
  1054. }
  1055. e4b->bd_bitmap_page = page;
  1056. e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
  1057. mark_page_accessed(page);
  1058. block++;
  1059. pnum = block / blocks_per_page;
  1060. poff = block % blocks_per_page;
  1061. page = find_get_page(inode->i_mapping, pnum);
  1062. if (page == NULL || !PageUptodate(page)) {
  1063. if (page)
  1064. page_cache_release(page);
  1065. page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
  1066. if (page) {
  1067. BUG_ON(page->mapping != inode->i_mapping);
  1068. if (!PageUptodate(page)) {
  1069. ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
  1070. if (ret) {
  1071. unlock_page(page);
  1072. goto err;
  1073. }
  1074. }
  1075. unlock_page(page);
  1076. }
  1077. }
  1078. if (page == NULL || !PageUptodate(page)) {
  1079. ret = -EIO;
  1080. goto err;
  1081. }
  1082. e4b->bd_buddy_page = page;
  1083. e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
  1084. mark_page_accessed(page);
  1085. BUG_ON(e4b->bd_bitmap_page == NULL);
  1086. BUG_ON(e4b->bd_buddy_page == NULL);
  1087. return 0;
  1088. err:
  1089. if (e4b->bd_bitmap_page)
  1090. page_cache_release(e4b->bd_bitmap_page);
  1091. if (e4b->bd_buddy_page)
  1092. page_cache_release(e4b->bd_buddy_page);
  1093. e4b->bd_buddy = NULL;
  1094. e4b->bd_bitmap = NULL;
  1095. /* Done with the buddy cache */
  1096. up_read(e4b->alloc_semp);
  1097. return ret;
  1098. }
  1099. static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
  1100. {
  1101. if (e4b->bd_bitmap_page)
  1102. page_cache_release(e4b->bd_bitmap_page);
  1103. if (e4b->bd_buddy_page)
  1104. page_cache_release(e4b->bd_buddy_page);
  1105. /* Done with the buddy cache */
  1106. if (e4b->alloc_semp)
  1107. up_read(e4b->alloc_semp);
  1108. }
  1109. static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
  1110. {
  1111. int order = 1;
  1112. void *bb;
  1113. BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
  1114. BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
  1115. bb = EXT4_MB_BUDDY(e4b);
  1116. while (order <= e4b->bd_blkbits + 1) {
  1117. block = block >> 1;
  1118. if (!mb_test_bit(block, bb)) {
  1119. /* this block is part of buddy of order 'order' */
  1120. return order;
  1121. }
  1122. bb += 1 << (e4b->bd_blkbits - order);
  1123. order++;
  1124. }
  1125. return 0;
  1126. }
  1127. static void mb_clear_bits(void *bm, int cur, int len)
  1128. {
  1129. __u32 *addr;
  1130. len = cur + len;
  1131. while (cur < len) {
  1132. if ((cur & 31) == 0 && (len - cur) >= 32) {
  1133. /* fast path: clear whole word at once */
  1134. addr = bm + (cur >> 3);
  1135. *addr = 0;
  1136. cur += 32;
  1137. continue;
  1138. }
  1139. mb_clear_bit(cur, bm);
  1140. cur++;
  1141. }
  1142. }
  1143. static void mb_set_bits(void *bm, int cur, int len)
  1144. {
  1145. __u32 *addr;
  1146. len = cur + len;
  1147. while (cur < len) {
  1148. if ((cur & 31) == 0 && (len - cur) >= 32) {
  1149. /* fast path: set whole word at once */
  1150. addr = bm + (cur >> 3);
  1151. *addr = 0xffffffff;
  1152. cur += 32;
  1153. continue;
  1154. }
  1155. mb_set_bit(cur, bm);
  1156. cur++;
  1157. }
  1158. }
  1159. static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
  1160. int first, int count)
  1161. {
  1162. int block = 0;
  1163. int max = 0;
  1164. int order;
  1165. void *buddy;
  1166. void *buddy2;
  1167. struct super_block *sb = e4b->bd_sb;
  1168. BUG_ON(first + count > (sb->s_blocksize << 3));
  1169. assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
  1170. mb_check_buddy(e4b);
  1171. mb_free_blocks_double(inode, e4b, first, count);
  1172. e4b->bd_info->bb_free += count;
  1173. if (first < e4b->bd_info->bb_first_free)
  1174. e4b->bd_info->bb_first_free = first;
  1175. /* let's maintain fragments counter */
  1176. if (first != 0)
  1177. block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
  1178. if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
  1179. max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
  1180. if (block && max)
  1181. e4b->bd_info->bb_fragments--;
  1182. else if (!block && !max)
  1183. e4b->bd_info->bb_fragments++;
  1184. /* let's maintain buddy itself */
  1185. while (count-- > 0) {
  1186. block = first++;
  1187. order = 0;
  1188. if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
  1189. ext4_fsblk_t blocknr;
  1190. blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
  1191. blocknr += block;
  1192. ext4_grp_locked_error(sb, e4b->bd_group,
  1193. inode ? inode->i_ino : 0,
  1194. blocknr,
  1195. "freeing already freed block "
  1196. "(bit %u)", block);
  1197. }
  1198. mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
  1199. e4b->bd_info->bb_counters[order]++;
  1200. /* start of the buddy */
  1201. buddy = mb_find_buddy(e4b, order, &max);
  1202. do {
  1203. block &= ~1UL;
  1204. if (mb_test_bit(block, buddy) ||
  1205. mb_test_bit(block + 1, buddy))
  1206. break;
  1207. /* both the buddies are free, try to coalesce them */
  1208. buddy2 = mb_find_buddy(e4b, order + 1, &max);
  1209. if (!buddy2)
  1210. break;
  1211. if (order > 0) {
  1212. /* for special purposes, we don't set
  1213. * free bits in bitmap */
  1214. mb_set_bit(block, buddy);
  1215. mb_set_bit(block + 1, buddy);
  1216. }
  1217. e4b->bd_info->bb_counters[order]--;
  1218. e4b->bd_info->bb_counters[order]--;
  1219. block = block >> 1;
  1220. order++;
  1221. e4b->bd_info->bb_counters[order]++;
  1222. mb_clear_bit(block, buddy2);
  1223. buddy = buddy2;
  1224. } while (1);
  1225. }
  1226. mb_set_largest_free_order(sb, e4b->bd_info);
  1227. mb_check_buddy(e4b);
  1228. }
  1229. static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
  1230. int needed, struct ext4_free_extent *ex)
  1231. {
  1232. int next = block;
  1233. int max;
  1234. int ord;
  1235. void *buddy;
  1236. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  1237. BUG_ON(ex == NULL);
  1238. buddy = mb_find_buddy(e4b, order, &max);
  1239. BUG_ON(buddy == NULL);
  1240. BUG_ON(block >= max);
  1241. if (mb_test_bit(block, buddy)) {
  1242. ex->fe_len = 0;
  1243. ex->fe_start = 0;
  1244. ex->fe_group = 0;
  1245. return 0;
  1246. }
  1247. /* FIXME dorp order completely ? */
  1248. if (likely(order == 0)) {
  1249. /* find actual order */
  1250. order = mb_find_order_for_block(e4b, block);
  1251. block = block >> order;
  1252. }
  1253. ex->fe_len = 1 << order;
  1254. ex->fe_start = block << order;
  1255. ex->fe_group = e4b->bd_group;
  1256. /* calc difference from given start */
  1257. next = next - ex->fe_start;
  1258. ex->fe_len -= next;
  1259. ex->fe_start += next;
  1260. while (needed > ex->fe_len &&
  1261. (buddy = mb_find_buddy(e4b, order, &max))) {
  1262. if (block + 1 >= max)
  1263. break;
  1264. next = (block + 1) * (1 << order);
  1265. if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
  1266. break;
  1267. ord = mb_find_order_for_block(e4b, next);
  1268. order = ord;
  1269. block = next >> order;
  1270. ex->fe_len += 1 << order;
  1271. }
  1272. BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
  1273. return ex->fe_len;
  1274. }
  1275. static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
  1276. {
  1277. int ord;
  1278. int mlen = 0;
  1279. int max = 0;
  1280. int cur;
  1281. int start = ex->fe_start;
  1282. int len = ex->fe_len;
  1283. unsigned ret = 0;
  1284. int len0 = len;
  1285. void *buddy;
  1286. BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
  1287. BUG_ON(e4b->bd_group != ex->fe_group);
  1288. assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
  1289. mb_check_buddy(e4b);
  1290. mb_mark_used_double(e4b, start, len);
  1291. e4b->bd_info->bb_free -= len;
  1292. if (e4b->bd_info->bb_first_free == start)
  1293. e4b->bd_info->bb_first_free += len;
  1294. /* let's maintain fragments counter */
  1295. if (start != 0)
  1296. mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
  1297. if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
  1298. max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
  1299. if (mlen && max)
  1300. e4b->bd_info->bb_fragments++;
  1301. else if (!mlen && !max)
  1302. e4b->bd_info->bb_fragments--;
  1303. /* let's maintain buddy itself */
  1304. while (len) {
  1305. ord = mb_find_order_for_block(e4b, start);
  1306. if (((start >> ord) << ord) == start && len >= (1 << ord)) {
  1307. /* the whole chunk may be allocated at once! */
  1308. mlen = 1 << ord;
  1309. buddy = mb_find_buddy(e4b, ord, &max);
  1310. BUG_ON((start >> ord) >= max);
  1311. mb_set_bit(start >> ord, buddy);
  1312. e4b->bd_info->bb_counters[ord]--;
  1313. start += mlen;
  1314. len -= mlen;
  1315. BUG_ON(len < 0);
  1316. continue;
  1317. }
  1318. /* store for history */
  1319. if (ret == 0)
  1320. ret = len | (ord << 16);
  1321. /* we have to split large buddy */
  1322. BUG_ON(ord <= 0);
  1323. buddy = mb_find_buddy(e4b, ord, &max);
  1324. mb_set_bit(start >> ord, buddy);
  1325. e4b->bd_info->bb_counters[ord]--;
  1326. ord--;
  1327. cur = (start >> ord) & ~1U;
  1328. buddy = mb_find_buddy(e4b, ord, &max);
  1329. mb_clear_bit(cur, buddy);
  1330. mb_clear_bit(cur + 1, buddy);
  1331. e4b->bd_info->bb_counters[ord]++;
  1332. e4b->bd_info->bb_counters[ord]++;
  1333. }
  1334. mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
  1335. mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
  1336. mb_check_buddy(e4b);
  1337. return ret;
  1338. }
  1339. /*
  1340. * Must be called under group lock!
  1341. */
  1342. static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
  1343. struct ext4_buddy *e4b)
  1344. {
  1345. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1346. int ret;
  1347. BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
  1348. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1349. ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
  1350. ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
  1351. ret = mb_mark_used(e4b, &ac->ac_b_ex);
  1352. /* preallocation can change ac_b_ex, thus we store actually
  1353. * allocated blocks for history */
  1354. ac->ac_f_ex = ac->ac_b_ex;
  1355. ac->ac_status = AC_STATUS_FOUND;
  1356. ac->ac_tail = ret & 0xffff;
  1357. ac->ac_buddy = ret >> 16;
  1358. /*
  1359. * take the page reference. We want the page to be pinned
  1360. * so that we don't get a ext4_mb_init_cache_call for this
  1361. * group until we update the bitmap. That would mean we
  1362. * double allocate blocks. The reference is dropped
  1363. * in ext4_mb_release_context
  1364. */
  1365. ac->ac_bitmap_page = e4b->bd_bitmap_page;
  1366. get_page(ac->ac_bitmap_page);
  1367. ac->ac_buddy_page = e4b->bd_buddy_page;
  1368. get_page(ac->ac_buddy_page);
  1369. /* on allocation we use ac to track the held semaphore */
  1370. ac->alloc_semp = e4b->alloc_semp;
  1371. e4b->alloc_semp = NULL;
  1372. /* store last allocated for subsequent stream allocation */
  1373. if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
  1374. spin_lock(&sbi->s_md_lock);
  1375. sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
  1376. sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
  1377. spin_unlock(&sbi->s_md_lock);
  1378. }
  1379. }
  1380. /*
  1381. * regular allocator, for general purposes allocation
  1382. */
  1383. static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
  1384. struct ext4_buddy *e4b,
  1385. int finish_group)
  1386. {
  1387. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1388. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1389. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1390. struct ext4_free_extent ex;
  1391. int max;
  1392. if (ac->ac_status == AC_STATUS_FOUND)
  1393. return;
  1394. /*
  1395. * We don't want to scan for a whole year
  1396. */
  1397. if (ac->ac_found > sbi->s_mb_max_to_scan &&
  1398. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1399. ac->ac_status = AC_STATUS_BREAK;
  1400. return;
  1401. }
  1402. /*
  1403. * Haven't found good chunk so far, let's continue
  1404. */
  1405. if (bex->fe_len < gex->fe_len)
  1406. return;
  1407. if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
  1408. && bex->fe_group == e4b->bd_group) {
  1409. /* recheck chunk's availability - we don't know
  1410. * when it was found (within this lock-unlock
  1411. * period or not) */
  1412. max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
  1413. if (max >= gex->fe_len) {
  1414. ext4_mb_use_best_found(ac, e4b);
  1415. return;
  1416. }
  1417. }
  1418. }
  1419. /*
  1420. * The routine checks whether found extent is good enough. If it is,
  1421. * then the extent gets marked used and flag is set to the context
  1422. * to stop scanning. Otherwise, the extent is compared with the
  1423. * previous found extent and if new one is better, then it's stored
  1424. * in the context. Later, the best found extent will be used, if
  1425. * mballoc can't find good enough extent.
  1426. *
  1427. * FIXME: real allocation policy is to be designed yet!
  1428. */
  1429. static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
  1430. struct ext4_free_extent *ex,
  1431. struct ext4_buddy *e4b)
  1432. {
  1433. struct ext4_free_extent *bex = &ac->ac_b_ex;
  1434. struct ext4_free_extent *gex = &ac->ac_g_ex;
  1435. BUG_ON(ex->fe_len <= 0);
  1436. BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1437. BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  1438. BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
  1439. ac->ac_found++;
  1440. /*
  1441. * The special case - take what you catch first
  1442. */
  1443. if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1444. *bex = *ex;
  1445. ext4_mb_use_best_found(ac, e4b);
  1446. return;
  1447. }
  1448. /*
  1449. * Let's check whether the chuck is good enough
  1450. */
  1451. if (ex->fe_len == gex->fe_len) {
  1452. *bex = *ex;
  1453. ext4_mb_use_best_found(ac, e4b);
  1454. return;
  1455. }
  1456. /*
  1457. * If this is first found extent, just store it in the context
  1458. */
  1459. if (bex->fe_len == 0) {
  1460. *bex = *ex;
  1461. return;
  1462. }
  1463. /*
  1464. * If new found extent is better, store it in the context
  1465. */
  1466. if (bex->fe_len < gex->fe_len) {
  1467. /* if the request isn't satisfied, any found extent
  1468. * larger than previous best one is better */
  1469. if (ex->fe_len > bex->fe_len)
  1470. *bex = *ex;
  1471. } else if (ex->fe_len > gex->fe_len) {
  1472. /* if the request is satisfied, then we try to find
  1473. * an extent that still satisfy the request, but is
  1474. * smaller than previous one */
  1475. if (ex->fe_len < bex->fe_len)
  1476. *bex = *ex;
  1477. }
  1478. ext4_mb_check_limits(ac, e4b, 0);
  1479. }
  1480. static noinline_for_stack
  1481. int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
  1482. struct ext4_buddy *e4b)
  1483. {
  1484. struct ext4_free_extent ex = ac->ac_b_ex;
  1485. ext4_group_t group = ex.fe_group;
  1486. int max;
  1487. int err;
  1488. BUG_ON(ex.fe_len <= 0);
  1489. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1490. if (err)
  1491. return err;
  1492. ext4_lock_group(ac->ac_sb, group);
  1493. max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
  1494. if (max > 0) {
  1495. ac->ac_b_ex = ex;
  1496. ext4_mb_use_best_found(ac, e4b);
  1497. }
  1498. ext4_unlock_group(ac->ac_sb, group);
  1499. ext4_mb_unload_buddy(e4b);
  1500. return 0;
  1501. }
  1502. static noinline_for_stack
  1503. int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
  1504. struct ext4_buddy *e4b)
  1505. {
  1506. ext4_group_t group = ac->ac_g_ex.fe_group;
  1507. int max;
  1508. int err;
  1509. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  1510. struct ext4_free_extent ex;
  1511. if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
  1512. return 0;
  1513. err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
  1514. if (err)
  1515. return err;
  1516. ext4_lock_group(ac->ac_sb, group);
  1517. max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
  1518. ac->ac_g_ex.fe_len, &ex);
  1519. if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
  1520. ext4_fsblk_t start;
  1521. start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
  1522. ex.fe_start;
  1523. /* use do_div to get remainder (would be 64-bit modulo) */
  1524. if (do_div(start, sbi->s_stripe) == 0) {
  1525. ac->ac_found++;
  1526. ac->ac_b_ex = ex;
  1527. ext4_mb_use_best_found(ac, e4b);
  1528. }
  1529. } else if (max >= ac->ac_g_ex.fe_len) {
  1530. BUG_ON(ex.fe_len <= 0);
  1531. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1532. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1533. ac->ac_found++;
  1534. ac->ac_b_ex = ex;
  1535. ext4_mb_use_best_found(ac, e4b);
  1536. } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
  1537. /* Sometimes, caller may want to merge even small
  1538. * number of blocks to an existing extent */
  1539. BUG_ON(ex.fe_len <= 0);
  1540. BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
  1541. BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
  1542. ac->ac_found++;
  1543. ac->ac_b_ex = ex;
  1544. ext4_mb_use_best_found(ac, e4b);
  1545. }
  1546. ext4_unlock_group(ac->ac_sb, group);
  1547. ext4_mb_unload_buddy(e4b);
  1548. return 0;
  1549. }
  1550. /*
  1551. * The routine scans buddy structures (not bitmap!) from given order
  1552. * to max order and tries to find big enough chunk to satisfy the req
  1553. */
  1554. static noinline_for_stack
  1555. void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
  1556. struct ext4_buddy *e4b)
  1557. {
  1558. struct super_block *sb = ac->ac_sb;
  1559. struct ext4_group_info *grp = e4b->bd_info;
  1560. void *buddy;
  1561. int i;
  1562. int k;
  1563. int max;
  1564. BUG_ON(ac->ac_2order <= 0);
  1565. for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
  1566. if (grp->bb_counters[i] == 0)
  1567. continue;
  1568. buddy = mb_find_buddy(e4b, i, &max);
  1569. BUG_ON(buddy == NULL);
  1570. k = mb_find_next_zero_bit(buddy, max, 0);
  1571. BUG_ON(k >= max);
  1572. ac->ac_found++;
  1573. ac->ac_b_ex.fe_len = 1 << i;
  1574. ac->ac_b_ex.fe_start = k << i;
  1575. ac->ac_b_ex.fe_group = e4b->bd_group;
  1576. ext4_mb_use_best_found(ac, e4b);
  1577. BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
  1578. if (EXT4_SB(sb)->s_mb_stats)
  1579. atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
  1580. break;
  1581. }
  1582. }
  1583. /*
  1584. * The routine scans the group and measures all found extents.
  1585. * In order to optimize scanning, caller must pass number of
  1586. * free blocks in the group, so the routine can know upper limit.
  1587. */
  1588. static noinline_for_stack
  1589. void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
  1590. struct ext4_buddy *e4b)
  1591. {
  1592. struct super_block *sb = ac->ac_sb;
  1593. void *bitmap = EXT4_MB_BITMAP(e4b);
  1594. struct ext4_free_extent ex;
  1595. int i;
  1596. int free;
  1597. free = e4b->bd_info->bb_free;
  1598. BUG_ON(free <= 0);
  1599. i = e4b->bd_info->bb_first_free;
  1600. while (free && ac->ac_status == AC_STATUS_CONTINUE) {
  1601. i = mb_find_next_zero_bit(bitmap,
  1602. EXT4_BLOCKS_PER_GROUP(sb), i);
  1603. if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
  1604. /*
  1605. * IF we have corrupt bitmap, we won't find any
  1606. * free blocks even though group info says we
  1607. * we have free blocks
  1608. */
  1609. ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
  1610. "%d free blocks as per "
  1611. "group info. But bitmap says 0",
  1612. free);
  1613. break;
  1614. }
  1615. mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
  1616. BUG_ON(ex.fe_len <= 0);
  1617. if (free < ex.fe_len) {
  1618. ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
  1619. "%d free blocks as per "
  1620. "group info. But got %d blocks",
  1621. free, ex.fe_len);
  1622. /*
  1623. * The number of free blocks differs. This mostly
  1624. * indicate that the bitmap is corrupt. So exit
  1625. * without claiming the space.
  1626. */
  1627. break;
  1628. }
  1629. ext4_mb_measure_extent(ac, &ex, e4b);
  1630. i += ex.fe_len;
  1631. free -= ex.fe_len;
  1632. }
  1633. ext4_mb_check_limits(ac, e4b, 1);
  1634. }
  1635. /*
  1636. * This is a special case for storages like raid5
  1637. * we try to find stripe-aligned chunks for stripe-size-multiple requests
  1638. */
  1639. static noinline_for_stack
  1640. void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
  1641. struct ext4_buddy *e4b)
  1642. {
  1643. struct super_block *sb = ac->ac_sb;
  1644. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1645. void *bitmap = EXT4_MB_BITMAP(e4b);
  1646. struct ext4_free_extent ex;
  1647. ext4_fsblk_t first_group_block;
  1648. ext4_fsblk_t a;
  1649. ext4_grpblk_t i;
  1650. int max;
  1651. BUG_ON(sbi->s_stripe == 0);
  1652. /* find first stripe-aligned block in group */
  1653. first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
  1654. a = first_group_block + sbi->s_stripe - 1;
  1655. do_div(a, sbi->s_stripe);
  1656. i = (a * sbi->s_stripe) - first_group_block;
  1657. while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
  1658. if (!mb_test_bit(i, bitmap)) {
  1659. max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
  1660. if (max >= sbi->s_stripe) {
  1661. ac->ac_found++;
  1662. ac->ac_b_ex = ex;
  1663. ext4_mb_use_best_found(ac, e4b);
  1664. break;
  1665. }
  1666. }
  1667. i += sbi->s_stripe;
  1668. }
  1669. }
  1670. /* This is now called BEFORE we load the buddy bitmap. */
  1671. static int ext4_mb_good_group(struct ext4_allocation_context *ac,
  1672. ext4_group_t group, int cr)
  1673. {
  1674. unsigned free, fragments;
  1675. int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
  1676. struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
  1677. BUG_ON(cr < 0 || cr >= 4);
  1678. /* We only do this if the grp has never been initialized */
  1679. if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
  1680. int ret = ext4_mb_init_group(ac->ac_sb, group);
  1681. if (ret)
  1682. return 0;
  1683. }
  1684. free = grp->bb_free;
  1685. fragments = grp->bb_fragments;
  1686. if (free == 0)
  1687. return 0;
  1688. if (fragments == 0)
  1689. return 0;
  1690. switch (cr) {
  1691. case 0:
  1692. BUG_ON(ac->ac_2order == 0);
  1693. if (grp->bb_largest_free_order < ac->ac_2order)
  1694. return 0;
  1695. /* Avoid using the first bg of a flexgroup for data files */
  1696. if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
  1697. (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
  1698. ((group % flex_size) == 0))
  1699. return 0;
  1700. return 1;
  1701. case 1:
  1702. if ((free / fragments) >= ac->ac_g_ex.fe_len)
  1703. return 1;
  1704. break;
  1705. case 2:
  1706. if (free >= ac->ac_g_ex.fe_len)
  1707. return 1;
  1708. break;
  1709. case 3:
  1710. return 1;
  1711. default:
  1712. BUG();
  1713. }
  1714. return 0;
  1715. }
  1716. /*
  1717. * lock the group_info alloc_sem of all the groups
  1718. * belonging to the same buddy cache page. This
  1719. * make sure other parallel operation on the buddy
  1720. * cache doesn't happen whild holding the buddy cache
  1721. * lock
  1722. */
  1723. int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
  1724. {
  1725. int i;
  1726. int block, pnum;
  1727. int blocks_per_page;
  1728. int groups_per_page;
  1729. ext4_group_t ngroups = ext4_get_groups_count(sb);
  1730. ext4_group_t first_group;
  1731. struct ext4_group_info *grp;
  1732. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1733. /*
  1734. * the buddy cache inode stores the block bitmap
  1735. * and buddy information in consecutive blocks.
  1736. * So for each group we need two blocks.
  1737. */
  1738. block = group * 2;
  1739. pnum = block / blocks_per_page;
  1740. first_group = pnum * blocks_per_page / 2;
  1741. groups_per_page = blocks_per_page >> 1;
  1742. if (groups_per_page == 0)
  1743. groups_per_page = 1;
  1744. /* read all groups the page covers into the cache */
  1745. for (i = 0; i < groups_per_page; i++) {
  1746. if ((first_group + i) >= ngroups)
  1747. break;
  1748. grp = ext4_get_group_info(sb, first_group + i);
  1749. /* take all groups write allocation
  1750. * semaphore. This make sure there is
  1751. * no block allocation going on in any
  1752. * of that groups
  1753. */
  1754. down_write_nested(&grp->alloc_sem, i);
  1755. }
  1756. return i;
  1757. }
  1758. void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
  1759. ext4_group_t group, int locked_group)
  1760. {
  1761. int i;
  1762. int block, pnum;
  1763. int blocks_per_page;
  1764. ext4_group_t first_group;
  1765. struct ext4_group_info *grp;
  1766. blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
  1767. /*
  1768. * the buddy cache inode stores the block bitmap
  1769. * and buddy information in consecutive blocks.
  1770. * So for each group we need two blocks.
  1771. */
  1772. block = group * 2;
  1773. pnum = block / blocks_per_page;
  1774. first_group = pnum * blocks_per_page / 2;
  1775. /* release locks on all the groups */
  1776. for (i = 0; i < locked_group; i++) {
  1777. grp = ext4_get_group_info(sb, first_group + i);
  1778. /* take all groups write allocation
  1779. * semaphore. This make sure there is
  1780. * no block allocation going on in any
  1781. * of that groups
  1782. */
  1783. up_write(&grp->alloc_sem);
  1784. }
  1785. }
  1786. static noinline_for_stack int
  1787. ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
  1788. {
  1789. ext4_group_t ngroups, group, i;
  1790. int cr;
  1791. int err = 0;
  1792. struct ext4_sb_info *sbi;
  1793. struct super_block *sb;
  1794. struct ext4_buddy e4b;
  1795. sb = ac->ac_sb;
  1796. sbi = EXT4_SB(sb);
  1797. ngroups = ext4_get_groups_count(sb);
  1798. /* non-extent files are limited to low blocks/groups */
  1799. if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
  1800. ngroups = sbi->s_blockfile_groups;
  1801. BUG_ON(ac->ac_status == AC_STATUS_FOUND);
  1802. /* first, try the goal */
  1803. err = ext4_mb_find_by_goal(ac, &e4b);
  1804. if (err || ac->ac_status == AC_STATUS_FOUND)
  1805. goto out;
  1806. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  1807. goto out;
  1808. /*
  1809. * ac->ac2_order is set only if the fe_len is a power of 2
  1810. * if ac2_order is set we also set criteria to 0 so that we
  1811. * try exact allocation using buddy.
  1812. */
  1813. i = fls(ac->ac_g_ex.fe_len);
  1814. ac->ac_2order = 0;
  1815. /*
  1816. * We search using buddy data only if the order of the request
  1817. * is greater than equal to the sbi_s_mb_order2_reqs
  1818. * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
  1819. */
  1820. if (i >= sbi->s_mb_order2_reqs) {
  1821. /*
  1822. * This should tell if fe_len is exactly power of 2
  1823. */
  1824. if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
  1825. ac->ac_2order = i - 1;
  1826. }
  1827. /* if stream allocation is enabled, use global goal */
  1828. if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
  1829. /* TBD: may be hot point */
  1830. spin_lock(&sbi->s_md_lock);
  1831. ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
  1832. ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
  1833. spin_unlock(&sbi->s_md_lock);
  1834. }
  1835. /* Let's just scan groups to find more-less suitable blocks */
  1836. cr = ac->ac_2order ? 0 : 1;
  1837. /*
  1838. * cr == 0 try to get exact allocation,
  1839. * cr == 3 try to get anything
  1840. */
  1841. repeat:
  1842. for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
  1843. ac->ac_criteria = cr;
  1844. /*
  1845. * searching for the right group start
  1846. * from the goal value specified
  1847. */
  1848. group = ac->ac_g_ex.fe_group;
  1849. for (i = 0; i < ngroups; group++, i++) {
  1850. if (group == ngroups)
  1851. group = 0;
  1852. /* This now checks without needing the buddy page */
  1853. if (!ext4_mb_good_group(ac, group, cr))
  1854. continue;
  1855. err = ext4_mb_load_buddy(sb, group, &e4b);
  1856. if (err)
  1857. goto out;
  1858. ext4_lock_group(sb, group);
  1859. /*
  1860. * We need to check again after locking the
  1861. * block group
  1862. */
  1863. if (!ext4_mb_good_group(ac, group, cr)) {
  1864. ext4_unlock_group(sb, group);
  1865. ext4_mb_unload_buddy(&e4b);
  1866. continue;
  1867. }
  1868. ac->ac_groups_scanned++;
  1869. if (cr == 0)
  1870. ext4_mb_simple_scan_group(ac, &e4b);
  1871. else if (cr == 1 && sbi->s_stripe &&
  1872. !(ac->ac_g_ex.fe_len % sbi->s_stripe))
  1873. ext4_mb_scan_aligned(ac, &e4b);
  1874. else
  1875. ext4_mb_complex_scan_group(ac, &e4b);
  1876. ext4_unlock_group(sb, group);
  1877. ext4_mb_unload_buddy(&e4b);
  1878. if (ac->ac_status != AC_STATUS_CONTINUE)
  1879. break;
  1880. }
  1881. }
  1882. if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
  1883. !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
  1884. /*
  1885. * We've been searching too long. Let's try to allocate
  1886. * the best chunk we've found so far
  1887. */
  1888. ext4_mb_try_best_found(ac, &e4b);
  1889. if (ac->ac_status != AC_STATUS_FOUND) {
  1890. /*
  1891. * Someone more lucky has already allocated it.
  1892. * The only thing we can do is just take first
  1893. * found block(s)
  1894. printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
  1895. */
  1896. ac->ac_b_ex.fe_group = 0;
  1897. ac->ac_b_ex.fe_start = 0;
  1898. ac->ac_b_ex.fe_len = 0;
  1899. ac->ac_status = AC_STATUS_CONTINUE;
  1900. ac->ac_flags |= EXT4_MB_HINT_FIRST;
  1901. cr = 3;
  1902. atomic_inc(&sbi->s_mb_lost_chunks);
  1903. goto repeat;
  1904. }
  1905. }
  1906. out:
  1907. return err;
  1908. }
  1909. static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
  1910. {
  1911. struct super_block *sb = seq->private;
  1912. ext4_group_t group;
  1913. if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
  1914. return NULL;
  1915. group = *pos + 1;
  1916. return (void *) ((unsigned long) group);
  1917. }
  1918. static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
  1919. {
  1920. struct super_block *sb = seq->private;
  1921. ext4_group_t group;
  1922. ++*pos;
  1923. if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
  1924. return NULL;
  1925. group = *pos + 1;
  1926. return (void *) ((unsigned long) group);
  1927. }
  1928. static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
  1929. {
  1930. struct super_block *sb = seq->private;
  1931. ext4_group_t group = (ext4_group_t) ((unsigned long) v);
  1932. int i;
  1933. int err;
  1934. struct ext4_buddy e4b;
  1935. struct sg {
  1936. struct ext4_group_info info;
  1937. ext4_grpblk_t counters[16];
  1938. } sg;
  1939. group--;
  1940. if (group == 0)
  1941. seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
  1942. "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
  1943. "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
  1944. "group", "free", "frags", "first",
  1945. "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
  1946. "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
  1947. i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
  1948. sizeof(struct ext4_group_info);
  1949. err = ext4_mb_load_buddy(sb, group, &e4b);
  1950. if (err) {
  1951. seq_printf(seq, "#%-5u: I/O error\n", group);
  1952. return 0;
  1953. }
  1954. ext4_lock_group(sb, group);
  1955. memcpy(&sg, ext4_get_group_info(sb, group), i);
  1956. ext4_unlock_group(sb, group);
  1957. ext4_mb_unload_buddy(&e4b);
  1958. seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
  1959. sg.info.bb_fragments, sg.info.bb_first_free);
  1960. for (i = 0; i <= 13; i++)
  1961. seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
  1962. sg.info.bb_counters[i] : 0);
  1963. seq_printf(seq, " ]\n");
  1964. return 0;
  1965. }
  1966. static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
  1967. {
  1968. }
  1969. static const struct seq_operations ext4_mb_seq_groups_ops = {
  1970. .start = ext4_mb_seq_groups_start,
  1971. .next = ext4_mb_seq_groups_next,
  1972. .stop = ext4_mb_seq_groups_stop,
  1973. .show = ext4_mb_seq_groups_show,
  1974. };
  1975. static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
  1976. {
  1977. struct super_block *sb = PDE(inode)->data;
  1978. int rc;
  1979. rc = seq_open(file, &ext4_mb_seq_groups_ops);
  1980. if (rc == 0) {
  1981. struct seq_file *m = file->private_data;
  1982. m->private = sb;
  1983. }
  1984. return rc;
  1985. }
  1986. static const struct file_operations ext4_mb_seq_groups_fops = {
  1987. .owner = THIS_MODULE,
  1988. .open = ext4_mb_seq_groups_open,
  1989. .read = seq_read,
  1990. .llseek = seq_lseek,
  1991. .release = seq_release,
  1992. };
  1993. static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
  1994. {
  1995. int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
  1996. struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
  1997. BUG_ON(!cachep);
  1998. return cachep;
  1999. }
  2000. /* Create and initialize ext4_group_info data for the given group. */
  2001. int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
  2002. struct ext4_group_desc *desc)
  2003. {
  2004. int i;
  2005. int metalen = 0;
  2006. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2007. struct ext4_group_info **meta_group_info;
  2008. struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
  2009. /*
  2010. * First check if this group is the first of a reserved block.
  2011. * If it's true, we have to allocate a new table of pointers
  2012. * to ext4_group_info structures
  2013. */
  2014. if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
  2015. metalen = sizeof(*meta_group_info) <<
  2016. EXT4_DESC_PER_BLOCK_BITS(sb);
  2017. meta_group_info = kmalloc(metalen, GFP_KERNEL);
  2018. if (meta_group_info == NULL) {
  2019. printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
  2020. "buddy group\n");
  2021. goto exit_meta_group_info;
  2022. }
  2023. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
  2024. meta_group_info;
  2025. }
  2026. meta_group_info =
  2027. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
  2028. i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
  2029. meta_group_info[i] = kmem_cache_alloc(cachep, GFP_KERNEL);
  2030. if (meta_group_info[i] == NULL) {
  2031. printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
  2032. goto exit_group_info;
  2033. }
  2034. memset(meta_group_info[i], 0, kmem_cache_size(cachep));
  2035. set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
  2036. &(meta_group_info[i]->bb_state));
  2037. /*
  2038. * initialize bb_free to be able to skip
  2039. * empty groups without initialization
  2040. */
  2041. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2042. meta_group_info[i]->bb_free =
  2043. ext4_free_blocks_after_init(sb, group, desc);
  2044. } else {
  2045. meta_group_info[i]->bb_free =
  2046. ext4_free_blks_count(sb, desc);
  2047. }
  2048. INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
  2049. init_rwsem(&meta_group_info[i]->alloc_sem);
  2050. meta_group_info[i]->bb_free_root = RB_ROOT;
  2051. meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
  2052. #ifdef DOUBLE_CHECK
  2053. {
  2054. struct buffer_head *bh;
  2055. meta_group_info[i]->bb_bitmap =
  2056. kmalloc(sb->s_blocksize, GFP_KERNEL);
  2057. BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
  2058. bh = ext4_read_block_bitmap(sb, group);
  2059. BUG_ON(bh == NULL);
  2060. memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
  2061. sb->s_blocksize);
  2062. put_bh(bh);
  2063. }
  2064. #endif
  2065. return 0;
  2066. exit_group_info:
  2067. /* If a meta_group_info table has been allocated, release it now */
  2068. if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
  2069. kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
  2070. exit_meta_group_info:
  2071. return -ENOMEM;
  2072. } /* ext4_mb_add_groupinfo */
  2073. static int ext4_mb_init_backend(struct super_block *sb)
  2074. {
  2075. ext4_group_t ngroups = ext4_get_groups_count(sb);
  2076. ext4_group_t i;
  2077. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2078. struct ext4_super_block *es = sbi->s_es;
  2079. int num_meta_group_infos;
  2080. int num_meta_group_infos_max;
  2081. int array_size;
  2082. struct ext4_group_desc *desc;
  2083. struct kmem_cache *cachep;
  2084. /* This is the number of blocks used by GDT */
  2085. num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
  2086. 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
  2087. /*
  2088. * This is the total number of blocks used by GDT including
  2089. * the number of reserved blocks for GDT.
  2090. * The s_group_info array is allocated with this value
  2091. * to allow a clean online resize without a complex
  2092. * manipulation of pointer.
  2093. * The drawback is the unused memory when no resize
  2094. * occurs but it's very low in terms of pages
  2095. * (see comments below)
  2096. * Need to handle this properly when META_BG resizing is allowed
  2097. */
  2098. num_meta_group_infos_max = num_meta_group_infos +
  2099. le16_to_cpu(es->s_reserved_gdt_blocks);
  2100. /*
  2101. * array_size is the size of s_group_info array. We round it
  2102. * to the next power of two because this approximation is done
  2103. * internally by kmalloc so we can have some more memory
  2104. * for free here (e.g. may be used for META_BG resize).
  2105. */
  2106. array_size = 1;
  2107. while (array_size < sizeof(*sbi->s_group_info) *
  2108. num_meta_group_infos_max)
  2109. array_size = array_size << 1;
  2110. /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
  2111. * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
  2112. * So a two level scheme suffices for now. */
  2113. sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
  2114. if (sbi->s_group_info == NULL) {
  2115. printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
  2116. return -ENOMEM;
  2117. }
  2118. sbi->s_buddy_cache = new_inode(sb);
  2119. if (sbi->s_buddy_cache == NULL) {
  2120. printk(KERN_ERR "EXT4-fs: can't get new inode\n");
  2121. goto err_freesgi;
  2122. }
  2123. EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
  2124. for (i = 0; i < ngroups; i++) {
  2125. desc = ext4_get_group_desc(sb, i, NULL);
  2126. if (desc == NULL) {
  2127. printk(KERN_ERR
  2128. "EXT4-fs: can't read descriptor %u\n", i);
  2129. goto err_freebuddy;
  2130. }
  2131. if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
  2132. goto err_freebuddy;
  2133. }
  2134. return 0;
  2135. err_freebuddy:
  2136. cachep = get_groupinfo_cache(sb->s_blocksize_bits);
  2137. while (i-- > 0)
  2138. kmem_cache_free(cachep, ext4_get_group_info(sb, i));
  2139. i = num_meta_group_infos;
  2140. while (i-- > 0)
  2141. kfree(sbi->s_group_info[i]);
  2142. iput(sbi->s_buddy_cache);
  2143. err_freesgi:
  2144. kfree(sbi->s_group_info);
  2145. return -ENOMEM;
  2146. }
  2147. int ext4_mb_init(struct super_block *sb, int needs_recovery)
  2148. {
  2149. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2150. unsigned i, j;
  2151. unsigned offset;
  2152. unsigned max;
  2153. int ret;
  2154. int cache_index;
  2155. struct kmem_cache *cachep;
  2156. char *namep = NULL;
  2157. i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
  2158. sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
  2159. if (sbi->s_mb_offsets == NULL) {
  2160. ret = -ENOMEM;
  2161. goto out;
  2162. }
  2163. i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
  2164. sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
  2165. if (sbi->s_mb_maxs == NULL) {
  2166. ret = -ENOMEM;
  2167. goto out;
  2168. }
  2169. cache_index = sb->s_blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
  2170. cachep = ext4_groupinfo_caches[cache_index];
  2171. if (!cachep) {
  2172. char name[32];
  2173. int len = offsetof(struct ext4_group_info,
  2174. bb_counters[sb->s_blocksize_bits + 2]);
  2175. sprintf(name, "ext4_groupinfo_%d", sb->s_blocksize_bits);
  2176. namep = kstrdup(name, GFP_KERNEL);
  2177. if (!namep) {
  2178. ret = -ENOMEM;
  2179. goto out;
  2180. }
  2181. /* Need to free the kmem_cache_name() when we
  2182. * destroy the slab */
  2183. cachep = kmem_cache_create(namep, len, 0,
  2184. SLAB_RECLAIM_ACCOUNT, NULL);
  2185. if (!cachep) {
  2186. ret = -ENOMEM;
  2187. goto out;
  2188. }
  2189. ext4_groupinfo_caches[cache_index] = cachep;
  2190. }
  2191. /* order 0 is regular bitmap */
  2192. sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
  2193. sbi->s_mb_offsets[0] = 0;
  2194. i = 1;
  2195. offset = 0;
  2196. max = sb->s_blocksize << 2;
  2197. do {
  2198. sbi->s_mb_offsets[i] = offset;
  2199. sbi->s_mb_maxs[i] = max;
  2200. offset += 1 << (sb->s_blocksize_bits - i);
  2201. max = max >> 1;
  2202. i++;
  2203. } while (i <= sb->s_blocksize_bits + 1);
  2204. /* init file for buddy data */
  2205. ret = ext4_mb_init_backend(sb);
  2206. if (ret != 0) {
  2207. goto out;
  2208. }
  2209. spin_lock_init(&sbi->s_md_lock);
  2210. spin_lock_init(&sbi->s_bal_lock);
  2211. sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
  2212. sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
  2213. sbi->s_mb_stats = MB_DEFAULT_STATS;
  2214. sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
  2215. sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
  2216. sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
  2217. sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
  2218. if (sbi->s_locality_groups == NULL) {
  2219. ret = -ENOMEM;
  2220. goto out;
  2221. }
  2222. for_each_possible_cpu(i) {
  2223. struct ext4_locality_group *lg;
  2224. lg = per_cpu_ptr(sbi->s_locality_groups, i);
  2225. mutex_init(&lg->lg_mutex);
  2226. for (j = 0; j < PREALLOC_TB_SIZE; j++)
  2227. INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
  2228. spin_lock_init(&lg->lg_prealloc_lock);
  2229. }
  2230. if (sbi->s_proc)
  2231. proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
  2232. &ext4_mb_seq_groups_fops, sb);
  2233. if (sbi->s_journal)
  2234. sbi->s_journal->j_commit_callback = release_blocks_on_commit;
  2235. out:
  2236. if (ret) {
  2237. kfree(sbi->s_mb_offsets);
  2238. kfree(sbi->s_mb_maxs);
  2239. kfree(namep);
  2240. }
  2241. return ret;
  2242. }
  2243. /* need to called with the ext4 group lock held */
  2244. static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
  2245. {
  2246. struct ext4_prealloc_space *pa;
  2247. struct list_head *cur, *tmp;
  2248. int count = 0;
  2249. list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
  2250. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  2251. list_del(&pa->pa_group_list);
  2252. count++;
  2253. kmem_cache_free(ext4_pspace_cachep, pa);
  2254. }
  2255. if (count)
  2256. mb_debug(1, "mballoc: %u PAs left\n", count);
  2257. }
  2258. int ext4_mb_release(struct super_block *sb)
  2259. {
  2260. ext4_group_t ngroups = ext4_get_groups_count(sb);
  2261. ext4_group_t i;
  2262. int num_meta_group_infos;
  2263. struct ext4_group_info *grinfo;
  2264. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2265. struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
  2266. if (sbi->s_group_info) {
  2267. for (i = 0; i < ngroups; i++) {
  2268. grinfo = ext4_get_group_info(sb, i);
  2269. #ifdef DOUBLE_CHECK
  2270. kfree(grinfo->bb_bitmap);
  2271. #endif
  2272. ext4_lock_group(sb, i);
  2273. ext4_mb_cleanup_pa(grinfo);
  2274. ext4_unlock_group(sb, i);
  2275. kmem_cache_free(cachep, grinfo);
  2276. }
  2277. num_meta_group_infos = (ngroups +
  2278. EXT4_DESC_PER_BLOCK(sb) - 1) >>
  2279. EXT4_DESC_PER_BLOCK_BITS(sb);
  2280. for (i = 0; i < num_meta_group_infos; i++)
  2281. kfree(sbi->s_group_info[i]);
  2282. kfree(sbi->s_group_info);
  2283. }
  2284. kfree(sbi->s_mb_offsets);
  2285. kfree(sbi->s_mb_maxs);
  2286. if (sbi->s_buddy_cache)
  2287. iput(sbi->s_buddy_cache);
  2288. if (sbi->s_mb_stats) {
  2289. printk(KERN_INFO
  2290. "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
  2291. atomic_read(&sbi->s_bal_allocated),
  2292. atomic_read(&sbi->s_bal_reqs),
  2293. atomic_read(&sbi->s_bal_success));
  2294. printk(KERN_INFO
  2295. "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
  2296. "%u 2^N hits, %u breaks, %u lost\n",
  2297. atomic_read(&sbi->s_bal_ex_scanned),
  2298. atomic_read(&sbi->s_bal_goals),
  2299. atomic_read(&sbi->s_bal_2orders),
  2300. atomic_read(&sbi->s_bal_breaks),
  2301. atomic_read(&sbi->s_mb_lost_chunks));
  2302. printk(KERN_INFO
  2303. "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
  2304. sbi->s_mb_buddies_generated++,
  2305. sbi->s_mb_generation_time);
  2306. printk(KERN_INFO
  2307. "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
  2308. atomic_read(&sbi->s_mb_preallocated),
  2309. atomic_read(&sbi->s_mb_discarded));
  2310. }
  2311. free_percpu(sbi->s_locality_groups);
  2312. if (sbi->s_proc)
  2313. remove_proc_entry("mb_groups", sbi->s_proc);
  2314. return 0;
  2315. }
  2316. static inline void ext4_issue_discard(struct super_block *sb,
  2317. ext4_group_t block_group, ext4_grpblk_t block, int count)
  2318. {
  2319. int ret;
  2320. ext4_fsblk_t discard_block;
  2321. discard_block = block + ext4_group_first_block_no(sb, block_group);
  2322. trace_ext4_discard_blocks(sb,
  2323. (unsigned long long) discard_block, count);
  2324. ret = sb_issue_discard(sb, discard_block, count);
  2325. if (ret == EOPNOTSUPP) {
  2326. ext4_warning(sb, "discard not supported, disabling");
  2327. clear_opt(EXT4_SB(sb)->s_mount_opt, DISCARD);
  2328. }
  2329. }
  2330. /*
  2331. * This function is called by the jbd2 layer once the commit has finished,
  2332. * so we know we can free the blocks that were released with that commit.
  2333. */
  2334. static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
  2335. {
  2336. struct super_block *sb = journal->j_private;
  2337. struct ext4_buddy e4b;
  2338. struct ext4_group_info *db;
  2339. int err, count = 0, count2 = 0;
  2340. struct ext4_free_data *entry;
  2341. struct list_head *l, *ltmp;
  2342. list_for_each_safe(l, ltmp, &txn->t_private_list) {
  2343. entry = list_entry(l, struct ext4_free_data, list);
  2344. mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
  2345. entry->count, entry->group, entry);
  2346. if (test_opt(sb, DISCARD))
  2347. ext4_issue_discard(sb, entry->group,
  2348. entry->start_blk, entry->count);
  2349. err = ext4_mb_load_buddy(sb, entry->group, &e4b);
  2350. /* we expect to find existing buddy because it's pinned */
  2351. BUG_ON(err != 0);
  2352. db = e4b.bd_info;
  2353. /* there are blocks to put in buddy to make them really free */
  2354. count += entry->count;
  2355. count2++;
  2356. ext4_lock_group(sb, entry->group);
  2357. /* Take it out of per group rb tree */
  2358. rb_erase(&entry->node, &(db->bb_free_root));
  2359. mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
  2360. if (!db->bb_free_root.rb_node) {
  2361. /* No more items in the per group rb tree
  2362. * balance refcounts from ext4_mb_free_metadata()
  2363. */
  2364. page_cache_release(e4b.bd_buddy_page);
  2365. page_cache_release(e4b.bd_bitmap_page);
  2366. }
  2367. ext4_unlock_group(sb, entry->group);
  2368. kmem_cache_free(ext4_free_ext_cachep, entry);
  2369. ext4_mb_unload_buddy(&e4b);
  2370. }
  2371. mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
  2372. }
  2373. #ifdef CONFIG_EXT4_DEBUG
  2374. u8 mb_enable_debug __read_mostly;
  2375. static struct dentry *debugfs_dir;
  2376. static struct dentry *debugfs_debug;
  2377. static void __init ext4_create_debugfs_entry(void)
  2378. {
  2379. debugfs_dir = debugfs_create_dir("ext4", NULL);
  2380. if (debugfs_dir)
  2381. debugfs_debug = debugfs_create_u8("mballoc-debug",
  2382. S_IRUGO | S_IWUSR,
  2383. debugfs_dir,
  2384. &mb_enable_debug);
  2385. }
  2386. static void ext4_remove_debugfs_entry(void)
  2387. {
  2388. debugfs_remove(debugfs_debug);
  2389. debugfs_remove(debugfs_dir);
  2390. }
  2391. #else
  2392. static void __init ext4_create_debugfs_entry(void)
  2393. {
  2394. }
  2395. static void ext4_remove_debugfs_entry(void)
  2396. {
  2397. }
  2398. #endif
  2399. int __init init_ext4_mballoc(void)
  2400. {
  2401. ext4_pspace_cachep =
  2402. kmem_cache_create("ext4_prealloc_space",
  2403. sizeof(struct ext4_prealloc_space),
  2404. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2405. if (ext4_pspace_cachep == NULL)
  2406. return -ENOMEM;
  2407. ext4_ac_cachep =
  2408. kmem_cache_create("ext4_alloc_context",
  2409. sizeof(struct ext4_allocation_context),
  2410. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2411. if (ext4_ac_cachep == NULL) {
  2412. kmem_cache_destroy(ext4_pspace_cachep);
  2413. return -ENOMEM;
  2414. }
  2415. ext4_free_ext_cachep =
  2416. kmem_cache_create("ext4_free_block_extents",
  2417. sizeof(struct ext4_free_data),
  2418. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2419. if (ext4_free_ext_cachep == NULL) {
  2420. kmem_cache_destroy(ext4_pspace_cachep);
  2421. kmem_cache_destroy(ext4_ac_cachep);
  2422. return -ENOMEM;
  2423. }
  2424. ext4_create_debugfs_entry();
  2425. return 0;
  2426. }
  2427. void exit_ext4_mballoc(void)
  2428. {
  2429. int i;
  2430. /*
  2431. * Wait for completion of call_rcu()'s on ext4_pspace_cachep
  2432. * before destroying the slab cache.
  2433. */
  2434. rcu_barrier();
  2435. kmem_cache_destroy(ext4_pspace_cachep);
  2436. kmem_cache_destroy(ext4_ac_cachep);
  2437. kmem_cache_destroy(ext4_free_ext_cachep);
  2438. for (i = 0; i < NR_GRPINFO_CACHES; i++) {
  2439. struct kmem_cache *cachep = ext4_groupinfo_caches[i];
  2440. if (cachep) {
  2441. char *name = (char *)kmem_cache_name(cachep);
  2442. kmem_cache_destroy(cachep);
  2443. kfree(name);
  2444. }
  2445. }
  2446. ext4_remove_debugfs_entry();
  2447. }
  2448. /*
  2449. * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
  2450. * Returns 0 if success or error code
  2451. */
  2452. static noinline_for_stack int
  2453. ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
  2454. handle_t *handle, unsigned int reserv_blks)
  2455. {
  2456. struct buffer_head *bitmap_bh = NULL;
  2457. struct ext4_group_desc *gdp;
  2458. struct buffer_head *gdp_bh;
  2459. struct ext4_sb_info *sbi;
  2460. struct super_block *sb;
  2461. ext4_fsblk_t block;
  2462. int err, len;
  2463. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  2464. BUG_ON(ac->ac_b_ex.fe_len <= 0);
  2465. sb = ac->ac_sb;
  2466. sbi = EXT4_SB(sb);
  2467. err = -EIO;
  2468. bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
  2469. if (!bitmap_bh)
  2470. goto out_err;
  2471. err = ext4_journal_get_write_access(handle, bitmap_bh);
  2472. if (err)
  2473. goto out_err;
  2474. err = -EIO;
  2475. gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
  2476. if (!gdp)
  2477. goto out_err;
  2478. ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
  2479. ext4_free_blks_count(sb, gdp));
  2480. err = ext4_journal_get_write_access(handle, gdp_bh);
  2481. if (err)
  2482. goto out_err;
  2483. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  2484. len = ac->ac_b_ex.fe_len;
  2485. if (!ext4_data_block_valid(sbi, block, len)) {
  2486. ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
  2487. "fs metadata\n", block, block+len);
  2488. /* File system mounted not to panic on error
  2489. * Fix the bitmap and repeat the block allocation
  2490. * We leak some of the blocks here.
  2491. */
  2492. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2493. mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
  2494. ac->ac_b_ex.fe_len);
  2495. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2496. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2497. if (!err)
  2498. err = -EAGAIN;
  2499. goto out_err;
  2500. }
  2501. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2502. #ifdef AGGRESSIVE_CHECK
  2503. {
  2504. int i;
  2505. for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
  2506. BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
  2507. bitmap_bh->b_data));
  2508. }
  2509. }
  2510. #endif
  2511. mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,ac->ac_b_ex.fe_len);
  2512. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2513. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  2514. ext4_free_blks_set(sb, gdp,
  2515. ext4_free_blocks_after_init(sb,
  2516. ac->ac_b_ex.fe_group, gdp));
  2517. }
  2518. len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
  2519. ext4_free_blks_set(sb, gdp, len);
  2520. gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
  2521. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2522. percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
  2523. /*
  2524. * Now reduce the dirty block count also. Should not go negative
  2525. */
  2526. if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
  2527. /* release all the reserved blocks if non delalloc */
  2528. percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
  2529. if (sbi->s_log_groups_per_flex) {
  2530. ext4_group_t flex_group = ext4_flex_group(sbi,
  2531. ac->ac_b_ex.fe_group);
  2532. atomic_sub(ac->ac_b_ex.fe_len,
  2533. &sbi->s_flex_groups[flex_group].free_blocks);
  2534. }
  2535. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2536. if (err)
  2537. goto out_err;
  2538. err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
  2539. out_err:
  2540. ext4_mark_super_dirty(sb);
  2541. brelse(bitmap_bh);
  2542. return err;
  2543. }
  2544. /*
  2545. * here we normalize request for locality group
  2546. * Group request are normalized to s_strip size if we set the same via mount
  2547. * option. If not we set it to s_mb_group_prealloc which can be configured via
  2548. * /sys/fs/ext4/<partition>/mb_group_prealloc
  2549. *
  2550. * XXX: should we try to preallocate more than the group has now?
  2551. */
  2552. static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
  2553. {
  2554. struct super_block *sb = ac->ac_sb;
  2555. struct ext4_locality_group *lg = ac->ac_lg;
  2556. BUG_ON(lg == NULL);
  2557. if (EXT4_SB(sb)->s_stripe)
  2558. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
  2559. else
  2560. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
  2561. mb_debug(1, "#%u: goal %u blocks for locality group\n",
  2562. current->pid, ac->ac_g_ex.fe_len);
  2563. }
  2564. /*
  2565. * Normalization means making request better in terms of
  2566. * size and alignment
  2567. */
  2568. static noinline_for_stack void
  2569. ext4_mb_normalize_request(struct ext4_allocation_context *ac,
  2570. struct ext4_allocation_request *ar)
  2571. {
  2572. int bsbits, max;
  2573. ext4_lblk_t end;
  2574. loff_t size, orig_size, start_off;
  2575. ext4_lblk_t start;
  2576. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2577. struct ext4_prealloc_space *pa;
  2578. /* do normalize only data requests, metadata requests
  2579. do not need preallocation */
  2580. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2581. return;
  2582. /* sometime caller may want exact blocks */
  2583. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  2584. return;
  2585. /* caller may indicate that preallocation isn't
  2586. * required (it's a tail, for example) */
  2587. if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
  2588. return;
  2589. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
  2590. ext4_mb_normalize_group_request(ac);
  2591. return ;
  2592. }
  2593. bsbits = ac->ac_sb->s_blocksize_bits;
  2594. /* first, let's learn actual file size
  2595. * given current request is allocated */
  2596. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  2597. size = size << bsbits;
  2598. if (size < i_size_read(ac->ac_inode))
  2599. size = i_size_read(ac->ac_inode);
  2600. orig_size = size;
  2601. /* max size of free chunks */
  2602. max = 2 << bsbits;
  2603. #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
  2604. (req <= (size) || max <= (chunk_size))
  2605. /* first, try to predict filesize */
  2606. /* XXX: should this table be tunable? */
  2607. start_off = 0;
  2608. if (size <= 16 * 1024) {
  2609. size = 16 * 1024;
  2610. } else if (size <= 32 * 1024) {
  2611. size = 32 * 1024;
  2612. } else if (size <= 64 * 1024) {
  2613. size = 64 * 1024;
  2614. } else if (size <= 128 * 1024) {
  2615. size = 128 * 1024;
  2616. } else if (size <= 256 * 1024) {
  2617. size = 256 * 1024;
  2618. } else if (size <= 512 * 1024) {
  2619. size = 512 * 1024;
  2620. } else if (size <= 1024 * 1024) {
  2621. size = 1024 * 1024;
  2622. } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
  2623. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2624. (21 - bsbits)) << 21;
  2625. size = 2 * 1024 * 1024;
  2626. } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
  2627. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2628. (22 - bsbits)) << 22;
  2629. size = 4 * 1024 * 1024;
  2630. } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
  2631. (8<<20)>>bsbits, max, 8 * 1024)) {
  2632. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2633. (23 - bsbits)) << 23;
  2634. size = 8 * 1024 * 1024;
  2635. } else {
  2636. start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
  2637. size = ac->ac_o_ex.fe_len << bsbits;
  2638. }
  2639. size = size >> bsbits;
  2640. start = start_off >> bsbits;
  2641. /* don't cover already allocated blocks in selected range */
  2642. if (ar->pleft && start <= ar->lleft) {
  2643. size -= ar->lleft + 1 - start;
  2644. start = ar->lleft + 1;
  2645. }
  2646. if (ar->pright && start + size - 1 >= ar->lright)
  2647. size -= start + size - ar->lright;
  2648. end = start + size;
  2649. /* check we don't cross already preallocated blocks */
  2650. rcu_read_lock();
  2651. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2652. ext4_lblk_t pa_end;
  2653. if (pa->pa_deleted)
  2654. continue;
  2655. spin_lock(&pa->pa_lock);
  2656. if (pa->pa_deleted) {
  2657. spin_unlock(&pa->pa_lock);
  2658. continue;
  2659. }
  2660. pa_end = pa->pa_lstart + pa->pa_len;
  2661. /* PA must not overlap original request */
  2662. BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
  2663. ac->ac_o_ex.fe_logical < pa->pa_lstart));
  2664. /* skip PAs this normalized request doesn't overlap with */
  2665. if (pa->pa_lstart >= end || pa_end <= start) {
  2666. spin_unlock(&pa->pa_lock);
  2667. continue;
  2668. }
  2669. BUG_ON(pa->pa_lstart <= start && pa_end >= end);
  2670. /* adjust start or end to be adjacent to this pa */
  2671. if (pa_end <= ac->ac_o_ex.fe_logical) {
  2672. BUG_ON(pa_end < start);
  2673. start = pa_end;
  2674. } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
  2675. BUG_ON(pa->pa_lstart > end);
  2676. end = pa->pa_lstart;
  2677. }
  2678. spin_unlock(&pa->pa_lock);
  2679. }
  2680. rcu_read_unlock();
  2681. size = end - start;
  2682. /* XXX: extra loop to check we really don't overlap preallocations */
  2683. rcu_read_lock();
  2684. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2685. ext4_lblk_t pa_end;
  2686. spin_lock(&pa->pa_lock);
  2687. if (pa->pa_deleted == 0) {
  2688. pa_end = pa->pa_lstart + pa->pa_len;
  2689. BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
  2690. }
  2691. spin_unlock(&pa->pa_lock);
  2692. }
  2693. rcu_read_unlock();
  2694. if (start + size <= ac->ac_o_ex.fe_logical &&
  2695. start > ac->ac_o_ex.fe_logical) {
  2696. printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
  2697. (unsigned long) start, (unsigned long) size,
  2698. (unsigned long) ac->ac_o_ex.fe_logical);
  2699. }
  2700. BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
  2701. start > ac->ac_o_ex.fe_logical);
  2702. BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  2703. /* now prepare goal request */
  2704. /* XXX: is it better to align blocks WRT to logical
  2705. * placement or satisfy big request as is */
  2706. ac->ac_g_ex.fe_logical = start;
  2707. ac->ac_g_ex.fe_len = size;
  2708. /* define goal start in order to merge */
  2709. if (ar->pright && (ar->lright == (start + size))) {
  2710. /* merge to the right */
  2711. ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
  2712. &ac->ac_f_ex.fe_group,
  2713. &ac->ac_f_ex.fe_start);
  2714. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2715. }
  2716. if (ar->pleft && (ar->lleft + 1 == start)) {
  2717. /* merge to the left */
  2718. ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
  2719. &ac->ac_f_ex.fe_group,
  2720. &ac->ac_f_ex.fe_start);
  2721. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2722. }
  2723. mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
  2724. (unsigned) orig_size, (unsigned) start);
  2725. }
  2726. static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
  2727. {
  2728. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2729. if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
  2730. atomic_inc(&sbi->s_bal_reqs);
  2731. atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
  2732. if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
  2733. atomic_inc(&sbi->s_bal_success);
  2734. atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
  2735. if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
  2736. ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
  2737. atomic_inc(&sbi->s_bal_goals);
  2738. if (ac->ac_found > sbi->s_mb_max_to_scan)
  2739. atomic_inc(&sbi->s_bal_breaks);
  2740. }
  2741. if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
  2742. trace_ext4_mballoc_alloc(ac);
  2743. else
  2744. trace_ext4_mballoc_prealloc(ac);
  2745. }
  2746. /*
  2747. * Called on failure; free up any blocks from the inode PA for this
  2748. * context. We don't need this for MB_GROUP_PA because we only change
  2749. * pa_free in ext4_mb_release_context(), but on failure, we've already
  2750. * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
  2751. */
  2752. static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
  2753. {
  2754. struct ext4_prealloc_space *pa = ac->ac_pa;
  2755. int len;
  2756. if (pa && pa->pa_type == MB_INODE_PA) {
  2757. len = ac->ac_b_ex.fe_len;
  2758. pa->pa_free += len;
  2759. }
  2760. }
  2761. /*
  2762. * use blocks preallocated to inode
  2763. */
  2764. static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
  2765. struct ext4_prealloc_space *pa)
  2766. {
  2767. ext4_fsblk_t start;
  2768. ext4_fsblk_t end;
  2769. int len;
  2770. /* found preallocated blocks, use them */
  2771. start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
  2772. end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
  2773. len = end - start;
  2774. ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
  2775. &ac->ac_b_ex.fe_start);
  2776. ac->ac_b_ex.fe_len = len;
  2777. ac->ac_status = AC_STATUS_FOUND;
  2778. ac->ac_pa = pa;
  2779. BUG_ON(start < pa->pa_pstart);
  2780. BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
  2781. BUG_ON(pa->pa_free < len);
  2782. pa->pa_free -= len;
  2783. mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
  2784. }
  2785. /*
  2786. * use blocks preallocated to locality group
  2787. */
  2788. static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
  2789. struct ext4_prealloc_space *pa)
  2790. {
  2791. unsigned int len = ac->ac_o_ex.fe_len;
  2792. ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
  2793. &ac->ac_b_ex.fe_group,
  2794. &ac->ac_b_ex.fe_start);
  2795. ac->ac_b_ex.fe_len = len;
  2796. ac->ac_status = AC_STATUS_FOUND;
  2797. ac->ac_pa = pa;
  2798. /* we don't correct pa_pstart or pa_plen here to avoid
  2799. * possible race when the group is being loaded concurrently
  2800. * instead we correct pa later, after blocks are marked
  2801. * in on-disk bitmap -- see ext4_mb_release_context()
  2802. * Other CPUs are prevented from allocating from this pa by lg_mutex
  2803. */
  2804. mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
  2805. }
  2806. /*
  2807. * Return the prealloc space that have minimal distance
  2808. * from the goal block. @cpa is the prealloc
  2809. * space that is having currently known minimal distance
  2810. * from the goal block.
  2811. */
  2812. static struct ext4_prealloc_space *
  2813. ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
  2814. struct ext4_prealloc_space *pa,
  2815. struct ext4_prealloc_space *cpa)
  2816. {
  2817. ext4_fsblk_t cur_distance, new_distance;
  2818. if (cpa == NULL) {
  2819. atomic_inc(&pa->pa_count);
  2820. return pa;
  2821. }
  2822. cur_distance = abs(goal_block - cpa->pa_pstart);
  2823. new_distance = abs(goal_block - pa->pa_pstart);
  2824. if (cur_distance < new_distance)
  2825. return cpa;
  2826. /* drop the previous reference */
  2827. atomic_dec(&cpa->pa_count);
  2828. atomic_inc(&pa->pa_count);
  2829. return pa;
  2830. }
  2831. /*
  2832. * search goal blocks in preallocated space
  2833. */
  2834. static noinline_for_stack int
  2835. ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
  2836. {
  2837. int order, i;
  2838. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2839. struct ext4_locality_group *lg;
  2840. struct ext4_prealloc_space *pa, *cpa = NULL;
  2841. ext4_fsblk_t goal_block;
  2842. /* only data can be preallocated */
  2843. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2844. return 0;
  2845. /* first, try per-file preallocation */
  2846. rcu_read_lock();
  2847. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2848. /* all fields in this condition don't change,
  2849. * so we can skip locking for them */
  2850. if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
  2851. ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
  2852. continue;
  2853. /* non-extent files can't have physical blocks past 2^32 */
  2854. if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
  2855. pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
  2856. continue;
  2857. /* found preallocated blocks, use them */
  2858. spin_lock(&pa->pa_lock);
  2859. if (pa->pa_deleted == 0 && pa->pa_free) {
  2860. atomic_inc(&pa->pa_count);
  2861. ext4_mb_use_inode_pa(ac, pa);
  2862. spin_unlock(&pa->pa_lock);
  2863. ac->ac_criteria = 10;
  2864. rcu_read_unlock();
  2865. return 1;
  2866. }
  2867. spin_unlock(&pa->pa_lock);
  2868. }
  2869. rcu_read_unlock();
  2870. /* can we use group allocation? */
  2871. if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
  2872. return 0;
  2873. /* inode may have no locality group for some reason */
  2874. lg = ac->ac_lg;
  2875. if (lg == NULL)
  2876. return 0;
  2877. order = fls(ac->ac_o_ex.fe_len) - 1;
  2878. if (order > PREALLOC_TB_SIZE - 1)
  2879. /* The max size of hash table is PREALLOC_TB_SIZE */
  2880. order = PREALLOC_TB_SIZE - 1;
  2881. goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
  2882. /*
  2883. * search for the prealloc space that is having
  2884. * minimal distance from the goal block.
  2885. */
  2886. for (i = order; i < PREALLOC_TB_SIZE; i++) {
  2887. rcu_read_lock();
  2888. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
  2889. pa_inode_list) {
  2890. spin_lock(&pa->pa_lock);
  2891. if (pa->pa_deleted == 0 &&
  2892. pa->pa_free >= ac->ac_o_ex.fe_len) {
  2893. cpa = ext4_mb_check_group_pa(goal_block,
  2894. pa, cpa);
  2895. }
  2896. spin_unlock(&pa->pa_lock);
  2897. }
  2898. rcu_read_unlock();
  2899. }
  2900. if (cpa) {
  2901. ext4_mb_use_group_pa(ac, cpa);
  2902. ac->ac_criteria = 20;
  2903. return 1;
  2904. }
  2905. return 0;
  2906. }
  2907. /*
  2908. * the function goes through all block freed in the group
  2909. * but not yet committed and marks them used in in-core bitmap.
  2910. * buddy must be generated from this bitmap
  2911. * Need to be called with the ext4 group lock held
  2912. */
  2913. static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
  2914. ext4_group_t group)
  2915. {
  2916. struct rb_node *n;
  2917. struct ext4_group_info *grp;
  2918. struct ext4_free_data *entry;
  2919. grp = ext4_get_group_info(sb, group);
  2920. n = rb_first(&(grp->bb_free_root));
  2921. while (n) {
  2922. entry = rb_entry(n, struct ext4_free_data, node);
  2923. mb_set_bits(bitmap, entry->start_blk, entry->count);
  2924. n = rb_next(n);
  2925. }
  2926. return;
  2927. }
  2928. /*
  2929. * the function goes through all preallocation in this group and marks them
  2930. * used in in-core bitmap. buddy must be generated from this bitmap
  2931. * Need to be called with ext4 group lock held
  2932. */
  2933. static noinline_for_stack
  2934. void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  2935. ext4_group_t group)
  2936. {
  2937. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  2938. struct ext4_prealloc_space *pa;
  2939. struct list_head *cur;
  2940. ext4_group_t groupnr;
  2941. ext4_grpblk_t start;
  2942. int preallocated = 0;
  2943. int count = 0;
  2944. int len;
  2945. /* all form of preallocation discards first load group,
  2946. * so the only competing code is preallocation use.
  2947. * we don't need any locking here
  2948. * notice we do NOT ignore preallocations with pa_deleted
  2949. * otherwise we could leave used blocks available for
  2950. * allocation in buddy when concurrent ext4_mb_put_pa()
  2951. * is dropping preallocation
  2952. */
  2953. list_for_each(cur, &grp->bb_prealloc_list) {
  2954. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  2955. spin_lock(&pa->pa_lock);
  2956. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  2957. &groupnr, &start);
  2958. len = pa->pa_len;
  2959. spin_unlock(&pa->pa_lock);
  2960. if (unlikely(len == 0))
  2961. continue;
  2962. BUG_ON(groupnr != group);
  2963. mb_set_bits(bitmap, start, len);
  2964. preallocated += len;
  2965. count++;
  2966. }
  2967. mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
  2968. }
  2969. static void ext4_mb_pa_callback(struct rcu_head *head)
  2970. {
  2971. struct ext4_prealloc_space *pa;
  2972. pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
  2973. kmem_cache_free(ext4_pspace_cachep, pa);
  2974. }
  2975. /*
  2976. * drops a reference to preallocated space descriptor
  2977. * if this was the last reference and the space is consumed
  2978. */
  2979. static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
  2980. struct super_block *sb, struct ext4_prealloc_space *pa)
  2981. {
  2982. ext4_group_t grp;
  2983. ext4_fsblk_t grp_blk;
  2984. if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
  2985. return;
  2986. /* in this short window concurrent discard can set pa_deleted */
  2987. spin_lock(&pa->pa_lock);
  2988. if (pa->pa_deleted == 1) {
  2989. spin_unlock(&pa->pa_lock);
  2990. return;
  2991. }
  2992. pa->pa_deleted = 1;
  2993. spin_unlock(&pa->pa_lock);
  2994. grp_blk = pa->pa_pstart;
  2995. /*
  2996. * If doing group-based preallocation, pa_pstart may be in the
  2997. * next group when pa is used up
  2998. */
  2999. if (pa->pa_type == MB_GROUP_PA)
  3000. grp_blk--;
  3001. ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
  3002. /*
  3003. * possible race:
  3004. *
  3005. * P1 (buddy init) P2 (regular allocation)
  3006. * find block B in PA
  3007. * copy on-disk bitmap to buddy
  3008. * mark B in on-disk bitmap
  3009. * drop PA from group
  3010. * mark all PAs in buddy
  3011. *
  3012. * thus, P1 initializes buddy with B available. to prevent this
  3013. * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
  3014. * against that pair
  3015. */
  3016. ext4_lock_group(sb, grp);
  3017. list_del(&pa->pa_group_list);
  3018. ext4_unlock_group(sb, grp);
  3019. spin_lock(pa->pa_obj_lock);
  3020. list_del_rcu(&pa->pa_inode_list);
  3021. spin_unlock(pa->pa_obj_lock);
  3022. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3023. }
  3024. /*
  3025. * creates new preallocated space for given inode
  3026. */
  3027. static noinline_for_stack int
  3028. ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
  3029. {
  3030. struct super_block *sb = ac->ac_sb;
  3031. struct ext4_prealloc_space *pa;
  3032. struct ext4_group_info *grp;
  3033. struct ext4_inode_info *ei;
  3034. /* preallocate only when found space is larger then requested */
  3035. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3036. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3037. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3038. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3039. if (pa == NULL)
  3040. return -ENOMEM;
  3041. if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
  3042. int winl;
  3043. int wins;
  3044. int win;
  3045. int offs;
  3046. /* we can't allocate as much as normalizer wants.
  3047. * so, found space must get proper lstart
  3048. * to cover original request */
  3049. BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
  3050. BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
  3051. /* we're limited by original request in that
  3052. * logical block must be covered any way
  3053. * winl is window we can move our chunk within */
  3054. winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
  3055. /* also, we should cover whole original request */
  3056. wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
  3057. /* the smallest one defines real window */
  3058. win = min(winl, wins);
  3059. offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
  3060. if (offs && offs < win)
  3061. win = offs;
  3062. ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
  3063. BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
  3064. BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
  3065. }
  3066. /* preallocation can change ac_b_ex, thus we store actually
  3067. * allocated blocks for history */
  3068. ac->ac_f_ex = ac->ac_b_ex;
  3069. pa->pa_lstart = ac->ac_b_ex.fe_logical;
  3070. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3071. pa->pa_len = ac->ac_b_ex.fe_len;
  3072. pa->pa_free = pa->pa_len;
  3073. atomic_set(&pa->pa_count, 1);
  3074. spin_lock_init(&pa->pa_lock);
  3075. INIT_LIST_HEAD(&pa->pa_inode_list);
  3076. INIT_LIST_HEAD(&pa->pa_group_list);
  3077. pa->pa_deleted = 0;
  3078. pa->pa_type = MB_INODE_PA;
  3079. mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
  3080. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3081. trace_ext4_mb_new_inode_pa(ac, pa);
  3082. ext4_mb_use_inode_pa(ac, pa);
  3083. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3084. ei = EXT4_I(ac->ac_inode);
  3085. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3086. pa->pa_obj_lock = &ei->i_prealloc_lock;
  3087. pa->pa_inode = ac->ac_inode;
  3088. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3089. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3090. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3091. spin_lock(pa->pa_obj_lock);
  3092. list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
  3093. spin_unlock(pa->pa_obj_lock);
  3094. return 0;
  3095. }
  3096. /*
  3097. * creates new preallocated space for locality group inodes belongs to
  3098. */
  3099. static noinline_for_stack int
  3100. ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
  3101. {
  3102. struct super_block *sb = ac->ac_sb;
  3103. struct ext4_locality_group *lg;
  3104. struct ext4_prealloc_space *pa;
  3105. struct ext4_group_info *grp;
  3106. /* preallocate only when found space is larger then requested */
  3107. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3108. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3109. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3110. BUG_ON(ext4_pspace_cachep == NULL);
  3111. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3112. if (pa == NULL)
  3113. return -ENOMEM;
  3114. /* preallocation can change ac_b_ex, thus we store actually
  3115. * allocated blocks for history */
  3116. ac->ac_f_ex = ac->ac_b_ex;
  3117. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3118. pa->pa_lstart = pa->pa_pstart;
  3119. pa->pa_len = ac->ac_b_ex.fe_len;
  3120. pa->pa_free = pa->pa_len;
  3121. atomic_set(&pa->pa_count, 1);
  3122. spin_lock_init(&pa->pa_lock);
  3123. INIT_LIST_HEAD(&pa->pa_inode_list);
  3124. INIT_LIST_HEAD(&pa->pa_group_list);
  3125. pa->pa_deleted = 0;
  3126. pa->pa_type = MB_GROUP_PA;
  3127. mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
  3128. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3129. trace_ext4_mb_new_group_pa(ac, pa);
  3130. ext4_mb_use_group_pa(ac, pa);
  3131. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3132. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3133. lg = ac->ac_lg;
  3134. BUG_ON(lg == NULL);
  3135. pa->pa_obj_lock = &lg->lg_prealloc_lock;
  3136. pa->pa_inode = NULL;
  3137. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3138. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3139. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3140. /*
  3141. * We will later add the new pa to the right bucket
  3142. * after updating the pa_free in ext4_mb_release_context
  3143. */
  3144. return 0;
  3145. }
  3146. static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
  3147. {
  3148. int err;
  3149. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3150. err = ext4_mb_new_group_pa(ac);
  3151. else
  3152. err = ext4_mb_new_inode_pa(ac);
  3153. return err;
  3154. }
  3155. /*
  3156. * finds all unused blocks in on-disk bitmap, frees them in
  3157. * in-core bitmap and buddy.
  3158. * @pa must be unlinked from inode and group lists, so that
  3159. * nobody else can find/use it.
  3160. * the caller MUST hold group/inode locks.
  3161. * TODO: optimize the case when there are no in-core structures yet
  3162. */
  3163. static noinline_for_stack int
  3164. ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
  3165. struct ext4_prealloc_space *pa,
  3166. struct ext4_allocation_context *ac)
  3167. {
  3168. struct super_block *sb = e4b->bd_sb;
  3169. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3170. unsigned int end;
  3171. unsigned int next;
  3172. ext4_group_t group;
  3173. ext4_grpblk_t bit;
  3174. unsigned long long grp_blk_start;
  3175. int err = 0;
  3176. int free = 0;
  3177. BUG_ON(pa->pa_deleted == 0);
  3178. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3179. grp_blk_start = pa->pa_pstart - bit;
  3180. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3181. end = bit + pa->pa_len;
  3182. if (ac) {
  3183. ac->ac_sb = sb;
  3184. ac->ac_inode = pa->pa_inode;
  3185. }
  3186. while (bit < end) {
  3187. bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
  3188. if (bit >= end)
  3189. break;
  3190. next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
  3191. mb_debug(1, " free preallocated %u/%u in group %u\n",
  3192. (unsigned) ext4_group_first_block_no(sb, group) + bit,
  3193. (unsigned) next - bit, (unsigned) group);
  3194. free += next - bit;
  3195. if (ac) {
  3196. ac->ac_b_ex.fe_group = group;
  3197. ac->ac_b_ex.fe_start = bit;
  3198. ac->ac_b_ex.fe_len = next - bit;
  3199. ac->ac_b_ex.fe_logical = 0;
  3200. trace_ext4_mballoc_discard(ac);
  3201. }
  3202. trace_ext4_mb_release_inode_pa(sb, ac, pa, grp_blk_start + bit,
  3203. next - bit);
  3204. mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
  3205. bit = next + 1;
  3206. }
  3207. if (free != pa->pa_free) {
  3208. printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
  3209. pa, (unsigned long) pa->pa_lstart,
  3210. (unsigned long) pa->pa_pstart,
  3211. (unsigned long) pa->pa_len);
  3212. ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
  3213. free, pa->pa_free);
  3214. /*
  3215. * pa is already deleted so we use the value obtained
  3216. * from the bitmap and continue.
  3217. */
  3218. }
  3219. atomic_add(free, &sbi->s_mb_discarded);
  3220. return err;
  3221. }
  3222. static noinline_for_stack int
  3223. ext4_mb_release_group_pa(struct ext4_buddy *e4b,
  3224. struct ext4_prealloc_space *pa,
  3225. struct ext4_allocation_context *ac)
  3226. {
  3227. struct super_block *sb = e4b->bd_sb;
  3228. ext4_group_t group;
  3229. ext4_grpblk_t bit;
  3230. trace_ext4_mb_release_group_pa(sb, ac, pa);
  3231. BUG_ON(pa->pa_deleted == 0);
  3232. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3233. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3234. mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
  3235. atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
  3236. if (ac) {
  3237. ac->ac_sb = sb;
  3238. ac->ac_inode = NULL;
  3239. ac->ac_b_ex.fe_group = group;
  3240. ac->ac_b_ex.fe_start = bit;
  3241. ac->ac_b_ex.fe_len = pa->pa_len;
  3242. ac->ac_b_ex.fe_logical = 0;
  3243. trace_ext4_mballoc_discard(ac);
  3244. }
  3245. return 0;
  3246. }
  3247. /*
  3248. * releases all preallocations in given group
  3249. *
  3250. * first, we need to decide discard policy:
  3251. * - when do we discard
  3252. * 1) ENOSPC
  3253. * - how many do we discard
  3254. * 1) how many requested
  3255. */
  3256. static noinline_for_stack int
  3257. ext4_mb_discard_group_preallocations(struct super_block *sb,
  3258. ext4_group_t group, int needed)
  3259. {
  3260. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3261. struct buffer_head *bitmap_bh = NULL;
  3262. struct ext4_prealloc_space *pa, *tmp;
  3263. struct ext4_allocation_context *ac;
  3264. struct list_head list;
  3265. struct ext4_buddy e4b;
  3266. int err;
  3267. int busy = 0;
  3268. int free = 0;
  3269. mb_debug(1, "discard preallocation for group %u\n", group);
  3270. if (list_empty(&grp->bb_prealloc_list))
  3271. return 0;
  3272. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3273. if (bitmap_bh == NULL) {
  3274. ext4_error(sb, "Error reading block bitmap for %u", group);
  3275. return 0;
  3276. }
  3277. err = ext4_mb_load_buddy(sb, group, &e4b);
  3278. if (err) {
  3279. ext4_error(sb, "Error loading buddy information for %u", group);
  3280. put_bh(bitmap_bh);
  3281. return 0;
  3282. }
  3283. if (needed == 0)
  3284. needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
  3285. INIT_LIST_HEAD(&list);
  3286. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3287. if (ac)
  3288. ac->ac_sb = sb;
  3289. repeat:
  3290. ext4_lock_group(sb, group);
  3291. list_for_each_entry_safe(pa, tmp,
  3292. &grp->bb_prealloc_list, pa_group_list) {
  3293. spin_lock(&pa->pa_lock);
  3294. if (atomic_read(&pa->pa_count)) {
  3295. spin_unlock(&pa->pa_lock);
  3296. busy = 1;
  3297. continue;
  3298. }
  3299. if (pa->pa_deleted) {
  3300. spin_unlock(&pa->pa_lock);
  3301. continue;
  3302. }
  3303. /* seems this one can be freed ... */
  3304. pa->pa_deleted = 1;
  3305. /* we can trust pa_free ... */
  3306. free += pa->pa_free;
  3307. spin_unlock(&pa->pa_lock);
  3308. list_del(&pa->pa_group_list);
  3309. list_add(&pa->u.pa_tmp_list, &list);
  3310. }
  3311. /* if we still need more blocks and some PAs were used, try again */
  3312. if (free < needed && busy) {
  3313. busy = 0;
  3314. ext4_unlock_group(sb, group);
  3315. /*
  3316. * Yield the CPU here so that we don't get soft lockup
  3317. * in non preempt case.
  3318. */
  3319. yield();
  3320. goto repeat;
  3321. }
  3322. /* found anything to free? */
  3323. if (list_empty(&list)) {
  3324. BUG_ON(free != 0);
  3325. goto out;
  3326. }
  3327. /* now free all selected PAs */
  3328. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3329. /* remove from object (inode or locality group) */
  3330. spin_lock(pa->pa_obj_lock);
  3331. list_del_rcu(&pa->pa_inode_list);
  3332. spin_unlock(pa->pa_obj_lock);
  3333. if (pa->pa_type == MB_GROUP_PA)
  3334. ext4_mb_release_group_pa(&e4b, pa, ac);
  3335. else
  3336. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3337. list_del(&pa->u.pa_tmp_list);
  3338. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3339. }
  3340. out:
  3341. ext4_unlock_group(sb, group);
  3342. if (ac)
  3343. kmem_cache_free(ext4_ac_cachep, ac);
  3344. ext4_mb_unload_buddy(&e4b);
  3345. put_bh(bitmap_bh);
  3346. return free;
  3347. }
  3348. /*
  3349. * releases all non-used preallocated blocks for given inode
  3350. *
  3351. * It's important to discard preallocations under i_data_sem
  3352. * We don't want another block to be served from the prealloc
  3353. * space when we are discarding the inode prealloc space.
  3354. *
  3355. * FIXME!! Make sure it is valid at all the call sites
  3356. */
  3357. void ext4_discard_preallocations(struct inode *inode)
  3358. {
  3359. struct ext4_inode_info *ei = EXT4_I(inode);
  3360. struct super_block *sb = inode->i_sb;
  3361. struct buffer_head *bitmap_bh = NULL;
  3362. struct ext4_prealloc_space *pa, *tmp;
  3363. struct ext4_allocation_context *ac;
  3364. ext4_group_t group = 0;
  3365. struct list_head list;
  3366. struct ext4_buddy e4b;
  3367. int err;
  3368. if (!S_ISREG(inode->i_mode)) {
  3369. /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
  3370. return;
  3371. }
  3372. mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
  3373. trace_ext4_discard_preallocations(inode);
  3374. INIT_LIST_HEAD(&list);
  3375. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3376. if (ac) {
  3377. ac->ac_sb = sb;
  3378. ac->ac_inode = inode;
  3379. }
  3380. repeat:
  3381. /* first, collect all pa's in the inode */
  3382. spin_lock(&ei->i_prealloc_lock);
  3383. while (!list_empty(&ei->i_prealloc_list)) {
  3384. pa = list_entry(ei->i_prealloc_list.next,
  3385. struct ext4_prealloc_space, pa_inode_list);
  3386. BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
  3387. spin_lock(&pa->pa_lock);
  3388. if (atomic_read(&pa->pa_count)) {
  3389. /* this shouldn't happen often - nobody should
  3390. * use preallocation while we're discarding it */
  3391. spin_unlock(&pa->pa_lock);
  3392. spin_unlock(&ei->i_prealloc_lock);
  3393. printk(KERN_ERR "uh-oh! used pa while discarding\n");
  3394. WARN_ON(1);
  3395. schedule_timeout_uninterruptible(HZ);
  3396. goto repeat;
  3397. }
  3398. if (pa->pa_deleted == 0) {
  3399. pa->pa_deleted = 1;
  3400. spin_unlock(&pa->pa_lock);
  3401. list_del_rcu(&pa->pa_inode_list);
  3402. list_add(&pa->u.pa_tmp_list, &list);
  3403. continue;
  3404. }
  3405. /* someone is deleting pa right now */
  3406. spin_unlock(&pa->pa_lock);
  3407. spin_unlock(&ei->i_prealloc_lock);
  3408. /* we have to wait here because pa_deleted
  3409. * doesn't mean pa is already unlinked from
  3410. * the list. as we might be called from
  3411. * ->clear_inode() the inode will get freed
  3412. * and concurrent thread which is unlinking
  3413. * pa from inode's list may access already
  3414. * freed memory, bad-bad-bad */
  3415. /* XXX: if this happens too often, we can
  3416. * add a flag to force wait only in case
  3417. * of ->clear_inode(), but not in case of
  3418. * regular truncate */
  3419. schedule_timeout_uninterruptible(HZ);
  3420. goto repeat;
  3421. }
  3422. spin_unlock(&ei->i_prealloc_lock);
  3423. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3424. BUG_ON(pa->pa_type != MB_INODE_PA);
  3425. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3426. err = ext4_mb_load_buddy(sb, group, &e4b);
  3427. if (err) {
  3428. ext4_error(sb, "Error loading buddy information for %u",
  3429. group);
  3430. continue;
  3431. }
  3432. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3433. if (bitmap_bh == NULL) {
  3434. ext4_error(sb, "Error reading block bitmap for %u",
  3435. group);
  3436. ext4_mb_unload_buddy(&e4b);
  3437. continue;
  3438. }
  3439. ext4_lock_group(sb, group);
  3440. list_del(&pa->pa_group_list);
  3441. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3442. ext4_unlock_group(sb, group);
  3443. ext4_mb_unload_buddy(&e4b);
  3444. put_bh(bitmap_bh);
  3445. list_del(&pa->u.pa_tmp_list);
  3446. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3447. }
  3448. if (ac)
  3449. kmem_cache_free(ext4_ac_cachep, ac);
  3450. }
  3451. /*
  3452. * finds all preallocated spaces and return blocks being freed to them
  3453. * if preallocated space becomes full (no block is used from the space)
  3454. * then the function frees space in buddy
  3455. * XXX: at the moment, truncate (which is the only way to free blocks)
  3456. * discards all preallocations
  3457. */
  3458. static void ext4_mb_return_to_preallocation(struct inode *inode,
  3459. struct ext4_buddy *e4b,
  3460. sector_t block, int count)
  3461. {
  3462. BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
  3463. }
  3464. #ifdef CONFIG_EXT4_DEBUG
  3465. static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3466. {
  3467. struct super_block *sb = ac->ac_sb;
  3468. ext4_group_t ngroups, i;
  3469. if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
  3470. return;
  3471. printk(KERN_ERR "EXT4-fs: Can't allocate:"
  3472. " Allocation context details:\n");
  3473. printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
  3474. ac->ac_status, ac->ac_flags);
  3475. printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
  3476. "best %lu/%lu/%lu@%lu cr %d\n",
  3477. (unsigned long)ac->ac_o_ex.fe_group,
  3478. (unsigned long)ac->ac_o_ex.fe_start,
  3479. (unsigned long)ac->ac_o_ex.fe_len,
  3480. (unsigned long)ac->ac_o_ex.fe_logical,
  3481. (unsigned long)ac->ac_g_ex.fe_group,
  3482. (unsigned long)ac->ac_g_ex.fe_start,
  3483. (unsigned long)ac->ac_g_ex.fe_len,
  3484. (unsigned long)ac->ac_g_ex.fe_logical,
  3485. (unsigned long)ac->ac_b_ex.fe_group,
  3486. (unsigned long)ac->ac_b_ex.fe_start,
  3487. (unsigned long)ac->ac_b_ex.fe_len,
  3488. (unsigned long)ac->ac_b_ex.fe_logical,
  3489. (int)ac->ac_criteria);
  3490. printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
  3491. ac->ac_found);
  3492. printk(KERN_ERR "EXT4-fs: groups: \n");
  3493. ngroups = ext4_get_groups_count(sb);
  3494. for (i = 0; i < ngroups; i++) {
  3495. struct ext4_group_info *grp = ext4_get_group_info(sb, i);
  3496. struct ext4_prealloc_space *pa;
  3497. ext4_grpblk_t start;
  3498. struct list_head *cur;
  3499. ext4_lock_group(sb, i);
  3500. list_for_each(cur, &grp->bb_prealloc_list) {
  3501. pa = list_entry(cur, struct ext4_prealloc_space,
  3502. pa_group_list);
  3503. spin_lock(&pa->pa_lock);
  3504. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3505. NULL, &start);
  3506. spin_unlock(&pa->pa_lock);
  3507. printk(KERN_ERR "PA:%u:%d:%u \n", i,
  3508. start, pa->pa_len);
  3509. }
  3510. ext4_unlock_group(sb, i);
  3511. if (grp->bb_free == 0)
  3512. continue;
  3513. printk(KERN_ERR "%u: %d/%d \n",
  3514. i, grp->bb_free, grp->bb_fragments);
  3515. }
  3516. printk(KERN_ERR "\n");
  3517. }
  3518. #else
  3519. static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3520. {
  3521. return;
  3522. }
  3523. #endif
  3524. /*
  3525. * We use locality group preallocation for small size file. The size of the
  3526. * file is determined by the current size or the resulting size after
  3527. * allocation which ever is larger
  3528. *
  3529. * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
  3530. */
  3531. static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
  3532. {
  3533. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  3534. int bsbits = ac->ac_sb->s_blocksize_bits;
  3535. loff_t size, isize;
  3536. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  3537. return;
  3538. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  3539. return;
  3540. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  3541. isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
  3542. >> bsbits;
  3543. if ((size == isize) &&
  3544. !ext4_fs_is_busy(sbi) &&
  3545. (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
  3546. ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
  3547. return;
  3548. }
  3549. /* don't use group allocation for large files */
  3550. size = max(size, isize);
  3551. if (size > sbi->s_mb_stream_request) {
  3552. ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
  3553. return;
  3554. }
  3555. BUG_ON(ac->ac_lg != NULL);
  3556. /*
  3557. * locality group prealloc space are per cpu. The reason for having
  3558. * per cpu locality group is to reduce the contention between block
  3559. * request from multiple CPUs.
  3560. */
  3561. ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
  3562. /* we're going to use group allocation */
  3563. ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
  3564. /* serialize all allocations in the group */
  3565. mutex_lock(&ac->ac_lg->lg_mutex);
  3566. }
  3567. static noinline_for_stack int
  3568. ext4_mb_initialize_context(struct ext4_allocation_context *ac,
  3569. struct ext4_allocation_request *ar)
  3570. {
  3571. struct super_block *sb = ar->inode->i_sb;
  3572. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3573. struct ext4_super_block *es = sbi->s_es;
  3574. ext4_group_t group;
  3575. unsigned int len;
  3576. ext4_fsblk_t goal;
  3577. ext4_grpblk_t block;
  3578. /* we can't allocate > group size */
  3579. len = ar->len;
  3580. /* just a dirty hack to filter too big requests */
  3581. if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
  3582. len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
  3583. /* start searching from the goal */
  3584. goal = ar->goal;
  3585. if (goal < le32_to_cpu(es->s_first_data_block) ||
  3586. goal >= ext4_blocks_count(es))
  3587. goal = le32_to_cpu(es->s_first_data_block);
  3588. ext4_get_group_no_and_offset(sb, goal, &group, &block);
  3589. /* set up allocation goals */
  3590. memset(ac, 0, sizeof(struct ext4_allocation_context));
  3591. ac->ac_b_ex.fe_logical = ar->logical;
  3592. ac->ac_status = AC_STATUS_CONTINUE;
  3593. ac->ac_sb = sb;
  3594. ac->ac_inode = ar->inode;
  3595. ac->ac_o_ex.fe_logical = ar->logical;
  3596. ac->ac_o_ex.fe_group = group;
  3597. ac->ac_o_ex.fe_start = block;
  3598. ac->ac_o_ex.fe_len = len;
  3599. ac->ac_g_ex.fe_logical = ar->logical;
  3600. ac->ac_g_ex.fe_group = group;
  3601. ac->ac_g_ex.fe_start = block;
  3602. ac->ac_g_ex.fe_len = len;
  3603. ac->ac_flags = ar->flags;
  3604. /* we have to define context: we'll we work with a file or
  3605. * locality group. this is a policy, actually */
  3606. ext4_mb_group_or_file(ac);
  3607. mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
  3608. "left: %u/%u, right %u/%u to %swritable\n",
  3609. (unsigned) ar->len, (unsigned) ar->logical,
  3610. (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
  3611. (unsigned) ar->lleft, (unsigned) ar->pleft,
  3612. (unsigned) ar->lright, (unsigned) ar->pright,
  3613. atomic_read(&ar->inode->i_writecount) ? "" : "non-");
  3614. return 0;
  3615. }
  3616. static noinline_for_stack void
  3617. ext4_mb_discard_lg_preallocations(struct super_block *sb,
  3618. struct ext4_locality_group *lg,
  3619. int order, int total_entries)
  3620. {
  3621. ext4_group_t group = 0;
  3622. struct ext4_buddy e4b;
  3623. struct list_head discard_list;
  3624. struct ext4_prealloc_space *pa, *tmp;
  3625. struct ext4_allocation_context *ac;
  3626. mb_debug(1, "discard locality group preallocation\n");
  3627. INIT_LIST_HEAD(&discard_list);
  3628. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3629. if (ac)
  3630. ac->ac_sb = sb;
  3631. spin_lock(&lg->lg_prealloc_lock);
  3632. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
  3633. pa_inode_list) {
  3634. spin_lock(&pa->pa_lock);
  3635. if (atomic_read(&pa->pa_count)) {
  3636. /*
  3637. * This is the pa that we just used
  3638. * for block allocation. So don't
  3639. * free that
  3640. */
  3641. spin_unlock(&pa->pa_lock);
  3642. continue;
  3643. }
  3644. if (pa->pa_deleted) {
  3645. spin_unlock(&pa->pa_lock);
  3646. continue;
  3647. }
  3648. /* only lg prealloc space */
  3649. BUG_ON(pa->pa_type != MB_GROUP_PA);
  3650. /* seems this one can be freed ... */
  3651. pa->pa_deleted = 1;
  3652. spin_unlock(&pa->pa_lock);
  3653. list_del_rcu(&pa->pa_inode_list);
  3654. list_add(&pa->u.pa_tmp_list, &discard_list);
  3655. total_entries--;
  3656. if (total_entries <= 5) {
  3657. /*
  3658. * we want to keep only 5 entries
  3659. * allowing it to grow to 8. This
  3660. * mak sure we don't call discard
  3661. * soon for this list.
  3662. */
  3663. break;
  3664. }
  3665. }
  3666. spin_unlock(&lg->lg_prealloc_lock);
  3667. list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
  3668. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3669. if (ext4_mb_load_buddy(sb, group, &e4b)) {
  3670. ext4_error(sb, "Error loading buddy information for %u",
  3671. group);
  3672. continue;
  3673. }
  3674. ext4_lock_group(sb, group);
  3675. list_del(&pa->pa_group_list);
  3676. ext4_mb_release_group_pa(&e4b, pa, ac);
  3677. ext4_unlock_group(sb, group);
  3678. ext4_mb_unload_buddy(&e4b);
  3679. list_del(&pa->u.pa_tmp_list);
  3680. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3681. }
  3682. if (ac)
  3683. kmem_cache_free(ext4_ac_cachep, ac);
  3684. }
  3685. /*
  3686. * We have incremented pa_count. So it cannot be freed at this
  3687. * point. Also we hold lg_mutex. So no parallel allocation is
  3688. * possible from this lg. That means pa_free cannot be updated.
  3689. *
  3690. * A parallel ext4_mb_discard_group_preallocations is possible.
  3691. * which can cause the lg_prealloc_list to be updated.
  3692. */
  3693. static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
  3694. {
  3695. int order, added = 0, lg_prealloc_count = 1;
  3696. struct super_block *sb = ac->ac_sb;
  3697. struct ext4_locality_group *lg = ac->ac_lg;
  3698. struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
  3699. order = fls(pa->pa_free) - 1;
  3700. if (order > PREALLOC_TB_SIZE - 1)
  3701. /* The max size of hash table is PREALLOC_TB_SIZE */
  3702. order = PREALLOC_TB_SIZE - 1;
  3703. /* Add the prealloc space to lg */
  3704. rcu_read_lock();
  3705. list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
  3706. pa_inode_list) {
  3707. spin_lock(&tmp_pa->pa_lock);
  3708. if (tmp_pa->pa_deleted) {
  3709. spin_unlock(&tmp_pa->pa_lock);
  3710. continue;
  3711. }
  3712. if (!added && pa->pa_free < tmp_pa->pa_free) {
  3713. /* Add to the tail of the previous entry */
  3714. list_add_tail_rcu(&pa->pa_inode_list,
  3715. &tmp_pa->pa_inode_list);
  3716. added = 1;
  3717. /*
  3718. * we want to count the total
  3719. * number of entries in the list
  3720. */
  3721. }
  3722. spin_unlock(&tmp_pa->pa_lock);
  3723. lg_prealloc_count++;
  3724. }
  3725. if (!added)
  3726. list_add_tail_rcu(&pa->pa_inode_list,
  3727. &lg->lg_prealloc_list[order]);
  3728. rcu_read_unlock();
  3729. /* Now trim the list to be not more than 8 elements */
  3730. if (lg_prealloc_count > 8) {
  3731. ext4_mb_discard_lg_preallocations(sb, lg,
  3732. order, lg_prealloc_count);
  3733. return;
  3734. }
  3735. return ;
  3736. }
  3737. /*
  3738. * release all resource we used in allocation
  3739. */
  3740. static int ext4_mb_release_context(struct ext4_allocation_context *ac)
  3741. {
  3742. struct ext4_prealloc_space *pa = ac->ac_pa;
  3743. if (pa) {
  3744. if (pa->pa_type == MB_GROUP_PA) {
  3745. /* see comment in ext4_mb_use_group_pa() */
  3746. spin_lock(&pa->pa_lock);
  3747. pa->pa_pstart += ac->ac_b_ex.fe_len;
  3748. pa->pa_lstart += ac->ac_b_ex.fe_len;
  3749. pa->pa_free -= ac->ac_b_ex.fe_len;
  3750. pa->pa_len -= ac->ac_b_ex.fe_len;
  3751. spin_unlock(&pa->pa_lock);
  3752. }
  3753. }
  3754. if (ac->alloc_semp)
  3755. up_read(ac->alloc_semp);
  3756. if (pa) {
  3757. /*
  3758. * We want to add the pa to the right bucket.
  3759. * Remove it from the list and while adding
  3760. * make sure the list to which we are adding
  3761. * doesn't grow big. We need to release
  3762. * alloc_semp before calling ext4_mb_add_n_trim()
  3763. */
  3764. if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
  3765. spin_lock(pa->pa_obj_lock);
  3766. list_del_rcu(&pa->pa_inode_list);
  3767. spin_unlock(pa->pa_obj_lock);
  3768. ext4_mb_add_n_trim(ac);
  3769. }
  3770. ext4_mb_put_pa(ac, ac->ac_sb, pa);
  3771. }
  3772. if (ac->ac_bitmap_page)
  3773. page_cache_release(ac->ac_bitmap_page);
  3774. if (ac->ac_buddy_page)
  3775. page_cache_release(ac->ac_buddy_page);
  3776. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3777. mutex_unlock(&ac->ac_lg->lg_mutex);
  3778. ext4_mb_collect_stats(ac);
  3779. return 0;
  3780. }
  3781. static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
  3782. {
  3783. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3784. int ret;
  3785. int freed = 0;
  3786. trace_ext4_mb_discard_preallocations(sb, needed);
  3787. for (i = 0; i < ngroups && needed > 0; i++) {
  3788. ret = ext4_mb_discard_group_preallocations(sb, i, needed);
  3789. freed += ret;
  3790. needed -= ret;
  3791. }
  3792. return freed;
  3793. }
  3794. /*
  3795. * Main entry point into mballoc to allocate blocks
  3796. * it tries to use preallocation first, then falls back
  3797. * to usual allocation
  3798. */
  3799. ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  3800. struct ext4_allocation_request *ar, int *errp)
  3801. {
  3802. int freed;
  3803. struct ext4_allocation_context *ac = NULL;
  3804. struct ext4_sb_info *sbi;
  3805. struct super_block *sb;
  3806. ext4_fsblk_t block = 0;
  3807. unsigned int inquota = 0;
  3808. unsigned int reserv_blks = 0;
  3809. sb = ar->inode->i_sb;
  3810. sbi = EXT4_SB(sb);
  3811. trace_ext4_request_blocks(ar);
  3812. /*
  3813. * For delayed allocation, we could skip the ENOSPC and
  3814. * EDQUOT check, as blocks and quotas have been already
  3815. * reserved when data being copied into pagecache.
  3816. */
  3817. if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
  3818. ar->flags |= EXT4_MB_DELALLOC_RESERVED;
  3819. else {
  3820. /* Without delayed allocation we need to verify
  3821. * there is enough free blocks to do block allocation
  3822. * and verify allocation doesn't exceed the quota limits.
  3823. */
  3824. while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
  3825. /* let others to free the space */
  3826. yield();
  3827. ar->len = ar->len >> 1;
  3828. }
  3829. if (!ar->len) {
  3830. *errp = -ENOSPC;
  3831. return 0;
  3832. }
  3833. reserv_blks = ar->len;
  3834. while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
  3835. ar->flags |= EXT4_MB_HINT_NOPREALLOC;
  3836. ar->len--;
  3837. }
  3838. inquota = ar->len;
  3839. if (ar->len == 0) {
  3840. *errp = -EDQUOT;
  3841. goto out;
  3842. }
  3843. }
  3844. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3845. if (!ac) {
  3846. ar->len = 0;
  3847. *errp = -ENOMEM;
  3848. goto out;
  3849. }
  3850. *errp = ext4_mb_initialize_context(ac, ar);
  3851. if (*errp) {
  3852. ar->len = 0;
  3853. goto out;
  3854. }
  3855. ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
  3856. if (!ext4_mb_use_preallocated(ac)) {
  3857. ac->ac_op = EXT4_MB_HISTORY_ALLOC;
  3858. ext4_mb_normalize_request(ac, ar);
  3859. repeat:
  3860. /* allocate space in core */
  3861. *errp = ext4_mb_regular_allocator(ac);
  3862. if (*errp)
  3863. goto errout;
  3864. /* as we've just preallocated more space than
  3865. * user requested orinally, we store allocated
  3866. * space in a special descriptor */
  3867. if (ac->ac_status == AC_STATUS_FOUND &&
  3868. ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
  3869. ext4_mb_new_preallocation(ac);
  3870. }
  3871. if (likely(ac->ac_status == AC_STATUS_FOUND)) {
  3872. *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
  3873. if (*errp == -EAGAIN) {
  3874. /*
  3875. * drop the reference that we took
  3876. * in ext4_mb_use_best_found
  3877. */
  3878. ext4_mb_release_context(ac);
  3879. ac->ac_b_ex.fe_group = 0;
  3880. ac->ac_b_ex.fe_start = 0;
  3881. ac->ac_b_ex.fe_len = 0;
  3882. ac->ac_status = AC_STATUS_CONTINUE;
  3883. goto repeat;
  3884. } else if (*errp)
  3885. errout:
  3886. ext4_discard_allocated_blocks(ac);
  3887. else {
  3888. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3889. ar->len = ac->ac_b_ex.fe_len;
  3890. }
  3891. } else {
  3892. freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
  3893. if (freed)
  3894. goto repeat;
  3895. *errp = -ENOSPC;
  3896. }
  3897. if (*errp) {
  3898. ac->ac_b_ex.fe_len = 0;
  3899. ar->len = 0;
  3900. ext4_mb_show_ac(ac);
  3901. }
  3902. ext4_mb_release_context(ac);
  3903. out:
  3904. if (ac)
  3905. kmem_cache_free(ext4_ac_cachep, ac);
  3906. if (inquota && ar->len < inquota)
  3907. dquot_free_block(ar->inode, inquota - ar->len);
  3908. if (!ar->len) {
  3909. if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
  3910. /* release all the reserved blocks if non delalloc */
  3911. percpu_counter_sub(&sbi->s_dirtyblocks_counter,
  3912. reserv_blks);
  3913. }
  3914. trace_ext4_allocate_blocks(ar, (unsigned long long)block);
  3915. return block;
  3916. }
  3917. /*
  3918. * We can merge two free data extents only if the physical blocks
  3919. * are contiguous, AND the extents were freed by the same transaction,
  3920. * AND the blocks are associated with the same group.
  3921. */
  3922. static int can_merge(struct ext4_free_data *entry1,
  3923. struct ext4_free_data *entry2)
  3924. {
  3925. if ((entry1->t_tid == entry2->t_tid) &&
  3926. (entry1->group == entry2->group) &&
  3927. ((entry1->start_blk + entry1->count) == entry2->start_blk))
  3928. return 1;
  3929. return 0;
  3930. }
  3931. static noinline_for_stack int
  3932. ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
  3933. struct ext4_free_data *new_entry)
  3934. {
  3935. ext4_group_t group = e4b->bd_group;
  3936. ext4_grpblk_t block;
  3937. struct ext4_free_data *entry;
  3938. struct ext4_group_info *db = e4b->bd_info;
  3939. struct super_block *sb = e4b->bd_sb;
  3940. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3941. struct rb_node **n = &db->bb_free_root.rb_node, *node;
  3942. struct rb_node *parent = NULL, *new_node;
  3943. BUG_ON(!ext4_handle_valid(handle));
  3944. BUG_ON(e4b->bd_bitmap_page == NULL);
  3945. BUG_ON(e4b->bd_buddy_page == NULL);
  3946. new_node = &new_entry->node;
  3947. block = new_entry->start_blk;
  3948. if (!*n) {
  3949. /* first free block exent. We need to
  3950. protect buddy cache from being freed,
  3951. * otherwise we'll refresh it from
  3952. * on-disk bitmap and lose not-yet-available
  3953. * blocks */
  3954. page_cache_get(e4b->bd_buddy_page);
  3955. page_cache_get(e4b->bd_bitmap_page);
  3956. }
  3957. while (*n) {
  3958. parent = *n;
  3959. entry = rb_entry(parent, struct ext4_free_data, node);
  3960. if (block < entry->start_blk)
  3961. n = &(*n)->rb_left;
  3962. else if (block >= (entry->start_blk + entry->count))
  3963. n = &(*n)->rb_right;
  3964. else {
  3965. ext4_grp_locked_error(sb, group, 0,
  3966. ext4_group_first_block_no(sb, group) + block,
  3967. "Block already on to-be-freed list");
  3968. return 0;
  3969. }
  3970. }
  3971. rb_link_node(new_node, parent, n);
  3972. rb_insert_color(new_node, &db->bb_free_root);
  3973. /* Now try to see the extent can be merged to left and right */
  3974. node = rb_prev(new_node);
  3975. if (node) {
  3976. entry = rb_entry(node, struct ext4_free_data, node);
  3977. if (can_merge(entry, new_entry)) {
  3978. new_entry->start_blk = entry->start_blk;
  3979. new_entry->count += entry->count;
  3980. rb_erase(node, &(db->bb_free_root));
  3981. spin_lock(&sbi->s_md_lock);
  3982. list_del(&entry->list);
  3983. spin_unlock(&sbi->s_md_lock);
  3984. kmem_cache_free(ext4_free_ext_cachep, entry);
  3985. }
  3986. }
  3987. node = rb_next(new_node);
  3988. if (node) {
  3989. entry = rb_entry(node, struct ext4_free_data, node);
  3990. if (can_merge(new_entry, entry)) {
  3991. new_entry->count += entry->count;
  3992. rb_erase(node, &(db->bb_free_root));
  3993. spin_lock(&sbi->s_md_lock);
  3994. list_del(&entry->list);
  3995. spin_unlock(&sbi->s_md_lock);
  3996. kmem_cache_free(ext4_free_ext_cachep, entry);
  3997. }
  3998. }
  3999. /* Add the extent to transaction's private list */
  4000. spin_lock(&sbi->s_md_lock);
  4001. list_add(&new_entry->list, &handle->h_transaction->t_private_list);
  4002. spin_unlock(&sbi->s_md_lock);
  4003. return 0;
  4004. }
  4005. /**
  4006. * ext4_free_blocks() -- Free given blocks and update quota
  4007. * @handle: handle for this transaction
  4008. * @inode: inode
  4009. * @block: start physical block to free
  4010. * @count: number of blocks to count
  4011. * @metadata: Are these metadata blocks
  4012. */
  4013. void ext4_free_blocks(handle_t *handle, struct inode *inode,
  4014. struct buffer_head *bh, ext4_fsblk_t block,
  4015. unsigned long count, int flags)
  4016. {
  4017. struct buffer_head *bitmap_bh = NULL;
  4018. struct super_block *sb = inode->i_sb;
  4019. struct ext4_allocation_context *ac = NULL;
  4020. struct ext4_group_desc *gdp;
  4021. unsigned long freed = 0;
  4022. unsigned int overflow;
  4023. ext4_grpblk_t bit;
  4024. struct buffer_head *gd_bh;
  4025. ext4_group_t block_group;
  4026. struct ext4_sb_info *sbi;
  4027. struct ext4_buddy e4b;
  4028. int err = 0;
  4029. int ret;
  4030. if (bh) {
  4031. if (block)
  4032. BUG_ON(block != bh->b_blocknr);
  4033. else
  4034. block = bh->b_blocknr;
  4035. }
  4036. sbi = EXT4_SB(sb);
  4037. if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
  4038. !ext4_data_block_valid(sbi, block, count)) {
  4039. ext4_error(sb, "Freeing blocks not in datazone - "
  4040. "block = %llu, count = %lu", block, count);
  4041. goto error_return;
  4042. }
  4043. ext4_debug("freeing block %llu\n", block);
  4044. trace_ext4_free_blocks(inode, block, count, flags);
  4045. if (flags & EXT4_FREE_BLOCKS_FORGET) {
  4046. struct buffer_head *tbh = bh;
  4047. int i;
  4048. BUG_ON(bh && (count > 1));
  4049. for (i = 0; i < count; i++) {
  4050. if (!bh)
  4051. tbh = sb_find_get_block(inode->i_sb,
  4052. block + i);
  4053. ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
  4054. inode, tbh, block + i);
  4055. }
  4056. }
  4057. /*
  4058. * We need to make sure we don't reuse the freed block until
  4059. * after the transaction is committed, which we can do by
  4060. * treating the block as metadata, below. We make an
  4061. * exception if the inode is to be written in writeback mode
  4062. * since writeback mode has weak data consistency guarantees.
  4063. */
  4064. if (!ext4_should_writeback_data(inode))
  4065. flags |= EXT4_FREE_BLOCKS_METADATA;
  4066. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  4067. if (ac) {
  4068. ac->ac_inode = inode;
  4069. ac->ac_sb = sb;
  4070. }
  4071. do_more:
  4072. overflow = 0;
  4073. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  4074. /*
  4075. * Check to see if we are freeing blocks across a group
  4076. * boundary.
  4077. */
  4078. if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  4079. overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
  4080. count -= overflow;
  4081. }
  4082. bitmap_bh = ext4_read_block_bitmap(sb, block_group);
  4083. if (!bitmap_bh) {
  4084. err = -EIO;
  4085. goto error_return;
  4086. }
  4087. gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
  4088. if (!gdp) {
  4089. err = -EIO;
  4090. goto error_return;
  4091. }
  4092. if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
  4093. in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
  4094. in_range(block, ext4_inode_table(sb, gdp),
  4095. EXT4_SB(sb)->s_itb_per_group) ||
  4096. in_range(block + count - 1, ext4_inode_table(sb, gdp),
  4097. EXT4_SB(sb)->s_itb_per_group)) {
  4098. ext4_error(sb, "Freeing blocks in system zone - "
  4099. "Block = %llu, count = %lu", block, count);
  4100. /* err = 0. ext4_std_error should be a no op */
  4101. goto error_return;
  4102. }
  4103. BUFFER_TRACE(bitmap_bh, "getting write access");
  4104. err = ext4_journal_get_write_access(handle, bitmap_bh);
  4105. if (err)
  4106. goto error_return;
  4107. /*
  4108. * We are about to modify some metadata. Call the journal APIs
  4109. * to unshare ->b_data if a currently-committing transaction is
  4110. * using it
  4111. */
  4112. BUFFER_TRACE(gd_bh, "get_write_access");
  4113. err = ext4_journal_get_write_access(handle, gd_bh);
  4114. if (err)
  4115. goto error_return;
  4116. #ifdef AGGRESSIVE_CHECK
  4117. {
  4118. int i;
  4119. for (i = 0; i < count; i++)
  4120. BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
  4121. }
  4122. #endif
  4123. if (ac) {
  4124. ac->ac_b_ex.fe_group = block_group;
  4125. ac->ac_b_ex.fe_start = bit;
  4126. ac->ac_b_ex.fe_len = count;
  4127. trace_ext4_mballoc_free(ac);
  4128. }
  4129. err = ext4_mb_load_buddy(sb, block_group, &e4b);
  4130. if (err)
  4131. goto error_return;
  4132. if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
  4133. struct ext4_free_data *new_entry;
  4134. /*
  4135. * blocks being freed are metadata. these blocks shouldn't
  4136. * be used until this transaction is committed
  4137. */
  4138. new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
  4139. new_entry->start_blk = bit;
  4140. new_entry->group = block_group;
  4141. new_entry->count = count;
  4142. new_entry->t_tid = handle->h_transaction->t_tid;
  4143. ext4_lock_group(sb, block_group);
  4144. mb_clear_bits(bitmap_bh->b_data, bit, count);
  4145. ext4_mb_free_metadata(handle, &e4b, new_entry);
  4146. } else {
  4147. /* need to update group_info->bb_free and bitmap
  4148. * with group lock held. generate_buddy look at
  4149. * them with group lock_held
  4150. */
  4151. ext4_lock_group(sb, block_group);
  4152. mb_clear_bits(bitmap_bh->b_data, bit, count);
  4153. mb_free_blocks(inode, &e4b, bit, count);
  4154. ext4_mb_return_to_preallocation(inode, &e4b, block, count);
  4155. if (test_opt(sb, DISCARD))
  4156. ext4_issue_discard(sb, block_group, bit, count);
  4157. }
  4158. ret = ext4_free_blks_count(sb, gdp) + count;
  4159. ext4_free_blks_set(sb, gdp, ret);
  4160. gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
  4161. ext4_unlock_group(sb, block_group);
  4162. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  4163. if (sbi->s_log_groups_per_flex) {
  4164. ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
  4165. atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
  4166. }
  4167. ext4_mb_unload_buddy(&e4b);
  4168. freed += count;
  4169. /* We dirtied the bitmap block */
  4170. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  4171. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  4172. /* And the group descriptor block */
  4173. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  4174. ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
  4175. if (!err)
  4176. err = ret;
  4177. if (overflow && !err) {
  4178. block += count;
  4179. count = overflow;
  4180. put_bh(bitmap_bh);
  4181. goto do_more;
  4182. }
  4183. ext4_mark_super_dirty(sb);
  4184. error_return:
  4185. if (freed)
  4186. dquot_free_block(inode, freed);
  4187. brelse(bitmap_bh);
  4188. ext4_std_error(sb, err);
  4189. if (ac)
  4190. kmem_cache_free(ext4_ac_cachep, ac);
  4191. return;
  4192. }