xfs_alloc.c 74 KB

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