xfs_alloc.c 78 KB

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