xfs_alloc.c 82 KB

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