xfs_alloc.c 81 KB

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