mballoc.c 125 KB

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