xfs_alloc.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_shared.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_defer.h"
  15. #include "xfs_btree.h"
  16. #include "xfs_rmap.h"
  17. #include "xfs_alloc_btree.h"
  18. #include "xfs_alloc.h"
  19. #include "xfs_extent_busy.h"
  20. #include "xfs_errortag.h"
  21. #include "xfs_error.h"
  22. #include "xfs_trace.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_buf_item.h"
  25. #include "xfs_log.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_ag_resv.h"
  28. #include "xfs_bmap.h"
  29. #include "xfs_health.h"
  30. #include "xfs_extfree_item.h"
  31. struct kmem_cache *xfs_extfree_item_cache;
  32. struct workqueue_struct *xfs_alloc_wq;
  33. #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
  34. #define XFSA_FIXUP_BNO_OK 1
  35. #define XFSA_FIXUP_CNT_OK 2
  36. /*
  37. * Size of the AGFL. For CRC-enabled filesystes we steal a couple of slots in
  38. * the beginning of the block for a proper header with the location information
  39. * and CRC.
  40. */
  41. unsigned int
  42. xfs_agfl_size(
  43. struct xfs_mount *mp)
  44. {
  45. unsigned int size = mp->m_sb.sb_sectsize;
  46. if (xfs_has_crc(mp))
  47. size -= sizeof(struct xfs_agfl);
  48. return size / sizeof(xfs_agblock_t);
  49. }
  50. unsigned int
  51. xfs_refc_block(
  52. struct xfs_mount *mp)
  53. {
  54. if (xfs_has_rmapbt(mp))
  55. return XFS_RMAP_BLOCK(mp) + 1;
  56. if (xfs_has_finobt(mp))
  57. return XFS_FIBT_BLOCK(mp) + 1;
  58. return XFS_IBT_BLOCK(mp) + 1;
  59. }
  60. xfs_extlen_t
  61. xfs_prealloc_blocks(
  62. struct xfs_mount *mp)
  63. {
  64. if (xfs_has_reflink(mp))
  65. return xfs_refc_block(mp) + 1;
  66. if (xfs_has_rmapbt(mp))
  67. return XFS_RMAP_BLOCK(mp) + 1;
  68. if (xfs_has_finobt(mp))
  69. return XFS_FIBT_BLOCK(mp) + 1;
  70. return XFS_IBT_BLOCK(mp) + 1;
  71. }
  72. /*
  73. * The number of blocks per AG that we withhold from xfs_dec_fdblocks to
  74. * guarantee that we can refill the AGFL prior to allocating space in a nearly
  75. * full AG. Although the space described by the free space btrees, the
  76. * blocks used by the freesp btrees themselves, and the blocks owned by the
  77. * AGFL are counted in the ondisk fdblocks, it's a mistake to let the ondisk
  78. * free space in the AG drop so low that the free space btrees cannot refill an
  79. * empty AGFL up to the minimum level. Rather than grind through empty AGs
  80. * until the fs goes down, we subtract this many AG blocks from the incore
  81. * fdblocks to ensure user allocation does not overcommit the space the
  82. * filesystem needs for the AGFLs. The rmap btree uses a per-AG reservation to
  83. * withhold space from xfs_dec_fdblocks, so we do not account for that here.
  84. */
  85. #define XFS_ALLOCBT_AGFL_RESERVE 4
  86. /*
  87. * Compute the number of blocks that we set aside to guarantee the ability to
  88. * refill the AGFL and handle a full bmap btree split.
  89. *
  90. * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
  91. * AGF buffer (PV 947395), we place constraints on the relationship among
  92. * actual allocations for data blocks, freelist blocks, and potential file data
  93. * bmap btree blocks. However, these restrictions may result in no actual space
  94. * allocated for a delayed extent, for example, a data block in a certain AG is
  95. * allocated but there is no additional block for the additional bmap btree
  96. * block due to a split of the bmap btree of the file. The result of this may
  97. * lead to an infinite loop when the file gets flushed to disk and all delayed
  98. * extents need to be actually allocated. To get around this, we explicitly set
  99. * aside a few blocks which will not be reserved in delayed allocation.
  100. *
  101. * For each AG, we need to reserve enough blocks to replenish a totally empty
  102. * AGFL and 4 more to handle a potential split of the file's bmap btree.
  103. */
  104. unsigned int
  105. xfs_alloc_set_aside(
  106. struct xfs_mount *mp)
  107. {
  108. return mp->m_sb.sb_agcount * (XFS_ALLOCBT_AGFL_RESERVE + 4);
  109. }
  110. /*
  111. * When deciding how much space to allocate out of an AG, we limit the
  112. * allocation maximum size to the size the AG. However, we cannot use all the
  113. * blocks in the AG - some are permanently used by metadata. These
  114. * blocks are generally:
  115. * - the AG superblock, AGF, AGI and AGFL
  116. * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
  117. * the AGI free inode and rmap btree root blocks.
  118. * - blocks on the AGFL according to xfs_alloc_set_aside() limits
  119. * - the rmapbt root block
  120. *
  121. * The AG headers are sector sized, so the amount of space they take up is
  122. * dependent on filesystem geometry. The others are all single blocks.
  123. */
  124. unsigned int
  125. xfs_alloc_ag_max_usable(
  126. struct xfs_mount *mp)
  127. {
  128. unsigned int blocks;
  129. blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
  130. blocks += XFS_ALLOCBT_AGFL_RESERVE;
  131. blocks += 3; /* AGF, AGI btree root blocks */
  132. if (xfs_has_finobt(mp))
  133. blocks++; /* finobt root block */
  134. if (xfs_has_rmapbt(mp))
  135. blocks++; /* rmap root block */
  136. if (xfs_has_reflink(mp))
  137. blocks++; /* refcount root block */
  138. return mp->m_sb.sb_agblocks - blocks;
  139. }
  140. static int
  141. xfs_alloc_lookup(
  142. struct xfs_btree_cur *cur,
  143. xfs_lookup_t dir,
  144. xfs_agblock_t bno,
  145. xfs_extlen_t len,
  146. int *stat)
  147. {
  148. int error;
  149. cur->bc_rec.a.ar_startblock = bno;
  150. cur->bc_rec.a.ar_blockcount = len;
  151. error = xfs_btree_lookup(cur, dir, stat);
  152. if (*stat == 1)
  153. cur->bc_flags |= XFS_BTREE_ALLOCBT_ACTIVE;
  154. else
  155. cur->bc_flags &= ~XFS_BTREE_ALLOCBT_ACTIVE;
  156. return error;
  157. }
  158. /*
  159. * Lookup the record equal to [bno, len] in the btree given by cur.
  160. */
  161. static inline int /* error */
  162. xfs_alloc_lookup_eq(
  163. struct xfs_btree_cur *cur, /* btree cursor */
  164. xfs_agblock_t bno, /* starting block of extent */
  165. xfs_extlen_t len, /* length of extent */
  166. int *stat) /* success/failure */
  167. {
  168. return xfs_alloc_lookup(cur, XFS_LOOKUP_EQ, bno, len, stat);
  169. }
  170. /*
  171. * Lookup the first record greater than or equal to [bno, len]
  172. * in the btree given by cur.
  173. */
  174. int /* error */
  175. xfs_alloc_lookup_ge(
  176. struct xfs_btree_cur *cur, /* btree cursor */
  177. xfs_agblock_t bno, /* starting block of extent */
  178. xfs_extlen_t len, /* length of extent */
  179. int *stat) /* success/failure */
  180. {
  181. return xfs_alloc_lookup(cur, XFS_LOOKUP_GE, bno, len, stat);
  182. }
  183. /*
  184. * Lookup the first record less than or equal to [bno, len]
  185. * in the btree given by cur.
  186. */
  187. int /* error */
  188. xfs_alloc_lookup_le(
  189. struct xfs_btree_cur *cur, /* btree cursor */
  190. xfs_agblock_t bno, /* starting block of extent */
  191. xfs_extlen_t len, /* length of extent */
  192. int *stat) /* success/failure */
  193. {
  194. return xfs_alloc_lookup(cur, XFS_LOOKUP_LE, bno, len, stat);
  195. }
  196. static inline bool
  197. xfs_alloc_cur_active(
  198. struct xfs_btree_cur *cur)
  199. {
  200. return cur && (cur->bc_flags & XFS_BTREE_ALLOCBT_ACTIVE);
  201. }
  202. /*
  203. * Update the record referred to by cur to the value given
  204. * by [bno, len].
  205. * This either works (return 0) or gets an EFSCORRUPTED error.
  206. */
  207. STATIC int /* error */
  208. xfs_alloc_update(
  209. struct xfs_btree_cur *cur, /* btree cursor */
  210. xfs_agblock_t bno, /* starting block of extent */
  211. xfs_extlen_t len) /* length of extent */
  212. {
  213. union xfs_btree_rec rec;
  214. rec.alloc.ar_startblock = cpu_to_be32(bno);
  215. rec.alloc.ar_blockcount = cpu_to_be32(len);
  216. return xfs_btree_update(cur, &rec);
  217. }
  218. /* Convert the ondisk btree record to its incore representation. */
  219. void
  220. xfs_alloc_btrec_to_irec(
  221. const union xfs_btree_rec *rec,
  222. struct xfs_alloc_rec_incore *irec)
  223. {
  224. irec->ar_startblock = be32_to_cpu(rec->alloc.ar_startblock);
  225. irec->ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount);
  226. }
  227. /* Simple checks for free space records. */
  228. xfs_failaddr_t
  229. xfs_alloc_check_irec(
  230. struct xfs_perag *pag,
  231. const struct xfs_alloc_rec_incore *irec)
  232. {
  233. if (irec->ar_blockcount == 0)
  234. return __this_address;
  235. /* check for valid extent range, including overflow */
  236. if (!xfs_verify_agbext(pag, irec->ar_startblock, irec->ar_blockcount))
  237. return __this_address;
  238. return NULL;
  239. }
  240. static inline int
  241. xfs_alloc_complain_bad_rec(
  242. struct xfs_btree_cur *cur,
  243. xfs_failaddr_t fa,
  244. const struct xfs_alloc_rec_incore *irec)
  245. {
  246. struct xfs_mount *mp = cur->bc_mp;
  247. xfs_warn(mp,
  248. "%sbt record corruption in AG %d detected at %pS!",
  249. cur->bc_ops->name, cur->bc_ag.pag->pag_agno, fa);
  250. xfs_warn(mp,
  251. "start block 0x%x block count 0x%x", irec->ar_startblock,
  252. irec->ar_blockcount);
  253. xfs_btree_mark_sick(cur);
  254. return -EFSCORRUPTED;
  255. }
  256. /*
  257. * Get the data from the pointed-to record.
  258. */
  259. int /* error */
  260. xfs_alloc_get_rec(
  261. struct xfs_btree_cur *cur, /* btree cursor */
  262. xfs_agblock_t *bno, /* output: starting block of extent */
  263. xfs_extlen_t *len, /* output: length of extent */
  264. int *stat) /* output: success/failure */
  265. {
  266. struct xfs_alloc_rec_incore irec;
  267. union xfs_btree_rec *rec;
  268. xfs_failaddr_t fa;
  269. int error;
  270. error = xfs_btree_get_rec(cur, &rec, stat);
  271. if (error || !(*stat))
  272. return error;
  273. xfs_alloc_btrec_to_irec(rec, &irec);
  274. fa = xfs_alloc_check_irec(cur->bc_ag.pag, &irec);
  275. if (fa)
  276. return xfs_alloc_complain_bad_rec(cur, fa, &irec);
  277. *bno = irec.ar_startblock;
  278. *len = irec.ar_blockcount;
  279. return 0;
  280. }
  281. /*
  282. * Compute aligned version of the found extent.
  283. * Takes alignment and min length into account.
  284. */
  285. STATIC bool
  286. xfs_alloc_compute_aligned(
  287. xfs_alloc_arg_t *args, /* allocation argument structure */
  288. xfs_agblock_t foundbno, /* starting block in found extent */
  289. xfs_extlen_t foundlen, /* length in found extent */
  290. xfs_agblock_t *resbno, /* result block number */
  291. xfs_extlen_t *reslen, /* result length */
  292. unsigned *busy_gen)
  293. {
  294. xfs_agblock_t bno = foundbno;
  295. xfs_extlen_t len = foundlen;
  296. xfs_extlen_t diff;
  297. bool busy;
  298. /* Trim busy sections out of found extent */
  299. busy = xfs_extent_busy_trim(args, &bno, &len, busy_gen);
  300. /*
  301. * If we have a largish extent that happens to start before min_agbno,
  302. * see if we can shift it into range...
  303. */
  304. if (bno < args->min_agbno && bno + len > args->min_agbno) {
  305. diff = args->min_agbno - bno;
  306. if (len > diff) {
  307. bno += diff;
  308. len -= diff;
  309. }
  310. }
  311. if (args->alignment > 1 && len >= args->minlen) {
  312. xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
  313. diff = aligned_bno - bno;
  314. *resbno = aligned_bno;
  315. *reslen = diff >= len ? 0 : len - diff;
  316. } else {
  317. *resbno = bno;
  318. *reslen = len;
  319. }
  320. return busy;
  321. }
  322. /*
  323. * Compute best start block and diff for "near" allocations.
  324. * freelen >= wantlen already checked by caller.
  325. */
  326. STATIC xfs_extlen_t /* difference value (absolute) */
  327. xfs_alloc_compute_diff(
  328. xfs_agblock_t wantbno, /* target starting block */
  329. xfs_extlen_t wantlen, /* target length */
  330. xfs_extlen_t alignment, /* target alignment */
  331. int datatype, /* are we allocating data? */
  332. xfs_agblock_t freebno, /* freespace's starting block */
  333. xfs_extlen_t freelen, /* freespace's length */
  334. xfs_agblock_t *newbnop) /* result: best start block from free */
  335. {
  336. xfs_agblock_t freeend; /* end of freespace extent */
  337. xfs_agblock_t newbno1; /* return block number */
  338. xfs_agblock_t newbno2; /* other new block number */
  339. xfs_extlen_t newlen1=0; /* length with newbno1 */
  340. xfs_extlen_t newlen2=0; /* length with newbno2 */
  341. xfs_agblock_t wantend; /* end of target extent */
  342. bool userdata = datatype & XFS_ALLOC_USERDATA;
  343. ASSERT(freelen >= wantlen);
  344. freeend = freebno + freelen;
  345. wantend = wantbno + wantlen;
  346. /*
  347. * We want to allocate from the start of a free extent if it is past
  348. * the desired block or if we are allocating user data and the free
  349. * extent is before desired block. The second case is there to allow
  350. * for contiguous allocation from the remaining free space if the file
  351. * grows in the short term.
  352. */
  353. if (freebno >= wantbno || (userdata && freeend < wantend)) {
  354. if ((newbno1 = roundup(freebno, alignment)) >= freeend)
  355. newbno1 = NULLAGBLOCK;
  356. } else if (freeend >= wantend && alignment > 1) {
  357. newbno1 = roundup(wantbno, alignment);
  358. newbno2 = newbno1 - alignment;
  359. if (newbno1 >= freeend)
  360. newbno1 = NULLAGBLOCK;
  361. else
  362. newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
  363. if (newbno2 < freebno)
  364. newbno2 = NULLAGBLOCK;
  365. else
  366. newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
  367. if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
  368. if (newlen1 < newlen2 ||
  369. (newlen1 == newlen2 &&
  370. XFS_ABSDIFF(newbno1, wantbno) >
  371. XFS_ABSDIFF(newbno2, wantbno)))
  372. newbno1 = newbno2;
  373. } else if (newbno2 != NULLAGBLOCK)
  374. newbno1 = newbno2;
  375. } else if (freeend >= wantend) {
  376. newbno1 = wantbno;
  377. } else if (alignment > 1) {
  378. newbno1 = roundup(freeend - wantlen, alignment);
  379. if (newbno1 > freeend - wantlen &&
  380. newbno1 - alignment >= freebno)
  381. newbno1 -= alignment;
  382. else if (newbno1 >= freeend)
  383. newbno1 = NULLAGBLOCK;
  384. } else
  385. newbno1 = freeend - wantlen;
  386. *newbnop = newbno1;
  387. return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
  388. }
  389. /*
  390. * Fix up the length, based on mod and prod.
  391. * len should be k * prod + mod for some k.
  392. * If len is too small it is returned unchanged.
  393. * If len hits maxlen it is left alone.
  394. */
  395. STATIC void
  396. xfs_alloc_fix_len(
  397. xfs_alloc_arg_t *args) /* allocation argument structure */
  398. {
  399. xfs_extlen_t k;
  400. xfs_extlen_t rlen;
  401. ASSERT(args->mod < args->prod);
  402. rlen = args->len;
  403. ASSERT(rlen >= args->minlen);
  404. ASSERT(rlen <= args->maxlen);
  405. if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
  406. (args->mod == 0 && rlen < args->prod))
  407. return;
  408. k = rlen % args->prod;
  409. if (k == args->mod)
  410. return;
  411. if (k > args->mod)
  412. rlen = rlen - (k - args->mod);
  413. else
  414. rlen = rlen - args->prod + (args->mod - k);
  415. /* casts to (int) catch length underflows */
  416. if ((int)rlen < (int)args->minlen)
  417. return;
  418. ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
  419. ASSERT(rlen % args->prod == args->mod);
  420. ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
  421. rlen + args->minleft);
  422. args->len = rlen;
  423. }
  424. /*
  425. * Determine if the cursor points to the block that contains the right-most
  426. * block of records in the by-count btree. This block contains the largest
  427. * contiguous free extent in the AG, so if we modify a record in this block we
  428. * need to call xfs_alloc_fixup_longest() once the modifications are done to
  429. * ensure the agf->agf_longest field is kept up to date with the longest free
  430. * extent tracked by the by-count btree.
  431. */
  432. static bool
  433. xfs_alloc_cursor_at_lastrec(
  434. struct xfs_btree_cur *cnt_cur)
  435. {
  436. struct xfs_btree_block *block;
  437. union xfs_btree_ptr ptr;
  438. struct xfs_buf *bp;
  439. block = xfs_btree_get_block(cnt_cur, 0, &bp);
  440. xfs_btree_get_sibling(cnt_cur, block, &ptr, XFS_BB_RIGHTSIB);
  441. return xfs_btree_ptr_is_null(cnt_cur, &ptr);
  442. }
  443. /*
  444. * Find the rightmost record of the cntbt, and return the longest free space
  445. * recorded in it. Simply set both the block number and the length to their
  446. * maximum values before searching.
  447. */
  448. static int
  449. xfs_cntbt_longest(
  450. struct xfs_btree_cur *cnt_cur,
  451. xfs_extlen_t *longest)
  452. {
  453. struct xfs_alloc_rec_incore irec;
  454. union xfs_btree_rec *rec;
  455. int stat = 0;
  456. int error;
  457. memset(&cnt_cur->bc_rec, 0xFF, sizeof(cnt_cur->bc_rec));
  458. error = xfs_btree_lookup(cnt_cur, XFS_LOOKUP_LE, &stat);
  459. if (error)
  460. return error;
  461. if (!stat) {
  462. /* totally empty tree */
  463. *longest = 0;
  464. return 0;
  465. }
  466. error = xfs_btree_get_rec(cnt_cur, &rec, &stat);
  467. if (error)
  468. return error;
  469. if (XFS_IS_CORRUPT(cnt_cur->bc_mp, !stat)) {
  470. xfs_btree_mark_sick(cnt_cur);
  471. return -EFSCORRUPTED;
  472. }
  473. xfs_alloc_btrec_to_irec(rec, &irec);
  474. *longest = irec.ar_blockcount;
  475. return 0;
  476. }
  477. /*
  478. * Update the longest contiguous free extent in the AG from the by-count cursor
  479. * that is passed to us. This should be done at the end of any allocation or
  480. * freeing operation that touches the longest extent in the btree.
  481. *
  482. * Needing to update the longest extent can be determined by calling
  483. * xfs_alloc_cursor_at_lastrec() after the cursor is positioned for record
  484. * modification but before the modification begins.
  485. */
  486. static int
  487. xfs_alloc_fixup_longest(
  488. struct xfs_btree_cur *cnt_cur)
  489. {
  490. struct xfs_perag *pag = cnt_cur->bc_ag.pag;
  491. struct xfs_buf *bp = cnt_cur->bc_ag.agbp;
  492. struct xfs_agf *agf = bp->b_addr;
  493. xfs_extlen_t longest = 0;
  494. int error;
  495. /* Lookup last rec in order to update AGF. */
  496. error = xfs_cntbt_longest(cnt_cur, &longest);
  497. if (error)
  498. return error;
  499. pag->pagf_longest = longest;
  500. agf->agf_longest = cpu_to_be32(pag->pagf_longest);
  501. xfs_alloc_log_agf(cnt_cur->bc_tp, bp, XFS_AGF_LONGEST);
  502. return 0;
  503. }
  504. /*
  505. * Update the two btrees, logically removing from freespace the extent
  506. * starting at rbno, rlen blocks. The extent is contained within the
  507. * actual (current) free extent fbno for flen blocks.
  508. * Flags are passed in indicating whether the cursors are set to the
  509. * relevant records.
  510. */
  511. STATIC int /* error code */
  512. xfs_alloc_fixup_trees(
  513. struct xfs_btree_cur *cnt_cur, /* cursor for by-size btree */
  514. struct xfs_btree_cur *bno_cur, /* cursor for by-block btree */
  515. xfs_agblock_t fbno, /* starting block of free extent */
  516. xfs_extlen_t flen, /* length of free extent */
  517. xfs_agblock_t rbno, /* starting block of returned extent */
  518. xfs_extlen_t rlen, /* length of returned extent */
  519. int flags) /* flags, XFSA_FIXUP_... */
  520. {
  521. int error; /* error code */
  522. int i; /* operation results */
  523. xfs_agblock_t nfbno1; /* first new free startblock */
  524. xfs_agblock_t nfbno2; /* second new free startblock */
  525. xfs_extlen_t nflen1=0; /* first new free length */
  526. xfs_extlen_t nflen2=0; /* second new free length */
  527. struct xfs_mount *mp;
  528. bool fixup_longest = false;
  529. mp = cnt_cur->bc_mp;
  530. /*
  531. * Look up the record in the by-size tree if necessary.
  532. */
  533. if (flags & XFSA_FIXUP_CNT_OK) {
  534. #ifdef DEBUG
  535. if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
  536. return error;
  537. if (XFS_IS_CORRUPT(mp,
  538. i != 1 ||
  539. nfbno1 != fbno ||
  540. nflen1 != flen)) {
  541. xfs_btree_mark_sick(cnt_cur);
  542. return -EFSCORRUPTED;
  543. }
  544. #endif
  545. } else {
  546. if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
  547. return error;
  548. if (XFS_IS_CORRUPT(mp, i != 1)) {
  549. xfs_btree_mark_sick(cnt_cur);
  550. return -EFSCORRUPTED;
  551. }
  552. }
  553. /*
  554. * Look up the record in the by-block tree if necessary.
  555. */
  556. if (flags & XFSA_FIXUP_BNO_OK) {
  557. #ifdef DEBUG
  558. if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
  559. return error;
  560. if (XFS_IS_CORRUPT(mp,
  561. i != 1 ||
  562. nfbno1 != fbno ||
  563. nflen1 != flen)) {
  564. xfs_btree_mark_sick(bno_cur);
  565. return -EFSCORRUPTED;
  566. }
  567. #endif
  568. } else {
  569. if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
  570. return error;
  571. if (XFS_IS_CORRUPT(mp, i != 1)) {
  572. xfs_btree_mark_sick(bno_cur);
  573. return -EFSCORRUPTED;
  574. }
  575. }
  576. #ifdef DEBUG
  577. if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
  578. struct xfs_btree_block *bnoblock;
  579. struct xfs_btree_block *cntblock;
  580. bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_levels[0].bp);
  581. cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_levels[0].bp);
  582. if (XFS_IS_CORRUPT(mp,
  583. bnoblock->bb_numrecs !=
  584. cntblock->bb_numrecs)) {
  585. xfs_btree_mark_sick(bno_cur);
  586. return -EFSCORRUPTED;
  587. }
  588. }
  589. #endif
  590. /*
  591. * Deal with all four cases: the allocated record is contained
  592. * within the freespace record, so we can have new freespace
  593. * at either (or both) end, or no freespace remaining.
  594. */
  595. if (rbno == fbno && rlen == flen)
  596. nfbno1 = nfbno2 = NULLAGBLOCK;
  597. else if (rbno == fbno) {
  598. nfbno1 = rbno + rlen;
  599. nflen1 = flen - rlen;
  600. nfbno2 = NULLAGBLOCK;
  601. } else if (rbno + rlen == fbno + flen) {
  602. nfbno1 = fbno;
  603. nflen1 = flen - rlen;
  604. nfbno2 = NULLAGBLOCK;
  605. } else {
  606. nfbno1 = fbno;
  607. nflen1 = rbno - fbno;
  608. nfbno2 = rbno + rlen;
  609. nflen2 = (fbno + flen) - nfbno2;
  610. }
  611. if (xfs_alloc_cursor_at_lastrec(cnt_cur))
  612. fixup_longest = true;
  613. /*
  614. * Delete the entry from the by-size btree.
  615. */
  616. if ((error = xfs_btree_delete(cnt_cur, &i)))
  617. return error;
  618. if (XFS_IS_CORRUPT(mp, i != 1)) {
  619. xfs_btree_mark_sick(cnt_cur);
  620. return -EFSCORRUPTED;
  621. }
  622. /*
  623. * Add new by-size btree entry(s).
  624. */
  625. if (nfbno1 != NULLAGBLOCK) {
  626. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
  627. return error;
  628. if (XFS_IS_CORRUPT(mp, i != 0)) {
  629. xfs_btree_mark_sick(cnt_cur);
  630. return -EFSCORRUPTED;
  631. }
  632. if ((error = xfs_btree_insert(cnt_cur, &i)))
  633. return error;
  634. if (XFS_IS_CORRUPT(mp, i != 1)) {
  635. xfs_btree_mark_sick(cnt_cur);
  636. return -EFSCORRUPTED;
  637. }
  638. }
  639. if (nfbno2 != NULLAGBLOCK) {
  640. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
  641. return error;
  642. if (XFS_IS_CORRUPT(mp, i != 0)) {
  643. xfs_btree_mark_sick(cnt_cur);
  644. return -EFSCORRUPTED;
  645. }
  646. if ((error = xfs_btree_insert(cnt_cur, &i)))
  647. return error;
  648. if (XFS_IS_CORRUPT(mp, i != 1)) {
  649. xfs_btree_mark_sick(cnt_cur);
  650. return -EFSCORRUPTED;
  651. }
  652. }
  653. /*
  654. * Fix up the by-block btree entry(s).
  655. */
  656. if (nfbno1 == NULLAGBLOCK) {
  657. /*
  658. * No remaining freespace, just delete the by-block tree entry.
  659. */
  660. if ((error = xfs_btree_delete(bno_cur, &i)))
  661. return error;
  662. if (XFS_IS_CORRUPT(mp, i != 1)) {
  663. xfs_btree_mark_sick(bno_cur);
  664. return -EFSCORRUPTED;
  665. }
  666. } else {
  667. /*
  668. * Update the by-block entry to start later|be shorter.
  669. */
  670. if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
  671. return error;
  672. }
  673. if (nfbno2 != NULLAGBLOCK) {
  674. /*
  675. * 2 resulting free entries, need to add one.
  676. */
  677. if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
  678. return error;
  679. if (XFS_IS_CORRUPT(mp, i != 0)) {
  680. xfs_btree_mark_sick(bno_cur);
  681. return -EFSCORRUPTED;
  682. }
  683. if ((error = xfs_btree_insert(bno_cur, &i)))
  684. return error;
  685. if (XFS_IS_CORRUPT(mp, i != 1)) {
  686. xfs_btree_mark_sick(bno_cur);
  687. return -EFSCORRUPTED;
  688. }
  689. }
  690. if (fixup_longest)
  691. return xfs_alloc_fixup_longest(cnt_cur);
  692. return 0;
  693. }
  694. /*
  695. * We do not verify the AGFL contents against AGF-based index counters here,
  696. * even though we may have access to the perag that contains shadow copies. We
  697. * don't know if the AGF based counters have been checked, and if they have they
  698. * still may be inconsistent because they haven't yet been reset on the first
  699. * allocation after the AGF has been read in.
  700. *
  701. * This means we can only check that all agfl entries contain valid or null
  702. * values because we can't reliably determine the active range to exclude
  703. * NULLAGBNO as a valid value.
  704. *
  705. * However, we can't even do that for v4 format filesystems because there are
  706. * old versions of mkfs out there that does not initialise the AGFL to known,
  707. * verifiable values. HEnce we can't tell the difference between a AGFL block
  708. * allocated by mkfs and a corrupted AGFL block here on v4 filesystems.
  709. *
  710. * As a result, we can only fully validate AGFL block numbers when we pull them
  711. * from the freelist in xfs_alloc_get_freelist().
  712. */
  713. static xfs_failaddr_t
  714. xfs_agfl_verify(
  715. struct xfs_buf *bp)
  716. {
  717. struct xfs_mount *mp = bp->b_mount;
  718. struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
  719. __be32 *agfl_bno = xfs_buf_to_agfl_bno(bp);
  720. int i;
  721. if (!xfs_has_crc(mp))
  722. return NULL;
  723. if (!xfs_verify_magic(bp, agfl->agfl_magicnum))
  724. return __this_address;
  725. if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
  726. return __this_address;
  727. /*
  728. * during growfs operations, the perag is not fully initialised,
  729. * so we can't use it for any useful checking. growfs ensures we can't
  730. * use it by using uncached buffers that don't have the perag attached
  731. * so we can detect and avoid this problem.
  732. */
  733. if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
  734. return __this_address;
  735. for (i = 0; i < xfs_agfl_size(mp); i++) {
  736. if (be32_to_cpu(agfl_bno[i]) != NULLAGBLOCK &&
  737. be32_to_cpu(agfl_bno[i]) >= mp->m_sb.sb_agblocks)
  738. return __this_address;
  739. }
  740. if (!xfs_log_check_lsn(mp, be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn)))
  741. return __this_address;
  742. return NULL;
  743. }
  744. static void
  745. xfs_agfl_read_verify(
  746. struct xfs_buf *bp)
  747. {
  748. struct xfs_mount *mp = bp->b_mount;
  749. xfs_failaddr_t fa;
  750. /*
  751. * There is no verification of non-crc AGFLs because mkfs does not
  752. * initialise the AGFL to zero or NULL. Hence the only valid part of the
  753. * AGFL is what the AGF says is active. We can't get to the AGF, so we
  754. * can't verify just those entries are valid.
  755. */
  756. if (!xfs_has_crc(mp))
  757. return;
  758. if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
  759. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  760. else {
  761. fa = xfs_agfl_verify(bp);
  762. if (fa)
  763. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  764. }
  765. }
  766. static void
  767. xfs_agfl_write_verify(
  768. struct xfs_buf *bp)
  769. {
  770. struct xfs_mount *mp = bp->b_mount;
  771. struct xfs_buf_log_item *bip = bp->b_log_item;
  772. xfs_failaddr_t fa;
  773. /* no verification of non-crc AGFLs */
  774. if (!xfs_has_crc(mp))
  775. return;
  776. fa = xfs_agfl_verify(bp);
  777. if (fa) {
  778. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  779. return;
  780. }
  781. if (bip)
  782. XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  783. xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
  784. }
  785. const struct xfs_buf_ops xfs_agfl_buf_ops = {
  786. .name = "xfs_agfl",
  787. .magic = { cpu_to_be32(XFS_AGFL_MAGIC), cpu_to_be32(XFS_AGFL_MAGIC) },
  788. .verify_read = xfs_agfl_read_verify,
  789. .verify_write = xfs_agfl_write_verify,
  790. .verify_struct = xfs_agfl_verify,
  791. };
  792. /*
  793. * Read in the allocation group free block array.
  794. */
  795. int
  796. xfs_alloc_read_agfl(
  797. struct xfs_perag *pag,
  798. struct xfs_trans *tp,
  799. struct xfs_buf **bpp)
  800. {
  801. struct xfs_mount *mp = pag->pag_mount;
  802. struct xfs_buf *bp;
  803. int error;
  804. error = xfs_trans_read_buf(
  805. mp, tp, mp->m_ddev_targp,
  806. XFS_AG_DADDR(mp, pag->pag_agno, XFS_AGFL_DADDR(mp)),
  807. XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
  808. if (xfs_metadata_is_sick(error))
  809. xfs_ag_mark_sick(pag, XFS_SICK_AG_AGFL);
  810. if (error)
  811. return error;
  812. xfs_buf_set_ref(bp, XFS_AGFL_REF);
  813. *bpp = bp;
  814. return 0;
  815. }
  816. STATIC int
  817. xfs_alloc_update_counters(
  818. struct xfs_trans *tp,
  819. struct xfs_buf *agbp,
  820. long len)
  821. {
  822. struct xfs_agf *agf = agbp->b_addr;
  823. agbp->b_pag->pagf_freeblks += len;
  824. be32_add_cpu(&agf->agf_freeblks, len);
  825. if (unlikely(be32_to_cpu(agf->agf_freeblks) >
  826. be32_to_cpu(agf->agf_length))) {
  827. xfs_buf_mark_corrupt(agbp);
  828. xfs_ag_mark_sick(agbp->b_pag, XFS_SICK_AG_AGF);
  829. return -EFSCORRUPTED;
  830. }
  831. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
  832. return 0;
  833. }
  834. /*
  835. * Block allocation algorithm and data structures.
  836. */
  837. struct xfs_alloc_cur {
  838. struct xfs_btree_cur *cnt; /* btree cursors */
  839. struct xfs_btree_cur *bnolt;
  840. struct xfs_btree_cur *bnogt;
  841. xfs_extlen_t cur_len;/* current search length */
  842. xfs_agblock_t rec_bno;/* extent startblock */
  843. xfs_extlen_t rec_len;/* extent length */
  844. xfs_agblock_t bno; /* alloc bno */
  845. xfs_extlen_t len; /* alloc len */
  846. xfs_extlen_t diff; /* diff from search bno */
  847. unsigned int busy_gen;/* busy state */
  848. bool busy;
  849. };
  850. /*
  851. * Set up cursors, etc. in the extent allocation cursor. This function can be
  852. * called multiple times to reset an initialized structure without having to
  853. * reallocate cursors.
  854. */
  855. static int
  856. xfs_alloc_cur_setup(
  857. struct xfs_alloc_arg *args,
  858. struct xfs_alloc_cur *acur)
  859. {
  860. int error;
  861. int i;
  862. acur->cur_len = args->maxlen;
  863. acur->rec_bno = 0;
  864. acur->rec_len = 0;
  865. acur->bno = 0;
  866. acur->len = 0;
  867. acur->diff = -1;
  868. acur->busy = false;
  869. acur->busy_gen = 0;
  870. /*
  871. * Perform an initial cntbt lookup to check for availability of maxlen
  872. * extents. If this fails, we'll return -ENOSPC to signal the caller to
  873. * attempt a small allocation.
  874. */
  875. if (!acur->cnt)
  876. acur->cnt = xfs_cntbt_init_cursor(args->mp, args->tp,
  877. args->agbp, args->pag);
  878. error = xfs_alloc_lookup_ge(acur->cnt, 0, args->maxlen, &i);
  879. if (error)
  880. return error;
  881. /*
  882. * Allocate the bnobt left and right search cursors.
  883. */
  884. if (!acur->bnolt)
  885. acur->bnolt = xfs_bnobt_init_cursor(args->mp, args->tp,
  886. args->agbp, args->pag);
  887. if (!acur->bnogt)
  888. acur->bnogt = xfs_bnobt_init_cursor(args->mp, args->tp,
  889. args->agbp, args->pag);
  890. return i == 1 ? 0 : -ENOSPC;
  891. }
  892. static void
  893. xfs_alloc_cur_close(
  894. struct xfs_alloc_cur *acur,
  895. bool error)
  896. {
  897. int cur_error = XFS_BTREE_NOERROR;
  898. if (error)
  899. cur_error = XFS_BTREE_ERROR;
  900. if (acur->cnt)
  901. xfs_btree_del_cursor(acur->cnt, cur_error);
  902. if (acur->bnolt)
  903. xfs_btree_del_cursor(acur->bnolt, cur_error);
  904. if (acur->bnogt)
  905. xfs_btree_del_cursor(acur->bnogt, cur_error);
  906. acur->cnt = acur->bnolt = acur->bnogt = NULL;
  907. }
  908. /*
  909. * Check an extent for allocation and track the best available candidate in the
  910. * allocation structure. The cursor is deactivated if it has entered an out of
  911. * range state based on allocation arguments. Optionally return the extent
  912. * extent geometry and allocation status if requested by the caller.
  913. */
  914. static int
  915. xfs_alloc_cur_check(
  916. struct xfs_alloc_arg *args,
  917. struct xfs_alloc_cur *acur,
  918. struct xfs_btree_cur *cur,
  919. int *new)
  920. {
  921. int error, i;
  922. xfs_agblock_t bno, bnoa, bnew;
  923. xfs_extlen_t len, lena, diff = -1;
  924. bool busy;
  925. unsigned busy_gen = 0;
  926. bool deactivate = false;
  927. bool isbnobt = xfs_btree_is_bno(cur->bc_ops);
  928. *new = 0;
  929. error = xfs_alloc_get_rec(cur, &bno, &len, &i);
  930. if (error)
  931. return error;
  932. if (XFS_IS_CORRUPT(args->mp, i != 1)) {
  933. xfs_btree_mark_sick(cur);
  934. return -EFSCORRUPTED;
  935. }
  936. /*
  937. * Check minlen and deactivate a cntbt cursor if out of acceptable size
  938. * range (i.e., walking backwards looking for a minlen extent).
  939. */
  940. if (len < args->minlen) {
  941. deactivate = !isbnobt;
  942. goto out;
  943. }
  944. busy = xfs_alloc_compute_aligned(args, bno, len, &bnoa, &lena,
  945. &busy_gen);
  946. acur->busy |= busy;
  947. if (busy)
  948. acur->busy_gen = busy_gen;
  949. /* deactivate a bnobt cursor outside of locality range */
  950. if (bnoa < args->min_agbno || bnoa > args->max_agbno) {
  951. deactivate = isbnobt;
  952. goto out;
  953. }
  954. if (lena < args->minlen)
  955. goto out;
  956. args->len = XFS_EXTLEN_MIN(lena, args->maxlen);
  957. xfs_alloc_fix_len(args);
  958. ASSERT(args->len >= args->minlen);
  959. if (args->len < acur->len)
  960. goto out;
  961. /*
  962. * We have an aligned record that satisfies minlen and beats or matches
  963. * the candidate extent size. Compare locality for near allocation mode.
  964. */
  965. diff = xfs_alloc_compute_diff(args->agbno, args->len,
  966. args->alignment, args->datatype,
  967. bnoa, lena, &bnew);
  968. if (bnew == NULLAGBLOCK)
  969. goto out;
  970. /*
  971. * Deactivate a bnobt cursor with worse locality than the current best.
  972. */
  973. if (diff > acur->diff) {
  974. deactivate = isbnobt;
  975. goto out;
  976. }
  977. ASSERT(args->len > acur->len ||
  978. (args->len == acur->len && diff <= acur->diff));
  979. acur->rec_bno = bno;
  980. acur->rec_len = len;
  981. acur->bno = bnew;
  982. acur->len = args->len;
  983. acur->diff = diff;
  984. *new = 1;
  985. /*
  986. * We're done if we found a perfect allocation. This only deactivates
  987. * the current cursor, but this is just an optimization to terminate a
  988. * cntbt search that otherwise runs to the edge of the tree.
  989. */
  990. if (acur->diff == 0 && acur->len == args->maxlen)
  991. deactivate = true;
  992. out:
  993. if (deactivate)
  994. cur->bc_flags &= ~XFS_BTREE_ALLOCBT_ACTIVE;
  995. trace_xfs_alloc_cur_check(cur, bno, len, diff, *new);
  996. return 0;
  997. }
  998. /*
  999. * Complete an allocation of a candidate extent. Remove the extent from both
  1000. * trees and update the args structure.
  1001. */
  1002. STATIC int
  1003. xfs_alloc_cur_finish(
  1004. struct xfs_alloc_arg *args,
  1005. struct xfs_alloc_cur *acur)
  1006. {
  1007. int error;
  1008. ASSERT(acur->cnt && acur->bnolt);
  1009. ASSERT(acur->bno >= acur->rec_bno);
  1010. ASSERT(acur->bno + acur->len <= acur->rec_bno + acur->rec_len);
  1011. ASSERT(xfs_verify_agbext(args->pag, acur->rec_bno, acur->rec_len));
  1012. error = xfs_alloc_fixup_trees(acur->cnt, acur->bnolt, acur->rec_bno,
  1013. acur->rec_len, acur->bno, acur->len, 0);
  1014. if (error)
  1015. return error;
  1016. args->agbno = acur->bno;
  1017. args->len = acur->len;
  1018. args->wasfromfl = 0;
  1019. trace_xfs_alloc_cur(args);
  1020. return 0;
  1021. }
  1022. /*
  1023. * Locality allocation lookup algorithm. This expects a cntbt cursor and uses
  1024. * bno optimized lookup to search for extents with ideal size and locality.
  1025. */
  1026. STATIC int
  1027. xfs_alloc_cntbt_iter(
  1028. struct xfs_alloc_arg *args,
  1029. struct xfs_alloc_cur *acur)
  1030. {
  1031. struct xfs_btree_cur *cur = acur->cnt;
  1032. xfs_agblock_t bno;
  1033. xfs_extlen_t len, cur_len;
  1034. int error;
  1035. int i;
  1036. if (!xfs_alloc_cur_active(cur))
  1037. return 0;
  1038. /* locality optimized lookup */
  1039. cur_len = acur->cur_len;
  1040. error = xfs_alloc_lookup_ge(cur, args->agbno, cur_len, &i);
  1041. if (error)
  1042. return error;
  1043. if (i == 0)
  1044. return 0;
  1045. error = xfs_alloc_get_rec(cur, &bno, &len, &i);
  1046. if (error)
  1047. return error;
  1048. /* check the current record and update search length from it */
  1049. error = xfs_alloc_cur_check(args, acur, cur, &i);
  1050. if (error)
  1051. return error;
  1052. ASSERT(len >= acur->cur_len);
  1053. acur->cur_len = len;
  1054. /*
  1055. * We looked up the first record >= [agbno, len] above. The agbno is a
  1056. * secondary key and so the current record may lie just before or after
  1057. * agbno. If it is past agbno, check the previous record too so long as
  1058. * the length matches as it may be closer. Don't check a smaller record
  1059. * because that could deactivate our cursor.
  1060. */
  1061. if (bno > args->agbno) {
  1062. error = xfs_btree_decrement(cur, 0, &i);
  1063. if (!error && i) {
  1064. error = xfs_alloc_get_rec(cur, &bno, &len, &i);
  1065. if (!error && i && len == acur->cur_len)
  1066. error = xfs_alloc_cur_check(args, acur, cur,
  1067. &i);
  1068. }
  1069. if (error)
  1070. return error;
  1071. }
  1072. /*
  1073. * Increment the search key until we find at least one allocation
  1074. * candidate or if the extent we found was larger. Otherwise, double the
  1075. * search key to optimize the search. Efficiency is more important here
  1076. * than absolute best locality.
  1077. */
  1078. cur_len <<= 1;
  1079. if (!acur->len || acur->cur_len >= cur_len)
  1080. acur->cur_len++;
  1081. else
  1082. acur->cur_len = cur_len;
  1083. return error;
  1084. }
  1085. /*
  1086. * Deal with the case where only small freespaces remain. Either return the
  1087. * contents of the last freespace record, or allocate space from the freelist if
  1088. * there is nothing in the tree.
  1089. */
  1090. STATIC int /* error */
  1091. xfs_alloc_ag_vextent_small(
  1092. struct xfs_alloc_arg *args, /* allocation argument structure */
  1093. struct xfs_btree_cur *ccur, /* optional by-size cursor */
  1094. xfs_agblock_t *fbnop, /* result block number */
  1095. xfs_extlen_t *flenp, /* result length */
  1096. int *stat) /* status: 0-freelist, 1-normal/none */
  1097. {
  1098. struct xfs_agf *agf = args->agbp->b_addr;
  1099. int error = 0;
  1100. xfs_agblock_t fbno = NULLAGBLOCK;
  1101. xfs_extlen_t flen = 0;
  1102. int i = 0;
  1103. /*
  1104. * If a cntbt cursor is provided, try to allocate the largest record in
  1105. * the tree. Try the AGFL if the cntbt is empty, otherwise fail the
  1106. * allocation. Make sure to respect minleft even when pulling from the
  1107. * freelist.
  1108. */
  1109. if (ccur)
  1110. error = xfs_btree_decrement(ccur, 0, &i);
  1111. if (error)
  1112. goto error;
  1113. if (i) {
  1114. error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i);
  1115. if (error)
  1116. goto error;
  1117. if (XFS_IS_CORRUPT(args->mp, i != 1)) {
  1118. xfs_btree_mark_sick(ccur);
  1119. error = -EFSCORRUPTED;
  1120. goto error;
  1121. }
  1122. goto out;
  1123. }
  1124. if (args->minlen != 1 || args->alignment != 1 ||
  1125. args->resv == XFS_AG_RESV_AGFL ||
  1126. be32_to_cpu(agf->agf_flcount) <= args->minleft)
  1127. goto out;
  1128. error = xfs_alloc_get_freelist(args->pag, args->tp, args->agbp,
  1129. &fbno, 0);
  1130. if (error)
  1131. goto error;
  1132. if (fbno == NULLAGBLOCK)
  1133. goto out;
  1134. xfs_extent_busy_reuse(args->mp, args->pag, fbno, 1,
  1135. (args->datatype & XFS_ALLOC_NOBUSY));
  1136. if (args->datatype & XFS_ALLOC_USERDATA) {
  1137. struct xfs_buf *bp;
  1138. error = xfs_trans_get_buf(args->tp, args->mp->m_ddev_targp,
  1139. XFS_AGB_TO_DADDR(args->mp, args->agno, fbno),
  1140. args->mp->m_bsize, 0, &bp);
  1141. if (error)
  1142. goto error;
  1143. xfs_trans_binval(args->tp, bp);
  1144. }
  1145. *fbnop = args->agbno = fbno;
  1146. *flenp = args->len = 1;
  1147. if (XFS_IS_CORRUPT(args->mp, fbno >= be32_to_cpu(agf->agf_length))) {
  1148. xfs_btree_mark_sick(ccur);
  1149. error = -EFSCORRUPTED;
  1150. goto error;
  1151. }
  1152. args->wasfromfl = 1;
  1153. trace_xfs_alloc_small_freelist(args);
  1154. /*
  1155. * If we're feeding an AGFL block to something that doesn't live in the
  1156. * free space, we need to clear out the OWN_AG rmap.
  1157. */
  1158. error = xfs_rmap_free(args->tp, args->agbp, args->pag, fbno, 1,
  1159. &XFS_RMAP_OINFO_AG);
  1160. if (error)
  1161. goto error;
  1162. *stat = 0;
  1163. return 0;
  1164. out:
  1165. /*
  1166. * Can't do the allocation, give up.
  1167. */
  1168. if (flen < args->minlen) {
  1169. args->agbno = NULLAGBLOCK;
  1170. trace_xfs_alloc_small_notenough(args);
  1171. flen = 0;
  1172. }
  1173. *fbnop = fbno;
  1174. *flenp = flen;
  1175. *stat = 1;
  1176. trace_xfs_alloc_small_done(args);
  1177. return 0;
  1178. error:
  1179. trace_xfs_alloc_small_error(args);
  1180. return error;
  1181. }
  1182. /*
  1183. * Allocate a variable extent at exactly agno/bno.
  1184. * Extent's length (returned in *len) will be between minlen and maxlen,
  1185. * and of the form k * prod + mod unless there's nothing that large.
  1186. * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
  1187. */
  1188. STATIC int /* error */
  1189. xfs_alloc_ag_vextent_exact(
  1190. xfs_alloc_arg_t *args) /* allocation argument structure */
  1191. {
  1192. struct xfs_btree_cur *bno_cur;/* by block-number btree cursor */
  1193. struct xfs_btree_cur *cnt_cur;/* by count btree cursor */
  1194. int error;
  1195. xfs_agblock_t fbno; /* start block of found extent */
  1196. xfs_extlen_t flen; /* length of found extent */
  1197. xfs_agblock_t tbno; /* start block of busy extent */
  1198. xfs_extlen_t tlen; /* length of busy extent */
  1199. xfs_agblock_t tend; /* end block of busy extent */
  1200. int i; /* success/failure of operation */
  1201. unsigned busy_gen;
  1202. ASSERT(args->alignment == 1);
  1203. /*
  1204. * Allocate/initialize a cursor for the by-number freespace btree.
  1205. */
  1206. bno_cur = xfs_bnobt_init_cursor(args->mp, args->tp, args->agbp,
  1207. args->pag);
  1208. /*
  1209. * Lookup bno and minlen in the btree (minlen is irrelevant, really).
  1210. * Look for the closest free block <= bno, it must contain bno
  1211. * if any free block does.
  1212. */
  1213. error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
  1214. if (error)
  1215. goto error0;
  1216. if (!i)
  1217. goto not_found;
  1218. /*
  1219. * Grab the freespace record.
  1220. */
  1221. error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
  1222. if (error)
  1223. goto error0;
  1224. if (XFS_IS_CORRUPT(args->mp, i != 1)) {
  1225. xfs_btree_mark_sick(bno_cur);
  1226. error = -EFSCORRUPTED;
  1227. goto error0;
  1228. }
  1229. ASSERT(fbno <= args->agbno);
  1230. /*
  1231. * Check for overlapping busy extents.
  1232. */
  1233. tbno = fbno;
  1234. tlen = flen;
  1235. xfs_extent_busy_trim(args, &tbno, &tlen, &busy_gen);
  1236. /*
  1237. * Give up if the start of the extent is busy, or the freespace isn't
  1238. * long enough for the minimum request.
  1239. */
  1240. if (tbno > args->agbno)
  1241. goto not_found;
  1242. if (tlen < args->minlen)
  1243. goto not_found;
  1244. tend = tbno + tlen;
  1245. if (tend < args->agbno + args->minlen)
  1246. goto not_found;
  1247. /*
  1248. * End of extent will be smaller of the freespace end and the
  1249. * maximal requested end.
  1250. *
  1251. * Fix the length according to mod and prod if given.
  1252. */
  1253. args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
  1254. - args->agbno;
  1255. xfs_alloc_fix_len(args);
  1256. ASSERT(args->agbno + args->len <= tend);
  1257. /*
  1258. * We are allocating agbno for args->len
  1259. * Allocate/initialize a cursor for the by-size btree.
  1260. */
  1261. cnt_cur = xfs_cntbt_init_cursor(args->mp, args->tp, args->agbp,
  1262. args->pag);
  1263. ASSERT(xfs_verify_agbext(args->pag, args->agbno, args->len));
  1264. error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
  1265. args->len, XFSA_FIXUP_BNO_OK);
  1266. if (error) {
  1267. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1268. goto error0;
  1269. }
  1270. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1271. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1272. args->wasfromfl = 0;
  1273. trace_xfs_alloc_exact_done(args);
  1274. return 0;
  1275. not_found:
  1276. /* Didn't find it, return null. */
  1277. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1278. args->agbno = NULLAGBLOCK;
  1279. trace_xfs_alloc_exact_notfound(args);
  1280. return 0;
  1281. error0:
  1282. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1283. trace_xfs_alloc_exact_error(args);
  1284. return error;
  1285. }
  1286. /*
  1287. * Search a given number of btree records in a given direction. Check each
  1288. * record against the good extent we've already found.
  1289. */
  1290. STATIC int
  1291. xfs_alloc_walk_iter(
  1292. struct xfs_alloc_arg *args,
  1293. struct xfs_alloc_cur *acur,
  1294. struct xfs_btree_cur *cur,
  1295. bool increment,
  1296. bool find_one, /* quit on first candidate */
  1297. int count, /* rec count (-1 for infinite) */
  1298. int *stat)
  1299. {
  1300. int error;
  1301. int i;
  1302. *stat = 0;
  1303. /*
  1304. * Search so long as the cursor is active or we find a better extent.
  1305. * The cursor is deactivated if it extends beyond the range of the
  1306. * current allocation candidate.
  1307. */
  1308. while (xfs_alloc_cur_active(cur) && count) {
  1309. error = xfs_alloc_cur_check(args, acur, cur, &i);
  1310. if (error)
  1311. return error;
  1312. if (i == 1) {
  1313. *stat = 1;
  1314. if (find_one)
  1315. break;
  1316. }
  1317. if (!xfs_alloc_cur_active(cur))
  1318. break;
  1319. if (increment)
  1320. error = xfs_btree_increment(cur, 0, &i);
  1321. else
  1322. error = xfs_btree_decrement(cur, 0, &i);
  1323. if (error)
  1324. return error;
  1325. if (i == 0)
  1326. cur->bc_flags &= ~XFS_BTREE_ALLOCBT_ACTIVE;
  1327. if (count > 0)
  1328. count--;
  1329. }
  1330. return 0;
  1331. }
  1332. /*
  1333. * Search the by-bno and by-size btrees in parallel in search of an extent with
  1334. * ideal locality based on the NEAR mode ->agbno locality hint.
  1335. */
  1336. STATIC int
  1337. xfs_alloc_ag_vextent_locality(
  1338. struct xfs_alloc_arg *args,
  1339. struct xfs_alloc_cur *acur,
  1340. int *stat)
  1341. {
  1342. struct xfs_btree_cur *fbcur = NULL;
  1343. int error;
  1344. int i;
  1345. bool fbinc;
  1346. ASSERT(acur->len == 0);
  1347. *stat = 0;
  1348. error = xfs_alloc_lookup_ge(acur->cnt, args->agbno, acur->cur_len, &i);
  1349. if (error)
  1350. return error;
  1351. error = xfs_alloc_lookup_le(acur->bnolt, args->agbno, 0, &i);
  1352. if (error)
  1353. return error;
  1354. error = xfs_alloc_lookup_ge(acur->bnogt, args->agbno, 0, &i);
  1355. if (error)
  1356. return error;
  1357. /*
  1358. * Search the bnobt and cntbt in parallel. Search the bnobt left and
  1359. * right and lookup the closest extent to the locality hint for each
  1360. * extent size key in the cntbt. The entire search terminates
  1361. * immediately on a bnobt hit because that means we've found best case
  1362. * locality. Otherwise the search continues until the cntbt cursor runs
  1363. * off the end of the tree. If no allocation candidate is found at this
  1364. * point, give up on locality, walk backwards from the end of the cntbt
  1365. * and take the first available extent.
  1366. *
  1367. * The parallel tree searches balance each other out to provide fairly
  1368. * consistent performance for various situations. The bnobt search can
  1369. * have pathological behavior in the worst case scenario of larger
  1370. * allocation requests and fragmented free space. On the other hand, the
  1371. * bnobt is able to satisfy most smaller allocation requests much more
  1372. * quickly than the cntbt. The cntbt search can sift through fragmented
  1373. * free space and sets of free extents for larger allocation requests
  1374. * more quickly than the bnobt. Since the locality hint is just a hint
  1375. * and we don't want to scan the entire bnobt for perfect locality, the
  1376. * cntbt search essentially bounds the bnobt search such that we can
  1377. * find good enough locality at reasonable performance in most cases.
  1378. */
  1379. while (xfs_alloc_cur_active(acur->bnolt) ||
  1380. xfs_alloc_cur_active(acur->bnogt) ||
  1381. xfs_alloc_cur_active(acur->cnt)) {
  1382. trace_xfs_alloc_cur_lookup(args);
  1383. /*
  1384. * Search the bnobt left and right. In the case of a hit, finish
  1385. * the search in the opposite direction and we're done.
  1386. */
  1387. error = xfs_alloc_walk_iter(args, acur, acur->bnolt, false,
  1388. true, 1, &i);
  1389. if (error)
  1390. return error;
  1391. if (i == 1) {
  1392. trace_xfs_alloc_cur_left(args);
  1393. fbcur = acur->bnogt;
  1394. fbinc = true;
  1395. break;
  1396. }
  1397. error = xfs_alloc_walk_iter(args, acur, acur->bnogt, true, true,
  1398. 1, &i);
  1399. if (error)
  1400. return error;
  1401. if (i == 1) {
  1402. trace_xfs_alloc_cur_right(args);
  1403. fbcur = acur->bnolt;
  1404. fbinc = false;
  1405. break;
  1406. }
  1407. /*
  1408. * Check the extent with best locality based on the current
  1409. * extent size search key and keep track of the best candidate.
  1410. */
  1411. error = xfs_alloc_cntbt_iter(args, acur);
  1412. if (error)
  1413. return error;
  1414. if (!xfs_alloc_cur_active(acur->cnt)) {
  1415. trace_xfs_alloc_cur_lookup_done(args);
  1416. break;
  1417. }
  1418. }
  1419. /*
  1420. * If we failed to find anything due to busy extents, return empty
  1421. * handed so the caller can flush and retry. If no busy extents were
  1422. * found, walk backwards from the end of the cntbt as a last resort.
  1423. */
  1424. if (!xfs_alloc_cur_active(acur->cnt) && !acur->len && !acur->busy) {
  1425. error = xfs_btree_decrement(acur->cnt, 0, &i);
  1426. if (error)
  1427. return error;
  1428. if (i) {
  1429. acur->cnt->bc_flags |= XFS_BTREE_ALLOCBT_ACTIVE;
  1430. fbcur = acur->cnt;
  1431. fbinc = false;
  1432. }
  1433. }
  1434. /*
  1435. * Search in the opposite direction for a better entry in the case of
  1436. * a bnobt hit or walk backwards from the end of the cntbt.
  1437. */
  1438. if (fbcur) {
  1439. error = xfs_alloc_walk_iter(args, acur, fbcur, fbinc, true, -1,
  1440. &i);
  1441. if (error)
  1442. return error;
  1443. }
  1444. if (acur->len)
  1445. *stat = 1;
  1446. return 0;
  1447. }
  1448. /* Check the last block of the cnt btree for allocations. */
  1449. static int
  1450. xfs_alloc_ag_vextent_lastblock(
  1451. struct xfs_alloc_arg *args,
  1452. struct xfs_alloc_cur *acur,
  1453. xfs_agblock_t *bno,
  1454. xfs_extlen_t *len,
  1455. bool *allocated)
  1456. {
  1457. int error;
  1458. int i;
  1459. #ifdef DEBUG
  1460. /* Randomly don't execute the first algorithm. */
  1461. if (get_random_u32_below(2))
  1462. return 0;
  1463. #endif
  1464. /*
  1465. * Start from the entry that lookup found, sequence through all larger
  1466. * free blocks. If we're actually pointing at a record smaller than
  1467. * maxlen, go to the start of this block, and skip all those smaller
  1468. * than minlen.
  1469. */
  1470. if (*len || args->alignment > 1) {
  1471. acur->cnt->bc_levels[0].ptr = 1;
  1472. do {
  1473. error = xfs_alloc_get_rec(acur->cnt, bno, len, &i);
  1474. if (error)
  1475. return error;
  1476. if (XFS_IS_CORRUPT(args->mp, i != 1)) {
  1477. xfs_btree_mark_sick(acur->cnt);
  1478. return -EFSCORRUPTED;
  1479. }
  1480. if (*len >= args->minlen)
  1481. break;
  1482. error = xfs_btree_increment(acur->cnt, 0, &i);
  1483. if (error)
  1484. return error;
  1485. } while (i);
  1486. ASSERT(*len >= args->minlen);
  1487. if (!i)
  1488. return 0;
  1489. }
  1490. error = xfs_alloc_walk_iter(args, acur, acur->cnt, true, false, -1, &i);
  1491. if (error)
  1492. return error;
  1493. /*
  1494. * It didn't work. We COULD be in a case where there's a good record
  1495. * somewhere, so try again.
  1496. */
  1497. if (acur->len == 0)
  1498. return 0;
  1499. trace_xfs_alloc_near_first(args);
  1500. *allocated = true;
  1501. return 0;
  1502. }
  1503. /*
  1504. * Allocate a variable extent near bno in the allocation group agno.
  1505. * Extent's length (returned in len) will be between minlen and maxlen,
  1506. * and of the form k * prod + mod unless there's nothing that large.
  1507. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  1508. */
  1509. STATIC int
  1510. xfs_alloc_ag_vextent_near(
  1511. struct xfs_alloc_arg *args,
  1512. uint32_t alloc_flags)
  1513. {
  1514. struct xfs_alloc_cur acur = {};
  1515. int error; /* error code */
  1516. int i; /* result code, temporary */
  1517. xfs_agblock_t bno;
  1518. xfs_extlen_t len;
  1519. /* handle uninitialized agbno range so caller doesn't have to */
  1520. if (!args->min_agbno && !args->max_agbno)
  1521. args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
  1522. ASSERT(args->min_agbno <= args->max_agbno);
  1523. /* clamp agbno to the range if it's outside */
  1524. if (args->agbno < args->min_agbno)
  1525. args->agbno = args->min_agbno;
  1526. if (args->agbno > args->max_agbno)
  1527. args->agbno = args->max_agbno;
  1528. /* Retry once quickly if we find busy extents before blocking. */
  1529. alloc_flags |= XFS_ALLOC_FLAG_TRYFLUSH;
  1530. restart:
  1531. len = 0;
  1532. /*
  1533. * Set up cursors and see if there are any free extents as big as
  1534. * maxlen. If not, pick the last entry in the tree unless the tree is
  1535. * empty.
  1536. */
  1537. error = xfs_alloc_cur_setup(args, &acur);
  1538. if (error == -ENOSPC) {
  1539. error = xfs_alloc_ag_vextent_small(args, acur.cnt, &bno,
  1540. &len, &i);
  1541. if (error)
  1542. goto out;
  1543. if (i == 0 || len == 0) {
  1544. trace_xfs_alloc_near_noentry(args);
  1545. goto out;
  1546. }
  1547. ASSERT(i == 1);
  1548. } else if (error) {
  1549. goto out;
  1550. }
  1551. /*
  1552. * First algorithm.
  1553. * If the requested extent is large wrt the freespaces available
  1554. * in this a.g., then the cursor will be pointing to a btree entry
  1555. * near the right edge of the tree. If it's in the last btree leaf
  1556. * block, then we just examine all the entries in that block
  1557. * that are big enough, and pick the best one.
  1558. */
  1559. if (xfs_btree_islastblock(acur.cnt, 0)) {
  1560. bool allocated = false;
  1561. error = xfs_alloc_ag_vextent_lastblock(args, &acur, &bno, &len,
  1562. &allocated);
  1563. if (error)
  1564. goto out;
  1565. if (allocated)
  1566. goto alloc_finish;
  1567. }
  1568. /*
  1569. * Second algorithm. Combined cntbt and bnobt search to find ideal
  1570. * locality.
  1571. */
  1572. error = xfs_alloc_ag_vextent_locality(args, &acur, &i);
  1573. if (error)
  1574. goto out;
  1575. /*
  1576. * If we couldn't get anything, give up.
  1577. */
  1578. if (!acur.len) {
  1579. if (acur.busy) {
  1580. /*
  1581. * Our only valid extents must have been busy. Flush and
  1582. * retry the allocation again. If we get an -EAGAIN
  1583. * error, we're being told that a deadlock was avoided
  1584. * and the current transaction needs committing before
  1585. * the allocation can be retried.
  1586. */
  1587. trace_xfs_alloc_near_busy(args);
  1588. error = xfs_extent_busy_flush(args->tp, args->pag,
  1589. acur.busy_gen, alloc_flags);
  1590. if (error)
  1591. goto out;
  1592. alloc_flags &= ~XFS_ALLOC_FLAG_TRYFLUSH;
  1593. goto restart;
  1594. }
  1595. trace_xfs_alloc_size_neither(args);
  1596. args->agbno = NULLAGBLOCK;
  1597. goto out;
  1598. }
  1599. alloc_finish:
  1600. /* fix up btrees on a successful allocation */
  1601. error = xfs_alloc_cur_finish(args, &acur);
  1602. out:
  1603. xfs_alloc_cur_close(&acur, error);
  1604. return error;
  1605. }
  1606. /*
  1607. * Allocate a variable extent anywhere in the allocation group agno.
  1608. * Extent's length (returned in len) will be between minlen and maxlen,
  1609. * and of the form k * prod + mod unless there's nothing that large.
  1610. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  1611. */
  1612. static int
  1613. xfs_alloc_ag_vextent_size(
  1614. struct xfs_alloc_arg *args,
  1615. uint32_t alloc_flags)
  1616. {
  1617. struct xfs_agf *agf = args->agbp->b_addr;
  1618. struct xfs_btree_cur *bno_cur;
  1619. struct xfs_btree_cur *cnt_cur;
  1620. xfs_agblock_t fbno; /* start of found freespace */
  1621. xfs_extlen_t flen; /* length of found freespace */
  1622. xfs_agblock_t rbno; /* returned block number */
  1623. xfs_extlen_t rlen; /* length of returned extent */
  1624. bool busy;
  1625. unsigned busy_gen;
  1626. int error;
  1627. int i;
  1628. /* Retry once quickly if we find busy extents before blocking. */
  1629. alloc_flags |= XFS_ALLOC_FLAG_TRYFLUSH;
  1630. restart:
  1631. /*
  1632. * Allocate and initialize a cursor for the by-size btree.
  1633. */
  1634. cnt_cur = xfs_cntbt_init_cursor(args->mp, args->tp, args->agbp,
  1635. args->pag);
  1636. bno_cur = NULL;
  1637. /*
  1638. * Look for an entry >= maxlen+alignment-1 blocks.
  1639. */
  1640. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
  1641. args->maxlen + args->alignment - 1, &i)))
  1642. goto error0;
  1643. /*
  1644. * If none then we have to settle for a smaller extent. In the case that
  1645. * there are no large extents, this will return the last entry in the
  1646. * tree unless the tree is empty. In the case that there are only busy
  1647. * large extents, this will return the largest small extent unless there
  1648. * are no smaller extents available.
  1649. */
  1650. if (!i) {
  1651. error = xfs_alloc_ag_vextent_small(args, cnt_cur,
  1652. &fbno, &flen, &i);
  1653. if (error)
  1654. goto error0;
  1655. if (i == 0 || flen == 0) {
  1656. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1657. trace_xfs_alloc_size_noentry(args);
  1658. return 0;
  1659. }
  1660. ASSERT(i == 1);
  1661. busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
  1662. &rlen, &busy_gen);
  1663. } else {
  1664. /*
  1665. * Search for a non-busy extent that is large enough.
  1666. */
  1667. for (;;) {
  1668. error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
  1669. if (error)
  1670. goto error0;
  1671. if (XFS_IS_CORRUPT(args->mp, i != 1)) {
  1672. xfs_btree_mark_sick(cnt_cur);
  1673. error = -EFSCORRUPTED;
  1674. goto error0;
  1675. }
  1676. busy = xfs_alloc_compute_aligned(args, fbno, flen,
  1677. &rbno, &rlen, &busy_gen);
  1678. if (rlen >= args->maxlen)
  1679. break;
  1680. error = xfs_btree_increment(cnt_cur, 0, &i);
  1681. if (error)
  1682. goto error0;
  1683. if (i)
  1684. continue;
  1685. /*
  1686. * Our only valid extents must have been busy. Flush and
  1687. * retry the allocation again. If we get an -EAGAIN
  1688. * error, we're being told that a deadlock was avoided
  1689. * and the current transaction needs committing before
  1690. * the allocation can be retried.
  1691. */
  1692. trace_xfs_alloc_size_busy(args);
  1693. error = xfs_extent_busy_flush(args->tp, args->pag,
  1694. busy_gen, alloc_flags);
  1695. if (error)
  1696. goto error0;
  1697. alloc_flags &= ~XFS_ALLOC_FLAG_TRYFLUSH;
  1698. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1699. goto restart;
  1700. }
  1701. }
  1702. /*
  1703. * In the first case above, we got the last entry in the
  1704. * by-size btree. Now we check to see if the space hits maxlen
  1705. * once aligned; if not, we search left for something better.
  1706. * This can't happen in the second case above.
  1707. */
  1708. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1709. if (XFS_IS_CORRUPT(args->mp,
  1710. rlen != 0 &&
  1711. (rlen > flen ||
  1712. rbno + rlen > fbno + flen))) {
  1713. xfs_btree_mark_sick(cnt_cur);
  1714. error = -EFSCORRUPTED;
  1715. goto error0;
  1716. }
  1717. if (rlen < args->maxlen) {
  1718. xfs_agblock_t bestfbno;
  1719. xfs_extlen_t bestflen;
  1720. xfs_agblock_t bestrbno;
  1721. xfs_extlen_t bestrlen;
  1722. bestrlen = rlen;
  1723. bestrbno = rbno;
  1724. bestflen = flen;
  1725. bestfbno = fbno;
  1726. for (;;) {
  1727. if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
  1728. goto error0;
  1729. if (i == 0)
  1730. break;
  1731. if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
  1732. &i)))
  1733. goto error0;
  1734. if (XFS_IS_CORRUPT(args->mp, i != 1)) {
  1735. xfs_btree_mark_sick(cnt_cur);
  1736. error = -EFSCORRUPTED;
  1737. goto error0;
  1738. }
  1739. if (flen <= bestrlen)
  1740. break;
  1741. busy = xfs_alloc_compute_aligned(args, fbno, flen,
  1742. &rbno, &rlen, &busy_gen);
  1743. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1744. if (XFS_IS_CORRUPT(args->mp,
  1745. rlen != 0 &&
  1746. (rlen > flen ||
  1747. rbno + rlen > fbno + flen))) {
  1748. xfs_btree_mark_sick(cnt_cur);
  1749. error = -EFSCORRUPTED;
  1750. goto error0;
  1751. }
  1752. if (rlen > bestrlen) {
  1753. bestrlen = rlen;
  1754. bestrbno = rbno;
  1755. bestflen = flen;
  1756. bestfbno = fbno;
  1757. if (rlen == args->maxlen)
  1758. break;
  1759. }
  1760. }
  1761. if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
  1762. &i)))
  1763. goto error0;
  1764. if (XFS_IS_CORRUPT(args->mp, i != 1)) {
  1765. xfs_btree_mark_sick(cnt_cur);
  1766. error = -EFSCORRUPTED;
  1767. goto error0;
  1768. }
  1769. rlen = bestrlen;
  1770. rbno = bestrbno;
  1771. flen = bestflen;
  1772. fbno = bestfbno;
  1773. }
  1774. args->wasfromfl = 0;
  1775. /*
  1776. * Fix up the length.
  1777. */
  1778. args->len = rlen;
  1779. if (rlen < args->minlen) {
  1780. if (busy) {
  1781. /*
  1782. * Our only valid extents must have been busy. Flush and
  1783. * retry the allocation again. If we get an -EAGAIN
  1784. * error, we're being told that a deadlock was avoided
  1785. * and the current transaction needs committing before
  1786. * the allocation can be retried.
  1787. */
  1788. trace_xfs_alloc_size_busy(args);
  1789. error = xfs_extent_busy_flush(args->tp, args->pag,
  1790. busy_gen, alloc_flags);
  1791. if (error)
  1792. goto error0;
  1793. alloc_flags &= ~XFS_ALLOC_FLAG_TRYFLUSH;
  1794. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1795. goto restart;
  1796. }
  1797. goto out_nominleft;
  1798. }
  1799. xfs_alloc_fix_len(args);
  1800. rlen = args->len;
  1801. if (XFS_IS_CORRUPT(args->mp, rlen > flen)) {
  1802. xfs_btree_mark_sick(cnt_cur);
  1803. error = -EFSCORRUPTED;
  1804. goto error0;
  1805. }
  1806. /*
  1807. * Allocate and initialize a cursor for the by-block tree.
  1808. */
  1809. bno_cur = xfs_bnobt_init_cursor(args->mp, args->tp, args->agbp,
  1810. args->pag);
  1811. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
  1812. rbno, rlen, XFSA_FIXUP_CNT_OK)))
  1813. goto error0;
  1814. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1815. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1816. cnt_cur = bno_cur = NULL;
  1817. args->len = rlen;
  1818. args->agbno = rbno;
  1819. if (XFS_IS_CORRUPT(args->mp,
  1820. args->agbno + args->len >
  1821. be32_to_cpu(agf->agf_length))) {
  1822. xfs_ag_mark_sick(args->pag, XFS_SICK_AG_BNOBT);
  1823. error = -EFSCORRUPTED;
  1824. goto error0;
  1825. }
  1826. trace_xfs_alloc_size_done(args);
  1827. return 0;
  1828. error0:
  1829. trace_xfs_alloc_size_error(args);
  1830. if (cnt_cur)
  1831. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1832. if (bno_cur)
  1833. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1834. return error;
  1835. out_nominleft:
  1836. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1837. trace_xfs_alloc_size_nominleft(args);
  1838. args->agbno = NULLAGBLOCK;
  1839. return 0;
  1840. }
  1841. /*
  1842. * Free the extent starting at agno/bno for length.
  1843. */
  1844. int
  1845. xfs_free_ag_extent(
  1846. struct xfs_trans *tp,
  1847. struct xfs_buf *agbp,
  1848. xfs_agnumber_t agno,
  1849. xfs_agblock_t bno,
  1850. xfs_extlen_t len,
  1851. const struct xfs_owner_info *oinfo,
  1852. enum xfs_ag_resv_type type)
  1853. {
  1854. struct xfs_mount *mp;
  1855. struct xfs_btree_cur *bno_cur;
  1856. struct xfs_btree_cur *cnt_cur;
  1857. xfs_agblock_t gtbno; /* start of right neighbor */
  1858. xfs_extlen_t gtlen; /* length of right neighbor */
  1859. xfs_agblock_t ltbno; /* start of left neighbor */
  1860. xfs_extlen_t ltlen; /* length of left neighbor */
  1861. xfs_agblock_t nbno; /* new starting block of freesp */
  1862. xfs_extlen_t nlen; /* new length of freespace */
  1863. int haveleft; /* have a left neighbor */
  1864. int haveright; /* have a right neighbor */
  1865. int i;
  1866. int error;
  1867. struct xfs_perag *pag = agbp->b_pag;
  1868. bool fixup_longest = false;
  1869. bno_cur = cnt_cur = NULL;
  1870. mp = tp->t_mountp;
  1871. if (!xfs_rmap_should_skip_owner_update(oinfo)) {
  1872. error = xfs_rmap_free(tp, agbp, pag, bno, len, oinfo);
  1873. if (error)
  1874. goto error0;
  1875. }
  1876. /*
  1877. * Allocate and initialize a cursor for the by-block btree.
  1878. */
  1879. bno_cur = xfs_bnobt_init_cursor(mp, tp, agbp, pag);
  1880. /*
  1881. * Look for a neighboring block on the left (lower block numbers)
  1882. * that is contiguous with this space.
  1883. */
  1884. if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
  1885. goto error0;
  1886. if (haveleft) {
  1887. /*
  1888. * There is a block to our left.
  1889. */
  1890. if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
  1891. goto error0;
  1892. if (XFS_IS_CORRUPT(mp, i != 1)) {
  1893. xfs_btree_mark_sick(bno_cur);
  1894. error = -EFSCORRUPTED;
  1895. goto error0;
  1896. }
  1897. /*
  1898. * It's not contiguous, though.
  1899. */
  1900. if (ltbno + ltlen < bno)
  1901. haveleft = 0;
  1902. else {
  1903. /*
  1904. * If this failure happens the request to free this
  1905. * space was invalid, it's (partly) already free.
  1906. * Very bad.
  1907. */
  1908. if (XFS_IS_CORRUPT(mp, ltbno + ltlen > bno)) {
  1909. xfs_btree_mark_sick(bno_cur);
  1910. error = -EFSCORRUPTED;
  1911. goto error0;
  1912. }
  1913. }
  1914. }
  1915. /*
  1916. * Look for a neighboring block on the right (higher block numbers)
  1917. * that is contiguous with this space.
  1918. */
  1919. if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
  1920. goto error0;
  1921. if (haveright) {
  1922. /*
  1923. * There is a block to our right.
  1924. */
  1925. if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
  1926. goto error0;
  1927. if (XFS_IS_CORRUPT(mp, i != 1)) {
  1928. xfs_btree_mark_sick(bno_cur);
  1929. error = -EFSCORRUPTED;
  1930. goto error0;
  1931. }
  1932. /*
  1933. * It's not contiguous, though.
  1934. */
  1935. if (bno + len < gtbno)
  1936. haveright = 0;
  1937. else {
  1938. /*
  1939. * If this failure happens the request to free this
  1940. * space was invalid, it's (partly) already free.
  1941. * Very bad.
  1942. */
  1943. if (XFS_IS_CORRUPT(mp, bno + len > gtbno)) {
  1944. xfs_btree_mark_sick(bno_cur);
  1945. error = -EFSCORRUPTED;
  1946. goto error0;
  1947. }
  1948. }
  1949. }
  1950. /*
  1951. * Now allocate and initialize a cursor for the by-size tree.
  1952. */
  1953. cnt_cur = xfs_cntbt_init_cursor(mp, tp, agbp, pag);
  1954. /*
  1955. * Have both left and right contiguous neighbors.
  1956. * Merge all three into a single free block.
  1957. */
  1958. if (haveleft && haveright) {
  1959. /*
  1960. * Delete the old by-size entry on the left.
  1961. */
  1962. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1963. goto error0;
  1964. if (XFS_IS_CORRUPT(mp, i != 1)) {
  1965. xfs_btree_mark_sick(cnt_cur);
  1966. error = -EFSCORRUPTED;
  1967. goto error0;
  1968. }
  1969. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1970. goto error0;
  1971. if (XFS_IS_CORRUPT(mp, i != 1)) {
  1972. xfs_btree_mark_sick(cnt_cur);
  1973. error = -EFSCORRUPTED;
  1974. goto error0;
  1975. }
  1976. /*
  1977. * Delete the old by-size entry on the right.
  1978. */
  1979. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1980. goto error0;
  1981. if (XFS_IS_CORRUPT(mp, i != 1)) {
  1982. xfs_btree_mark_sick(cnt_cur);
  1983. error = -EFSCORRUPTED;
  1984. goto error0;
  1985. }
  1986. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1987. goto error0;
  1988. if (XFS_IS_CORRUPT(mp, i != 1)) {
  1989. xfs_btree_mark_sick(cnt_cur);
  1990. error = -EFSCORRUPTED;
  1991. goto error0;
  1992. }
  1993. /*
  1994. * Delete the old by-block entry for the right block.
  1995. */
  1996. if ((error = xfs_btree_delete(bno_cur, &i)))
  1997. goto error0;
  1998. if (XFS_IS_CORRUPT(mp, i != 1)) {
  1999. xfs_btree_mark_sick(bno_cur);
  2000. error = -EFSCORRUPTED;
  2001. goto error0;
  2002. }
  2003. /*
  2004. * Move the by-block cursor back to the left neighbor.
  2005. */
  2006. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  2007. goto error0;
  2008. if (XFS_IS_CORRUPT(mp, i != 1)) {
  2009. xfs_btree_mark_sick(bno_cur);
  2010. error = -EFSCORRUPTED;
  2011. goto error0;
  2012. }
  2013. #ifdef DEBUG
  2014. /*
  2015. * Check that this is the right record: delete didn't
  2016. * mangle the cursor.
  2017. */
  2018. {
  2019. xfs_agblock_t xxbno;
  2020. xfs_extlen_t xxlen;
  2021. if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
  2022. &i)))
  2023. goto error0;
  2024. if (XFS_IS_CORRUPT(mp,
  2025. i != 1 ||
  2026. xxbno != ltbno ||
  2027. xxlen != ltlen)) {
  2028. xfs_btree_mark_sick(bno_cur);
  2029. error = -EFSCORRUPTED;
  2030. goto error0;
  2031. }
  2032. }
  2033. #endif
  2034. /*
  2035. * Update remaining by-block entry to the new, joined block.
  2036. */
  2037. nbno = ltbno;
  2038. nlen = len + ltlen + gtlen;
  2039. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  2040. goto error0;
  2041. }
  2042. /*
  2043. * Have only a left contiguous neighbor.
  2044. * Merge it together with the new freespace.
  2045. */
  2046. else if (haveleft) {
  2047. /*
  2048. * Delete the old by-size entry on the left.
  2049. */
  2050. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  2051. goto error0;
  2052. if (XFS_IS_CORRUPT(mp, i != 1)) {
  2053. xfs_btree_mark_sick(cnt_cur);
  2054. error = -EFSCORRUPTED;
  2055. goto error0;
  2056. }
  2057. if ((error = xfs_btree_delete(cnt_cur, &i)))
  2058. goto error0;
  2059. if (XFS_IS_CORRUPT(mp, i != 1)) {
  2060. xfs_btree_mark_sick(cnt_cur);
  2061. error = -EFSCORRUPTED;
  2062. goto error0;
  2063. }
  2064. /*
  2065. * Back up the by-block cursor to the left neighbor, and
  2066. * update its length.
  2067. */
  2068. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  2069. goto error0;
  2070. if (XFS_IS_CORRUPT(mp, i != 1)) {
  2071. xfs_btree_mark_sick(bno_cur);
  2072. error = -EFSCORRUPTED;
  2073. goto error0;
  2074. }
  2075. nbno = ltbno;
  2076. nlen = len + ltlen;
  2077. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  2078. goto error0;
  2079. }
  2080. /*
  2081. * Have only a right contiguous neighbor.
  2082. * Merge it together with the new freespace.
  2083. */
  2084. else if (haveright) {
  2085. /*
  2086. * Delete the old by-size entry on the right.
  2087. */
  2088. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  2089. goto error0;
  2090. if (XFS_IS_CORRUPT(mp, i != 1)) {
  2091. xfs_btree_mark_sick(cnt_cur);
  2092. error = -EFSCORRUPTED;
  2093. goto error0;
  2094. }
  2095. if ((error = xfs_btree_delete(cnt_cur, &i)))
  2096. goto error0;
  2097. if (XFS_IS_CORRUPT(mp, i != 1)) {
  2098. xfs_btree_mark_sick(cnt_cur);
  2099. error = -EFSCORRUPTED;
  2100. goto error0;
  2101. }
  2102. /*
  2103. * Update the starting block and length of the right
  2104. * neighbor in the by-block tree.
  2105. */
  2106. nbno = bno;
  2107. nlen = len + gtlen;
  2108. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  2109. goto error0;
  2110. }
  2111. /*
  2112. * No contiguous neighbors.
  2113. * Insert the new freespace into the by-block tree.
  2114. */
  2115. else {
  2116. nbno = bno;
  2117. nlen = len;
  2118. if ((error = xfs_btree_insert(bno_cur, &i)))
  2119. goto error0;
  2120. if (XFS_IS_CORRUPT(mp, i != 1)) {
  2121. xfs_btree_mark_sick(bno_cur);
  2122. error = -EFSCORRUPTED;
  2123. goto error0;
  2124. }
  2125. }
  2126. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  2127. bno_cur = NULL;
  2128. /*
  2129. * In all cases we need to insert the new freespace in the by-size tree.
  2130. *
  2131. * If this new freespace is being inserted in the block that contains
  2132. * the largest free space in the btree, make sure we also fix up the
  2133. * agf->agf-longest tracker field.
  2134. */
  2135. if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
  2136. goto error0;
  2137. if (XFS_IS_CORRUPT(mp, i != 0)) {
  2138. xfs_btree_mark_sick(cnt_cur);
  2139. error = -EFSCORRUPTED;
  2140. goto error0;
  2141. }
  2142. if (xfs_alloc_cursor_at_lastrec(cnt_cur))
  2143. fixup_longest = true;
  2144. if ((error = xfs_btree_insert(cnt_cur, &i)))
  2145. goto error0;
  2146. if (XFS_IS_CORRUPT(mp, i != 1)) {
  2147. xfs_btree_mark_sick(cnt_cur);
  2148. error = -EFSCORRUPTED;
  2149. goto error0;
  2150. }
  2151. if (fixup_longest) {
  2152. error = xfs_alloc_fixup_longest(cnt_cur);
  2153. if (error)
  2154. goto error0;
  2155. }
  2156. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  2157. cnt_cur = NULL;
  2158. /*
  2159. * Update the freespace totals in the ag and superblock.
  2160. */
  2161. error = xfs_alloc_update_counters(tp, agbp, len);
  2162. xfs_ag_resv_free_extent(agbp->b_pag, type, tp, len);
  2163. if (error)
  2164. goto error0;
  2165. XFS_STATS_INC(mp, xs_freex);
  2166. XFS_STATS_ADD(mp, xs_freeb, len);
  2167. trace_xfs_free_extent(mp, agno, bno, len, type, haveleft, haveright);
  2168. return 0;
  2169. error0:
  2170. trace_xfs_free_extent(mp, agno, bno, len, type, -1, -1);
  2171. if (bno_cur)
  2172. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  2173. if (cnt_cur)
  2174. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  2175. return error;
  2176. }
  2177. /*
  2178. * Visible (exported) allocation/free functions.
  2179. * Some of these are used just by xfs_alloc_btree.c and this file.
  2180. */
  2181. /*
  2182. * Compute and fill in value of m_alloc_maxlevels.
  2183. */
  2184. void
  2185. xfs_alloc_compute_maxlevels(
  2186. xfs_mount_t *mp) /* file system mount structure */
  2187. {
  2188. mp->m_alloc_maxlevels = xfs_btree_compute_maxlevels(mp->m_alloc_mnr,
  2189. (mp->m_sb.sb_agblocks + 1) / 2);
  2190. ASSERT(mp->m_alloc_maxlevels <= xfs_allocbt_maxlevels_ondisk());
  2191. }
  2192. /*
  2193. * Find the length of the longest extent in an AG. The 'need' parameter
  2194. * specifies how much space we're going to need for the AGFL and the
  2195. * 'reserved' parameter tells us how many blocks in this AG are reserved for
  2196. * other callers.
  2197. */
  2198. xfs_extlen_t
  2199. xfs_alloc_longest_free_extent(
  2200. struct xfs_perag *pag,
  2201. xfs_extlen_t need,
  2202. xfs_extlen_t reserved)
  2203. {
  2204. xfs_extlen_t delta = 0;
  2205. /*
  2206. * If the AGFL needs a recharge, we'll have to subtract that from the
  2207. * longest extent.
  2208. */
  2209. if (need > pag->pagf_flcount)
  2210. delta = need - pag->pagf_flcount;
  2211. /*
  2212. * If we cannot maintain others' reservations with space from the
  2213. * not-longest freesp extents, we'll have to subtract /that/ from
  2214. * the longest extent too.
  2215. */
  2216. if (pag->pagf_freeblks - pag->pagf_longest < reserved)
  2217. delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
  2218. /*
  2219. * If the longest extent is long enough to satisfy all the
  2220. * reservations and AGFL rules in place, we can return this extent.
  2221. */
  2222. if (pag->pagf_longest > delta)
  2223. return min_t(xfs_extlen_t, pag->pag_mount->m_ag_max_usable,
  2224. pag->pagf_longest - delta);
  2225. /* Otherwise, let the caller try for 1 block if there's space. */
  2226. return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
  2227. }
  2228. /*
  2229. * Compute the minimum length of the AGFL in the given AG. If @pag is NULL,
  2230. * return the largest possible minimum length.
  2231. */
  2232. unsigned int
  2233. xfs_alloc_min_freelist(
  2234. struct xfs_mount *mp,
  2235. struct xfs_perag *pag)
  2236. {
  2237. /* AG btrees have at least 1 level. */
  2238. const unsigned int bno_level = pag ? pag->pagf_bno_level : 1;
  2239. const unsigned int cnt_level = pag ? pag->pagf_cnt_level : 1;
  2240. const unsigned int rmap_level = pag ? pag->pagf_rmap_level : 1;
  2241. unsigned int min_free;
  2242. ASSERT(mp->m_alloc_maxlevels > 0);
  2243. /*
  2244. * For a btree shorter than the maximum height, the worst case is that
  2245. * every level gets split and a new level is added, then while inserting
  2246. * another entry to refill the AGFL, every level under the old root gets
  2247. * split again. This is:
  2248. *
  2249. * (full height split reservation) + (AGFL refill split height)
  2250. * = (current height + 1) + (current height - 1)
  2251. * = (new height) + (new height - 2)
  2252. * = 2 * new height - 2
  2253. *
  2254. * For a btree of maximum height, the worst case is that every level
  2255. * under the root gets split, then while inserting another entry to
  2256. * refill the AGFL, every level under the root gets split again. This is
  2257. * also:
  2258. *
  2259. * 2 * (current height - 1)
  2260. * = 2 * (new height - 1)
  2261. * = 2 * new height - 2
  2262. */
  2263. /* space needed by-bno freespace btree */
  2264. min_free = min(bno_level + 1, mp->m_alloc_maxlevels) * 2 - 2;
  2265. /* space needed by-size freespace btree */
  2266. min_free += min(cnt_level + 1, mp->m_alloc_maxlevels) * 2 - 2;
  2267. /* space needed reverse mapping used space btree */
  2268. if (xfs_has_rmapbt(mp))
  2269. min_free += min(rmap_level + 1, mp->m_rmap_maxlevels) * 2 - 2;
  2270. return min_free;
  2271. }
  2272. /*
  2273. * Check if the operation we are fixing up the freelist for should go ahead or
  2274. * not. If we are freeing blocks, we always allow it, otherwise the allocation
  2275. * is dependent on whether the size and shape of free space available will
  2276. * permit the requested allocation to take place.
  2277. */
  2278. static bool
  2279. xfs_alloc_space_available(
  2280. struct xfs_alloc_arg *args,
  2281. xfs_extlen_t min_free,
  2282. int flags)
  2283. {
  2284. struct xfs_perag *pag = args->pag;
  2285. xfs_extlen_t alloc_len, longest;
  2286. xfs_extlen_t reservation; /* blocks that are still reserved */
  2287. int available;
  2288. xfs_extlen_t agflcount;
  2289. if (flags & XFS_ALLOC_FLAG_FREEING)
  2290. return true;
  2291. reservation = xfs_ag_resv_needed(pag, args->resv);
  2292. /* do we have enough contiguous free space for the allocation? */
  2293. alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
  2294. longest = xfs_alloc_longest_free_extent(pag, min_free, reservation);
  2295. if (longest < alloc_len)
  2296. return false;
  2297. /*
  2298. * Do we have enough free space remaining for the allocation? Don't
  2299. * account extra agfl blocks because we are about to defer free them,
  2300. * making them unavailable until the current transaction commits.
  2301. */
  2302. agflcount = min_t(xfs_extlen_t, pag->pagf_flcount, min_free);
  2303. available = (int)(pag->pagf_freeblks + agflcount -
  2304. reservation - min_free - args->minleft);
  2305. if (available < (int)max(args->total, alloc_len))
  2306. return false;
  2307. /*
  2308. * Clamp maxlen to the amount of free space available for the actual
  2309. * extent allocation.
  2310. */
  2311. if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
  2312. args->maxlen = available;
  2313. ASSERT(args->maxlen > 0);
  2314. ASSERT(args->maxlen >= args->minlen);
  2315. }
  2316. return true;
  2317. }
  2318. /*
  2319. * Check the agfl fields of the agf for inconsistency or corruption.
  2320. *
  2321. * The original purpose was to detect an agfl header padding mismatch between
  2322. * current and early v5 kernels. This problem manifests as a 1-slot size
  2323. * difference between the on-disk flcount and the active [first, last] range of
  2324. * a wrapped agfl.
  2325. *
  2326. * However, we need to use these same checks to catch agfl count corruptions
  2327. * unrelated to padding. This could occur on any v4 or v5 filesystem, so either
  2328. * way, we need to reset the agfl and warn the user.
  2329. *
  2330. * Return true if a reset is required before the agfl can be used, false
  2331. * otherwise.
  2332. */
  2333. static bool
  2334. xfs_agfl_needs_reset(
  2335. struct xfs_mount *mp,
  2336. struct xfs_agf *agf)
  2337. {
  2338. uint32_t f = be32_to_cpu(agf->agf_flfirst);
  2339. uint32_t l = be32_to_cpu(agf->agf_fllast);
  2340. uint32_t c = be32_to_cpu(agf->agf_flcount);
  2341. int agfl_size = xfs_agfl_size(mp);
  2342. int active;
  2343. /*
  2344. * The agf read verifier catches severe corruption of these fields.
  2345. * Repeat some sanity checks to cover a packed -> unpacked mismatch if
  2346. * the verifier allows it.
  2347. */
  2348. if (f >= agfl_size || l >= agfl_size)
  2349. return true;
  2350. if (c > agfl_size)
  2351. return true;
  2352. /*
  2353. * Check consistency between the on-disk count and the active range. An
  2354. * agfl padding mismatch manifests as an inconsistent flcount.
  2355. */
  2356. if (c && l >= f)
  2357. active = l - f + 1;
  2358. else if (c)
  2359. active = agfl_size - f + l + 1;
  2360. else
  2361. active = 0;
  2362. return active != c;
  2363. }
  2364. /*
  2365. * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
  2366. * agfl content cannot be trusted. Warn the user that a repair is required to
  2367. * recover leaked blocks.
  2368. *
  2369. * The purpose of this mechanism is to handle filesystems affected by the agfl
  2370. * header padding mismatch problem. A reset keeps the filesystem online with a
  2371. * relatively minor free space accounting inconsistency rather than suffer the
  2372. * inevitable crash from use of an invalid agfl block.
  2373. */
  2374. static void
  2375. xfs_agfl_reset(
  2376. struct xfs_trans *tp,
  2377. struct xfs_buf *agbp,
  2378. struct xfs_perag *pag)
  2379. {
  2380. struct xfs_mount *mp = tp->t_mountp;
  2381. struct xfs_agf *agf = agbp->b_addr;
  2382. ASSERT(xfs_perag_agfl_needs_reset(pag));
  2383. trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_);
  2384. xfs_warn(mp,
  2385. "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
  2386. "Please unmount and run xfs_repair.",
  2387. pag->pag_agno, pag->pagf_flcount);
  2388. agf->agf_flfirst = 0;
  2389. agf->agf_fllast = cpu_to_be32(xfs_agfl_size(mp) - 1);
  2390. agf->agf_flcount = 0;
  2391. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLLAST |
  2392. XFS_AGF_FLCOUNT);
  2393. pag->pagf_flcount = 0;
  2394. clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
  2395. }
  2396. /*
  2397. * Add the extent to the list of extents to be free at transaction end.
  2398. * The list is maintained sorted (by block number).
  2399. */
  2400. static int
  2401. xfs_defer_extent_free(
  2402. struct xfs_trans *tp,
  2403. xfs_fsblock_t bno,
  2404. xfs_filblks_t len,
  2405. const struct xfs_owner_info *oinfo,
  2406. enum xfs_ag_resv_type type,
  2407. unsigned int free_flags,
  2408. struct xfs_defer_pending **dfpp)
  2409. {
  2410. struct xfs_extent_free_item *xefi;
  2411. struct xfs_mount *mp = tp->t_mountp;
  2412. ASSERT(len <= XFS_MAX_BMBT_EXTLEN);
  2413. ASSERT(!isnullstartblock(bno));
  2414. ASSERT(!(free_flags & ~XFS_FREE_EXTENT_ALL_FLAGS));
  2415. if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbext(mp, bno, len)))
  2416. return -EFSCORRUPTED;
  2417. xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
  2418. GFP_KERNEL | __GFP_NOFAIL);
  2419. xefi->xefi_startblock = bno;
  2420. xefi->xefi_blockcount = (xfs_extlen_t)len;
  2421. xefi->xefi_agresv = type;
  2422. if (free_flags & XFS_FREE_EXTENT_SKIP_DISCARD)
  2423. xefi->xefi_flags |= XFS_EFI_SKIP_DISCARD;
  2424. if (oinfo) {
  2425. ASSERT(oinfo->oi_offset == 0);
  2426. if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
  2427. xefi->xefi_flags |= XFS_EFI_ATTR_FORK;
  2428. if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
  2429. xefi->xefi_flags |= XFS_EFI_BMBT_BLOCK;
  2430. xefi->xefi_owner = oinfo->oi_owner;
  2431. } else {
  2432. xefi->xefi_owner = XFS_RMAP_OWN_NULL;
  2433. }
  2434. xfs_extent_free_defer_add(tp, xefi, dfpp);
  2435. return 0;
  2436. }
  2437. int
  2438. xfs_free_extent_later(
  2439. struct xfs_trans *tp,
  2440. xfs_fsblock_t bno,
  2441. xfs_filblks_t len,
  2442. const struct xfs_owner_info *oinfo,
  2443. enum xfs_ag_resv_type type,
  2444. unsigned int free_flags)
  2445. {
  2446. struct xfs_defer_pending *dontcare = NULL;
  2447. return xfs_defer_extent_free(tp, bno, len, oinfo, type, free_flags,
  2448. &dontcare);
  2449. }
  2450. /*
  2451. * Set up automatic freeing of unwritten space in the filesystem.
  2452. *
  2453. * This function attached a paused deferred extent free item to the
  2454. * transaction. Pausing means that the EFI will be logged in the next
  2455. * transaction commit, but the pending EFI will not be finished until the
  2456. * pending item is unpaused.
  2457. *
  2458. * If the system goes down after the EFI has been persisted to the log but
  2459. * before the pending item is unpaused, log recovery will find the EFI, fail to
  2460. * find the EFD, and free the space.
  2461. *
  2462. * If the pending item is unpaused, the next transaction commit will log an EFD
  2463. * without freeing the space.
  2464. *
  2465. * Caller must ensure that the tp, fsbno, len, oinfo, and resv flags of the
  2466. * @args structure are set to the relevant values.
  2467. */
  2468. int
  2469. xfs_alloc_schedule_autoreap(
  2470. const struct xfs_alloc_arg *args,
  2471. unsigned int free_flags,
  2472. struct xfs_alloc_autoreap *aarp)
  2473. {
  2474. int error;
  2475. error = xfs_defer_extent_free(args->tp, args->fsbno, args->len,
  2476. &args->oinfo, args->resv, free_flags, &aarp->dfp);
  2477. if (error)
  2478. return error;
  2479. xfs_defer_item_pause(args->tp, aarp->dfp);
  2480. return 0;
  2481. }
  2482. /*
  2483. * Cancel automatic freeing of unwritten space in the filesystem.
  2484. *
  2485. * Earlier, we created a paused deferred extent free item and attached it to
  2486. * this transaction so that we could automatically roll back a new space
  2487. * allocation if the system went down. Now we want to cancel the paused work
  2488. * item by marking the EFI stale so we don't actually free the space, unpausing
  2489. * the pending item and logging an EFD.
  2490. *
  2491. * The caller generally should have already mapped the space into the ondisk
  2492. * filesystem. If the reserved space was partially used, the caller must call
  2493. * xfs_free_extent_later to create a new EFI to free the unused space.
  2494. */
  2495. void
  2496. xfs_alloc_cancel_autoreap(
  2497. struct xfs_trans *tp,
  2498. struct xfs_alloc_autoreap *aarp)
  2499. {
  2500. struct xfs_defer_pending *dfp = aarp->dfp;
  2501. struct xfs_extent_free_item *xefi;
  2502. if (!dfp)
  2503. return;
  2504. list_for_each_entry(xefi, &dfp->dfp_work, xefi_list)
  2505. xefi->xefi_flags |= XFS_EFI_CANCELLED;
  2506. xfs_defer_item_unpause(tp, dfp);
  2507. }
  2508. /*
  2509. * Commit automatic freeing of unwritten space in the filesystem.
  2510. *
  2511. * This unpauses an earlier _schedule_autoreap and commits to freeing the
  2512. * allocated space. Call this if none of the reserved space was used.
  2513. */
  2514. void
  2515. xfs_alloc_commit_autoreap(
  2516. struct xfs_trans *tp,
  2517. struct xfs_alloc_autoreap *aarp)
  2518. {
  2519. if (aarp->dfp)
  2520. xfs_defer_item_unpause(tp, aarp->dfp);
  2521. }
  2522. /*
  2523. * Check if an AGF has a free extent record whose length is equal to
  2524. * args->minlen.
  2525. */
  2526. STATIC int
  2527. xfs_exact_minlen_extent_available(
  2528. struct xfs_alloc_arg *args,
  2529. struct xfs_buf *agbp,
  2530. int *stat)
  2531. {
  2532. struct xfs_btree_cur *cnt_cur;
  2533. xfs_agblock_t fbno;
  2534. xfs_extlen_t flen;
  2535. int error = 0;
  2536. cnt_cur = xfs_cntbt_init_cursor(args->mp, args->tp, agbp,
  2537. args->pag);
  2538. error = xfs_alloc_lookup_ge(cnt_cur, 0, args->minlen, stat);
  2539. if (error)
  2540. goto out;
  2541. if (*stat == 0) {
  2542. xfs_btree_mark_sick(cnt_cur);
  2543. error = -EFSCORRUPTED;
  2544. goto out;
  2545. }
  2546. error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, stat);
  2547. if (error)
  2548. goto out;
  2549. if (*stat == 1 && flen != args->minlen)
  2550. *stat = 0;
  2551. out:
  2552. xfs_btree_del_cursor(cnt_cur, error);
  2553. return error;
  2554. }
  2555. /*
  2556. * Decide whether to use this allocation group for this allocation.
  2557. * If so, fix up the btree freelist's size.
  2558. */
  2559. int /* error */
  2560. xfs_alloc_fix_freelist(
  2561. struct xfs_alloc_arg *args, /* allocation argument structure */
  2562. uint32_t alloc_flags)
  2563. {
  2564. struct xfs_mount *mp = args->mp;
  2565. struct xfs_perag *pag = args->pag;
  2566. struct xfs_trans *tp = args->tp;
  2567. struct xfs_buf *agbp = NULL;
  2568. struct xfs_buf *agflbp = NULL;
  2569. struct xfs_alloc_arg targs; /* local allocation arguments */
  2570. xfs_agblock_t bno; /* freelist block */
  2571. xfs_extlen_t need; /* total blocks needed in freelist */
  2572. int error = 0;
  2573. /* deferred ops (AGFL block frees) require permanent transactions */
  2574. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  2575. if (!xfs_perag_initialised_agf(pag)) {
  2576. error = xfs_alloc_read_agf(pag, tp, alloc_flags, &agbp);
  2577. if (error) {
  2578. /* Couldn't lock the AGF so skip this AG. */
  2579. if (error == -EAGAIN)
  2580. error = 0;
  2581. goto out_no_agbp;
  2582. }
  2583. }
  2584. /*
  2585. * If this is a metadata preferred pag and we are user data then try
  2586. * somewhere else if we are not being asked to try harder at this
  2587. * point
  2588. */
  2589. if (xfs_perag_prefers_metadata(pag) &&
  2590. (args->datatype & XFS_ALLOC_USERDATA) &&
  2591. (alloc_flags & XFS_ALLOC_FLAG_TRYLOCK)) {
  2592. ASSERT(!(alloc_flags & XFS_ALLOC_FLAG_FREEING));
  2593. goto out_agbp_relse;
  2594. }
  2595. need = xfs_alloc_min_freelist(mp, pag);
  2596. if (!xfs_alloc_space_available(args, need, alloc_flags |
  2597. XFS_ALLOC_FLAG_CHECK))
  2598. goto out_agbp_relse;
  2599. /*
  2600. * Get the a.g. freespace buffer.
  2601. * Can fail if we're not blocking on locks, and it's held.
  2602. */
  2603. if (!agbp) {
  2604. error = xfs_alloc_read_agf(pag, tp, alloc_flags, &agbp);
  2605. if (error) {
  2606. /* Couldn't lock the AGF so skip this AG. */
  2607. if (error == -EAGAIN)
  2608. error = 0;
  2609. goto out_no_agbp;
  2610. }
  2611. }
  2612. /* reset a padding mismatched agfl before final free space check */
  2613. if (xfs_perag_agfl_needs_reset(pag))
  2614. xfs_agfl_reset(tp, agbp, pag);
  2615. /* If there isn't enough total space or single-extent, reject it. */
  2616. need = xfs_alloc_min_freelist(mp, pag);
  2617. if (!xfs_alloc_space_available(args, need, alloc_flags))
  2618. goto out_agbp_relse;
  2619. if (IS_ENABLED(CONFIG_XFS_DEBUG) && args->alloc_minlen_only) {
  2620. int stat;
  2621. error = xfs_exact_minlen_extent_available(args, agbp, &stat);
  2622. if (error || !stat)
  2623. goto out_agbp_relse;
  2624. }
  2625. /*
  2626. * Make the freelist shorter if it's too long.
  2627. *
  2628. * Note that from this point onwards, we will always release the agf and
  2629. * agfl buffers on error. This handles the case where we error out and
  2630. * the buffers are clean or may not have been joined to the transaction
  2631. * and hence need to be released manually. If they have been joined to
  2632. * the transaction, then xfs_trans_brelse() will handle them
  2633. * appropriately based on the recursion count and dirty state of the
  2634. * buffer.
  2635. *
  2636. * XXX (dgc): When we have lots of free space, does this buy us
  2637. * anything other than extra overhead when we need to put more blocks
  2638. * back on the free list? Maybe we should only do this when space is
  2639. * getting low or the AGFL is more than half full?
  2640. *
  2641. * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
  2642. * big; the NORMAP flag prevents AGFL expand/shrink operations from
  2643. * updating the rmapbt. Both flags are used in xfs_repair while we're
  2644. * rebuilding the rmapbt, and neither are used by the kernel. They're
  2645. * both required to ensure that rmaps are correctly recorded for the
  2646. * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
  2647. * repair/rmap.c in xfsprogs for details.
  2648. */
  2649. memset(&targs, 0, sizeof(targs));
  2650. /* struct copy below */
  2651. if (alloc_flags & XFS_ALLOC_FLAG_NORMAP)
  2652. targs.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
  2653. else
  2654. targs.oinfo = XFS_RMAP_OINFO_AG;
  2655. while (!(alloc_flags & XFS_ALLOC_FLAG_NOSHRINK) &&
  2656. pag->pagf_flcount > need) {
  2657. error = xfs_alloc_get_freelist(pag, tp, agbp, &bno, 0);
  2658. if (error)
  2659. goto out_agbp_relse;
  2660. /*
  2661. * Defer the AGFL block free.
  2662. *
  2663. * This helps to prevent log reservation overruns due to too
  2664. * many allocation operations in a transaction. AGFL frees are
  2665. * prone to this problem because for one they are always freed
  2666. * one at a time. Further, an immediate AGFL block free can
  2667. * cause a btree join and require another block free before the
  2668. * real allocation can proceed.
  2669. * Deferring the free disconnects freeing up the AGFL slot from
  2670. * freeing the block.
  2671. */
  2672. error = xfs_free_extent_later(tp,
  2673. XFS_AGB_TO_FSB(mp, args->agno, bno), 1,
  2674. &targs.oinfo, XFS_AG_RESV_AGFL, 0);
  2675. if (error)
  2676. goto out_agbp_relse;
  2677. }
  2678. targs.tp = tp;
  2679. targs.mp = mp;
  2680. targs.agbp = agbp;
  2681. targs.agno = args->agno;
  2682. targs.alignment = targs.minlen = targs.prod = 1;
  2683. targs.pag = pag;
  2684. error = xfs_alloc_read_agfl(pag, tp, &agflbp);
  2685. if (error)
  2686. goto out_agbp_relse;
  2687. /* Make the freelist longer if it's too short. */
  2688. while (pag->pagf_flcount < need) {
  2689. targs.agbno = 0;
  2690. targs.maxlen = need - pag->pagf_flcount;
  2691. targs.resv = XFS_AG_RESV_AGFL;
  2692. /* Allocate as many blocks as possible at once. */
  2693. error = xfs_alloc_ag_vextent_size(&targs, alloc_flags);
  2694. if (error)
  2695. goto out_agflbp_relse;
  2696. /*
  2697. * Stop if we run out. Won't happen if callers are obeying
  2698. * the restrictions correctly. Can happen for free calls
  2699. * on a completely full ag.
  2700. */
  2701. if (targs.agbno == NULLAGBLOCK) {
  2702. if (alloc_flags & XFS_ALLOC_FLAG_FREEING)
  2703. break;
  2704. goto out_agflbp_relse;
  2705. }
  2706. if (!xfs_rmap_should_skip_owner_update(&targs.oinfo)) {
  2707. error = xfs_rmap_alloc(tp, agbp, pag,
  2708. targs.agbno, targs.len, &targs.oinfo);
  2709. if (error)
  2710. goto out_agflbp_relse;
  2711. }
  2712. error = xfs_alloc_update_counters(tp, agbp,
  2713. -((long)(targs.len)));
  2714. if (error)
  2715. goto out_agflbp_relse;
  2716. /*
  2717. * Put each allocated block on the list.
  2718. */
  2719. for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
  2720. error = xfs_alloc_put_freelist(pag, tp, agbp,
  2721. agflbp, bno, 0);
  2722. if (error)
  2723. goto out_agflbp_relse;
  2724. }
  2725. }
  2726. xfs_trans_brelse(tp, agflbp);
  2727. args->agbp = agbp;
  2728. return 0;
  2729. out_agflbp_relse:
  2730. xfs_trans_brelse(tp, agflbp);
  2731. out_agbp_relse:
  2732. if (agbp)
  2733. xfs_trans_brelse(tp, agbp);
  2734. out_no_agbp:
  2735. args->agbp = NULL;
  2736. return error;
  2737. }
  2738. /*
  2739. * Get a block from the freelist.
  2740. * Returns with the buffer for the block gotten.
  2741. */
  2742. int
  2743. xfs_alloc_get_freelist(
  2744. struct xfs_perag *pag,
  2745. struct xfs_trans *tp,
  2746. struct xfs_buf *agbp,
  2747. xfs_agblock_t *bnop,
  2748. int btreeblk)
  2749. {
  2750. struct xfs_agf *agf = agbp->b_addr;
  2751. struct xfs_buf *agflbp;
  2752. xfs_agblock_t bno;
  2753. __be32 *agfl_bno;
  2754. int error;
  2755. uint32_t logflags;
  2756. struct xfs_mount *mp = tp->t_mountp;
  2757. /*
  2758. * Freelist is empty, give up.
  2759. */
  2760. if (!agf->agf_flcount) {
  2761. *bnop = NULLAGBLOCK;
  2762. return 0;
  2763. }
  2764. /*
  2765. * Read the array of free blocks.
  2766. */
  2767. error = xfs_alloc_read_agfl(pag, tp, &agflbp);
  2768. if (error)
  2769. return error;
  2770. /*
  2771. * Get the block number and update the data structures.
  2772. */
  2773. agfl_bno = xfs_buf_to_agfl_bno(agflbp);
  2774. bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
  2775. if (XFS_IS_CORRUPT(tp->t_mountp, !xfs_verify_agbno(pag, bno)))
  2776. return -EFSCORRUPTED;
  2777. be32_add_cpu(&agf->agf_flfirst, 1);
  2778. xfs_trans_brelse(tp, agflbp);
  2779. if (be32_to_cpu(agf->agf_flfirst) == xfs_agfl_size(mp))
  2780. agf->agf_flfirst = 0;
  2781. ASSERT(!xfs_perag_agfl_needs_reset(pag));
  2782. be32_add_cpu(&agf->agf_flcount, -1);
  2783. pag->pagf_flcount--;
  2784. logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
  2785. if (btreeblk) {
  2786. be32_add_cpu(&agf->agf_btreeblks, 1);
  2787. pag->pagf_btreeblks++;
  2788. logflags |= XFS_AGF_BTREEBLKS;
  2789. }
  2790. xfs_alloc_log_agf(tp, agbp, logflags);
  2791. *bnop = bno;
  2792. return 0;
  2793. }
  2794. /*
  2795. * Log the given fields from the agf structure.
  2796. */
  2797. void
  2798. xfs_alloc_log_agf(
  2799. struct xfs_trans *tp,
  2800. struct xfs_buf *bp,
  2801. uint32_t fields)
  2802. {
  2803. int first; /* first byte offset */
  2804. int last; /* last byte offset */
  2805. static const short offsets[] = {
  2806. offsetof(xfs_agf_t, agf_magicnum),
  2807. offsetof(xfs_agf_t, agf_versionnum),
  2808. offsetof(xfs_agf_t, agf_seqno),
  2809. offsetof(xfs_agf_t, agf_length),
  2810. offsetof(xfs_agf_t, agf_bno_root), /* also cnt/rmap root */
  2811. offsetof(xfs_agf_t, agf_bno_level), /* also cnt/rmap levels */
  2812. offsetof(xfs_agf_t, agf_flfirst),
  2813. offsetof(xfs_agf_t, agf_fllast),
  2814. offsetof(xfs_agf_t, agf_flcount),
  2815. offsetof(xfs_agf_t, agf_freeblks),
  2816. offsetof(xfs_agf_t, agf_longest),
  2817. offsetof(xfs_agf_t, agf_btreeblks),
  2818. offsetof(xfs_agf_t, agf_uuid),
  2819. offsetof(xfs_agf_t, agf_rmap_blocks),
  2820. offsetof(xfs_agf_t, agf_refcount_blocks),
  2821. offsetof(xfs_agf_t, agf_refcount_root),
  2822. offsetof(xfs_agf_t, agf_refcount_level),
  2823. /* needed so that we don't log the whole rest of the structure: */
  2824. offsetof(xfs_agf_t, agf_spare64),
  2825. sizeof(xfs_agf_t)
  2826. };
  2827. trace_xfs_agf(tp->t_mountp, bp->b_addr, fields, _RET_IP_);
  2828. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
  2829. xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
  2830. xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
  2831. }
  2832. /*
  2833. * Put the block on the freelist for the allocation group.
  2834. */
  2835. int
  2836. xfs_alloc_put_freelist(
  2837. struct xfs_perag *pag,
  2838. struct xfs_trans *tp,
  2839. struct xfs_buf *agbp,
  2840. struct xfs_buf *agflbp,
  2841. xfs_agblock_t bno,
  2842. int btreeblk)
  2843. {
  2844. struct xfs_mount *mp = tp->t_mountp;
  2845. struct xfs_agf *agf = agbp->b_addr;
  2846. __be32 *blockp;
  2847. int error;
  2848. uint32_t logflags;
  2849. __be32 *agfl_bno;
  2850. int startoff;
  2851. if (!agflbp) {
  2852. error = xfs_alloc_read_agfl(pag, tp, &agflbp);
  2853. if (error)
  2854. return error;
  2855. }
  2856. be32_add_cpu(&agf->agf_fllast, 1);
  2857. if (be32_to_cpu(agf->agf_fllast) == xfs_agfl_size(mp))
  2858. agf->agf_fllast = 0;
  2859. ASSERT(!xfs_perag_agfl_needs_reset(pag));
  2860. be32_add_cpu(&agf->agf_flcount, 1);
  2861. pag->pagf_flcount++;
  2862. logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
  2863. if (btreeblk) {
  2864. be32_add_cpu(&agf->agf_btreeblks, -1);
  2865. pag->pagf_btreeblks--;
  2866. logflags |= XFS_AGF_BTREEBLKS;
  2867. }
  2868. xfs_alloc_log_agf(tp, agbp, logflags);
  2869. ASSERT(be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp));
  2870. agfl_bno = xfs_buf_to_agfl_bno(agflbp);
  2871. blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
  2872. *blockp = cpu_to_be32(bno);
  2873. startoff = (char *)blockp - (char *)agflbp->b_addr;
  2874. xfs_alloc_log_agf(tp, agbp, logflags);
  2875. xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
  2876. xfs_trans_log_buf(tp, agflbp, startoff,
  2877. startoff + sizeof(xfs_agblock_t) - 1);
  2878. return 0;
  2879. }
  2880. /*
  2881. * Check that this AGF/AGI header's sequence number and length matches the AG
  2882. * number and size in fsblocks.
  2883. */
  2884. xfs_failaddr_t
  2885. xfs_validate_ag_length(
  2886. struct xfs_buf *bp,
  2887. uint32_t seqno,
  2888. uint32_t length)
  2889. {
  2890. struct xfs_mount *mp = bp->b_mount;
  2891. /*
  2892. * During growfs operations, the perag is not fully initialised,
  2893. * so we can't use it for any useful checking. growfs ensures we can't
  2894. * use it by using uncached buffers that don't have the perag attached
  2895. * so we can detect and avoid this problem.
  2896. */
  2897. if (bp->b_pag && seqno != bp->b_pag->pag_agno)
  2898. return __this_address;
  2899. /*
  2900. * Only the last AG in the filesystem is allowed to be shorter
  2901. * than the AG size recorded in the superblock.
  2902. */
  2903. if (length != mp->m_sb.sb_agblocks) {
  2904. /*
  2905. * During growfs, the new last AG can get here before we
  2906. * have updated the superblock. Give it a pass on the seqno
  2907. * check.
  2908. */
  2909. if (bp->b_pag && seqno != mp->m_sb.sb_agcount - 1)
  2910. return __this_address;
  2911. if (length < XFS_MIN_AG_BLOCKS)
  2912. return __this_address;
  2913. if (length > mp->m_sb.sb_agblocks)
  2914. return __this_address;
  2915. }
  2916. return NULL;
  2917. }
  2918. /*
  2919. * Verify the AGF is consistent.
  2920. *
  2921. * We do not verify the AGFL indexes in the AGF are fully consistent here
  2922. * because of issues with variable on-disk structure sizes. Instead, we check
  2923. * the agfl indexes for consistency when we initialise the perag from the AGF
  2924. * information after a read completes.
  2925. *
  2926. * If the index is inconsistent, then we mark the perag as needing an AGFL
  2927. * reset. The first AGFL update performed then resets the AGFL indexes and
  2928. * refills the AGFL with known good free blocks, allowing the filesystem to
  2929. * continue operating normally at the cost of a few leaked free space blocks.
  2930. */
  2931. static xfs_failaddr_t
  2932. xfs_agf_verify(
  2933. struct xfs_buf *bp)
  2934. {
  2935. struct xfs_mount *mp = bp->b_mount;
  2936. struct xfs_agf *agf = bp->b_addr;
  2937. xfs_failaddr_t fa;
  2938. uint32_t agf_seqno = be32_to_cpu(agf->agf_seqno);
  2939. uint32_t agf_length = be32_to_cpu(agf->agf_length);
  2940. if (xfs_has_crc(mp)) {
  2941. if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
  2942. return __this_address;
  2943. if (!xfs_log_check_lsn(mp, be64_to_cpu(agf->agf_lsn)))
  2944. return __this_address;
  2945. }
  2946. if (!xfs_verify_magic(bp, agf->agf_magicnum))
  2947. return __this_address;
  2948. if (!XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)))
  2949. return __this_address;
  2950. /*
  2951. * Both agf_seqno and agf_length need to validated before anything else
  2952. * block number related in the AGF or AGFL can be checked.
  2953. */
  2954. fa = xfs_validate_ag_length(bp, agf_seqno, agf_length);
  2955. if (fa)
  2956. return fa;
  2957. if (be32_to_cpu(agf->agf_flfirst) >= xfs_agfl_size(mp))
  2958. return __this_address;
  2959. if (be32_to_cpu(agf->agf_fllast) >= xfs_agfl_size(mp))
  2960. return __this_address;
  2961. if (be32_to_cpu(agf->agf_flcount) > xfs_agfl_size(mp))
  2962. return __this_address;
  2963. if (be32_to_cpu(agf->agf_freeblks) < be32_to_cpu(agf->agf_longest) ||
  2964. be32_to_cpu(agf->agf_freeblks) > agf_length)
  2965. return __this_address;
  2966. if (be32_to_cpu(agf->agf_bno_level) < 1 ||
  2967. be32_to_cpu(agf->agf_cnt_level) < 1 ||
  2968. be32_to_cpu(agf->agf_bno_level) > mp->m_alloc_maxlevels ||
  2969. be32_to_cpu(agf->agf_cnt_level) > mp->m_alloc_maxlevels)
  2970. return __this_address;
  2971. if (xfs_has_lazysbcount(mp) &&
  2972. be32_to_cpu(agf->agf_btreeblks) > agf_length)
  2973. return __this_address;
  2974. if (xfs_has_rmapbt(mp)) {
  2975. if (be32_to_cpu(agf->agf_rmap_blocks) > agf_length)
  2976. return __this_address;
  2977. if (be32_to_cpu(agf->agf_rmap_level) < 1 ||
  2978. be32_to_cpu(agf->agf_rmap_level) > mp->m_rmap_maxlevels)
  2979. return __this_address;
  2980. }
  2981. if (xfs_has_reflink(mp)) {
  2982. if (be32_to_cpu(agf->agf_refcount_blocks) > agf_length)
  2983. return __this_address;
  2984. if (be32_to_cpu(agf->agf_refcount_level) < 1 ||
  2985. be32_to_cpu(agf->agf_refcount_level) > mp->m_refc_maxlevels)
  2986. return __this_address;
  2987. }
  2988. return NULL;
  2989. }
  2990. static void
  2991. xfs_agf_read_verify(
  2992. struct xfs_buf *bp)
  2993. {
  2994. struct xfs_mount *mp = bp->b_mount;
  2995. xfs_failaddr_t fa;
  2996. if (xfs_has_crc(mp) &&
  2997. !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
  2998. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  2999. else {
  3000. fa = xfs_agf_verify(bp);
  3001. if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_ALLOC_READ_AGF))
  3002. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  3003. }
  3004. }
  3005. static void
  3006. xfs_agf_write_verify(
  3007. struct xfs_buf *bp)
  3008. {
  3009. struct xfs_mount *mp = bp->b_mount;
  3010. struct xfs_buf_log_item *bip = bp->b_log_item;
  3011. struct xfs_agf *agf = bp->b_addr;
  3012. xfs_failaddr_t fa;
  3013. fa = xfs_agf_verify(bp);
  3014. if (fa) {
  3015. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  3016. return;
  3017. }
  3018. if (!xfs_has_crc(mp))
  3019. return;
  3020. if (bip)
  3021. agf->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  3022. xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
  3023. }
  3024. const struct xfs_buf_ops xfs_agf_buf_ops = {
  3025. .name = "xfs_agf",
  3026. .magic = { cpu_to_be32(XFS_AGF_MAGIC), cpu_to_be32(XFS_AGF_MAGIC) },
  3027. .verify_read = xfs_agf_read_verify,
  3028. .verify_write = xfs_agf_write_verify,
  3029. .verify_struct = xfs_agf_verify,
  3030. };
  3031. /*
  3032. * Read in the allocation group header (free/alloc section).
  3033. */
  3034. int
  3035. xfs_read_agf(
  3036. struct xfs_perag *pag,
  3037. struct xfs_trans *tp,
  3038. int flags,
  3039. struct xfs_buf **agfbpp)
  3040. {
  3041. struct xfs_mount *mp = pag->pag_mount;
  3042. int error;
  3043. trace_xfs_read_agf(pag->pag_mount, pag->pag_agno);
  3044. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  3045. XFS_AG_DADDR(mp, pag->pag_agno, XFS_AGF_DADDR(mp)),
  3046. XFS_FSS_TO_BB(mp, 1), flags, agfbpp, &xfs_agf_buf_ops);
  3047. if (xfs_metadata_is_sick(error))
  3048. xfs_ag_mark_sick(pag, XFS_SICK_AG_AGF);
  3049. if (error)
  3050. return error;
  3051. xfs_buf_set_ref(*agfbpp, XFS_AGF_REF);
  3052. return 0;
  3053. }
  3054. /*
  3055. * Read in the allocation group header (free/alloc section) and initialise the
  3056. * perag structure if necessary. If the caller provides @agfbpp, then return the
  3057. * locked buffer to the caller, otherwise free it.
  3058. */
  3059. int
  3060. xfs_alloc_read_agf(
  3061. struct xfs_perag *pag,
  3062. struct xfs_trans *tp,
  3063. int flags,
  3064. struct xfs_buf **agfbpp)
  3065. {
  3066. struct xfs_buf *agfbp;
  3067. struct xfs_agf *agf;
  3068. int error;
  3069. int allocbt_blks;
  3070. trace_xfs_alloc_read_agf(pag->pag_mount, pag->pag_agno);
  3071. /* We don't support trylock when freeing. */
  3072. ASSERT((flags & (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK)) !=
  3073. (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK));
  3074. error = xfs_read_agf(pag, tp,
  3075. (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
  3076. &agfbp);
  3077. if (error)
  3078. return error;
  3079. agf = agfbp->b_addr;
  3080. if (!xfs_perag_initialised_agf(pag)) {
  3081. pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
  3082. pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
  3083. pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
  3084. pag->pagf_longest = be32_to_cpu(agf->agf_longest);
  3085. pag->pagf_bno_level = be32_to_cpu(agf->agf_bno_level);
  3086. pag->pagf_cnt_level = be32_to_cpu(agf->agf_cnt_level);
  3087. pag->pagf_rmap_level = be32_to_cpu(agf->agf_rmap_level);
  3088. pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
  3089. if (xfs_agfl_needs_reset(pag->pag_mount, agf))
  3090. set_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
  3091. else
  3092. clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
  3093. /*
  3094. * Update the in-core allocbt counter. Filter out the rmapbt
  3095. * subset of the btreeblks counter because the rmapbt is managed
  3096. * by perag reservation. Subtract one for the rmapbt root block
  3097. * because the rmap counter includes it while the btreeblks
  3098. * counter only tracks non-root blocks.
  3099. */
  3100. allocbt_blks = pag->pagf_btreeblks;
  3101. if (xfs_has_rmapbt(pag->pag_mount))
  3102. allocbt_blks -= be32_to_cpu(agf->agf_rmap_blocks) - 1;
  3103. if (allocbt_blks > 0)
  3104. atomic64_add(allocbt_blks,
  3105. &pag->pag_mount->m_allocbt_blks);
  3106. set_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
  3107. }
  3108. #ifdef DEBUG
  3109. else if (!xfs_is_shutdown(pag->pag_mount)) {
  3110. ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
  3111. ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
  3112. ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
  3113. ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
  3114. ASSERT(pag->pagf_bno_level == be32_to_cpu(agf->agf_bno_level));
  3115. ASSERT(pag->pagf_cnt_level == be32_to_cpu(agf->agf_cnt_level));
  3116. }
  3117. #endif
  3118. if (agfbpp)
  3119. *agfbpp = agfbp;
  3120. else
  3121. xfs_trans_brelse(tp, agfbp);
  3122. return 0;
  3123. }
  3124. /*
  3125. * Pre-proces allocation arguments to set initial state that we don't require
  3126. * callers to set up correctly, as well as bounds check the allocation args
  3127. * that are set up.
  3128. */
  3129. static int
  3130. xfs_alloc_vextent_check_args(
  3131. struct xfs_alloc_arg *args,
  3132. xfs_fsblock_t target,
  3133. xfs_agnumber_t *minimum_agno)
  3134. {
  3135. struct xfs_mount *mp = args->mp;
  3136. xfs_agblock_t agsize;
  3137. args->fsbno = NULLFSBLOCK;
  3138. *minimum_agno = 0;
  3139. if (args->tp->t_highest_agno != NULLAGNUMBER)
  3140. *minimum_agno = args->tp->t_highest_agno;
  3141. /*
  3142. * Just fix this up, for the case where the last a.g. is shorter
  3143. * (or there's only one a.g.) and the caller couldn't easily figure
  3144. * that out (xfs_bmap_alloc).
  3145. */
  3146. agsize = mp->m_sb.sb_agblocks;
  3147. if (args->maxlen > agsize)
  3148. args->maxlen = agsize;
  3149. if (args->alignment == 0)
  3150. args->alignment = 1;
  3151. ASSERT(args->minlen > 0);
  3152. ASSERT(args->maxlen > 0);
  3153. ASSERT(args->alignment > 0);
  3154. ASSERT(args->resv != XFS_AG_RESV_AGFL);
  3155. ASSERT(XFS_FSB_TO_AGNO(mp, target) < mp->m_sb.sb_agcount);
  3156. ASSERT(XFS_FSB_TO_AGBNO(mp, target) < agsize);
  3157. ASSERT(args->minlen <= args->maxlen);
  3158. ASSERT(args->minlen <= agsize);
  3159. ASSERT(args->mod < args->prod);
  3160. if (XFS_FSB_TO_AGNO(mp, target) >= mp->m_sb.sb_agcount ||
  3161. XFS_FSB_TO_AGBNO(mp, target) >= agsize ||
  3162. args->minlen > args->maxlen || args->minlen > agsize ||
  3163. args->mod >= args->prod) {
  3164. trace_xfs_alloc_vextent_badargs(args);
  3165. return -ENOSPC;
  3166. }
  3167. if (args->agno != NULLAGNUMBER && *minimum_agno > args->agno) {
  3168. trace_xfs_alloc_vextent_skip_deadlock(args);
  3169. return -ENOSPC;
  3170. }
  3171. return 0;
  3172. }
  3173. /*
  3174. * Prepare an AG for allocation. If the AG is not prepared to accept the
  3175. * allocation, return failure.
  3176. *
  3177. * XXX(dgc): The complexity of "need_pag" will go away as all caller paths are
  3178. * modified to hold their own perag references.
  3179. */
  3180. static int
  3181. xfs_alloc_vextent_prepare_ag(
  3182. struct xfs_alloc_arg *args,
  3183. uint32_t alloc_flags)
  3184. {
  3185. bool need_pag = !args->pag;
  3186. int error;
  3187. if (need_pag)
  3188. args->pag = xfs_perag_get(args->mp, args->agno);
  3189. args->agbp = NULL;
  3190. error = xfs_alloc_fix_freelist(args, alloc_flags);
  3191. if (error) {
  3192. trace_xfs_alloc_vextent_nofix(args);
  3193. if (need_pag)
  3194. xfs_perag_put(args->pag);
  3195. args->agbno = NULLAGBLOCK;
  3196. return error;
  3197. }
  3198. if (!args->agbp) {
  3199. /* cannot allocate in this AG at all */
  3200. trace_xfs_alloc_vextent_noagbp(args);
  3201. args->agbno = NULLAGBLOCK;
  3202. return 0;
  3203. }
  3204. args->wasfromfl = 0;
  3205. return 0;
  3206. }
  3207. /*
  3208. * Post-process allocation results to account for the allocation if it succeed
  3209. * and set the allocated block number correctly for the caller.
  3210. *
  3211. * XXX: we should really be returning ENOSPC for ENOSPC, not
  3212. * hiding it behind a "successful" NULLFSBLOCK allocation.
  3213. */
  3214. static int
  3215. xfs_alloc_vextent_finish(
  3216. struct xfs_alloc_arg *args,
  3217. xfs_agnumber_t minimum_agno,
  3218. int alloc_error,
  3219. bool drop_perag)
  3220. {
  3221. struct xfs_mount *mp = args->mp;
  3222. int error = 0;
  3223. /*
  3224. * We can end up here with a locked AGF. If we failed, the caller is
  3225. * likely going to try to allocate again with different parameters, and
  3226. * that can widen the AGs that are searched for free space. If we have
  3227. * to do BMBT block allocation, we have to do a new allocation.
  3228. *
  3229. * Hence leaving this function with the AGF locked opens up potential
  3230. * ABBA AGF deadlocks because a future allocation attempt in this
  3231. * transaction may attempt to lock a lower number AGF.
  3232. *
  3233. * We can't release the AGF until the transaction is commited, so at
  3234. * this point we must update the "first allocation" tracker to point at
  3235. * this AG if the tracker is empty or points to a lower AG. This allows
  3236. * the next allocation attempt to be modified appropriately to avoid
  3237. * deadlocks.
  3238. */
  3239. if (args->agbp &&
  3240. (args->tp->t_highest_agno == NULLAGNUMBER ||
  3241. args->agno > minimum_agno))
  3242. args->tp->t_highest_agno = args->agno;
  3243. /*
  3244. * If the allocation failed with an error or we had an ENOSPC result,
  3245. * preserve the returned error whilst also marking the allocation result
  3246. * as "no extent allocated". This ensures that callers that fail to
  3247. * capture the error will still treat it as a failed allocation.
  3248. */
  3249. if (alloc_error || args->agbno == NULLAGBLOCK) {
  3250. args->fsbno = NULLFSBLOCK;
  3251. error = alloc_error;
  3252. goto out_drop_perag;
  3253. }
  3254. args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
  3255. ASSERT(args->len >= args->minlen);
  3256. ASSERT(args->len <= args->maxlen);
  3257. ASSERT(args->agbno % args->alignment == 0);
  3258. XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno), args->len);
  3259. /* if not file data, insert new block into the reverse map btree */
  3260. if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) {
  3261. error = xfs_rmap_alloc(args->tp, args->agbp, args->pag,
  3262. args->agbno, args->len, &args->oinfo);
  3263. if (error)
  3264. goto out_drop_perag;
  3265. }
  3266. if (!args->wasfromfl) {
  3267. error = xfs_alloc_update_counters(args->tp, args->agbp,
  3268. -((long)(args->len)));
  3269. if (error)
  3270. goto out_drop_perag;
  3271. ASSERT(!xfs_extent_busy_search(mp, args->pag, args->agbno,
  3272. args->len));
  3273. }
  3274. xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
  3275. XFS_STATS_INC(mp, xs_allocx);
  3276. XFS_STATS_ADD(mp, xs_allocb, args->len);
  3277. trace_xfs_alloc_vextent_finish(args);
  3278. out_drop_perag:
  3279. if (drop_perag && args->pag) {
  3280. xfs_perag_rele(args->pag);
  3281. args->pag = NULL;
  3282. }
  3283. return error;
  3284. }
  3285. /*
  3286. * Allocate within a single AG only. This uses a best-fit length algorithm so if
  3287. * you need an exact sized allocation without locality constraints, this is the
  3288. * fastest way to do it.
  3289. *
  3290. * Caller is expected to hold a perag reference in args->pag.
  3291. */
  3292. int
  3293. xfs_alloc_vextent_this_ag(
  3294. struct xfs_alloc_arg *args,
  3295. xfs_agnumber_t agno)
  3296. {
  3297. struct xfs_mount *mp = args->mp;
  3298. xfs_agnumber_t minimum_agno;
  3299. uint32_t alloc_flags = 0;
  3300. int error;
  3301. ASSERT(args->pag != NULL);
  3302. ASSERT(args->pag->pag_agno == agno);
  3303. args->agno = agno;
  3304. args->agbno = 0;
  3305. trace_xfs_alloc_vextent_this_ag(args);
  3306. error = xfs_alloc_vextent_check_args(args, XFS_AGB_TO_FSB(mp, agno, 0),
  3307. &minimum_agno);
  3308. if (error) {
  3309. if (error == -ENOSPC)
  3310. return 0;
  3311. return error;
  3312. }
  3313. error = xfs_alloc_vextent_prepare_ag(args, alloc_flags);
  3314. if (!error && args->agbp)
  3315. error = xfs_alloc_ag_vextent_size(args, alloc_flags);
  3316. return xfs_alloc_vextent_finish(args, minimum_agno, error, false);
  3317. }
  3318. /*
  3319. * Iterate all AGs trying to allocate an extent starting from @start_ag.
  3320. *
  3321. * If the incoming allocation type is XFS_ALLOCTYPE_NEAR_BNO, it means the
  3322. * allocation attempts in @start_agno have locality information. If we fail to
  3323. * allocate in that AG, then we revert to anywhere-in-AG for all the other AGs
  3324. * we attempt to allocation in as there is no locality optimisation possible for
  3325. * those allocations.
  3326. *
  3327. * On return, args->pag may be left referenced if we finish before the "all
  3328. * failed" return point. The allocation finish still needs the perag, and
  3329. * so the caller will release it once they've finished the allocation.
  3330. *
  3331. * When we wrap the AG iteration at the end of the filesystem, we have to be
  3332. * careful not to wrap into AGs below ones we already have locked in the
  3333. * transaction if we are doing a blocking iteration. This will result in an
  3334. * out-of-order locking of AGFs and hence can cause deadlocks.
  3335. */
  3336. static int
  3337. xfs_alloc_vextent_iterate_ags(
  3338. struct xfs_alloc_arg *args,
  3339. xfs_agnumber_t minimum_agno,
  3340. xfs_agnumber_t start_agno,
  3341. xfs_agblock_t target_agbno,
  3342. uint32_t alloc_flags)
  3343. {
  3344. struct xfs_mount *mp = args->mp;
  3345. xfs_agnumber_t restart_agno = minimum_agno;
  3346. xfs_agnumber_t agno;
  3347. int error = 0;
  3348. if (alloc_flags & XFS_ALLOC_FLAG_TRYLOCK)
  3349. restart_agno = 0;
  3350. restart:
  3351. for_each_perag_wrap_range(mp, start_agno, restart_agno,
  3352. mp->m_sb.sb_agcount, agno, args->pag) {
  3353. args->agno = agno;
  3354. error = xfs_alloc_vextent_prepare_ag(args, alloc_flags);
  3355. if (error)
  3356. break;
  3357. if (!args->agbp) {
  3358. trace_xfs_alloc_vextent_loopfailed(args);
  3359. continue;
  3360. }
  3361. /*
  3362. * Allocation is supposed to succeed now, so break out of the
  3363. * loop regardless of whether we succeed or not.
  3364. */
  3365. if (args->agno == start_agno && target_agbno) {
  3366. args->agbno = target_agbno;
  3367. error = xfs_alloc_ag_vextent_near(args, alloc_flags);
  3368. } else {
  3369. args->agbno = 0;
  3370. error = xfs_alloc_ag_vextent_size(args, alloc_flags);
  3371. }
  3372. break;
  3373. }
  3374. if (error) {
  3375. xfs_perag_rele(args->pag);
  3376. args->pag = NULL;
  3377. return error;
  3378. }
  3379. if (args->agbp)
  3380. return 0;
  3381. /*
  3382. * We didn't find an AG we can alloation from. If we were given
  3383. * constraining flags by the caller, drop them and retry the allocation
  3384. * without any constraints being set.
  3385. */
  3386. if (alloc_flags & XFS_ALLOC_FLAG_TRYLOCK) {
  3387. alloc_flags &= ~XFS_ALLOC_FLAG_TRYLOCK;
  3388. restart_agno = minimum_agno;
  3389. goto restart;
  3390. }
  3391. ASSERT(args->pag == NULL);
  3392. trace_xfs_alloc_vextent_allfailed(args);
  3393. return 0;
  3394. }
  3395. /*
  3396. * Iterate from the AGs from the start AG to the end of the filesystem, trying
  3397. * to allocate blocks. It starts with a near allocation attempt in the initial
  3398. * AG, then falls back to anywhere-in-ag after the first AG fails. It will wrap
  3399. * back to zero if allowed by previous allocations in this transaction,
  3400. * otherwise will wrap back to the start AG and run a second blocking pass to
  3401. * the end of the filesystem.
  3402. */
  3403. int
  3404. xfs_alloc_vextent_start_ag(
  3405. struct xfs_alloc_arg *args,
  3406. xfs_fsblock_t target)
  3407. {
  3408. struct xfs_mount *mp = args->mp;
  3409. xfs_agnumber_t minimum_agno;
  3410. xfs_agnumber_t start_agno;
  3411. xfs_agnumber_t rotorstep = xfs_rotorstep;
  3412. bool bump_rotor = false;
  3413. uint32_t alloc_flags = XFS_ALLOC_FLAG_TRYLOCK;
  3414. int error;
  3415. ASSERT(args->pag == NULL);
  3416. args->agno = NULLAGNUMBER;
  3417. args->agbno = NULLAGBLOCK;
  3418. trace_xfs_alloc_vextent_start_ag(args);
  3419. error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
  3420. if (error) {
  3421. if (error == -ENOSPC)
  3422. return 0;
  3423. return error;
  3424. }
  3425. if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
  3426. xfs_is_inode32(mp)) {
  3427. target = XFS_AGB_TO_FSB(mp,
  3428. ((mp->m_agfrotor / rotorstep) %
  3429. mp->m_sb.sb_agcount), 0);
  3430. bump_rotor = 1;
  3431. }
  3432. start_agno = max(minimum_agno, XFS_FSB_TO_AGNO(mp, target));
  3433. error = xfs_alloc_vextent_iterate_ags(args, minimum_agno, start_agno,
  3434. XFS_FSB_TO_AGBNO(mp, target), alloc_flags);
  3435. if (bump_rotor) {
  3436. if (args->agno == start_agno)
  3437. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  3438. (mp->m_sb.sb_agcount * rotorstep);
  3439. else
  3440. mp->m_agfrotor = (args->agno * rotorstep + 1) %
  3441. (mp->m_sb.sb_agcount * rotorstep);
  3442. }
  3443. return xfs_alloc_vextent_finish(args, minimum_agno, error, true);
  3444. }
  3445. /*
  3446. * Iterate from the agno indicated via @target through to the end of the
  3447. * filesystem attempting blocking allocation. This does not wrap or try a second
  3448. * pass, so will not recurse into AGs lower than indicated by the target.
  3449. */
  3450. int
  3451. xfs_alloc_vextent_first_ag(
  3452. struct xfs_alloc_arg *args,
  3453. xfs_fsblock_t target)
  3454. {
  3455. struct xfs_mount *mp = args->mp;
  3456. xfs_agnumber_t minimum_agno;
  3457. xfs_agnumber_t start_agno;
  3458. uint32_t alloc_flags = XFS_ALLOC_FLAG_TRYLOCK;
  3459. int error;
  3460. ASSERT(args->pag == NULL);
  3461. args->agno = NULLAGNUMBER;
  3462. args->agbno = NULLAGBLOCK;
  3463. trace_xfs_alloc_vextent_first_ag(args);
  3464. error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
  3465. if (error) {
  3466. if (error == -ENOSPC)
  3467. return 0;
  3468. return error;
  3469. }
  3470. start_agno = max(minimum_agno, XFS_FSB_TO_AGNO(mp, target));
  3471. error = xfs_alloc_vextent_iterate_ags(args, minimum_agno, start_agno,
  3472. XFS_FSB_TO_AGBNO(mp, target), alloc_flags);
  3473. return xfs_alloc_vextent_finish(args, minimum_agno, error, true);
  3474. }
  3475. /*
  3476. * Allocate at the exact block target or fail. Caller is expected to hold a
  3477. * perag reference in args->pag.
  3478. */
  3479. int
  3480. xfs_alloc_vextent_exact_bno(
  3481. struct xfs_alloc_arg *args,
  3482. xfs_fsblock_t target)
  3483. {
  3484. struct xfs_mount *mp = args->mp;
  3485. xfs_agnumber_t minimum_agno;
  3486. int error;
  3487. ASSERT(args->pag != NULL);
  3488. ASSERT(args->pag->pag_agno == XFS_FSB_TO_AGNO(mp, target));
  3489. args->agno = XFS_FSB_TO_AGNO(mp, target);
  3490. args->agbno = XFS_FSB_TO_AGBNO(mp, target);
  3491. trace_xfs_alloc_vextent_exact_bno(args);
  3492. error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
  3493. if (error) {
  3494. if (error == -ENOSPC)
  3495. return 0;
  3496. return error;
  3497. }
  3498. error = xfs_alloc_vextent_prepare_ag(args, 0);
  3499. if (!error && args->agbp)
  3500. error = xfs_alloc_ag_vextent_exact(args);
  3501. return xfs_alloc_vextent_finish(args, minimum_agno, error, false);
  3502. }
  3503. /*
  3504. * Allocate an extent as close to the target as possible. If there are not
  3505. * viable candidates in the AG, then fail the allocation.
  3506. *
  3507. * Caller may or may not have a per-ag reference in args->pag.
  3508. */
  3509. int
  3510. xfs_alloc_vextent_near_bno(
  3511. struct xfs_alloc_arg *args,
  3512. xfs_fsblock_t target)
  3513. {
  3514. struct xfs_mount *mp = args->mp;
  3515. xfs_agnumber_t minimum_agno;
  3516. bool needs_perag = args->pag == NULL;
  3517. uint32_t alloc_flags = 0;
  3518. int error;
  3519. if (!needs_perag)
  3520. ASSERT(args->pag->pag_agno == XFS_FSB_TO_AGNO(mp, target));
  3521. args->agno = XFS_FSB_TO_AGNO(mp, target);
  3522. args->agbno = XFS_FSB_TO_AGBNO(mp, target);
  3523. trace_xfs_alloc_vextent_near_bno(args);
  3524. error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
  3525. if (error) {
  3526. if (error == -ENOSPC)
  3527. return 0;
  3528. return error;
  3529. }
  3530. if (needs_perag)
  3531. args->pag = xfs_perag_grab(mp, args->agno);
  3532. error = xfs_alloc_vextent_prepare_ag(args, alloc_flags);
  3533. if (!error && args->agbp)
  3534. error = xfs_alloc_ag_vextent_near(args, alloc_flags);
  3535. return xfs_alloc_vextent_finish(args, minimum_agno, error, needs_perag);
  3536. }
  3537. /* Ensure that the freelist is at full capacity. */
  3538. int
  3539. xfs_free_extent_fix_freelist(
  3540. struct xfs_trans *tp,
  3541. struct xfs_perag *pag,
  3542. struct xfs_buf **agbp)
  3543. {
  3544. struct xfs_alloc_arg args;
  3545. int error;
  3546. memset(&args, 0, sizeof(struct xfs_alloc_arg));
  3547. args.tp = tp;
  3548. args.mp = tp->t_mountp;
  3549. args.agno = pag->pag_agno;
  3550. args.pag = pag;
  3551. /*
  3552. * validate that the block number is legal - the enables us to detect
  3553. * and handle a silent filesystem corruption rather than crashing.
  3554. */
  3555. if (args.agno >= args.mp->m_sb.sb_agcount)
  3556. return -EFSCORRUPTED;
  3557. error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
  3558. if (error)
  3559. return error;
  3560. *agbp = args.agbp;
  3561. return 0;
  3562. }
  3563. /*
  3564. * Free an extent.
  3565. * Just break up the extent address and hand off to xfs_free_ag_extent
  3566. * after fixing up the freelist.
  3567. */
  3568. int
  3569. __xfs_free_extent(
  3570. struct xfs_trans *tp,
  3571. struct xfs_perag *pag,
  3572. xfs_agblock_t agbno,
  3573. xfs_extlen_t len,
  3574. const struct xfs_owner_info *oinfo,
  3575. enum xfs_ag_resv_type type,
  3576. bool skip_discard)
  3577. {
  3578. struct xfs_mount *mp = tp->t_mountp;
  3579. struct xfs_buf *agbp;
  3580. struct xfs_agf *agf;
  3581. int error;
  3582. unsigned int busy_flags = 0;
  3583. ASSERT(len != 0);
  3584. ASSERT(type != XFS_AG_RESV_AGFL);
  3585. if (XFS_TEST_ERROR(false, mp,
  3586. XFS_ERRTAG_FREE_EXTENT))
  3587. return -EIO;
  3588. error = xfs_free_extent_fix_freelist(tp, pag, &agbp);
  3589. if (error) {
  3590. if (xfs_metadata_is_sick(error))
  3591. xfs_ag_mark_sick(pag, XFS_SICK_AG_BNOBT);
  3592. return error;
  3593. }
  3594. agf = agbp->b_addr;
  3595. if (XFS_IS_CORRUPT(mp, agbno >= mp->m_sb.sb_agblocks)) {
  3596. xfs_ag_mark_sick(pag, XFS_SICK_AG_BNOBT);
  3597. error = -EFSCORRUPTED;
  3598. goto err_release;
  3599. }
  3600. /* validate the extent size is legal now we have the agf locked */
  3601. if (XFS_IS_CORRUPT(mp, agbno + len > be32_to_cpu(agf->agf_length))) {
  3602. xfs_ag_mark_sick(pag, XFS_SICK_AG_BNOBT);
  3603. error = -EFSCORRUPTED;
  3604. goto err_release;
  3605. }
  3606. error = xfs_free_ag_extent(tp, agbp, pag->pag_agno, agbno, len, oinfo,
  3607. type);
  3608. if (error)
  3609. goto err_release;
  3610. if (skip_discard)
  3611. busy_flags |= XFS_EXTENT_BUSY_SKIP_DISCARD;
  3612. xfs_extent_busy_insert(tp, pag, agbno, len, busy_flags);
  3613. return 0;
  3614. err_release:
  3615. xfs_trans_brelse(tp, agbp);
  3616. return error;
  3617. }
  3618. struct xfs_alloc_query_range_info {
  3619. xfs_alloc_query_range_fn fn;
  3620. void *priv;
  3621. };
  3622. /* Format btree record and pass to our callback. */
  3623. STATIC int
  3624. xfs_alloc_query_range_helper(
  3625. struct xfs_btree_cur *cur,
  3626. const union xfs_btree_rec *rec,
  3627. void *priv)
  3628. {
  3629. struct xfs_alloc_query_range_info *query = priv;
  3630. struct xfs_alloc_rec_incore irec;
  3631. xfs_failaddr_t fa;
  3632. xfs_alloc_btrec_to_irec(rec, &irec);
  3633. fa = xfs_alloc_check_irec(cur->bc_ag.pag, &irec);
  3634. if (fa)
  3635. return xfs_alloc_complain_bad_rec(cur, fa, &irec);
  3636. return query->fn(cur, &irec, query->priv);
  3637. }
  3638. /* Find all free space within a given range of blocks. */
  3639. int
  3640. xfs_alloc_query_range(
  3641. struct xfs_btree_cur *cur,
  3642. const struct xfs_alloc_rec_incore *low_rec,
  3643. const struct xfs_alloc_rec_incore *high_rec,
  3644. xfs_alloc_query_range_fn fn,
  3645. void *priv)
  3646. {
  3647. union xfs_btree_irec low_brec = { .a = *low_rec };
  3648. union xfs_btree_irec high_brec = { .a = *high_rec };
  3649. struct xfs_alloc_query_range_info query = { .priv = priv, .fn = fn };
  3650. ASSERT(xfs_btree_is_bno(cur->bc_ops));
  3651. return xfs_btree_query_range(cur, &low_brec, &high_brec,
  3652. xfs_alloc_query_range_helper, &query);
  3653. }
  3654. /* Find all free space records. */
  3655. int
  3656. xfs_alloc_query_all(
  3657. struct xfs_btree_cur *cur,
  3658. xfs_alloc_query_range_fn fn,
  3659. void *priv)
  3660. {
  3661. struct xfs_alloc_query_range_info query;
  3662. ASSERT(xfs_btree_is_bno(cur->bc_ops));
  3663. query.priv = priv;
  3664. query.fn = fn;
  3665. return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
  3666. }
  3667. /*
  3668. * Scan part of the keyspace of the free space and tell us if the area has no
  3669. * records, is fully mapped by records, or is partially filled.
  3670. */
  3671. int
  3672. xfs_alloc_has_records(
  3673. struct xfs_btree_cur *cur,
  3674. xfs_agblock_t bno,
  3675. xfs_extlen_t len,
  3676. enum xbtree_recpacking *outcome)
  3677. {
  3678. union xfs_btree_irec low;
  3679. union xfs_btree_irec high;
  3680. memset(&low, 0, sizeof(low));
  3681. low.a.ar_startblock = bno;
  3682. memset(&high, 0xFF, sizeof(high));
  3683. high.a.ar_startblock = bno + len - 1;
  3684. return xfs_btree_has_records(cur, &low, &high, NULL, outcome);
  3685. }
  3686. /*
  3687. * Walk all the blocks in the AGFL. The @walk_fn can return any negative
  3688. * error code or XFS_ITER_*.
  3689. */
  3690. int
  3691. xfs_agfl_walk(
  3692. struct xfs_mount *mp,
  3693. struct xfs_agf *agf,
  3694. struct xfs_buf *agflbp,
  3695. xfs_agfl_walk_fn walk_fn,
  3696. void *priv)
  3697. {
  3698. __be32 *agfl_bno;
  3699. unsigned int i;
  3700. int error;
  3701. agfl_bno = xfs_buf_to_agfl_bno(agflbp);
  3702. i = be32_to_cpu(agf->agf_flfirst);
  3703. /* Nothing to walk in an empty AGFL. */
  3704. if (agf->agf_flcount == cpu_to_be32(0))
  3705. return 0;
  3706. /* Otherwise, walk from first to last, wrapping as needed. */
  3707. for (;;) {
  3708. error = walk_fn(mp, be32_to_cpu(agfl_bno[i]), priv);
  3709. if (error)
  3710. return error;
  3711. if (i == be32_to_cpu(agf->agf_fllast))
  3712. break;
  3713. if (++i == xfs_agfl_size(mp))
  3714. i = 0;
  3715. }
  3716. return 0;
  3717. }
  3718. int __init
  3719. xfs_extfree_intent_init_cache(void)
  3720. {
  3721. xfs_extfree_item_cache = kmem_cache_create("xfs_extfree_intent",
  3722. sizeof(struct xfs_extent_free_item),
  3723. 0, 0, NULL);
  3724. return xfs_extfree_item_cache != NULL ? 0 : -ENOMEM;
  3725. }
  3726. void
  3727. xfs_extfree_intent_destroy_cache(void)
  3728. {
  3729. kmem_cache_destroy(xfs_extfree_item_cache);
  3730. xfs_extfree_item_cache = NULL;
  3731. }