xfs_alloc.c 76 KB

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