xfs_alloc.c 78 KB

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