xfs_alloc.c 72 KB

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