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