xfs_alloc.c 69 KB

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