xfs_alloc.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180
  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_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_btree.h"
  18. #include "xfs_rmap.h"
  19. #include "xfs_alloc_btree.h"
  20. #include "xfs_alloc.h"
  21. #include "xfs_extent_busy.h"
  22. #include "xfs_errortag.h"
  23. #include "xfs_error.h"
  24. #include "xfs_cksum.h"
  25. #include "xfs_trace.h"
  26. #include "xfs_trans.h"
  27. #include "xfs_buf_item.h"
  28. #include "xfs_log.h"
  29. #include "xfs_ag_resv.h"
  30. #include "xfs_bmap.h"
  31. extern kmem_zone_t *xfs_bmap_free_item_zone;
  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. STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
  37. STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
  38. STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
  39. STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
  40. xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
  41. /*
  42. * Size of the AGFL. For CRC-enabled filesystes we steal a couple of slots in
  43. * the beginning of the block for a proper header with the location information
  44. * and CRC.
  45. */
  46. unsigned int
  47. xfs_agfl_size(
  48. struct xfs_mount *mp)
  49. {
  50. unsigned int size = mp->m_sb.sb_sectsize;
  51. if (xfs_sb_version_hascrc(&mp->m_sb))
  52. size -= sizeof(struct xfs_agfl);
  53. return size / sizeof(xfs_agblock_t);
  54. }
  55. unsigned int
  56. xfs_refc_block(
  57. struct xfs_mount *mp)
  58. {
  59. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  60. return XFS_RMAP_BLOCK(mp) + 1;
  61. if (xfs_sb_version_hasfinobt(&mp->m_sb))
  62. return XFS_FIBT_BLOCK(mp) + 1;
  63. return XFS_IBT_BLOCK(mp) + 1;
  64. }
  65. xfs_extlen_t
  66. xfs_prealloc_blocks(
  67. struct xfs_mount *mp)
  68. {
  69. if (xfs_sb_version_hasreflink(&mp->m_sb))
  70. return xfs_refc_block(mp) + 1;
  71. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  72. return XFS_RMAP_BLOCK(mp) + 1;
  73. if (xfs_sb_version_hasfinobt(&mp->m_sb))
  74. return XFS_FIBT_BLOCK(mp) + 1;
  75. return XFS_IBT_BLOCK(mp) + 1;
  76. }
  77. /*
  78. * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
  79. * AGF buffer (PV 947395), we place constraints on the relationship among
  80. * actual allocations for data blocks, freelist blocks, and potential file data
  81. * bmap btree blocks. However, these restrictions may result in no actual space
  82. * allocated for a delayed extent, for example, a data block in a certain AG is
  83. * allocated but there is no additional block for the additional bmap btree
  84. * block due to a split of the bmap btree of the file. The result of this may
  85. * lead to an infinite loop when the file gets flushed to disk and all delayed
  86. * extents need to be actually allocated. To get around this, we explicitly set
  87. * aside a few blocks which will not be reserved in delayed allocation.
  88. *
  89. * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
  90. * potential split of the file's bmap btree.
  91. */
  92. unsigned int
  93. xfs_alloc_set_aside(
  94. struct xfs_mount *mp)
  95. {
  96. return mp->m_sb.sb_agcount * (XFS_ALLOC_AGFL_RESERVE + 4);
  97. }
  98. /*
  99. * When deciding how much space to allocate out of an AG, we limit the
  100. * allocation maximum size to the size the AG. However, we cannot use all the
  101. * blocks in the AG - some are permanently used by metadata. These
  102. * blocks are generally:
  103. * - the AG superblock, AGF, AGI and AGFL
  104. * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
  105. * the AGI free inode and rmap btree root blocks.
  106. * - blocks on the AGFL according to xfs_alloc_set_aside() limits
  107. * - the rmapbt root block
  108. *
  109. * The AG headers are sector sized, so the amount of space they take up is
  110. * dependent on filesystem geometry. The others are all single blocks.
  111. */
  112. unsigned int
  113. xfs_alloc_ag_max_usable(
  114. struct xfs_mount *mp)
  115. {
  116. unsigned int blocks;
  117. blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
  118. blocks += XFS_ALLOC_AGFL_RESERVE;
  119. blocks += 3; /* AGF, AGI btree root blocks */
  120. if (xfs_sb_version_hasfinobt(&mp->m_sb))
  121. blocks++; /* finobt root block */
  122. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  123. blocks++; /* rmap root block */
  124. if (xfs_sb_version_hasreflink(&mp->m_sb))
  125. blocks++; /* refcount root block */
  126. return mp->m_sb.sb_agblocks - blocks;
  127. }
  128. /*
  129. * Lookup the record equal to [bno, len] in the btree given by cur.
  130. */
  131. STATIC int /* error */
  132. xfs_alloc_lookup_eq(
  133. struct xfs_btree_cur *cur, /* btree cursor */
  134. xfs_agblock_t bno, /* starting block of extent */
  135. xfs_extlen_t len, /* length of extent */
  136. int *stat) /* success/failure */
  137. {
  138. cur->bc_rec.a.ar_startblock = bno;
  139. cur->bc_rec.a.ar_blockcount = len;
  140. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  141. }
  142. /*
  143. * Lookup the first record greater than or equal to [bno, len]
  144. * in the btree given by cur.
  145. */
  146. int /* error */
  147. xfs_alloc_lookup_ge(
  148. struct xfs_btree_cur *cur, /* btree cursor */
  149. xfs_agblock_t bno, /* starting block of extent */
  150. xfs_extlen_t len, /* length of extent */
  151. int *stat) /* success/failure */
  152. {
  153. cur->bc_rec.a.ar_startblock = bno;
  154. cur->bc_rec.a.ar_blockcount = len;
  155. return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
  156. }
  157. /*
  158. * Lookup the first record less than or equal to [bno, len]
  159. * in the btree given by cur.
  160. */
  161. int /* error */
  162. xfs_alloc_lookup_le(
  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. cur->bc_rec.a.ar_startblock = bno;
  169. cur->bc_rec.a.ar_blockcount = len;
  170. return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
  171. }
  172. /*
  173. * Update the record referred to by cur to the value given
  174. * by [bno, len].
  175. * This either works (return 0) or gets an EFSCORRUPTED error.
  176. */
  177. STATIC int /* error */
  178. xfs_alloc_update(
  179. struct xfs_btree_cur *cur, /* btree cursor */
  180. xfs_agblock_t bno, /* starting block of extent */
  181. xfs_extlen_t len) /* length of extent */
  182. {
  183. union xfs_btree_rec rec;
  184. rec.alloc.ar_startblock = cpu_to_be32(bno);
  185. rec.alloc.ar_blockcount = cpu_to_be32(len);
  186. return xfs_btree_update(cur, &rec);
  187. }
  188. /*
  189. * Get the data from the pointed-to record.
  190. */
  191. int /* error */
  192. xfs_alloc_get_rec(
  193. struct xfs_btree_cur *cur, /* btree cursor */
  194. xfs_agblock_t *bno, /* output: starting block of extent */
  195. xfs_extlen_t *len, /* output: length of extent */
  196. int *stat) /* output: success/failure */
  197. {
  198. struct xfs_mount *mp = cur->bc_mp;
  199. xfs_agnumber_t agno = cur->bc_private.a.agno;
  200. union xfs_btree_rec *rec;
  201. int error;
  202. error = xfs_btree_get_rec(cur, &rec, stat);
  203. if (error || !(*stat))
  204. return error;
  205. if (rec->alloc.ar_blockcount == 0)
  206. goto out_bad_rec;
  207. *bno = be32_to_cpu(rec->alloc.ar_startblock);
  208. *len = be32_to_cpu(rec->alloc.ar_blockcount);
  209. /* check for valid extent range, including overflow */
  210. if (!xfs_verify_agbno(mp, agno, *bno))
  211. goto out_bad_rec;
  212. if (*bno > *bno + *len)
  213. goto out_bad_rec;
  214. if (!xfs_verify_agbno(mp, agno, *bno + *len - 1))
  215. goto out_bad_rec;
  216. return 0;
  217. out_bad_rec:
  218. xfs_warn(mp,
  219. "%s Freespace BTree record corruption in AG %d detected!",
  220. cur->bc_btnum == XFS_BTNUM_BNO ? "Block" : "Size", agno);
  221. xfs_warn(mp,
  222. "start block 0x%x block count 0x%x", *bno, *len);
  223. return -EFSCORRUPTED;
  224. }
  225. /*
  226. * Compute aligned version of the found extent.
  227. * Takes alignment and min length into account.
  228. */
  229. STATIC bool
  230. xfs_alloc_compute_aligned(
  231. xfs_alloc_arg_t *args, /* allocation argument structure */
  232. xfs_agblock_t foundbno, /* starting block in found extent */
  233. xfs_extlen_t foundlen, /* length in found extent */
  234. xfs_agblock_t *resbno, /* result block number */
  235. xfs_extlen_t *reslen, /* result length */
  236. unsigned *busy_gen)
  237. {
  238. xfs_agblock_t bno = foundbno;
  239. xfs_extlen_t len = foundlen;
  240. xfs_extlen_t diff;
  241. bool busy;
  242. /* Trim busy sections out of found extent */
  243. busy = xfs_extent_busy_trim(args, &bno, &len, busy_gen);
  244. /*
  245. * If we have a largish extent that happens to start before min_agbno,
  246. * see if we can shift it into range...
  247. */
  248. if (bno < args->min_agbno && bno + len > args->min_agbno) {
  249. diff = args->min_agbno - bno;
  250. if (len > diff) {
  251. bno += diff;
  252. len -= diff;
  253. }
  254. }
  255. if (args->alignment > 1 && len >= args->minlen) {
  256. xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
  257. diff = aligned_bno - bno;
  258. *resbno = aligned_bno;
  259. *reslen = diff >= len ? 0 : len - diff;
  260. } else {
  261. *resbno = bno;
  262. *reslen = len;
  263. }
  264. return busy;
  265. }
  266. /*
  267. * Compute best start block and diff for "near" allocations.
  268. * freelen >= wantlen already checked by caller.
  269. */
  270. STATIC xfs_extlen_t /* difference value (absolute) */
  271. xfs_alloc_compute_diff(
  272. xfs_agblock_t wantbno, /* target starting block */
  273. xfs_extlen_t wantlen, /* target length */
  274. xfs_extlen_t alignment, /* target alignment */
  275. int datatype, /* are we allocating data? */
  276. xfs_agblock_t freebno, /* freespace's starting block */
  277. xfs_extlen_t freelen, /* freespace's length */
  278. xfs_agblock_t *newbnop) /* result: best start block from free */
  279. {
  280. xfs_agblock_t freeend; /* end of freespace extent */
  281. xfs_agblock_t newbno1; /* return block number */
  282. xfs_agblock_t newbno2; /* other new block number */
  283. xfs_extlen_t newlen1=0; /* length with newbno1 */
  284. xfs_extlen_t newlen2=0; /* length with newbno2 */
  285. xfs_agblock_t wantend; /* end of target extent */
  286. bool userdata = xfs_alloc_is_userdata(datatype);
  287. ASSERT(freelen >= wantlen);
  288. freeend = freebno + freelen;
  289. wantend = wantbno + wantlen;
  290. /*
  291. * We want to allocate from the start of a free extent if it is past
  292. * the desired block or if we are allocating user data and the free
  293. * extent is before desired block. The second case is there to allow
  294. * for contiguous allocation from the remaining free space if the file
  295. * grows in the short term.
  296. */
  297. if (freebno >= wantbno || (userdata && freeend < wantend)) {
  298. if ((newbno1 = roundup(freebno, alignment)) >= freeend)
  299. newbno1 = NULLAGBLOCK;
  300. } else if (freeend >= wantend && alignment > 1) {
  301. newbno1 = roundup(wantbno, alignment);
  302. newbno2 = newbno1 - alignment;
  303. if (newbno1 >= freeend)
  304. newbno1 = NULLAGBLOCK;
  305. else
  306. newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
  307. if (newbno2 < freebno)
  308. newbno2 = NULLAGBLOCK;
  309. else
  310. newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
  311. if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
  312. if (newlen1 < newlen2 ||
  313. (newlen1 == newlen2 &&
  314. XFS_ABSDIFF(newbno1, wantbno) >
  315. XFS_ABSDIFF(newbno2, wantbno)))
  316. newbno1 = newbno2;
  317. } else if (newbno2 != NULLAGBLOCK)
  318. newbno1 = newbno2;
  319. } else if (freeend >= wantend) {
  320. newbno1 = wantbno;
  321. } else if (alignment > 1) {
  322. newbno1 = roundup(freeend - wantlen, alignment);
  323. if (newbno1 > freeend - wantlen &&
  324. newbno1 - alignment >= freebno)
  325. newbno1 -= alignment;
  326. else if (newbno1 >= freeend)
  327. newbno1 = NULLAGBLOCK;
  328. } else
  329. newbno1 = freeend - wantlen;
  330. *newbnop = newbno1;
  331. return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
  332. }
  333. /*
  334. * Fix up the length, based on mod and prod.
  335. * len should be k * prod + mod for some k.
  336. * If len is too small it is returned unchanged.
  337. * If len hits maxlen it is left alone.
  338. */
  339. STATIC void
  340. xfs_alloc_fix_len(
  341. xfs_alloc_arg_t *args) /* allocation argument structure */
  342. {
  343. xfs_extlen_t k;
  344. xfs_extlen_t rlen;
  345. ASSERT(args->mod < args->prod);
  346. rlen = args->len;
  347. ASSERT(rlen >= args->minlen);
  348. ASSERT(rlen <= args->maxlen);
  349. if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
  350. (args->mod == 0 && rlen < args->prod))
  351. return;
  352. k = rlen % args->prod;
  353. if (k == args->mod)
  354. return;
  355. if (k > args->mod)
  356. rlen = rlen - (k - args->mod);
  357. else
  358. rlen = rlen - args->prod + (args->mod - k);
  359. /* casts to (int) catch length underflows */
  360. if ((int)rlen < (int)args->minlen)
  361. return;
  362. ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
  363. ASSERT(rlen % args->prod == args->mod);
  364. ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
  365. rlen + args->minleft);
  366. args->len = rlen;
  367. }
  368. /*
  369. * Update the two btrees, logically removing from freespace the extent
  370. * starting at rbno, rlen blocks. The extent is contained within the
  371. * actual (current) free extent fbno for flen blocks.
  372. * Flags are passed in indicating whether the cursors are set to the
  373. * relevant records.
  374. */
  375. STATIC int /* error code */
  376. xfs_alloc_fixup_trees(
  377. xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
  378. xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
  379. xfs_agblock_t fbno, /* starting block of free extent */
  380. xfs_extlen_t flen, /* length of free extent */
  381. xfs_agblock_t rbno, /* starting block of returned extent */
  382. xfs_extlen_t rlen, /* length of returned extent */
  383. int flags) /* flags, XFSA_FIXUP_... */
  384. {
  385. int error; /* error code */
  386. int i; /* operation results */
  387. xfs_agblock_t nfbno1; /* first new free startblock */
  388. xfs_agblock_t nfbno2; /* second new free startblock */
  389. xfs_extlen_t nflen1=0; /* first new free length */
  390. xfs_extlen_t nflen2=0; /* second new free length */
  391. struct xfs_mount *mp;
  392. mp = cnt_cur->bc_mp;
  393. /*
  394. * Look up the record in the by-size tree if necessary.
  395. */
  396. if (flags & XFSA_FIXUP_CNT_OK) {
  397. #ifdef DEBUG
  398. if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
  399. return error;
  400. XFS_WANT_CORRUPTED_RETURN(mp,
  401. i == 1 && nfbno1 == fbno && nflen1 == flen);
  402. #endif
  403. } else {
  404. if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
  405. return error;
  406. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  407. }
  408. /*
  409. * Look up the record in the by-block tree if necessary.
  410. */
  411. if (flags & XFSA_FIXUP_BNO_OK) {
  412. #ifdef DEBUG
  413. if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
  414. return error;
  415. XFS_WANT_CORRUPTED_RETURN(mp,
  416. i == 1 && nfbno1 == fbno && nflen1 == flen);
  417. #endif
  418. } else {
  419. if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
  420. return error;
  421. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  422. }
  423. #ifdef DEBUG
  424. if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
  425. struct xfs_btree_block *bnoblock;
  426. struct xfs_btree_block *cntblock;
  427. bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
  428. cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
  429. XFS_WANT_CORRUPTED_RETURN(mp,
  430. bnoblock->bb_numrecs == cntblock->bb_numrecs);
  431. }
  432. #endif
  433. /*
  434. * Deal with all four cases: the allocated record is contained
  435. * within the freespace record, so we can have new freespace
  436. * at either (or both) end, or no freespace remaining.
  437. */
  438. if (rbno == fbno && rlen == flen)
  439. nfbno1 = nfbno2 = NULLAGBLOCK;
  440. else if (rbno == fbno) {
  441. nfbno1 = rbno + rlen;
  442. nflen1 = flen - rlen;
  443. nfbno2 = NULLAGBLOCK;
  444. } else if (rbno + rlen == fbno + flen) {
  445. nfbno1 = fbno;
  446. nflen1 = flen - rlen;
  447. nfbno2 = NULLAGBLOCK;
  448. } else {
  449. nfbno1 = fbno;
  450. nflen1 = rbno - fbno;
  451. nfbno2 = rbno + rlen;
  452. nflen2 = (fbno + flen) - nfbno2;
  453. }
  454. /*
  455. * Delete the entry from the by-size btree.
  456. */
  457. if ((error = xfs_btree_delete(cnt_cur, &i)))
  458. return error;
  459. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  460. /*
  461. * Add new by-size btree entry(s).
  462. */
  463. if (nfbno1 != NULLAGBLOCK) {
  464. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
  465. return error;
  466. XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
  467. if ((error = xfs_btree_insert(cnt_cur, &i)))
  468. return error;
  469. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  470. }
  471. if (nfbno2 != NULLAGBLOCK) {
  472. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
  473. return error;
  474. XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
  475. if ((error = xfs_btree_insert(cnt_cur, &i)))
  476. return error;
  477. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  478. }
  479. /*
  480. * Fix up the by-block btree entry(s).
  481. */
  482. if (nfbno1 == NULLAGBLOCK) {
  483. /*
  484. * No remaining freespace, just delete the by-block tree entry.
  485. */
  486. if ((error = xfs_btree_delete(bno_cur, &i)))
  487. return error;
  488. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  489. } else {
  490. /*
  491. * Update the by-block entry to start later|be shorter.
  492. */
  493. if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
  494. return error;
  495. }
  496. if (nfbno2 != NULLAGBLOCK) {
  497. /*
  498. * 2 resulting free entries, need to add one.
  499. */
  500. if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
  501. return error;
  502. XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
  503. if ((error = xfs_btree_insert(bno_cur, &i)))
  504. return error;
  505. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  506. }
  507. return 0;
  508. }
  509. static xfs_failaddr_t
  510. xfs_agfl_verify(
  511. struct xfs_buf *bp)
  512. {
  513. struct xfs_mount *mp = bp->b_target->bt_mount;
  514. struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
  515. int i;
  516. /*
  517. * There is no verification of non-crc AGFLs because mkfs does not
  518. * initialise the AGFL to zero or NULL. Hence the only valid part of the
  519. * AGFL is what the AGF says is active. We can't get to the AGF, so we
  520. * can't verify just those entries are valid.
  521. */
  522. if (!xfs_sb_version_hascrc(&mp->m_sb))
  523. return NULL;
  524. if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
  525. return __this_address;
  526. if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
  527. return __this_address;
  528. /*
  529. * during growfs operations, the perag is not fully initialised,
  530. * so we can't use it for any useful checking. growfs ensures we can't
  531. * use it by using uncached buffers that don't have the perag attached
  532. * so we can detect and avoid this problem.
  533. */
  534. if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
  535. return __this_address;
  536. for (i = 0; i < xfs_agfl_size(mp); i++) {
  537. if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
  538. be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
  539. return __this_address;
  540. }
  541. if (!xfs_log_check_lsn(mp, be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn)))
  542. return __this_address;
  543. return NULL;
  544. }
  545. static void
  546. xfs_agfl_read_verify(
  547. struct xfs_buf *bp)
  548. {
  549. struct xfs_mount *mp = bp->b_target->bt_mount;
  550. xfs_failaddr_t fa;
  551. /*
  552. * There is no verification of non-crc AGFLs because mkfs does not
  553. * initialise the AGFL to zero or NULL. Hence the only valid part of the
  554. * AGFL is what the AGF says is active. We can't get to the AGF, so we
  555. * can't verify just those entries are valid.
  556. */
  557. if (!xfs_sb_version_hascrc(&mp->m_sb))
  558. return;
  559. if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
  560. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  561. else {
  562. fa = xfs_agfl_verify(bp);
  563. if (fa)
  564. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  565. }
  566. }
  567. static void
  568. xfs_agfl_write_verify(
  569. struct xfs_buf *bp)
  570. {
  571. struct xfs_mount *mp = bp->b_target->bt_mount;
  572. struct xfs_buf_log_item *bip = bp->b_log_item;
  573. xfs_failaddr_t fa;
  574. /* no verification of non-crc AGFLs */
  575. if (!xfs_sb_version_hascrc(&mp->m_sb))
  576. return;
  577. fa = xfs_agfl_verify(bp);
  578. if (fa) {
  579. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  580. return;
  581. }
  582. if (bip)
  583. XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  584. xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
  585. }
  586. const struct xfs_buf_ops xfs_agfl_buf_ops = {
  587. .name = "xfs_agfl",
  588. .verify_read = xfs_agfl_read_verify,
  589. .verify_write = xfs_agfl_write_verify,
  590. .verify_struct = xfs_agfl_verify,
  591. };
  592. /*
  593. * Read in the allocation group free block array.
  594. */
  595. int /* error */
  596. xfs_alloc_read_agfl(
  597. xfs_mount_t *mp, /* mount point structure */
  598. xfs_trans_t *tp, /* transaction pointer */
  599. xfs_agnumber_t agno, /* allocation group number */
  600. xfs_buf_t **bpp) /* buffer for the ag free block array */
  601. {
  602. xfs_buf_t *bp; /* return value */
  603. int error;
  604. ASSERT(agno != NULLAGNUMBER);
  605. error = xfs_trans_read_buf(
  606. mp, tp, mp->m_ddev_targp,
  607. XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
  608. XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
  609. if (error)
  610. return error;
  611. xfs_buf_set_ref(bp, XFS_AGFL_REF);
  612. *bpp = bp;
  613. return 0;
  614. }
  615. STATIC int
  616. xfs_alloc_update_counters(
  617. struct xfs_trans *tp,
  618. struct xfs_perag *pag,
  619. struct xfs_buf *agbp,
  620. long len)
  621. {
  622. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  623. pag->pagf_freeblks += len;
  624. be32_add_cpu(&agf->agf_freeblks, len);
  625. xfs_trans_agblocks_delta(tp, len);
  626. if (unlikely(be32_to_cpu(agf->agf_freeblks) >
  627. be32_to_cpu(agf->agf_length)))
  628. return -EFSCORRUPTED;
  629. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
  630. return 0;
  631. }
  632. /*
  633. * Allocation group level functions.
  634. */
  635. /*
  636. * Allocate a variable extent in the allocation group agno.
  637. * Type and bno are used to determine where in the allocation group the
  638. * extent will start.
  639. * Extent's length (returned in *len) will be between minlen and maxlen,
  640. * and of the form k * prod + mod unless there's nothing that large.
  641. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  642. */
  643. STATIC int /* error */
  644. xfs_alloc_ag_vextent(
  645. xfs_alloc_arg_t *args) /* argument structure for allocation */
  646. {
  647. int error=0;
  648. ASSERT(args->minlen > 0);
  649. ASSERT(args->maxlen > 0);
  650. ASSERT(args->minlen <= args->maxlen);
  651. ASSERT(args->mod < args->prod);
  652. ASSERT(args->alignment > 0);
  653. /*
  654. * Branch to correct routine based on the type.
  655. */
  656. args->wasfromfl = 0;
  657. switch (args->type) {
  658. case XFS_ALLOCTYPE_THIS_AG:
  659. error = xfs_alloc_ag_vextent_size(args);
  660. break;
  661. case XFS_ALLOCTYPE_NEAR_BNO:
  662. error = xfs_alloc_ag_vextent_near(args);
  663. break;
  664. case XFS_ALLOCTYPE_THIS_BNO:
  665. error = xfs_alloc_ag_vextent_exact(args);
  666. break;
  667. default:
  668. ASSERT(0);
  669. /* NOTREACHED */
  670. }
  671. if (error || args->agbno == NULLAGBLOCK)
  672. return error;
  673. ASSERT(args->len >= args->minlen);
  674. ASSERT(args->len <= args->maxlen);
  675. ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
  676. ASSERT(args->agbno % args->alignment == 0);
  677. /* if not file data, insert new block into the reverse map btree */
  678. if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) {
  679. error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
  680. args->agbno, args->len, &args->oinfo);
  681. if (error)
  682. return error;
  683. }
  684. if (!args->wasfromfl) {
  685. error = xfs_alloc_update_counters(args->tp, args->pag,
  686. args->agbp,
  687. -((long)(args->len)));
  688. if (error)
  689. return error;
  690. ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
  691. args->agbno, args->len));
  692. }
  693. xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
  694. XFS_STATS_INC(args->mp, xs_allocx);
  695. XFS_STATS_ADD(args->mp, xs_allocb, args->len);
  696. return error;
  697. }
  698. /*
  699. * Allocate a variable extent at exactly agno/bno.
  700. * Extent's length (returned in *len) will be between minlen and maxlen,
  701. * and of the form k * prod + mod unless there's nothing that large.
  702. * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
  703. */
  704. STATIC int /* error */
  705. xfs_alloc_ag_vextent_exact(
  706. xfs_alloc_arg_t *args) /* allocation argument structure */
  707. {
  708. xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
  709. xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
  710. int error;
  711. xfs_agblock_t fbno; /* start block of found extent */
  712. xfs_extlen_t flen; /* length of found extent */
  713. xfs_agblock_t tbno; /* start block of busy extent */
  714. xfs_extlen_t tlen; /* length of busy extent */
  715. xfs_agblock_t tend; /* end block of busy extent */
  716. int i; /* success/failure of operation */
  717. unsigned busy_gen;
  718. ASSERT(args->alignment == 1);
  719. /*
  720. * Allocate/initialize a cursor for the by-number freespace btree.
  721. */
  722. bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  723. args->agno, XFS_BTNUM_BNO);
  724. /*
  725. * Lookup bno and minlen in the btree (minlen is irrelevant, really).
  726. * Look for the closest free block <= bno, it must contain bno
  727. * if any free block does.
  728. */
  729. error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
  730. if (error)
  731. goto error0;
  732. if (!i)
  733. goto not_found;
  734. /*
  735. * Grab the freespace record.
  736. */
  737. error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
  738. if (error)
  739. goto error0;
  740. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  741. ASSERT(fbno <= args->agbno);
  742. /*
  743. * Check for overlapping busy extents.
  744. */
  745. tbno = fbno;
  746. tlen = flen;
  747. xfs_extent_busy_trim(args, &tbno, &tlen, &busy_gen);
  748. /*
  749. * Give up if the start of the extent is busy, or the freespace isn't
  750. * long enough for the minimum request.
  751. */
  752. if (tbno > args->agbno)
  753. goto not_found;
  754. if (tlen < args->minlen)
  755. goto not_found;
  756. tend = tbno + tlen;
  757. if (tend < args->agbno + args->minlen)
  758. goto not_found;
  759. /*
  760. * End of extent will be smaller of the freespace end and the
  761. * maximal requested end.
  762. *
  763. * Fix the length according to mod and prod if given.
  764. */
  765. args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
  766. - args->agbno;
  767. xfs_alloc_fix_len(args);
  768. ASSERT(args->agbno + args->len <= tend);
  769. /*
  770. * We are allocating agbno for args->len
  771. * Allocate/initialize a cursor for the by-size btree.
  772. */
  773. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  774. args->agno, XFS_BTNUM_CNT);
  775. ASSERT(args->agbno + args->len <=
  776. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  777. error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
  778. args->len, XFSA_FIXUP_BNO_OK);
  779. if (error) {
  780. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  781. goto error0;
  782. }
  783. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  784. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  785. args->wasfromfl = 0;
  786. trace_xfs_alloc_exact_done(args);
  787. return 0;
  788. not_found:
  789. /* Didn't find it, return null. */
  790. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  791. args->agbno = NULLAGBLOCK;
  792. trace_xfs_alloc_exact_notfound(args);
  793. return 0;
  794. error0:
  795. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  796. trace_xfs_alloc_exact_error(args);
  797. return error;
  798. }
  799. /*
  800. * Search the btree in a given direction via the search cursor and compare
  801. * the records found against the good extent we've already found.
  802. */
  803. STATIC int
  804. xfs_alloc_find_best_extent(
  805. struct xfs_alloc_arg *args, /* allocation argument structure */
  806. struct xfs_btree_cur **gcur, /* good cursor */
  807. struct xfs_btree_cur **scur, /* searching cursor */
  808. xfs_agblock_t gdiff, /* difference for search comparison */
  809. xfs_agblock_t *sbno, /* extent found by search */
  810. xfs_extlen_t *slen, /* extent length */
  811. xfs_agblock_t *sbnoa, /* aligned extent found by search */
  812. xfs_extlen_t *slena, /* aligned extent length */
  813. int dir) /* 0 = search right, 1 = search left */
  814. {
  815. xfs_agblock_t new;
  816. xfs_agblock_t sdiff;
  817. int error;
  818. int i;
  819. unsigned busy_gen;
  820. /* The good extent is perfect, no need to search. */
  821. if (!gdiff)
  822. goto out_use_good;
  823. /*
  824. * Look until we find a better one, run out of space or run off the end.
  825. */
  826. do {
  827. error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
  828. if (error)
  829. goto error0;
  830. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  831. xfs_alloc_compute_aligned(args, *sbno, *slen,
  832. sbnoa, slena, &busy_gen);
  833. /*
  834. * The good extent is closer than this one.
  835. */
  836. if (!dir) {
  837. if (*sbnoa > args->max_agbno)
  838. goto out_use_good;
  839. if (*sbnoa >= args->agbno + gdiff)
  840. goto out_use_good;
  841. } else {
  842. if (*sbnoa < args->min_agbno)
  843. goto out_use_good;
  844. if (*sbnoa <= args->agbno - gdiff)
  845. goto out_use_good;
  846. }
  847. /*
  848. * Same distance, compare length and pick the best.
  849. */
  850. if (*slena >= args->minlen) {
  851. args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
  852. xfs_alloc_fix_len(args);
  853. sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  854. args->alignment,
  855. args->datatype, *sbnoa,
  856. *slena, &new);
  857. /*
  858. * Choose closer size and invalidate other cursor.
  859. */
  860. if (sdiff < gdiff)
  861. goto out_use_search;
  862. goto out_use_good;
  863. }
  864. if (!dir)
  865. error = xfs_btree_increment(*scur, 0, &i);
  866. else
  867. error = xfs_btree_decrement(*scur, 0, &i);
  868. if (error)
  869. goto error0;
  870. } while (i);
  871. out_use_good:
  872. xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
  873. *scur = NULL;
  874. return 0;
  875. out_use_search:
  876. xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
  877. *gcur = NULL;
  878. return 0;
  879. error0:
  880. /* caller invalidates cursors */
  881. return error;
  882. }
  883. /*
  884. * Allocate a variable extent near bno in the allocation group agno.
  885. * Extent's length (returned in len) will be between minlen and maxlen,
  886. * and of the form k * prod + mod unless there's nothing that large.
  887. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  888. */
  889. STATIC int /* error */
  890. xfs_alloc_ag_vextent_near(
  891. xfs_alloc_arg_t *args) /* allocation argument structure */
  892. {
  893. xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
  894. xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
  895. xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
  896. xfs_agblock_t gtbno; /* start bno of right side entry */
  897. xfs_agblock_t gtbnoa; /* aligned ... */
  898. xfs_extlen_t gtdiff; /* difference to right side entry */
  899. xfs_extlen_t gtlen; /* length of right side entry */
  900. xfs_extlen_t gtlena; /* aligned ... */
  901. xfs_agblock_t gtnew; /* useful start bno of right side */
  902. int error; /* error code */
  903. int i; /* result code, temporary */
  904. int j; /* result code, temporary */
  905. xfs_agblock_t ltbno; /* start bno of left side entry */
  906. xfs_agblock_t ltbnoa; /* aligned ... */
  907. xfs_extlen_t ltdiff; /* difference to left side entry */
  908. xfs_extlen_t ltlen; /* length of left side entry */
  909. xfs_extlen_t ltlena; /* aligned ... */
  910. xfs_agblock_t ltnew; /* useful start bno of left side */
  911. xfs_extlen_t rlen; /* length of returned extent */
  912. bool busy;
  913. unsigned busy_gen;
  914. #ifdef DEBUG
  915. /*
  916. * Randomly don't execute the first algorithm.
  917. */
  918. int dofirst; /* set to do first algorithm */
  919. dofirst = prandom_u32() & 1;
  920. #endif
  921. /* handle unitialized agbno range so caller doesn't have to */
  922. if (!args->min_agbno && !args->max_agbno)
  923. args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
  924. ASSERT(args->min_agbno <= args->max_agbno);
  925. /* clamp agbno to the range if it's outside */
  926. if (args->agbno < args->min_agbno)
  927. args->agbno = args->min_agbno;
  928. if (args->agbno > args->max_agbno)
  929. args->agbno = args->max_agbno;
  930. restart:
  931. bno_cur_lt = NULL;
  932. bno_cur_gt = NULL;
  933. ltlen = 0;
  934. gtlena = 0;
  935. ltlena = 0;
  936. busy = false;
  937. /*
  938. * Get a cursor for the by-size btree.
  939. */
  940. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  941. args->agno, XFS_BTNUM_CNT);
  942. /*
  943. * See if there are any free extents as big as maxlen.
  944. */
  945. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
  946. goto error0;
  947. /*
  948. * If none, then pick up the last entry in the tree unless the
  949. * tree is empty.
  950. */
  951. if (!i) {
  952. if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
  953. &ltlen, &i)))
  954. goto error0;
  955. if (i == 0 || ltlen == 0) {
  956. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  957. trace_xfs_alloc_near_noentry(args);
  958. return 0;
  959. }
  960. ASSERT(i == 1);
  961. }
  962. args->wasfromfl = 0;
  963. /*
  964. * First algorithm.
  965. * If the requested extent is large wrt the freespaces available
  966. * in this a.g., then the cursor will be pointing to a btree entry
  967. * near the right edge of the tree. If it's in the last btree leaf
  968. * block, then we just examine all the entries in that block
  969. * that are big enough, and pick the best one.
  970. * This is written as a while loop so we can break out of it,
  971. * but we never loop back to the top.
  972. */
  973. while (xfs_btree_islastblock(cnt_cur, 0)) {
  974. xfs_extlen_t bdiff;
  975. int besti=0;
  976. xfs_extlen_t blen=0;
  977. xfs_agblock_t bnew=0;
  978. #ifdef DEBUG
  979. if (dofirst)
  980. break;
  981. #endif
  982. /*
  983. * Start from the entry that lookup found, sequence through
  984. * all larger free blocks. If we're actually pointing at a
  985. * record smaller than maxlen, go to the start of this block,
  986. * and skip all those smaller than minlen.
  987. */
  988. if (ltlen || args->alignment > 1) {
  989. cnt_cur->bc_ptrs[0] = 1;
  990. do {
  991. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
  992. &ltlen, &i)))
  993. goto error0;
  994. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  995. if (ltlen >= args->minlen)
  996. break;
  997. if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
  998. goto error0;
  999. } while (i);
  1000. ASSERT(ltlen >= args->minlen);
  1001. if (!i)
  1002. break;
  1003. }
  1004. i = cnt_cur->bc_ptrs[0];
  1005. for (j = 1, blen = 0, bdiff = 0;
  1006. !error && j && (blen < args->maxlen || bdiff > 0);
  1007. error = xfs_btree_increment(cnt_cur, 0, &j)) {
  1008. /*
  1009. * For each entry, decide if it's better than
  1010. * the previous best entry.
  1011. */
  1012. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  1013. goto error0;
  1014. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1015. busy = xfs_alloc_compute_aligned(args, ltbno, ltlen,
  1016. &ltbnoa, &ltlena, &busy_gen);
  1017. if (ltlena < args->minlen)
  1018. continue;
  1019. if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
  1020. continue;
  1021. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1022. xfs_alloc_fix_len(args);
  1023. ASSERT(args->len >= args->minlen);
  1024. if (args->len < blen)
  1025. continue;
  1026. ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1027. args->alignment, args->datatype, ltbnoa,
  1028. ltlena, &ltnew);
  1029. if (ltnew != NULLAGBLOCK &&
  1030. (args->len > blen || ltdiff < bdiff)) {
  1031. bdiff = ltdiff;
  1032. bnew = ltnew;
  1033. blen = args->len;
  1034. besti = cnt_cur->bc_ptrs[0];
  1035. }
  1036. }
  1037. /*
  1038. * It didn't work. We COULD be in a case where
  1039. * there's a good record somewhere, so try again.
  1040. */
  1041. if (blen == 0)
  1042. break;
  1043. /*
  1044. * Point at the best entry, and retrieve it again.
  1045. */
  1046. cnt_cur->bc_ptrs[0] = besti;
  1047. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  1048. goto error0;
  1049. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1050. ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  1051. args->len = blen;
  1052. /*
  1053. * We are allocating starting at bnew for blen blocks.
  1054. */
  1055. args->agbno = bnew;
  1056. ASSERT(bnew >= ltbno);
  1057. ASSERT(bnew + blen <= ltbno + ltlen);
  1058. /*
  1059. * Set up a cursor for the by-bno tree.
  1060. */
  1061. bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
  1062. args->agbp, args->agno, XFS_BTNUM_BNO);
  1063. /*
  1064. * Fix up the btree entries.
  1065. */
  1066. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
  1067. ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
  1068. goto error0;
  1069. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1070. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1071. trace_xfs_alloc_near_first(args);
  1072. return 0;
  1073. }
  1074. /*
  1075. * Second algorithm.
  1076. * Search in the by-bno tree to the left and to the right
  1077. * simultaneously, until in each case we find a space big enough,
  1078. * or run into the edge of the tree. When we run into the edge,
  1079. * we deallocate that cursor.
  1080. * If both searches succeed, we compare the two spaces and pick
  1081. * the better one.
  1082. * With alignment, it's possible for both to fail; the upper
  1083. * level algorithm that picks allocation groups for allocations
  1084. * is not supposed to do this.
  1085. */
  1086. /*
  1087. * Allocate and initialize the cursor for the leftward search.
  1088. */
  1089. bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1090. args->agno, XFS_BTNUM_BNO);
  1091. /*
  1092. * Lookup <= bno to find the leftward search's starting point.
  1093. */
  1094. if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
  1095. goto error0;
  1096. if (!i) {
  1097. /*
  1098. * Didn't find anything; use this cursor for the rightward
  1099. * search.
  1100. */
  1101. bno_cur_gt = bno_cur_lt;
  1102. bno_cur_lt = NULL;
  1103. }
  1104. /*
  1105. * Found something. Duplicate the cursor for the rightward search.
  1106. */
  1107. else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
  1108. goto error0;
  1109. /*
  1110. * Increment the cursor, so we will point at the entry just right
  1111. * of the leftward entry if any, or to the leftmost entry.
  1112. */
  1113. if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
  1114. goto error0;
  1115. if (!i) {
  1116. /*
  1117. * It failed, there are no rightward entries.
  1118. */
  1119. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
  1120. bno_cur_gt = NULL;
  1121. }
  1122. /*
  1123. * Loop going left with the leftward cursor, right with the
  1124. * rightward cursor, until either both directions give up or
  1125. * we find an entry at least as big as minlen.
  1126. */
  1127. do {
  1128. if (bno_cur_lt) {
  1129. if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
  1130. goto error0;
  1131. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1132. busy |= xfs_alloc_compute_aligned(args, ltbno, ltlen,
  1133. &ltbnoa, &ltlena, &busy_gen);
  1134. if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
  1135. break;
  1136. if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
  1137. goto error0;
  1138. if (!i || ltbnoa < args->min_agbno) {
  1139. xfs_btree_del_cursor(bno_cur_lt,
  1140. XFS_BTREE_NOERROR);
  1141. bno_cur_lt = NULL;
  1142. }
  1143. }
  1144. if (bno_cur_gt) {
  1145. if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
  1146. goto error0;
  1147. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1148. busy |= xfs_alloc_compute_aligned(args, gtbno, gtlen,
  1149. &gtbnoa, &gtlena, &busy_gen);
  1150. if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
  1151. break;
  1152. if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
  1153. goto error0;
  1154. if (!i || gtbnoa > args->max_agbno) {
  1155. xfs_btree_del_cursor(bno_cur_gt,
  1156. XFS_BTREE_NOERROR);
  1157. bno_cur_gt = NULL;
  1158. }
  1159. }
  1160. } while (bno_cur_lt || bno_cur_gt);
  1161. /*
  1162. * Got both cursors still active, need to find better entry.
  1163. */
  1164. if (bno_cur_lt && bno_cur_gt) {
  1165. if (ltlena >= args->minlen) {
  1166. /*
  1167. * Left side is good, look for a right side entry.
  1168. */
  1169. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1170. xfs_alloc_fix_len(args);
  1171. ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1172. args->alignment, args->datatype, ltbnoa,
  1173. ltlena, &ltnew);
  1174. error = xfs_alloc_find_best_extent(args,
  1175. &bno_cur_lt, &bno_cur_gt,
  1176. ltdiff, &gtbno, &gtlen,
  1177. &gtbnoa, &gtlena,
  1178. 0 /* search right */);
  1179. } else {
  1180. ASSERT(gtlena >= args->minlen);
  1181. /*
  1182. * Right side is good, look for a left side entry.
  1183. */
  1184. args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
  1185. xfs_alloc_fix_len(args);
  1186. gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1187. args->alignment, args->datatype, gtbnoa,
  1188. gtlena, &gtnew);
  1189. error = xfs_alloc_find_best_extent(args,
  1190. &bno_cur_gt, &bno_cur_lt,
  1191. gtdiff, &ltbno, &ltlen,
  1192. &ltbnoa, &ltlena,
  1193. 1 /* search left */);
  1194. }
  1195. if (error)
  1196. goto error0;
  1197. }
  1198. /*
  1199. * If we couldn't get anything, give up.
  1200. */
  1201. if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
  1202. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1203. if (busy) {
  1204. trace_xfs_alloc_near_busy(args);
  1205. xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
  1206. goto restart;
  1207. }
  1208. trace_xfs_alloc_size_neither(args);
  1209. args->agbno = NULLAGBLOCK;
  1210. return 0;
  1211. }
  1212. /*
  1213. * At this point we have selected a freespace entry, either to the
  1214. * left or to the right. If it's on the right, copy all the
  1215. * useful variables to the "left" set so we only have one
  1216. * copy of this code.
  1217. */
  1218. if (bno_cur_gt) {
  1219. bno_cur_lt = bno_cur_gt;
  1220. bno_cur_gt = NULL;
  1221. ltbno = gtbno;
  1222. ltbnoa = gtbnoa;
  1223. ltlen = gtlen;
  1224. ltlena = gtlena;
  1225. j = 1;
  1226. } else
  1227. j = 0;
  1228. /*
  1229. * Fix up the length and compute the useful address.
  1230. */
  1231. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1232. xfs_alloc_fix_len(args);
  1233. rlen = args->len;
  1234. (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
  1235. args->datatype, ltbnoa, ltlena, &ltnew);
  1236. ASSERT(ltnew >= ltbno);
  1237. ASSERT(ltnew + rlen <= ltbnoa + ltlena);
  1238. ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  1239. ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
  1240. args->agbno = ltnew;
  1241. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
  1242. ltnew, rlen, XFSA_FIXUP_BNO_OK)))
  1243. goto error0;
  1244. if (j)
  1245. trace_xfs_alloc_near_greater(args);
  1246. else
  1247. trace_xfs_alloc_near_lesser(args);
  1248. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1249. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1250. return 0;
  1251. error0:
  1252. trace_xfs_alloc_near_error(args);
  1253. if (cnt_cur != NULL)
  1254. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1255. if (bno_cur_lt != NULL)
  1256. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
  1257. if (bno_cur_gt != NULL)
  1258. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
  1259. return error;
  1260. }
  1261. /*
  1262. * Allocate a variable extent anywhere in the allocation group agno.
  1263. * Extent's length (returned in len) will be between minlen and maxlen,
  1264. * and of the form k * prod + mod unless there's nothing that large.
  1265. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  1266. */
  1267. STATIC int /* error */
  1268. xfs_alloc_ag_vextent_size(
  1269. xfs_alloc_arg_t *args) /* allocation argument structure */
  1270. {
  1271. xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
  1272. xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
  1273. int error; /* error result */
  1274. xfs_agblock_t fbno; /* start of found freespace */
  1275. xfs_extlen_t flen; /* length of found freespace */
  1276. int i; /* temp status variable */
  1277. xfs_agblock_t rbno; /* returned block number */
  1278. xfs_extlen_t rlen; /* length of returned extent */
  1279. bool busy;
  1280. unsigned busy_gen;
  1281. restart:
  1282. /*
  1283. * Allocate and initialize a cursor for the by-size btree.
  1284. */
  1285. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1286. args->agno, XFS_BTNUM_CNT);
  1287. bno_cur = NULL;
  1288. busy = false;
  1289. /*
  1290. * Look for an entry >= maxlen+alignment-1 blocks.
  1291. */
  1292. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
  1293. args->maxlen + args->alignment - 1, &i)))
  1294. goto error0;
  1295. /*
  1296. * If none then we have to settle for a smaller extent. In the case that
  1297. * there are no large extents, this will return the last entry in the
  1298. * tree unless the tree is empty. In the case that there are only busy
  1299. * large extents, this will return the largest small extent unless there
  1300. * are no smaller extents available.
  1301. */
  1302. if (!i) {
  1303. error = xfs_alloc_ag_vextent_small(args, cnt_cur,
  1304. &fbno, &flen, &i);
  1305. if (error)
  1306. goto error0;
  1307. if (i == 0 || flen == 0) {
  1308. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1309. trace_xfs_alloc_size_noentry(args);
  1310. return 0;
  1311. }
  1312. ASSERT(i == 1);
  1313. busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
  1314. &rlen, &busy_gen);
  1315. } else {
  1316. /*
  1317. * Search for a non-busy extent that is large enough.
  1318. */
  1319. for (;;) {
  1320. error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
  1321. if (error)
  1322. goto error0;
  1323. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1324. busy = xfs_alloc_compute_aligned(args, fbno, flen,
  1325. &rbno, &rlen, &busy_gen);
  1326. if (rlen >= args->maxlen)
  1327. break;
  1328. error = xfs_btree_increment(cnt_cur, 0, &i);
  1329. if (error)
  1330. goto error0;
  1331. if (i == 0) {
  1332. /*
  1333. * Our only valid extents must have been busy.
  1334. * Make it unbusy by forcing the log out and
  1335. * retrying.
  1336. */
  1337. xfs_btree_del_cursor(cnt_cur,
  1338. XFS_BTREE_NOERROR);
  1339. trace_xfs_alloc_size_busy(args);
  1340. xfs_extent_busy_flush(args->mp,
  1341. args->pag, busy_gen);
  1342. goto restart;
  1343. }
  1344. }
  1345. }
  1346. /*
  1347. * In the first case above, we got the last entry in the
  1348. * by-size btree. Now we check to see if the space hits maxlen
  1349. * once aligned; if not, we search left for something better.
  1350. * This can't happen in the second case above.
  1351. */
  1352. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1353. XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
  1354. (rlen <= flen && rbno + rlen <= fbno + flen), error0);
  1355. if (rlen < args->maxlen) {
  1356. xfs_agblock_t bestfbno;
  1357. xfs_extlen_t bestflen;
  1358. xfs_agblock_t bestrbno;
  1359. xfs_extlen_t bestrlen;
  1360. bestrlen = rlen;
  1361. bestrbno = rbno;
  1362. bestflen = flen;
  1363. bestfbno = fbno;
  1364. for (;;) {
  1365. if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
  1366. goto error0;
  1367. if (i == 0)
  1368. break;
  1369. if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
  1370. &i)))
  1371. goto error0;
  1372. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1373. if (flen < bestrlen)
  1374. break;
  1375. busy = xfs_alloc_compute_aligned(args, fbno, flen,
  1376. &rbno, &rlen, &busy_gen);
  1377. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1378. XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
  1379. (rlen <= flen && rbno + rlen <= fbno + flen),
  1380. error0);
  1381. if (rlen > bestrlen) {
  1382. bestrlen = rlen;
  1383. bestrbno = rbno;
  1384. bestflen = flen;
  1385. bestfbno = fbno;
  1386. if (rlen == args->maxlen)
  1387. break;
  1388. }
  1389. }
  1390. if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
  1391. &i)))
  1392. goto error0;
  1393. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1394. rlen = bestrlen;
  1395. rbno = bestrbno;
  1396. flen = bestflen;
  1397. fbno = bestfbno;
  1398. }
  1399. args->wasfromfl = 0;
  1400. /*
  1401. * Fix up the length.
  1402. */
  1403. args->len = rlen;
  1404. if (rlen < args->minlen) {
  1405. if (busy) {
  1406. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1407. trace_xfs_alloc_size_busy(args);
  1408. xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
  1409. goto restart;
  1410. }
  1411. goto out_nominleft;
  1412. }
  1413. xfs_alloc_fix_len(args);
  1414. rlen = args->len;
  1415. XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
  1416. /*
  1417. * Allocate and initialize a cursor for the by-block tree.
  1418. */
  1419. bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1420. args->agno, XFS_BTNUM_BNO);
  1421. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
  1422. rbno, rlen, XFSA_FIXUP_CNT_OK)))
  1423. goto error0;
  1424. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1425. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1426. cnt_cur = bno_cur = NULL;
  1427. args->len = rlen;
  1428. args->agbno = rbno;
  1429. XFS_WANT_CORRUPTED_GOTO(args->mp,
  1430. args->agbno + args->len <=
  1431. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
  1432. error0);
  1433. trace_xfs_alloc_size_done(args);
  1434. return 0;
  1435. error0:
  1436. trace_xfs_alloc_size_error(args);
  1437. if (cnt_cur)
  1438. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1439. if (bno_cur)
  1440. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1441. return error;
  1442. out_nominleft:
  1443. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1444. trace_xfs_alloc_size_nominleft(args);
  1445. args->agbno = NULLAGBLOCK;
  1446. return 0;
  1447. }
  1448. /*
  1449. * Deal with the case where only small freespaces remain.
  1450. * Either return the contents of the last freespace record,
  1451. * or allocate space from the freelist if there is nothing in the tree.
  1452. */
  1453. STATIC int /* error */
  1454. xfs_alloc_ag_vextent_small(
  1455. xfs_alloc_arg_t *args, /* allocation argument structure */
  1456. xfs_btree_cur_t *ccur, /* by-size cursor */
  1457. xfs_agblock_t *fbnop, /* result block number */
  1458. xfs_extlen_t *flenp, /* result length */
  1459. int *stat) /* status: 0-freelist, 1-normal/none */
  1460. {
  1461. struct xfs_owner_info oinfo;
  1462. int error;
  1463. xfs_agblock_t fbno;
  1464. xfs_extlen_t flen;
  1465. int i;
  1466. if ((error = xfs_btree_decrement(ccur, 0, &i)))
  1467. goto error0;
  1468. if (i) {
  1469. if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
  1470. goto error0;
  1471. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1472. }
  1473. /*
  1474. * Nothing in the btree, try the freelist. Make sure
  1475. * to respect minleft even when pulling from the
  1476. * freelist.
  1477. */
  1478. else if (args->minlen == 1 && args->alignment == 1 &&
  1479. args->resv != XFS_AG_RESV_AGFL &&
  1480. (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
  1481. > args->minleft)) {
  1482. error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
  1483. if (error)
  1484. goto error0;
  1485. if (fbno != NULLAGBLOCK) {
  1486. xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
  1487. xfs_alloc_allow_busy_reuse(args->datatype));
  1488. if (xfs_alloc_is_userdata(args->datatype)) {
  1489. xfs_buf_t *bp;
  1490. bp = xfs_btree_get_bufs(args->mp, args->tp,
  1491. args->agno, fbno, 0);
  1492. if (!bp) {
  1493. error = -EFSCORRUPTED;
  1494. goto error0;
  1495. }
  1496. xfs_trans_binval(args->tp, bp);
  1497. }
  1498. args->len = 1;
  1499. args->agbno = fbno;
  1500. XFS_WANT_CORRUPTED_GOTO(args->mp,
  1501. args->agbno + args->len <=
  1502. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
  1503. error0);
  1504. args->wasfromfl = 1;
  1505. trace_xfs_alloc_small_freelist(args);
  1506. /*
  1507. * If we're feeding an AGFL block to something that
  1508. * doesn't live in the free space, we need to clear
  1509. * out the OWN_AG rmap.
  1510. */
  1511. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
  1512. error = xfs_rmap_free(args->tp, args->agbp, args->agno,
  1513. fbno, 1, &oinfo);
  1514. if (error)
  1515. goto error0;
  1516. *stat = 0;
  1517. return 0;
  1518. }
  1519. /*
  1520. * Nothing in the freelist.
  1521. */
  1522. else
  1523. flen = 0;
  1524. }
  1525. /*
  1526. * Can't allocate from the freelist for some reason.
  1527. */
  1528. else {
  1529. fbno = NULLAGBLOCK;
  1530. flen = 0;
  1531. }
  1532. /*
  1533. * Can't do the allocation, give up.
  1534. */
  1535. if (flen < args->minlen) {
  1536. args->agbno = NULLAGBLOCK;
  1537. trace_xfs_alloc_small_notenough(args);
  1538. flen = 0;
  1539. }
  1540. *fbnop = fbno;
  1541. *flenp = flen;
  1542. *stat = 1;
  1543. trace_xfs_alloc_small_done(args);
  1544. return 0;
  1545. error0:
  1546. trace_xfs_alloc_small_error(args);
  1547. return error;
  1548. }
  1549. /*
  1550. * Free the extent starting at agno/bno for length.
  1551. */
  1552. STATIC int
  1553. xfs_free_ag_extent(
  1554. xfs_trans_t *tp,
  1555. xfs_buf_t *agbp,
  1556. xfs_agnumber_t agno,
  1557. xfs_agblock_t bno,
  1558. xfs_extlen_t len,
  1559. struct xfs_owner_info *oinfo,
  1560. enum xfs_ag_resv_type type)
  1561. {
  1562. xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
  1563. xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
  1564. int error; /* error return value */
  1565. xfs_agblock_t gtbno; /* start of right neighbor block */
  1566. xfs_extlen_t gtlen; /* length of right neighbor block */
  1567. int haveleft; /* have a left neighbor block */
  1568. int haveright; /* have a right neighbor block */
  1569. int i; /* temp, result code */
  1570. xfs_agblock_t ltbno; /* start of left neighbor block */
  1571. xfs_extlen_t ltlen; /* length of left neighbor block */
  1572. xfs_mount_t *mp; /* mount point struct for filesystem */
  1573. xfs_agblock_t nbno; /* new starting block of freespace */
  1574. xfs_extlen_t nlen; /* new length of freespace */
  1575. xfs_perag_t *pag; /* per allocation group data */
  1576. bno_cur = cnt_cur = NULL;
  1577. mp = tp->t_mountp;
  1578. if (!xfs_rmap_should_skip_owner_update(oinfo)) {
  1579. error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
  1580. if (error)
  1581. goto error0;
  1582. }
  1583. /*
  1584. * Allocate and initialize a cursor for the by-block btree.
  1585. */
  1586. bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
  1587. /*
  1588. * Look for a neighboring block on the left (lower block numbers)
  1589. * that is contiguous with this space.
  1590. */
  1591. if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
  1592. goto error0;
  1593. if (haveleft) {
  1594. /*
  1595. * There is a block to our left.
  1596. */
  1597. if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
  1598. goto error0;
  1599. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1600. /*
  1601. * It's not contiguous, though.
  1602. */
  1603. if (ltbno + ltlen < bno)
  1604. haveleft = 0;
  1605. else {
  1606. /*
  1607. * If this failure happens the request to free this
  1608. * space was invalid, it's (partly) already free.
  1609. * Very bad.
  1610. */
  1611. XFS_WANT_CORRUPTED_GOTO(mp,
  1612. ltbno + ltlen <= bno, error0);
  1613. }
  1614. }
  1615. /*
  1616. * Look for a neighboring block on the right (higher block numbers)
  1617. * that is contiguous with this space.
  1618. */
  1619. if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
  1620. goto error0;
  1621. if (haveright) {
  1622. /*
  1623. * There is a block to our right.
  1624. */
  1625. if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
  1626. goto error0;
  1627. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1628. /*
  1629. * It's not contiguous, though.
  1630. */
  1631. if (bno + len < gtbno)
  1632. haveright = 0;
  1633. else {
  1634. /*
  1635. * If this failure happens the request to free this
  1636. * space was invalid, it's (partly) already free.
  1637. * Very bad.
  1638. */
  1639. XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
  1640. }
  1641. }
  1642. /*
  1643. * Now allocate and initialize a cursor for the by-size tree.
  1644. */
  1645. cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
  1646. /*
  1647. * Have both left and right contiguous neighbors.
  1648. * Merge all three into a single free block.
  1649. */
  1650. if (haveleft && haveright) {
  1651. /*
  1652. * Delete the old by-size entry on the left.
  1653. */
  1654. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1655. goto error0;
  1656. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1657. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1658. goto error0;
  1659. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1660. /*
  1661. * Delete the old by-size entry on the right.
  1662. */
  1663. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1664. goto error0;
  1665. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1666. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1667. goto error0;
  1668. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1669. /*
  1670. * Delete the old by-block entry for the right block.
  1671. */
  1672. if ((error = xfs_btree_delete(bno_cur, &i)))
  1673. goto error0;
  1674. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1675. /*
  1676. * Move the by-block cursor back to the left neighbor.
  1677. */
  1678. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  1679. goto error0;
  1680. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1681. #ifdef DEBUG
  1682. /*
  1683. * Check that this is the right record: delete didn't
  1684. * mangle the cursor.
  1685. */
  1686. {
  1687. xfs_agblock_t xxbno;
  1688. xfs_extlen_t xxlen;
  1689. if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
  1690. &i)))
  1691. goto error0;
  1692. XFS_WANT_CORRUPTED_GOTO(mp,
  1693. i == 1 && xxbno == ltbno && xxlen == ltlen,
  1694. error0);
  1695. }
  1696. #endif
  1697. /*
  1698. * Update remaining by-block entry to the new, joined block.
  1699. */
  1700. nbno = ltbno;
  1701. nlen = len + ltlen + gtlen;
  1702. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1703. goto error0;
  1704. }
  1705. /*
  1706. * Have only a left contiguous neighbor.
  1707. * Merge it together with the new freespace.
  1708. */
  1709. else if (haveleft) {
  1710. /*
  1711. * Delete the old by-size entry on the left.
  1712. */
  1713. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1714. goto error0;
  1715. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1716. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1717. goto error0;
  1718. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1719. /*
  1720. * Back up the by-block cursor to the left neighbor, and
  1721. * update its length.
  1722. */
  1723. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  1724. goto error0;
  1725. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1726. nbno = ltbno;
  1727. nlen = len + ltlen;
  1728. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1729. goto error0;
  1730. }
  1731. /*
  1732. * Have only a right contiguous neighbor.
  1733. * Merge it together with the new freespace.
  1734. */
  1735. else if (haveright) {
  1736. /*
  1737. * Delete the old by-size entry on the right.
  1738. */
  1739. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1740. goto error0;
  1741. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1742. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1743. goto error0;
  1744. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1745. /*
  1746. * Update the starting block and length of the right
  1747. * neighbor in the by-block tree.
  1748. */
  1749. nbno = bno;
  1750. nlen = len + gtlen;
  1751. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1752. goto error0;
  1753. }
  1754. /*
  1755. * No contiguous neighbors.
  1756. * Insert the new freespace into the by-block tree.
  1757. */
  1758. else {
  1759. nbno = bno;
  1760. nlen = len;
  1761. if ((error = xfs_btree_insert(bno_cur, &i)))
  1762. goto error0;
  1763. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1764. }
  1765. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1766. bno_cur = NULL;
  1767. /*
  1768. * In all cases we need to insert the new freespace in the by-size tree.
  1769. */
  1770. if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
  1771. goto error0;
  1772. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
  1773. if ((error = xfs_btree_insert(cnt_cur, &i)))
  1774. goto error0;
  1775. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1776. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1777. cnt_cur = NULL;
  1778. /*
  1779. * Update the freespace totals in the ag and superblock.
  1780. */
  1781. pag = xfs_perag_get(mp, agno);
  1782. error = xfs_alloc_update_counters(tp, pag, agbp, len);
  1783. xfs_ag_resv_free_extent(pag, type, tp, len);
  1784. xfs_perag_put(pag);
  1785. if (error)
  1786. goto error0;
  1787. XFS_STATS_INC(mp, xs_freex);
  1788. XFS_STATS_ADD(mp, xs_freeb, len);
  1789. trace_xfs_free_extent(mp, agno, bno, len, type, haveleft, haveright);
  1790. return 0;
  1791. error0:
  1792. trace_xfs_free_extent(mp, agno, bno, len, type, -1, -1);
  1793. if (bno_cur)
  1794. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1795. if (cnt_cur)
  1796. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1797. return error;
  1798. }
  1799. /*
  1800. * Visible (exported) allocation/free functions.
  1801. * Some of these are used just by xfs_alloc_btree.c and this file.
  1802. */
  1803. /*
  1804. * Compute and fill in value of m_ag_maxlevels.
  1805. */
  1806. void
  1807. xfs_alloc_compute_maxlevels(
  1808. xfs_mount_t *mp) /* file system mount structure */
  1809. {
  1810. mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp->m_alloc_mnr,
  1811. (mp->m_sb.sb_agblocks + 1) / 2);
  1812. }
  1813. /*
  1814. * Find the length of the longest extent in an AG. The 'need' parameter
  1815. * specifies how much space we're going to need for the AGFL and the
  1816. * 'reserved' parameter tells us how many blocks in this AG are reserved for
  1817. * other callers.
  1818. */
  1819. xfs_extlen_t
  1820. xfs_alloc_longest_free_extent(
  1821. struct xfs_perag *pag,
  1822. xfs_extlen_t need,
  1823. xfs_extlen_t reserved)
  1824. {
  1825. xfs_extlen_t delta = 0;
  1826. /*
  1827. * If the AGFL needs a recharge, we'll have to subtract that from the
  1828. * longest extent.
  1829. */
  1830. if (need > pag->pagf_flcount)
  1831. delta = need - pag->pagf_flcount;
  1832. /*
  1833. * If we cannot maintain others' reservations with space from the
  1834. * not-longest freesp extents, we'll have to subtract /that/ from
  1835. * the longest extent too.
  1836. */
  1837. if (pag->pagf_freeblks - pag->pagf_longest < reserved)
  1838. delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
  1839. /*
  1840. * If the longest extent is long enough to satisfy all the
  1841. * reservations and AGFL rules in place, we can return this extent.
  1842. */
  1843. if (pag->pagf_longest > delta)
  1844. return pag->pagf_longest - delta;
  1845. /* Otherwise, let the caller try for 1 block if there's space. */
  1846. return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
  1847. }
  1848. unsigned int
  1849. xfs_alloc_min_freelist(
  1850. struct xfs_mount *mp,
  1851. struct xfs_perag *pag)
  1852. {
  1853. unsigned int min_free;
  1854. /* space needed by-bno freespace btree */
  1855. min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
  1856. mp->m_ag_maxlevels);
  1857. /* space needed by-size freespace btree */
  1858. min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
  1859. mp->m_ag_maxlevels);
  1860. /* space needed reverse mapping used space btree */
  1861. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  1862. min_free += min_t(unsigned int,
  1863. pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
  1864. mp->m_rmap_maxlevels);
  1865. return min_free;
  1866. }
  1867. /*
  1868. * Check if the operation we are fixing up the freelist for should go ahead or
  1869. * not. If we are freeing blocks, we always allow it, otherwise the allocation
  1870. * is dependent on whether the size and shape of free space available will
  1871. * permit the requested allocation to take place.
  1872. */
  1873. static bool
  1874. xfs_alloc_space_available(
  1875. struct xfs_alloc_arg *args,
  1876. xfs_extlen_t min_free,
  1877. int flags)
  1878. {
  1879. struct xfs_perag *pag = args->pag;
  1880. xfs_extlen_t alloc_len, longest;
  1881. xfs_extlen_t reservation; /* blocks that are still reserved */
  1882. int available;
  1883. if (flags & XFS_ALLOC_FLAG_FREEING)
  1884. return true;
  1885. reservation = xfs_ag_resv_needed(pag, args->resv);
  1886. /* do we have enough contiguous free space for the allocation? */
  1887. alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
  1888. longest = xfs_alloc_longest_free_extent(pag, min_free, reservation);
  1889. if (longest < alloc_len)
  1890. return false;
  1891. /* do we have enough free space remaining for the allocation? */
  1892. available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
  1893. reservation - min_free - args->minleft);
  1894. if (available < (int)max(args->total, alloc_len))
  1895. return false;
  1896. /*
  1897. * Clamp maxlen to the amount of free space available for the actual
  1898. * extent allocation.
  1899. */
  1900. if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
  1901. args->maxlen = available;
  1902. ASSERT(args->maxlen > 0);
  1903. ASSERT(args->maxlen >= args->minlen);
  1904. }
  1905. return true;
  1906. }
  1907. int
  1908. xfs_free_agfl_block(
  1909. struct xfs_trans *tp,
  1910. xfs_agnumber_t agno,
  1911. xfs_agblock_t agbno,
  1912. struct xfs_buf *agbp,
  1913. struct xfs_owner_info *oinfo)
  1914. {
  1915. int error;
  1916. struct xfs_buf *bp;
  1917. error = xfs_free_ag_extent(tp, agbp, agno, agbno, 1, oinfo,
  1918. XFS_AG_RESV_AGFL);
  1919. if (error)
  1920. return error;
  1921. bp = xfs_btree_get_bufs(tp->t_mountp, tp, agno, agbno, 0);
  1922. if (!bp)
  1923. return -EFSCORRUPTED;
  1924. xfs_trans_binval(tp, bp);
  1925. return 0;
  1926. }
  1927. /*
  1928. * Check the agfl fields of the agf for inconsistency or corruption. The purpose
  1929. * is to detect an agfl header padding mismatch between current and early v5
  1930. * kernels. This problem manifests as a 1-slot size difference between the
  1931. * on-disk flcount and the active [first, last] range of a wrapped agfl. This
  1932. * may also catch variants of agfl count corruption unrelated to padding. Either
  1933. * way, we'll reset the agfl and warn the user.
  1934. *
  1935. * Return true if a reset is required before the agfl can be used, false
  1936. * otherwise.
  1937. */
  1938. static bool
  1939. xfs_agfl_needs_reset(
  1940. struct xfs_mount *mp,
  1941. struct xfs_agf *agf)
  1942. {
  1943. uint32_t f = be32_to_cpu(agf->agf_flfirst);
  1944. uint32_t l = be32_to_cpu(agf->agf_fllast);
  1945. uint32_t c = be32_to_cpu(agf->agf_flcount);
  1946. int agfl_size = xfs_agfl_size(mp);
  1947. int active;
  1948. /* no agfl header on v4 supers */
  1949. if (!xfs_sb_version_hascrc(&mp->m_sb))
  1950. return false;
  1951. /*
  1952. * The agf read verifier catches severe corruption of these fields.
  1953. * Repeat some sanity checks to cover a packed -> unpacked mismatch if
  1954. * the verifier allows it.
  1955. */
  1956. if (f >= agfl_size || l >= agfl_size)
  1957. return true;
  1958. if (c > agfl_size)
  1959. return true;
  1960. /*
  1961. * Check consistency between the on-disk count and the active range. An
  1962. * agfl padding mismatch manifests as an inconsistent flcount.
  1963. */
  1964. if (c && l >= f)
  1965. active = l - f + 1;
  1966. else if (c)
  1967. active = agfl_size - f + l + 1;
  1968. else
  1969. active = 0;
  1970. return active != c;
  1971. }
  1972. /*
  1973. * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
  1974. * agfl content cannot be trusted. Warn the user that a repair is required to
  1975. * recover leaked blocks.
  1976. *
  1977. * The purpose of this mechanism is to handle filesystems affected by the agfl
  1978. * header padding mismatch problem. A reset keeps the filesystem online with a
  1979. * relatively minor free space accounting inconsistency rather than suffer the
  1980. * inevitable crash from use of an invalid agfl block.
  1981. */
  1982. static void
  1983. xfs_agfl_reset(
  1984. struct xfs_trans *tp,
  1985. struct xfs_buf *agbp,
  1986. struct xfs_perag *pag)
  1987. {
  1988. struct xfs_mount *mp = tp->t_mountp;
  1989. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  1990. ASSERT(pag->pagf_agflreset);
  1991. trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_);
  1992. xfs_warn(mp,
  1993. "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
  1994. "Please unmount and run xfs_repair.",
  1995. pag->pag_agno, pag->pagf_flcount);
  1996. agf->agf_flfirst = 0;
  1997. agf->agf_fllast = cpu_to_be32(xfs_agfl_size(mp) - 1);
  1998. agf->agf_flcount = 0;
  1999. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLLAST |
  2000. XFS_AGF_FLCOUNT);
  2001. pag->pagf_flcount = 0;
  2002. pag->pagf_agflreset = false;
  2003. }
  2004. /*
  2005. * Defer an AGFL block free. This is effectively equivalent to
  2006. * xfs_bmap_add_free() with some special handling particular to AGFL blocks.
  2007. *
  2008. * Deferring AGFL frees helps prevent log reservation overruns due to too many
  2009. * allocation operations in a transaction. AGFL frees are prone to this problem
  2010. * because for one they are always freed one at a time. Further, an immediate
  2011. * AGFL block free can cause a btree join and require another block free before
  2012. * the real allocation can proceed. Deferring the free disconnects freeing up
  2013. * the AGFL slot from freeing the block.
  2014. */
  2015. STATIC void
  2016. xfs_defer_agfl_block(
  2017. struct xfs_mount *mp,
  2018. struct xfs_defer_ops *dfops,
  2019. xfs_agnumber_t agno,
  2020. xfs_fsblock_t agbno,
  2021. struct xfs_owner_info *oinfo)
  2022. {
  2023. struct xfs_extent_free_item *new; /* new element */
  2024. ASSERT(xfs_bmap_free_item_zone != NULL);
  2025. ASSERT(oinfo != NULL);
  2026. new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
  2027. new->xefi_startblock = XFS_AGB_TO_FSB(mp, agno, agbno);
  2028. new->xefi_blockcount = 1;
  2029. new->xefi_oinfo = *oinfo;
  2030. trace_xfs_agfl_free_defer(mp, agno, 0, agbno, 1);
  2031. xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_AGFL_FREE, &new->xefi_list);
  2032. }
  2033. /*
  2034. * Decide whether to use this allocation group for this allocation.
  2035. * If so, fix up the btree freelist's size.
  2036. */
  2037. int /* error */
  2038. xfs_alloc_fix_freelist(
  2039. struct xfs_alloc_arg *args, /* allocation argument structure */
  2040. int flags) /* XFS_ALLOC_FLAG_... */
  2041. {
  2042. struct xfs_mount *mp = args->mp;
  2043. struct xfs_perag *pag = args->pag;
  2044. struct xfs_trans *tp = args->tp;
  2045. struct xfs_buf *agbp = NULL;
  2046. struct xfs_buf *agflbp = NULL;
  2047. struct xfs_alloc_arg targs; /* local allocation arguments */
  2048. xfs_agblock_t bno; /* freelist block */
  2049. xfs_extlen_t need; /* total blocks needed in freelist */
  2050. int error = 0;
  2051. if (!pag->pagf_init) {
  2052. error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
  2053. if (error)
  2054. goto out_no_agbp;
  2055. if (!pag->pagf_init) {
  2056. ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
  2057. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  2058. goto out_agbp_relse;
  2059. }
  2060. }
  2061. /*
  2062. * If this is a metadata preferred pag and we are user data then try
  2063. * somewhere else if we are not being asked to try harder at this
  2064. * point
  2065. */
  2066. if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
  2067. (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
  2068. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  2069. goto out_agbp_relse;
  2070. }
  2071. need = xfs_alloc_min_freelist(mp, pag);
  2072. if (!xfs_alloc_space_available(args, need, flags |
  2073. XFS_ALLOC_FLAG_CHECK))
  2074. goto out_agbp_relse;
  2075. /*
  2076. * Get the a.g. freespace buffer.
  2077. * Can fail if we're not blocking on locks, and it's held.
  2078. */
  2079. if (!agbp) {
  2080. error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
  2081. if (error)
  2082. goto out_no_agbp;
  2083. if (!agbp) {
  2084. ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
  2085. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  2086. goto out_no_agbp;
  2087. }
  2088. }
  2089. /* reset a padding mismatched agfl before final free space check */
  2090. if (pag->pagf_agflreset)
  2091. xfs_agfl_reset(tp, agbp, pag);
  2092. /* If there isn't enough total space or single-extent, reject it. */
  2093. need = xfs_alloc_min_freelist(mp, pag);
  2094. if (!xfs_alloc_space_available(args, need, flags))
  2095. goto out_agbp_relse;
  2096. /*
  2097. * Make the freelist shorter if it's too long.
  2098. *
  2099. * Note that from this point onwards, we will always release the agf and
  2100. * agfl buffers on error. This handles the case where we error out and
  2101. * the buffers are clean or may not have been joined to the transaction
  2102. * and hence need to be released manually. If they have been joined to
  2103. * the transaction, then xfs_trans_brelse() will handle them
  2104. * appropriately based on the recursion count and dirty state of the
  2105. * buffer.
  2106. *
  2107. * XXX (dgc): When we have lots of free space, does this buy us
  2108. * anything other than extra overhead when we need to put more blocks
  2109. * back on the free list? Maybe we should only do this when space is
  2110. * getting low or the AGFL is more than half full?
  2111. *
  2112. * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
  2113. * big; the NORMAP flag prevents AGFL expand/shrink operations from
  2114. * updating the rmapbt. Both flags are used in xfs_repair while we're
  2115. * rebuilding the rmapbt, and neither are used by the kernel. They're
  2116. * both required to ensure that rmaps are correctly recorded for the
  2117. * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
  2118. * repair/rmap.c in xfsprogs for details.
  2119. */
  2120. memset(&targs, 0, sizeof(targs));
  2121. if (flags & XFS_ALLOC_FLAG_NORMAP)
  2122. xfs_rmap_skip_owner_update(&targs.oinfo);
  2123. else
  2124. xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
  2125. while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
  2126. error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
  2127. if (error)
  2128. goto out_agbp_relse;
  2129. /* defer agfl frees if dfops is provided */
  2130. if (tp->t_agfl_dfops) {
  2131. xfs_defer_agfl_block(mp, tp->t_agfl_dfops, args->agno,
  2132. bno, &targs.oinfo);
  2133. } else {
  2134. error = xfs_free_agfl_block(tp, args->agno, bno, agbp,
  2135. &targs.oinfo);
  2136. if (error)
  2137. goto out_agbp_relse;
  2138. }
  2139. }
  2140. targs.tp = tp;
  2141. targs.mp = mp;
  2142. targs.agbp = agbp;
  2143. targs.agno = args->agno;
  2144. targs.alignment = targs.minlen = targs.prod = 1;
  2145. targs.type = XFS_ALLOCTYPE_THIS_AG;
  2146. targs.pag = pag;
  2147. error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
  2148. if (error)
  2149. goto out_agbp_relse;
  2150. /* Make the freelist longer if it's too short. */
  2151. while (pag->pagf_flcount < need) {
  2152. targs.agbno = 0;
  2153. targs.maxlen = need - pag->pagf_flcount;
  2154. targs.resv = XFS_AG_RESV_AGFL;
  2155. /* Allocate as many blocks as possible at once. */
  2156. error = xfs_alloc_ag_vextent(&targs);
  2157. if (error)
  2158. goto out_agflbp_relse;
  2159. /*
  2160. * Stop if we run out. Won't happen if callers are obeying
  2161. * the restrictions correctly. Can happen for free calls
  2162. * on a completely full ag.
  2163. */
  2164. if (targs.agbno == NULLAGBLOCK) {
  2165. if (flags & XFS_ALLOC_FLAG_FREEING)
  2166. break;
  2167. goto out_agflbp_relse;
  2168. }
  2169. /*
  2170. * Put each allocated block on the list.
  2171. */
  2172. for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
  2173. error = xfs_alloc_put_freelist(tp, agbp,
  2174. agflbp, bno, 0);
  2175. if (error)
  2176. goto out_agflbp_relse;
  2177. }
  2178. }
  2179. xfs_trans_brelse(tp, agflbp);
  2180. args->agbp = agbp;
  2181. return 0;
  2182. out_agflbp_relse:
  2183. xfs_trans_brelse(tp, agflbp);
  2184. out_agbp_relse:
  2185. if (agbp)
  2186. xfs_trans_brelse(tp, agbp);
  2187. out_no_agbp:
  2188. args->agbp = NULL;
  2189. return error;
  2190. }
  2191. /*
  2192. * Get a block from the freelist.
  2193. * Returns with the buffer for the block gotten.
  2194. */
  2195. int /* error */
  2196. xfs_alloc_get_freelist(
  2197. xfs_trans_t *tp, /* transaction pointer */
  2198. xfs_buf_t *agbp, /* buffer containing the agf structure */
  2199. xfs_agblock_t *bnop, /* block address retrieved from freelist */
  2200. int btreeblk) /* destination is a AGF btree */
  2201. {
  2202. xfs_agf_t *agf; /* a.g. freespace structure */
  2203. xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
  2204. xfs_agblock_t bno; /* block number returned */
  2205. __be32 *agfl_bno;
  2206. int error;
  2207. int logflags;
  2208. xfs_mount_t *mp = tp->t_mountp;
  2209. xfs_perag_t *pag; /* per allocation group data */
  2210. /*
  2211. * Freelist is empty, give up.
  2212. */
  2213. agf = XFS_BUF_TO_AGF(agbp);
  2214. if (!agf->agf_flcount) {
  2215. *bnop = NULLAGBLOCK;
  2216. return 0;
  2217. }
  2218. /*
  2219. * Read the array of free blocks.
  2220. */
  2221. error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
  2222. &agflbp);
  2223. if (error)
  2224. return error;
  2225. /*
  2226. * Get the block number and update the data structures.
  2227. */
  2228. agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
  2229. bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
  2230. be32_add_cpu(&agf->agf_flfirst, 1);
  2231. xfs_trans_brelse(tp, agflbp);
  2232. if (be32_to_cpu(agf->agf_flfirst) == xfs_agfl_size(mp))
  2233. agf->agf_flfirst = 0;
  2234. pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
  2235. ASSERT(!pag->pagf_agflreset);
  2236. be32_add_cpu(&agf->agf_flcount, -1);
  2237. xfs_trans_agflist_delta(tp, -1);
  2238. pag->pagf_flcount--;
  2239. xfs_perag_put(pag);
  2240. logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
  2241. if (btreeblk) {
  2242. be32_add_cpu(&agf->agf_btreeblks, 1);
  2243. pag->pagf_btreeblks++;
  2244. logflags |= XFS_AGF_BTREEBLKS;
  2245. }
  2246. xfs_alloc_log_agf(tp, agbp, logflags);
  2247. *bnop = bno;
  2248. return 0;
  2249. }
  2250. /*
  2251. * Log the given fields from the agf structure.
  2252. */
  2253. void
  2254. xfs_alloc_log_agf(
  2255. xfs_trans_t *tp, /* transaction pointer */
  2256. xfs_buf_t *bp, /* buffer for a.g. freelist header */
  2257. int fields) /* mask of fields to be logged (XFS_AGF_...) */
  2258. {
  2259. int first; /* first byte offset */
  2260. int last; /* last byte offset */
  2261. static const short offsets[] = {
  2262. offsetof(xfs_agf_t, agf_magicnum),
  2263. offsetof(xfs_agf_t, agf_versionnum),
  2264. offsetof(xfs_agf_t, agf_seqno),
  2265. offsetof(xfs_agf_t, agf_length),
  2266. offsetof(xfs_agf_t, agf_roots[0]),
  2267. offsetof(xfs_agf_t, agf_levels[0]),
  2268. offsetof(xfs_agf_t, agf_flfirst),
  2269. offsetof(xfs_agf_t, agf_fllast),
  2270. offsetof(xfs_agf_t, agf_flcount),
  2271. offsetof(xfs_agf_t, agf_freeblks),
  2272. offsetof(xfs_agf_t, agf_longest),
  2273. offsetof(xfs_agf_t, agf_btreeblks),
  2274. offsetof(xfs_agf_t, agf_uuid),
  2275. offsetof(xfs_agf_t, agf_rmap_blocks),
  2276. offsetof(xfs_agf_t, agf_refcount_blocks),
  2277. offsetof(xfs_agf_t, agf_refcount_root),
  2278. offsetof(xfs_agf_t, agf_refcount_level),
  2279. /* needed so that we don't log the whole rest of the structure: */
  2280. offsetof(xfs_agf_t, agf_spare64),
  2281. sizeof(xfs_agf_t)
  2282. };
  2283. trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
  2284. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
  2285. xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
  2286. xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
  2287. }
  2288. /*
  2289. * Interface for inode allocation to force the pag data to be initialized.
  2290. */
  2291. int /* error */
  2292. xfs_alloc_pagf_init(
  2293. xfs_mount_t *mp, /* file system mount structure */
  2294. xfs_trans_t *tp, /* transaction pointer */
  2295. xfs_agnumber_t agno, /* allocation group number */
  2296. int flags) /* XFS_ALLOC_FLAGS_... */
  2297. {
  2298. xfs_buf_t *bp;
  2299. int error;
  2300. if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
  2301. return error;
  2302. if (bp)
  2303. xfs_trans_brelse(tp, bp);
  2304. return 0;
  2305. }
  2306. /*
  2307. * Put the block on the freelist for the allocation group.
  2308. */
  2309. int /* error */
  2310. xfs_alloc_put_freelist(
  2311. xfs_trans_t *tp, /* transaction pointer */
  2312. xfs_buf_t *agbp, /* buffer for a.g. freelist header */
  2313. xfs_buf_t *agflbp,/* buffer for a.g. free block array */
  2314. xfs_agblock_t bno, /* block being freed */
  2315. int btreeblk) /* block came from a AGF btree */
  2316. {
  2317. xfs_agf_t *agf; /* a.g. freespace structure */
  2318. __be32 *blockp;/* pointer to array entry */
  2319. int error;
  2320. int logflags;
  2321. xfs_mount_t *mp; /* mount structure */
  2322. xfs_perag_t *pag; /* per allocation group data */
  2323. __be32 *agfl_bno;
  2324. int startoff;
  2325. agf = XFS_BUF_TO_AGF(agbp);
  2326. mp = tp->t_mountp;
  2327. if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
  2328. be32_to_cpu(agf->agf_seqno), &agflbp)))
  2329. return error;
  2330. be32_add_cpu(&agf->agf_fllast, 1);
  2331. if (be32_to_cpu(agf->agf_fllast) == xfs_agfl_size(mp))
  2332. agf->agf_fllast = 0;
  2333. pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
  2334. ASSERT(!pag->pagf_agflreset);
  2335. be32_add_cpu(&agf->agf_flcount, 1);
  2336. xfs_trans_agflist_delta(tp, 1);
  2337. pag->pagf_flcount++;
  2338. logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
  2339. if (btreeblk) {
  2340. be32_add_cpu(&agf->agf_btreeblks, -1);
  2341. pag->pagf_btreeblks--;
  2342. logflags |= XFS_AGF_BTREEBLKS;
  2343. }
  2344. xfs_perag_put(pag);
  2345. xfs_alloc_log_agf(tp, agbp, logflags);
  2346. ASSERT(be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp));
  2347. agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
  2348. blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
  2349. *blockp = cpu_to_be32(bno);
  2350. startoff = (char *)blockp - (char *)agflbp->b_addr;
  2351. xfs_alloc_log_agf(tp, agbp, logflags);
  2352. xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
  2353. xfs_trans_log_buf(tp, agflbp, startoff,
  2354. startoff + sizeof(xfs_agblock_t) - 1);
  2355. return 0;
  2356. }
  2357. static xfs_failaddr_t
  2358. xfs_agf_verify(
  2359. struct xfs_buf *bp)
  2360. {
  2361. struct xfs_mount *mp = bp->b_target->bt_mount;
  2362. struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
  2363. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  2364. if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
  2365. return __this_address;
  2366. if (!xfs_log_check_lsn(mp,
  2367. be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
  2368. return __this_address;
  2369. }
  2370. if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
  2371. XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
  2372. be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
  2373. be32_to_cpu(agf->agf_flfirst) < xfs_agfl_size(mp) &&
  2374. be32_to_cpu(agf->agf_fllast) < xfs_agfl_size(mp) &&
  2375. be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp)))
  2376. return __this_address;
  2377. if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
  2378. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
  2379. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
  2380. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
  2381. return __this_address;
  2382. if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
  2383. (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
  2384. be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS))
  2385. return __this_address;
  2386. /*
  2387. * during growfs operations, the perag is not fully initialised,
  2388. * so we can't use it for any useful checking. growfs ensures we can't
  2389. * use it by using uncached buffers that don't have the perag attached
  2390. * so we can detect and avoid this problem.
  2391. */
  2392. if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
  2393. return __this_address;
  2394. if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
  2395. be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
  2396. return __this_address;
  2397. if (xfs_sb_version_hasreflink(&mp->m_sb) &&
  2398. (be32_to_cpu(agf->agf_refcount_level) < 1 ||
  2399. be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS))
  2400. return __this_address;
  2401. return NULL;
  2402. }
  2403. static void
  2404. xfs_agf_read_verify(
  2405. struct xfs_buf *bp)
  2406. {
  2407. struct xfs_mount *mp = bp->b_target->bt_mount;
  2408. xfs_failaddr_t fa;
  2409. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  2410. !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
  2411. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  2412. else {
  2413. fa = xfs_agf_verify(bp);
  2414. if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_ALLOC_READ_AGF))
  2415. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  2416. }
  2417. }
  2418. static void
  2419. xfs_agf_write_verify(
  2420. struct xfs_buf *bp)
  2421. {
  2422. struct xfs_mount *mp = bp->b_target->bt_mount;
  2423. struct xfs_buf_log_item *bip = bp->b_log_item;
  2424. xfs_failaddr_t fa;
  2425. fa = xfs_agf_verify(bp);
  2426. if (fa) {
  2427. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  2428. return;
  2429. }
  2430. if (!xfs_sb_version_hascrc(&mp->m_sb))
  2431. return;
  2432. if (bip)
  2433. XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  2434. xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
  2435. }
  2436. const struct xfs_buf_ops xfs_agf_buf_ops = {
  2437. .name = "xfs_agf",
  2438. .verify_read = xfs_agf_read_verify,
  2439. .verify_write = xfs_agf_write_verify,
  2440. .verify_struct = xfs_agf_verify,
  2441. };
  2442. /*
  2443. * Read in the allocation group header (free/alloc section).
  2444. */
  2445. int /* error */
  2446. xfs_read_agf(
  2447. struct xfs_mount *mp, /* mount point structure */
  2448. struct xfs_trans *tp, /* transaction pointer */
  2449. xfs_agnumber_t agno, /* allocation group number */
  2450. int flags, /* XFS_BUF_ */
  2451. struct xfs_buf **bpp) /* buffer for the ag freelist header */
  2452. {
  2453. int error;
  2454. trace_xfs_read_agf(mp, agno);
  2455. ASSERT(agno != NULLAGNUMBER);
  2456. error = xfs_trans_read_buf(
  2457. mp, tp, mp->m_ddev_targp,
  2458. XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
  2459. XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
  2460. if (error)
  2461. return error;
  2462. if (!*bpp)
  2463. return 0;
  2464. ASSERT(!(*bpp)->b_error);
  2465. xfs_buf_set_ref(*bpp, XFS_AGF_REF);
  2466. return 0;
  2467. }
  2468. /*
  2469. * Read in the allocation group header (free/alloc section).
  2470. */
  2471. int /* error */
  2472. xfs_alloc_read_agf(
  2473. struct xfs_mount *mp, /* mount point structure */
  2474. struct xfs_trans *tp, /* transaction pointer */
  2475. xfs_agnumber_t agno, /* allocation group number */
  2476. int flags, /* XFS_ALLOC_FLAG_... */
  2477. struct xfs_buf **bpp) /* buffer for the ag freelist header */
  2478. {
  2479. struct xfs_agf *agf; /* ag freelist header */
  2480. struct xfs_perag *pag; /* per allocation group data */
  2481. int error;
  2482. trace_xfs_alloc_read_agf(mp, agno);
  2483. ASSERT(agno != NULLAGNUMBER);
  2484. error = xfs_read_agf(mp, tp, agno,
  2485. (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
  2486. bpp);
  2487. if (error)
  2488. return error;
  2489. if (!*bpp)
  2490. return 0;
  2491. ASSERT(!(*bpp)->b_error);
  2492. agf = XFS_BUF_TO_AGF(*bpp);
  2493. pag = xfs_perag_get(mp, agno);
  2494. if (!pag->pagf_init) {
  2495. pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
  2496. pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
  2497. pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
  2498. pag->pagf_longest = be32_to_cpu(agf->agf_longest);
  2499. pag->pagf_levels[XFS_BTNUM_BNOi] =
  2500. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
  2501. pag->pagf_levels[XFS_BTNUM_CNTi] =
  2502. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
  2503. pag->pagf_levels[XFS_BTNUM_RMAPi] =
  2504. be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
  2505. pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
  2506. spin_lock_init(&pag->pagb_lock);
  2507. pag->pagb_count = 0;
  2508. pag->pagb_tree = RB_ROOT;
  2509. pag->pagf_init = 1;
  2510. pag->pagf_agflreset = xfs_agfl_needs_reset(mp, agf);
  2511. }
  2512. #ifdef DEBUG
  2513. else if (!XFS_FORCED_SHUTDOWN(mp)) {
  2514. ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
  2515. ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
  2516. ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
  2517. ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
  2518. ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
  2519. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
  2520. ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
  2521. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
  2522. }
  2523. #endif
  2524. xfs_perag_put(pag);
  2525. return 0;
  2526. }
  2527. /*
  2528. * Allocate an extent (variable-size).
  2529. * Depending on the allocation type, we either look in a single allocation
  2530. * group or loop over the allocation groups to find the result.
  2531. */
  2532. int /* error */
  2533. xfs_alloc_vextent(
  2534. xfs_alloc_arg_t *args) /* allocation argument structure */
  2535. {
  2536. xfs_agblock_t agsize; /* allocation group size */
  2537. int error;
  2538. int flags; /* XFS_ALLOC_FLAG_... locking flags */
  2539. xfs_mount_t *mp; /* mount structure pointer */
  2540. xfs_agnumber_t sagno; /* starting allocation group number */
  2541. xfs_alloctype_t type; /* input allocation type */
  2542. int bump_rotor = 0;
  2543. xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
  2544. mp = args->mp;
  2545. type = args->otype = args->type;
  2546. args->agbno = NULLAGBLOCK;
  2547. /*
  2548. * Just fix this up, for the case where the last a.g. is shorter
  2549. * (or there's only one a.g.) and the caller couldn't easily figure
  2550. * that out (xfs_bmap_alloc).
  2551. */
  2552. agsize = mp->m_sb.sb_agblocks;
  2553. if (args->maxlen > agsize)
  2554. args->maxlen = agsize;
  2555. if (args->alignment == 0)
  2556. args->alignment = 1;
  2557. ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
  2558. ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
  2559. ASSERT(args->minlen <= args->maxlen);
  2560. ASSERT(args->minlen <= agsize);
  2561. ASSERT(args->mod < args->prod);
  2562. if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
  2563. XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
  2564. args->minlen > args->maxlen || args->minlen > agsize ||
  2565. args->mod >= args->prod) {
  2566. args->fsbno = NULLFSBLOCK;
  2567. trace_xfs_alloc_vextent_badargs(args);
  2568. return 0;
  2569. }
  2570. switch (type) {
  2571. case XFS_ALLOCTYPE_THIS_AG:
  2572. case XFS_ALLOCTYPE_NEAR_BNO:
  2573. case XFS_ALLOCTYPE_THIS_BNO:
  2574. /*
  2575. * These three force us into a single a.g.
  2576. */
  2577. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2578. args->pag = xfs_perag_get(mp, args->agno);
  2579. error = xfs_alloc_fix_freelist(args, 0);
  2580. if (error) {
  2581. trace_xfs_alloc_vextent_nofix(args);
  2582. goto error0;
  2583. }
  2584. if (!args->agbp) {
  2585. trace_xfs_alloc_vextent_noagbp(args);
  2586. break;
  2587. }
  2588. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2589. if ((error = xfs_alloc_ag_vextent(args)))
  2590. goto error0;
  2591. break;
  2592. case XFS_ALLOCTYPE_START_BNO:
  2593. /*
  2594. * Try near allocation first, then anywhere-in-ag after
  2595. * the first a.g. fails.
  2596. */
  2597. if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
  2598. (mp->m_flags & XFS_MOUNT_32BITINODES)) {
  2599. args->fsbno = XFS_AGB_TO_FSB(mp,
  2600. ((mp->m_agfrotor / rotorstep) %
  2601. mp->m_sb.sb_agcount), 0);
  2602. bump_rotor = 1;
  2603. }
  2604. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2605. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2606. /* FALLTHROUGH */
  2607. case XFS_ALLOCTYPE_FIRST_AG:
  2608. /*
  2609. * Rotate through the allocation groups looking for a winner.
  2610. */
  2611. if (type == XFS_ALLOCTYPE_FIRST_AG) {
  2612. /*
  2613. * Start with allocation group given by bno.
  2614. */
  2615. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2616. args->type = XFS_ALLOCTYPE_THIS_AG;
  2617. sagno = 0;
  2618. flags = 0;
  2619. } else {
  2620. /*
  2621. * Start with the given allocation group.
  2622. */
  2623. args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2624. flags = XFS_ALLOC_FLAG_TRYLOCK;
  2625. }
  2626. /*
  2627. * Loop over allocation groups twice; first time with
  2628. * trylock set, second time without.
  2629. */
  2630. for (;;) {
  2631. args->pag = xfs_perag_get(mp, args->agno);
  2632. error = xfs_alloc_fix_freelist(args, flags);
  2633. if (error) {
  2634. trace_xfs_alloc_vextent_nofix(args);
  2635. goto error0;
  2636. }
  2637. /*
  2638. * If we get a buffer back then the allocation will fly.
  2639. */
  2640. if (args->agbp) {
  2641. if ((error = xfs_alloc_ag_vextent(args)))
  2642. goto error0;
  2643. break;
  2644. }
  2645. trace_xfs_alloc_vextent_loopfailed(args);
  2646. /*
  2647. * Didn't work, figure out the next iteration.
  2648. */
  2649. if (args->agno == sagno &&
  2650. type == XFS_ALLOCTYPE_START_BNO)
  2651. args->type = XFS_ALLOCTYPE_THIS_AG;
  2652. /*
  2653. * For the first allocation, we can try any AG to get
  2654. * space. However, if we already have allocated a
  2655. * block, we don't want to try AGs whose number is below
  2656. * sagno. Otherwise, we may end up with out-of-order
  2657. * locking of AGF, which might cause deadlock.
  2658. */
  2659. if (++(args->agno) == mp->m_sb.sb_agcount) {
  2660. if (args->firstblock != NULLFSBLOCK)
  2661. args->agno = sagno;
  2662. else
  2663. args->agno = 0;
  2664. }
  2665. /*
  2666. * Reached the starting a.g., must either be done
  2667. * or switch to non-trylock mode.
  2668. */
  2669. if (args->agno == sagno) {
  2670. if (flags == 0) {
  2671. args->agbno = NULLAGBLOCK;
  2672. trace_xfs_alloc_vextent_allfailed(args);
  2673. break;
  2674. }
  2675. flags = 0;
  2676. if (type == XFS_ALLOCTYPE_START_BNO) {
  2677. args->agbno = XFS_FSB_TO_AGBNO(mp,
  2678. args->fsbno);
  2679. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2680. }
  2681. }
  2682. xfs_perag_put(args->pag);
  2683. }
  2684. if (bump_rotor) {
  2685. if (args->agno == sagno)
  2686. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  2687. (mp->m_sb.sb_agcount * rotorstep);
  2688. else
  2689. mp->m_agfrotor = (args->agno * rotorstep + 1) %
  2690. (mp->m_sb.sb_agcount * rotorstep);
  2691. }
  2692. break;
  2693. default:
  2694. ASSERT(0);
  2695. /* NOTREACHED */
  2696. }
  2697. if (args->agbno == NULLAGBLOCK)
  2698. args->fsbno = NULLFSBLOCK;
  2699. else {
  2700. args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
  2701. #ifdef DEBUG
  2702. ASSERT(args->len >= args->minlen);
  2703. ASSERT(args->len <= args->maxlen);
  2704. ASSERT(args->agbno % args->alignment == 0);
  2705. XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
  2706. args->len);
  2707. #endif
  2708. /* Zero the extent if we were asked to do so */
  2709. if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
  2710. error = xfs_zero_extent(args->ip, args->fsbno, args->len);
  2711. if (error)
  2712. goto error0;
  2713. }
  2714. }
  2715. xfs_perag_put(args->pag);
  2716. return 0;
  2717. error0:
  2718. xfs_perag_put(args->pag);
  2719. return error;
  2720. }
  2721. /* Ensure that the freelist is at full capacity. */
  2722. int
  2723. xfs_free_extent_fix_freelist(
  2724. struct xfs_trans *tp,
  2725. xfs_agnumber_t agno,
  2726. struct xfs_buf **agbp)
  2727. {
  2728. struct xfs_alloc_arg args;
  2729. int error;
  2730. memset(&args, 0, sizeof(struct xfs_alloc_arg));
  2731. args.tp = tp;
  2732. args.mp = tp->t_mountp;
  2733. args.agno = agno;
  2734. /*
  2735. * validate that the block number is legal - the enables us to detect
  2736. * and handle a silent filesystem corruption rather than crashing.
  2737. */
  2738. if (args.agno >= args.mp->m_sb.sb_agcount)
  2739. return -EFSCORRUPTED;
  2740. args.pag = xfs_perag_get(args.mp, args.agno);
  2741. ASSERT(args.pag);
  2742. error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
  2743. if (error)
  2744. goto out;
  2745. *agbp = args.agbp;
  2746. out:
  2747. xfs_perag_put(args.pag);
  2748. return error;
  2749. }
  2750. /*
  2751. * Free an extent.
  2752. * Just break up the extent address and hand off to xfs_free_ag_extent
  2753. * after fixing up the freelist.
  2754. */
  2755. int /* error */
  2756. __xfs_free_extent(
  2757. struct xfs_trans *tp, /* transaction pointer */
  2758. xfs_fsblock_t bno, /* starting block number of extent */
  2759. xfs_extlen_t len, /* length of extent */
  2760. struct xfs_owner_info *oinfo, /* extent owner */
  2761. enum xfs_ag_resv_type type, /* block reservation type */
  2762. bool skip_discard)
  2763. {
  2764. struct xfs_mount *mp = tp->t_mountp;
  2765. struct xfs_buf *agbp;
  2766. xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
  2767. xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
  2768. int error;
  2769. unsigned int busy_flags = 0;
  2770. ASSERT(len != 0);
  2771. ASSERT(type != XFS_AG_RESV_AGFL);
  2772. if (XFS_TEST_ERROR(false, mp,
  2773. XFS_ERRTAG_FREE_EXTENT))
  2774. return -EIO;
  2775. error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
  2776. if (error)
  2777. return error;
  2778. XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
  2779. /* validate the extent size is legal now we have the agf locked */
  2780. XFS_WANT_CORRUPTED_GOTO(mp,
  2781. agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
  2782. err);
  2783. error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
  2784. if (error)
  2785. goto err;
  2786. if (skip_discard)
  2787. busy_flags |= XFS_EXTENT_BUSY_SKIP_DISCARD;
  2788. xfs_extent_busy_insert(tp, agno, agbno, len, busy_flags);
  2789. return 0;
  2790. err:
  2791. xfs_trans_brelse(tp, agbp);
  2792. return error;
  2793. }
  2794. struct xfs_alloc_query_range_info {
  2795. xfs_alloc_query_range_fn fn;
  2796. void *priv;
  2797. };
  2798. /* Format btree record and pass to our callback. */
  2799. STATIC int
  2800. xfs_alloc_query_range_helper(
  2801. struct xfs_btree_cur *cur,
  2802. union xfs_btree_rec *rec,
  2803. void *priv)
  2804. {
  2805. struct xfs_alloc_query_range_info *query = priv;
  2806. struct xfs_alloc_rec_incore irec;
  2807. irec.ar_startblock = be32_to_cpu(rec->alloc.ar_startblock);
  2808. irec.ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount);
  2809. return query->fn(cur, &irec, query->priv);
  2810. }
  2811. /* Find all free space within a given range of blocks. */
  2812. int
  2813. xfs_alloc_query_range(
  2814. struct xfs_btree_cur *cur,
  2815. struct xfs_alloc_rec_incore *low_rec,
  2816. struct xfs_alloc_rec_incore *high_rec,
  2817. xfs_alloc_query_range_fn fn,
  2818. void *priv)
  2819. {
  2820. union xfs_btree_irec low_brec;
  2821. union xfs_btree_irec high_brec;
  2822. struct xfs_alloc_query_range_info query;
  2823. ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
  2824. low_brec.a = *low_rec;
  2825. high_brec.a = *high_rec;
  2826. query.priv = priv;
  2827. query.fn = fn;
  2828. return xfs_btree_query_range(cur, &low_brec, &high_brec,
  2829. xfs_alloc_query_range_helper, &query);
  2830. }
  2831. /* Find all free space records. */
  2832. int
  2833. xfs_alloc_query_all(
  2834. struct xfs_btree_cur *cur,
  2835. xfs_alloc_query_range_fn fn,
  2836. void *priv)
  2837. {
  2838. struct xfs_alloc_query_range_info query;
  2839. ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
  2840. query.priv = priv;
  2841. query.fn = fn;
  2842. return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
  2843. }
  2844. /* Is there a record covering a given extent? */
  2845. int
  2846. xfs_alloc_has_record(
  2847. struct xfs_btree_cur *cur,
  2848. xfs_agblock_t bno,
  2849. xfs_extlen_t len,
  2850. bool *exists)
  2851. {
  2852. union xfs_btree_irec low;
  2853. union xfs_btree_irec high;
  2854. memset(&low, 0, sizeof(low));
  2855. low.a.ar_startblock = bno;
  2856. memset(&high, 0xFF, sizeof(high));
  2857. high.a.ar_startblock = bno + len - 1;
  2858. return xfs_btree_has_record(cur, &low, &high, exists);
  2859. }
  2860. /*
  2861. * Walk all the blocks in the AGFL. The @walk_fn can return any negative
  2862. * error code or XFS_BTREE_QUERY_RANGE_ABORT.
  2863. */
  2864. int
  2865. xfs_agfl_walk(
  2866. struct xfs_mount *mp,
  2867. struct xfs_agf *agf,
  2868. struct xfs_buf *agflbp,
  2869. xfs_agfl_walk_fn walk_fn,
  2870. void *priv)
  2871. {
  2872. __be32 *agfl_bno;
  2873. unsigned int i;
  2874. int error;
  2875. agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
  2876. i = be32_to_cpu(agf->agf_flfirst);
  2877. /* Nothing to walk in an empty AGFL. */
  2878. if (agf->agf_flcount == cpu_to_be32(0))
  2879. return 0;
  2880. /* Otherwise, walk from first to last, wrapping as needed. */
  2881. for (;;) {
  2882. error = walk_fn(mp, be32_to_cpu(agfl_bno[i]), priv);
  2883. if (error)
  2884. return error;
  2885. if (i == be32_to_cpu(agf->agf_fllast))
  2886. break;
  2887. if (++i == xfs_agfl_size(mp))
  2888. i = 0;
  2889. }
  2890. return 0;
  2891. }