xfs_alloc.c 71 KB

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