xfs_alloc.c 80 KB

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