xfs_alloc.c 81 KB

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