xfs_alloc.c 76 KB

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