xfs_alloc.c 77 KB

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