xfs_alloc.c 79 KB

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