xfs_alloc.c 79 KB

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