xfs_alloc.c 75 KB

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