mballoc.c 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657
  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. sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
  2205. if (sbi->s_mb_maxs == NULL) {
  2206. kfree(sbi->s_mb_maxs);
  2207. return -ENOMEM;
  2208. }
  2209. /* order 0 is regular bitmap */
  2210. sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
  2211. sbi->s_mb_offsets[0] = 0;
  2212. i = 1;
  2213. offset = 0;
  2214. max = sb->s_blocksize << 2;
  2215. do {
  2216. sbi->s_mb_offsets[i] = offset;
  2217. sbi->s_mb_maxs[i] = max;
  2218. offset += 1 << (sb->s_blocksize_bits - i);
  2219. max = max >> 1;
  2220. i++;
  2221. } while (i <= sb->s_blocksize_bits + 1);
  2222. /* init file for buddy data */
  2223. ret = ext4_mb_init_backend(sb);
  2224. if (ret != 0) {
  2225. kfree(sbi->s_mb_offsets);
  2226. kfree(sbi->s_mb_maxs);
  2227. return ret;
  2228. }
  2229. spin_lock_init(&sbi->s_md_lock);
  2230. spin_lock_init(&sbi->s_bal_lock);
  2231. sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
  2232. sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
  2233. sbi->s_mb_stats = MB_DEFAULT_STATS;
  2234. sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
  2235. sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
  2236. sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
  2237. sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
  2238. sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
  2239. if (sbi->s_locality_groups == NULL) {
  2240. kfree(sbi->s_mb_offsets);
  2241. kfree(sbi->s_mb_maxs);
  2242. return -ENOMEM;
  2243. }
  2244. for_each_possible_cpu(i) {
  2245. struct ext4_locality_group *lg;
  2246. lg = per_cpu_ptr(sbi->s_locality_groups, i);
  2247. mutex_init(&lg->lg_mutex);
  2248. for (j = 0; j < PREALLOC_TB_SIZE; j++)
  2249. INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
  2250. spin_lock_init(&lg->lg_prealloc_lock);
  2251. }
  2252. ext4_mb_init_per_dev_proc(sb);
  2253. ext4_mb_history_init(sb);
  2254. sbi->s_journal->j_commit_callback = release_blocks_on_commit;
  2255. printk(KERN_INFO "EXT4-fs: mballoc enabled\n");
  2256. return 0;
  2257. }
  2258. /* need to called with ext4 group lock (ext4_lock_group) */
  2259. static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
  2260. {
  2261. struct ext4_prealloc_space *pa;
  2262. struct list_head *cur, *tmp;
  2263. int count = 0;
  2264. list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
  2265. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  2266. list_del(&pa->pa_group_list);
  2267. count++;
  2268. kmem_cache_free(ext4_pspace_cachep, pa);
  2269. }
  2270. if (count)
  2271. mb_debug("mballoc: %u PAs left\n", count);
  2272. }
  2273. int ext4_mb_release(struct super_block *sb)
  2274. {
  2275. ext4_group_t i;
  2276. int num_meta_group_infos;
  2277. struct ext4_group_info *grinfo;
  2278. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2279. if (sbi->s_group_info) {
  2280. for (i = 0; i < sbi->s_groups_count; i++) {
  2281. grinfo = ext4_get_group_info(sb, i);
  2282. #ifdef DOUBLE_CHECK
  2283. kfree(grinfo->bb_bitmap);
  2284. #endif
  2285. ext4_lock_group(sb, i);
  2286. ext4_mb_cleanup_pa(grinfo);
  2287. ext4_unlock_group(sb, i);
  2288. kfree(grinfo);
  2289. }
  2290. num_meta_group_infos = (sbi->s_groups_count +
  2291. EXT4_DESC_PER_BLOCK(sb) - 1) >>
  2292. EXT4_DESC_PER_BLOCK_BITS(sb);
  2293. for (i = 0; i < num_meta_group_infos; i++)
  2294. kfree(sbi->s_group_info[i]);
  2295. kfree(sbi->s_group_info);
  2296. }
  2297. kfree(sbi->s_mb_offsets);
  2298. kfree(sbi->s_mb_maxs);
  2299. if (sbi->s_buddy_cache)
  2300. iput(sbi->s_buddy_cache);
  2301. if (sbi->s_mb_stats) {
  2302. printk(KERN_INFO
  2303. "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
  2304. atomic_read(&sbi->s_bal_allocated),
  2305. atomic_read(&sbi->s_bal_reqs),
  2306. atomic_read(&sbi->s_bal_success));
  2307. printk(KERN_INFO
  2308. "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
  2309. "%u 2^N hits, %u breaks, %u lost\n",
  2310. atomic_read(&sbi->s_bal_ex_scanned),
  2311. atomic_read(&sbi->s_bal_goals),
  2312. atomic_read(&sbi->s_bal_2orders),
  2313. atomic_read(&sbi->s_bal_breaks),
  2314. atomic_read(&sbi->s_mb_lost_chunks));
  2315. printk(KERN_INFO
  2316. "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
  2317. sbi->s_mb_buddies_generated++,
  2318. sbi->s_mb_generation_time);
  2319. printk(KERN_INFO
  2320. "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
  2321. atomic_read(&sbi->s_mb_preallocated),
  2322. atomic_read(&sbi->s_mb_discarded));
  2323. }
  2324. free_percpu(sbi->s_locality_groups);
  2325. ext4_mb_history_release(sb);
  2326. ext4_mb_destroy_per_dev_proc(sb);
  2327. return 0;
  2328. }
  2329. /*
  2330. * This function is called by the jbd2 layer once the commit has finished,
  2331. * so we know we can free the blocks that were released with that commit.
  2332. */
  2333. static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
  2334. {
  2335. struct super_block *sb = journal->j_private;
  2336. struct ext4_buddy e4b;
  2337. struct ext4_group_info *db;
  2338. int err, count = 0, count2 = 0;
  2339. struct ext4_free_data *entry;
  2340. ext4_fsblk_t discard_block;
  2341. struct list_head *l, *ltmp;
  2342. list_for_each_safe(l, ltmp, &txn->t_private_list) {
  2343. entry = list_entry(l, struct ext4_free_data, list);
  2344. mb_debug("gonna free %u blocks in group %lu (0x%p):",
  2345. entry->count, entry->group, entry);
  2346. err = ext4_mb_load_buddy(sb, entry->group, &e4b);
  2347. /* we expect to find existing buddy because it's pinned */
  2348. BUG_ON(err != 0);
  2349. db = e4b.bd_info;
  2350. /* there are blocks to put in buddy to make them really free */
  2351. count += entry->count;
  2352. count2++;
  2353. ext4_lock_group(sb, entry->group);
  2354. /* Take it out of per group rb tree */
  2355. rb_erase(&entry->node, &(db->bb_free_root));
  2356. mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
  2357. if (!db->bb_free_root.rb_node) {
  2358. /* No more items in the per group rb tree
  2359. * balance refcounts from ext4_mb_free_metadata()
  2360. */
  2361. page_cache_release(e4b.bd_buddy_page);
  2362. page_cache_release(e4b.bd_bitmap_page);
  2363. }
  2364. ext4_unlock_group(sb, entry->group);
  2365. discard_block = (ext4_fsblk_t) entry->group * EXT4_BLOCKS_PER_GROUP(sb)
  2366. + entry->start_blk
  2367. + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  2368. trace_mark(ext4_discard_blocks, "dev %s blk %llu count %u", sb->s_id,
  2369. (unsigned long long) discard_block, entry->count);
  2370. sb_issue_discard(sb, discard_block, entry->count);
  2371. kmem_cache_free(ext4_free_ext_cachep, entry);
  2372. ext4_mb_release_desc(&e4b);
  2373. }
  2374. mb_debug("freed %u blocks in %u structures\n", count, count2);
  2375. }
  2376. #define EXT4_MB_STATS_NAME "stats"
  2377. #define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
  2378. #define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
  2379. #define EXT4_MB_ORDER2_REQ "order2_req"
  2380. #define EXT4_MB_STREAM_REQ "stream_req"
  2381. #define EXT4_MB_GROUP_PREALLOC "group_prealloc"
  2382. static int ext4_mb_init_per_dev_proc(struct super_block *sb)
  2383. {
  2384. #ifdef CONFIG_PROC_FS
  2385. mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
  2386. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2387. struct proc_dir_entry *proc;
  2388. if (sbi->s_proc == NULL)
  2389. return -EINVAL;
  2390. EXT4_PROC_HANDLER(EXT4_MB_STATS_NAME, mb_stats);
  2391. EXT4_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME, mb_max_to_scan);
  2392. EXT4_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME, mb_min_to_scan);
  2393. EXT4_PROC_HANDLER(EXT4_MB_ORDER2_REQ, mb_order2_reqs);
  2394. EXT4_PROC_HANDLER(EXT4_MB_STREAM_REQ, mb_stream_request);
  2395. EXT4_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC, mb_group_prealloc);
  2396. return 0;
  2397. err_out:
  2398. remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
  2399. remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
  2400. remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
  2401. remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
  2402. remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
  2403. remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_proc);
  2404. return -ENOMEM;
  2405. #else
  2406. return 0;
  2407. #endif
  2408. }
  2409. static int ext4_mb_destroy_per_dev_proc(struct super_block *sb)
  2410. {
  2411. #ifdef CONFIG_PROC_FS
  2412. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2413. if (sbi->s_proc == NULL)
  2414. return -EINVAL;
  2415. remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
  2416. remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
  2417. remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
  2418. remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
  2419. remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
  2420. remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_proc);
  2421. #endif
  2422. return 0;
  2423. }
  2424. int __init init_ext4_mballoc(void)
  2425. {
  2426. ext4_pspace_cachep =
  2427. kmem_cache_create("ext4_prealloc_space",
  2428. sizeof(struct ext4_prealloc_space),
  2429. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2430. if (ext4_pspace_cachep == NULL)
  2431. return -ENOMEM;
  2432. ext4_ac_cachep =
  2433. kmem_cache_create("ext4_alloc_context",
  2434. sizeof(struct ext4_allocation_context),
  2435. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2436. if (ext4_ac_cachep == NULL) {
  2437. kmem_cache_destroy(ext4_pspace_cachep);
  2438. return -ENOMEM;
  2439. }
  2440. ext4_free_ext_cachep =
  2441. kmem_cache_create("ext4_free_block_extents",
  2442. sizeof(struct ext4_free_data),
  2443. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2444. if (ext4_free_ext_cachep == NULL) {
  2445. kmem_cache_destroy(ext4_pspace_cachep);
  2446. kmem_cache_destroy(ext4_ac_cachep);
  2447. return -ENOMEM;
  2448. }
  2449. return 0;
  2450. }
  2451. void exit_ext4_mballoc(void)
  2452. {
  2453. /* XXX: synchronize_rcu(); */
  2454. kmem_cache_destroy(ext4_pspace_cachep);
  2455. kmem_cache_destroy(ext4_ac_cachep);
  2456. kmem_cache_destroy(ext4_free_ext_cachep);
  2457. }
  2458. /*
  2459. * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
  2460. * Returns 0 if success or error code
  2461. */
  2462. static noinline_for_stack int
  2463. ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
  2464. handle_t *handle, unsigned long reserv_blks)
  2465. {
  2466. struct buffer_head *bitmap_bh = NULL;
  2467. struct ext4_super_block *es;
  2468. struct ext4_group_desc *gdp;
  2469. struct buffer_head *gdp_bh;
  2470. struct ext4_sb_info *sbi;
  2471. struct super_block *sb;
  2472. ext4_fsblk_t block;
  2473. int err, len;
  2474. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  2475. BUG_ON(ac->ac_b_ex.fe_len <= 0);
  2476. sb = ac->ac_sb;
  2477. sbi = EXT4_SB(sb);
  2478. es = sbi->s_es;
  2479. err = -EIO;
  2480. bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
  2481. if (!bitmap_bh)
  2482. goto out_err;
  2483. err = ext4_journal_get_write_access(handle, bitmap_bh);
  2484. if (err)
  2485. goto out_err;
  2486. err = -EIO;
  2487. gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
  2488. if (!gdp)
  2489. goto out_err;
  2490. ext4_debug("using block group %lu(%d)\n", ac->ac_b_ex.fe_group,
  2491. gdp->bg_free_blocks_count);
  2492. err = ext4_journal_get_write_access(handle, gdp_bh);
  2493. if (err)
  2494. goto out_err;
  2495. block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
  2496. + ac->ac_b_ex.fe_start
  2497. + le32_to_cpu(es->s_first_data_block);
  2498. len = ac->ac_b_ex.fe_len;
  2499. if (in_range(ext4_block_bitmap(sb, gdp), block, len) ||
  2500. in_range(ext4_inode_bitmap(sb, gdp), block, len) ||
  2501. in_range(block, ext4_inode_table(sb, gdp),
  2502. EXT4_SB(sb)->s_itb_per_group) ||
  2503. in_range(block + len - 1, ext4_inode_table(sb, gdp),
  2504. EXT4_SB(sb)->s_itb_per_group)) {
  2505. ext4_error(sb, __func__,
  2506. "Allocating block in system zone - block = %llu",
  2507. block);
  2508. /* File system mounted not to panic on error
  2509. * Fix the bitmap and repeat the block allocation
  2510. * We leak some of the blocks here.
  2511. */
  2512. mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group),
  2513. bitmap_bh->b_data, ac->ac_b_ex.fe_start,
  2514. ac->ac_b_ex.fe_len);
  2515. err = ext4_journal_dirty_metadata(handle, bitmap_bh);
  2516. if (!err)
  2517. err = -EAGAIN;
  2518. goto out_err;
  2519. }
  2520. #ifdef AGGRESSIVE_CHECK
  2521. {
  2522. int i;
  2523. for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
  2524. BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
  2525. bitmap_bh->b_data));
  2526. }
  2527. }
  2528. #endif
  2529. mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group), bitmap_bh->b_data,
  2530. ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
  2531. spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
  2532. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2533. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  2534. gdp->bg_free_blocks_count =
  2535. cpu_to_le16(ext4_free_blocks_after_init(sb,
  2536. ac->ac_b_ex.fe_group,
  2537. gdp));
  2538. }
  2539. le16_add_cpu(&gdp->bg_free_blocks_count, -ac->ac_b_ex.fe_len);
  2540. gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
  2541. spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
  2542. percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
  2543. /*
  2544. * Now reduce the dirty block count also. Should not go negative
  2545. */
  2546. if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
  2547. /* release all the reserved blocks if non delalloc */
  2548. percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
  2549. else
  2550. percpu_counter_sub(&sbi->s_dirtyblocks_counter,
  2551. ac->ac_b_ex.fe_len);
  2552. if (sbi->s_log_groups_per_flex) {
  2553. ext4_group_t flex_group = ext4_flex_group(sbi,
  2554. ac->ac_b_ex.fe_group);
  2555. spin_lock(sb_bgl_lock(sbi, flex_group));
  2556. sbi->s_flex_groups[flex_group].free_blocks -= ac->ac_b_ex.fe_len;
  2557. spin_unlock(sb_bgl_lock(sbi, flex_group));
  2558. }
  2559. err = ext4_journal_dirty_metadata(handle, bitmap_bh);
  2560. if (err)
  2561. goto out_err;
  2562. err = ext4_journal_dirty_metadata(handle, gdp_bh);
  2563. out_err:
  2564. sb->s_dirt = 1;
  2565. brelse(bitmap_bh);
  2566. return err;
  2567. }
  2568. /*
  2569. * here we normalize request for locality group
  2570. * Group request are normalized to s_strip size if we set the same via mount
  2571. * option. If not we set it to s_mb_group_prealloc which can be configured via
  2572. * /proc/fs/ext4/<partition>/group_prealloc
  2573. *
  2574. * XXX: should we try to preallocate more than the group has now?
  2575. */
  2576. static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
  2577. {
  2578. struct super_block *sb = ac->ac_sb;
  2579. struct ext4_locality_group *lg = ac->ac_lg;
  2580. BUG_ON(lg == NULL);
  2581. if (EXT4_SB(sb)->s_stripe)
  2582. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
  2583. else
  2584. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
  2585. mb_debug("#%u: goal %u blocks for locality group\n",
  2586. current->pid, ac->ac_g_ex.fe_len);
  2587. }
  2588. /*
  2589. * Normalization means making request better in terms of
  2590. * size and alignment
  2591. */
  2592. static noinline_for_stack void
  2593. ext4_mb_normalize_request(struct ext4_allocation_context *ac,
  2594. struct ext4_allocation_request *ar)
  2595. {
  2596. int bsbits, max;
  2597. ext4_lblk_t end;
  2598. loff_t size, orig_size, start_off;
  2599. ext4_lblk_t start, orig_start;
  2600. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2601. struct ext4_prealloc_space *pa;
  2602. /* do normalize only data requests, metadata requests
  2603. do not need preallocation */
  2604. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2605. return;
  2606. /* sometime caller may want exact blocks */
  2607. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  2608. return;
  2609. /* caller may indicate that preallocation isn't
  2610. * required (it's a tail, for example) */
  2611. if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
  2612. return;
  2613. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
  2614. ext4_mb_normalize_group_request(ac);
  2615. return ;
  2616. }
  2617. bsbits = ac->ac_sb->s_blocksize_bits;
  2618. /* first, let's learn actual file size
  2619. * given current request is allocated */
  2620. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  2621. size = size << bsbits;
  2622. if (size < i_size_read(ac->ac_inode))
  2623. size = i_size_read(ac->ac_inode);
  2624. /* max size of free chunks */
  2625. max = 2 << bsbits;
  2626. #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
  2627. (req <= (size) || max <= (chunk_size))
  2628. /* first, try to predict filesize */
  2629. /* XXX: should this table be tunable? */
  2630. start_off = 0;
  2631. if (size <= 16 * 1024) {
  2632. size = 16 * 1024;
  2633. } else if (size <= 32 * 1024) {
  2634. size = 32 * 1024;
  2635. } else if (size <= 64 * 1024) {
  2636. size = 64 * 1024;
  2637. } else if (size <= 128 * 1024) {
  2638. size = 128 * 1024;
  2639. } else if (size <= 256 * 1024) {
  2640. size = 256 * 1024;
  2641. } else if (size <= 512 * 1024) {
  2642. size = 512 * 1024;
  2643. } else if (size <= 1024 * 1024) {
  2644. size = 1024 * 1024;
  2645. } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
  2646. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2647. (21 - bsbits)) << 21;
  2648. size = 2 * 1024 * 1024;
  2649. } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
  2650. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2651. (22 - bsbits)) << 22;
  2652. size = 4 * 1024 * 1024;
  2653. } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
  2654. (8<<20)>>bsbits, max, 8 * 1024)) {
  2655. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2656. (23 - bsbits)) << 23;
  2657. size = 8 * 1024 * 1024;
  2658. } else {
  2659. start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
  2660. size = ac->ac_o_ex.fe_len << bsbits;
  2661. }
  2662. orig_size = size = size >> bsbits;
  2663. orig_start = start = start_off >> bsbits;
  2664. /* don't cover already allocated blocks in selected range */
  2665. if (ar->pleft && start <= ar->lleft) {
  2666. size -= ar->lleft + 1 - start;
  2667. start = ar->lleft + 1;
  2668. }
  2669. if (ar->pright && start + size - 1 >= ar->lright)
  2670. size -= start + size - ar->lright;
  2671. end = start + size;
  2672. /* check we don't cross already preallocated blocks */
  2673. rcu_read_lock();
  2674. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2675. unsigned long pa_end;
  2676. if (pa->pa_deleted)
  2677. continue;
  2678. spin_lock(&pa->pa_lock);
  2679. if (pa->pa_deleted) {
  2680. spin_unlock(&pa->pa_lock);
  2681. continue;
  2682. }
  2683. pa_end = pa->pa_lstart + pa->pa_len;
  2684. /* PA must not overlap original request */
  2685. BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
  2686. ac->ac_o_ex.fe_logical < pa->pa_lstart));
  2687. /* skip PA normalized request doesn't overlap with */
  2688. if (pa->pa_lstart >= end) {
  2689. spin_unlock(&pa->pa_lock);
  2690. continue;
  2691. }
  2692. if (pa_end <= start) {
  2693. spin_unlock(&pa->pa_lock);
  2694. continue;
  2695. }
  2696. BUG_ON(pa->pa_lstart <= start && pa_end >= end);
  2697. if (pa_end <= ac->ac_o_ex.fe_logical) {
  2698. BUG_ON(pa_end < start);
  2699. start = pa_end;
  2700. }
  2701. if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
  2702. BUG_ON(pa->pa_lstart > end);
  2703. end = pa->pa_lstart;
  2704. }
  2705. spin_unlock(&pa->pa_lock);
  2706. }
  2707. rcu_read_unlock();
  2708. size = end - start;
  2709. /* XXX: extra loop to check we really don't overlap preallocations */
  2710. rcu_read_lock();
  2711. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2712. unsigned long pa_end;
  2713. spin_lock(&pa->pa_lock);
  2714. if (pa->pa_deleted == 0) {
  2715. pa_end = pa->pa_lstart + pa->pa_len;
  2716. BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
  2717. }
  2718. spin_unlock(&pa->pa_lock);
  2719. }
  2720. rcu_read_unlock();
  2721. if (start + size <= ac->ac_o_ex.fe_logical &&
  2722. start > ac->ac_o_ex.fe_logical) {
  2723. printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
  2724. (unsigned long) start, (unsigned long) size,
  2725. (unsigned long) ac->ac_o_ex.fe_logical);
  2726. }
  2727. BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
  2728. start > ac->ac_o_ex.fe_logical);
  2729. BUG_ON(size <= 0 || size >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  2730. /* now prepare goal request */
  2731. /* XXX: is it better to align blocks WRT to logical
  2732. * placement or satisfy big request as is */
  2733. ac->ac_g_ex.fe_logical = start;
  2734. ac->ac_g_ex.fe_len = size;
  2735. /* define goal start in order to merge */
  2736. if (ar->pright && (ar->lright == (start + size))) {
  2737. /* merge to the right */
  2738. ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
  2739. &ac->ac_f_ex.fe_group,
  2740. &ac->ac_f_ex.fe_start);
  2741. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2742. }
  2743. if (ar->pleft && (ar->lleft + 1 == start)) {
  2744. /* merge to the left */
  2745. ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
  2746. &ac->ac_f_ex.fe_group,
  2747. &ac->ac_f_ex.fe_start);
  2748. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2749. }
  2750. mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
  2751. (unsigned) orig_size, (unsigned) start);
  2752. }
  2753. static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
  2754. {
  2755. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2756. if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
  2757. atomic_inc(&sbi->s_bal_reqs);
  2758. atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
  2759. if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
  2760. atomic_inc(&sbi->s_bal_success);
  2761. atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
  2762. if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
  2763. ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
  2764. atomic_inc(&sbi->s_bal_goals);
  2765. if (ac->ac_found > sbi->s_mb_max_to_scan)
  2766. atomic_inc(&sbi->s_bal_breaks);
  2767. }
  2768. ext4_mb_store_history(ac);
  2769. }
  2770. /*
  2771. * use blocks preallocated to inode
  2772. */
  2773. static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
  2774. struct ext4_prealloc_space *pa)
  2775. {
  2776. ext4_fsblk_t start;
  2777. ext4_fsblk_t end;
  2778. int len;
  2779. /* found preallocated blocks, use them */
  2780. start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
  2781. end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
  2782. len = end - start;
  2783. ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
  2784. &ac->ac_b_ex.fe_start);
  2785. ac->ac_b_ex.fe_len = len;
  2786. ac->ac_status = AC_STATUS_FOUND;
  2787. ac->ac_pa = pa;
  2788. BUG_ON(start < pa->pa_pstart);
  2789. BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
  2790. BUG_ON(pa->pa_free < len);
  2791. pa->pa_free -= len;
  2792. mb_debug("use %llu/%u from inode pa %p\n", start, len, pa);
  2793. }
  2794. /*
  2795. * use blocks preallocated to locality group
  2796. */
  2797. static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
  2798. struct ext4_prealloc_space *pa)
  2799. {
  2800. unsigned int len = ac->ac_o_ex.fe_len;
  2801. ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
  2802. &ac->ac_b_ex.fe_group,
  2803. &ac->ac_b_ex.fe_start);
  2804. ac->ac_b_ex.fe_len = len;
  2805. ac->ac_status = AC_STATUS_FOUND;
  2806. ac->ac_pa = pa;
  2807. /* we don't correct pa_pstart or pa_plen here to avoid
  2808. * possible race when the group is being loaded concurrently
  2809. * instead we correct pa later, after blocks are marked
  2810. * in on-disk bitmap -- see ext4_mb_release_context()
  2811. * Other CPUs are prevented from allocating from this pa by lg_mutex
  2812. */
  2813. mb_debug("use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
  2814. }
  2815. /*
  2816. * Return the prealloc space that have minimal distance
  2817. * from the goal block. @cpa is the prealloc
  2818. * space that is having currently known minimal distance
  2819. * from the goal block.
  2820. */
  2821. static struct ext4_prealloc_space *
  2822. ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
  2823. struct ext4_prealloc_space *pa,
  2824. struct ext4_prealloc_space *cpa)
  2825. {
  2826. ext4_fsblk_t cur_distance, new_distance;
  2827. if (cpa == NULL) {
  2828. atomic_inc(&pa->pa_count);
  2829. return pa;
  2830. }
  2831. cur_distance = abs(goal_block - cpa->pa_pstart);
  2832. new_distance = abs(goal_block - pa->pa_pstart);
  2833. if (cur_distance < new_distance)
  2834. return cpa;
  2835. /* drop the previous reference */
  2836. atomic_dec(&cpa->pa_count);
  2837. atomic_inc(&pa->pa_count);
  2838. return pa;
  2839. }
  2840. /*
  2841. * search goal blocks in preallocated space
  2842. */
  2843. static noinline_for_stack int
  2844. ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
  2845. {
  2846. int order, i;
  2847. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2848. struct ext4_locality_group *lg;
  2849. struct ext4_prealloc_space *pa, *cpa = NULL;
  2850. ext4_fsblk_t goal_block;
  2851. /* only data can be preallocated */
  2852. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2853. return 0;
  2854. /* first, try per-file preallocation */
  2855. rcu_read_lock();
  2856. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2857. /* all fields in this condition don't change,
  2858. * so we can skip locking for them */
  2859. if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
  2860. ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
  2861. continue;
  2862. /* found preallocated blocks, use them */
  2863. spin_lock(&pa->pa_lock);
  2864. if (pa->pa_deleted == 0 && pa->pa_free) {
  2865. atomic_inc(&pa->pa_count);
  2866. ext4_mb_use_inode_pa(ac, pa);
  2867. spin_unlock(&pa->pa_lock);
  2868. ac->ac_criteria = 10;
  2869. rcu_read_unlock();
  2870. return 1;
  2871. }
  2872. spin_unlock(&pa->pa_lock);
  2873. }
  2874. rcu_read_unlock();
  2875. /* can we use group allocation? */
  2876. if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
  2877. return 0;
  2878. /* inode may have no locality group for some reason */
  2879. lg = ac->ac_lg;
  2880. if (lg == NULL)
  2881. return 0;
  2882. order = fls(ac->ac_o_ex.fe_len) - 1;
  2883. if (order > PREALLOC_TB_SIZE - 1)
  2884. /* The max size of hash table is PREALLOC_TB_SIZE */
  2885. order = PREALLOC_TB_SIZE - 1;
  2886. goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) +
  2887. ac->ac_g_ex.fe_start +
  2888. le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block);
  2889. /*
  2890. * search for the prealloc space that is having
  2891. * minimal distance from the goal block.
  2892. */
  2893. for (i = order; i < PREALLOC_TB_SIZE; i++) {
  2894. rcu_read_lock();
  2895. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
  2896. pa_inode_list) {
  2897. spin_lock(&pa->pa_lock);
  2898. if (pa->pa_deleted == 0 &&
  2899. pa->pa_free >= ac->ac_o_ex.fe_len) {
  2900. cpa = ext4_mb_check_group_pa(goal_block,
  2901. pa, cpa);
  2902. }
  2903. spin_unlock(&pa->pa_lock);
  2904. }
  2905. rcu_read_unlock();
  2906. }
  2907. if (cpa) {
  2908. ext4_mb_use_group_pa(ac, cpa);
  2909. ac->ac_criteria = 20;
  2910. return 1;
  2911. }
  2912. return 0;
  2913. }
  2914. /*
  2915. * the function goes through all preallocation in this group and marks them
  2916. * used in in-core bitmap. buddy must be generated from this bitmap
  2917. * Need to be called with ext4 group lock (ext4_lock_group)
  2918. */
  2919. static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  2920. ext4_group_t group)
  2921. {
  2922. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  2923. struct ext4_prealloc_space *pa;
  2924. struct list_head *cur;
  2925. ext4_group_t groupnr;
  2926. ext4_grpblk_t start;
  2927. int preallocated = 0;
  2928. int count = 0;
  2929. int len;
  2930. /* all form of preallocation discards first load group,
  2931. * so the only competing code is preallocation use.
  2932. * we don't need any locking here
  2933. * notice we do NOT ignore preallocations with pa_deleted
  2934. * otherwise we could leave used blocks available for
  2935. * allocation in buddy when concurrent ext4_mb_put_pa()
  2936. * is dropping preallocation
  2937. */
  2938. list_for_each(cur, &grp->bb_prealloc_list) {
  2939. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  2940. spin_lock(&pa->pa_lock);
  2941. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  2942. &groupnr, &start);
  2943. len = pa->pa_len;
  2944. spin_unlock(&pa->pa_lock);
  2945. if (unlikely(len == 0))
  2946. continue;
  2947. BUG_ON(groupnr != group);
  2948. mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
  2949. bitmap, start, len);
  2950. preallocated += len;
  2951. count++;
  2952. }
  2953. mb_debug("prellocated %u for group %lu\n", preallocated, group);
  2954. }
  2955. static void ext4_mb_pa_callback(struct rcu_head *head)
  2956. {
  2957. struct ext4_prealloc_space *pa;
  2958. pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
  2959. kmem_cache_free(ext4_pspace_cachep, pa);
  2960. }
  2961. /*
  2962. * drops a reference to preallocated space descriptor
  2963. * if this was the last reference and the space is consumed
  2964. */
  2965. static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
  2966. struct super_block *sb, struct ext4_prealloc_space *pa)
  2967. {
  2968. unsigned long grp;
  2969. if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
  2970. return;
  2971. /* in this short window concurrent discard can set pa_deleted */
  2972. spin_lock(&pa->pa_lock);
  2973. if (pa->pa_deleted == 1) {
  2974. spin_unlock(&pa->pa_lock);
  2975. return;
  2976. }
  2977. pa->pa_deleted = 1;
  2978. spin_unlock(&pa->pa_lock);
  2979. /* -1 is to protect from crossing allocation group */
  2980. ext4_get_group_no_and_offset(sb, pa->pa_pstart - 1, &grp, NULL);
  2981. /*
  2982. * possible race:
  2983. *
  2984. * P1 (buddy init) P2 (regular allocation)
  2985. * find block B in PA
  2986. * copy on-disk bitmap to buddy
  2987. * mark B in on-disk bitmap
  2988. * drop PA from group
  2989. * mark all PAs in buddy
  2990. *
  2991. * thus, P1 initializes buddy with B available. to prevent this
  2992. * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
  2993. * against that pair
  2994. */
  2995. ext4_lock_group(sb, grp);
  2996. list_del(&pa->pa_group_list);
  2997. ext4_unlock_group(sb, grp);
  2998. spin_lock(pa->pa_obj_lock);
  2999. list_del_rcu(&pa->pa_inode_list);
  3000. spin_unlock(pa->pa_obj_lock);
  3001. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3002. }
  3003. /*
  3004. * creates new preallocated space for given inode
  3005. */
  3006. static noinline_for_stack int
  3007. ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
  3008. {
  3009. struct super_block *sb = ac->ac_sb;
  3010. struct ext4_prealloc_space *pa;
  3011. struct ext4_group_info *grp;
  3012. struct ext4_inode_info *ei;
  3013. /* preallocate only when found space is larger then requested */
  3014. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3015. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3016. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3017. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3018. if (pa == NULL)
  3019. return -ENOMEM;
  3020. if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
  3021. int winl;
  3022. int wins;
  3023. int win;
  3024. int offs;
  3025. /* we can't allocate as much as normalizer wants.
  3026. * so, found space must get proper lstart
  3027. * to cover original request */
  3028. BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
  3029. BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
  3030. /* we're limited by original request in that
  3031. * logical block must be covered any way
  3032. * winl is window we can move our chunk within */
  3033. winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
  3034. /* also, we should cover whole original request */
  3035. wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
  3036. /* the smallest one defines real window */
  3037. win = min(winl, wins);
  3038. offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
  3039. if (offs && offs < win)
  3040. win = offs;
  3041. ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
  3042. BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
  3043. BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
  3044. }
  3045. /* preallocation can change ac_b_ex, thus we store actually
  3046. * allocated blocks for history */
  3047. ac->ac_f_ex = ac->ac_b_ex;
  3048. pa->pa_lstart = ac->ac_b_ex.fe_logical;
  3049. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3050. pa->pa_len = ac->ac_b_ex.fe_len;
  3051. pa->pa_free = pa->pa_len;
  3052. atomic_set(&pa->pa_count, 1);
  3053. spin_lock_init(&pa->pa_lock);
  3054. pa->pa_deleted = 0;
  3055. pa->pa_linear = 0;
  3056. mb_debug("new inode pa %p: %llu/%u for %u\n", pa,
  3057. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3058. ext4_mb_use_inode_pa(ac, pa);
  3059. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3060. ei = EXT4_I(ac->ac_inode);
  3061. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3062. pa->pa_obj_lock = &ei->i_prealloc_lock;
  3063. pa->pa_inode = ac->ac_inode;
  3064. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3065. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3066. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3067. spin_lock(pa->pa_obj_lock);
  3068. list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
  3069. spin_unlock(pa->pa_obj_lock);
  3070. return 0;
  3071. }
  3072. /*
  3073. * creates new preallocated space for locality group inodes belongs to
  3074. */
  3075. static noinline_for_stack int
  3076. ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
  3077. {
  3078. struct super_block *sb = ac->ac_sb;
  3079. struct ext4_locality_group *lg;
  3080. struct ext4_prealloc_space *pa;
  3081. struct ext4_group_info *grp;
  3082. /* preallocate only when found space is larger then requested */
  3083. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3084. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3085. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3086. BUG_ON(ext4_pspace_cachep == NULL);
  3087. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3088. if (pa == NULL)
  3089. return -ENOMEM;
  3090. /* preallocation can change ac_b_ex, thus we store actually
  3091. * allocated blocks for history */
  3092. ac->ac_f_ex = ac->ac_b_ex;
  3093. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3094. pa->pa_lstart = pa->pa_pstart;
  3095. pa->pa_len = ac->ac_b_ex.fe_len;
  3096. pa->pa_free = pa->pa_len;
  3097. atomic_set(&pa->pa_count, 1);
  3098. spin_lock_init(&pa->pa_lock);
  3099. INIT_LIST_HEAD(&pa->pa_inode_list);
  3100. pa->pa_deleted = 0;
  3101. pa->pa_linear = 1;
  3102. mb_debug("new group pa %p: %llu/%u for %u\n", pa,
  3103. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3104. ext4_mb_use_group_pa(ac, pa);
  3105. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3106. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3107. lg = ac->ac_lg;
  3108. BUG_ON(lg == NULL);
  3109. pa->pa_obj_lock = &lg->lg_prealloc_lock;
  3110. pa->pa_inode = NULL;
  3111. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3112. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3113. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3114. /*
  3115. * We will later add the new pa to the right bucket
  3116. * after updating the pa_free in ext4_mb_release_context
  3117. */
  3118. return 0;
  3119. }
  3120. static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
  3121. {
  3122. int err;
  3123. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3124. err = ext4_mb_new_group_pa(ac);
  3125. else
  3126. err = ext4_mb_new_inode_pa(ac);
  3127. return err;
  3128. }
  3129. /*
  3130. * finds all unused blocks in on-disk bitmap, frees them in
  3131. * in-core bitmap and buddy.
  3132. * @pa must be unlinked from inode and group lists, so that
  3133. * nobody else can find/use it.
  3134. * the caller MUST hold group/inode locks.
  3135. * TODO: optimize the case when there are no in-core structures yet
  3136. */
  3137. static noinline_for_stack int
  3138. ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
  3139. struct ext4_prealloc_space *pa,
  3140. struct ext4_allocation_context *ac)
  3141. {
  3142. struct super_block *sb = e4b->bd_sb;
  3143. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3144. unsigned long end;
  3145. unsigned long next;
  3146. ext4_group_t group;
  3147. ext4_grpblk_t bit;
  3148. sector_t start;
  3149. int err = 0;
  3150. int free = 0;
  3151. BUG_ON(pa->pa_deleted == 0);
  3152. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3153. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3154. end = bit + pa->pa_len;
  3155. if (ac) {
  3156. ac->ac_sb = sb;
  3157. ac->ac_inode = pa->pa_inode;
  3158. ac->ac_op = EXT4_MB_HISTORY_DISCARD;
  3159. }
  3160. while (bit < end) {
  3161. bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
  3162. if (bit >= end)
  3163. break;
  3164. next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
  3165. start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
  3166. le32_to_cpu(sbi->s_es->s_first_data_block);
  3167. mb_debug(" free preallocated %u/%u in group %u\n",
  3168. (unsigned) start, (unsigned) next - bit,
  3169. (unsigned) group);
  3170. free += next - bit;
  3171. if (ac) {
  3172. ac->ac_b_ex.fe_group = group;
  3173. ac->ac_b_ex.fe_start = bit;
  3174. ac->ac_b_ex.fe_len = next - bit;
  3175. ac->ac_b_ex.fe_logical = 0;
  3176. ext4_mb_store_history(ac);
  3177. }
  3178. mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
  3179. bit = next + 1;
  3180. }
  3181. if (free != pa->pa_free) {
  3182. printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
  3183. pa, (unsigned long) pa->pa_lstart,
  3184. (unsigned long) pa->pa_pstart,
  3185. (unsigned long) pa->pa_len);
  3186. ext4_error(sb, __func__, "free %u, pa_free %u\n",
  3187. free, pa->pa_free);
  3188. /*
  3189. * pa is already deleted so we use the value obtained
  3190. * from the bitmap and continue.
  3191. */
  3192. }
  3193. atomic_add(free, &sbi->s_mb_discarded);
  3194. return err;
  3195. }
  3196. static noinline_for_stack int
  3197. ext4_mb_release_group_pa(struct ext4_buddy *e4b,
  3198. struct ext4_prealloc_space *pa,
  3199. struct ext4_allocation_context *ac)
  3200. {
  3201. struct super_block *sb = e4b->bd_sb;
  3202. ext4_group_t group;
  3203. ext4_grpblk_t bit;
  3204. if (ac)
  3205. ac->ac_op = EXT4_MB_HISTORY_DISCARD;
  3206. BUG_ON(pa->pa_deleted == 0);
  3207. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3208. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3209. mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
  3210. atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
  3211. if (ac) {
  3212. ac->ac_sb = sb;
  3213. ac->ac_inode = NULL;
  3214. ac->ac_b_ex.fe_group = group;
  3215. ac->ac_b_ex.fe_start = bit;
  3216. ac->ac_b_ex.fe_len = pa->pa_len;
  3217. ac->ac_b_ex.fe_logical = 0;
  3218. ext4_mb_store_history(ac);
  3219. }
  3220. return 0;
  3221. }
  3222. /*
  3223. * releases all preallocations in given group
  3224. *
  3225. * first, we need to decide discard policy:
  3226. * - when do we discard
  3227. * 1) ENOSPC
  3228. * - how many do we discard
  3229. * 1) how many requested
  3230. */
  3231. static noinline_for_stack int
  3232. ext4_mb_discard_group_preallocations(struct super_block *sb,
  3233. ext4_group_t group, int needed)
  3234. {
  3235. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3236. struct buffer_head *bitmap_bh = NULL;
  3237. struct ext4_prealloc_space *pa, *tmp;
  3238. struct ext4_allocation_context *ac;
  3239. struct list_head list;
  3240. struct ext4_buddy e4b;
  3241. int err;
  3242. int busy = 0;
  3243. int free = 0;
  3244. mb_debug("discard preallocation for group %lu\n", group);
  3245. if (list_empty(&grp->bb_prealloc_list))
  3246. return 0;
  3247. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3248. if (bitmap_bh == NULL) {
  3249. ext4_error(sb, __func__, "Error in reading block "
  3250. "bitmap for %lu\n", group);
  3251. return 0;
  3252. }
  3253. err = ext4_mb_load_buddy(sb, group, &e4b);
  3254. if (err) {
  3255. ext4_error(sb, __func__, "Error in loading buddy "
  3256. "information for %lu\n", group);
  3257. put_bh(bitmap_bh);
  3258. return 0;
  3259. }
  3260. if (needed == 0)
  3261. needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
  3262. INIT_LIST_HEAD(&list);
  3263. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3264. repeat:
  3265. ext4_lock_group(sb, group);
  3266. list_for_each_entry_safe(pa, tmp,
  3267. &grp->bb_prealloc_list, pa_group_list) {
  3268. spin_lock(&pa->pa_lock);
  3269. if (atomic_read(&pa->pa_count)) {
  3270. spin_unlock(&pa->pa_lock);
  3271. busy = 1;
  3272. continue;
  3273. }
  3274. if (pa->pa_deleted) {
  3275. spin_unlock(&pa->pa_lock);
  3276. continue;
  3277. }
  3278. /* seems this one can be freed ... */
  3279. pa->pa_deleted = 1;
  3280. /* we can trust pa_free ... */
  3281. free += pa->pa_free;
  3282. spin_unlock(&pa->pa_lock);
  3283. list_del(&pa->pa_group_list);
  3284. list_add(&pa->u.pa_tmp_list, &list);
  3285. }
  3286. /* if we still need more blocks and some PAs were used, try again */
  3287. if (free < needed && busy) {
  3288. busy = 0;
  3289. ext4_unlock_group(sb, group);
  3290. /*
  3291. * Yield the CPU here so that we don't get soft lockup
  3292. * in non preempt case.
  3293. */
  3294. yield();
  3295. goto repeat;
  3296. }
  3297. /* found anything to free? */
  3298. if (list_empty(&list)) {
  3299. BUG_ON(free != 0);
  3300. goto out;
  3301. }
  3302. /* now free all selected PAs */
  3303. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3304. /* remove from object (inode or locality group) */
  3305. spin_lock(pa->pa_obj_lock);
  3306. list_del_rcu(&pa->pa_inode_list);
  3307. spin_unlock(pa->pa_obj_lock);
  3308. if (pa->pa_linear)
  3309. ext4_mb_release_group_pa(&e4b, pa, ac);
  3310. else
  3311. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3312. list_del(&pa->u.pa_tmp_list);
  3313. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3314. }
  3315. out:
  3316. ext4_unlock_group(sb, group);
  3317. if (ac)
  3318. kmem_cache_free(ext4_ac_cachep, ac);
  3319. ext4_mb_release_desc(&e4b);
  3320. put_bh(bitmap_bh);
  3321. return free;
  3322. }
  3323. /*
  3324. * releases all non-used preallocated blocks for given inode
  3325. *
  3326. * It's important to discard preallocations under i_data_sem
  3327. * We don't want another block to be served from the prealloc
  3328. * space when we are discarding the inode prealloc space.
  3329. *
  3330. * FIXME!! Make sure it is valid at all the call sites
  3331. */
  3332. void ext4_discard_preallocations(struct inode *inode)
  3333. {
  3334. struct ext4_inode_info *ei = EXT4_I(inode);
  3335. struct super_block *sb = inode->i_sb;
  3336. struct buffer_head *bitmap_bh = NULL;
  3337. struct ext4_prealloc_space *pa, *tmp;
  3338. struct ext4_allocation_context *ac;
  3339. ext4_group_t group = 0;
  3340. struct list_head list;
  3341. struct ext4_buddy e4b;
  3342. int err;
  3343. if (!S_ISREG(inode->i_mode)) {
  3344. /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
  3345. return;
  3346. }
  3347. mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
  3348. INIT_LIST_HEAD(&list);
  3349. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3350. repeat:
  3351. /* first, collect all pa's in the inode */
  3352. spin_lock(&ei->i_prealloc_lock);
  3353. while (!list_empty(&ei->i_prealloc_list)) {
  3354. pa = list_entry(ei->i_prealloc_list.next,
  3355. struct ext4_prealloc_space, pa_inode_list);
  3356. BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
  3357. spin_lock(&pa->pa_lock);
  3358. if (atomic_read(&pa->pa_count)) {
  3359. /* this shouldn't happen often - nobody should
  3360. * use preallocation while we're discarding it */
  3361. spin_unlock(&pa->pa_lock);
  3362. spin_unlock(&ei->i_prealloc_lock);
  3363. printk(KERN_ERR "uh-oh! used pa while discarding\n");
  3364. WARN_ON(1);
  3365. schedule_timeout_uninterruptible(HZ);
  3366. goto repeat;
  3367. }
  3368. if (pa->pa_deleted == 0) {
  3369. pa->pa_deleted = 1;
  3370. spin_unlock(&pa->pa_lock);
  3371. list_del_rcu(&pa->pa_inode_list);
  3372. list_add(&pa->u.pa_tmp_list, &list);
  3373. continue;
  3374. }
  3375. /* someone is deleting pa right now */
  3376. spin_unlock(&pa->pa_lock);
  3377. spin_unlock(&ei->i_prealloc_lock);
  3378. /* we have to wait here because pa_deleted
  3379. * doesn't mean pa is already unlinked from
  3380. * the list. as we might be called from
  3381. * ->clear_inode() the inode will get freed
  3382. * and concurrent thread which is unlinking
  3383. * pa from inode's list may access already
  3384. * freed memory, bad-bad-bad */
  3385. /* XXX: if this happens too often, we can
  3386. * add a flag to force wait only in case
  3387. * of ->clear_inode(), but not in case of
  3388. * regular truncate */
  3389. schedule_timeout_uninterruptible(HZ);
  3390. goto repeat;
  3391. }
  3392. spin_unlock(&ei->i_prealloc_lock);
  3393. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3394. BUG_ON(pa->pa_linear != 0);
  3395. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3396. err = ext4_mb_load_buddy(sb, group, &e4b);
  3397. if (err) {
  3398. ext4_error(sb, __func__, "Error in loading buddy "
  3399. "information for %lu\n", group);
  3400. continue;
  3401. }
  3402. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3403. if (bitmap_bh == NULL) {
  3404. ext4_error(sb, __func__, "Error in reading block "
  3405. "bitmap for %lu\n", group);
  3406. ext4_mb_release_desc(&e4b);
  3407. continue;
  3408. }
  3409. ext4_lock_group(sb, group);
  3410. list_del(&pa->pa_group_list);
  3411. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
  3412. ext4_unlock_group(sb, group);
  3413. ext4_mb_release_desc(&e4b);
  3414. put_bh(bitmap_bh);
  3415. list_del(&pa->u.pa_tmp_list);
  3416. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3417. }
  3418. if (ac)
  3419. kmem_cache_free(ext4_ac_cachep, ac);
  3420. }
  3421. /*
  3422. * finds all preallocated spaces and return blocks being freed to them
  3423. * if preallocated space becomes full (no block is used from the space)
  3424. * then the function frees space in buddy
  3425. * XXX: at the moment, truncate (which is the only way to free blocks)
  3426. * discards all preallocations
  3427. */
  3428. static void ext4_mb_return_to_preallocation(struct inode *inode,
  3429. struct ext4_buddy *e4b,
  3430. sector_t block, int count)
  3431. {
  3432. BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
  3433. }
  3434. #ifdef MB_DEBUG
  3435. static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3436. {
  3437. struct super_block *sb = ac->ac_sb;
  3438. ext4_group_t i;
  3439. printk(KERN_ERR "EXT4-fs: Can't allocate:"
  3440. " Allocation context details:\n");
  3441. printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
  3442. ac->ac_status, ac->ac_flags);
  3443. printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
  3444. "best %lu/%lu/%lu@%lu cr %d\n",
  3445. (unsigned long)ac->ac_o_ex.fe_group,
  3446. (unsigned long)ac->ac_o_ex.fe_start,
  3447. (unsigned long)ac->ac_o_ex.fe_len,
  3448. (unsigned long)ac->ac_o_ex.fe_logical,
  3449. (unsigned long)ac->ac_g_ex.fe_group,
  3450. (unsigned long)ac->ac_g_ex.fe_start,
  3451. (unsigned long)ac->ac_g_ex.fe_len,
  3452. (unsigned long)ac->ac_g_ex.fe_logical,
  3453. (unsigned long)ac->ac_b_ex.fe_group,
  3454. (unsigned long)ac->ac_b_ex.fe_start,
  3455. (unsigned long)ac->ac_b_ex.fe_len,
  3456. (unsigned long)ac->ac_b_ex.fe_logical,
  3457. (int)ac->ac_criteria);
  3458. printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
  3459. ac->ac_found);
  3460. printk(KERN_ERR "EXT4-fs: groups: \n");
  3461. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  3462. struct ext4_group_info *grp = ext4_get_group_info(sb, i);
  3463. struct ext4_prealloc_space *pa;
  3464. ext4_grpblk_t start;
  3465. struct list_head *cur;
  3466. ext4_lock_group(sb, i);
  3467. list_for_each(cur, &grp->bb_prealloc_list) {
  3468. pa = list_entry(cur, struct ext4_prealloc_space,
  3469. pa_group_list);
  3470. spin_lock(&pa->pa_lock);
  3471. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3472. NULL, &start);
  3473. spin_unlock(&pa->pa_lock);
  3474. printk(KERN_ERR "PA:%lu:%d:%u \n", i,
  3475. start, pa->pa_len);
  3476. }
  3477. ext4_unlock_group(sb, i);
  3478. if (grp->bb_free == 0)
  3479. continue;
  3480. printk(KERN_ERR "%lu: %d/%d \n",
  3481. i, grp->bb_free, grp->bb_fragments);
  3482. }
  3483. printk(KERN_ERR "\n");
  3484. }
  3485. #else
  3486. static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3487. {
  3488. return;
  3489. }
  3490. #endif
  3491. /*
  3492. * We use locality group preallocation for small size file. The size of the
  3493. * file is determined by the current size or the resulting size after
  3494. * allocation which ever is larger
  3495. *
  3496. * One can tune this size via /proc/fs/ext4/<partition>/stream_req
  3497. */
  3498. static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
  3499. {
  3500. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  3501. int bsbits = ac->ac_sb->s_blocksize_bits;
  3502. loff_t size, isize;
  3503. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  3504. return;
  3505. size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
  3506. isize = i_size_read(ac->ac_inode) >> bsbits;
  3507. size = max(size, isize);
  3508. /* don't use group allocation for large files */
  3509. if (size >= sbi->s_mb_stream_request)
  3510. return;
  3511. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  3512. return;
  3513. BUG_ON(ac->ac_lg != NULL);
  3514. /*
  3515. * locality group prealloc space are per cpu. The reason for having
  3516. * per cpu locality group is to reduce the contention between block
  3517. * request from multiple CPUs.
  3518. */
  3519. ac->ac_lg = per_cpu_ptr(sbi->s_locality_groups, raw_smp_processor_id());
  3520. /* we're going to use group allocation */
  3521. ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
  3522. /* serialize all allocations in the group */
  3523. mutex_lock(&ac->ac_lg->lg_mutex);
  3524. }
  3525. static noinline_for_stack int
  3526. ext4_mb_initialize_context(struct ext4_allocation_context *ac,
  3527. struct ext4_allocation_request *ar)
  3528. {
  3529. struct super_block *sb = ar->inode->i_sb;
  3530. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3531. struct ext4_super_block *es = sbi->s_es;
  3532. ext4_group_t group;
  3533. unsigned long len;
  3534. unsigned long goal;
  3535. ext4_grpblk_t block;
  3536. /* we can't allocate > group size */
  3537. len = ar->len;
  3538. /* just a dirty hack to filter too big requests */
  3539. if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
  3540. len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
  3541. /* start searching from the goal */
  3542. goal = ar->goal;
  3543. if (goal < le32_to_cpu(es->s_first_data_block) ||
  3544. goal >= ext4_blocks_count(es))
  3545. goal = le32_to_cpu(es->s_first_data_block);
  3546. ext4_get_group_no_and_offset(sb, goal, &group, &block);
  3547. /* set up allocation goals */
  3548. ac->ac_b_ex.fe_logical = ar->logical;
  3549. ac->ac_b_ex.fe_group = 0;
  3550. ac->ac_b_ex.fe_start = 0;
  3551. ac->ac_b_ex.fe_len = 0;
  3552. ac->ac_status = AC_STATUS_CONTINUE;
  3553. ac->ac_groups_scanned = 0;
  3554. ac->ac_ex_scanned = 0;
  3555. ac->ac_found = 0;
  3556. ac->ac_sb = sb;
  3557. ac->ac_inode = ar->inode;
  3558. ac->ac_o_ex.fe_logical = ar->logical;
  3559. ac->ac_o_ex.fe_group = group;
  3560. ac->ac_o_ex.fe_start = block;
  3561. ac->ac_o_ex.fe_len = len;
  3562. ac->ac_g_ex.fe_logical = ar->logical;
  3563. ac->ac_g_ex.fe_group = group;
  3564. ac->ac_g_ex.fe_start = block;
  3565. ac->ac_g_ex.fe_len = len;
  3566. ac->ac_f_ex.fe_len = 0;
  3567. ac->ac_flags = ar->flags;
  3568. ac->ac_2order = 0;
  3569. ac->ac_criteria = 0;
  3570. ac->ac_pa = NULL;
  3571. ac->ac_bitmap_page = NULL;
  3572. ac->ac_buddy_page = NULL;
  3573. ac->ac_lg = NULL;
  3574. /* we have to define context: we'll we work with a file or
  3575. * locality group. this is a policy, actually */
  3576. ext4_mb_group_or_file(ac);
  3577. mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
  3578. "left: %u/%u, right %u/%u to %swritable\n",
  3579. (unsigned) ar->len, (unsigned) ar->logical,
  3580. (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
  3581. (unsigned) ar->lleft, (unsigned) ar->pleft,
  3582. (unsigned) ar->lright, (unsigned) ar->pright,
  3583. atomic_read(&ar->inode->i_writecount) ? "" : "non-");
  3584. return 0;
  3585. }
  3586. static noinline_for_stack void
  3587. ext4_mb_discard_lg_preallocations(struct super_block *sb,
  3588. struct ext4_locality_group *lg,
  3589. int order, int total_entries)
  3590. {
  3591. ext4_group_t group = 0;
  3592. struct ext4_buddy e4b;
  3593. struct list_head discard_list;
  3594. struct ext4_prealloc_space *pa, *tmp;
  3595. struct ext4_allocation_context *ac;
  3596. mb_debug("discard locality group preallocation\n");
  3597. INIT_LIST_HEAD(&discard_list);
  3598. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3599. spin_lock(&lg->lg_prealloc_lock);
  3600. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
  3601. pa_inode_list) {
  3602. spin_lock(&pa->pa_lock);
  3603. if (atomic_read(&pa->pa_count)) {
  3604. /*
  3605. * This is the pa that we just used
  3606. * for block allocation. So don't
  3607. * free that
  3608. */
  3609. spin_unlock(&pa->pa_lock);
  3610. continue;
  3611. }
  3612. if (pa->pa_deleted) {
  3613. spin_unlock(&pa->pa_lock);
  3614. continue;
  3615. }
  3616. /* only lg prealloc space */
  3617. BUG_ON(!pa->pa_linear);
  3618. /* seems this one can be freed ... */
  3619. pa->pa_deleted = 1;
  3620. spin_unlock(&pa->pa_lock);
  3621. list_del_rcu(&pa->pa_inode_list);
  3622. list_add(&pa->u.pa_tmp_list, &discard_list);
  3623. total_entries--;
  3624. if (total_entries <= 5) {
  3625. /*
  3626. * we want to keep only 5 entries
  3627. * allowing it to grow to 8. This
  3628. * mak sure we don't call discard
  3629. * soon for this list.
  3630. */
  3631. break;
  3632. }
  3633. }
  3634. spin_unlock(&lg->lg_prealloc_lock);
  3635. list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
  3636. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
  3637. if (ext4_mb_load_buddy(sb, group, &e4b)) {
  3638. ext4_error(sb, __func__, "Error in loading buddy "
  3639. "information for %lu\n", group);
  3640. continue;
  3641. }
  3642. ext4_lock_group(sb, group);
  3643. list_del(&pa->pa_group_list);
  3644. ext4_mb_release_group_pa(&e4b, pa, ac);
  3645. ext4_unlock_group(sb, group);
  3646. ext4_mb_release_desc(&e4b);
  3647. list_del(&pa->u.pa_tmp_list);
  3648. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3649. }
  3650. if (ac)
  3651. kmem_cache_free(ext4_ac_cachep, ac);
  3652. }
  3653. /*
  3654. * We have incremented pa_count. So it cannot be freed at this
  3655. * point. Also we hold lg_mutex. So no parallel allocation is
  3656. * possible from this lg. That means pa_free cannot be updated.
  3657. *
  3658. * A parallel ext4_mb_discard_group_preallocations is possible.
  3659. * which can cause the lg_prealloc_list to be updated.
  3660. */
  3661. static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
  3662. {
  3663. int order, added = 0, lg_prealloc_count = 1;
  3664. struct super_block *sb = ac->ac_sb;
  3665. struct ext4_locality_group *lg = ac->ac_lg;
  3666. struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
  3667. order = fls(pa->pa_free) - 1;
  3668. if (order > PREALLOC_TB_SIZE - 1)
  3669. /* The max size of hash table is PREALLOC_TB_SIZE */
  3670. order = PREALLOC_TB_SIZE - 1;
  3671. /* Add the prealloc space to lg */
  3672. rcu_read_lock();
  3673. list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
  3674. pa_inode_list) {
  3675. spin_lock(&tmp_pa->pa_lock);
  3676. if (tmp_pa->pa_deleted) {
  3677. spin_unlock(&pa->pa_lock);
  3678. continue;
  3679. }
  3680. if (!added && pa->pa_free < tmp_pa->pa_free) {
  3681. /* Add to the tail of the previous entry */
  3682. list_add_tail_rcu(&pa->pa_inode_list,
  3683. &tmp_pa->pa_inode_list);
  3684. added = 1;
  3685. /*
  3686. * we want to count the total
  3687. * number of entries in the list
  3688. */
  3689. }
  3690. spin_unlock(&tmp_pa->pa_lock);
  3691. lg_prealloc_count++;
  3692. }
  3693. if (!added)
  3694. list_add_tail_rcu(&pa->pa_inode_list,
  3695. &lg->lg_prealloc_list[order]);
  3696. rcu_read_unlock();
  3697. /* Now trim the list to be not more than 8 elements */
  3698. if (lg_prealloc_count > 8) {
  3699. ext4_mb_discard_lg_preallocations(sb, lg,
  3700. order, lg_prealloc_count);
  3701. return;
  3702. }
  3703. return ;
  3704. }
  3705. /*
  3706. * release all resource we used in allocation
  3707. */
  3708. static int ext4_mb_release_context(struct ext4_allocation_context *ac)
  3709. {
  3710. struct ext4_prealloc_space *pa = ac->ac_pa;
  3711. if (pa) {
  3712. if (pa->pa_linear) {
  3713. /* see comment in ext4_mb_use_group_pa() */
  3714. spin_lock(&pa->pa_lock);
  3715. pa->pa_pstart += ac->ac_b_ex.fe_len;
  3716. pa->pa_lstart += ac->ac_b_ex.fe_len;
  3717. pa->pa_free -= ac->ac_b_ex.fe_len;
  3718. pa->pa_len -= ac->ac_b_ex.fe_len;
  3719. spin_unlock(&pa->pa_lock);
  3720. /*
  3721. * We want to add the pa to the right bucket.
  3722. * Remove it from the list and while adding
  3723. * make sure the list to which we are adding
  3724. * doesn't grow big.
  3725. */
  3726. if (likely(pa->pa_free)) {
  3727. spin_lock(pa->pa_obj_lock);
  3728. list_del_rcu(&pa->pa_inode_list);
  3729. spin_unlock(pa->pa_obj_lock);
  3730. ext4_mb_add_n_trim(ac);
  3731. }
  3732. }
  3733. ext4_mb_put_pa(ac, ac->ac_sb, pa);
  3734. }
  3735. if (ac->ac_bitmap_page)
  3736. page_cache_release(ac->ac_bitmap_page);
  3737. if (ac->ac_buddy_page)
  3738. page_cache_release(ac->ac_buddy_page);
  3739. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3740. mutex_unlock(&ac->ac_lg->lg_mutex);
  3741. ext4_mb_collect_stats(ac);
  3742. return 0;
  3743. }
  3744. static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
  3745. {
  3746. ext4_group_t i;
  3747. int ret;
  3748. int freed = 0;
  3749. for (i = 0; i < EXT4_SB(sb)->s_groups_count && needed > 0; i++) {
  3750. ret = ext4_mb_discard_group_preallocations(sb, i, needed);
  3751. freed += ret;
  3752. needed -= ret;
  3753. }
  3754. return freed;
  3755. }
  3756. /*
  3757. * Main entry point into mballoc to allocate blocks
  3758. * it tries to use preallocation first, then falls back
  3759. * to usual allocation
  3760. */
  3761. ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  3762. struct ext4_allocation_request *ar, int *errp)
  3763. {
  3764. int freed;
  3765. struct ext4_allocation_context *ac = NULL;
  3766. struct ext4_sb_info *sbi;
  3767. struct super_block *sb;
  3768. ext4_fsblk_t block = 0;
  3769. unsigned long inquota;
  3770. unsigned long reserv_blks = 0;
  3771. sb = ar->inode->i_sb;
  3772. sbi = EXT4_SB(sb);
  3773. if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag) {
  3774. /*
  3775. * With delalloc we already reserved the blocks
  3776. */
  3777. while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
  3778. /* let others to free the space */
  3779. yield();
  3780. ar->len = ar->len >> 1;
  3781. }
  3782. if (!ar->len) {
  3783. *errp = -ENOSPC;
  3784. return 0;
  3785. }
  3786. reserv_blks = ar->len;
  3787. }
  3788. while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
  3789. ar->flags |= EXT4_MB_HINT_NOPREALLOC;
  3790. ar->len--;
  3791. }
  3792. if (ar->len == 0) {
  3793. *errp = -EDQUOT;
  3794. return 0;
  3795. }
  3796. inquota = ar->len;
  3797. if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
  3798. ar->flags |= EXT4_MB_DELALLOC_RESERVED;
  3799. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3800. if (!ac) {
  3801. ar->len = 0;
  3802. *errp = -ENOMEM;
  3803. goto out1;
  3804. }
  3805. *errp = ext4_mb_initialize_context(ac, ar);
  3806. if (*errp) {
  3807. ar->len = 0;
  3808. goto out2;
  3809. }
  3810. ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
  3811. if (!ext4_mb_use_preallocated(ac)) {
  3812. ac->ac_op = EXT4_MB_HISTORY_ALLOC;
  3813. ext4_mb_normalize_request(ac, ar);
  3814. repeat:
  3815. /* allocate space in core */
  3816. ext4_mb_regular_allocator(ac);
  3817. /* as we've just preallocated more space than
  3818. * user requested orinally, we store allocated
  3819. * space in a special descriptor */
  3820. if (ac->ac_status == AC_STATUS_FOUND &&
  3821. ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
  3822. ext4_mb_new_preallocation(ac);
  3823. }
  3824. if (likely(ac->ac_status == AC_STATUS_FOUND)) {
  3825. *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
  3826. if (*errp == -EAGAIN) {
  3827. ac->ac_b_ex.fe_group = 0;
  3828. ac->ac_b_ex.fe_start = 0;
  3829. ac->ac_b_ex.fe_len = 0;
  3830. ac->ac_status = AC_STATUS_CONTINUE;
  3831. goto repeat;
  3832. } else if (*errp) {
  3833. ac->ac_b_ex.fe_len = 0;
  3834. ar->len = 0;
  3835. ext4_mb_show_ac(ac);
  3836. } else {
  3837. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3838. ar->len = ac->ac_b_ex.fe_len;
  3839. }
  3840. } else {
  3841. freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
  3842. if (freed)
  3843. goto repeat;
  3844. *errp = -ENOSPC;
  3845. ac->ac_b_ex.fe_len = 0;
  3846. ar->len = 0;
  3847. ext4_mb_show_ac(ac);
  3848. }
  3849. ext4_mb_release_context(ac);
  3850. out2:
  3851. kmem_cache_free(ext4_ac_cachep, ac);
  3852. out1:
  3853. if (ar->len < inquota)
  3854. DQUOT_FREE_BLOCK(ar->inode, inquota - ar->len);
  3855. return block;
  3856. }
  3857. /*
  3858. * We can merge two free data extents only if the physical blocks
  3859. * are contiguous, AND the extents were freed by the same transaction,
  3860. * AND the blocks are associated with the same group.
  3861. */
  3862. static int can_merge(struct ext4_free_data *entry1,
  3863. struct ext4_free_data *entry2)
  3864. {
  3865. if ((entry1->t_tid == entry2->t_tid) &&
  3866. (entry1->group == entry2->group) &&
  3867. ((entry1->start_blk + entry1->count) == entry2->start_blk))
  3868. return 1;
  3869. return 0;
  3870. }
  3871. static noinline_for_stack int
  3872. ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
  3873. ext4_group_t group, ext4_grpblk_t block, int count)
  3874. {
  3875. struct ext4_group_info *db = e4b->bd_info;
  3876. struct super_block *sb = e4b->bd_sb;
  3877. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3878. struct ext4_free_data *entry, *new_entry;
  3879. struct rb_node **n = &db->bb_free_root.rb_node, *node;
  3880. struct rb_node *parent = NULL, *new_node;
  3881. BUG_ON(e4b->bd_bitmap_page == NULL);
  3882. BUG_ON(e4b->bd_buddy_page == NULL);
  3883. new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
  3884. new_entry->start_blk = block;
  3885. new_entry->group = group;
  3886. new_entry->count = count;
  3887. new_entry->t_tid = handle->h_transaction->t_tid;
  3888. new_node = &new_entry->node;
  3889. ext4_lock_group(sb, group);
  3890. if (!*n) {
  3891. /* first free block exent. We need to
  3892. protect buddy cache from being freed,
  3893. * otherwise we'll refresh it from
  3894. * on-disk bitmap and lose not-yet-available
  3895. * blocks */
  3896. page_cache_get(e4b->bd_buddy_page);
  3897. page_cache_get(e4b->bd_bitmap_page);
  3898. }
  3899. while (*n) {
  3900. parent = *n;
  3901. entry = rb_entry(parent, struct ext4_free_data, node);
  3902. if (block < entry->start_blk)
  3903. n = &(*n)->rb_left;
  3904. else if (block >= (entry->start_blk + entry->count))
  3905. n = &(*n)->rb_right;
  3906. else {
  3907. ext4_unlock_group(sb, group);
  3908. ext4_error(sb, __func__,
  3909. "Double free of blocks %d (%d %d)\n",
  3910. block, entry->start_blk, entry->count);
  3911. return 0;
  3912. }
  3913. }
  3914. rb_link_node(new_node, parent, n);
  3915. rb_insert_color(new_node, &db->bb_free_root);
  3916. /* Now try to see the extent can be merged to left and right */
  3917. node = rb_prev(new_node);
  3918. if (node) {
  3919. entry = rb_entry(node, struct ext4_free_data, node);
  3920. if (can_merge(entry, new_entry)) {
  3921. new_entry->start_blk = entry->start_blk;
  3922. new_entry->count += entry->count;
  3923. rb_erase(node, &(db->bb_free_root));
  3924. spin_lock(&sbi->s_md_lock);
  3925. list_del(&entry->list);
  3926. spin_unlock(&sbi->s_md_lock);
  3927. kmem_cache_free(ext4_free_ext_cachep, entry);
  3928. }
  3929. }
  3930. node = rb_next(new_node);
  3931. if (node) {
  3932. entry = rb_entry(node, struct ext4_free_data, node);
  3933. if (can_merge(new_entry, entry)) {
  3934. new_entry->count += entry->count;
  3935. rb_erase(node, &(db->bb_free_root));
  3936. spin_lock(&sbi->s_md_lock);
  3937. list_del(&entry->list);
  3938. spin_unlock(&sbi->s_md_lock);
  3939. kmem_cache_free(ext4_free_ext_cachep, entry);
  3940. }
  3941. }
  3942. /* Add the extent to transaction's private list */
  3943. spin_lock(&sbi->s_md_lock);
  3944. list_add(&new_entry->list, &handle->h_transaction->t_private_list);
  3945. spin_unlock(&sbi->s_md_lock);
  3946. ext4_unlock_group(sb, group);
  3947. return 0;
  3948. }
  3949. /*
  3950. * Main entry point into mballoc to free blocks
  3951. */
  3952. void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
  3953. unsigned long block, unsigned long count,
  3954. int metadata, unsigned long *freed)
  3955. {
  3956. struct buffer_head *bitmap_bh = NULL;
  3957. struct super_block *sb = inode->i_sb;
  3958. struct ext4_allocation_context *ac = NULL;
  3959. struct ext4_group_desc *gdp;
  3960. struct ext4_super_block *es;
  3961. unsigned long overflow;
  3962. ext4_grpblk_t bit;
  3963. struct buffer_head *gd_bh;
  3964. ext4_group_t block_group;
  3965. struct ext4_sb_info *sbi;
  3966. struct ext4_buddy e4b;
  3967. int err = 0;
  3968. int ret;
  3969. *freed = 0;
  3970. sbi = EXT4_SB(sb);
  3971. es = EXT4_SB(sb)->s_es;
  3972. if (block < le32_to_cpu(es->s_first_data_block) ||
  3973. block + count < block ||
  3974. block + count > ext4_blocks_count(es)) {
  3975. ext4_error(sb, __func__,
  3976. "Freeing blocks not in datazone - "
  3977. "block = %lu, count = %lu", block, count);
  3978. goto error_return;
  3979. }
  3980. ext4_debug("freeing block %lu\n", block);
  3981. ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
  3982. if (ac) {
  3983. ac->ac_op = EXT4_MB_HISTORY_FREE;
  3984. ac->ac_inode = inode;
  3985. ac->ac_sb = sb;
  3986. }
  3987. do_more:
  3988. overflow = 0;
  3989. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  3990. /*
  3991. * Check to see if we are freeing blocks across a group
  3992. * boundary.
  3993. */
  3994. if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  3995. overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
  3996. count -= overflow;
  3997. }
  3998. bitmap_bh = ext4_read_block_bitmap(sb, block_group);
  3999. if (!bitmap_bh) {
  4000. err = -EIO;
  4001. goto error_return;
  4002. }
  4003. gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
  4004. if (!gdp) {
  4005. err = -EIO;
  4006. goto error_return;
  4007. }
  4008. if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
  4009. in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
  4010. in_range(block, ext4_inode_table(sb, gdp),
  4011. EXT4_SB(sb)->s_itb_per_group) ||
  4012. in_range(block + count - 1, ext4_inode_table(sb, gdp),
  4013. EXT4_SB(sb)->s_itb_per_group)) {
  4014. ext4_error(sb, __func__,
  4015. "Freeing blocks in system zone - "
  4016. "Block = %lu, count = %lu", block, count);
  4017. /* err = 0. ext4_std_error should be a no op */
  4018. goto error_return;
  4019. }
  4020. BUFFER_TRACE(bitmap_bh, "getting write access");
  4021. err = ext4_journal_get_write_access(handle, bitmap_bh);
  4022. if (err)
  4023. goto error_return;
  4024. /*
  4025. * We are about to modify some metadata. Call the journal APIs
  4026. * to unshare ->b_data if a currently-committing transaction is
  4027. * using it
  4028. */
  4029. BUFFER_TRACE(gd_bh, "get_write_access");
  4030. err = ext4_journal_get_write_access(handle, gd_bh);
  4031. if (err)
  4032. goto error_return;
  4033. err = ext4_mb_load_buddy(sb, block_group, &e4b);
  4034. if (err)
  4035. goto error_return;
  4036. #ifdef AGGRESSIVE_CHECK
  4037. {
  4038. int i;
  4039. for (i = 0; i < count; i++)
  4040. BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
  4041. }
  4042. #endif
  4043. mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
  4044. bit, count);
  4045. /* We dirtied the bitmap block */
  4046. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  4047. err = ext4_journal_dirty_metadata(handle, bitmap_bh);
  4048. if (ac) {
  4049. ac->ac_b_ex.fe_group = block_group;
  4050. ac->ac_b_ex.fe_start = bit;
  4051. ac->ac_b_ex.fe_len = count;
  4052. ext4_mb_store_history(ac);
  4053. }
  4054. if (metadata) {
  4055. /* blocks being freed are metadata. these blocks shouldn't
  4056. * be used until this transaction is committed */
  4057. ext4_mb_free_metadata(handle, &e4b, block_group, bit, count);
  4058. } else {
  4059. ext4_lock_group(sb, block_group);
  4060. mb_free_blocks(inode, &e4b, bit, count);
  4061. ext4_mb_return_to_preallocation(inode, &e4b, block, count);
  4062. ext4_unlock_group(sb, block_group);
  4063. }
  4064. spin_lock(sb_bgl_lock(sbi, block_group));
  4065. le16_add_cpu(&gdp->bg_free_blocks_count, count);
  4066. gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
  4067. spin_unlock(sb_bgl_lock(sbi, block_group));
  4068. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  4069. if (sbi->s_log_groups_per_flex) {
  4070. ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
  4071. spin_lock(sb_bgl_lock(sbi, flex_group));
  4072. sbi->s_flex_groups[flex_group].free_blocks += count;
  4073. spin_unlock(sb_bgl_lock(sbi, flex_group));
  4074. }
  4075. ext4_mb_release_desc(&e4b);
  4076. *freed += count;
  4077. /* And the group descriptor block */
  4078. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  4079. ret = ext4_journal_dirty_metadata(handle, gd_bh);
  4080. if (!err)
  4081. err = ret;
  4082. if (overflow && !err) {
  4083. block += count;
  4084. count = overflow;
  4085. put_bh(bitmap_bh);
  4086. goto do_more;
  4087. }
  4088. sb->s_dirt = 1;
  4089. error_return:
  4090. brelse(bitmap_bh);
  4091. ext4_std_error(sb, err);
  4092. if (ac)
  4093. kmem_cache_free(ext4_ac_cachep, ac);
  4094. return;
  4095. }