xfs_alloc.c 74 KB

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