xfs_alloc.c 74 KB

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