mballoc.c 125 KB

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