xfs_alloc.c 74 KB

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