mballoc.c 123 KB

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