mballoc.c 117 KB

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