mballoc.c 126 KB

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